[+] 接入ET8服务端

[+] 接入ET8服务端
This commit is contained in:
ALEXTANG
2023-07-13 12:23:48 +08:00
parent e0be062006
commit 336d4b2eb9
1316 changed files with 130657 additions and 626 deletions

View File

@@ -0,0 +1,163 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace ET
{
public static class WaitTypeError
{
public const int Success = 0;
public const int Destroy = 1;
public const int Cancel = 2;
public const int Timeout = 3;
}
public interface IWaitType
{
int Error
{
get;
set;
}
}
[EntitySystemOf(typeof(ObjectWait))]
[FriendOf(typeof(ObjectWait))]
public static partial class ObjectWaitSystem
{
[EntitySystem]
private static void Awake(this ObjectWait self)
{
self.tcss.Clear();
}
[EntitySystem]
private static void Destroy(this ObjectWait self)
{
foreach (object v in self.tcss.Values.ToArray())
{
((IDestroyRun) v).SetResult();
}
}
private interface IDestroyRun
{
void SetResult();
}
private class ResultCallback<K>: IDestroyRun where K : struct, IWaitType
{
private ETTask<K> tcs;
public ResultCallback()
{
this.tcs = ETTask<K>.Create(true);
}
public bool IsDisposed
{
get
{
return this.tcs == null;
}
}
public ETTask<K> Task => this.tcs;
public void SetResult(K k)
{
var t = tcs;
this.tcs = null;
t.SetResult(k);
}
public void SetResult()
{
var t = tcs;
this.tcs = null;
t.SetResult(new K() { Error = WaitTypeError.Destroy });
}
}
public static async ETTask<T> Wait<T>(this ObjectWait self, ETCancellationToken cancellationToken = null) where T : struct, IWaitType
{
ResultCallback<T> tcs = new ResultCallback<T>();
Type type = typeof (T);
self.tcss.Add(type, tcs);
void CancelAction()
{
self.Notify(new T() { Error = WaitTypeError.Cancel });
}
T ret;
try
{
cancellationToken?.Add(CancelAction);
ret = await tcs.Task;
}
finally
{
cancellationToken?.Remove(CancelAction);
}
return ret;
}
public static async ETTask<T> Wait<T>(this ObjectWait self, int timeout, ETCancellationToken cancellationToken = null) where T : struct, IWaitType
{
ResultCallback<T> tcs = new ResultCallback<T>();
async ETTask WaitTimeout()
{
await self.Fiber().TimerComponent.WaitAsync(timeout, cancellationToken);
if (cancellationToken.IsCancel())
{
return;
}
if (tcs.IsDisposed)
{
return;
}
self.Notify(new T() { Error = WaitTypeError.Timeout });
}
WaitTimeout().Coroutine();
self.tcss.Add(typeof (T), tcs);
void CancelAction()
{
self.Notify(new T() { Error = WaitTypeError.Cancel });
}
T ret;
try
{
cancellationToken?.Add(CancelAction);
ret = await tcs.Task;
}
finally
{
cancellationToken?.Remove(CancelAction);
}
return ret;
}
public static void Notify<T>(this ObjectWait self, T obj) where T : struct, IWaitType
{
Type type = typeof (T);
if (!self.tcss.TryGetValue(type, out object tcs))
{
return;
}
self.tcss.Remove(type);
((ResultCallback<T>) tcs).SetResult(obj);
}
}
[ComponentOf]
public class ObjectWait: Entity, IAwake, IDestroy
{
public Dictionary<Type, object> tcss = new Dictionary<Type, object>();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 32a775e5dd87b864c81d6f579572dc9c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: