diff --git a/Server/App/Program.cs b/Server/App/Program.cs index bd20ed22e31e5b274b3e9a3f94f0440e6f4143b3..26db3c5cb223729bcc44aa3b53fec4e6d4b93179 100644 --- a/Server/App/Program.cs +++ b/Server/App/Program.cs @@ -2,7 +2,6 @@ using Base; using Model; using NLog; -using Object = Model.Object; namespace App { @@ -12,8 +11,8 @@ namespace App { try { - Object.ObjectManager.Register("Model", typeof(Game).Assembly); - Object.ObjectManager.Register("Controller", DllHelper.GetController()); + ObjectManager.Instance.Register("Model", typeof(Game).Assembly); + ObjectManager.Instance.Register("Controller", DllHelper.GetController()); StartConfig startConfig = Game.Scene.AddComponent(args).MyConfig; @@ -67,7 +66,7 @@ namespace App while (true) { - Object.ObjectManager.Update(); + ObjectManager.Instance.Update(); } } catch (Exception e) diff --git a/Server/Controller/Message/M2A_ReloadHandler.cs b/Server/Controller/Message/M2A_ReloadHandler.cs index 5121465cb6bd06ee076ffe0a32480f78a41ee9a0..65a692d6c96bbf33dc04fbc88e2fe695ba73befe 100644 --- a/Server/Controller/Message/M2A_ReloadHandler.cs +++ b/Server/Controller/Message/M2A_ReloadHandler.cs @@ -1,7 +1,6 @@ using System; using Base; using Model; -using Object = Model.Object; namespace Controller { @@ -13,7 +12,7 @@ namespace Controller A2M_Reload a2MReload = new A2M_Reload(); try { - Object.ObjectManager.Register("Controller", DllHelper.GetController()); + ObjectManager.Instance.Register("Controller", DllHelper.GetController()); } catch (Exception e) { diff --git a/Server/Model/Server.Model.csproj b/Server/Model/Server.Model.csproj index 65782861e471b937d392096c817535a8abfe560f..6130d8ea6a31ae6e1c021b685ca6919994930fb4 100644 --- a/Server/Model/Server.Model.csproj +++ b/Server/Model/Server.Model.csproj @@ -173,9 +173,6 @@ Object\ILoader.cs - - Object\IStart.cs - Object\IUpdate.cs diff --git a/Unity/Assets/Editor/EditorInit.cs b/Unity/Assets/Editor/EditorInit.cs index a21fc545591b1994be163c76a67f18cbd69383a6..b5989547bdfe414b7ba4cb3ba6d731b4628052a2 100644 --- a/Unity/Assets/Editor/EditorInit.cs +++ b/Unity/Assets/Editor/EditorInit.cs @@ -3,7 +3,6 @@ using Base; using UnityEditor; using UnityEngine; using Model; -using Object = Model.Object; namespace MyEditor { @@ -12,6 +11,7 @@ namespace MyEditor { static EditorInit() { + ObjectManager.Instance.Register("Editor", typeof(EditorInit).Assembly); EditorApplication.update += Update; } @@ -24,12 +24,11 @@ namespace MyEditor try { - Object.ObjectManager.Update(); + ObjectManager.Instance.Update(); } catch (Exception e) { - Object.ObjectManager.Dispose(); - Object.ObjectManager = new ObjectManager(); + ObjectManager.Reset(); Log.Error(e.ToString()); } } diff --git a/Unity/Assets/Editor/ObjectManagerToolsEditor/ObjectManagerToolsWindow.cs b/Unity/Assets/Editor/ObjectManagerToolsEditor/ObjectManagerToolsWindow.cs index 98d8da6d13650e47704539646dc8849f1688921b..0c70a9ade792bb6540b2f06c90dd8233f041596b 100644 --- a/Unity/Assets/Editor/ObjectManagerToolsEditor/ObjectManagerToolsWindow.cs +++ b/Unity/Assets/Editor/ObjectManagerToolsEditor/ObjectManagerToolsWindow.cs @@ -7,14 +7,13 @@ public class ObjectManagerToolsWindow : EditorWindow [MenuItem("Tools/ObjectManagerTools/显示未Dispose的对象")] private static void ShowUnDisposeObjects() { - Log.Info(Object.ObjectManager.ToString()); + Log.Info(ObjectManager.Instance.ToString()); } [MenuItem("Tools/ObjectManagerTools/清除所有对象")] private static void ClearAllObjects() { - Object.ObjectManager.Dispose(); - Object.ObjectManager = new ObjectManager(); + ObjectManager.Reset(); } } \ No newline at end of file diff --git a/Unity/Assets/Scripts/Component/ConfigComponent.cs b/Unity/Assets/Scripts/Component/ConfigComponent.cs index 033907b1e21716fb58e1ee99e231335097a1a64a..fdef7be3c0eec5cff10a3f3b1e6a009f836a29c7 100644 --- a/Unity/Assets/Scripts/Component/ConfigComponent.cs +++ b/Unity/Assets/Scripts/Component/ConfigComponent.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; using System.Reflection; -using Base; -using Object = Model.Object; namespace Model { @@ -12,7 +10,7 @@ namespace Model public void Load() { - Assembly assembly = Object.ObjectManager.GetAssembly("Base"); + Assembly assembly = ObjectManager.Instance.GetAssembly("Base"); this.allConfig = new Dictionary(); Type[] types = assembly.GetTypes(); diff --git a/Unity/Assets/Scripts/Component/EventComponent.cs b/Unity/Assets/Scripts/Component/EventComponent.cs index bfdb60306b41ce27fa6270ac0d93cb8052ae4412..295155f35e02175b51befd8047e142a3fd45b62e 100644 --- a/Unity/Assets/Scripts/Component/EventComponent.cs +++ b/Unity/Assets/Scripts/Component/EventComponent.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Reflection; using Base; -using Object = Model.Object; namespace Model { @@ -30,7 +29,7 @@ namespace Model public void Load() { this.allEvents = new Dictionary>(); - Assembly[] assemblies = Object.ObjectManager.GetAssemblies(); + Assembly[] assemblies = ObjectManager.Instance.GetAssemblies(); foreach (Assembly assembly in assemblies) { Type[] types = assembly.GetTypes(); diff --git a/Unity/Assets/Scripts/Component/MessageDispatherComponent.cs b/Unity/Assets/Scripts/Component/MessageDispatherComponent.cs index 1e4748a32c133a81f29bc40f8f2a152518fddb0f..435978557fadbbfe3f98f4ba0bd89dc8a1da31f6 100644 --- a/Unity/Assets/Scripts/Component/MessageDispatherComponent.cs +++ b/Unity/Assets/Scripts/Component/MessageDispatherComponent.cs @@ -40,7 +40,7 @@ namespace Model this.handlers = new Dictionary>(); this.messageOpcode = new Dictionary(); - Assembly[] assemblies = Object.ObjectManager.GetAssemblies(); + Assembly[] assemblies = ObjectManager.Instance.GetAssemblies(); foreach (Assembly assembly in assemblies) { diff --git a/Unity/Assets/Scripts/Init.cs b/Unity/Assets/Scripts/Init.cs index ebccc9be3088e1debc36a803eaede7435380240a..4c0419e293f94ce7992cc1bcd6815e8930f06cf4 100644 --- a/Unity/Assets/Scripts/Init.cs +++ b/Unity/Assets/Scripts/Init.cs @@ -8,8 +8,8 @@ namespace Model { private void Start() { - Object.ObjectManager.Register("Model", typeof(Game).Assembly); - Object.ObjectManager.Register("Controller", DllHelper.GetController()); + ObjectManager.Instance.Register("Model", typeof(Game).Assembly); + ObjectManager.Instance.Register("Controller", DllHelper.GetController()); Game.Scene.AddComponent().Run(EventIdType.InitSceneStart); } @@ -18,7 +18,7 @@ namespace Model { try { - Object.ObjectManager.Update(); + ObjectManager.Instance.Update(); } catch (Exception e) { diff --git a/Unity/Assets/Scripts/Object/Component.cs b/Unity/Assets/Scripts/Object/Component.cs index 657948604c77db4474221a856b638d338940db54..50d234c25c53b10a903d50a700ce0d9acf79e2a6 100644 --- a/Unity/Assets/Scripts/Object/Component.cs +++ b/Unity/Assets/Scripts/Object/Component.cs @@ -2,9 +2,6 @@ namespace Model { - /// - /// Component的Id与Owner Entity Id一样 - /// [BsonKnownTypes(typeof(AConfigComponent))] public abstract class Component : Object { diff --git a/Unity/Assets/Scripts/Object/Entity.cs b/Unity/Assets/Scripts/Object/Entity.cs index 6f080763c4a923a7c0c4d64ef273ba2d2c15767d..c50d3ecdff80c5b54eb46a0371712b74998d76a3 100644 --- a/Unity/Assets/Scripts/Object/Entity.cs +++ b/Unity/Assets/Scripts/Object/Entity.cs @@ -66,7 +66,7 @@ namespace Model this.components.Add(component); this.componentDict.Add(component.GetType(), component); - ObjectManager.Awake(component.Id); + ObjectManager.Instance.Awake(component); return component; } @@ -87,7 +87,7 @@ namespace Model this.components.Add(component); this.componentDict.Add(component.GetType(), component); - ObjectManager.Awake(component.Id, p1); + ObjectManager.Instance.Awake(component, p1); return component; } @@ -108,7 +108,7 @@ namespace Model this.components.Add(component); this.componentDict.Add(component.GetType(), component); - ObjectManager.Awake(component.Id, p1, p2); + ObjectManager.Instance.Awake(component, p1, p2); return component; } @@ -130,7 +130,7 @@ namespace Model this.components.Add(component); this.componentDict.Add(component.GetType(), component); - ObjectManager.Awake(component.Id, p1, p2, p3); + ObjectManager.Instance.Awake(component, p1, p2, p3); return component; } @@ -147,7 +147,7 @@ namespace Model } this.components.Add(component); this.componentDict.Add(component.GetType(), component); - ObjectManager.Awake(component.Id); + ObjectManager.Instance.Awake(component); } public void RemoveComponent() where K : Component diff --git a/Unity/Assets/Scripts/Object/IStart.cs b/Unity/Assets/Scripts/Object/IStart.cs deleted file mode 100644 index 0e168464c05c59f917890d366413c2697fc9078c..0000000000000000000000000000000000000000 --- a/Unity/Assets/Scripts/Object/IStart.cs +++ /dev/null @@ -1,10 +0,0 @@ -namespace Model -{ - /// - /// World的Componet实现该接口后,会在World.Start时调用该Start方法 - /// - public interface IStart - { - void Start(); - } -} \ No newline at end of file diff --git a/Unity/Assets/Scripts/Object/IStart.cs.meta b/Unity/Assets/Scripts/Object/IStart.cs.meta deleted file mode 100644 index 01ecbfa1b2e6446201c99e30c412fe26c1d160fe..0000000000000000000000000000000000000000 --- a/Unity/Assets/Scripts/Object/IStart.cs.meta +++ /dev/null @@ -1,12 +0,0 @@ -fileFormatVersion: 2 -guid: ee0b99021f2abfb408ca33f2f551a1d8 -timeCreated: 1474942922 -licenseType: Pro -MonoImporter: - serializedVersion: 2 - defaultReferences: [] - executionOrder: 0 - icon: {instanceID: 0} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Unity/Assets/Scripts/Object/Object.cs b/Unity/Assets/Scripts/Object/Object.cs index 517345fdf3480cdf91c2f8c69fce618429614f27..79a860b4bd8df98d852e195c89c44e3576b9ee83 100644 --- a/Unity/Assets/Scripts/Object/Object.cs +++ b/Unity/Assets/Scripts/Object/Object.cs @@ -7,22 +7,19 @@ namespace Model { public abstract class Object: IDisposable, ISupportInitialize { - [BsonIgnore] - public static ObjectManager ObjectManager = new ObjectManager(); - [BsonId] public long Id { get; private set; } protected Object() { Id = IdGenerater.GenerateId(); - ObjectManager.Add(this); + ObjectManager.Instance.Add(this); } protected Object(long id) { this.Id = id; - ObjectManager.Add(this); + ObjectManager.Instance.Add(this); } public virtual void Dispose() @@ -32,7 +29,7 @@ namespace Model return; } - ObjectManager.Remove(this.Id); + ObjectManager.Instance.Remove(this); this.Id = 0; } diff --git a/Unity/Assets/Scripts/Object/ObjectManager.cs b/Unity/Assets/Scripts/Object/ObjectManager.cs index 10b560020d5ee4ff66c02ee7624a9fa059019835..ce8c9f4db0559c5cf7a144fefa86f9f409e9af5e 100644 --- a/Unity/Assets/Scripts/Object/ObjectManager.cs +++ b/Unity/Assets/Scripts/Object/ObjectManager.cs @@ -39,19 +39,33 @@ namespace Model private Dictionary objectEvents; - private readonly Dictionary objects = new Dictionary(); + private readonly HashSet objects = new HashSet(); + private readonly HashSet updates = new HashSet(); + private readonly HashSet loaders = new HashSet(); - private List starts = new List(); - private List newStarts = new List(); + private static ObjectManager instance = new ObjectManager(); - private List updates = new List(3000); - private List newUpdates = new List(3000); + public static ObjectManager Instance + { + get + { + return instance; + } + } - private readonly List loaders = new List(); + private ObjectManager() + { + } + + public static void Reset() + { + instance.Dispose(); + instance = new ObjectManager(); + } public void Dispose() { - foreach (Object o in this.objects.Values.ToArray()) + foreach (Object o in this.objects.ToArray()) { o.Dispose(); } @@ -99,13 +113,8 @@ namespace Model private void Load() { - foreach (long id in this.loaders) + foreach (Object obj in this.loaders) { - Object obj; - if (!this.objects.TryGetValue(id, out obj)) - { - continue; - } IObjectEvent objectEvent; if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent)) { @@ -128,44 +137,51 @@ namespace Model return; } - this.objects.Add(obj.Id, obj); + this.objects.Add(obj); IObjectEvent objectEvent; if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent)) { return; } - IStart iStart = objectEvent as IStart; - if (iStart != null) - { - this.newStarts.Add(obj.Id); - } - IUpdate iUpdate = objectEvent as IUpdate; if (iUpdate != null) { - this.newUpdates.Add(obj.Id); + this.updates.Add(obj); } ILoader iLoader = objectEvent as ILoader; if (iLoader != null) { - this.loaders.Add(obj.Id); + this.loaders.Add(obj); } } - public void Remove(long id) + public void Remove(Object obj) { - this.objects.Remove(id); - } + this.objects.Remove(obj); - public void Awake(long id) - { - Object obj; - if (!objects.TryGetValue(id, out obj)) + IObjectEvent objectEvent; + if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent)) { return; } + + IUpdate iUpdate = objectEvent as IUpdate; + if (iUpdate != null) + { + this.updates.Remove(obj); + } + + ILoader iLoader = objectEvent as ILoader; + if (iLoader != null) + { + this.loaders.Remove(obj); + } + } + + public void Awake(Object obj) + { IObjectEvent objectEvent; if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent)) { @@ -180,13 +196,8 @@ namespace Model iAwake.Awake(); } - public void Awake(long id, P1 p1) + public void Awake(Object obj, P1 p1) { - Object obj; - if (!objects.TryGetValue(id, out obj)) - { - return; - } IObjectEvent objectEvent; if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent)) { @@ -201,13 +212,8 @@ namespace Model iAwake.Awake(p1); } - public void Awake(long id, P1 p1, P2 p2) + public void Awake(Object obj, P1 p1, P2 p2) { - Object obj; - if (!objects.TryGetValue(id, out obj)) - { - return; - } IObjectEvent objectEvent; if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent)) { @@ -222,13 +228,8 @@ namespace Model iAwake.Awake(p1, p2); } - public void Awake(long id, P1 p1, P2 p2, P3 p3) + public void Awake(Object obj, P1 p1, P2 p2, P3 p3) { - Object obj; - if (!objects.TryGetValue(id, out obj)) - { - return; - } IObjectEvent objectEvent; if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent)) { @@ -242,49 +243,11 @@ namespace Model objectEvent.SetValue(obj); iAwake.Awake(p1, p2, p3); } - - private void Start() - { - starts = newStarts; - newStarts = new List(); - foreach (long id in starts) - { - Object obj; - if (!this.objects.TryGetValue(id, out obj)) - { - continue; - } - IObjectEvent objectEvent; - if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent)) - { - continue; - } - IStart iStart = objectEvent as IStart; - if (iStart == null) - { - continue; - } - objectEvent.SetValue(obj); - iStart.Start(); - } - } public void Update() { - this.Start(); - - // 交换update - List tmpUpdate = updates; - updates = newUpdates; - newUpdates = tmpUpdate; - newUpdates.Clear(); - foreach (long id in updates) + foreach (Object obj in updates) { - Object obj; - if (!objects.TryGetValue(id, out obj)) - { - continue; - } IObjectEvent objectEvent; if (!objectEvents.TryGetValue(obj.GetType(), out objectEvent)) { @@ -295,7 +258,6 @@ namespace Model { continue; } - newUpdates.Add(id); objectEvent.SetValue(obj); try { @@ -311,7 +273,7 @@ namespace Model public override string ToString() { var info = new Dictionary(); - foreach (Object obj in objects.Values) + foreach (Object obj in objects) { if (info.ContainsKey(obj.GetType().Name)) { @@ -330,7 +292,7 @@ namespace Model sb.Append($"{info[key],10} {key}\r\n"); } - sb.Append($"\r\n start: {newStarts.Count}, update: {newUpdates.Count} total: {this.objects.Count}"); + sb.Append($"\r\n update: {this.updates.Count} total: {this.objects.Count}"); return sb.ToString(); } } diff --git a/Unity/Unity.CSharp.csproj b/Unity/Unity.CSharp.csproj index fbf9d866e45e25ffe6cc2f857b8b9033f0caf82b..c946e4d8a1f55b7d8d03631a3b2813d030f8aa61 100644 --- a/Unity/Unity.CSharp.csproj +++ b/Unity/Unity.CSharp.csproj @@ -130,7 +130,6 @@ -