#if DISABLE_DEBUG #undef DEBUG #endif using System.Runtime.CompilerServices; namespace DCFApixels.DragonECS.Core { #region IEcsWorldComponent public interface IEcsWorldComponent { void Init(ref T component, EcsWorld world); void OnDestroy(ref T component, EcsWorld world); } public static class EcsWorldComponent { public static readonly IEcsWorldComponent CustomHandler; public static readonly bool IsCustom; static EcsWorldComponent() { T raw = default; if (raw is IEcsWorldComponent handler) { IsCustom = true; CustomHandler = handler; } else { IsCustom = false; CustomHandler = new DummyHandler(); } } private sealed class DummyHandler : IEcsWorldComponent { [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Init(ref T component, EcsWorld world) { } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void OnDestroy(ref T component, EcsWorld world) { } } } #endregion #region IEcsComponentLifecycle public interface IEcsComponentLifecycle { void OnAdd(ref T component, short worldID, int entityID); void OnDel(ref T component, short worldID, int entityID); } public static class EcsComponentLifecycle where T : struct { public static readonly IEcsComponentLifecycle CustomHandler; public static readonly bool IsCustom; static EcsComponentLifecycle() { T raw = default; if (raw is IEcsComponentLifecycle handler) { IsCustom = true; CustomHandler = handler; } else { IsCustom = false; CustomHandler = new DummyHandler(); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void OnAdd(bool isCustom, IEcsComponentLifecycle custom, ref T component, short worldID, int entityID) { if (isCustom) { custom.OnAdd(ref component, worldID, entityID); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void OnDel(bool isCustom, IEcsComponentLifecycle custom, ref T component, short worldID, int entityID) { if (isCustom) { custom.OnDel(ref component, worldID, entityID); } else { component = default; } } private sealed class DummyHandler : IEcsComponentLifecycle { [MethodImpl(MethodImplOptions.AggressiveInlining)] public void OnAdd(ref T component, short worldID, int entityID) { component = default; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public void OnDel(ref T component, short worldID, int entityID) { component = default; } } } #endregion #region IEcsComponentCopy public interface IEcsComponentCopy { void Copy(ref T from, ref T to); } public static class EcsComponentCopy where T : struct { public static readonly IEcsComponentCopy CustomHandler; public static readonly bool IsCustom; static EcsComponentCopy() { T raw = default; if (raw is IEcsComponentCopy handler) { IsCustom = true; CustomHandler = handler; } else { IsCustom = false; CustomHandler = new DummyHandler(); } } [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Copy(bool isCustom, IEcsComponentCopy custom, ref T from, ref T to) { if (isCustom) { custom.Copy(ref from, ref to); } else { to = from; } } private sealed class DummyHandler : IEcsComponentCopy { [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Copy(ref T from, ref T to) { to = from; } } } #endregion }