From 341f153ad17159e4f5ac43012b4d0070f86c453b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=99=88=E6=80=9D=E6=B5=B7?= <1464576565@qq.com> Date: Sun, 27 Apr 2025 19:27:48 +0800 Subject: [PATCH] modify --- Runtime/Base/Module/ModuleSystem.cs | 180 ++++++++++------------ Runtime/ObjectPool/ObjectPoolComponent.cs | 2 +- 2 files changed, 84 insertions(+), 98 deletions(-) diff --git a/Runtime/Base/Module/ModuleSystem.cs b/Runtime/Base/Module/ModuleSystem.cs index bd1bb55..3d4a9ea 100644 --- a/Runtime/Base/Module/ModuleSystem.cs +++ b/Runtime/Base/Module/ModuleSystem.cs @@ -11,22 +11,32 @@ namespace AlicizaX private static readonly Dictionary _moduleMaps = new Dictionary(DesignModuleCount); private static readonly GameFrameworkLinkedList _modules = new GameFrameworkLinkedList(); + // Update systems private static readonly GameFrameworkLinkedList _updateModules = new GameFrameworkLinkedList(); private static readonly IModuleUpdate[] _updateExecuteArray = new IModuleUpdate[DesignModuleCount]; + private static int _updateExecuteCount; + // LateUpdate systems private static readonly GameFrameworkLinkedList _lateUpdateModules = new GameFrameworkLinkedList(); private static readonly IModuleLateUpdate[] _lateUpdateExecuteArray = new IModuleLateUpdate[DesignModuleCount]; + private static int _lateUpdateExecuteCount; + // FixedUpdate systems private static readonly GameFrameworkLinkedList _fixedUpdateModules = new GameFrameworkLinkedList(); private static readonly IModuleFixedUpdate[] _fixedUpdateExecuteArray = new IModuleFixedUpdate[DesignModuleCount]; + private static int _fixedUpdateExecuteCount; + // Gizmos systems private static readonly GameFrameworkLinkedList _gizmosUpdateModules = new GameFrameworkLinkedList(); private static readonly IModuleDrawGizmos[] _gizmosUpdateExecuteArray = new IModuleDrawGizmos[DesignModuleCount]; + private static int _gizmosExecuteCount; - + // GUI systems private static readonly GameFrameworkLinkedList _guiUpdateModules = new GameFrameworkLinkedList(); private static readonly IModuleGUI[] _guiUpdateExecuteArray = new IModuleGUI[DesignModuleCount]; + private static int _guiExecuteCount; + // Dirty flags private static bool _isExecuteListDirty; private static bool _isLateExecuteListDirty; private static bool _isFixedExecuteListDirty; @@ -35,23 +45,29 @@ namespace AlicizaX internal static void Dispose() { + // Clear all modules and execute arrays _updateModules.Clear(); Array.Clear(_updateExecuteArray, 0, _updateExecuteArray.Length); + _updateExecuteCount = 0; _lateUpdateModules.Clear(); Array.Clear(_lateUpdateExecuteArray, 0, _lateUpdateExecuteArray.Length); + _lateUpdateExecuteCount = 0; _fixedUpdateModules.Clear(); Array.Clear(_fixedUpdateExecuteArray, 0, _fixedUpdateExecuteArray.Length); + _fixedUpdateExecuteCount = 0; _gizmosUpdateModules.Clear(); Array.Clear(_gizmosUpdateExecuteArray, 0, _gizmosUpdateExecuteArray.Length); - + _gizmosExecuteCount = 0; _guiUpdateModules.Clear(); Array.Clear(_guiUpdateExecuteArray, 0, _guiUpdateExecuteArray.Length); + _guiExecuteCount = 0; - for (LinkedListNode current = _modules.Last; current != null; current = current.Previous) + // Dispose all modules + for (var current = _modules.Last; current != null; current = current.Previous) { current.Value.Dispose(); } @@ -87,13 +103,10 @@ namespace AlicizaX #region Public System - public static T RegisterModule(Type implType) - { - return (T)RegisterModule(typeof(T), implType); - } - - public static IModule RegisterModule(Type interfaceType, Type implType) + public static T RegisterModule() where T : IModule where TImple : class, T, new() { + Type interfaceType = typeof(T); + Type implType = typeof(TImple); if (!interfaceType.IsInterface) { throw new GameFrameworkException(Utility.Text.Format("You must register module by interface, but '{0}' is not.", interfaceType.FullName)); @@ -106,7 +119,7 @@ namespace AlicizaX if (!typeof(IModule).IsAssignableFrom(interfaceType)) { - throw new GameFrameworkException(Utility.Text.Format("You must register module by Class and not Interface and Abstract, but '{0}' is not.", implType.FullName)); + throw new GameFrameworkException(Utility.Text.Format("Module must implement IModule.", interfaceType.FullName)); } if (GetModule(interfaceType) != null) @@ -115,13 +128,13 @@ namespace AlicizaX return default; } - return SetModuleInstance(interfaceType, implType); + TImple impl = new TImple(); + return (T)SetModuleInstance(interfaceType, impl); } public static T GetModule() where T : class { Type interfaceType = typeof(T); - if (!interfaceType.IsInterface) { throw new GameFrameworkException(Utility.Text.Format("You must get module by interface, but '{0}' is not.", interfaceType.FullName)); @@ -132,28 +145,21 @@ namespace AlicizaX public static IModule GetModule(Type moduleType) { - if (_moduleMaps.TryGetValue(moduleType, out var ret)) - { - return ret; - } - - return default; + return _moduleMaps.TryGetValue(moduleType, out var ret) ? ret : default; } - private static IModule SetModuleInstance(Type interfaceType, Type implType) + private static IModule SetModuleInstance(Type interfaceType, TImpl impl) where TImpl : class, IModule { - IModule module = (IModule)Activator.CreateInstance(implType); + _moduleMaps[interfaceType] = impl; + _modules.AddLast(impl); - _moduleMaps[interfaceType] = module; - _modules.AddLast(module); - - if (module is IModuleAwake awakeSystem) + if (impl is IModuleAwake awakeSystem) { awakeSystem.Awake(); } - CheckSystemLife(module); - return module; + CheckSystemLife(impl); + return impl; } #endregion @@ -162,46 +168,41 @@ namespace AlicizaX { if (module is IModuleUpdate updateSystem) { - BindSystemLife(updateSystem, _updateModules, ref _isExecuteListDirty); + BindSystemLife(updateSystem, _updateModules, ref _isExecuteListDirty); } if (module is IModuleLateUpdate lateUpdate) { - BindSystemLife(lateUpdate, _lateUpdateModules, ref _isLateExecuteListDirty); + BindSystemLife(lateUpdate, _lateUpdateModules, ref _isLateExecuteListDirty); } if (module is IModuleFixedUpdate fixedUpdate) { - BindSystemLife(fixedUpdate, _fixedUpdateModules, ref _isFixedExecuteListDirty); + BindSystemLife(fixedUpdate, _fixedUpdateModules, ref _isFixedExecuteListDirty); } if (module is IModuleDrawGizmos drawGizmosUpdate) { - BindSystemLife(drawGizmosUpdate, _gizmosUpdateModules, ref _isGizmosExecuteListDirty); + BindSystemLife(drawGizmosUpdate, _gizmosUpdateModules, ref _isGizmosExecuteListDirty); } if (module is IModuleGUI guiUpdate) { - BindSystemLife(guiUpdate, _guiUpdateModules, ref _isGUIExecuteListDirty); + BindSystemLife(guiUpdate, _guiUpdateModules, ref _isGUIExecuteListDirty); } } - internal static void BindSystemLife(T system, GameFrameworkLinkedList updateModule, ref bool executeDirty) where T : IExecuteSystem + private static void BindSystemLife(T system, GameFrameworkLinkedList updateModule, ref bool executeDirty) where T : IExecuteSystem { - LinkedListNode currentUpdate = updateModule.First; - while (currentUpdate != null) + var current = updateModule.First; + while (current != null && system.Priority <= current.Value.Priority) { - if (system.Priority > currentUpdate.Value.Priority) - { - break; - } - - currentUpdate = currentUpdate.Next; + current = current.Next; } - if (currentUpdate != null) + if (current != null) { - updateModule.AddBefore(currentUpdate, system); + updateModule.AddBefore(current, system); } else { @@ -215,71 +216,61 @@ namespace AlicizaX private static void BuildExecuteList() { - if (_isExecuteListDirty) + if (!_isExecuteListDirty) return; + _isExecuteListDirty = false; + _updateExecuteCount = 0; + foreach (var module in _updateModules) { - _isExecuteListDirty = false; - Array.Clear(_updateExecuteArray, 0, _updateExecuteArray.Length); - int index = 0; - foreach (var module in _updateModules) - { - _updateExecuteArray[index++] = module; - } + if (_updateExecuteCount >= _updateExecuteArray.Length) break; + _updateExecuteArray[_updateExecuteCount++] = module; } } private static void BuildLateExecuteList() { - if (_isLateExecuteListDirty) + if (!_isLateExecuteListDirty) return; + _isLateExecuteListDirty = false; + _lateUpdateExecuteCount = 0; + foreach (var module in _lateUpdateModules) { - _isLateExecuteListDirty = false; - Array.Clear(_lateUpdateExecuteArray, 0, _lateUpdateExecuteArray.Length); - int index = 0; - foreach (var module in _lateUpdateModules) - { - _lateUpdateExecuteArray[index++] = module; - } + if (_lateUpdateExecuteCount >= _lateUpdateExecuteArray.Length) break; + _lateUpdateExecuteArray[_lateUpdateExecuteCount++] = module; } } private static void BuildFixedExecuteList() { - if (_isFixedExecuteListDirty) + if (!_isFixedExecuteListDirty) return; + _isFixedExecuteListDirty = false; + _fixedUpdateExecuteCount = 0; + foreach (var module in _fixedUpdateModules) { - _isFixedExecuteListDirty = false; - Array.Clear(_fixedUpdateExecuteArray, 0, _fixedUpdateExecuteArray.Length); - int index = 0; - foreach (var module in _fixedUpdateModules) - { - _fixedUpdateExecuteArray[index++] = module; - } + if (_fixedUpdateExecuteCount >= _fixedUpdateExecuteArray.Length) break; + _fixedUpdateExecuteArray[_fixedUpdateExecuteCount++] = module; } } private static void BuildGizmosExecuteList() { - if (_isGizmosExecuteListDirty) + if (!_isGizmosExecuteListDirty) return; + _isGizmosExecuteListDirty = false; + _gizmosExecuteCount = 0; + foreach (var module in _gizmosUpdateModules) { - _isGizmosExecuteListDirty = false; - Array.Clear(_gizmosUpdateExecuteArray, 0, _gizmosUpdateExecuteArray.Length); - int index = 0; - foreach (var module in _gizmosUpdateModules) - { - _gizmosUpdateExecuteArray[index++] = module; - } + if (_gizmosExecuteCount >= _gizmosUpdateExecuteArray.Length) break; + _gizmosUpdateExecuteArray[_gizmosExecuteCount++] = module; } } private static void BuildGUIExecuteList() { - if (_isGUIExecuteListDirty) + if (!_isGUIExecuteListDirty) return; + _isGUIExecuteListDirty = false; + _guiExecuteCount = 0; + foreach (var module in _guiUpdateModules) { - _isGUIExecuteListDirty = false; - Array.Clear(_guiUpdateExecuteArray, 0, _guiUpdateExecuteArray.Length); - int index = 0; - foreach (var module in _guiUpdateModules) - { - _guiUpdateExecuteArray[index++] = module; - } + if (_guiExecuteCount >= _guiUpdateExecuteArray.Length) break; + _guiUpdateExecuteArray[_guiExecuteCount++] = module; } } @@ -290,50 +281,45 @@ namespace AlicizaX internal static void UpdateExecuteList(float elapseSeconds, float realElapseSeconds) { BuildExecuteList(); - int executeCount = _updateExecuteArray.Length; - for (int i = 0; i < executeCount; i++) + for (int i = 0; i < _updateExecuteCount; i++) { - _updateExecuteArray[i]?.Update(elapseSeconds, realElapseSeconds); + _updateExecuteArray[i].Update(elapseSeconds, realElapseSeconds); } } internal static void UpdateLateExecuteList() { BuildLateExecuteList(); - int executeCount = _lateUpdateExecuteArray.Length; - for (int i = 0; i < executeCount; i++) + for (int i = 0; i < _lateUpdateExecuteCount; i++) { - _lateUpdateExecuteArray[i]?.LateUpdate(); + _lateUpdateExecuteArray[i].LateUpdate(); } } internal static void UpdateFixedExecuteList() { BuildFixedExecuteList(); - int executeCount = _fixedUpdateExecuteArray.Length; - for (int i = 0; i < executeCount; i++) + for (int i = 0; i < _fixedUpdateExecuteCount; i++) { - _fixedUpdateExecuteArray[i]?.FixedUpdate(); + _fixedUpdateExecuteArray[i].FixedUpdate(); } } internal static void UpdateGizmosExecuteList() { BuildGizmosExecuteList(); - int executeCount = _gizmosUpdateExecuteArray.Length; - for (int i = 0; i < executeCount; i++) + for (int i = 0; i < _gizmosExecuteCount; i++) { - _gizmosUpdateExecuteArray[i]?.DrawGizmos(); + _gizmosUpdateExecuteArray[i].DrawGizmos(); } } internal static void UpdateGUIExecuteList() { BuildGUIExecuteList(); - int executeCount = _guiUpdateExecuteArray.Length; - for (int i = 0; i < executeCount; i++) + for (int i = 0; i < _guiExecuteCount; i++) { - _guiUpdateExecuteArray[i]?.OnGUI(); + _guiUpdateExecuteArray[i].OnGUI(); } } diff --git a/Runtime/ObjectPool/ObjectPoolComponent.cs b/Runtime/ObjectPool/ObjectPoolComponent.cs index 91ee46c..42b0ab3 100644 --- a/Runtime/ObjectPool/ObjectPoolComponent.cs +++ b/Runtime/ObjectPool/ObjectPoolComponent.cs @@ -20,7 +20,7 @@ namespace AlicizaX private void Awake() { - _mObjectPoolModule = ModuleSystem.RegisterModule(typeof(ObjectPoolModule)); + _mObjectPoolModule = ModuleSystem.RegisterModule(); if (_mObjectPoolModule == null) { Log.Error("Object pool manager is invalid.");