2
0
mirror of https://github.com/esiur/esiur-dotnet.git synced 2025-06-27 05:23:13 +00:00

Add project files.

This commit is contained in:
Ahmed Zamil
2017-09-15 23:40:03 +03:00
parent 4c95cb1cc6
commit 7ae722ab51
99 changed files with 14687 additions and 0 deletions

64
Esiur/Engine/AsyncBag.cs Normal file
View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Esiur.Engine
{
public class AsyncBag<T>:AsyncReply
{
//List<AsyncReply> replies = new List<AsyncReply>();
//List<T> results = new List<T>();
Dictionary<AsyncReply, T> results = new Dictionary<AsyncReply, T>();
int count = 0;
bool sealedBag = false;
public void Then(Action<T[]> callback)
{
base.Then(new Action<object>(o => callback((T[])o)));
}
/*
public void Trigger(T[] result)
{
Trigger((object)result);
}
*/
public void Seal()
{
sealedBag = true;
if (results.Count == 0)
Trigger(new T[0]);
for(var i = 0; i < results.Count; i++)
//foreach(var reply in results.Keys)
results.Keys.ElementAt(i).Then((r) => {
results[results.Keys.ElementAt(i)] = (T)r;
count++;
if (count == results.Count)
Trigger(results.Values.ToArray());
});
}
public void Add(AsyncReply reply)
{
if (!sealedBag)
results.Add(reply, default(T));
}
public AsyncBag()
{
}
/*
public AsyncBag(T[] result)
{
this.result = result;
}
*/
}
}

View File

@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Esiur.Engine
{
public class AsyncQueue<T> : AsyncReply
{
List<AsyncReply<T>> list = new List<AsyncReply<T>>();
//Action<T> callback;
object queueLock = new object();
public void Then(Action<T> callback)
{
base.Then(new Action<object>(o => callback((T)o)));
}
public void Add(AsyncReply<T> reply)
{
lock (queueLock)
list.Add(reply);
resultReady = false;
reply.Then(processQueue);
}
public void Remove(AsyncReply<T> reply)
{
lock (queueLock)
list.Remove(reply);
processQueue(default(T));
}
void processQueue(T o)
{
lock (queueLock)
for (var i = 0; i < list.Count; i++)
if (list[i].Ready)
{
Trigger(list[i].Result);
list.RemoveAt(i);
i--;
}
else
break;
resultReady = (list.Count == 0);
}
public AsyncQueue()
{
}
}
}

View File

@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Esiur.Engine
{
public class AsyncReply
{
protected List<Action<object>> callbacks = new List<Action<object>>();
protected object result;
object callbacksLock = new object();
//bool fired = false;
protected bool resultReady = false;
public bool Ready
{
get { return resultReady; }
}
public object Result
{
get { return result; }
}
public void Then(Action<object> callback)
{
callbacks.Add(callback);
if (resultReady)
callback(result);
// Trigger(this.result);
}
public void Trigger(object result)
{
//if (!fired)
//{
this.result = result;
resultReady = true;
lock (callbacksLock)
{
foreach (var cb in callbacks)
cb(result);
//callbacks.Clear();
}
/*
if (callback == null)
{
fireAtChance = true;
}
else
{
callback(result);
fired = true;
}
*/
//}
}
public AsyncReply()
{
}
public AsyncReply(object result)
{
resultReady = true;
this.result = result;
}
}
}

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Esiur.Resource;
namespace Esiur.Engine
{
public class AsyncReply<T>: AsyncReply
{
public void Then(Action<T> callback)
{
base.Then(new Action<object>(o => callback((T)o)));
}
public void Trigger(T result)
{
Trigger((object)result);
}
public AsyncReply()
{
}
public AsyncReply(T result)
: base(result)
{
}
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Esiur.Engine
{
public delegate void DestroyedEvent(object sender);
public interface IDestructible
{
event DestroyedEvent OnDestroy;
void Destroy();
}
}

15
Esiur/Engine/LogType.cs Normal file
View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Esiur.Engine
{
public enum LogType
{
Debug,
Warning,
Error,
}
}