using DCFApixels.DragonECS.Internal; using System; using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS { #region IEcsWorldComponent public interface IEcsWorldComponent { void Init(ref T component, EcsWorld world); } public static class EcsWorldComponentHandler { public static readonly IEcsWorldComponent instance; public static readonly bool isHasHandler; static EcsWorldComponentHandler() { Type targetType = typeof(T); isHasHandler = targetType.GetInterfaces().Contains(typeof(IEcsWorldComponent<>).MakeGenericType(targetType)); if (isHasHandler) { instance = (IEcsWorldComponent)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType)); } else { instance = new DummyHandler(); } } private class DummyHandler : IEcsWorldComponent { [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Init(ref T component, EcsWorld world) { } } } internal class WorldComponentHandler : IEcsWorldComponent where T : IEcsWorldComponent { private T _fakeInstnace; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Init(ref T component, EcsWorld world) => _fakeInstnace.Init(ref component, world); } #endregion #region IEcsComponentReset public interface IEcsComponentReset { void Reset(ref T component); } public static class EcsComponentResetHandler { public static readonly IEcsComponentReset instance; public static readonly bool isHasHandler; static EcsComponentResetHandler() { Type targetType = typeof(T); isHasHandler = targetType.GetInterfaces().Contains(typeof(IEcsComponentReset<>).MakeGenericType(targetType)); if (isHasHandler) { instance = (IEcsComponentReset)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType)); } else { instance = new DummyHandler(); } } private sealed class DummyHandler : IEcsComponentReset { [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Reset(ref T component) => component = default; } } internal sealed class ComponentResetHandler : IEcsComponentReset where T : IEcsComponentReset { private T _fakeInstnace; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Reset(ref T component) => _fakeInstnace.Reset(ref component); } #endregion #region IEcsComponentCopy public interface IEcsComponentCopy { void Copy(ref T from, ref T to); } public static class EcsComponentCopyHandler { public static readonly IEcsComponentCopy instance; public static readonly bool isHasHandler; static EcsComponentCopyHandler() { Type targetType = typeof(T); isHasHandler = targetType.GetInterfaces().Contains(typeof(IEcsComponentCopy<>).MakeGenericType(targetType)); if (isHasHandler) { instance = (IEcsComponentCopy)Activator.CreateInstance(typeof(ComponentCopyHandler<>).MakeGenericType(targetType)); } else { instance = new DummyHandler(); } } private sealed class DummyHandler : IEcsComponentCopy { [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Copy(ref T from, ref T to) => to = from; } } internal sealed class ComponentCopyHandler : IEcsComponentCopy where T : IEcsComponentCopy { private T _fakeInstnace; [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Copy(ref T from, ref T to) => _fakeInstnace.Copy(ref from, ref to); } #endregion }