diff --git a/DragonECS-AutoInjections.asmdef b/DragonECS-AutoInjections.asmdef new file mode 100644 index 0000000..837fa4d --- /dev/null +++ b/DragonECS-AutoInjections.asmdef @@ -0,0 +1,16 @@ +{ + "name": "DCFApixels.DragonECS.AutoInjections", + "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-AutoInjections.asmdef.meta b/DragonECS-AutoInjections.asmdef.meta new file mode 100644 index 0000000..97b4df8 --- /dev/null +++ b/DragonECS-AutoInjections.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 2bb6f22a7d4079d4588ef2864e8cbaad +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/LICENSE.meta b/LICENSE.meta new file mode 100644 index 0000000..62373f7 --- /dev/null +++ b/LICENSE.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 1e67cc8ab052ec7478befd6bb1d6a2b4 +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/README.md.meta b/README.md.meta new file mode 100644 index 0000000..9e31039 --- /dev/null +++ b/README.md.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: c0ecdd9e1aa29254da7223f5992aaa0a +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src.meta b/src.meta new file mode 100644 index 0000000..f06778b --- /dev/null +++ b/src.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 2896ca087bdefa1448c9dc592e97275d +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Attributes.meta b/src/Attributes.meta new file mode 100644 index 0000000..58b8b63 --- /dev/null +++ b/src/Attributes.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b4716a87a1163d8489ed18a605aa1778 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/Attributes/AutoInjectAttribute.cs b/src/Attributes/AutoInjectAttribute.cs new file mode 100644 index 0000000..9bacdc4 --- /dev/null +++ b/src/Attributes/AutoInjectAttribute.cs @@ -0,0 +1,11 @@ +using System; + +namespace DCFApixels.DragonECS +{ + [AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)] + public sealed class AutoInjectAttribute : Attribute + { + + } +} + diff --git a/src/Attributes/AutoInjectAttribute.cs.meta b/src/Attributes/AutoInjectAttribute.cs.meta new file mode 100644 index 0000000..4666105 --- /dev/null +++ b/src/Attributes/AutoInjectAttribute.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 41cac83b50af5aa4eb5dda3ef383054b +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/AutoInjectSystem.cs b/src/AutoInjectSystem.cs new file mode 100644 index 0000000..6c54915 --- /dev/null +++ b/src/AutoInjectSystem.cs @@ -0,0 +1,103 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace DCFApixels.DragonECS +{ + internal class AutoInjectionMap + { + private readonly EcsSystems _source; + + private Dictionary> _systems; + + public AutoInjectionMap(EcsSystems source) + { + _source = source; + + var allsystems = _source.AllSystems; + + _systems = new Dictionary>(); + foreach (var system in allsystems) + { + Type systemType = system.GetType(); + foreach (var field in systemType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic)) + { + if(field.GetCustomAttribute() != null) + { + + Type fieldType = field.FieldType; + List list; + if (!_systems.TryGetValue(fieldType, out list)) + { + list = new List(); + _systems.Add(fieldType, list); + } + + list.Add(new FiledRecord(system, field)); + + + } + } + } + } + + public void Inject(object obj) + { + Type objectType = obj.GetType(); + if(_systems.TryGetValue(objectType, out List list)) + { + foreach (var item in list) + { + item.field.SetValue(item.target, obj); + } + } + } + + private readonly struct FiledRecord + { + public readonly IEcsSystem target; + public readonly FieldInfo field; + public FiledRecord(IEcsSystem target, FieldInfo field) + { + this.target = target; + this.field = field; + } + } + } + + [DebugHide, DebugColor(DebugColor.Gray)] + public class AutoInjectSystem : IEcsPreInitSystem, IEcsPreInject + { + private EcsSystems _systems; + private List _injectQueue = new List(); + + private AutoInjectionMap _autoInjectionMap; + + public void PreInject(object obj) + { + if(_systems == null) + { + _injectQueue.Add(obj); + return; + } + AutoInject(obj); + } + public void PreInit(EcsSystems systems) + { + _systems = systems; + _autoInjectionMap = new AutoInjectionMap(_systems); + + foreach (var obj in _injectQueue) + { + AutoInject(obj); + } + _injectQueue.Clear(); + _injectQueue = null; + } + + private void AutoInject(object obj) + { + _autoInjectionMap.Inject(obj); + } + } +} \ No newline at end of file diff --git a/src/AutoInjectSystem.cs.meta b/src/AutoInjectSystem.cs.meta new file mode 100644 index 0000000..c77acd6 --- /dev/null +++ b/src/AutoInjectSystem.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 30435f5cadc830d4e89b2809fdbaa848 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/src/AutoInjectSystemExtensions.cs b/src/AutoInjectSystemExtensions.cs new file mode 100644 index 0000000..5be09c3 --- /dev/null +++ b/src/AutoInjectSystemExtensions.cs @@ -0,0 +1,11 @@ +namespace DCFApixels.DragonECS +{ + public static class AutoInjectSystemExtensions + { + public static EcsSystems.Builder AutoInject(this EcsSystems.Builder self) + { + self.Add(new AutoInjectSystem()); + return self; + } + } +} diff --git a/src/AutoInjectSystemExtensions.cs.meta b/src/AutoInjectSystemExtensions.cs.meta new file mode 100644 index 0000000..d6c0cfb --- /dev/null +++ b/src/AutoInjectSystemExtensions.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b9f52b500c2da8a49ae3eaeac1839797 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: