提交 dd13e100 编写于 作者: T tanghai

增加DeserializeSystem,Component在反序列化后会触发该System。不过要小心使用,因为假如这个Component会保存到DB,那么...

增加DeserializeSystem,Component在反序列化后会触发该System。不过要小心使用,因为假如这个Component会保存到DB,那么传到dbserver也会执行这个System
上级 59778c77
......@@ -10,7 +10,7 @@ namespace ETModel
}
[ObjectSystem]
public class UnitSystem : AwakeSystem<Unit, UnitType>
public class UnitAwakeSystem : AwakeSystem<Unit, UnitType>
{
public override void Awake(Unit self, UnitType a)
{
......@@ -30,11 +30,6 @@ namespace ETModel
this.UnitType = unitType;
}
public override void EndDeSerialize()
{
Game.EventSystem.Add(this);
}
public override void Dispose()
{
if (this.IsDisposed)
......
......@@ -44,6 +44,9 @@
<Compile Include="..\..\Unity\Assets\Model\Base\Async\MoveNextRunner.cs">
<Link>Base\Async\MoveNextRunner.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Model\Base\Object\IDeserializeSystem.cs">
<Link>Base\Base\IDeserializeSystem.cs</Link>
</Compile>
<Compile Include="..\..\Unity\Assets\Model\Base\UnityMath\Color32.cs">
<Link>Base\UnityMath\Color32.cs</Link>
</Compile>
......@@ -131,7 +134,6 @@
<Compile Include="..\..\Unity\Assets\Model\Base\Object\EventSystem.cs" Link="Base\Base\EventSystem.cs" />
<Compile Include="..\..\Unity\Assets\Model\Base\Object\IAwakeSystem.cs" Link="Base\Base\IAwakeSystem.cs" />
<Compile Include="..\..\Unity\Assets\Model\Base\Object\IChangeSystem.cs" Link="Base\Base\IChangeSystem.cs" />
<Compile Include="..\..\Unity\Assets\Model\Base\Object\IComponentSerialize.cs" Link="Base\Base\IComponentSerialize.cs" />
<Compile Include="..\..\Unity\Assets\Model\Base\Object\IDestroySystem.cs" Link="Base\Base\IDestroySystem.cs" />
<Compile Include="..\..\Unity\Assets\Model\Base\Object\ILateUpdateSystem.cs" Link="Base\Base\ILateUpdateSystem.cs" />
<Compile Include="..\..\Unity\Assets\Model\Base\Object\ILoadSystem.cs" Link="Base\Base\ILoadSystem.cs" />
......
......@@ -4,7 +4,7 @@ using MongoDB.Bson.Serialization.Attributes;
namespace ETHotfix
{
[BsonIgnoreExtraElements]
public abstract class Component : Object, IDisposable, IComponentSerialize
public abstract class Component : Object, IDisposable
{
[BsonIgnore]
public long InstanceId { get; protected set; }
......@@ -84,12 +84,9 @@ namespace ETHotfix
}
}
public virtual void BeginSerialize()
{
}
public virtual void EndDeSerialize()
public override void EndInit()
{
Game.EventSystem.Deserialize(this);
}
}
}
\ No newline at end of file
......@@ -243,25 +243,5 @@ namespace ETHotfix
Log.Error(e);
}
}
public override void BeginSerialize()
{
base.BeginSerialize();
foreach (Component component in this.components)
{
component.BeginSerialize();
}
}
public override void EndDeSerialize()
{
base.EndDeSerialize();
foreach (Component component in this.components)
{
component.EndDeSerialize();
}
}
}
}
\ No newline at end of file
......@@ -25,6 +25,8 @@ namespace ETHotfix
private readonly UnOrderMultiMap<Type, ILateUpdateSystem> lateUpdateSystems = new UnOrderMultiMap<Type, ILateUpdateSystem>();
private readonly UnOrderMultiMap<Type, IChangeSystem> changeSystems = new UnOrderMultiMap<Type, IChangeSystem>();
private readonly UnOrderMultiMap<Type, IDeserializeSystem> deserializeSystems = new UnOrderMultiMap<Type, IDeserializeSystem>();
private Queue<long> updates = new Queue<long>();
private Queue<long> updates2 = new Queue<long>();
......@@ -65,46 +67,32 @@ namespace ETHotfix
object obj = Activator.CreateInstance(type);
IAwakeSystem objectSystem = obj as IAwakeSystem;
if (objectSystem != null)
{
this.awakeSystems.Add(objectSystem.Type(), objectSystem);
}
IUpdateSystem updateSystem = obj as IUpdateSystem;
if (updateSystem != null)
{
this.updateSystems.Add(updateSystem.Type(), updateSystem);
}
ILateUpdateSystem lateUpdateSystem = obj as ILateUpdateSystem;
if (lateUpdateSystem != null)
{
this.lateUpdateSystems.Add(lateUpdateSystem.Type(), lateUpdateSystem);
}
IStartSystem startSystem = obj as IStartSystem;
if (startSystem != null)
{
this.startSystems.Add(startSystem.Type(), startSystem);
}
IDestroySystem destroySystem = obj as IDestroySystem;
if (destroySystem != null)
{
this.destroySystems.Add(destroySystem.Type(), destroySystem);
}
ILoadSystem loadSystem = obj as ILoadSystem;
if (loadSystem != null)
{
this.loadSystems.Add(loadSystem.Type(), loadSystem);
}
IChangeSystem changeSystem = obj as IChangeSystem;
if (changeSystem != null)
{
this.changeSystems.Add(changeSystem.Type(), changeSystem);
switch (obj)
{
case IAwakeSystem objectSystem:
this.awakeSystems.Add(objectSystem.Type(), objectSystem);
break;
case IUpdateSystem updateSystem:
this.updateSystems.Add(updateSystem.Type(), updateSystem);
break;
case ILateUpdateSystem lateUpdateSystem:
this.lateUpdateSystems.Add(lateUpdateSystem.Type(), lateUpdateSystem);
break;
case IStartSystem startSystem:
this.startSystems.Add(startSystem.Type(), startSystem);
break;
case IDestroySystem destroySystem:
this.destroySystems.Add(destroySystem.Type(), destroySystem);
break;
case ILoadSystem loadSystem:
this.loadSystems.Add(loadSystem.Type(), loadSystem);
break;
case IChangeSystem changeSystem:
this.changeSystems.Add(changeSystem.Type(), changeSystem);
break;
case IDeserializeSystem deserializeSystem:
this.deserializeSystems.Add(deserializeSystem.Type(), deserializeSystem);
break;
}
}
......@@ -197,6 +185,32 @@ namespace ETHotfix
{
this.allComponents.Remove(instanceId);
}
public void Deserialize(Component component)
{
List<IDeserializeSystem> iDeserializeSystems = this.deserializeSystems[component.GetType()];
if (iDeserializeSystems == null)
{
return;
}
foreach (IDeserializeSystem deserializeSystem in iDeserializeSystems)
{
if (deserializeSystem == null)
{
continue;
}
try
{
deserializeSystem.Run(component);
}
catch (Exception e)
{
Log.Error(e);
}
}
}
public void Awake(Component component)
{
......
namespace ETHotfix
{
// 在序列化前或者反序列化之后需要做一些操作,可以实现该接口,该接口的方法需要手动调用
// 相比ISupportInitialize接口,BeginSerialize在BeginInit之前调用,EndDeSerialize在EndInit之后调用
// 并且需要手动调用,可以在反序列化之后,在次方法中将注册组件到EventSystem之中等等
public interface IComponentSerialize
{
// 序列化之前调用
void BeginSerialize();
// 反序列化之后调用
void EndDeSerialize();
}
}
\ No newline at end of file
using System;
namespace ETHotfix
{
public interface IDeserializeSystem
{
Type Type();
void Run(object o);
}
public abstract class DeserializeSystem<T> : IDeserializeSystem
{
public void Run(object o)
{
this.Deserialize((T)o);
}
public Type Type()
{
return typeof(T);
}
public abstract void Deserialize(T self);
}
}
fileFormatVersion: 2
guid: f4c02bef60f3a024eb86be6cbe51eefe
guid: f3e96494042eb2445b639892a5a761af
MonoImporter:
externalObjects: {}
serializedVersion: 2
......
......@@ -4,7 +4,7 @@ using MongoDB.Bson.Serialization.Attributes;
namespace ETModel
{
[BsonIgnoreExtraElements]
public abstract class Component : Object, IDisposable, IComponentSerialize
public abstract class Component : Object, IDisposable
{
[BsonIgnore]
public long InstanceId { get; private set; }
......@@ -84,12 +84,9 @@ namespace ETModel
}
}
public virtual void BeginSerialize()
{
}
public virtual void EndDeSerialize()
public override void EndInit()
{
Game.EventSystem.Deserialize(this);
}
}
}
\ No newline at end of file
......@@ -243,25 +243,5 @@ namespace ETModel
Log.Error(e);
}
}
public override void BeginSerialize()
{
base.BeginSerialize();
foreach (Component component in this.components)
{
component.BeginSerialize();
}
}
public override void EndDeSerialize()
{
base.EndDeSerialize();
foreach (Component component in this.components)
{
component.EndDeSerialize();
}
}
}
}
\ No newline at end of file
......@@ -33,6 +33,8 @@ namespace ETModel
private readonly UnOrderMultiMap<Type, ILateUpdateSystem> lateUpdateSystems = new UnOrderMultiMap<Type, ILateUpdateSystem>();
private readonly UnOrderMultiMap<Type, IChangeSystem> changeSystems = new UnOrderMultiMap<Type, IChangeSystem>();
private readonly UnOrderMultiMap<Type, IDeserializeSystem> deserializeSystems = new UnOrderMultiMap<Type, IDeserializeSystem>();
private Queue<long> updates = new Queue<long>();
private Queue<long> updates2 = new Queue<long>();
......@@ -82,46 +84,32 @@ namespace ETModel
object obj = Activator.CreateInstance(type);
IAwakeSystem objectSystem = obj as IAwakeSystem;
if (objectSystem != null)
{
this.awakeSystems.Add(objectSystem.Type(), objectSystem);
}
IUpdateSystem updateSystem = obj as IUpdateSystem;
if (updateSystem != null)
{
this.updateSystems.Add(updateSystem.Type(), updateSystem);
}
ILateUpdateSystem lateUpdateSystem = obj as ILateUpdateSystem;
if (lateUpdateSystem != null)
{
this.lateUpdateSystems.Add(lateUpdateSystem.Type(), lateUpdateSystem);
}
IStartSystem startSystem = obj as IStartSystem;
if (startSystem != null)
{
this.startSystems.Add(startSystem.Type(), startSystem);
}
IDestroySystem destroySystem = obj as IDestroySystem;
if (destroySystem != null)
{
this.destroySystems.Add(destroySystem.Type(), destroySystem);
}
ILoadSystem loadSystem = obj as ILoadSystem;
if (loadSystem != null)
{
this.loadSystems.Add(loadSystem.Type(), loadSystem);
}
IChangeSystem changeSystem = obj as IChangeSystem;
if (changeSystem != null)
{
this.changeSystems.Add(changeSystem.Type(), changeSystem);
switch (obj)
{
case IAwakeSystem objectSystem:
this.awakeSystems.Add(objectSystem.Type(), objectSystem);
break;
case IUpdateSystem updateSystem:
this.updateSystems.Add(updateSystem.Type(), updateSystem);
break;
case ILateUpdateSystem lateUpdateSystem:
this.lateUpdateSystems.Add(lateUpdateSystem.Type(), lateUpdateSystem);
break;
case IStartSystem startSystem:
this.startSystems.Add(startSystem.Type(), startSystem);
break;
case IDestroySystem destroySystem:
this.destroySystems.Add(destroySystem.Type(), destroySystem);
break;
case ILoadSystem loadSystem:
this.loadSystems.Add(loadSystem.Type(), loadSystem);
break;
case IChangeSystem changeSystem:
this.changeSystems.Add(changeSystem.Type(), changeSystem);
break;
case IDeserializeSystem deserializeSystem:
this.deserializeSystems.Add(deserializeSystem.Type(), deserializeSystem);
break;
}
}
......@@ -207,6 +195,32 @@ namespace ETModel
this.allComponents.TryGetValue(id, out component);
return component;
}
public void Deserialize(Component component)
{
List<IDeserializeSystem> iDeserializeSystems = this.deserializeSystems[component.GetType()];
if (iDeserializeSystems == null)
{
return;
}
foreach (IDeserializeSystem deserializeSystem in iDeserializeSystems)
{
if (deserializeSystem == null)
{
continue;
}
try
{
deserializeSystem.Run(component);
}
catch (Exception e)
{
Log.Error(e);
}
}
}
public void Awake(Component component)
{
......
namespace ETModel
{
// 在序列化前或者反序列化之后需要做一些操作,可以实现该接口,该接口的方法需要手动调用
// 相比ISupportInitialize接口,BeginSerialize在BeginInit之前调用,EndDeSerialize在EndInit之后调用
// 并且需要手动调用,可以在反序列化之后,在次方法中将注册组件到EventSystem之中等等
public interface IComponentSerialize
{
// 序列化之前调用
void BeginSerialize();
// 反序列化之后调用
void EndDeSerialize();
}
}
\ No newline at end of file
using System;
namespace ETModel
{
public interface IDeserializeSystem
{
Type Type();
void Run(object o);
}
/// <summary>
/// 反序列化后执行的System
/// 要小心使用这个System,因为对象假如要保存到数据库,到dbserver也会进行反序列化,那么也会执行该System
/// </summary>
/// <typeparam name="T"></typeparam>
public abstract class DeserializeSystem<T> : IDeserializeSystem
{
public void Run(object o)
{
this.Deserialize((T)o);
}
public Type Type()
{
return typeof(T);
}
public abstract void Deserialize(T self);
}
}
fileFormatVersion: 2
guid: 1d4dd4af4f735fc43a9893df26ab0bce
timeCreated: 1526888904
licenseType: Pro
guid: 5e19578888c954c48ae3e6d6cd5a3ceb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
......
......@@ -70,7 +70,7 @@
<Compile Include="Assets\Hotfix\Base\Object\EventSystem.cs" />
<Compile Include="Assets\Hotfix\Base\Object\IAwakeSystem.cs" />
<Compile Include="Assets\Hotfix\Base\Object\IChangeSystem.cs" />
<Compile Include="Assets\Hotfix\Base\Object\IComponentSerialize.cs" />
<Compile Include="Assets\Hotfix\Base\Object\IDeserializeSystem.cs" />
<Compile Include="Assets\Hotfix\Base\Object\IDestroySystem.cs" />
<Compile Include="Assets\Hotfix\Base\Object\ILateUpdateSystem.cs" />
<Compile Include="Assets\Hotfix\Base\Object\ILoadSystem.cs" />
......
......@@ -101,7 +101,7 @@
<Compile Include="Assets\Model\Base\Object\EventSystem.cs" />
<Compile Include="Assets\Model\Base\Object\IAwakeSystem.cs" />
<Compile Include="Assets\Model\Base\Object\IChangeSystem.cs" />
<Compile Include="Assets\Model\Base\Object\IComponentSerialize.cs" />
<Compile Include="Assets\Model\Base\Object\IDeserializeSystem.cs" />
<Compile Include="Assets\Model\Base\Object\IDestroySystem.cs" />
<Compile Include="Assets\Model\Base\Object\ILateUpdateSystem.cs" />
<Compile Include="Assets\Model\Base\Object\ILoadSystem.cs" />
......
......@@ -4,10 +4,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Model", "Unity.Model.
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.ThirdParty", "Unity.ThirdParty.csproj", "{E15BADD2-3A26-309A-AB0F-DC5B08044350}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Editor", "Unity.Editor.csproj", "{CD311104-1830-B119-81B6-5DBEE2467FFB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Hotfix", "Unity.Hotfix.csproj", "{1066F652-6A89-D1C4-9881-1A19DF7AB80E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Unity.Editor", "Unity.Editor.csproj", "{CD311104-1830-B119-81B6-5DBEE2467FFB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
......@@ -22,14 +22,14 @@ Global
{E15BADD2-3A26-309A-AB0F-DC5B08044350}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E15BADD2-3A26-309A-AB0F-DC5B08044350}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E15BADD2-3A26-309A-AB0F-DC5B08044350}.Release|Any CPU.Build.0 = Release|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Release|Any CPU.Build.0 = Release|Any CPU
{1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1066F652-6A89-D1C4-9881-1A19DF7AB80E}.Release|Any CPU.Build.0 = Release|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CD311104-1830-B119-81B6-5DBEE2467FFB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册