提交 c4930ded 编写于 作者: T tanghai

1.每个Component都增加了一个InstanceId,这个id每个对象是唯一的,区分开来逻辑Id跟对象Id,比如Player对象的Id在整个游戏期间都...

1.每个Component都增加了一个InstanceId,这个id每个对象是唯一的,区分开来逻辑Id跟对象Id,比如Player对象的Id在整个游戏期间都是不变的,但是每次登录Player的InstanceId都应该不一样,actorid应该用InstanceId来处理。例如erlang的进程id是actorid,但是erlang进程中的玩家id不会是进程id。
2.加入instanceid是因为存在一个问题:Player的id是唯一的,但是断线重连,假如只有Player.Id系统无法区分,断线前的Player跟断线后的Player。
3.简化了EventSystem的实现,并且不需要ActorManagerComponent了,所有的Component都会注册在EventSystem里面,有很多好处,不细说了。
4.Disposer类删掉了,之前继承Disposer的类都可以通过继承Component来实现
上级 613f2d9d
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=EnforceWhileStatementBraces/@EntryIndexedValue">HINT</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=EnforceWhileStatementBraces/@EntryIndexedValue">HINT</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Unity/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Unity"&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSRemoveCodeRedundancies&gt;True&lt;/CSRemoveCodeRedundancies&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;DISABLED&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;IMPLICIT_WHEN_INITIALIZER_HAS_TYPE&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;IMPLICIT_EXCEPT_SIMPLE_TYPES&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSMakeAutoPropertyGetOnly&gt;True&lt;/CSMakeAutoPropertyGetOnly&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;RemoveCodeRedundancies&gt;True&lt;/RemoveCodeRedundancies&gt;&lt;CSCodeStyleAttributes ArrangeTypeAccessModifier="True" ArrangeTypeMemberAccessModifier="True" SortModifiers="True" RemoveRedundantParentheses="True" AddMissingParentheses="True" ArrangeBraces="True" ArrangeAttributes="True" ArrangeArgumentsStyle="True" ArrangeCodeBodyStyle="True" /&gt;&lt;/Profile&gt;</s:String> <s:String x:Key="/Default/CodeStyle/CodeCleanup/Profiles/=Unity/@EntryIndexedValue">&lt;?xml version="1.0" encoding="utf-16"?&gt;&lt;Profile name="Unity"&gt;&lt;CSArrangeThisQualifier&gt;True&lt;/CSArrangeThisQualifier&gt;&lt;CSRemoveCodeRedundancies&gt;True&lt;/CSRemoveCodeRedundancies&gt;&lt;CSMakeFieldReadonly&gt;True&lt;/CSMakeFieldReadonly&gt;&lt;CSUseVar&gt;&lt;BehavourStyle&gt;DISABLED&lt;/BehavourStyle&gt;&lt;LocalVariableStyle&gt;IMPLICIT_WHEN_INITIALIZER_HAS_TYPE&lt;/LocalVariableStyle&gt;&lt;ForeachVariableStyle&gt;IMPLICIT_EXCEPT_SIMPLE_TYPES&lt;/ForeachVariableStyle&gt;&lt;/CSUseVar&gt;&lt;CSOptimizeUsings&gt;&lt;OptimizeUsings&gt;True&lt;/OptimizeUsings&gt;&lt;EmbraceInRegion&gt;False&lt;/EmbraceInRegion&gt;&lt;RegionName&gt;&lt;/RegionName&gt;&lt;/CSOptimizeUsings&gt;&lt;CSShortenReferences&gt;True&lt;/CSShortenReferences&gt;&lt;CSReformatCode&gt;True&lt;/CSReformatCode&gt;&lt;CSMakeAutoPropertyGetOnly&gt;True&lt;/CSMakeAutoPropertyGetOnly&gt;&lt;CSUseAutoProperty&gt;True&lt;/CSUseAutoProperty&gt;&lt;RemoveCodeRedundancies&gt;True&lt;/RemoveCodeRedundancies&gt;&lt;CSCodeStyleAttributes ArrangeTypeAccessModifier="True" ArrangeTypeMemberAccessModifier="True" SortModifiers="True" RemoveRedundantParentheses="True" AddMissingParentheses="True" ArrangeBraces="True" ArrangeAttributes="True" ArrangeArgumentsStyle="True" ArrangeCodeBodyStyle="True" /&gt;&lt;/Profile&gt;</s:String>
<s:String x:Key="/Default/CodeStyle/CodeCleanup/RecentlyUsedProfile/@EntryValue">Unity</s:String> <s:String x:Key="/Default/CodeStyle/CodeCleanup/RecentlyUsedProfile/@EntryValue">Unity</s:String>
<s:String x:Key="/Default/Housekeeping/Layout/DialogWindows/RefactoringWizardWindow/Location/@EntryValue">-164,-76</s:String> <s:String x:Key="/Default/Housekeeping/Layout/DialogWindows/RefactoringWizardWindow/Location/@EntryValue">122,-178</s:String>
<s:Int64 x:Key="/Default/Environment/SearchAndNavigation/DefaultOccurrencesGroupingIndex/@EntryValue">0</s:Int64> <s:Int64 x:Key="/Default/Environment/SearchAndNavigation/DefaultOccurrencesGroupingIndex/@EntryValue">0</s:Int64>
<s:String x:Key="/Default/Profiling/Configurations/=1/@EntryIndexedValue">&lt;data&gt;&lt;HostParameters type="LocalHostParameters" /&gt;&lt;Argument type="StandaloneArgument"&gt;&lt;Arguments IsNull="False"&gt;&lt;/Arguments&gt;&lt;FileName IsNull="False"&gt;&lt;/FileName&gt;&lt;WorkingDirectory IsNull="False"&gt;&lt;/WorkingDirectory&gt;&lt;Scope&gt;&lt;ProcessFilters /&gt;&lt;/Scope&gt;&lt;/Argument&gt;&lt;Info type="TimelineInfo" /&gt;&lt;HostOptions type="HostOptions"&gt;&lt;HostTempPath IsNull="False"&gt;&lt;/HostTempPath&gt;&lt;/HostOptions&gt;&lt;/data&gt;</s:String> <s:String x:Key="/Default/Profiling/Configurations/=1/@EntryIndexedValue">&lt;data&gt;&lt;HostParameters type="LocalHostParameters" /&gt;&lt;Argument type="StandaloneArgument"&gt;&lt;Arguments IsNull="False"&gt;&lt;/Arguments&gt;&lt;FileName IsNull="False"&gt;&lt;/FileName&gt;&lt;WorkingDirectory IsNull="False"&gt;&lt;/WorkingDirectory&gt;&lt;Scope&gt;&lt;ProcessFilters /&gt;&lt;/Scope&gt;&lt;/Argument&gt;&lt;Info type="TimelineInfo" /&gt;&lt;HostOptions type="HostOptions"&gt;&lt;HostTempPath IsNull="False"&gt;&lt;/HostTempPath&gt;&lt;/HostOptions&gt;&lt;/data&gt;</s:String>
</wpf:ResourceDictionary> </wpf:ResourceDictionary>
\ No newline at end of file
{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10000, "Host2" : null }], "AppId" : 1, "AppType" : "Manager", "ServerIP" : "*" } { "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20000 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10000, "Host2" : "127.0.0.1" }], "AppId" : 1, "AppType" : "Manager", "ServerIP" : "*" }
{ "_t" : "StartConfig", "_id" : NumberLong("98565862653976"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 10001 }], "AppId" : 2, "AppType" : "Location", "ServerIP" : "*" } { "_t" : "StartConfig", "_id" : NumberLong("98565862653976"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 10001 }], "AppId" : 2, "AppType" : "Location", "ServerIP" : "*" }
{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20001 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10002, "Host2" : null }], "AppId" : 3, "AppType" : "Realm", "ServerIP" : "*" } { "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20001 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10002, "Host2" : "127.0.0.1" }], "AppId" : 3, "AppType" : "Realm", "ServerIP" : "*" }
{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20002 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10003, "Host2" : null }], "AppId" : 4, "AppType" : "Gate", "ServerIP" : "*" } { "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20002 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10003, "Host2" : "127.0.0.1" }], "AppId" : 4, "AppType" : "Gate", "ServerIP" : "*" }
{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20003 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10004, "Host2" : null }], "AppId" : 5, "AppType" : "Gate", "ServerIP" : "*" } { "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20003 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10004, "Host2" : "127.0.0.1" }], "AppId" : 5, "AppType" : "Gate", "ServerIP" : "*" }
{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20004 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10005, "Host2" : null }], "AppId" : 6, "AppType" : "Gate", "ServerIP" : "*" } { "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20004 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10005, "Host2" : "127.0.0.1" }], "AppId" : 6, "AppType" : "Gate", "ServerIP" : "*" }
{ "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20005 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10006, "Host2" : null }], "AppId" : 7, "AppType" : "Gate", "ServerIP" : "*" } { "_t" : "StartConfig", "_id" : NumberLong("96832366182401"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20005 }, { "_t" : "OuterConfig", "Host" : "127.0.0.1", "Port" : 10006, "Host2" : "127.0.0.1" }], "AppId" : 7, "AppType" : "Gate", "ServerIP" : "*" }
{ "_t" : "StartConfig", "_id" : NumberLong("98565723062316"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20006 }], "AppId" : 8, "AppType" : "Map", "ServerIP" : "*" } { "_t" : "StartConfig", "_id" : NumberLong("98565723062316"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20006 }], "AppId" : 8, "AppType" : "Map", "ServerIP" : "*" }
{ "_t" : "StartConfig", "_id" : NumberLong("98565723062316"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20007 }], "AppId" : 9, "AppType" : "Map", "ServerIP" : "*" } { "_t" : "StartConfig", "_id" : NumberLong("98565723062316"), "components" : [{ "_t" : "InnerConfig", "Host" : "127.0.0.1", "Port" : 20007 }], "AppId" : 9, "AppType" : "Map", "ServerIP" : "*" }
...@@ -56,7 +56,6 @@ namespace App ...@@ -56,7 +56,6 @@ namespace App
break; break;
case AppType.Realm: case AppType.Realm:
Game.Scene.AddComponent<ActorMessageDispatherComponent>(); Game.Scene.AddComponent<ActorMessageDispatherComponent>();
Game.Scene.AddComponent<ActorManagerComponent>();
Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint); Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint); Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint);
Game.Scene.AddComponent<LocationProxyComponent>(); Game.Scene.AddComponent<LocationProxyComponent>();
...@@ -65,7 +64,6 @@ namespace App ...@@ -65,7 +64,6 @@ namespace App
case AppType.Gate: case AppType.Gate:
Game.Scene.AddComponent<PlayerComponent>(); Game.Scene.AddComponent<PlayerComponent>();
Game.Scene.AddComponent<ActorMessageDispatherComponent>(); Game.Scene.AddComponent<ActorMessageDispatherComponent>();
Game.Scene.AddComponent<ActorManagerComponent>();
Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint); Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint); Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint);
Game.Scene.AddComponent<LocationProxyComponent>(); Game.Scene.AddComponent<LocationProxyComponent>();
...@@ -78,7 +76,6 @@ namespace App ...@@ -78,7 +76,6 @@ namespace App
break; break;
case AppType.Map: case AppType.Map:
Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint); Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
Game.Scene.AddComponent<ActorManagerComponent>();
Game.Scene.AddComponent<UnitComponent>(); Game.Scene.AddComponent<UnitComponent>();
Game.Scene.AddComponent<LocationProxyComponent>(); Game.Scene.AddComponent<LocationProxyComponent>();
Game.Scene.AddComponent<ActorProxyComponent>(); Game.Scene.AddComponent<ActorProxyComponent>();
...@@ -94,7 +91,6 @@ namespace App ...@@ -94,7 +91,6 @@ namespace App
Game.Scene.AddComponent<DBCacheComponent>(); Game.Scene.AddComponent<DBCacheComponent>();
Game.Scene.AddComponent<LocationComponent>(); Game.Scene.AddComponent<LocationComponent>();
Game.Scene.AddComponent<ActorMessageDispatherComponent>(); Game.Scene.AddComponent<ActorMessageDispatherComponent>();
Game.Scene.AddComponent<ActorManagerComponent>();
Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint); Game.Scene.AddComponent<NetInnerComponent, IPEndPoint>(innerConfig.IPEndPoint);
Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint); Game.Scene.AddComponent<NetOuterComponent, IPEndPoint>(outerConfig.IPEndPoint);
Game.Scene.AddComponent<LocationProxyComponent>(); Game.Scene.AddComponent<LocationProxyComponent>();
...@@ -122,13 +118,13 @@ namespace App ...@@ -122,13 +118,13 @@ namespace App
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
......
...@@ -41,8 +41,6 @@ namespace ETHotfix ...@@ -41,8 +41,6 @@ namespace ETHotfix
{ {
self.entityActorHandler = new CommonEntityActorHandler(); self.entityActorHandler = new CommonEntityActorHandler();
self.queue = new Queue<ActorMessageInfo>(); self.queue = new Queue<ActorMessageInfo>();
self.actorId = self.Entity.Id;
Game.Scene.GetComponent<ActorManagerComponent>().Add((Entity)self.Parent);
self.HandleAsync(); self.HandleAsync();
} }
...@@ -50,8 +48,6 @@ namespace ETHotfix ...@@ -50,8 +48,6 @@ namespace ETHotfix
{ {
self.entityActorHandler = iEntityActorHandler; self.entityActorHandler = iEntityActorHandler;
self.queue = new Queue<ActorMessageInfo>(); self.queue = new Queue<ActorMessageInfo>();
self.actorId = self.Entity.Id;
Game.Scene.GetComponent<ActorManagerComponent>().Add((Entity)self.Parent);
self.HandleAsync(); self.HandleAsync();
} }
...@@ -63,12 +59,12 @@ namespace ETHotfix ...@@ -63,12 +59,12 @@ namespace ETHotfix
public static async Task AddLocation(this ActorComponent self) public static async Task AddLocation(this ActorComponent self)
{ {
await Game.Scene.GetComponent<LocationProxyComponent>().Add(self.actorId); await Game.Scene.GetComponent<LocationProxyComponent>().Add(self.Parent.InstanceId);
} }
public static async Task RemoveLocation(this ActorComponent self) public static async Task RemoveLocation(this ActorComponent self)
{ {
await Game.Scene.GetComponent<LocationProxyComponent>().Remove(self.actorId); await Game.Scene.GetComponent<LocationProxyComponent>().Remove(self.Parent.InstanceId);
} }
public static void Add(this ActorComponent self, ActorMessageInfo info) public static void Add(this ActorComponent self, ActorMessageInfo info)
...@@ -116,7 +112,7 @@ namespace ETHotfix ...@@ -116,7 +112,7 @@ namespace ETHotfix
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
......
...@@ -13,15 +13,15 @@ namespace ETHotfix ...@@ -13,15 +13,15 @@ namespace ETHotfix
try try
{ {
DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>(); DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>();
List<Component> disposers = await dbCacheComponent.GetBatch(message.CollectionName, message.IdList); List<Component> components = await dbCacheComponent.GetBatch(message.CollectionName, message.IdList);
response.Disposers = disposers; response.Components = components;
if (message.NeedCache) if (message.NeedCache)
{ {
foreach (Component disposer in disposers) foreach (Component component in components)
{ {
dbCacheComponent.AddToCache(disposer, message.CollectionName); dbCacheComponent.AddToCache(component, message.CollectionName);
} }
} }
......
...@@ -13,15 +13,15 @@ namespace ETHotfix ...@@ -13,15 +13,15 @@ namespace ETHotfix
try try
{ {
DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>(); DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>();
List<Component> disposers = await dbCacheComponent.GetJson(message.CollectionName, message.Json); List<Component> components = await dbCacheComponent.GetJson(message.CollectionName, message.Json);
response.Disposers = disposers; response.Components = components;
if (message.NeedCache) if (message.NeedCache)
{ {
foreach (Component disposer in disposers) foreach (Component component in components)
{ {
dbCacheComponent.AddToCache(disposer, message.CollectionName); dbCacheComponent.AddToCache(component, message.CollectionName);
} }
} }
......
...@@ -12,13 +12,13 @@ namespace ETHotfix ...@@ -12,13 +12,13 @@ namespace ETHotfix
try try
{ {
DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>(); DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>();
Component disposer = await dbCacheComponent.Get(message.CollectionName, message.Id); Component component = await dbCacheComponent.Get(message.CollectionName, message.Id);
response.Disposer = disposer; response.Component = component;
if (message.NeedCache && disposer != null) if (message.NeedCache && component != null)
{ {
dbCacheComponent.AddToCache(disposer, message.CollectionName); dbCacheComponent.AddToCache(component, message.CollectionName);
} }
reply(response); reply(response);
......
...@@ -15,18 +15,18 @@ namespace ETHotfix ...@@ -15,18 +15,18 @@ namespace ETHotfix
if (string.IsNullOrEmpty(message.CollectionName)) if (string.IsNullOrEmpty(message.CollectionName))
{ {
message.CollectionName = message.Disposers[0].GetType().Name; message.CollectionName = message.Components[0].GetType().Name;
} }
if (message.NeedCache) if (message.NeedCache)
{ {
foreach (Component disposer in message.Disposers) foreach (Component component in message.Components)
{ {
dbCacheComponent.AddToCache(disposer, message.CollectionName); dbCacheComponent.AddToCache(component, message.CollectionName);
} }
} }
await dbCacheComponent.AddBatch(message.Disposers, message.CollectionName); await dbCacheComponent.AddBatch(message.Components, message.CollectionName);
reply(response); reply(response);
} }
......
...@@ -14,14 +14,14 @@ namespace ETHotfix ...@@ -14,14 +14,14 @@ namespace ETHotfix
DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>(); DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>();
if (string.IsNullOrEmpty(message.CollectionName)) if (string.IsNullOrEmpty(message.CollectionName))
{ {
message.CollectionName = message.Disposer.GetType().Name; message.CollectionName = message.Component.GetType().Name;
} }
if (message.NeedCache) if (message.NeedCache)
{ {
dbCacheComponent.AddToCache(message.Disposer, message.CollectionName); dbCacheComponent.AddToCache(message.Component, message.CollectionName);
} }
await dbCacheComponent.Add(message.Disposer, message.CollectionName); await dbCacheComponent.Add(message.Component, message.CollectionName);
reply(response); reply(response);
} }
catch (Exception e) catch (Exception e)
......
...@@ -16,7 +16,7 @@ namespace ETHotfix ...@@ -16,7 +16,7 @@ namespace ETHotfix
// 在map服务器上创建战斗Unit // 在map服务器上创建战斗Unit
IPEndPoint mapAddress = Game.Scene.GetComponent<StartConfigComponent>().MapConfigs[0].GetComponent<InnerConfig>().IPEndPoint; IPEndPoint mapAddress = Game.Scene.GetComponent<StartConfigComponent>().MapConfigs[0].GetComponent<InnerConfig>().IPEndPoint;
Session mapSession = Game.Scene.GetComponent<NetInnerComponent>().Get(mapAddress); Session mapSession = Game.Scene.GetComponent<NetInnerComponent>().Get(mapAddress);
M2G_CreateUnit createUnit = (M2G_CreateUnit)await mapSession.Call(new G2M_CreateUnit() { PlayerId = player.Id, GateSessionId = session.Id }); M2G_CreateUnit createUnit = (M2G_CreateUnit)await mapSession.Call(new G2M_CreateUnit() { PlayerId = player.Id, GateSessionId = session.InstanceId });
player.UnitId = createUnit.UnitId; player.UnitId = createUnit.UnitId;
response.UnitId = createUnit.UnitId; response.UnitId = createUnit.UnitId;
response.Count = createUnit.Count; response.Count = createUnit.Count;
......
...@@ -12,6 +12,7 @@ namespace ETHotfix ...@@ -12,6 +12,7 @@ namespace ETHotfix
try try
{ {
Unit unit = ComponentFactory.Create<Unit>(); Unit unit = ComponentFactory.Create<Unit>();
await unit.AddComponent<ActorComponent, IEntityActorHandler>(new MapUnitEntityActorHandler()).AddLocation(); await unit.AddComponent<ActorComponent, IEntityActorHandler>(new MapUnitEntityActorHandler()).AddLocation();
unit.AddComponent<UnitGateComponent, long>(message.GateSessionId); unit.AddComponent<UnitGateComponent, long>(message.GateSessionId);
Game.Scene.GetComponent<UnitComponent>().Add(unit); Game.Scene.GetComponent<UnitComponent>().Add(unit);
......
...@@ -14,10 +14,10 @@ namespace ETHotfix ...@@ -14,10 +14,10 @@ namespace ETHotfix
// 收到actor消息,放入actor队列 // 收到actor消息,放入actor队列
if (message is IActorMessage iActorMessage) if (message is IActorMessage iActorMessage)
{ {
Entity entity = Game.Scene.GetComponent<ActorManagerComponent>().Get(iActorMessage.ActorId); Entity entity = Game.EventSystem.Get(iActorMessage.ActorId) as Entity;
if (entity == null) if (entity == null)
{ {
Log.Debug($"not found actor: {iActorMessage.ActorId}"); Log.Warning($"not found actor: {iActorMessage.ActorId}");
ActorResponse response = new ActorResponse ActorResponse response = new ActorResponse
{ {
Error = ErrorCode.ERR_NotFoundActor, Error = ErrorCode.ERR_NotFoundActor,
......
...@@ -10,7 +10,9 @@ namespace ETHotfix ...@@ -10,7 +10,9 @@ namespace ETHotfix
ushort opcode = packet.Opcode(); ushort opcode = packet.Opcode();
Type messageType = session.Network.Entity.GetComponent<OpcodeTypeComponent>().GetType(opcode); Type messageType = session.Network.Entity.GetComponent<OpcodeTypeComponent>().GetType(opcode);
object message = session.Network.MessagePacker.DeserializeFrom(messageType, packet.Bytes, Packet.Index, packet.Length - Packet.Index); object message = session.Network.MessagePacker.DeserializeFrom(messageType, packet.Bytes, Packet.Index, packet.Length - Packet.Index);
//Log.Debug($"recv: {JsonHelper.ToJson(message)}");
switch (message) switch (message)
{ {
case IFrameMessage iFrameMessage: // 如果是帧消息,构造成OneFrameMessage发给对应的unit case IFrameMessage iFrameMessage: // 如果是帧消息,构造成OneFrameMessage发给对应的unit
......
...@@ -65,7 +65,7 @@ namespace ETModel ...@@ -65,7 +65,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
......
...@@ -31,7 +31,7 @@ namespace ETModel ...@@ -31,7 +31,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
...@@ -51,11 +51,11 @@ namespace ETModel ...@@ -51,11 +51,11 @@ namespace ETModel
} }
catch (RpcException e) catch (RpcException e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
...@@ -78,7 +78,7 @@ namespace ETModel ...@@ -78,7 +78,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
......
...@@ -46,7 +46,7 @@ namespace ETModel ...@@ -46,7 +46,7 @@ namespace ETModel
{ {
protected static void ReplyError(Response response, Exception e, Action<Response> reply) protected static void ReplyError(Response response, Exception e, Action<Response> reply)
{ {
Log.Error(e.ToString()); Log.Error(e);
response.Error = ErrorCode.ERR_RpcFail; response.Error = ErrorCode.ERR_RpcFail;
response.Message = e.ToString(); response.Message = e.ToString();
reply(response); reply(response);
......
...@@ -17,8 +17,6 @@ namespace ETModel ...@@ -17,8 +17,6 @@ namespace ETModel
{ {
public IEntityActorHandler entityActorHandler; public IEntityActorHandler entityActorHandler;
public long actorId;
// 队列处理消息 // 队列处理消息
public Queue<ActorMessageInfo> queue; public Queue<ActorMessageInfo> queue;
...@@ -26,25 +24,16 @@ namespace ETModel ...@@ -26,25 +24,16 @@ namespace ETModel
public override void Dispose() public override void Dispose()
{ {
try if (this.IsDisposed)
{ {
if (this.IsDisposed) return;
{ }
return;
}
base.Dispose();
var t = this.tcs; base.Dispose();
this.tcs = null;
t?.SetResult(new ActorMessageInfo());
Game.Scene.GetComponent<ActorManagerComponent>().Remove(actorId); var t = this.tcs;
} this.tcs = null;
catch (Exception) t?.SetResult(new ActorMessageInfo());
{
Log.Error($"unregister actor fail: {this.actorId}");
}
} }
} }
} }
\ No newline at end of file
using System.Collections.Generic;
namespace ETModel
{
[ObjectSystem]
public class ActorManagerComponentAwakeSystem : AwakeSystem<ActorManagerComponent>
{
public override void Awake(ActorManagerComponent self)
{
self.Awake();
}
}
/// <summary>
/// 用来管理该服务器上所有的Actor对象
/// </summary>
public class ActorManagerComponent : Component
{
private readonly Dictionary<long, Entity> dictionary = new Dictionary<long, Entity>();
public void Awake()
{
}
public void Add(Entity entity)
{
Log.Info($"add actor: {entity.Id} {entity.GetType().Name}");
dictionary[entity.Id] = entity;
}
public void Remove(long id)
{
Entity entity;
if (!this.dictionary.TryGetValue(id, out entity))
{
return;
}
Log.Info($"remove actor: {entity.Id} {entity.GetType().Name}");
this.dictionary.Remove(id);
}
public Entity Get(long id)
{
Entity entity = null;
this.dictionary.TryGetValue(id, out entity);
return entity;
}
public override void Dispose()
{
if (this.IsDisposed)
{
return;
}
base.Dispose();
}
}
}
\ No newline at end of file
...@@ -31,19 +31,10 @@ namespace ETModel ...@@ -31,19 +31,10 @@ namespace ETModel
{ {
// actor的地址 // actor的地址
public IPEndPoint Address; public IPEndPoint Address;
// 已发送等待回应的消息
public Queue<ActorTask> RunningTasks = new Queue<ActorTask>();
// 还没发送的消息 // 还没发送的消息
public Queue<ActorTask> WaitingTasks = new Queue<ActorTask>(); public Queue<ActorTask> WaitingTasks = new Queue<ActorTask>();
// 发送窗口大小
public int WindowSize = 1;
// 最大窗口
public const int MaxWindowSize = 1;
// 最近发送消息的时间 // 最近发送消息的时间
public long LastSendTime; public long LastSendTime;
...@@ -56,7 +47,6 @@ namespace ETModel ...@@ -56,7 +47,6 @@ namespace ETModel
public void Awake() public void Awake()
{ {
this.LastSendTime = TimeHelper.Now(); this.LastSendTime = TimeHelper.Now();
this.WindowSize = 1;
this.tcs = null; this.tcs = null;
this.CancellationTokenSource = new CancellationTokenSource(); this.CancellationTokenSource = new CancellationTokenSource();
} }
...@@ -71,8 +61,13 @@ namespace ETModel ...@@ -71,8 +61,13 @@ namespace ETModel
base.Dispose(); base.Dispose();
this.LastSendTime = 0; this.LastSendTime = 0;
this.Address = null; this.Address = null;
this.RunningTasks.Clear();
this.WaitingTasks.Clear(); while (this.WaitingTasks.Count > 0)
{
ActorTask actorTask = this.WaitingTasks.Dequeue();
actorTask.RunFail(ErrorCode.ERR_NotFoundActor);
}
this.failTimes = 0; this.failTimes = 0;
var t = this.tcs; var t = this.tcs;
this.tcs = null; this.tcs = null;
...@@ -95,13 +90,11 @@ namespace ETModel ...@@ -95,13 +90,11 @@ namespace ETModel
private void AllowGet() private void AllowGet()
{ {
if (this.tcs == null || this.WaitingTasks.Count <= 0 || this.RunningTasks.Count >= this.WindowSize) if (this.tcs == null || this.WaitingTasks.Count <= 0)
{ {
return; return;
} }
ActorTask task = this.WaitingTasks.Peek();
ActorTask task = this.WaitingTasks.Dequeue();
this.RunningTasks.Enqueue(task);
var t = this.tcs; var t = this.tcs;
this.tcs = null; this.tcs = null;
...@@ -112,8 +105,7 @@ namespace ETModel ...@@ -112,8 +105,7 @@ namespace ETModel
{ {
if (this.WaitingTasks.Count > 0) if (this.WaitingTasks.Count > 0)
{ {
ActorTask task = this.WaitingTasks.Dequeue(); ActorTask task = this.WaitingTasks.Peek();
this.RunningTasks.Enqueue(task);
return Task.FromResult(task); return Task.FromResult(task);
} }
...@@ -132,51 +124,38 @@ namespace ETModel ...@@ -132,51 +124,38 @@ namespace ETModel
} }
try try
{ {
this.RunTask(actorTask); await this.RunTask(actorTask);
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
return; return;
} }
} }
} }
private async void RunTask(ActorTask task) private async Task RunTask(ActorTask task)
{ {
try try
{ {
IResponse response = await task.Run(); IResponse response = await task.Run();
// 如果没找到Actor,发送窗口减少为1,重试 // 如果没找到Actor,重试
if (response.Error == ErrorCode.ERR_NotFoundActor) if (response.Error == ErrorCode.ERR_NotFoundActor)
{ {
this.CancellationTokenSource.Cancel(); this.CancellationTokenSource.Cancel();
this.WindowSize = 1;
++this.failTimes; ++this.failTimes;
while (this.WaitingTasks.Count > 0)
{
ActorTask actorTask = this.WaitingTasks.Dequeue();
this.RunningTasks.Enqueue(actorTask);
}
ObjectHelper.Swap(ref this.RunningTasks, ref this.WaitingTasks);
// 失败3次则清空actor发送队列,返回失败 // 失败10次则清空actor发送队列,返回失败
if (this.failTimes > 3) if (this.failTimes > 10)
{ {
while (this.WaitingTasks.Count > 0)
{
ActorTask actorTask = this.WaitingTasks.Dequeue();
actorTask.RunFail(response.Error);
}
// 失败直接删除actorproxy // 失败直接删除actorproxy
Log.Info($"actor send message fail, actorid: {this.Id}");
Game.Scene.GetComponent<ActorProxyComponent>().Remove(this.Id); Game.Scene.GetComponent<ActorProxyComponent>().Remove(this.Id);
return; return;
} }
// 等待一会再发送 // 等待1s再发送
await Game.Scene.GetComponent<TimerComponent>().WaitAsync(this.failTimes * 500); await Game.Scene.GetComponent<TimerComponent>().WaitAsync(1000);
int appId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(this.Id); int appId = await Game.Scene.GetComponent<LocationProxyComponent>().Get(this.Id);
this.Address = Game.Scene.GetComponent<StartConfigComponent>().Get(appId).GetComponent<InnerConfig>().IPEndPoint; this.Address = Game.Scene.GetComponent<StartConfigComponent>().Get(appId).GetComponent<InnerConfig>().IPEndPoint;
this.CancellationTokenSource = new CancellationTokenSource(); this.CancellationTokenSource = new CancellationTokenSource();
...@@ -187,17 +166,12 @@ namespace ETModel ...@@ -187,17 +166,12 @@ namespace ETModel
// 发送成功 // 发送成功
this.LastSendTime = TimeHelper.Now(); this.LastSendTime = TimeHelper.Now();
this.failTimes = 0; this.failTimes = 0;
if (this.WindowSize < MaxWindowSize)
{
++this.WindowSize;
}
this.RunningTasks.Dequeue(); this.WaitingTasks.Dequeue();
this.AllowGet();
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
......
...@@ -31,35 +31,35 @@ namespace ETModel ...@@ -31,35 +31,35 @@ namespace ETModel
} }
} }
public Task<bool> Add(Component disposer, string collectionName = "") public Task<bool> Add(Component component, string collectionName = "")
{ {
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(); TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
this.AddToCache(disposer, collectionName); this.AddToCache(component, collectionName);
if (string.IsNullOrEmpty(collectionName)) if (string.IsNullOrEmpty(collectionName))
{ {
collectionName = disposer.GetType().Name; collectionName = component.GetType().Name;
} }
DBSaveTask task = ComponentFactory.CreateWithId<DBSaveTask, Component, string, TaskCompletionSource<bool>>(disposer.Id, disposer, collectionName, tcs); DBSaveTask task = ComponentFactory.CreateWithId<DBSaveTask, Component, string, TaskCompletionSource<bool>>(component.Id, component, collectionName, tcs);
this.tasks[(int)((ulong)task.Id % taskCount)].Add(task); this.tasks[(int)((ulong)task.Id % taskCount)].Add(task);
return tcs.Task; return tcs.Task;
} }
public Task<bool> AddBatch(List<Component> disposers, string collectionName) public Task<bool> AddBatch(List<Component> components, string collectionName)
{ {
TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>(); TaskCompletionSource<bool> tcs = new TaskCompletionSource<bool>();
DBSaveBatchTask task = ComponentFactory.Create<DBSaveBatchTask, List<Component>, string, TaskCompletionSource<bool>>(disposers, collectionName, tcs); DBSaveBatchTask task = ComponentFactory.Create<DBSaveBatchTask, List<Component>, string, TaskCompletionSource<bool>>(components, collectionName, tcs);
this.tasks[(int)((ulong)task.Id % taskCount)].Add(task); this.tasks[(int)((ulong)task.Id % taskCount)].Add(task);
return tcs.Task; return tcs.Task;
} }
public void AddToCache(Component disposer, string collectionName = "") public void AddToCache(Component component, string collectionName = "")
{ {
if (string.IsNullOrEmpty(collectionName)) if (string.IsNullOrEmpty(collectionName))
{ {
collectionName = disposer.GetType().Name; collectionName = component.GetType().Name;
} }
Dictionary<long, Component> collection; Dictionary<long, Component> collection;
if (!this.cache.TryGetValue(collectionName, out collection)) if (!this.cache.TryGetValue(collectionName, out collection))
...@@ -67,7 +67,7 @@ namespace ETModel ...@@ -67,7 +67,7 @@ namespace ETModel
collection = new Dictionary<long, Component>(); collection = new Dictionary<long, Component>();
this.cache.Add(collectionName, collection); this.cache.Add(collectionName, collection);
} }
collection[disposer.Id] = disposer; collection[component.Id] = component;
} }
public Component GetFromCache(string collectionName, long id) public Component GetFromCache(string collectionName, long id)
...@@ -97,10 +97,10 @@ namespace ETModel ...@@ -97,10 +97,10 @@ namespace ETModel
public Task<Component> Get(string collectionName, long id) public Task<Component> Get(string collectionName, long id)
{ {
Component disposer = GetFromCache(collectionName, id); Component component = GetFromCache(collectionName, id);
if (disposer != null) if (component != null)
{ {
return Task.FromResult(disposer); return Task.FromResult(component);
} }
TaskCompletionSource<Component> tcs = new TaskCompletionSource<Component>(); TaskCompletionSource<Component> tcs = new TaskCompletionSource<Component>();
...@@ -112,22 +112,22 @@ namespace ETModel ...@@ -112,22 +112,22 @@ namespace ETModel
public Task<List<Component>> GetBatch(string collectionName, List<long> idList) public Task<List<Component>> GetBatch(string collectionName, List<long> idList)
{ {
List <Component> disposers = new List<Component>(); List <Component> components = new List<Component>();
bool isAllInCache = true; bool isAllInCache = true;
foreach (long id in idList) foreach (long id in idList)
{ {
Component disposer = this.GetFromCache(collectionName, id); Component component = this.GetFromCache(collectionName, id);
if (disposer == null) if (component == null)
{ {
isAllInCache = false; isAllInCache = false;
break; break;
} }
disposers.Add(disposer); components.Add(component);
} }
if (isAllInCache) if (isAllInCache)
{ {
return Task.FromResult(disposers); return Task.FromResult(components);
} }
TaskCompletionSource<List<Component>> tcs = new TaskCompletionSource<List<Component>>(); TaskCompletionSource<List<Component>> tcs = new TaskCompletionSource<List<Component>>();
......
...@@ -27,35 +27,35 @@ namespace ETModel ...@@ -27,35 +27,35 @@ namespace ETModel
dbAddress = dbStartConfig.GetComponent<InnerConfig>().IPEndPoint; dbAddress = dbStartConfig.GetComponent<InnerConfig>().IPEndPoint;
} }
public async Task Save(Component disposer, bool needCache = true) public async Task Save(Component component, bool needCache = true)
{ {
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress); Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress);
await session.Call(new DBSaveRequest { Disposer = disposer, NeedCache = needCache}); await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache});
} }
public async Task SaveBatch(List<Component> disposers, bool needCache = true) public async Task SaveBatch(List<Component> components, bool needCache = true)
{ {
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress); Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress);
await session.Call(new DBSaveBatchRequest { Disposers = disposers, NeedCache = needCache}); await session.Call(new DBSaveBatchRequest { Components = components, NeedCache = needCache});
} }
public async Task Save(Component disposer, bool needCache, CancellationToken cancellationToken) public async Task Save(Component component, bool needCache, CancellationToken cancellationToken)
{ {
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress); Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress);
await session.Call(new DBSaveRequest { Disposer = disposer, NeedCache = needCache}, cancellationToken); await session.Call(new DBSaveRequest { Component = component, NeedCache = needCache}, cancellationToken);
} }
public async void SaveLog(Component disposer) public async void SaveLog(Component component)
{ {
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress); Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress);
await session.Call(new DBSaveRequest { Disposer = disposer, NeedCache = false, CollectionName = "Log" }); await session.Call(new DBSaveRequest { Component = component, NeedCache = false, CollectionName = "Log" });
} }
public async Task<T> Query<T>(long id, bool needCache = true) where T: Component public async Task<T> Query<T>(long id, bool needCache = true) where T: Component
{ {
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress); Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress);
DBQueryResponse dbQueryResponse = (DBQueryResponse)await session.Call(new DBQueryRequest { CollectionName = typeof(T).Name, Id = id, NeedCache = needCache }); DBQueryResponse dbQueryResponse = (DBQueryResponse)await session.Call(new DBQueryRequest { CollectionName = typeof(T).Name, Id = id, NeedCache = needCache });
return (T)dbQueryResponse.Disposer; return (T)dbQueryResponse.Component;
} }
public async Task<List<T>> QueryBatch<T>(List<long> ids, bool needCache = true) where T : Component public async Task<List<T>> QueryBatch<T>(List<long> ids, bool needCache = true) where T : Component
...@@ -63,9 +63,9 @@ namespace ETModel ...@@ -63,9 +63,9 @@ namespace ETModel
List<T> list = new List<T>(); List<T> list = new List<T>();
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress); Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress);
DBQueryBatchResponse dbQueryBatchResponse = (DBQueryBatchResponse)await session.Call(new DBQueryBatchRequest { CollectionName = typeof(T).Name, IdList = ids, NeedCache = needCache}); DBQueryBatchResponse dbQueryBatchResponse = (DBQueryBatchResponse)await session.Call(new DBQueryBatchRequest { CollectionName = typeof(T).Name, IdList = ids, NeedCache = needCache});
foreach (Component disposer in dbQueryBatchResponse.Disposers) foreach (Component component in dbQueryBatchResponse.Components)
{ {
list.Add((T)disposer); list.Add((T)component);
} }
return list; return list;
} }
...@@ -75,9 +75,9 @@ namespace ETModel ...@@ -75,9 +75,9 @@ namespace ETModel
List<T> list = new List<T>(); List<T> list = new List<T>();
Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress); Session session = Game.Scene.GetComponent<NetInnerComponent>().Get(dbAddress);
DBQueryJsonResponse dbQueryJsonResponse = (DBQueryJsonResponse)await session.Call(new DBQueryJsonRequest { CollectionName = typeof(T).Name, Json = json, NeedCache = needCache}); DBQueryJsonResponse dbQueryJsonResponse = (DBQueryJsonResponse)await session.Call(new DBQueryJsonRequest { CollectionName = typeof(T).Name, Json = json, NeedCache = needCache});
foreach (Component disposer in dbQueryJsonResponse.Disposers) foreach (Component component in dbQueryJsonResponse.Components)
{ {
list.Add((T)disposer); list.Add((T)component);
} }
return list; return list;
} }
......
...@@ -36,18 +36,18 @@ namespace ETModel ...@@ -36,18 +36,18 @@ namespace ETModel
// 执行查询数据库任务 // 执行查询数据库任务
foreach (long id in IdList) foreach (long id in IdList)
{ {
Component disposer = dbCacheComponent.GetFromCache(this.CollectionName, id); Component component = dbCacheComponent.GetFromCache(this.CollectionName, id);
if (disposer == null) if (component == null)
{ {
disposer = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == id).Result.FirstOrDefaultAsync(); component = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == id).Result.FirstOrDefaultAsync();
dbCacheComponent.AddToCache(disposer); dbCacheComponent.AddToCache(component);
} }
if (disposer == null) if (component == null)
{ {
continue; continue;
} }
result.Add(disposer); result.Add(component);
} }
this.Tcs.SetResult(result); this.Tcs.SetResult(result);
......
...@@ -31,8 +31,8 @@ namespace ETModel ...@@ -31,8 +31,8 @@ namespace ETModel
{ {
// 执行查询数据库任务 // 执行查询数据库任务
FilterDefinition<Component> filterDefinition = new JsonFilterDefinition<Component>(this.Json); FilterDefinition<Component> filterDefinition = new JsonFilterDefinition<Component>(this.Json);
List<Component> disposers = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition).Result.ToListAsync(); List<Component> components = await dbComponent.GetCollection(this.CollectionName).FindAsync(filterDefinition).Result.ToListAsync();
this.Tcs.SetResult(disposers); this.Tcs.SetResult(components);
} }
catch (Exception e) catch (Exception e)
{ {
......
...@@ -25,21 +25,21 @@ namespace ETModel ...@@ -25,21 +25,21 @@ namespace ETModel
DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>(); DBCacheComponent dbCacheComponent = Game.Scene.GetComponent<DBCacheComponent>();
DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>(); DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
// 执行查询前先看看cache中是否已经存在 // 执行查询前先看看cache中是否已经存在
Component disposer = dbCacheComponent.GetFromCache(this.CollectionName, this.Id); Component component = dbCacheComponent.GetFromCache(this.CollectionName, this.Id);
if (disposer != null) if (component != null)
{ {
this.Tcs.SetResult(disposer); this.Tcs.SetResult(component);
return; return;
} }
try try
{ {
// 执行查询数据库任务 // 执行查询数据库任务
disposer = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == this.Id).Result.FirstOrDefaultAsync(); component = await dbComponent.GetCollection(this.CollectionName).FindAsync((s) => s.Id == this.Id).Result.FirstOrDefaultAsync();
if (disposer != null) if (component != null)
{ {
dbCacheComponent.AddToCache(disposer); dbCacheComponent.AddToCache(component);
} }
this.Tcs.SetResult(disposer); this.Tcs.SetResult(component);
} }
catch (Exception e) catch (Exception e)
{ {
......
...@@ -9,9 +9,9 @@ namespace ETModel ...@@ -9,9 +9,9 @@ namespace ETModel
[ObjectSystem] [ObjectSystem]
public class DbSaveBatchTaskSystem : AwakeSystem<DBSaveBatchTask, List<Component>, string, TaskCompletionSource<bool>> public class DbSaveBatchTaskSystem : AwakeSystem<DBSaveBatchTask, List<Component>, string, TaskCompletionSource<bool>>
{ {
public override void Awake(DBSaveBatchTask self, List<Component> disposers, string collectionName, TaskCompletionSource<bool> tcs) public override void Awake(DBSaveBatchTask self, List<Component> components, string collectionName, TaskCompletionSource<bool> tcs)
{ {
self.Disposers = disposers; self.Components = components;
self.CollectionName = collectionName; self.CollectionName = collectionName;
self.Tcs = tcs; self.Tcs = tcs;
} }
...@@ -21,7 +21,7 @@ namespace ETModel ...@@ -21,7 +21,7 @@ namespace ETModel
{ {
public string CollectionName { get; set; } public string CollectionName { get; set; }
public List<Component> Disposers; public List<Component> Components;
public TaskCompletionSource<bool> Tcs; public TaskCompletionSource<bool> Tcs;
...@@ -29,9 +29,9 @@ namespace ETModel ...@@ -29,9 +29,9 @@ namespace ETModel
{ {
DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>(); DBComponent dbComponent = Game.Scene.GetComponent<DBComponent>();
foreach (Component disposer in this.Disposers) foreach (Component component in this.Components)
{ {
if (disposer == null) if (component == null)
{ {
continue; continue;
} }
...@@ -39,12 +39,12 @@ namespace ETModel ...@@ -39,12 +39,12 @@ namespace ETModel
try try
{ {
// 执行保存数据库任务 // 执行保存数据库任务
await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == disposer.Id, disposer, new UpdateOptions { IsUpsert = true }); await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == component.Id, component, new UpdateOptions { IsUpsert = true });
} }
catch (Exception e) catch (Exception e)
{ {
Log.Debug($"{disposer.GetType().Name} {disposer.ToJson()} {e}"); Log.Debug($"{component.GetType().Name} {component.ToJson()} {e}");
this.Tcs.SetException(new Exception($"保存数据失败! {CollectionName} {this.Disposers.ListToString()}", e)); this.Tcs.SetException(new Exception($"保存数据失败! {CollectionName} {this.Components.ListToString()}", e));
} }
} }
this.Tcs.SetResult(true); this.Tcs.SetResult(true);
......
...@@ -10,7 +10,7 @@ namespace ETModel ...@@ -10,7 +10,7 @@ namespace ETModel
{ {
public override void Awake(DBSaveTask self, Component entity, string collectionName, TaskCompletionSource<bool> tcs) public override void Awake(DBSaveTask self, Component entity, string collectionName, TaskCompletionSource<bool> tcs)
{ {
self.Disposer = entity; self.Component = entity;
self.CollectionName = collectionName; self.CollectionName = collectionName;
self.Tcs = tcs; self.Tcs = tcs;
} }
...@@ -18,7 +18,7 @@ namespace ETModel ...@@ -18,7 +18,7 @@ namespace ETModel
public sealed class DBSaveTask : DBTask public sealed class DBSaveTask : DBTask
{ {
public Component Disposer; public Component Component;
public string CollectionName { get; set; } public string CollectionName { get; set; }
...@@ -31,7 +31,7 @@ namespace ETModel ...@@ -31,7 +31,7 @@ namespace ETModel
try try
{ {
// 执行保存数据库任务 // 执行保存数据库任务
await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == this.Disposer.Id, this.Disposer, new UpdateOptions {IsUpsert = true}); await dbComponent.GetCollection(this.CollectionName).ReplaceOneAsync(s => s.Id == this.Component.Id, this.Component, new UpdateOptions {IsUpsert = true});
this.Tcs.SetResult(true); this.Tcs.SetResult(true);
} }
catch (Exception e) catch (Exception e)
......
...@@ -35,7 +35,7 @@ namespace ETModel ...@@ -35,7 +35,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
......
...@@ -6,7 +6,7 @@ namespace ETModel ...@@ -6,7 +6,7 @@ namespace ETModel
{ {
protected static void ReplyError(Response response, Exception e, Action<Response> reply) protected static void ReplyError(Response response, Exception e, Action<Response> reply)
{ {
Log.Error(e.ToString()); Log.Error(e);
response.Error = ErrorCode.ERR_RpcFail; response.Error = ErrorCode.ERR_RpcFail;
response.Message = e.ToString(); response.Message = e.ToString();
reply(response); reply(response);
......
...@@ -132,7 +132,7 @@ namespace ETModel ...@@ -132,7 +132,7 @@ namespace ETModel
public string CollectionName; public string CollectionName;
[ProtoMember(3, IsRequired = true)] [ProtoMember(3, IsRequired = true)]
public Component Disposer; public Component Component;
} }
...@@ -165,7 +165,7 @@ namespace ETModel ...@@ -165,7 +165,7 @@ namespace ETModel
public string CollectionName; public string CollectionName;
[ProtoMember(3)] [ProtoMember(3)]
public List<Component> Disposers = new List<Component>(); public List<Component> Components = new List<Component>();
} }
...@@ -216,7 +216,7 @@ namespace ETModel ...@@ -216,7 +216,7 @@ namespace ETModel
public string Message { get; set; } public string Message { get; set; }
[ProtoMember(1, IsRequired = true)] [ProtoMember(1, IsRequired = true)]
public Component Disposer; public Component Component;
} }
...@@ -252,7 +252,7 @@ namespace ETModel ...@@ -252,7 +252,7 @@ namespace ETModel
public string Message { get; set; } public string Message { get; set; }
[ProtoMember(1)] [ProtoMember(1)]
public List<Component> Disposers = new List<Component>(); public List<Component> Components = new List<Component>();
} }
...@@ -288,7 +288,7 @@ namespace ETModel ...@@ -288,7 +288,7 @@ namespace ETModel
public string Message { get; set; } public string Message { get; set; }
[ProtoMember(1)] [ProtoMember(1)]
public List<Component> Disposers = new List<Component>(); public List<Component> Components = new List<Component>();
} }
......
...@@ -43,7 +43,6 @@ ...@@ -43,7 +43,6 @@
<Compile Include="..\..\Unity\Assets\Scripts\Base\MultiMap.cs" Link="Base\MultiMap.cs" /> <Compile Include="..\..\Unity\Assets\Scripts\Base\MultiMap.cs" Link="Base\MultiMap.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\Object\Component.cs" Link="Base\Object\Component.cs" /> <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\Component.cs" Link="Base\Object\Component.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ComponentFactory.cs" Link="Base\Object\ComponentFactory.cs" /> <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\ComponentFactory.cs" Link="Base\Object\ComponentFactory.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\Object\Disposer.cs" Link="Base\Object\Disposer.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\Object\Entity.cs" Link="Base\Object\Entity.cs" /> <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\Entity.cs" Link="Base\Object\Entity.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\Object\EventSystem.cs" Link="Base\Object\EventSystem.cs" /> <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\EventSystem.cs" Link="Base\Object\EventSystem.cs" />
<Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IAwake.cs" Link="Base\Object\IAwake.cs" /> <Compile Include="..\..\Unity\Assets\Scripts\Base\Object\IAwake.cs" Link="Base\Object\IAwake.cs" />
......
...@@ -82,7 +82,7 @@ public class ExcelExporterEditor : EditorWindow ...@@ -82,7 +82,7 @@ public class ExcelExporterEditor : EditorWindow
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
......
using MongoDB.Bson.Serialization.Attributes; using System;
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel namespace ETModel
{ {
[BsonIgnoreExtraElements] [BsonIgnoreExtraElements]
public abstract partial class Component: Disposer public abstract partial class Component : Object, IDisposable
{ {
[BsonIgnore]
public long InstanceId { get; private set; }
[BsonIgnore]
private bool isFromPool;
[BsonIgnore]
public bool IsFromPool
{
get
{
return this.isFromPool;
}
set
{
this.isFromPool = value;
if (this.InstanceId == 0)
{
this.InstanceId = IdGenerater.GenerateId();
Game.EventSystem.Add(this);
}
}
}
[BsonIgnore]
public bool IsDisposed
{
get
{
return this.InstanceId == 0;
}
}
[BsonIgnoreIfDefault] [BsonIgnoreIfDefault]
[BsonDefaultValue(0L)] [BsonDefaultValue(0L)]
[BsonElement] [BsonElement]
...@@ -28,14 +64,33 @@ namespace ETModel ...@@ -28,14 +64,33 @@ namespace ETModel
} }
} }
public override void Dispose() protected Component()
{
this.InstanceId = IdGenerater.GenerateId();
Game.EventSystem.Add(this);
}
protected Component(long instanceId)
{
this.InstanceId = instanceId;
Game.EventSystem.Add(this);
}
public virtual void Dispose()
{ {
if (this.IsDisposed) if (this.IsDisposed)
{ {
return; return;
} }
base.Dispose(); Game.EventSystem.Remove(this.InstanceId);
this.InstanceId = 0;
if (this.IsFromPool)
{
Game.ObjectPool.Recycle(this);
}
} }
} }
} }
\ No newline at end of file
...@@ -6,102 +6,102 @@ namespace ETModel ...@@ -6,102 +6,102 @@ namespace ETModel
{ {
public static Component CreateWithParent(Type type, Component parent) public static Component CreateWithParent(Type type, Component parent)
{ {
Component disposer = (Component)Game.ObjectPool.Fetch(type); Component component = (Component)Game.ObjectPool.Fetch(type);
disposer.Parent = parent; component.Parent = parent;
Game.EventSystem.Awake(disposer); Game.EventSystem.Awake(component);
return disposer; return component;
} }
public static T CreateWithParent<T>(Component parent) where T : Component public static T CreateWithParent<T>(Component parent) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Parent = parent; component.Parent = parent;
Game.EventSystem.Awake(disposer); Game.EventSystem.Awake(component);
return disposer; return component;
} }
public static T CreateWithParent<T, A>(Component parent, A a) where T : Component public static T CreateWithParent<T, A>(Component parent, A a) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Parent = parent; component.Parent = parent;
Game.EventSystem.Awake(disposer, a); Game.EventSystem.Awake(component, a);
return disposer; return component;
} }
public static T CreateWithParent<T, A, B>(Component parent, A a, B b) where T : Component public static T CreateWithParent<T, A, B>(Component parent, A a, B b) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Parent = parent; component.Parent = parent;
Game.EventSystem.Awake(disposer, a, b); Game.EventSystem.Awake(component, a, b);
return disposer; return component;
} }
public static T CreateWithParent<T, A, B, C>(Component parent, A a, B b, C c) where T : Component public static T CreateWithParent<T, A, B, C>(Component parent, A a, B b, C c) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Parent = parent; component.Parent = parent;
Game.EventSystem.Awake(disposer, a, b, c); Game.EventSystem.Awake(component, a, b, c);
return disposer; return component;
} }
public static T Create<T>() where T : Component public static T Create<T>() where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
Game.EventSystem.Awake(disposer); Game.EventSystem.Awake(component);
return disposer; return component;
} }
public static T Create<T, A>(A a) where T : Component public static T Create<T, A>(A a) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
Game.EventSystem.Awake(disposer, a); Game.EventSystem.Awake(component, a);
return disposer; return component;
} }
public static T Create<T, A, B>(A a, B b) where T : Component public static T Create<T, A, B>(A a, B b) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
Game.EventSystem.Awake(disposer, a, b); Game.EventSystem.Awake(component, a, b);
return disposer; return component;
} }
public static T Create<T, A, B, C>(A a, B b, C c) where T : Component public static T Create<T, A, B, C>(A a, B b, C c) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
Game.EventSystem.Awake(disposer, a, b, c); Game.EventSystem.Awake(component, a, b, c);
return disposer; return component;
} }
public static T CreateWithId<T>(long id) where T : Component public static T CreateWithId<T>(long id) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Id = id; component.Id = id;
Game.EventSystem.Awake(disposer); Game.EventSystem.Awake(component);
return disposer; return component;
} }
public static T CreateWithId<T, A>(long id, A a) where T : Component public static T CreateWithId<T, A>(long id, A a) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Id = id; component.Id = id;
Game.EventSystem.Awake(disposer, a); Game.EventSystem.Awake(component, a);
return disposer; return component;
} }
public static T CreateWithId<T, A, B>(long id, A a, B b) where T : Component public static T CreateWithId<T, A, B>(long id, A a, B b) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Id = id; component.Id = id;
Game.EventSystem.Awake(disposer, a, b); Game.EventSystem.Awake(component, a, b);
return disposer; return component;
} }
public static T CreateWithId<T, A, B, C>(long id, A a, B b, C c) where T : Component public static T CreateWithId<T, A, B, C>(long id, A a, B b, C c) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Id = id; component.Id = id;
Game.EventSystem.Awake(disposer, a, b, c); Game.EventSystem.Awake(component, a, b, c);
return disposer; return component;
} }
} }
} }
using System;
using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
{
[BsonKnownTypes(typeof(Component))]
public abstract class Disposer : Object, IDisposable
{
[BsonIgnore]
public bool IsFromPool { get; set; }
[BsonIgnore]
public bool IsDisposed { get; set; }
public virtual void Dispose()
{
if (this.IsDisposed)
{
return;
}
this.IsDisposed = true;
if (this.IsFromPool)
{
Game.ObjectPool.Recycle(this);
}
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 785b1815942ba374b9cea238401cd506
timeCreated: 1498116853
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
...@@ -17,7 +17,7 @@ namespace ETModel ...@@ -17,7 +17,7 @@ namespace ETModel
protected Entity() protected Entity()
{ {
this.Id = IdGenerater.GenerateId(); this.Id = this.InstanceId;
this.components = new HashSet<Component>(); this.components = new HashSet<Component>();
this.componentDict = new Dictionary<Type, Component>(); this.componentDict = new Dictionary<Type, Component>();
} }
...@@ -46,7 +46,7 @@ namespace ETModel ...@@ -46,7 +46,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
...@@ -215,7 +215,7 @@ namespace ETModel ...@@ -215,7 +215,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
......
...@@ -14,6 +14,8 @@ namespace ETModel ...@@ -14,6 +14,8 @@ namespace ETModel
public sealed class EventSystem public sealed class EventSystem
{ {
private readonly Dictionary<long, Component> allComponents = new Dictionary<long, Component>();
private readonly Dictionary<DLLType, Assembly> assemblies = new Dictionary<DLLType, Assembly>(); private readonly Dictionary<DLLType, Assembly> assemblies = new Dictionary<DLLType, Assembly>();
private readonly Dictionary<string, List<IEvent>> allEvents = new Dictionary<string, List<IEvent>>(); private readonly Dictionary<string, List<IEvent>> allEvents = new Dictionary<string, List<IEvent>>();
...@@ -28,18 +30,16 @@ namespace ETModel ...@@ -28,18 +30,16 @@ namespace ETModel
private readonly UnOrderMultiMap<Type, ALateUpdateSystem> lateUpdateEvents = new UnOrderMultiMap<Type, ALateUpdateSystem>(); private readonly UnOrderMultiMap<Type, ALateUpdateSystem> lateUpdateEvents = new UnOrderMultiMap<Type, ALateUpdateSystem>();
private Queue<Component> updates = new Queue<Component>(); private Queue<long> updates = new Queue<long>();
private Queue<Component> updates2 = new Queue<Component>(); private Queue<long> updates2 = new Queue<long>();
private readonly Queue<Component> starts = new Queue<Component>(); private readonly Queue<long> starts = new Queue<long>();
private Queue<Component> loaders = new Queue<Component>();
private Queue<Component> loaders2 = new Queue<Component>();
private Queue<Component> lateUpdates = new Queue<Component>(); private Queue<long> loaders = new Queue<long>();
private Queue<Component> lateUpdates2 = new Queue<Component>(); private Queue<long> loaders2 = new Queue<long>();
private readonly HashSet<Component> unique = new HashSet<Component>(); private Queue<long> lateUpdates = new Queue<long>();
private Queue<long> lateUpdates2 = new Queue<long>();
public void Add(DLLType dllType, Assembly assembly) public void Add(DLLType dllType, Assembly assembly)
{ {
...@@ -134,36 +134,48 @@ namespace ETModel ...@@ -134,36 +134,48 @@ namespace ETModel
return this.assemblies.Values.ToArray(); return this.assemblies.Values.ToArray();
} }
public void Add(Component disposer) public void Add(Component component)
{ {
Type type = disposer.GetType(); this.allComponents.Add(component.InstanceId, component);
Type type = component.GetType();
if (this.loadEvents.ContainsKey(type)) if (this.loadEvents.ContainsKey(type))
{ {
this.loaders.Enqueue(disposer); this.loaders.Enqueue(component.InstanceId);
} }
if (this.updateEvents.ContainsKey(type)) if (this.updateEvents.ContainsKey(type))
{ {
this.updates.Enqueue(disposer); this.updates.Enqueue(component.InstanceId);
} }
if (this.startEvents.ContainsKey(type)) if (this.startEvents.ContainsKey(type))
{ {
this.starts.Enqueue(disposer); this.starts.Enqueue(component.InstanceId);
} }
if (this.lateUpdateEvents.ContainsKey(type)) if (this.lateUpdateEvents.ContainsKey(type))
{ {
this.lateUpdates.Enqueue(disposer); this.lateUpdates.Enqueue(component.InstanceId);
} }
} }
public void Awake(Component disposer) public void Remove(long instanceId)
{ {
this.Add(disposer); this.allComponents.Remove(instanceId);
}
List<AAwakeSystem> iAwakeSystems = this.awakeEvents[disposer.GetType()]; public Component Get(long instanceId)
{
Component component = null;
this.allComponents.TryGetValue(instanceId, out component);
return component;
}
public void Awake(Component component)
{
List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
if (iAwakeSystems == null) if (iAwakeSystems == null)
{ {
return; return;
...@@ -181,15 +193,13 @@ namespace ETModel ...@@ -181,15 +193,13 @@ namespace ETModel
{ {
continue; continue;
} }
iAwake.Run(disposer); iAwake.Run(component);
} }
} }
public void Awake<P1>(Component disposer, P1 p1) public void Awake<P1>(Component component, P1 p1)
{ {
this.Add(disposer); List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
List<AAwakeSystem> iAwakeSystems = this.awakeEvents[disposer.GetType()];
if (iAwakeSystems == null) if (iAwakeSystems == null)
{ {
return; return;
...@@ -207,15 +217,13 @@ namespace ETModel ...@@ -207,15 +217,13 @@ namespace ETModel
{ {
continue; continue;
} }
iAwake.Run(disposer, p1); iAwake.Run(component, p1);
} }
} }
public void Awake<P1, P2>(Component disposer, P1 p1, P2 p2) public void Awake<P1, P2>(Component component, P1 p1, P2 p2)
{ {
this.Add(disposer); List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
List<AAwakeSystem> iAwakeSystems = this.awakeEvents[disposer.GetType()];
if (iAwakeSystems == null) if (iAwakeSystems == null)
{ {
return; return;
...@@ -233,15 +241,13 @@ namespace ETModel ...@@ -233,15 +241,13 @@ namespace ETModel
{ {
continue; continue;
} }
iAwake.Run(disposer, p1, p2); iAwake.Run(component, p1, p2);
} }
} }
public void Awake<P1, P2, P3>(Component disposer, P1 p1, P2 p2, P3 p3) public void Awake<P1, P2, P3>(Component component, P1 p1, P2 p2, P3 p3)
{ {
this.Add(disposer); List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
List<AAwakeSystem> iAwakeSystems = this.awakeEvents[disposer.GetType()];
if (iAwakeSystems == null) if (iAwakeSystems == null)
{ {
return; return;
...@@ -259,43 +265,42 @@ namespace ETModel ...@@ -259,43 +265,42 @@ namespace ETModel
{ {
continue; continue;
} }
iAwake.Run(disposer, p1, p2, p3); iAwake.Run(component, p1, p2, p3);
} }
} }
public void Load() public void Load()
{ {
unique.Clear();
while (this.loaders.Count > 0) while (this.loaders.Count > 0)
{ {
Component disposer = this.loaders.Dequeue(); long instanceId = this.loaders.Dequeue();
if (disposer.IsDisposed) Component component;
if (!this.allComponents.TryGetValue(instanceId, out component))
{ {
continue; continue;
} }
if (component.IsDisposed)
if (!this.unique.Add(disposer))
{ {
continue; continue;
} }
List<ALoadSystem> aLoadSystems = this.loadEvents[disposer.GetType()]; List<ALoadSystem> aLoadSystems = this.loadEvents[component.GetType()];
if (aLoadSystems == null) if (aLoadSystems == null)
{ {
continue; continue;
} }
this.loaders2.Enqueue(disposer); this.loaders2.Enqueue(instanceId);
foreach (ALoadSystem aLoadSystem in aLoadSystems) foreach (ALoadSystem aLoadSystem in aLoadSystems)
{ {
try try
{ {
aLoadSystem.Run(disposer); aLoadSystem.Run(component);
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -305,17 +310,16 @@ namespace ETModel ...@@ -305,17 +310,16 @@ namespace ETModel
private void Start() private void Start()
{ {
unique.Clear();
while (this.starts.Count > 0) while (this.starts.Count > 0)
{ {
Component disposer = this.starts.Dequeue(); long instanceId = this.starts.Dequeue();
Component component;
if (!this.unique.Add(disposer)) if (!this.allComponents.TryGetValue(instanceId, out component))
{ {
continue; continue;
} }
List<AStartSystem> aStartSystems = this.startEvents[disposer.GetType()]; List<AStartSystem> aStartSystems = this.startEvents[component.GetType()];
if (aStartSystems == null) if (aStartSystems == null)
{ {
continue; continue;
...@@ -325,11 +329,11 @@ namespace ETModel ...@@ -325,11 +329,11 @@ namespace ETModel
{ {
try try
{ {
aStartSystem.Run(disposer); aStartSystem.Run(component);
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -338,38 +342,37 @@ namespace ETModel ...@@ -338,38 +342,37 @@ namespace ETModel
public void Update() public void Update()
{ {
this.Start(); this.Start();
this.unique.Clear();
while (this.updates.Count > 0) while (this.updates.Count > 0)
{ {
Component disposer = this.updates.Dequeue(); long instanceId = this.updates.Dequeue();
if (disposer.IsDisposed) Component component;
if (!this.allComponents.TryGetValue(instanceId, out component))
{ {
continue; continue;
} }
if (component.IsDisposed)
if (!this.unique.Add(disposer))
{ {
continue; continue;
} }
List<AUpdateSystem> aUpdateSystems = this.updateEvents[disposer.GetType()]; List<AUpdateSystem> aUpdateSystems = this.updateEvents[component.GetType()];
if (aUpdateSystems == null) if (aUpdateSystems == null)
{ {
continue; continue;
} }
this.updates2.Enqueue(disposer); this.updates2.Enqueue(instanceId);
foreach (AUpdateSystem aUpdateSystem in aUpdateSystems) foreach (AUpdateSystem aUpdateSystem in aUpdateSystems)
{ {
try try
{ {
aUpdateSystem.Run(disposer); aUpdateSystem.Run(component);
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -379,37 +382,36 @@ namespace ETModel ...@@ -379,37 +382,36 @@ namespace ETModel
public void LateUpdate() public void LateUpdate()
{ {
this.unique.Clear();
while (this.lateUpdates.Count > 0) while (this.lateUpdates.Count > 0)
{ {
Component disposer = this.lateUpdates.Dequeue(); long instanceId = this.lateUpdates.Dequeue();
if (disposer.IsDisposed) Component component;
if (!this.allComponents.TryGetValue(instanceId, out component))
{ {
continue; continue;
} }
if (component.IsDisposed)
if (!this.unique.Add(disposer))
{ {
continue; continue;
} }
List<ALateUpdateSystem> aLateUpdateSystems = this.lateUpdateEvents[disposer.GetType()]; List<ALateUpdateSystem> aLateUpdateSystems = this.lateUpdateEvents[component.GetType()];
if (aLateUpdateSystems == null) if (aLateUpdateSystems == null)
{ {
continue; continue;
} }
this.lateUpdates2.Enqueue(disposer); this.lateUpdates2.Enqueue(instanceId);
foreach (ALateUpdateSystem aLateUpdateSystem in aLateUpdateSystems) foreach (ALateUpdateSystem aLateUpdateSystem in aLateUpdateSystems)
{ {
try try
{ {
aLateUpdateSystem.Run(disposer); aLateUpdateSystem.Run(component);
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -432,7 +434,7 @@ namespace ETModel ...@@ -432,7 +434,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -452,7 +454,7 @@ namespace ETModel ...@@ -452,7 +454,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -472,7 +474,7 @@ namespace ETModel ...@@ -472,7 +474,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -492,7 +494,7 @@ namespace ETModel ...@@ -492,7 +494,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
......
...@@ -5,42 +5,41 @@ namespace ETModel ...@@ -5,42 +5,41 @@ namespace ETModel
{ {
public class ObjectPool public class ObjectPool
{ {
private readonly Dictionary<Type, Queue<Disposer>> dictionary = new Dictionary<Type, Queue<Disposer>>(); private readonly Dictionary<Type, Queue<Component>> dictionary = new Dictionary<Type, Queue<Component>>();
public Disposer Fetch(Type type) public Component Fetch(Type type)
{ {
Queue<Disposer> queue; Queue<Component> queue;
if (!this.dictionary.TryGetValue(type, out queue)) if (!this.dictionary.TryGetValue(type, out queue))
{ {
queue = new Queue<Disposer>(); queue = new Queue<Component>();
this.dictionary.Add(type, queue); this.dictionary.Add(type, queue);
} }
Disposer obj; Component obj;
if (queue.Count > 0) if (queue.Count > 0)
{ {
obj = queue.Dequeue(); obj = queue.Dequeue();
obj.IsDisposed = false;
obj.IsFromPool = true; obj.IsFromPool = true;
return obj; return obj;
} }
obj = (Disposer)Activator.CreateInstance(type); obj = (Component)Activator.CreateInstance(type);
return obj; return obj;
} }
public T Fetch<T>() where T: Disposer public T Fetch<T>() where T: Component
{ {
T t = (T) this.Fetch(typeof(T)); T t = (T) this.Fetch(typeof(T));
t.IsFromPool = true; t.IsFromPool = true;
return t; return t;
} }
public void Recycle(Disposer obj) public void Recycle(Component obj)
{ {
Type type = obj.GetType(); Type type = obj.GetType();
Queue<Disposer> queue; Queue<Component> queue;
if (!this.dictionary.TryGetValue(type, out queue)) if (!this.dictionary.TryGetValue(type, out queue))
{ {
queue = new Queue<Disposer>(); queue = new Queue<Component>();
this.dictionary.Add(type, queue); this.dictionary.Add(type, queue);
} }
queue.Enqueue(obj); queue.Enqueue(obj);
......
...@@ -28,7 +28,7 @@ namespace ETModel ...@@ -28,7 +28,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
......
...@@ -49,7 +49,7 @@ namespace ETModel ...@@ -49,7 +49,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
......
...@@ -11,7 +11,7 @@ using UnityEditor; ...@@ -11,7 +11,7 @@ using UnityEditor;
namespace ETModel namespace ETModel
{ {
public class ABInfo : Disposer public class ABInfo : Component
{ {
private int refCount; private int refCount;
public string Name { get; } public string Name { get; }
......
...@@ -91,7 +91,7 @@ namespace ETModel ...@@ -91,7 +91,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
......
...@@ -86,7 +86,7 @@ namespace ETModel ...@@ -86,7 +86,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
continue; continue;
} }
......
...@@ -171,7 +171,7 @@ namespace ETModel ...@@ -171,7 +171,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
this.OnError(SocketError.SocketError); this.OnError(SocketError.SocketError);
} }
} }
...@@ -225,7 +225,7 @@ namespace ETModel ...@@ -225,7 +225,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
this.OnError(SocketError.SocketError); this.OnError(SocketError.SocketError);
} }
} }
......
...@@ -114,7 +114,7 @@ namespace ETModel ...@@ -114,7 +114,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
return null; return null;
} }
} }
......
...@@ -110,7 +110,7 @@ namespace ETModel ...@@ -110,7 +110,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
continue; continue;
} }
...@@ -120,7 +120,7 @@ namespace ETModel ...@@ -120,7 +120,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -155,6 +155,7 @@ namespace ETModel ...@@ -155,6 +155,7 @@ namespace ETModel
OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent<OpcodeTypeComponent>(); OpcodeTypeComponent opcodeTypeComponent = this.Network.Entity.GetComponent<OpcodeTypeComponent>();
Type responseType = opcodeTypeComponent.GetType(opcode); Type responseType = opcodeTypeComponent.GetType(opcode);
object message = this.Network.MessagePacker.DeserializeFrom(responseType, packet.Bytes, Packet.Index, packet.Length - Packet.Index); object message = this.Network.MessagePacker.DeserializeFrom(responseType, packet.Bytes, Packet.Index, packet.Length - Packet.Index);
//Log.Debug($"recv: {JsonHelper.ToJson(message)}");
IResponse response = message as IResponse; IResponse response = message as IResponse;
if (response == null) if (response == null)
......
...@@ -20,7 +20,7 @@ namespace ETModel ...@@ -20,7 +20,7 @@ namespace ETModel
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
return null; return null;
} }
} }
......
...@@ -91,7 +91,6 @@ namespace ILRuntime.Runtime.Generated ...@@ -91,7 +91,6 @@ namespace ILRuntime.Runtime.Generated
ETModel_CanvasConfig_Binding.Register(app); ETModel_CanvasConfig_Binding.Register(app);
UnityEngine_Transform_Binding.Register(app); UnityEngine_Transform_Binding.Register(app);
System_Collections_Generic_List_1_String_Binding.Register(app); System_Collections_Generic_List_1_String_Binding.Register(app);
ETModel_Disposer_Binding.Register(app);
ETModel_Scene_Binding.Register(app); ETModel_Scene_Binding.Register(app);
UnityEngine_Object_Binding.Register(app); UnityEngine_Object_Binding.Register(app);
System_Collections_Generic_Dictionary_2_String_ILTypeInstance_Binding_ValueCollection_Binding.Register(app); System_Collections_Generic_Dictionary_2_String_ILTypeInstance_Binding_ValueCollection_Binding.Register(app);
......
...@@ -15,37 +15,6 @@ namespace ILRuntime.Runtime.Generated ...@@ -15,37 +15,6 @@ namespace ILRuntime.Runtime.Generated
{ {
unsafe class ETModel_Disposer_Binding unsafe class ETModel_Disposer_Binding
{ {
public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
{
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
MethodBase method;
FieldInfo field;
Type[] args;
Type type = typeof(ETModel.Disposer);
args = new Type[]{};
method = type.GetMethod("Dispose", flag, null, args, null);
app.RegisterCLRMethodRedirection(method, Dispose_0);
}
static StackObject* Dispose_0(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)
{
ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
StackObject* ptr_of_this_method;
StackObject* __ret = ILIntepreter.Minus(__esp, 1);
ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
ETModel.Disposer instance_of_this_method;
instance_of_this_method = (ETModel.Disposer)typeof(ETModel.Disposer).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
__intp.Free(ptr_of_this_method);
instance_of_this_method.Dispose();
return __ret;
}
} }
} }
using MongoDB.Bson.Serialization.Attributes; using System;
using ETModel;
using MongoDB.Bson.Serialization.Attributes;
namespace ETHotfix namespace ETHotfix
{ {
public abstract class Component : Disposer [BsonIgnoreExtraElements]
public abstract partial class Component : Object, IDisposable2
{ {
[BsonIgnore]
public long InstanceId { get; private set; }
[BsonIgnore]
private bool isFromPool;
[BsonIgnore]
public bool IsFromPool
{
get
{
return this.isFromPool;
}
set
{
this.isFromPool = value;
if (this.InstanceId == 0)
{
this.InstanceId = IdGenerater.GenerateId();
Game.EventSystem.Add(this);
}
}
}
[BsonIgnore]
public bool IsDisposed
{
get
{
return this.InstanceId == 0;
}
}
[BsonIgnoreIfDefault]
[BsonDefaultValue(0L)]
[BsonElement]
[BsonId]
public long Id { get; set; } public long Id { get; set; }
[BsonIgnore] [BsonIgnore]
...@@ -14,6 +55,7 @@ namespace ETHotfix ...@@ -14,6 +55,7 @@ namespace ETHotfix
return this.Parent as T; return this.Parent as T;
} }
[BsonIgnore]
public Entity Entity public Entity Entity
{ {
get get
...@@ -21,15 +63,34 @@ namespace ETHotfix ...@@ -21,15 +63,34 @@ namespace ETHotfix
return this.Parent as Entity; return this.Parent as Entity;
} }
} }
public override void Dispose() protected Component()
{
this.InstanceId = IdGenerater.GenerateId();
Game.EventSystem.Add(this);
}
protected Component(long instanceId)
{
this.InstanceId = instanceId;
Game.EventSystem.Add(this);
}
public virtual void Dispose()
{ {
if (this.IsDisposed) if (this.IsDisposed)
{ {
return; return;
} }
Game.EventSystem.Remove(this.InstanceId);
this.InstanceId = 0;
base.Dispose(); if (this.IsFromPool)
{
Game.ObjectPool.Recycle(this);
}
} }
} }
} }
\ No newline at end of file
...@@ -6,102 +6,102 @@ namespace ETHotfix ...@@ -6,102 +6,102 @@ namespace ETHotfix
{ {
public static Component CreateWithParent(Type type, Component parent) public static Component CreateWithParent(Type type, Component parent)
{ {
Component disposer = (Component)Game.ObjectPool.Fetch(type); Component component = (Component)Game.ObjectPool.Fetch(type);
disposer.Parent = parent; component.Parent = parent;
Game.EventSystem.Awake(disposer); Game.EventSystem.Awake(component);
return disposer; return component;
} }
public static T CreateWithParent<T>(Component parent) where T : Component public static T CreateWithParent<T>(Component parent) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Parent = parent; component.Parent = parent;
Game.EventSystem.Awake(disposer); Game.EventSystem.Awake(component);
return disposer; return component;
} }
public static T CreateWithParent<T, A>(Component parent, A a) where T : Component public static T CreateWithParent<T, A>(Component parent, A a) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Parent = parent; component.Parent = parent;
Game.EventSystem.Awake(disposer, a); Game.EventSystem.Awake(component, a);
return disposer; return component;
} }
public static T CreateWithParent<T, A, B>(Component parent, A a, B b) where T : Component public static T CreateWithParent<T, A, B>(Component parent, A a, B b) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Parent = parent; component.Parent = parent;
Game.EventSystem.Awake(disposer, a, b); Game.EventSystem.Awake(component, a, b);
return disposer; return component;
} }
public static T CreateWithParent<T, A, B, C>(Component parent, A a, B b, C c) where T : Component public static T CreateWithParent<T, A, B, C>(Component parent, A a, B b, C c) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Parent = parent; component.Parent = parent;
Game.EventSystem.Awake(disposer, a, b, c); Game.EventSystem.Awake(component, a, b, c);
return disposer; return component;
} }
public static T Create<T>() where T : Component public static T Create<T>() where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
Game.EventSystem.Awake(disposer); Game.EventSystem.Awake(component);
return disposer; return component;
} }
public static T Create<T, A>(A a) where T : Component public static T Create<T, A>(A a) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
Game.EventSystem.Awake(disposer, a); Game.EventSystem.Awake(component, a);
return disposer; return component;
} }
public static T Create<T, A, B>(A a, B b) where T : Component public static T Create<T, A, B>(A a, B b) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
Game.EventSystem.Awake(disposer, a, b); Game.EventSystem.Awake(component, a, b);
return disposer; return component;
} }
public static T Create<T, A, B, C>(A a, B b, C c) where T : Component public static T Create<T, A, B, C>(A a, B b, C c) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
Game.EventSystem.Awake(disposer, a, b, c); Game.EventSystem.Awake(component, a, b, c);
return disposer; return component;
} }
public static T CreateWithId<T>(long id) where T : Component public static T CreateWithId<T>(long id) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Id = id; component.Id = id;
Game.EventSystem.Awake(disposer); Game.EventSystem.Awake(component);
return disposer; return component;
} }
public static T CreateWithId<T, A>(long id, A a) where T : Component public static T CreateWithId<T, A>(long id, A a) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Id = id; component.Id = id;
Game.EventSystem.Awake(disposer, a); Game.EventSystem.Awake(component, a);
return disposer; return component;
} }
public static T CreateWithId<T, A, B>(long id, A a, B b) where T : Component public static T CreateWithId<T, A, B>(long id, A a, B b) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Id = id; component.Id = id;
Game.EventSystem.Awake(disposer, a, b); Game.EventSystem.Awake(component, a, b);
return disposer; return component;
} }
public static T CreateWithId<T, A, B, C>(long id, A a, B b, C c) where T : Component public static T CreateWithId<T, A, B, C>(long id, A a, B b, C c) where T : Component
{ {
T disposer = Game.ObjectPool.Fetch<T>(); T component = Game.ObjectPool.Fetch<T>();
disposer.Id = id; component.Id = id;
Game.EventSystem.Awake(disposer, a, b, c); Game.EventSystem.Awake(component, a, b, c);
return disposer; return component;
} }
} }
} }
using ETModel;
namespace ETHotfix
{
public abstract class Disposer : Object, IDisposable2
{
public bool IsFromPool { get; set; }
public bool IsDisposed { get; set; }
public virtual void Dispose()
{
if (this.IsDisposed)
{
return;
}
this.IsDisposed = true;
if (this.IsFromPool)
{
Game.ObjectPool.Recycle(this);
}
}
}
}
\ No newline at end of file
...@@ -47,7 +47,7 @@ namespace ETHotfix ...@@ -47,7 +47,7 @@ namespace ETHotfix
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
...@@ -216,7 +216,7 @@ namespace ETHotfix ...@@ -216,7 +216,7 @@ namespace ETHotfix
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
......
...@@ -6,6 +6,8 @@ namespace ETHotfix ...@@ -6,6 +6,8 @@ namespace ETHotfix
{ {
public sealed class EventSystem public sealed class EventSystem
{ {
private readonly Dictionary<long, Component> allComponents = new Dictionary<long, Component>();
private readonly Dictionary<string, List<IEvent>> allEvents = new Dictionary<string, List<IEvent>>(); private readonly Dictionary<string, List<IEvent>> allEvents = new Dictionary<string, List<IEvent>>();
private readonly UnOrderMultiMap<Type, AAwakeSystem> awakeEvents = new UnOrderMultiMap<Type, AAwakeSystem>(); private readonly UnOrderMultiMap<Type, AAwakeSystem> awakeEvents = new UnOrderMultiMap<Type, AAwakeSystem>();
...@@ -18,18 +20,16 @@ namespace ETHotfix ...@@ -18,18 +20,16 @@ namespace ETHotfix
private readonly UnOrderMultiMap<Type, ALateUpdateSystem> lateUpdateEvents = new UnOrderMultiMap<Type, ALateUpdateSystem>(); private readonly UnOrderMultiMap<Type, ALateUpdateSystem> lateUpdateEvents = new UnOrderMultiMap<Type, ALateUpdateSystem>();
private Queue<Component> updates = new Queue<Component>(); private Queue<long> updates = new Queue<long>();
private Queue<Component> updates2 = new Queue<Component>(); private Queue<long> updates2 = new Queue<long>();
private readonly Queue<Component> starts = new Queue<Component>();
private Queue<Component> loaders = new Queue<Component>(); private readonly Queue<long> starts = new Queue<long>();
private Queue<Component> loaders2 = new Queue<Component>();
private Queue<Component> lateUpdates = new Queue<Component>(); private Queue<long> loaders = new Queue<long>();
private Queue<Component> lateUpdates2 = new Queue<Component>(); private Queue<long> loaders2 = new Queue<long>();
private readonly HashSet<Component> unique = new HashSet<Component>(); private Queue<long> lateUpdates = new Queue<long>();
private Queue<long> lateUpdates2 = new Queue<long>();
public EventSystem() public EventSystem()
{ {
...@@ -129,36 +129,41 @@ namespace ETHotfix ...@@ -129,36 +129,41 @@ namespace ETHotfix
this.allEvents[eventId].Add(e); this.allEvents[eventId].Add(e);
} }
public void Add(Component disposer) public void Add(Component component)
{ {
Type type = disposer.GetType(); this.allComponents.Add(component.InstanceId, component);
Type type = component.GetType();
if (this.loadEvents.ContainsKey(type)) if (this.loadEvents.ContainsKey(type))
{ {
this.loaders.Enqueue(disposer); this.loaders.Enqueue(component.InstanceId);
} }
if (this.updateEvents.ContainsKey(type)) if (this.updateEvents.ContainsKey(type))
{ {
this.updates.Enqueue(disposer); this.updates.Enqueue(component.InstanceId);
} }
if (this.startEvents.ContainsKey(type)) if (this.startEvents.ContainsKey(type))
{ {
this.starts.Enqueue(disposer); this.starts.Enqueue(component.InstanceId);
} }
if (this.lateUpdateEvents.ContainsKey(type)) if (this.lateUpdateEvents.ContainsKey(type))
{ {
this.lateUpdates.Enqueue(disposer); this.lateUpdates.Enqueue(component.InstanceId);
} }
} }
public void Awake(Component disposer) public void Remove(long instanceId)
{ {
this.Add(disposer); this.allComponents.Remove(instanceId);
}
List<AAwakeSystem> iAwakeSystems = this.awakeEvents[disposer.GetType()]; public void Awake(Component component)
{
List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
if (iAwakeSystems == null) if (iAwakeSystems == null)
{ {
return; return;
...@@ -176,15 +181,13 @@ namespace ETHotfix ...@@ -176,15 +181,13 @@ namespace ETHotfix
{ {
continue; continue;
} }
iAwake.Run(disposer); iAwake.Run(component);
} }
} }
public void Awake<P1>(Component disposer, P1 p1) public void Awake<P1>(Component component, P1 p1)
{ {
this.Add(disposer); List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
List<AAwakeSystem> iAwakeSystems = this.awakeEvents[disposer.GetType()];
if (iAwakeSystems == null) if (iAwakeSystems == null)
{ {
return; return;
...@@ -202,15 +205,13 @@ namespace ETHotfix ...@@ -202,15 +205,13 @@ namespace ETHotfix
{ {
continue; continue;
} }
iAwake.Run(disposer, p1); iAwake.Run(component, p1);
} }
} }
public void Awake<P1, P2>(Component disposer, P1 p1, P2 p2) public void Awake<P1, P2>(Component component, P1 p1, P2 p2)
{ {
this.Add(disposer); List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
List<AAwakeSystem> iAwakeSystems = this.awakeEvents[disposer.GetType()];
if (iAwakeSystems == null) if (iAwakeSystems == null)
{ {
return; return;
...@@ -228,15 +229,13 @@ namespace ETHotfix ...@@ -228,15 +229,13 @@ namespace ETHotfix
{ {
continue; continue;
} }
iAwake.Run(disposer, p1, p2); iAwake.Run(component, p1, p2);
} }
} }
public void Awake<P1, P2, P3>(Component disposer, P1 p1, P2 p2, P3 p3) public void Awake<P1, P2, P3>(Component component, P1 p1, P2 p2, P3 p3)
{ {
this.Add(disposer); List<AAwakeSystem> iAwakeSystems = this.awakeEvents[component.GetType()];
List<AAwakeSystem> iAwakeSystems = this.awakeEvents[disposer.GetType()];
if (iAwakeSystems == null) if (iAwakeSystems == null)
{ {
return; return;
...@@ -254,43 +253,42 @@ namespace ETHotfix ...@@ -254,43 +253,42 @@ namespace ETHotfix
{ {
continue; continue;
} }
iAwake.Run(disposer, p1, p2, p3); iAwake.Run(component, p1, p2, p3);
} }
} }
public void Load() public void Load()
{ {
unique.Clear();
while (this.loaders.Count > 0) while (this.loaders.Count > 0)
{ {
Component disposer = this.loaders.Dequeue(); long instanceId = this.loaders.Dequeue();
if (disposer.IsDisposed) Component component;
if (!this.allComponents.TryGetValue(instanceId, out component))
{ {
continue; continue;
} }
if (component.IsDisposed)
if (!this.unique.Add(disposer))
{ {
continue; continue;
} }
List<ALoadSystem> aLoadSystems = this.loadEvents[disposer.GetType()]; List<ALoadSystem> aLoadSystems = this.loadEvents[component.GetType()];
if (aLoadSystems == null) if (aLoadSystems == null)
{ {
continue; continue;
} }
this.loaders2.Enqueue(disposer); this.loaders2.Enqueue(instanceId);
foreach (ALoadSystem aLoadSystem in aLoadSystems) foreach (ALoadSystem aLoadSystem in aLoadSystems)
{ {
try try
{ {
aLoadSystem.Run(disposer); aLoadSystem.Run(component);
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -300,17 +298,16 @@ namespace ETHotfix ...@@ -300,17 +298,16 @@ namespace ETHotfix
private void Start() private void Start()
{ {
unique.Clear();
while (this.starts.Count > 0) while (this.starts.Count > 0)
{ {
Component disposer = this.starts.Dequeue(); long instanceId = this.starts.Dequeue();
Component component;
if (!this.unique.Add(disposer)) if (!this.allComponents.TryGetValue(instanceId, out component))
{ {
continue; continue;
} }
List<AStartSystem> aStartSystems = this.startEvents[disposer.GetType()]; List<AStartSystem> aStartSystems = this.startEvents[component.GetType()];
if (aStartSystems == null) if (aStartSystems == null)
{ {
continue; continue;
...@@ -320,11 +317,11 @@ namespace ETHotfix ...@@ -320,11 +317,11 @@ namespace ETHotfix
{ {
try try
{ {
aStartSystem.Run(disposer); aStartSystem.Run(component);
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -333,38 +330,37 @@ namespace ETHotfix ...@@ -333,38 +330,37 @@ namespace ETHotfix
public void Update() public void Update()
{ {
this.Start(); this.Start();
this.unique.Clear();
while (this.updates.Count > 0) while (this.updates.Count > 0)
{ {
Component disposer = this.updates.Dequeue(); long instanceId = this.updates.Dequeue();
if (disposer.IsDisposed) Component component;
if (!this.allComponents.TryGetValue(instanceId, out component))
{ {
continue; continue;
} }
if (component.IsDisposed)
if (!this.unique.Add(disposer))
{ {
continue; continue;
} }
List<AUpdateSystem> aUpdateSystems = this.updateEvents[disposer.GetType()]; List<AUpdateSystem> aUpdateSystems = this.updateEvents[component.GetType()];
if (aUpdateSystems == null) if (aUpdateSystems == null)
{ {
continue; continue;
} }
this.updates2.Enqueue(disposer); this.updates2.Enqueue(instanceId);
foreach (AUpdateSystem aUpdateSystem in aUpdateSystems) foreach (AUpdateSystem aUpdateSystem in aUpdateSystems)
{ {
try try
{ {
aUpdateSystem.Run(disposer); aUpdateSystem.Run(component);
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -374,37 +370,36 @@ namespace ETHotfix ...@@ -374,37 +370,36 @@ namespace ETHotfix
public void LateUpdate() public void LateUpdate()
{ {
this.unique.Clear();
while (this.lateUpdates.Count > 0) while (this.lateUpdates.Count > 0)
{ {
Component disposer = this.lateUpdates.Dequeue(); long instanceId = this.lateUpdates.Dequeue();
if (disposer.IsDisposed) Component component;
if (!this.allComponents.TryGetValue(instanceId, out component))
{ {
continue; continue;
} }
if (component.IsDisposed)
if (!this.unique.Add(disposer))
{ {
continue; continue;
} }
List<ALateUpdateSystem> aLateUpdateSystems = this.lateUpdateEvents[disposer.GetType()]; List<ALateUpdateSystem> aLateUpdateSystems = this.lateUpdateEvents[component.GetType()];
if (aLateUpdateSystems == null) if (aLateUpdateSystems == null)
{ {
continue; continue;
} }
this.lateUpdates2.Enqueue(disposer); this.lateUpdates2.Enqueue(instanceId);
foreach (ALateUpdateSystem aLateUpdateSystem in aLateUpdateSystems) foreach (ALateUpdateSystem aLateUpdateSystem in aLateUpdateSystems)
{ {
try try
{ {
aLateUpdateSystem.Run(disposer); aLateUpdateSystem.Run(component);
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -427,7 +422,7 @@ namespace ETHotfix ...@@ -427,7 +422,7 @@ namespace ETHotfix
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -447,7 +442,7 @@ namespace ETHotfix ...@@ -447,7 +442,7 @@ namespace ETHotfix
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -467,7 +462,7 @@ namespace ETHotfix ...@@ -467,7 +462,7 @@ namespace ETHotfix
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
...@@ -487,7 +482,7 @@ namespace ETHotfix ...@@ -487,7 +482,7 @@ namespace ETHotfix
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
......
...@@ -5,41 +5,41 @@ namespace ETHotfix ...@@ -5,41 +5,41 @@ namespace ETHotfix
{ {
public class ObjectPool public class ObjectPool
{ {
private readonly Dictionary<Type, Queue<Disposer>> dictionary = new Dictionary<Type, Queue<Disposer>>(); private readonly Dictionary<Type, Queue<Component>> dictionary = new Dictionary<Type, Queue<Component>>();
public Disposer Fetch(Type type) public Component Fetch(Type type)
{ {
Queue<Disposer> queue; Queue<Component> queue;
if (!this.dictionary.TryGetValue(type, out queue)) if (!this.dictionary.TryGetValue(type, out queue))
{ {
queue = new Queue<Disposer>(); queue = new Queue<Component>();
this.dictionary.Add(type, queue); this.dictionary.Add(type, queue);
} }
Disposer obj; Component obj;
if (queue.Count > 0) if (queue.Count > 0)
{ {
obj = queue.Dequeue(); obj = queue.Dequeue();
obj.IsDisposed = false;
obj.IsFromPool = true; obj.IsFromPool = true;
return obj; return obj;
} }
obj = (Disposer)Activator.CreateInstance(type); obj = (Component)Activator.CreateInstance(type);
return obj; return obj;
} }
public T Fetch<T>() where T : Disposer public T Fetch<T>() where T : Component
{ {
T t = (T)this.Fetch(typeof(T)); T t = (T)this.Fetch(typeof(T));
t.IsFromPool = true;
return t; return t;
} }
public void Recycle(Disposer obj) public void Recycle(Component obj)
{ {
Type type = obj.GetType(); Type type = obj.GetType();
Queue<Disposer> queue; Queue<Component> queue;
if (!this.dictionary.TryGetValue(type, out queue)) if (!this.dictionary.TryGetValue(type, out queue))
{ {
queue = new Queue<Disposer>(); queue = new Queue<Component>();
this.dictionary.Add(type, queue); this.dictionary.Add(type, queue);
} }
queue.Enqueue(obj); queue.Enqueue(obj);
......
...@@ -105,7 +105,7 @@ namespace ETHotfix ...@@ -105,7 +105,7 @@ namespace ETHotfix
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
} }
} }
} }
......
...@@ -23,7 +23,7 @@ namespace ETHotfix ...@@ -23,7 +23,7 @@ namespace ETHotfix
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
return null; return null;
} }
} }
......
...@@ -23,7 +23,7 @@ namespace ETHotfix ...@@ -23,7 +23,7 @@ namespace ETHotfix
} }
catch (Exception e) catch (Exception e)
{ {
Log.Error(e.ToString()); Log.Error(e);
return null; return null;
} }
} }
......
...@@ -75,7 +75,6 @@ ...@@ -75,7 +75,6 @@
<Compile Include="Module\Message\IMHandler.cs" /> <Compile Include="Module\Message\IMHandler.cs" />
<Compile Include="Base\Object\Component.cs" /> <Compile Include="Base\Object\Component.cs" />
<Compile Include="Base\Object\ComponentFactory.cs" /> <Compile Include="Base\Object\ComponentFactory.cs" />
<Compile Include="Base\Object\Disposer.cs" />
<Compile Include="Base\Object\Entity.cs" /> <Compile Include="Base\Object\Entity.cs" />
<Compile Include="Base\Object\EntityType.cs" /> <Compile Include="Base\Object\EntityType.cs" />
<Compile Include="Base\Object\IAwake.cs" /> <Compile Include="Base\Object\IAwake.cs" />
......
...@@ -12,12 +12,15 @@ ...@@ -12,12 +12,15 @@
<ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> <ProjectTypeGuids>{E097FAD1-6243-4DAD-9C02-E9B9EFC3FFC1};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier> <TargetFrameworkIdentifier>.NETFramework</TargetFrameworkIdentifier>
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion> <TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
<TargetFrameworkProfile></TargetFrameworkProfile> <TargetFrameworkProfile>
<CompilerResponseFile></CompilerResponseFile> </TargetFrameworkProfile>
<CompilerResponseFile>
</CompilerResponseFile>
<UnityProjectType>Game:1</UnityProjectType> <UnityProjectType>Game:1</UnityProjectType>
<UnityBuildTarget>StandaloneWindows:5</UnityBuildTarget> <UnityBuildTarget>StandaloneWindows:5</UnityBuildTarget>
<UnityVersion>2017.1.1p4</UnityVersion> <UnityVersion>2017.1.1p4</UnityVersion>
<RootNamespace></RootNamespace> <RootNamespace>
</RootNamespace>
<LangVersion>6</LangVersion> <LangVersion>6</LangVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
...@@ -156,7 +159,6 @@ ...@@ -156,7 +159,6 @@
<Compile Include="Assets\Scripts\Base\Object\Component.cs" /> <Compile Include="Assets\Scripts\Base\Object\Component.cs" />
<Compile Include="Assets\Scripts\Base\Object\ComponentAttribute.cs" /> <Compile Include="Assets\Scripts\Base\Object\ComponentAttribute.cs" />
<Compile Include="Assets\Scripts\Base\Object\ComponentFactory.cs" /> <Compile Include="Assets\Scripts\Base\Object\ComponentFactory.cs" />
<Compile Include="Assets\Scripts\Base\Object\Disposer.cs" />
<Compile Include="Assets\Scripts\Base\Object\Entity.cs" /> <Compile Include="Assets\Scripts\Base\Object\Entity.cs" />
<Compile Include="Assets\Scripts\Base\Object\EntityAttribute.cs" /> <Compile Include="Assets\Scripts\Base\Object\EntityAttribute.cs" />
<Compile Include="Assets\Scripts\Base\Object\EntityEventAttribute.cs" /> <Compile Include="Assets\Scripts\Base\Object\EntityEventAttribute.cs" />
...@@ -780,4 +782,4 @@ ...@@ -780,4 +782,4 @@
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="GenerateTargetFrameworkMonikerAttribute" /> <Target Name="GenerateTargetFrameworkMonikerAttribute" />
</Project> </Project>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册