From 9509da8b95b0ba60b4b015113212548adebda96b Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Sun, 26 Mar 2023 11:20:02 +0800 Subject: [PATCH] Init commit/Add systems debug/Add new runners --- DragonECS-Unity.asmdef | 16 +++ DragonECS-Unity.asmdef.meta | 7 ++ LICENSE.meta | 7 ++ README.md.meta | 7 ++ src.meta | 8 ++ src/GameObjectRef.cs | 106 +++++++++++++++++++ src/GameObjectRef.cs.meta | 11 ++ src/Runner.meta | 8 ++ src/Runner/IEcsFixedRunSystem.cs | 15 +++ src/Runner/IEcsFixedRunSystem.cs.meta | 11 ++ src/Runner/IEcsLateRunSystem.cs | 15 +++ src/Runner/IEcsLateRunSystem.cs.meta | 11 ++ src/SystemsDebugSystem.cs | 127 +++++++++++++++++++++++ src/SystemsDebugSystem.cs.meta | 11 ++ src/Utils.meta | 8 ++ src/Utils/DebugColorAttributeExt.cs | 12 +++ src/Utils/DebugColorAttributeExt.cs.meta | 11 ++ src/WorldDebugSystem.cs | 11 ++ src/WorldDebugSystem.cs.meta | 11 ++ 19 files changed, 413 insertions(+) create mode 100644 DragonECS-Unity.asmdef create mode 100644 DragonECS-Unity.asmdef.meta create mode 100644 LICENSE.meta create mode 100644 README.md.meta create mode 100644 src.meta create mode 100644 src/GameObjectRef.cs create mode 100644 src/GameObjectRef.cs.meta create mode 100644 src/Runner.meta create mode 100644 src/Runner/IEcsFixedRunSystem.cs create mode 100644 src/Runner/IEcsFixedRunSystem.cs.meta create mode 100644 src/Runner/IEcsLateRunSystem.cs create mode 100644 src/Runner/IEcsLateRunSystem.cs.meta create mode 100644 src/SystemsDebugSystem.cs create mode 100644 src/SystemsDebugSystem.cs.meta create mode 100644 src/Utils.meta create mode 100644 src/Utils/DebugColorAttributeExt.cs create mode 100644 src/Utils/DebugColorAttributeExt.cs.meta create mode 100644 src/WorldDebugSystem.cs create mode 100644 src/WorldDebugSystem.cs.meta diff --git a/DragonECS-Unity.asmdef b/DragonECS-Unity.asmdef new file mode 100644 index 0000000..1607d30 --- /dev/null +++ b/DragonECS-Unity.asmdef @@ -0,0 +1,16 @@ +{ + "name": "DCFApixels.DragonECS-Unity", + "rootNamespace": "DCFApixels", + "references": [ + "GUID:abb125fa67fff1e45914d0825236f608" + ], + "includePlatforms": [], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/DragonECS-Unity.asmdef.meta b/DragonECS-Unity.asmdef.meta new file mode 100644 index 0000000..4dcf48a --- /dev/null +++ b/DragonECS-Unity.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: e1b0feb9d48fee64da21a08eeb2fafd4 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/LICENSE.meta b/LICENSE.meta new file mode 100644 index 0000000..c2c0785 --- /dev/null +++ b/LICENSE.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 0ef75f5af61ea7c4ea8fe3ef92d02b35 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/README.md.meta b/README.md.meta new file mode 100644 index 0000000..13eb7ed --- /dev/null +++ b/README.md.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 1f63a4d70d00eb641ba88fa50962d963 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src.meta b/src.meta new file mode 100644 index 0000000..f6047d1 --- /dev/null +++ b/src.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6d12f3413368d5c4aa87dcfe7c4c2792 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/GameObjectRef.cs b/src/GameObjectRef.cs new file mode 100644 index 0000000..4820bd5 --- /dev/null +++ b/src/GameObjectRef.cs @@ -0,0 +1,106 @@ +using System.Runtime.CompilerServices; +using UnityEngine; +#if UNITY_EDITOR +using UnityEditor; +#endif + +namespace DCFApixels.DragonECS.Unity +{ + public struct GameObjectRef + { + public GameObject gameObject; + public Transform transform; + + public string Name + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => gameObject.name; + } + + public GameObjectRef(GameObject gameObject) + { + this.gameObject = gameObject; + transform = gameObject.transform; + } + } + + public enum GameObjectIcon : byte + { + NONE, + Label_Gray, + Label_Blue, + Label_Teal, + Label_Green, + Label_Yellow, + Label_Orange, + Label_Red, + Label_Purple, + Circle_Gray, + Circle_Blue, + Circle_Teal, + Circle_Green, + Circle_Yellow, + Circle_Orange, + Circle_Red, + Circle_Purple, + Diamond_Gray, + Diamond_Blue, + Diamond_Teal, + Diamond_Green, + Diamond_Yellow, + Diamond_Orange, + Diamond_Red, + Diamond_Purple + } + public static class GameObjectIconConsts + { + public const int RAW_LABEL_ICON_LAST = (int)GameObjectIcon.Label_Purple; + } + + public static class GameObjectRefExt + { + public static ent NewEntityWithGameObject(this IEcsWorld self, string name = "Entity", GameObjectIcon icon = GameObjectIcon.NONE) + { + ent result = self.NewEntity(); + GameObject newGameObject = new GameObject(name); + newGameObject.AddComponent()._entity = result; + result.Write() = new GameObjectRef(newGameObject); + +#if UNITY_EDITOR + if (icon != GameObjectIcon.NONE) + { + string contentName; + int number = (int)icon - 1; + if (number < GameObjectIconConsts.RAW_LABEL_ICON_LAST) + { + contentName = $"sv_label_{number}"; + } + else + { + number -= GameObjectIconConsts.RAW_LABEL_ICON_LAST; + contentName = $"sv_icon_dot{number}_pix16_gizmo"; + } + GUIContent iconContent = EditorGUIUtility.IconContent(contentName); + EditorGUIUtility.SetIconForObject(newGameObject, (Texture2D)iconContent.image); + } +#endif + + return result; + } + } + + public class EcsEntity : MonoBehaviour + { + internal ent _entity; + public ent entity + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _entity; + } + public bool IsAlive + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => _entity.IsAlive(); + } + } +} diff --git a/src/GameObjectRef.cs.meta b/src/GameObjectRef.cs.meta new file mode 100644 index 0000000..1b88100 --- /dev/null +++ b/src/GameObjectRef.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 909b2b01fa1e58b4e9e739827e36cff4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Runner.meta b/src/Runner.meta new file mode 100644 index 0000000..05d544d --- /dev/null +++ b/src/Runner.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 70c8e3cb9125ee14fad9fdee48c3e8ba +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Runner/IEcsFixedRunSystem.cs b/src/Runner/IEcsFixedRunSystem.cs new file mode 100644 index 0000000..313cb50 --- /dev/null +++ b/src/Runner/IEcsFixedRunSystem.cs @@ -0,0 +1,15 @@ +namespace DCFApixels.DragonECS.Unity +{ + public interface IEcsFixedRunSystem : IEcsSystem + { + public void FixedRun(EcsSystems systems); + } + + public class EcsFixedRunSystemRunner : EcsRunner, IEcsFixedRunSystem + { + void IEcsFixedRunSystem.FixedRun(EcsSystems systems) + { + foreach (var item in targets) item.FixedRun(systems); + } + } +} diff --git a/src/Runner/IEcsFixedRunSystem.cs.meta b/src/Runner/IEcsFixedRunSystem.cs.meta new file mode 100644 index 0000000..1f3937e --- /dev/null +++ b/src/Runner/IEcsFixedRunSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: aff69967c9d73594283178ea5530cf71 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Runner/IEcsLateRunSystem.cs b/src/Runner/IEcsLateRunSystem.cs new file mode 100644 index 0000000..c32b41c --- /dev/null +++ b/src/Runner/IEcsLateRunSystem.cs @@ -0,0 +1,15 @@ +namespace DCFApixels.DragonECS.Unity +{ + public interface IEcsLateRunSystem : IEcsSystem + { + public void LateRun(EcsSystems systems); + } + + public class EcsLateRunSystemRunner : EcsRunner, IEcsLateRunSystem + { + void IEcsLateRunSystem.LateRun(EcsSystems systems) + { + foreach (var item in targets) item.LateRun(systems); + } + } +} diff --git a/src/Runner/IEcsLateRunSystem.cs.meta b/src/Runner/IEcsLateRunSystem.cs.meta new file mode 100644 index 0000000..2b4d105 --- /dev/null +++ b/src/Runner/IEcsLateRunSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 385a6c66660032944ad2cce7130715d7 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/SystemsDebugSystem.cs b/src/SystemsDebugSystem.cs new file mode 100644 index 0000000..b1ce958 --- /dev/null +++ b/src/SystemsDebugSystem.cs @@ -0,0 +1,127 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + + +namespace DCFApixels.DragonECS.Unity +{ + public class SystemsDebugSystem : IEcsPreInitSystem + { + private string _name; + public SystemsDebugSystem(string name = "Systems") + { + _name = name; + } + + void IEcsPreInitSystem.PreInit(EcsSystems systems) + { + SystemsDebugMonitor monitor = new GameObject(EcsConsts.DEBUG_PREFIX + _name).AddComponent(); + monitor.source = this; + monitor.systems = systems; + monitor.systemsName = _name; + Object.DontDestroyOnLoad(monitor.gameObject); + } + } + + public class SystemsDebugMonitor : MonoBehaviour + { + internal SystemsDebugSystem source; + internal EcsSystems systems; + internal string systemsName; + internal bool showInterfaces = false; + } + +#if UNITY_EDITOR + + namespace Editors + { + using System; + using System.Linq; + using UnityEditor; + + [CustomEditor(typeof(SystemsDebugMonitor))] + public class SystemsDebugMonitorEditor : Editor + { + private DebugColorAttribute _fakeDebugColorAttribute = new DebugColorAttribute(DebugColor.White); + private Type _debugColorAttributeType = typeof(DebugColorAttribute); + private GUIStyle _headerStyle; + private GUIStyle _interfacesStyle; + private Color _interfaceColor = new Color(0.96f, 1f, 0.16f); + private SystemsDebugMonitor Target => (SystemsDebugMonitor)target; + public override void OnInspectorGUI() + { + if (Target.source == null) + return; + if(_headerStyle == null) + { + _headerStyle = new GUIStyle(EditorStyles.boldLabel); + _interfacesStyle = new GUIStyle(EditorStyles.helpBox); + _interfacesStyle.hover.textColor = _interfaceColor; + _interfacesStyle.focused.textColor = _interfaceColor; + _interfacesStyle.active.textColor = _interfaceColor; + _interfacesStyle.normal.textColor = _interfaceColor; + _headerStyle.fontSize = 28; + } + + GUILayout.Label("[Systems]", _headerStyle); + + Target.showInterfaces = EditorGUILayout.Toggle("Show Interfaces", Target.showInterfaces); + + foreach (var item in Target.systems.AllSystems) + { + DrawSystem(item); + } + + GUILayout.Label("[Runners]", _headerStyle); + + foreach (var item in Target.systems.AllRunners) + { + DrawRunner(item.Value); + } + } + + private void DrawSystem(IEcsSystem system) + { + Type type = system.GetType(); + string name = type.Name; + Color color = (GetAttribute(type) ?? _fakeDebugColorAttribute).GetUnityColor(); + + Color defaultBackgroundColor = GUI.backgroundColor; + + GUI.backgroundColor = color; + GUILayout.BeginVertical(EditorStyles.helpBox); + if (Target.showInterfaces) + { + GUILayout.Label(string.Join(", ", type.GetInterfaces().Select(o => o.Name)), _interfacesStyle); + } + GUILayout.Label(name, EditorStyles.boldLabel); + GUILayout.EndVertical(); + + GUI.backgroundColor = defaultBackgroundColor; + } + + private void DrawRunner(IEcsRunner runner) + { + Type type = runner.GetType(); + Color color = (GetAttribute(type) ?? _fakeDebugColorAttribute).GetUnityColor(); + Color defaultBackgroundColor = GUI.backgroundColor; + GUI.backgroundColor = color; + GUILayout.BeginVertical(EditorStyles.helpBox); + GUILayout.Label(type.Name, EditorStyles.boldLabel); + GUILayout.Label(string.Join(", ", runner.Targets.Cast().Select(o => o.GetType().Name))); + GUILayout.EndVertical(); + GUI.backgroundColor = defaultBackgroundColor; + } + + private TAttribute GetAttribute(Type target) where TAttribute : Attribute + { + var result = target.GetCustomAttributes(_debugColorAttributeType, false); + if (result.Length > 0) + return (TAttribute)result[0]; + return null; + } + } + } + +#endif +} diff --git a/src/SystemsDebugSystem.cs.meta b/src/SystemsDebugSystem.cs.meta new file mode 100644 index 0000000..22bfdc1 --- /dev/null +++ b/src/SystemsDebugSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 6ed7ff62966b45141b388716e91dfac0 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Utils.meta b/src/Utils.meta new file mode 100644 index 0000000..9d54fbf --- /dev/null +++ b/src/Utils.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 6c2eeaf69783f0b41a968e7e4d539cf3 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Utils/DebugColorAttributeExt.cs b/src/Utils/DebugColorAttributeExt.cs new file mode 100644 index 0000000..a9aed87 --- /dev/null +++ b/src/Utils/DebugColorAttributeExt.cs @@ -0,0 +1,12 @@ +using UnityEngine; + +namespace DCFApixels.DragonECS.Unity +{ + public static class DebugColorAttributeExt + { + public static Color GetUnityColor(this DebugColorAttribute self) + { + return new Color(self.r / 255f, self.g / 255f, self.b / 255f); + } + } +} diff --git a/src/Utils/DebugColorAttributeExt.cs.meta b/src/Utils/DebugColorAttributeExt.cs.meta new file mode 100644 index 0000000..ff3cfa6 --- /dev/null +++ b/src/Utils/DebugColorAttributeExt.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1fae683793d3ed345ad3bf19731db13e +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/WorldDebugSystem.cs b/src/WorldDebugSystem.cs new file mode 100644 index 0000000..e34bf74 --- /dev/null +++ b/src/WorldDebugSystem.cs @@ -0,0 +1,11 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; + +namespace DCFApixels.DragonECS.Unity +{ + public class WorldDebugSystem : MonoBehaviour + { + + } +} diff --git a/src/WorldDebugSystem.cs.meta b/src/WorldDebugSystem.cs.meta new file mode 100644 index 0000000..5d8a9d5 --- /dev/null +++ b/src/WorldDebugSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 1d334d67cff28704c94924d2e6c8df84 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: