From 294a3af4ac03f7ca596518e7f37fd016adae9aa1 Mon Sep 17 00:00:00 2001 From: Mikhail <99481254+DCFApixels@users.noreply.github.com> Date: Thu, 25 Jan 2024 20:11:06 +0800 Subject: [PATCH] rework data interfaces --- src/DataInterfaces.cs | 82 +++++-------------------------------------- 1 file changed, 9 insertions(+), 73 deletions(-) diff --git a/src/DataInterfaces.cs b/src/DataInterfaces.cs index 6fb0316..4fed4d3 100644 --- a/src/DataInterfaces.cs +++ b/src/DataInterfaces.cs @@ -1,27 +1,5 @@ -using System; -using System.Linq; using System.Runtime.CompilerServices; -using static DCFApixels.DragonECS.Internal.DataInterfaceHalper; -namespace DCFApixels.DragonECS.Internal -{ - #region DataInterfaceHalper - public static class DataInterfaceHalper - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void CheckFakeInstanceValide(T fakeInstnace) - { -#if DEBUG - T nil = default; - if (fakeInstnace.Equals(nil) == false) - { - throw new Exception("Не правильное применение интерфейса, менять нужно передаваемое по ref значение"); - } -#endif - } - } - #endregion -} namespace DCFApixels.DragonECS { #region IEcsWorldComponent @@ -36,11 +14,10 @@ namespace DCFApixels.DragonECS public static readonly bool isHasHandler; static EcsWorldComponentHandler() { - Type targetType = typeof(T); - isHasHandler = targetType.GetInterfaces().Contains(typeof(IEcsWorldComponent<>).MakeGenericType(targetType)); - if (isHasHandler) + T def = default; + if (def is IEcsWorldComponent intrf) { - instance = (IEcsWorldComponent)Activator.CreateInstance(typeof(WorldComponentHandler<>).MakeGenericType(targetType)); + instance = intrf; } else { @@ -54,23 +31,6 @@ namespace DCFApixels.DragonECS public void OnDestroy(ref T component, EcsWorld world) { } } } - internal sealed class WorldComponentHandler : IEcsWorldComponent - where T : struct, IEcsWorldComponent - { - private T _fakeInstnace = default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Init(ref T component, EcsWorld world) - { - _fakeInstnace.Init(ref component, world); - CheckFakeInstanceValide(_fakeInstnace); - } - public void OnDestroy(ref T component, EcsWorld world) - { - _fakeInstnace.OnDestroy(ref component, world); - CheckFakeInstanceValide(_fakeInstnace); - } - - } #endregion #region IEcsComponentReset @@ -84,11 +44,10 @@ namespace DCFApixels.DragonECS public static readonly bool isHasHandler; static EcsComponentResetHandler() { - Type targetType = typeof(T); - isHasHandler = targetType.GetInterfaces().Contains(typeof(IEcsComponentReset<>).MakeGenericType(targetType)); - if (isHasHandler) + T def = default; + if (def is IEcsComponentReset intrf) { - instance = (IEcsComponentReset)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType)); + instance = intrf; } else { @@ -101,17 +60,6 @@ namespace DCFApixels.DragonECS public void Reset(ref T component) => component = default; } } - internal sealed class ComponentResetHandler : IEcsComponentReset - where T : IEcsComponentReset - { - private T _fakeInstnace = default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Reset(ref T component) - { - _fakeInstnace.Reset(ref component); - CheckFakeInstanceValide(_fakeInstnace); - } - } #endregion #region IEcsComponentCopy @@ -125,11 +73,10 @@ namespace DCFApixels.DragonECS public static readonly bool isHasHandler; static EcsComponentCopyHandler() { - Type targetType = typeof(T); - isHasHandler = targetType.GetInterfaces().Contains(typeof(IEcsComponentCopy<>).MakeGenericType(targetType)); - if (isHasHandler) + T def = default; + if(def is IEcsComponentCopy intrf) { - instance = (IEcsComponentCopy)Activator.CreateInstance(typeof(ComponentCopyHandler<>).MakeGenericType(targetType)); + instance = intrf; } else { @@ -142,16 +89,5 @@ namespace DCFApixels.DragonECS public void Copy(ref T from, ref T to) => to = from; } } - internal sealed class ComponentCopyHandler : IEcsComponentCopy - where T : IEcsComponentCopy - { - private T _fakeInstnace = default; - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Copy(ref T from, ref T to) - { - _fakeInstnace.Copy(ref from, ref to); - CheckFakeInstanceValide(_fakeInstnace); - } - } #endregion }