This commit is contained in:
Mikhail 2023-03-29 16:43:06 +08:00
parent 73e253f9ee
commit 1757121e6d
12 changed files with 211 additions and 0 deletions

View File

@ -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
}

View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 2bb6f22a7d4079d4588ef2864e8cbaad
AssemblyDefinitionImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

7
LICENSE.meta Normal file
View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 1e67cc8ab052ec7478befd6bb1d6a2b4
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

7
README.md.meta Normal file
View File

@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: c0ecdd9e1aa29254da7223f5992aaa0a
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
src.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 2896ca087bdefa1448c9dc592e97275d
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

8
src/Attributes.meta Normal file
View File

@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: b4716a87a1163d8489ed18a605aa1778
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,11 @@
using System;
namespace DCFApixels.DragonECS
{
[AttributeUsage(AttributeTargets.Field, Inherited = false, AllowMultiple = false)]
public sealed class AutoInjectAttribute : Attribute
{
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 41cac83b50af5aa4eb5dda3ef383054b
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

103
src/AutoInjectSystem.cs Normal file
View File

@ -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<Type, List<FiledRecord>> _systems;
public AutoInjectionMap(EcsSystems source)
{
_source = source;
var allsystems = _source.AllSystems;
_systems = new Dictionary<Type, List<FiledRecord>>();
foreach (var system in allsystems)
{
Type systemType = system.GetType();
foreach (var field in systemType.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic))
{
if(field.GetCustomAttribute<AutoInjectAttribute>() != null)
{
Type fieldType = field.FieldType;
List<FiledRecord> list;
if (!_systems.TryGetValue(fieldType, out list))
{
list = new List<FiledRecord>();
_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<FiledRecord> 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<object> _injectQueue = new List<object>();
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);
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 30435f5cadc830d4e89b2809fdbaa848
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -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;
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b9f52b500c2da8a49ae3eaeac1839797
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: