DragonECS/src/DataInterfaces.cs

158 lines
5.3 KiB
C#
Raw Normal View History

2023-06-22 10:24:07 +08:00
using System;
using System.Linq;
2023-06-21 01:37:05 +08:00
using System.Runtime.CompilerServices;
2024-01-11 00:48:39 +08:00
using static DCFApixels.DragonECS.Internal.DataInterfaceHalper;
2023-06-21 01:37:05 +08:00
2024-01-11 00:48:39 +08:00
namespace DCFApixels.DragonECS.Internal
{
#region DataInterfaceHalper
public static class DataInterfaceHalper
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void CheckFakeInstanceValide<T>(T fakeInstnace)
{
#if DEBUG
2024-01-11 00:53:15 +08:00
T nil = default;
if (fakeInstnace.Equals(nil) == false)
2024-01-11 00:48:39 +08:00
{
throw new Exception("Не правильное применение интерфейса, менять нужно передаваемое по ref значение");
}
#endif
}
}
#endregion
}
2023-06-21 01:37:05 +08:00
namespace DCFApixels.DragonECS
{
#region IEcsWorldComponent
public interface IEcsWorldComponent<T>
{
void Init(ref T component, EcsWorld world);
2023-06-21 15:03:33 +08:00
void OnDestroy(ref T component, EcsWorld world);
2023-06-21 01:37:05 +08:00
}
public static class EcsWorldComponentHandler<T>
{
public static readonly IEcsWorldComponent<T> instance;
public static readonly bool isHasHandler;
static EcsWorldComponentHandler()
{
Type targetType = typeof(T);
isHasHandler = targetType.GetInterfaces().Contains(typeof(IEcsWorldComponent<>).MakeGenericType(targetType));
if (isHasHandler)
{
2023-06-27 01:21:52 +08:00
instance = (IEcsWorldComponent<T>)Activator.CreateInstance(typeof(WorldComponentHandler<>).MakeGenericType(targetType));
2023-06-21 01:37:05 +08:00
}
else
{
instance = new DummyHandler();
}
}
private class DummyHandler : IEcsWorldComponent<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Init(ref T component, EcsWorld world) { }
2023-06-21 15:03:33 +08:00
public void OnDestroy(ref T component, EcsWorld world) { }
2023-06-21 01:37:05 +08:00
}
}
2024-01-11 00:48:39 +08:00
internal sealed class WorldComponentHandler<T> : IEcsWorldComponent<T>
where T : struct, IEcsWorldComponent<T>
2023-06-21 01:37:05 +08:00
{
2024-01-07 23:19:18 +08:00
private T _fakeInstnace = default;
2023-06-21 01:37:05 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-01-11 00:48:39 +08:00
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);
}
2023-06-21 01:37:05 +08:00
}
#endregion
#region IEcsComponentReset
public interface IEcsComponentReset<T>
{
void Reset(ref T component);
}
public static class EcsComponentResetHandler<T>
{
public static readonly IEcsComponentReset<T> instance;
public static readonly bool isHasHandler;
static EcsComponentResetHandler()
{
Type targetType = typeof(T);
isHasHandler = targetType.GetInterfaces().Contains(typeof(IEcsComponentReset<>).MakeGenericType(targetType));
if (isHasHandler)
{
instance = (IEcsComponentReset<T>)Activator.CreateInstance(typeof(ComponentResetHandler<>).MakeGenericType(targetType));
}
else
{
instance = new DummyHandler();
}
}
private sealed class DummyHandler : IEcsComponentReset<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Reset(ref T component) => component = default;
}
}
internal sealed class ComponentResetHandler<T> : IEcsComponentReset<T>
where T : IEcsComponentReset<T>
{
2024-01-08 13:39:38 +08:00
private T _fakeInstnace = default;
2023-06-21 01:37:05 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-01-11 00:48:39 +08:00
public void Reset(ref T component)
{
_fakeInstnace.Reset(ref component);
CheckFakeInstanceValide(_fakeInstnace);
}
2023-06-21 01:37:05 +08:00
}
#endregion
#region IEcsComponentCopy
public interface IEcsComponentCopy<T>
{
void Copy(ref T from, ref T to);
}
public static class EcsComponentCopyHandler<T>
{
public static readonly IEcsComponentCopy<T> instance;
public static readonly bool isHasHandler;
static EcsComponentCopyHandler()
{
Type targetType = typeof(T);
isHasHandler = targetType.GetInterfaces().Contains(typeof(IEcsComponentCopy<>).MakeGenericType(targetType));
if (isHasHandler)
{
instance = (IEcsComponentCopy<T>)Activator.CreateInstance(typeof(ComponentCopyHandler<>).MakeGenericType(targetType));
}
else
{
instance = new DummyHandler();
}
}
private sealed class DummyHandler : IEcsComponentCopy<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Copy(ref T from, ref T to) => to = from;
}
}
internal sealed class ComponentCopyHandler<T> : IEcsComponentCopy<T>
where T : IEcsComponentCopy<T>
{
2024-01-07 23:19:18 +08:00
private T _fakeInstnace = default;
2023-06-21 01:37:05 +08:00
[MethodImpl(MethodImplOptions.AggressiveInlining)]
2024-01-11 00:48:39 +08:00
public void Copy(ref T from, ref T to)
{
_fakeInstnace.Copy(ref from, ref to);
CheckFakeInstanceValide(_fakeInstnace);
}
2023-06-21 01:37:05 +08:00
}
#endregion
}