DragonECS/src/EcsRunner.cs

121 lines
4.1 KiB
C#
Raw Normal View History

using DCFApixels.DragonECS.Internal;
using DCFApixels.DragonECS.RunnersCore;
using System;
using System.Linq;
2023-05-26 04:25:09 +08:00
using static DCFApixels.DragonECS.EcsDebugUtility;
namespace DCFApixels.DragonECS
{
public interface IEcsProcess { }
2023-03-16 01:49:14 +08:00
2023-04-23 17:55:01 +08:00
namespace RunnersCore
{
public abstract class EcsRunner
{
internal abstract void Init_Internal(EcsPipeline source);
#region CheckRunnerValide
public static void CheckRunnerTypeIsValide(Type runnerType, Type processInterfaceType)
{
#region DEBUG
#pragma warning disable IL2070 // 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The parameter of method does not have matching annotations.
Type targetInterface = processInterfaceType;
if (runnerType.IsAbstract || runnerType.IsInterface)
{
Throw.UndefinedException();
}
2024-01-08 13:39:38 +08:00
Type GetRunnerBaseType(Type inType)
{
2024-01-08 13:39:38 +08:00
if (inType.IsGenericType && inType.GetGenericTypeDefinition() == typeof(EcsRunner<>))
{
2024-01-08 13:39:38 +08:00
return inType;
}
2024-01-08 13:39:38 +08:00
if (inType.BaseType != null)
{
2024-01-08 13:39:38 +08:00
return GetRunnerBaseType(inType.BaseType);
}
return null;
}
Type baseType = GetRunnerBaseType(runnerType);
Type baseTypeArgument = baseType.GenericTypeArguments[0];
if (baseTypeArgument != targetInterface)
{
Throw.UndefinedException();
}
if (!runnerType.GetInterfaces().Any(o => o == targetInterface))
{
throw new EcsRunnerImplementationException($"Runner {GetGenericTypeFullName(runnerType, 1)} does not implement interface {GetGenericTypeFullName(baseTypeArgument, 1)}.");
}
#pragma warning restore IL2070 // 'this' argument does not satisfy 'DynamicallyAccessedMembersAttribute' in call to target method. The parameter of method does not have matching annotations.
#endregion
2023-04-23 17:55:01 +08:00
}
#endregion
}
public interface IEcsRunner : IEcsProcess
{
EcsPipeline Pipeline { get; }
Type Interface { get; }
EcsProcessRaw ProcessRaw { get; }
bool IsEmpty { get; }
}
2023-12-20 23:21:10 +08:00
public abstract class EcsRunner<TProcess> : EcsRunner, IEcsRunner, IEcsProcess
where TProcess : IEcsProcess
{
2023-04-23 17:55:01 +08:00
private EcsPipeline _source;
private EcsProcess<TProcess> _process;
private bool _isInit = false;
2023-03-26 11:19:03 +08:00
2023-04-23 17:55:01 +08:00
#region Properties
public EcsPipeline Pipeline
{
get { return _source; }
}
public Type Interface
{
get { return typeof(TProcess); }
}
public EcsProcessRaw ProcessRaw
{
get { return _process; }
}
public EcsProcess<TProcess> Process
{
get { return _process; }
}
public bool IsEmpty
{
2024-02-22 23:48:10 +08:00
get { return _process.IsNullOrEmpty; }
}
2023-04-23 17:55:01 +08:00
#endregion
2023-03-27 17:34:12 +08:00
#region Constructor Init OnSetup
public EcsRunner() { }
internal override sealed void Init_Internal(EcsPipeline source)
2023-04-23 17:55:01 +08:00
{
if (_isInit)
{
Throw.UndefinedException();
}
_isInit = true;
2023-04-23 17:55:01 +08:00
_source = source;
_process = source.GetProcess<TProcess>();
2023-04-23 17:55:01 +08:00
OnSetup();
}
protected virtual void OnSetup() { }
#endregion
2023-04-23 17:55:01 +08:00
}
}
2023-05-26 04:25:09 +08:00
2023-03-30 01:57:10 +08:00
#region Extensions
public static class IEcsProcessExtensions
2023-03-30 01:57:10 +08:00
{
public static bool IsRunner(this IEcsProcess self)
2023-03-30 01:57:10 +08:00
{
return self is IEcsRunner;
}
}
#endregion
}