From fbfb31e1f3a7e4cddf83fb635c7da1dff3c619a0 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Thu, 5 Jun 2025 08:52:45 +0200 Subject: [PATCH 01/33] + wip: source gen for systems --- TinyEcs.sln | 75 +++++ samples/MyBattleground/MyBattleground.csproj | 4 + samples/MyBattleground/Program.cs | 256 ++++++++++++------ src/Bevy.cs | 28 ++ tools/TinyEcs.SourceGenerator/Program.cs | 211 +++++++++++++++ .../TinyEcs.SourceGenerator.csproj | 15 + 6 files changed, 501 insertions(+), 88 deletions(-) create mode 100644 tools/TinyEcs.SourceGenerator/Program.cs create mode 100644 tools/TinyEcs.SourceGenerator/TinyEcs.SourceGenerator.csproj diff --git a/TinyEcs.sln b/TinyEcs.sln index c8699ebb..1a6672c9 100644 --- a/TinyEcs.sln +++ b/TinyEcs.sln @@ -21,32 +21,106 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tools", "tools", "{2F77B046 EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TinyEcs.Generator", "tools\TinyEcs.Generator\TinyEcs.Generator.csproj", "{EC4831EE-15EE-46F7-9A5D-A8C5180A8930}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "benchmarks", "benchmarks", "{66320409-64EC-F7C5-3DEF-65E7510DAAD1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyEcs.Benchmarks", "benchmarks\TinyEcs.Benchmarks\TinyEcs.Benchmarks.csproj", "{D2BFC612-EF6B-4947-ADB6-32375AB36EB5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TinyEcs.SourceGenerator", "tools\TinyEcs.SourceGenerator\TinyEcs.SourceGenerator.csproj", "{81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU + Release|x64 = Release|x64 + Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Debug|x64.ActiveCfg = Debug|Any CPU + {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Debug|x64.Build.0 = Debug|Any CPU + {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Debug|x86.ActiveCfg = Debug|Any CPU + {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Debug|x86.Build.0 = Debug|Any CPU {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Release|Any CPU.ActiveCfg = Release|Any CPU {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Release|Any CPU.Build.0 = Release|Any CPU + {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Release|x64.ActiveCfg = Release|Any CPU + {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Release|x64.Build.0 = Release|Any CPU + {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Release|x86.ActiveCfg = Release|Any CPU + {D39BBB77-EBFE-4127-97A5-C62FD6D4EB36}.Release|x86.Build.0 = Release|Any CPU {C1B15479-149D-4951-B16E-1980B5451205}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {C1B15479-149D-4951-B16E-1980B5451205}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C1B15479-149D-4951-B16E-1980B5451205}.Debug|x64.ActiveCfg = Debug|Any CPU + {C1B15479-149D-4951-B16E-1980B5451205}.Debug|x64.Build.0 = Debug|Any CPU + {C1B15479-149D-4951-B16E-1980B5451205}.Debug|x86.ActiveCfg = Debug|Any CPU + {C1B15479-149D-4951-B16E-1980B5451205}.Debug|x86.Build.0 = Debug|Any CPU {C1B15479-149D-4951-B16E-1980B5451205}.Release|Any CPU.ActiveCfg = Release|Any CPU {C1B15479-149D-4951-B16E-1980B5451205}.Release|Any CPU.Build.0 = Release|Any CPU + {C1B15479-149D-4951-B16E-1980B5451205}.Release|x64.ActiveCfg = Release|Any CPU + {C1B15479-149D-4951-B16E-1980B5451205}.Release|x64.Build.0 = Release|Any CPU + {C1B15479-149D-4951-B16E-1980B5451205}.Release|x86.ActiveCfg = Release|Any CPU + {C1B15479-149D-4951-B16E-1980B5451205}.Release|x86.Build.0 = Release|Any CPU {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Debug|Any CPU.Build.0 = Debug|Any CPU + {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Debug|x64.ActiveCfg = Debug|Any CPU + {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Debug|x64.Build.0 = Debug|Any CPU + {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Debug|x86.ActiveCfg = Debug|Any CPU + {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Debug|x86.Build.0 = Debug|Any CPU {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Release|Any CPU.ActiveCfg = Release|Any CPU {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Release|Any CPU.Build.0 = Release|Any CPU + {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Release|x64.ActiveCfg = Release|Any CPU + {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Release|x64.Build.0 = Release|Any CPU + {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Release|x86.ActiveCfg = Release|Any CPU + {95AFBF2E-6A84-44D2-8496-07CD879D9E40}.Release|x86.Build.0 = Release|Any CPU {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Debug|x64.ActiveCfg = Debug|Any CPU + {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Debug|x64.Build.0 = Debug|Any CPU + {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Debug|x86.ActiveCfg = Debug|Any CPU + {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Debug|x86.Build.0 = Debug|Any CPU {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Release|Any CPU.ActiveCfg = Release|Any CPU {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Release|Any CPU.Build.0 = Release|Any CPU + {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Release|x64.ActiveCfg = Release|Any CPU + {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Release|x64.Build.0 = Release|Any CPU + {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Release|x86.ActiveCfg = Release|Any CPU + {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6}.Release|x86.Build.0 = Release|Any CPU {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Debug|x64.ActiveCfg = Debug|Any CPU + {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Debug|x64.Build.0 = Debug|Any CPU + {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Debug|x86.ActiveCfg = Debug|Any CPU + {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Debug|x86.Build.0 = Debug|Any CPU {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Release|Any CPU.ActiveCfg = Release|Any CPU {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Release|Any CPU.Build.0 = Release|Any CPU + {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Release|x64.ActiveCfg = Release|Any CPU + {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Release|x64.Build.0 = Release|Any CPU + {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Release|x86.ActiveCfg = Release|Any CPU + {EC4831EE-15EE-46F7-9A5D-A8C5180A8930}.Release|x86.Build.0 = Release|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Debug|x64.ActiveCfg = Debug|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Debug|x64.Build.0 = Debug|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Debug|x86.ActiveCfg = Debug|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Debug|x86.Build.0 = Debug|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Release|Any CPU.Build.0 = Release|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Release|x64.ActiveCfg = Release|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Release|x64.Build.0 = Release|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Release|x86.ActiveCfg = Release|Any CPU + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5}.Release|x86.Build.0 = Release|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Debug|x64.ActiveCfg = Debug|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Debug|x64.Build.0 = Debug|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Debug|x86.ActiveCfg = Debug|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Debug|x86.Build.0 = Debug|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Release|Any CPU.Build.0 = Release|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Release|x64.ActiveCfg = Release|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Release|x64.Build.0 = Release|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Release|x86.ActiveCfg = Release|Any CPU + {81BE3178-E9D7-43E5-BBE9-71489BF8E3E7}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -57,6 +131,7 @@ Global {95AFBF2E-6A84-44D2-8496-07CD879D9E40} = {1F4AAB6E-01EE-4314-AFD3-C246193A14CA} {9FDC10BD-6BCD-4F99-B0FA-E47921DE75B6} = {1F4AAB6E-01EE-4314-AFD3-C246193A14CA} {EC4831EE-15EE-46F7-9A5D-A8C5180A8930} = {2F77B046-8050-473D-BEE9-6E1C251F7A49} + {D2BFC612-EF6B-4947-ADB6-32375AB36EB5} = {66320409-64EC-F7C5-3DEF-65E7510DAAD1} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {41A04670-264C-4A26-AE2E-874466E06CF9} diff --git a/samples/MyBattleground/MyBattleground.csproj b/samples/MyBattleground/MyBattleground.csproj index 109c3d15..4653ad91 100644 --- a/samples/MyBattleground/MyBattleground.csproj +++ b/samples/MyBattleground/MyBattleground.csproj @@ -8,6 +8,9 @@ preview enable true + + true + $(BaseIntermediateOutputPath)\Generated @@ -33,6 +36,7 @@ + diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index fe68ffbc..41f45410 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -10,7 +10,19 @@ using var ecs = new World(); var scheduler = new Scheduler(ecs); +var pl = new AAA(); +scheduler.AddPlugin(pl); +pl.SetupSystems(scheduler); +scheduler.RunOnce(); + +return; +// ecs.Entity(); +// ecs.Entity().Delete(); +// var xx = ecs.Entity().Add(); +// +// xx.Get(); +// Console.WriteLine(""); // scheduler.OnUpdate((Query, Changed> query) => // { @@ -21,8 +33,8 @@ // }, ThreadingMode.Single); -var ab = ecs.Entity() - .Set(new Position()).Set(new Velocity()); +// var ab = ecs.Entity() +// .Set(new Position()).Set(new Velocity()); // ecs.Entity() // .Set(new Position() { X = -1 }); // // var q = ecs.QueryBuilder() @@ -49,7 +61,7 @@ // scheduler.RunOnce(); // scheduler.RunOnce(); -ab.Set(new Position() { X = 2 }); +// ab.Set(new Position() { X = 2 }); // scheduler.RunOnce(); // scheduler.AddState(GameState.Loading); @@ -96,47 +108,75 @@ // while (true) // scheduler.RunOnce(); -for (int i = 0; i < ENTITIES_COUNT; i++) - ecs.Entity() - .Set(new Position()) - .Set(new Velocity()); +for (var i = 0; i < ENTITIES_COUNT; i++) + ecs.Entity() + .Set(new Position()) + .Set(new Velocity()); // ecs.Entity().Set(new Position()).Set(new Velocity()).Set(new Mass()); +// ecs.Entity() +// .Set(new Velocity()); +// ecs.Entity() +// .Set(new Position()); - -scheduler.AddSystem(( - Query, With> q, - Query, Added> added +// ecs.Entity().Set(new Velocity()).Set(new Position()); +scheduler.AddSystem([TinySystem] ( + Query> q, + Query, Added> added ) => { - foreach ((var pos, var vel) in q) - { - pos.Ref.X *= vel.Ref.X; - pos.Ref.Y *= vel.Ref.Y; - - // pos.Ref.X *= vel.Ref.X; - // pos.Ref.Y *= vel.Ref.Y; - // if (pos.IsChanged) - // pos.ClearState(); - } - - // foreach ((var pos, var vel) in added) - // { - // pos.Ref.X *= vel.Ref.X; - // pos.Ref.Y *= vel.Ref.Y; - - // // if (pos.IsAdded) - // // pos.ClearState(); - // } + // foreach ((var pos, var vel) in or) + // { + // if (pos.IsValid()) + // { + // Console.Write("pos is valid "); + // } + // else + // { + + // } + + // if (vel.IsValid()) + // { + // Console.Write("vel is valid"); + // } + // else + // { + + // } + + // Console.WriteLine(); + // } + + foreach (var (pos, vel) in q) + { + pos.Ref.X *= vel.Ref.X; + pos.Ref.Y *= vel.Ref.Y; + + // pos.Ref.X *= vel.Ref.X; + // pos.Ref.Y *= vel.Ref.Y; + + // if (pos.IsChanged) + // pos.ClearState(); + } + + // foreach ((var pos, var vel) in added) + // { + // pos.Ref.X *= vel.Ref.X; + // pos.Ref.Y *= vel.Ref.Y; + + // // if (pos.IsAdded) + // // pos.ClearState(); + // } }, threadingType: ThreadingMode.Single); var query = ecs.QueryBuilder() - .With() - .With() - .Build(); + .With() + .With() + .Build(); var sw = Stopwatch.StartNew(); var start = 0f; @@ -144,78 +184,78 @@ Query, Added> added while (true) { - for (int i = 0; i < 3600; ++i) - { - scheduler.RunOnce(); + for (int i = 0; i < 3600; ++i) + { + // scheduler.RunOnce(); - // Execute(query); - // ExecuteIterator(query); + Execute(query); + // ExecuteIterator(query); - // var it = query.Iter(); - // while (it.Next()) - // { - // var count = it.Count; + // var it = query.Iter(); + // while (it.Next()) + // { + // var count = it.Count; - // ref var pos = ref it.DataRef(0); - // ref var vel = ref it.DataRef(1); - // ref var lastPos = ref Unsafe.Add(ref pos, count); + // ref var pos = ref it.DataRef(0); + // ref var vel = ref it.DataRef(1); + // ref var lastPos = ref Unsafe.Add(ref pos, count); - // while (Unsafe.IsAddressLessThan(ref pos, ref lastPos)) - // { - // pos.X *= vel.X; - // pos.Y *= vel.Y; + // while (Unsafe.IsAddressLessThan(ref pos, ref lastPos)) + // { + // pos.X *= vel.X; + // pos.Y *= vel.Y; - // pos = ref Unsafe.Add(ref pos, 1); - // vel = ref Unsafe.Add(ref vel, 1); - // } - // } - } + // pos = ref Unsafe.Add(ref pos, 1); + // vel = ref Unsafe.Add(ref vel, 1); + // } + // } + } - last = start; - start = sw.ElapsedMilliseconds; + last = start; + start = sw.ElapsedMilliseconds; - Console.WriteLine("query done in {0} ms", start - last); + Console.WriteLine("query done in {0} ms", start - last); } static void Execute(Query query) { - foreach ((var pos, var vel) in Data.CreateIterator(query.Iter())) - { - pos.Ref.X *= vel.Ref.X; - pos.Ref.Y *= vel.Ref.Y; - } + foreach (var (pos, vel) in Data.CreateIterator(query.Iter())) + { + pos.Ref.X *= vel.Ref.X; + pos.Ref.Y *= vel.Ref.Y; + } } static void ExecuteIterator(Query query) { - var it = query.Iter(); - - while (it.Next()) - { - var span0 = it.Data(0); - var span1 = it.Data(1); - var count = it.Count; - - for (var i = 0; i < count; ++i) - { - ref var pos = ref span0[i]; - ref var vel = ref span1[i]; - - pos.X *= vel.X; - pos.Y *= vel.Y; - } - } + var it = query.Iter(); + + while (it.Next()) + { + var span0 = it.Data(0); + var span1 = it.Data(1); + var count = it.Count; + + for (var i = 0; i < count; ++i) + { + ref var pos = ref span0[i]; + ref var vel = ref span1[i]; + + pos.X *= vel.X; + pos.Y *= vel.Y; + } + } } -struct Position +public struct Position { - public float X, Y, Z; + public float X, Y, Z; } -struct Velocity +public struct Velocity { - public float X, Y; + public float X, Y; } struct Mass { public float Value; } @@ -225,13 +265,53 @@ struct Tag { } enum GameState { - Loading, - Playing, - Menu, - Menu2 + Loading, + Playing, + Menu, + Menu2 } enum AnotherState { - A, B, C + A, B, C +} + + +public partial class AAA : TinyPlugin +{ + [TinySystem(Stages.FrameEnd, ThreadingMode.Single)] + [RunIf(nameof(TestRun))] + [RunIf(nameof(TestRun2))] + void Execute(Query> query) + { + foreach (var (pos, vel) in query) + { + pos.Ref.X *= vel.Ref.X; + pos.Ref.Y *= vel.Ref.Y; + } + } + + [TinySystem] + static void DoThat(Query> query) + { + foreach (var (pos, vel) in query) + { + pos.Ref.X *= vel.Ref.X; + pos.Ref.Y *= vel.Ref.Y; + } + } + + private bool TestRun() + { + return true; + } + + private bool TestRun2() + { + return true; + } + + public override void Build(Scheduler scheduler) + { + } } diff --git a/src/Bevy.cs b/src/Bevy.cs index a0f9efe2..d695cf6f 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -1277,4 +1277,32 @@ public bool MoveNext() public readonly QueryIter GetEnumerator() => this; } + +[AttributeUsage(System.AttributeTargets.Method, Inherited=true)] +public class TinySystemAttribute(Stages stage = Stages.Update, ThreadingMode threadingMode = ThreadingMode.Auto) : Attribute +{ + public Stages Stage { get; } = stage; + public ThreadingMode ThreadingMode { get; } = threadingMode; +} + +[AttributeUsage(System.AttributeTargets.Method, Inherited=true, AllowMultiple = true)] +public class RunIf(string name) : Attribute +{ + +} + +[AttributeUsage(System.AttributeTargets.Method, Inherited=true, AllowMultiple = true)] +public class BeforeOf(string name) : Attribute +{ + +} + +public abstract class TinyPlugin : IPlugin +{ + public abstract void Build(Scheduler scheduler); + public virtual void SetupSystems(Scheduler scheduler) { } +} + + + #endif diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs new file mode 100644 index 00000000..68361b9c --- /dev/null +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -0,0 +1,211 @@ +using System.Collections.Immutable; +using System.Diagnostics; +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +#pragma warning disable IDE0055 +#pragma warning disable IDE0008 +#pragma warning disable IDE0058 + +namespace TinyEcs.SourceGenerator; + +[Generator] +public sealed class SourceGenerator : IIncrementalGenerator +{ + private static Dictionary> _classToMethods = new(); + + + class Comparer : IEqualityComparer + { + public static readonly Comparer Instance = new(); + + public bool Equals(MethodDeclarationSyntax x, MethodDeclarationSyntax y) + { + return x.Equals(y); + } + + public int GetHashCode(MethodDeclarationSyntax obj) + { + return obj.GetHashCode(); + } + } + + public void Initialize(IncrementalGeneratorInitializationContext context) + { + IncrementalValuesProvider methodDeclarations = context.SyntaxProvider.CreateSyntaxProvider( + static (s, _) => s is MethodDeclarationSyntax { AttributeLists.Count: > 0 }, + static (ctx, _) => GetMethodSymbolIfAttributeof(ctx, "TinyEcs.TinySystemAttribute") + ).Where(static m => m is not null)!; + + IncrementalValueProvider<(Compilation, ImmutableArray)> compilationAndMethods = context.CompilationProvider.Combine(methodDeclarations.WithComparer(Comparer.Instance).Collect()); + context.RegisterSourceOutput(compilationAndMethods, static (spc, source) => Generate(source.Item1, source.Item2, spc)); + } + + static MethodDeclarationSyntax? GetMethodSymbolIfAttributeof(GeneratorSyntaxContext context, string name) + { + var enumDeclarationSyntax = (MethodDeclarationSyntax)context.Node; + + foreach (var attributeListSyntax in enumDeclarationSyntax.AttributeLists) + { + foreach (var attributeSyntax in attributeListSyntax.Attributes) + { + if (ModelExtensions.GetSymbolInfo(context.SemanticModel, attributeSyntax).Symbol is not IMethodSymbol attributeSymbol) continue; + + var attributeContainingTypeSymbol = attributeSymbol.ContainingType; + var fullName = attributeContainingTypeSymbol.ToDisplayString(); + + // Is the attribute the [EnumExtensions] attribute? + if (fullName != name) continue; + return enumDeclarationSyntax; + } + } + + return null; + } + + private static void AddMethodToClass(IMethodSymbol methodSymbol) + { + if (!_classToMethods.TryGetValue(methodSymbol.ContainingSymbol, out var list)) + { + list = new List(); + _classToMethods[methodSymbol.ContainingSymbol] = list; + } + list.Add(methodSymbol); + } + + static void Generate(Compilation compilation, ImmutableArray methods, SourceProductionContext context) + { + if (methods.IsDefaultOrEmpty) return; + + foreach (var methodSyntax in methods) + { + IMethodSymbol? methodSymbol = null; + try + { + var semanticModel = compilation.GetSemanticModel(methodSyntax.SyntaxTree); + methodSymbol = ModelExtensions.GetDeclaredSymbol(semanticModel, methodSyntax) as IMethodSymbol; + } + catch + { + continue; + } + + AddMethodToClass(methodSymbol); + } + + foreach (var classToMethod in _classToMethods) + { + var sb = new StringBuilder(); + foreach (var method in classToMethod.Value) + sb.AppendLine(GenerateOne(method)); + + var template = $@" + partial class {classToMethod.Key} {{ + public override void SetupSystems(TinyEcs.Scheduler scheduler) + {{ + {sb} + }} + + }} "; + + if (string.IsNullOrEmpty(template)) continue; + + var fileName = (classToMethod.Key as INamedTypeSymbol).ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat).Replace('<', '{').Replace('>', '}'); + context.AddSource($"{fileName}.g.cs", + CSharpSyntaxTree.ParseText(template).GetRoot().NormalizeWhitespace().ToFullString()); + } + } + + private static string GenerateOne(IMethodSymbol methodSymbol) + { + var systemDescData = GetAttributeData(methodSymbol, "TinySystem"); + var runifData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("RunIf")).ToArray(); + + var arguments = methodSymbol.Parameters.ToList(); + + if (systemDescData.ConstructorArguments.Length == 2) + { + Debug.WriteLine($"Method {methodSymbol.Name} does not have the correct number of arguments for TinySystem attribute. Expected 2, got {systemDescData.ConstructorArguments.Length}."); + return string.Empty; + } + + var stage = (systemDescData.ConstructorArguments[0].Type.ToDisplayString(), systemDescData.ConstructorArguments[0].Value); + var threading = (systemDescData.ConstructorArguments[1].Type.ToDisplayString(), systemDescData.ConstructorArguments[1].Value); + + var @namespace = methodSymbol.ContainingNamespace.ToString(); + var className = methodSymbol.ContainingSymbol.ToString(); + className = className.Replace("TinyEcs.", ""); + + var method = $@" + var {methodSymbol.Name}fn = {methodSymbol.Name}; + scheduler.AddSystem( + {methodSymbol.Name}fn, + stage: ({stage.Item1}){stage.Value}, + threadingType: ({threading.Item1}){threading.Value} + ); "; + + return method; + } + + private static AttributeData GetAttributeData(IMethodSymbol ms, string name) + { + foreach (var attribute in ms.GetAttributes()) + { + var classSymbol = attribute.AttributeClass; + if(!classSymbol.Name.Contains(name)) continue; + + return attribute; + } + + return default; + } +} + + +internal sealed class CodeFormatter : CSharpSyntaxRewriter +{ + public static string Format(string source) + { + SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source); + SyntaxNode normalized = syntaxTree.GetRoot().NormalizeWhitespace(); + + normalized = new CodeFormatter().Visit(normalized); + + return normalized.ToFullString(); + } + + private static T FormatMembers(T node, IEnumerable members) where T : SyntaxNode + { + SyntaxNode[] membersArray = members as SyntaxNode[] ?? members.ToArray(); + + int memberCount = membersArray.Length; + int current = 0; + + return node.ReplaceNodes(membersArray, RewriteTrivia); + + SyntaxNode RewriteTrivia(TNode oldMember, TNode _) where TNode : SyntaxNode + { + string trailingTrivia = oldMember.GetTrailingTrivia().ToFullString().TrimEnd() + "\n\n"; + return current++ != memberCount - 1 + ? oldMember.WithTrailingTrivia(SyntaxFactory.Whitespace(trailingTrivia)) + : oldMember; + } + } + + public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node) + { + return base.VisitNamespaceDeclaration(FormatMembers(node, node.Members))!; + } + + public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node) + { + return base.VisitClassDeclaration(FormatMembers(node, node.Members))!; + } + + public override SyntaxNode VisitStructDeclaration(StructDeclarationSyntax node) + { + return base.VisitStructDeclaration(FormatMembers(node, node.Members))!; + } +} diff --git a/tools/TinyEcs.SourceGenerator/TinyEcs.SourceGenerator.csproj b/tools/TinyEcs.SourceGenerator/TinyEcs.SourceGenerator.csproj new file mode 100644 index 00000000..9f6b1310 --- /dev/null +++ b/tools/TinyEcs.SourceGenerator/TinyEcs.SourceGenerator.csproj @@ -0,0 +1,15 @@ + + + + enable + enable + preview + netstandard2.0 + + + + + + + + From 0ce1ca1a3915f5bb56065989bf2b0d995fe01a53 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Thu, 5 Jun 2025 21:52:21 +0200 Subject: [PATCH 02/33] + minor --- tools/TinyEcs.SourceGenerator/Program.cs | 103 +++++++----------- .../Properties/launchSettings.json | 9 ++ 2 files changed, 48 insertions(+), 64 deletions(-) create mode 100644 tools/TinyEcs.SourceGenerator/Properties/launchSettings.json diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 68361b9c..22d542ce 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -14,10 +14,10 @@ namespace TinyEcs.SourceGenerator; [Generator] public sealed class SourceGenerator : IIncrementalGenerator { - private static Dictionary> _classToMethods = new(); + private readonly Dictionary> _classToMethods = new (); - class Comparer : IEqualityComparer + private class Comparer : IEqualityComparer { public static readonly Comparer Instance = new(); @@ -40,10 +40,10 @@ public void Initialize(IncrementalGeneratorInitializationContext context) ).Where(static m => m is not null)!; IncrementalValueProvider<(Compilation, ImmutableArray)> compilationAndMethods = context.CompilationProvider.Combine(methodDeclarations.WithComparer(Comparer.Instance).Collect()); - context.RegisterSourceOutput(compilationAndMethods, static (spc, source) => Generate(source.Item1, source.Item2, spc)); + context.RegisterSourceOutput(compilationAndMethods, (spc, source) => Generate(source.Item1, source.Item2, spc)); } - static MethodDeclarationSyntax? GetMethodSymbolIfAttributeof(GeneratorSyntaxContext context, string name) + private static MethodDeclarationSyntax? GetMethodSymbolIfAttributeof(GeneratorSyntaxContext context, string name) { var enumDeclarationSyntax = (MethodDeclarationSyntax)context.Node; @@ -56,7 +56,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context) var attributeContainingTypeSymbol = attributeSymbol.ContainingType; var fullName = attributeContainingTypeSymbol.ToDisplayString(); - // Is the attribute the [EnumExtensions] attribute? if (fullName != name) continue; return enumDeclarationSyntax; } @@ -65,7 +64,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) return null; } - private static void AddMethodToClass(IMethodSymbol methodSymbol) + private void AddMethodToClass(IMethodSymbol methodSymbol) { if (!_classToMethods.TryGetValue(methodSymbol.ContainingSymbol, out var list)) { @@ -75,7 +74,7 @@ private static void AddMethodToClass(IMethodSymbol methodSymbol) list.Add(methodSymbol); } - static void Generate(Compilation compilation, ImmutableArray methods, SourceProductionContext context) + private void Generate(Compilation compilation, ImmutableArray methods, SourceProductionContext context) { if (methods.IsDefaultOrEmpty) return; @@ -92,7 +91,29 @@ static void Generate(Compilation compilation, ImmutableArray().Any(member => member.IsOverride)) + { + isValid = false; + break; + } + + mem = symb.BaseType; + + if (mem?.Name == "TinyPlugin") + { + isValid = true; + break; + } + } + + if (isValid) + AddMethodToClass(methodSymbol); } foreach (var classToMethod in _classToMethods) @@ -107,7 +128,6 @@ public override void SetupSystems(TinyEcs.Scheduler scheduler) {{ {sb} }} - }} "; if (string.IsNullOrEmpty(template)) continue; @@ -118,14 +138,14 @@ public override void SetupSystems(TinyEcs.Scheduler scheduler) } } - private static string GenerateOne(IMethodSymbol methodSymbol) + private string GenerateOne(IMethodSymbol methodSymbol) { var systemDescData = GetAttributeData(methodSymbol, "TinySystem"); var runifData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("RunIf")).ToArray(); var arguments = methodSymbol.Parameters.ToList(); - if (systemDescData.ConstructorArguments.Length == 2) + if (systemDescData.ConstructorArguments.Length < 2) { Debug.WriteLine($"Method {methodSymbol.Name} does not have the correct number of arguments for TinySystem attribute. Expected 2, got {systemDescData.ConstructorArguments.Length}."); return string.Empty; @@ -138,13 +158,15 @@ private static string GenerateOne(IMethodSymbol methodSymbol) var className = methodSymbol.ContainingSymbol.ToString(); className = className.Replace("TinyEcs.", ""); + + var method = $@" - var {methodSymbol.Name}fn = {methodSymbol.Name}; - scheduler.AddSystem( - {methodSymbol.Name}fn, - stage: ({stage.Item1}){stage.Value}, - threadingType: ({threading.Item1}){threading.Value} - ); "; + var {methodSymbol.Name}fn = {methodSymbol.Name}; + scheduler.AddSystem( + {methodSymbol.Name}fn, + stage: ({stage.Item1}){stage.Value}, + threadingType: ({threading.Item1}){threading.Value} + ); "; return method; } @@ -162,50 +184,3 @@ private static AttributeData GetAttributeData(IMethodSymbol ms, string name) return default; } } - - -internal sealed class CodeFormatter : CSharpSyntaxRewriter -{ - public static string Format(string source) - { - SyntaxTree syntaxTree = CSharpSyntaxTree.ParseText(source); - SyntaxNode normalized = syntaxTree.GetRoot().NormalizeWhitespace(); - - normalized = new CodeFormatter().Visit(normalized); - - return normalized.ToFullString(); - } - - private static T FormatMembers(T node, IEnumerable members) where T : SyntaxNode - { - SyntaxNode[] membersArray = members as SyntaxNode[] ?? members.ToArray(); - - int memberCount = membersArray.Length; - int current = 0; - - return node.ReplaceNodes(membersArray, RewriteTrivia); - - SyntaxNode RewriteTrivia(TNode oldMember, TNode _) where TNode : SyntaxNode - { - string trailingTrivia = oldMember.GetTrailingTrivia().ToFullString().TrimEnd() + "\n\n"; - return current++ != memberCount - 1 - ? oldMember.WithTrailingTrivia(SyntaxFactory.Whitespace(trailingTrivia)) - : oldMember; - } - } - - public override SyntaxNode VisitNamespaceDeclaration(NamespaceDeclarationSyntax node) - { - return base.VisitNamespaceDeclaration(FormatMembers(node, node.Members))!; - } - - public override SyntaxNode VisitClassDeclaration(ClassDeclarationSyntax node) - { - return base.VisitClassDeclaration(FormatMembers(node, node.Members))!; - } - - public override SyntaxNode VisitStructDeclaration(StructDeclarationSyntax node) - { - return base.VisitStructDeclaration(FormatMembers(node, node.Members))!; - } -} diff --git a/tools/TinyEcs.SourceGenerator/Properties/launchSettings.json b/tools/TinyEcs.SourceGenerator/Properties/launchSettings.json new file mode 100644 index 00000000..ece1eb57 --- /dev/null +++ b/tools/TinyEcs.SourceGenerator/Properties/launchSettings.json @@ -0,0 +1,9 @@ +{ + "$schema": "http://json.schemastore.org/launchsettings.json", + "profiles": { + "Generators": { + "commandName": "DebugRoslynComponent", + "targetProject": "../../samples/MyBattleground/MyBattleground.csproj" + } + } +} From 88ce2ba10bc14b6c890105b20fe0fba22f5a8ca4 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Thu, 5 Jun 2025 22:46:04 +0200 Subject: [PATCH 03/33] support for RunIf --- samples/MyBattleground/Program.cs | 64 ++++++++++++++++-------- tools/TinyEcs.SourceGenerator/Program.cs | 38 ++++++++++---- 2 files changed, 72 insertions(+), 30 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index 41f45410..5bd67cc6 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -10,7 +10,7 @@ using var ecs = new World(); var scheduler = new Scheduler(ecs); -var pl = new AAA(); +var pl = new ANamespace.AAA(); scheduler.AddPlugin(pl); pl.SetupSystems(scheduler); @@ -277,11 +277,9 @@ enum AnotherState } -public partial class AAA : TinyPlugin +partial class BBB : TinyPlugin { - [TinySystem(Stages.FrameEnd, ThreadingMode.Single)] - [RunIf(nameof(TestRun))] - [RunIf(nameof(TestRun2))] + [TinySystem(Stages.Update, ThreadingMode.Single)] void Execute(Query> query) { foreach (var (pos, vel) in query) @@ -291,27 +289,51 @@ void Execute(Query> query) } } - [TinySystem] - static void DoThat(Query> query) + public override void Build(Scheduler scheduler) { - foreach (var (pos, vel) in query) - { - pos.Ref.X *= vel.Ref.X; - pos.Ref.Y *= vel.Ref.Y; - } + throw new NotImplementedException(); } +} - private bool TestRun() +namespace ANamespace +{ + public partial class AAA : TinyPlugin { - return true; - } + [TinySystem(Stages.FrameEnd, ThreadingMode.Single)] + [RunIf(nameof(TestRun))] + [RunIf(nameof(TestRun2))] + void Execute(Query> query) + { + foreach (var (pos, vel) in query) + { + pos.Ref.X *= vel.Ref.X; + pos.Ref.Y *= vel.Ref.Y; + } + } - private bool TestRun2() - { - return true; - } + [TinySystem] + static void DoThat(Query> query) + { + foreach (var (pos, vel) in query) + { + pos.Ref.X *= vel.Ref.X; + pos.Ref.Y *= vel.Ref.Y; + } + } - public override void Build(Scheduler scheduler) - { + private bool TestRun(SchedulerState state, World world, Local index) + { + return true; + } + + private bool TestRun2() + { + return true; + } + + public override void Build(Scheduler scheduler) + { + } } + } diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 22d542ce..f28a74d7 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -122,13 +122,18 @@ private void Generate(Compilation compilation, ImmutableArray().ToDictionary(k => k.Name, v => v); + var sbRunIfFn = new StringBuilder(); + var sb = new StringBuilder(); + foreach (var runif in runifData) + { + var runifMethodName = runif.ConstructorArguments[0].Value.ToString(); + if (string.IsNullOrEmpty(runifMethodName)) continue; + if (!allMethods.TryGetValue(runifMethodName, out var runIfMethod)) + { + Debug.WriteLine($"RunIf method {runifMethodName} not found in class {classSymbol.Name}."); + continue; + } + + sbRunIfFn.AppendLine($"var {runIfMethod.Name}fn = {runIfMethod.Name};"); + sb.AppendLine($".RunIf({runIfMethod.Name}fn)"); + } var method = $@" + {sbRunIfFn} var {methodSymbol.Name}fn = {methodSymbol.Name}; scheduler.AddSystem( {methodSymbol.Name}fn, stage: ({stage.Item1}){stage.Value}, threadingType: ({threading.Item1}){threading.Value} - ); "; + ){sb}; "; return method; } From 6137ec8ff1be52929fdd6c44d0f5765e892c80f4 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Fri, 6 Jun 2025 01:11:47 +0200 Subject: [PATCH 04/33] AfterOf/BeforeOf --- samples/MyBattleground/Program.cs | 21 ++++- src/Bevy.cs | 22 ++++-- tools/TinyEcs.SourceGenerator/Program.cs | 97 +++++++++++++++++++----- 3 files changed, 111 insertions(+), 29 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index 5bd67cc6..79e359cc 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -299,7 +299,7 @@ namespace ANamespace { public partial class AAA : TinyPlugin { - [TinySystem(Stages.FrameEnd, ThreadingMode.Single)] + [TinySystem(Stages.Update, ThreadingMode.Single)] [RunIf(nameof(TestRun))] [RunIf(nameof(TestRun2))] void Execute(Query> query) @@ -321,6 +321,25 @@ static void DoThat(Query> query) } } + + [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Second))] + void First() + { + Console.WriteLine("1"); + } + + [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Third))] + void Second() + { + Console.WriteLine("2"); + } + + [TinySystem(threadingMode: ThreadingMode.Single)] + void Third() + { + Console.WriteLine("3"); + } + private bool TestRun(SchedulerState state, World world, Local index) { return true; diff --git a/src/Bevy.cs b/src/Bevy.cs index d695cf6f..05fedbd1 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -293,7 +293,7 @@ public FuncSystem OnExit(TState st, Action system, ThreadingMode return sys; } - public Scheduler AddPlugin() where T : notnull, IPlugin, new() + public Scheduler AddPlugin() where T : IPlugin, new() => AddPlugin(new T()); public Scheduler AddPlugin(T plugin) where T : IPlugin @@ -324,14 +324,14 @@ public Scheduler AddResource(T resource) where T : notnull return AddSystemParam(new Res() { Value = resource }); } - public Scheduler AddSystemParam(T param) where T : notnull, ISystemParam + public Scheduler AddSystemParam(T param) where T : ISystemParam { _world.Entity>().Set(new Placeholder() { Value = param }); return this; } - internal bool ResourceExists() where T : notnull, ISystemParam + internal bool ResourceExists() where T : ISystemParam { return _world.Entity>().Has>(); } @@ -1278,21 +1278,27 @@ public bool MoveNext() } -[AttributeUsage(System.AttributeTargets.Method, Inherited=true)] +[AttributeUsage(System.AttributeTargets.Method)] public class TinySystemAttribute(Stages stage = Stages.Update, ThreadingMode threadingMode = ThreadingMode.Auto) : Attribute { public Stages Stage { get; } = stage; public ThreadingMode ThreadingMode { get; } = threadingMode; } -[AttributeUsage(System.AttributeTargets.Method, Inherited=true, AllowMultiple = true)] -public class RunIf(string name) : Attribute +[AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)] +public sealed class RunIf(string name) : Attribute { } -[AttributeUsage(System.AttributeTargets.Method, Inherited=true, AllowMultiple = true)] -public class BeforeOf(string name) : Attribute +[AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)] +public sealed class BeforeOf(string name) : Attribute +{ + +} + +[AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)] +public sealed class AfterOf(string name) : Attribute { } diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index f28a74d7..24b526b4 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -119,8 +119,13 @@ private void Generate(Compilation compilation, ImmutableArray s.AttributeClass.Name.Contains("RunIf")).ToArray(); + var beforeOfData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("BeforeOf")).ToArray(); + var afterOfData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("AfterOf")).ToArray(); var arguments = methodSymbol.Parameters.ToList(); if (systemDescData.ConstructorArguments.Length < 2) { Debug.WriteLine($"Method {methodSymbol.Name} does not have the correct number of arguments for TinySystem attribute. Expected 2, got {systemDescData.ConstructorArguments.Length}."); - return string.Empty; + return (string.Empty, string.Empty); } var stage = (systemDescData.ConstructorArguments[0].Type.ToDisplayString(), systemDescData.ConstructorArguments[0].Value); var threading = (systemDescData.ConstructorArguments[1].Type.ToDisplayString(), systemDescData.ConstructorArguments[1].Value); var classSymbol = methodSymbol.ContainingType; - var allMethods = classSymbol.GetMembers().OfType().ToDictionary(k => k.Name, v => v); + var allMethods = classSymbol.GetMembers().OfType().ToList(); - var sbRunIfFn = new StringBuilder(); - var sb = new StringBuilder(); - foreach (var runif in runifData) + + var runIfMethods = GetAssociatedMethods(runifData, allMethods, "RunIf", classSymbol); + var beforeMethodsTargets = GetAssociatedMethods(beforeOfData, allMethods, "BeforeOf", classSymbol); + var afterMethodsTargets = GetAssociatedMethods(afterOfData, allMethods, "AfterOf", classSymbol); + + var sbRunIfFns = new StringBuilder(); + var sbRunIfActions = new StringBuilder(); + foreach (var runIf in runIfMethods) { - var runifMethodName = runif.ConstructorArguments[0].Value.ToString(); - if (string.IsNullOrEmpty(runifMethodName)) continue; + sbRunIfFns.AppendLine($"var {runIf.Name}fn = {runIf.Name};"); + sbRunIfActions.AppendLine($".RunIf({runIf.Name}fn)"); + } - if (!allMethods.TryGetValue(runifMethodName, out var runIfMethod)) - { - Debug.WriteLine($"RunIf method {runifMethodName} not found in class {classSymbol.Name}."); - continue; - } + var sb = new StringBuilder(); + + foreach (var after in afterMethodsTargets) + { + sb.AppendLine($"{methodSymbol.Name}System.RunAfter({after.Name}System);"); + } - sbRunIfFn.AppendLine($"var {runIfMethod.Name}fn = {runIfMethod.Name};"); - sb.AppendLine($".RunIf({runIfMethod.Name}fn)"); + foreach (var before in beforeMethodsTargets) + { + sb.AppendLine($"{methodSymbol.Name}System.RunBefore({before.Name}System);"); } + + // var sbRunIfFn = new StringBuilder(); + // var sb = new StringBuilder(); + // foreach (var runif in runifData) + // { + // var runifMethodName = runif.ConstructorArguments[0].Value.ToString(); + // if (string.IsNullOrEmpty(runifMethodName)) continue; + // + // if (!allMethods.TryGetValue(runifMethodName, out var runIfMethod)) + // { + // Debug.WriteLine($"RunIf method {runifMethodName} not found in class {classSymbol.Name}."); + // continue; + // } + // + // sbRunIfFn.AppendLine($"var {runIfMethod.Name}fn = {runIfMethod.Name};"); + // sb.AppendLine($".RunIf({runIfMethod.Name}fn)"); + // } + var method = $@" - {sbRunIfFn} + {sbRunIfFns} var {methodSymbol.Name}fn = {methodSymbol.Name}; - scheduler.AddSystem( + var {methodSymbol.Name}System = scheduler.AddSystem( {methodSymbol.Name}fn, stage: ({stage.Item1}){stage.Value}, threadingType: ({threading.Item1}){threading.Value} - ){sb}; "; + ) + {sbRunIfActions};"; + + // var afterBefore = $@" + // {beforeFns} + // {afterFns} + // "; - return method; + return (method, sb.ToString()); + } + + private static List GetAssociatedMethods(AttributeData[] attributeData, List allMethods, string attributeType, INamedTypeSymbol classSymbol) + { + var associatedMethods = new List(); + foreach (var attr in attributeData) + { + var methodName = attr.ConstructorArguments[0].Value?.ToString(); + if (string.IsNullOrEmpty(methodName)) continue; + + var method = allMethods.FirstOrDefault(m => m.Name == methodName); + if (method != null) + associatedMethods.Add(method); + else + Debug.WriteLine($"{attributeType} method {methodName} not found in class {classSymbol.Name}."); + } + return associatedMethods; } private static AttributeData GetAttributeData(IMethodSymbol ms, string name) From 037394cb5ee2caa48b9808127845ac54f973569c Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sat, 7 Jun 2025 00:17:19 +0200 Subject: [PATCH 05/33] + minor --- samples/MyBattleground/Program.cs | 21 +++++--- src/Bevy.cs | 28 ++++++----- tools/TinyEcs.SourceGenerator/Program.cs | 63 ++++++++---------------- 3 files changed, 48 insertions(+), 64 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index 79e359cc..d57ad51a 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -3,17 +3,13 @@ using TinyEcs; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; -using System.Numerics; const int ENTITIES_COUNT = (524_288 * 2 * 1); using var ecs = new World(); var scheduler = new Scheduler(ecs); -var pl = new ANamespace.AAA(); -scheduler.AddPlugin(pl); -pl.SetupSystems(scheduler); - +scheduler.AddPlugin(); scheduler.RunOnce(); return; @@ -227,6 +223,7 @@ static void Execute(Query query) } } +[TinySystem] static void ExecuteIterator(Query query) { var it = query.Iter(); @@ -312,7 +309,7 @@ void Execute(Query> query) } [TinySystem] - static void DoThat(Query> query) + static void DoThat(Query> query, EventWriter writer) { foreach (var (pos, vel) in query) { @@ -329,12 +326,12 @@ void First() } [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Third))] - void Second() + void Second(EventReader reader) { Console.WriteLine("2"); } - [TinySystem(threadingMode: ThreadingMode.Single)] + [TinySystem(threadingMode: ThreadingMode.Single), BeforeOf("HELLO")] void Third() { Console.WriteLine("3"); @@ -352,6 +349,14 @@ private bool TestRun2() public override void Build(Scheduler scheduler) { + scheduler.AddEvent(); + + base.Build(scheduler); + } + + struct CustomEvent + { + public int Value; } } diff --git a/src/Bevy.cs b/src/Bevy.cs index 05fedbd1..317f8e29 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -1278,37 +1278,39 @@ public bool MoveNext() } -[AttributeUsage(System.AttributeTargets.Method)] +[AttributeUsage(AttributeTargets.Method)] public class TinySystemAttribute(Stages stage = Stages.Update, ThreadingMode threadingMode = ThreadingMode.Auto) : Attribute { public Stages Stage { get; } = stage; public ThreadingMode ThreadingMode { get; } = threadingMode; } -[AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)] -public sealed class RunIf(string name) : Attribute +[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] +public sealed class RunIf(string systemName) : Attribute { - + public string SystemName { get; } = systemName; } -[AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)] -public sealed class BeforeOf(string name) : Attribute +[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] +public sealed class BeforeOf(string systemName) : Attribute { - + public string SystemName { get; } = systemName; } [AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)] -public sealed class AfterOf(string name) : Attribute +public sealed class AfterOf(string systemName) : Attribute { - + public string SystemName { get; } = systemName; } public abstract class TinyPlugin : IPlugin { - public abstract void Build(Scheduler scheduler); - public virtual void SetupSystems(Scheduler scheduler) { } -} - + public virtual void Build(Scheduler scheduler) + { + SetupSystems(scheduler); + } + protected virtual void SetupSystems(Scheduler scheduler) { } +} #endif diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 24b526b4..b933f804 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -16,30 +16,14 @@ public sealed class SourceGenerator : IIncrementalGenerator { private readonly Dictionary> _classToMethods = new (); - - private class Comparer : IEqualityComparer - { - public static readonly Comparer Instance = new(); - - public bool Equals(MethodDeclarationSyntax x, MethodDeclarationSyntax y) - { - return x.Equals(y); - } - - public int GetHashCode(MethodDeclarationSyntax obj) - { - return obj.GetHashCode(); - } - } - public void Initialize(IncrementalGeneratorInitializationContext context) { - IncrementalValuesProvider methodDeclarations = context.SyntaxProvider.CreateSyntaxProvider( + var methodDeclarations = context.SyntaxProvider.CreateSyntaxProvider( static (s, _) => s is MethodDeclarationSyntax { AttributeLists.Count: > 0 }, static (ctx, _) => GetMethodSymbolIfAttributeof(ctx, "TinyEcs.TinySystemAttribute") ).Where(static m => m is not null)!; - IncrementalValueProvider<(Compilation, ImmutableArray)> compilationAndMethods = context.CompilationProvider.Combine(methodDeclarations.WithComparer(Comparer.Instance).Collect()); + var compilationAndMethods = context.CompilationProvider.Combine(methodDeclarations.WithComparer(Comparer.Instance).Collect()); context.RegisterSourceOutput(compilationAndMethods, (spc, source) => Generate(source.Item1, source.Item2, spc)); } @@ -133,7 +117,7 @@ private void Generate(Compilation compilation, ImmutableArray GetAssociatedMethods(AttributeData[] attributeData, List allMethods, string attributeType, INamedTypeSymbol classSymbol) { var associatedMethods = new List(); - foreach (var attr in attributeData) + foreach (var attr in attributeData.Where(s => !s.ConstructorArguments.IsEmpty)) { var methodName = attr.ConstructorArguments[0].Value?.ToString(); if (string.IsNullOrEmpty(methodName)) continue; @@ -260,4 +221,20 @@ private static AttributeData GetAttributeData(IMethodSymbol ms, string name) return default; } + + + private class Comparer : IEqualityComparer + { + public static readonly Comparer Instance = new(); + + public bool Equals(MethodDeclarationSyntax x, MethodDeclarationSyntax y) + { + return x.Equals(y); + } + + public int GetHashCode(MethodDeclarationSyntax obj) + { + return obj.GetHashCode(); + } + } } From ab90926abf6cc0c10a86461e5173fae51c375bc7 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sat, 7 Jun 2025 01:26:58 +0200 Subject: [PATCH 06/33] + TinyPlugin --- samples/MyBattleground/Program.cs | 25 +++-- src/Bevy.cs | 10 +- tools/TinyEcs.SourceGenerator/Program.cs | 130 +++++++++++++++++++---- 3 files changed, 129 insertions(+), 36 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index d57ad51a..d20e0de5 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -9,6 +9,7 @@ using var ecs = new World(); var scheduler = new Scheduler(ecs); +scheduler.AddPlugin(); scheduler.AddPlugin(); scheduler.RunOnce(); @@ -274,7 +275,9 @@ enum AnotherState } -partial class BBB : TinyPlugin + +[TinyPlugin] +partial class BBB { [TinySystem(Stages.Update, ThreadingMode.Single)] void Execute(Query> query) @@ -286,15 +289,16 @@ void Execute(Query> query) } } - public override void Build(Scheduler scheduler) + public void Build(Scheduler scheduler) { - throw new NotImplementedException(); + } } namespace ANamespace { - public partial class AAA : TinyPlugin + [TinyPlugin] + public partial class AAA { [TinySystem(Stages.Update, ThreadingMode.Single)] [RunIf(nameof(TestRun))] @@ -347,11 +351,9 @@ private bool TestRun2() return true; } - public override void Build(Scheduler scheduler) + public void Build(Scheduler scheduler) { scheduler.AddEvent(); - - base.Build(scheduler); } struct CustomEvent @@ -361,3 +363,12 @@ struct CustomEvent } } + +[TinyPlugin] +public partial class TestPlugin +{ + public void Build(Scheduler scheduler) + { + + } +} diff --git a/src/Bevy.cs b/src/Bevy.cs index 317f8e29..b90d109c 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -1303,14 +1303,10 @@ public sealed class AfterOf(string systemName) : Attribute public string SystemName { get; } = systemName; } -public abstract class TinyPlugin : IPlugin -{ - public virtual void Build(Scheduler scheduler) - { - SetupSystems(scheduler); - } - protected virtual void SetupSystems(Scheduler scheduler) { } +[AttributeUsage(System.AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)] +public sealed class TinyPluginAttribute : Attribute +{ } #endif diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index b933f804..f2e486bb 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -18,13 +18,101 @@ public sealed class SourceGenerator : IIncrementalGenerator public void Initialize(IncrementalGeneratorInitializationContext context) { - var methodDeclarations = context.SyntaxProvider.CreateSyntaxProvider( - static (s, _) => s is MethodDeclarationSyntax { AttributeLists.Count: > 0 }, - static (ctx, _) => GetMethodSymbolIfAttributeof(ctx, "TinyEcs.TinySystemAttribute") + var pluginDeclarations = context.SyntaxProvider.CreateSyntaxProvider( + static (s, _) => s is ClassDeclarationSyntax { AttributeLists.Count: > 0 }, + static (ctx, _) => GetClassDeclarationSymbolIfAttributeof(ctx, "TinyEcs.TinyPluginAttribute") ).Where(static m => m is not null)!; - var compilationAndMethods = context.CompilationProvider.Combine(methodDeclarations.WithComparer(Comparer.Instance).Collect()); - context.RegisterSourceOutput(compilationAndMethods, (spc, source) => Generate(source.Item1, source.Item2, spc)); + var compilationAndClasses = context.CompilationProvider.Combine(pluginDeclarations.WithComparer(Comparer.Instance).Collect()); + context.RegisterSourceOutput(compilationAndClasses, (spc, source) => Generate(source.Item1, source.Item2, spc)); + + + // var methodDeclarations = context.SyntaxProvider.CreateSyntaxProvider( + // static (s, _) => s is MethodDeclarationSyntax { AttributeLists.Count: > 0 }, + // static (ctx, _) => GetMethodSymbolIfAttributeof(ctx, "TinyEcs.TinySystemAttribute") + // ).Where(static m => m is not null)!; + // + // var compilationAndMethods = context.CompilationProvider.Combine(methodDeclarations.WithComparer(Comparer.Instance).Collect()); + // context.RegisterSourceOutput(compilationAndMethods, (spc, source) => Generate(source.Item1, source.Item2, spc)); + } + + private static ClassDeclarationSyntax? GetClassDeclarationSymbolIfAttributeof(GeneratorSyntaxContext context, string name) + { + var enumDeclarationSyntax = (ClassDeclarationSyntax)context.Node; + + foreach (var attributeListSyntax in enumDeclarationSyntax.AttributeLists) + { + foreach (var attributeSyntax in attributeListSyntax.Attributes) + { + if (ModelExtensions.GetSymbolInfo(context.SemanticModel, attributeSyntax).Symbol is not IMethodSymbol attributeSymbol) continue; + + var attributeContainingTypeSymbol = attributeSymbol.ContainingType; + var fullName = attributeContainingTypeSymbol.ToDisplayString(); + + if (fullName != name) continue; + return enumDeclarationSyntax; + } + } + + return null; + } + + private void Generate(Compilation compilation, ImmutableArray classes, SourceProductionContext context) + { + if (classes.IsDefaultOrEmpty) return; + + foreach (var cl in classes) + { + INamedTypeSymbol? nameSymbol = null; + try + { + var semanticModel = compilation.GetSemanticModel(cl.SyntaxTree); + nameSymbol = ModelExtensions.GetDeclaredSymbol(semanticModel, cl) as INamedTypeSymbol; + } + catch + { + continue; + } + + if (nameSymbol == null) + continue; + + var allMethods = nameSymbol.GetMembers().OfType().ToList(); + + var allSystems = allMethods.Where(s => s.GetAttributes() + .Any(s => s.AttributeClass.ToDisplayString() == "TinyEcs.TinySystemAttribute")) + .ToList(); + + var sb = new StringBuilder(); + var sbEnd = new StringBuilder(); + foreach (var method in allSystems) + { + (var systems, var systemsOrder) = GenerateOne(method); + sb.AppendLine(systems); + sbEnd.AppendLine(systemsOrder); + } + + var @namespace = nameSymbol.ContainingNamespace.ToString(); + var className = nameSymbol.Name.Substring(nameSymbol.Name.LastIndexOf('.') + 1); + + var template = $@" + using TinyEcs; + {(nameSymbol.ContainingNamespace.IsGlobalNamespace ? "" : $"namespace {@namespace} {{")} + {(nameSymbol.IsStatic ? "static" : "")} partial class {className} : IPlugin {{ + void IPlugin.Build(Scheduler scheduler) + {{ + this.Build(scheduler); + + {sb} + {sbEnd} + }} + }} + {(nameSymbol.ContainingNamespace.IsGlobalNamespace ? "" : "}")}"; + + var fileName = nameSymbol.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat).Replace('<', '{').Replace('>', '}'); + context.AddSource($"{fileName}.g.cs", + CSharpSyntaxTree.ParseText(template).GetRoot().NormalizeWhitespace().ToFullString()); + } } private static MethodDeclarationSyntax? GetMethodSymbolIfAttributeof(GeneratorSyntaxContext context, string name) @@ -75,24 +163,21 @@ private void Generate(Compilation compilation, ImmutableArray().Any(member => member.IsOverride)) + var members = symb.GetMembers("Build"); + if (members.OfType().Any(s => !s.IsStatic)) { - isValid = false; - break; + isValid = true; } - mem = symb.BaseType; - - if (mem?.Name == "TinyPlugin") + if (symb.AllInterfaces.Any(s => s.Name == "IPlugin")) { isValid = true; - break; } } @@ -115,9 +200,10 @@ private void Generate(Compilation compilation, ImmutableArray + private class Comparer : IEqualityComparer { - public static readonly Comparer Instance = new(); + public static readonly Comparer Instance = new(); - public bool Equals(MethodDeclarationSyntax x, MethodDeclarationSyntax y) + public bool Equals(T x, T y) { return x.Equals(y); } - public int GetHashCode(MethodDeclarationSyntax obj) + public int GetHashCode(T obj) { return obj.GetHashCode(); } From c73dcb785d86743007034a4563a04a1ba5e12a0f Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sat, 7 Jun 2025 01:32:57 +0200 Subject: [PATCH 07/33] + struct support + cleanup --- samples/MyBattleground/Program.cs | 2 +- tools/TinyEcs.SourceGenerator/Program.cs | 133 ++--------------------- 2 files changed, 13 insertions(+), 122 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index d20e0de5..74f89dde 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -277,7 +277,7 @@ enum AnotherState [TinyPlugin] -partial class BBB +partial struct BBB { [TinySystem(Stages.Update, ThreadingMode.Single)] void Execute(Query> query) diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index f2e486bb..33306052 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -14,31 +14,20 @@ namespace TinyEcs.SourceGenerator; [Generator] public sealed class SourceGenerator : IIncrementalGenerator { - private readonly Dictionary> _classToMethods = new (); - public void Initialize(IncrementalGeneratorInitializationContext context) { var pluginDeclarations = context.SyntaxProvider.CreateSyntaxProvider( - static (s, _) => s is ClassDeclarationSyntax { AttributeLists.Count: > 0 }, + static (s, _) => s is ClassDeclarationSyntax { AttributeLists.Count: > 0 } or StructDeclarationSyntax { AttributeLists.Count: > 0 }, static (ctx, _) => GetClassDeclarationSymbolIfAttributeof(ctx, "TinyEcs.TinyPluginAttribute") ).Where(static m => m is not null)!; - var compilationAndClasses = context.CompilationProvider.Combine(pluginDeclarations.WithComparer(Comparer.Instance).Collect()); + var compilationAndClasses = context.CompilationProvider.Combine(pluginDeclarations.WithComparer(Comparer.Instance).Collect()); context.RegisterSourceOutput(compilationAndClasses, (spc, source) => Generate(source.Item1, source.Item2, spc)); - - - // var methodDeclarations = context.SyntaxProvider.CreateSyntaxProvider( - // static (s, _) => s is MethodDeclarationSyntax { AttributeLists.Count: > 0 }, - // static (ctx, _) => GetMethodSymbolIfAttributeof(ctx, "TinyEcs.TinySystemAttribute") - // ).Where(static m => m is not null)!; - // - // var compilationAndMethods = context.CompilationProvider.Combine(methodDeclarations.WithComparer(Comparer.Instance).Collect()); - // context.RegisterSourceOutput(compilationAndMethods, (spc, source) => Generate(source.Item1, source.Item2, spc)); } - private static ClassDeclarationSyntax? GetClassDeclarationSymbolIfAttributeof(GeneratorSyntaxContext context, string name) + private static TypeDeclarationSyntax? GetClassDeclarationSymbolIfAttributeof(GeneratorSyntaxContext context, string name) { - var enumDeclarationSyntax = (ClassDeclarationSyntax)context.Node; + var enumDeclarationSyntax = (TypeDeclarationSyntax)context.Node; foreach (var attributeListSyntax in enumDeclarationSyntax.AttributeLists) { @@ -57,7 +46,7 @@ public void Initialize(IncrementalGeneratorInitializationContext context) return null; } - private void Generate(Compilation compilation, ImmutableArray classes, SourceProductionContext context) + private void Generate(Compilation compilation, ImmutableArray classes, SourceProductionContext context) { if (classes.IsDefaultOrEmpty) return; @@ -94,11 +83,17 @@ private void Generate(Compilation compilation, ImmutableArray "class", + TypeKind.Struct => "struct", + _ => throw new InvalidOperationException($"Unsupported type kind: {nameSymbol.TypeKind}") + }; var template = $@" using TinyEcs; {(nameSymbol.ContainingNamespace.IsGlobalNamespace ? "" : $"namespace {@namespace} {{")} - {(nameSymbol.IsStatic ? "static" : "")} partial class {className} : IPlugin {{ + {(nameSymbol.IsStatic ? "static" : "")} partial {typeName} {className} : IPlugin {{ void IPlugin.Build(Scheduler scheduler) {{ this.Build(scheduler); @@ -115,110 +110,6 @@ void IPlugin.Build(Scheduler scheduler) } } - private static MethodDeclarationSyntax? GetMethodSymbolIfAttributeof(GeneratorSyntaxContext context, string name) - { - var enumDeclarationSyntax = (MethodDeclarationSyntax)context.Node; - - foreach (var attributeListSyntax in enumDeclarationSyntax.AttributeLists) - { - foreach (var attributeSyntax in attributeListSyntax.Attributes) - { - if (ModelExtensions.GetSymbolInfo(context.SemanticModel, attributeSyntax).Symbol is not IMethodSymbol attributeSymbol) continue; - - var attributeContainingTypeSymbol = attributeSymbol.ContainingType; - var fullName = attributeContainingTypeSymbol.ToDisplayString(); - - if (fullName != name) continue; - return enumDeclarationSyntax; - } - } - - return null; - } - - private void AddMethodToClass(IMethodSymbol methodSymbol) - { - if (!_classToMethods.TryGetValue(methodSymbol.ContainingSymbol, out var list)) - { - list = new List(); - _classToMethods[methodSymbol.ContainingSymbol] = list; - } - list.Add(methodSymbol); - } - - private void Generate(Compilation compilation, ImmutableArray methods, SourceProductionContext context) - { - if (methods.IsDefaultOrEmpty) return; - - foreach (var methodSyntax in methods) - { - IMethodSymbol? methodSymbol = null; - try - { - var semanticModel = compilation.GetSemanticModel(methodSyntax.SyntaxTree); - methodSymbol = ModelExtensions.GetDeclaredSymbol(semanticModel, methodSyntax) as IMethodSymbol; - } - catch - { - continue; - } - - if (methodSymbol == null) - continue; - - var isValid = false; - if (methodSymbol.ContainingSymbol is INamedTypeSymbol symb) - { - var members = symb.GetMembers("Build"); - if (members.OfType().Any(s => !s.IsStatic)) - { - isValid = true; - } - - if (symb.AllInterfaces.Any(s => s.Name == "IPlugin")) - { - isValid = true; - } - } - - if (isValid) - AddMethodToClass(methodSymbol); - } - - foreach (var classToMethod in _classToMethods) - { - var sb = new StringBuilder(); - var sbEnd = new StringBuilder(); - foreach (var method in classToMethod.Value) - { - (var systems, var systemsOrder) = GenerateOne(method); - sb.AppendLine(systems); - sbEnd.AppendLine(systemsOrder); - } - - var @namespace = classToMethod.Key.ContainingNamespace.ToString(); - var className = classToMethod.Key.Name.Substring(classToMethod.Key.Name.LastIndexOf('.') + 1); - - var template = $@" - using TinyEcs; - {(classToMethod.Key.ContainingNamespace.IsGlobalNamespace ? "" : $"namespace {@namespace} {{")} - {(classToMethod.Key.IsStatic ? "static" : "")} partial class {className} : ISourceGenPlugin {{ - void ISourceGenPlugin.InternalBuild(Scheduler scheduler) - {{ - {sb} - {sbEnd} - }} - }} - {(classToMethod.Key.ContainingNamespace.IsGlobalNamespace ? "" : "}")}"; - - if (string.IsNullOrEmpty(template)) continue; - - var fileName = (classToMethod.Key as INamedTypeSymbol).ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat).Replace('<', '{').Replace('>', '}'); - context.AddSource($"{fileName}.g.cs", - CSharpSyntaxTree.ParseText(template).GetRoot().NormalizeWhitespace().ToFullString()); - } - } - private (string, string) GenerateOne(IMethodSymbol methodSymbol) { var systemDescData = GetAttributeData(methodSymbol, "TinySystem"); From 03d70b8e943d912cbe130bd851f9b723c4f9281c Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sat, 7 Jun 2025 12:06:02 +0200 Subject: [PATCH 08/33] + cache existing system declaractions --- samples/MyBattleground/Program.cs | 4 ++++ tools/TinyEcs.SourceGenerator/Program.cs | 27 +++++++++++++----------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index 74f89dde..0673e6e5 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -313,6 +313,10 @@ void Execute(Query> query) } [TinySystem] + [RunIf(nameof(TestRun2))] + [RunIf(nameof(TestRun2))] + [RunIf(nameof(TestRun2))] + [RunIf(nameof(TestRun2))] static void DoThat(Query> query, EventWriter writer) { foreach (var (pos, vel) in query) diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 33306052..170fc70a 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -17,7 +17,7 @@ public sealed class SourceGenerator : IIncrementalGenerator public void Initialize(IncrementalGeneratorInitializationContext context) { var pluginDeclarations = context.SyntaxProvider.CreateSyntaxProvider( - static (s, _) => s is ClassDeclarationSyntax { AttributeLists.Count: > 0 } or StructDeclarationSyntax { AttributeLists.Count: > 0 }, + static (s, _) => s is TypeDeclarationSyntax { AttributeLists.Count: > 0 }, static (ctx, _) => GetClassDeclarationSymbolIfAttributeof(ctx, "TinyEcs.TinyPluginAttribute") ).Where(static m => m is not null)!; @@ -72,13 +72,14 @@ private void Generate(Compilation compilation, ImmutableArray s.AttributeClass.ToDisplayString() == "TinyEcs.TinySystemAttribute")) .ToList(); - var sb = new StringBuilder(); - var sbEnd = new StringBuilder(); + var sbDeclarations = new StringBuilder(); + var sbSystemsOrder = new StringBuilder(); + var cache = new HashSet(); foreach (var method in allSystems) { - (var systems, var systemsOrder) = GenerateOne(method); - sb.AppendLine(systems); - sbEnd.AppendLine(systemsOrder); + (var systems, var systemsOrder) = GenerateOne(method, allMethods, cache); + sbDeclarations.AppendLine(systems); + sbSystemsOrder.AppendLine(systemsOrder); } var @namespace = nameSymbol.ContainingNamespace.ToString(); @@ -98,8 +99,8 @@ void IPlugin.Build(Scheduler scheduler) {{ this.Build(scheduler); - {sb} - {sbEnd} + {sbDeclarations} + {sbSystemsOrder} }} }} {(nameSymbol.ContainingNamespace.IsGlobalNamespace ? "" : "}")}"; @@ -110,7 +111,7 @@ void IPlugin.Build(Scheduler scheduler) } } - private (string, string) GenerateOne(IMethodSymbol methodSymbol) + private static (string, string) GenerateOne(IMethodSymbol methodSymbol, List allMethods, HashSet cache) { var systemDescData = GetAttributeData(methodSymbol, "TinySystem"); var runifData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("RunIf")).ToArray(); @@ -129,9 +130,6 @@ void IPlugin.Build(Scheduler scheduler) var threading = (systemDescData.ConstructorArguments[1].Type.ToDisplayString(), systemDescData.ConstructorArguments[1].Value); var classSymbol = methodSymbol.ContainingType; - var allMethods = classSymbol.GetMembers().OfType().ToList(); - - var runIfMethods = GetAssociatedMethods(runifData, allMethods, "RunIf", classSymbol); var beforeMethodsTargets = GetAssociatedMethods(beforeOfData, allMethods, "BeforeOf", classSymbol); var afterMethodsTargets = GetAssociatedMethods(afterOfData, allMethods, "AfterOf", classSymbol); @@ -140,6 +138,11 @@ void IPlugin.Build(Scheduler scheduler) var sbRunIfActions = new StringBuilder(); foreach (var runIf in runIfMethods) { + if (!cache.Add(runIf.Name)) + { + Debug.WriteLine($"Duplicate RunIf method found: {runIf.Name} in class {classSymbol.Name}. Skipping."); + continue; + } sbRunIfFns.AppendLine($"var {runIf.Name}fn = {runIf.Name};"); sbRunIfActions.AppendLine($".RunIf({runIf.Name}fn)"); } From 15027342cb4add1e6cb84136f296e3c9a0661cf7 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sat, 7 Jun 2025 17:22:00 +0200 Subject: [PATCH 09/33] minor --- tools/TinyEcs.SourceGenerator/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 170fc70a..91beece8 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -69,7 +69,7 @@ private void Generate(Compilation compilation, ImmutableArray().ToList(); var allSystems = allMethods.Where(s => s.GetAttributes() - .Any(s => s.AttributeClass.ToDisplayString() == "TinyEcs.TinySystemAttribute")) + .Any(k => k.AttributeClass.ToDisplayString() == "TinyEcs.TinySystemAttribute")) .ToList(); var sbDeclarations = new StringBuilder(); From b43bdcec1013b7e20b0aef6306042137a91c8186 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sat, 7 Jun 2025 17:53:46 +0200 Subject: [PATCH 10/33] + minor --- tools/TinyEcs.SourceGenerator/Program.cs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 91beece8..5b299c8b 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -138,13 +138,12 @@ private static (string, string) GenerateOne(IMethodSymbol methodSymbol, List Date: Wed, 11 Jun 2025 19:12:33 +0200 Subject: [PATCH 11/33] + trying another approach --- samples/MyBattleground/Program.cs | 257 +++++++++++++++------- tools/TinyEcs.SourceGenerator/Program.cs | 260 ++++++++++++++++++++++- 2 files changed, 436 insertions(+), 81 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index 0673e6e5..00951ab6 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -9,8 +9,11 @@ using var ecs = new World(); var scheduler = new Scheduler(ecs); -scheduler.AddPlugin(); -scheduler.AddPlugin(); +// scheduler.AddPlugin(); +// scheduler.AddPlugin(); + +scheduler.AddPlugin(); +scheduler.AddPlugin(); scheduler.RunOnce(); return; @@ -276,103 +279,205 @@ enum AnotherState -[TinyPlugin] -partial struct BBB +// [TinyPlugin] +// partial struct BBB +// { +// [TinySystem(Stages.Update, ThreadingMode.Single)] +// void Execute(Query> query) +// { +// foreach (var (pos, vel) in query) +// { +// pos.Ref.X *= vel.Ref.X; +// pos.Ref.Y *= vel.Ref.Y; +// } +// } +// +// public void Build(Scheduler scheduler) +// { +// +// } +// } + +class TestSys : IPlugin { - [TinySystem(Stages.Update, ThreadingMode.Single)] - void Execute(Query> query) + + public void Build(Scheduler scheduler) { - foreach (var (pos, vel) in query) + scheduler.OnUpdate2(TestMethod); + var xx = TestMethod2; + scheduler.OnUpdate2(xx); + scheduler.OnUpdate2(TestMethod2); + scheduler.OnUpdate2(TestMethod2); + scheduler.OnUpdate2(TestMethod2); + scheduler.OnUpdate2(Palle) + .RunIf((World w) => + { + return true; + }); + + scheduler.OnUpdate2((Local w) => { - pos.Ref.X *= vel.Ref.X; - pos.Ref.Y *= vel.Ref.Y; - } + + }); + + + scheduler.OnStartup2((World w) => { }); } - public void Build(Scheduler scheduler) + void TestMethod(World world, Query> query) { } -} -namespace ANamespace -{ - [TinyPlugin] - public partial class AAA + void TestMethod2(World world, Query> query) { - [TinySystem(Stages.Update, ThreadingMode.Single)] - [RunIf(nameof(TestRun))] - [RunIf(nameof(TestRun2))] - void Execute(Query> query) - { - foreach (var (pos, vel) in query) - { - pos.Ref.X *= vel.Ref.X; - pos.Ref.Y *= vel.Ref.Y; - } - } - - [TinySystem] - [RunIf(nameof(TestRun2))] - [RunIf(nameof(TestRun2))] - [RunIf(nameof(TestRun2))] - [RunIf(nameof(TestRun2))] - static void DoThat(Query> query, EventWriter writer) - { - foreach (var (pos, vel) in query) - { - pos.Ref.X *= vel.Ref.X; - pos.Ref.Y *= vel.Ref.Y; - } - } + } - [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Second))] - void First() - { - Console.WriteLine("1"); - } + void Palle() + { - [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Third))] - void Second(EventReader reader) - { - Console.WriteLine("2"); - } + } +} - [TinySystem(threadingMode: ThreadingMode.Single), BeforeOf("HELLO")] - void Third() - { - Console.WriteLine("3"); - } +class TestSys2 : IPlugin +{ - private bool TestRun(SchedulerState state, World world, Local index) - { - return true; - } + public void Build(Scheduler scheduler) + { + scheduler.OnUpdate2(TestMethod); + var xx = TestMethod2; + scheduler.OnUpdate2(xx); + scheduler.OnUpdate2(TestMethod2, ThreadingMode.Single); + scheduler.OnUpdate2(TestMethod2); + scheduler.OnUpdate2(TestMethod2); + scheduler.OnUpdate2(Palle) + .RunIf((World w) => + { + return true; + }); - private bool TestRun2() + scheduler.OnUpdate2((Local w) => { - return true; - } - public void Build(Scheduler scheduler) - { - scheduler.AddEvent(); - } + }); + } + + void TestMethod(World world, Query> query) + { - struct CustomEvent - { - public int Value; - } } -} + void TestMethod2(World world, Query> query) + { -[TinyPlugin] -public partial class TestPlugin -{ - public void Build(Scheduler scheduler) + } + + void Palle() { } } + + +// namespace TinyEcs +// { +// public delegate void SystemFn(World param1, SchedulerState param2); +// public partial class Scheduler { +// public void OnUpdate2(SystemFn fn) +// { +// +// } +// } +// } + + +// +// namespace ANamespace +// { +// [TinyPlugin] +// public partial class AAA +// { +// [TinySystem(Stages.Update, ThreadingMode.Single)] +// [RunIf(nameof(TestRun))] +// [RunIf(nameof(TestRun2))] +// void Execute(Query> query) +// { +// foreach (var (pos, vel) in query) +// { +// pos.Ref.X *= vel.Ref.X; +// pos.Ref.Y *= vel.Ref.Y; +// } +// } +// +// +// [TinySystem(Stages.OnEnter, ThreadingMode.Single)] +// static void CheckState(World world) +// { +// +// } +// +// [TinySystem] +// [RunIf(nameof(TestRun2))] +// [RunIf(nameof(TestRun2))] +// [RunIf(nameof(TestRun2))] +// [RunIf(nameof(TestRun2))] +// static void DoThat(Query> query, EventWriter writer) +// { +// foreach (var (pos, vel) in query) +// { +// pos.Ref.X *= vel.Ref.X; +// pos.Ref.Y *= vel.Ref.Y; +// } +// } +// +// +// [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Second))] +// void First() +// { +// Console.WriteLine("1"); +// } +// +// [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Third))] +// void Second(EventReader reader) +// { +// Console.WriteLine("2"); +// } +// +// [TinySystem(threadingMode: ThreadingMode.Single), BeforeOf("HELLO")] +// void Third() +// { +// Console.WriteLine("3"); +// } +// +// private bool TestRun(SchedulerState state, World world, Local index) +// { +// return true; +// } +// +// private bool TestRun2() +// { +// return true; +// } +// +// public void Build(Scheduler scheduler) +// { +// scheduler.AddEvent(); +// } +// +// struct CustomEvent +// { +// public int Value; +// } +// } +// +// } +// +// [TinyPlugin] +// public partial class TestPlugin +// { +// public void Build(Scheduler scheduler) +// { +// +// } +// } diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 5b299c8b..c1b2ebff 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -18,14 +18,261 @@ public void Initialize(IncrementalGeneratorInitializationContext context) { var pluginDeclarations = context.SyntaxProvider.CreateSyntaxProvider( static (s, _) => s is TypeDeclarationSyntax { AttributeLists.Count: > 0 }, - static (ctx, _) => GetClassDeclarationSymbolIfAttributeof(ctx, "TinyEcs.TinyPluginAttribute") + static (ctx, _) => GetClassDeclarationSymbolIfAttributeOf(ctx, "TinyEcs.TinyPluginAttribute") ).Where(static m => m is not null)!; var compilationAndClasses = context.CompilationProvider.Combine(pluginDeclarations.WithComparer(Comparer.Instance).Collect()); - context.RegisterSourceOutput(compilationAndClasses, (spc, source) => Generate(source.Item1, source.Item2, spc)); + context.RegisterSourceOutput(compilationAndClasses, (spc, source) => Generate(source.Left, source.Right, spc)); + + + + var pluginDeclarations2 = context.SyntaxProvider.CreateSyntaxProvider( + static (s, _) => s is TypeDeclarationSyntax, + static (ctx, _) => (TypeDeclarationSyntax)ctx.Node + ).Where(static m => m is not null)!; + + + var compilationAndClasses2 = context.CompilationProvider.Combine(pluginDeclarations2.WithComparer(Comparer.Instance).Collect()); + context.RegisterSourceOutput(compilationAndClasses2, (spc, source) => + { + var classes = source.Right; + var compilation = source.Left; + + if (classes.IsDefaultOrEmpty) + return; + + var iPluginSymbol = compilation.GetTypeByMetadataName("TinyEcs.IPlugin"); + if (iPluginSymbol == null) + return; + + var hashDelegates = new HashSet<(string MethodName, string Parameters)>(); + var sb = new StringBuilder(); + var allowedMethodNames = new Dictionary + { + { "OnStartup2", "OnStartup" }, + { "OnFrameStart2", "OnFrameStart" }, + { "OnBeforeUpdate2", "OnBeforeUpdate" }, + { "OnUpdate2", "OnUpdate" }, + { "OnAfterUpdate2", "OnAfterUpdate" }, + { "OnFrameEnd2", "OnFrameEnd" }, + }; + + foreach (var cl in classes) + { + var semanticModel = compilation.GetSemanticModel(cl.SyntaxTree); + + if (ModelExtensions.GetDeclaredSymbol(semanticModel, cl) is not INamedTypeSymbol symbol) + continue; + + var pluginInterfaces = symbol.AllInterfaces + .Where(s => SymbolEqualityComparer.Default.Equals(s, iPluginSymbol)) + .ToArray(); + + if (pluginInterfaces.Length == 0) + continue; + + + var build = symbol.GetMembers("Build") + .OfType() + .SingleOrDefault(m => m.Parameters.Length == 1 && + m.Parameters[0].Type.ToDisplayString() == "TinyEcs.Scheduler"); + + if (build == null) + continue; + + // Step 1: Get the syntax node for the method + if (build.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax buildSyntax) + continue; + + var semanticModel2 = compilation.GetSemanticModel(buildSyntax.SyntaxTree); + + // Step 2: Traverse the method body to find calls + + foreach (var methodName in allowedMethodNames.Keys) + { + var calls = GetMethodCalls(buildSyntax, semanticModel2, methodName); + + foreach (var invocation in calls) + { + ProcessCallInvocations(invocation, semanticModel2, methodName, hashDelegates); + } + } + } + + foreach ((var methodName, var paramList) in hashDelegates) + { + sb.AppendLine($@" + public static FuncSystem {methodName}(this Scheduler scheduler, {paramList} fn, ThreadingMode threading = ThreadingMode.Auto) {{ + return scheduler.{allowedMethodNames[methodName]}(fn, threading); + }}"); + } + + var template = $@" + // This code is auto-generated. + + using TinyEcs; + + public static partial class SchedulerExt {{ + {sb} + }} + "; + + spc.AddSource($"SchedulerExt.g.cs", + CSharpSyntaxTree.ParseText(template).GetRoot().NormalizeWhitespace().ToFullString()); + }); } - private static TypeDeclarationSyntax? GetClassDeclarationSymbolIfAttributeof(GeneratorSyntaxContext context, string name) + private static InvocationExpressionSyntax[] GetMethodCalls(MethodDeclarationSyntax buildSyntax, SemanticModel semanticModel, string name) + { + var calls = buildSyntax.DescendantNodes() + .OfType() + .Where(invocation => + { + // Get the expression being called + if (invocation.Expression is MemberAccessExpressionSyntax memberAccess) + { + var methodName = memberAccess.Name.Identifier.Text; + if (methodName != name) + return false; + + // Ensure the receiver is 'scheduler' + var symbolInfo = semanticModel.GetSymbolInfo(memberAccess.Expression).Symbol; + if (symbolInfo is IParameterSymbol parameter && + parameter.Name == "scheduler") + { + return true; + } + + // Optional: match by string name instead (less reliable) + // return memberAccess.Expression.ToString() == "scheduler"; + } + + return false; + }) + .ToArray(); + + return calls; + } + + private static void ProcessCallInvocations( + InvocationExpressionSyntax invocation, + SemanticModel semanticModel, + string methodName, + HashSet<(string ,string)> cacheDelegates + ) + { + var argument = invocation.ArgumentList.Arguments.FirstOrDefault()?.Expression; + if (argument is null) + return; + + var symbolInfo = semanticModel.GetSymbolInfo(argument); + var found = symbolInfo.Symbol ?? symbolInfo.CandidateSymbols.FirstOrDefault(); + + if (found is not IMethodSymbol method) + return; + + // if (!cache.Add(method.Name)) + // return; + + var j = string.Join(", ", method.Parameters.Select(s => s.Type.ToDisplayString())); + var act = $"{(method.Parameters.Any() ? $"Action<{j}>" : "Action")}"; + // var p = string.Join(", ", method.Parameters.Select(s => s.ToDisplayString())); + if (!cacheDelegates.Add((methodName, act))) + return; + + // var fileName = $"SchedulerExt_{found.Name}"; + + +// var template = $@" +// using TinyEcs; +// +// public static class {fileName} {{ +// public static void OnUpdate2(this Scheduler scheduler, {act} fn) +// {{ +// var system = scheduler.OnUpdate(fn); +// }} +// }} +// "; +// +// context.AddSource($"{fileName}.g.cs", +// CSharpSyntaxTree.ParseText(template).GetRoot().NormalizeWhitespace().ToFullString()); + } + + private static void ProcessLocalFunction(LocalFunctionStatementSyntax localFunc, INamedTypeSymbol pluginClass, GeneratorExecutionContext context) + { + var methodName = $"OnUpdate_{Guid.NewGuid():N}"; + + var parameters = localFunc.ParameterList.Parameters; + var body = localFunc.Body ?? SyntaxFactory.Block(SyntaxFactory.ExpressionStatement(localFunc.ExpressionBody!.Expression)); + + var methodSource = new StringBuilder(); + methodSource.AppendLine($"private static void {methodName}({string.Join(", ", parameters.Select(p => p.ToFullString()))})"); + methodSource.AppendLine(body.ToFullString()); + +// var ns = pluginClass.ContainingNamespace.ToDisplayString(); +// var className = pluginClass.Name; +// +// var fullSource = $@" +// namespace {ns} +// {{ +// public partial class {className} +// {{ +// {methodSource} +// }} +// }} +// "; +// +// context.AddSource($"{className}_{methodName}.g.cs", SourceText.From(fullSource, Encoding.UTF8)); + } + + + private static void ProcessLambda( + LambdaExpressionSyntax lambda, + InvocationExpressionSyntax originalInvocation, + INamedTypeSymbol pluginClassSymbol, + GeneratorExecutionContext context) + { + var parameters = lambda switch + { + SimpleLambdaExpressionSyntax simple => new[] { simple.Parameter }, + ParenthesizedLambdaExpressionSyntax complex => complex.ParameterList.Parameters.ToArray(), + _ => Array.Empty() + }; + + var lambdaBody = lambda.Body; + + var methodName = $"OnUpdate_{Guid.NewGuid().ToString("N")}"; + + var methodText = new StringBuilder(); + methodText.AppendLine($"private static void {methodName}({string.Join(", ", parameters.Select(p => p.ToFullString()))})"); + + if (lambdaBody is BlockSyntax block) + methodText.AppendLine(block.ToFullString()); + else if (lambdaBody is ExpressionSyntax expr) + methodText.AppendLine($"{{ return {expr.ToFullString()}; }}"); + +// // Create the partial class +// var ns = pluginClassSymbol.ContainingNamespace.ToDisplayString(); +// var className = pluginClassSymbol.Name; +// +// var fullSource = $@" +// namespace {ns} +// {{ +// public partial class {className} +// {{ +// {methodText} +// }} +// }} +// "; +// +// context.AddSource($"{className}_{methodName}.g.cs", SourceText.From(fullSource, Encoding.UTF8)); + + // Optionally: replace the lambda with the method name in a syntax rewriter + } + + + + private static TypeDeclarationSyntax? GetClassDeclarationSymbolIfAttributeOf(GeneratorSyntaxContext context, string name) { var enumDeclarationSyntax = (TypeDeclarationSyntax)context.Node; @@ -67,9 +314,8 @@ private void Generate(Compilation compilation, ImmutableArray().ToList(); - var allSystems = allMethods.Where(s => s.GetAttributes() - .Any(k => k.AttributeClass.ToDisplayString() == "TinyEcs.TinySystemAttribute")) + .Any(k => k.AttributeClass.ToDisplayString() is "TinyEcs.TinySystemAttribute")) .ToList(); var sbDeclarations = new StringBuilder(); @@ -117,6 +363,8 @@ private static (string, string) GenerateOne(IMethodSymbol methodSymbol, List s.AttributeClass.Name.Contains("RunIf")).ToArray(); var beforeOfData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("BeforeOf")).ToArray(); var afterOfData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("AfterOf")).ToArray(); + var onEnterData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("OnEnter")).ToArray(); + var onExitData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("OnExit")).ToArray(); var arguments = methodSymbol.Parameters.ToList(); @@ -133,6 +381,8 @@ private static (string, string) GenerateOne(IMethodSymbol methodSymbol, List Date: Sun, 7 Sep 2025 12:23:25 +0200 Subject: [PATCH 12/33] minor --- samples/MyBattleground/Program.cs | 186 +++++++++++++++--------------- src/Bevy.cs | 2 +- 2 files changed, 94 insertions(+), 94 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index 00951ab6..38e1d552 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -9,11 +9,11 @@ using var ecs = new World(); var scheduler = new Scheduler(ecs); -// scheduler.AddPlugin(); -// scheduler.AddPlugin(); +scheduler.AddPlugin(); +scheduler.AddPlugin(); -scheduler.AddPlugin(); -scheduler.AddPlugin(); +// scheduler.AddPlugin(); +// scheduler.AddPlugin(); scheduler.RunOnce(); return; @@ -392,92 +392,92 @@ void Palle() // } -// -// namespace ANamespace -// { -// [TinyPlugin] -// public partial class AAA -// { -// [TinySystem(Stages.Update, ThreadingMode.Single)] -// [RunIf(nameof(TestRun))] -// [RunIf(nameof(TestRun2))] -// void Execute(Query> query) -// { -// foreach (var (pos, vel) in query) -// { -// pos.Ref.X *= vel.Ref.X; -// pos.Ref.Y *= vel.Ref.Y; -// } -// } -// -// -// [TinySystem(Stages.OnEnter, ThreadingMode.Single)] -// static void CheckState(World world) -// { -// -// } -// -// [TinySystem] -// [RunIf(nameof(TestRun2))] -// [RunIf(nameof(TestRun2))] -// [RunIf(nameof(TestRun2))] -// [RunIf(nameof(TestRun2))] -// static void DoThat(Query> query, EventWriter writer) -// { -// foreach (var (pos, vel) in query) -// { -// pos.Ref.X *= vel.Ref.X; -// pos.Ref.Y *= vel.Ref.Y; -// } -// } -// -// -// [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Second))] -// void First() -// { -// Console.WriteLine("1"); -// } -// -// [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Third))] -// void Second(EventReader reader) -// { -// Console.WriteLine("2"); -// } -// -// [TinySystem(threadingMode: ThreadingMode.Single), BeforeOf("HELLO")] -// void Third() -// { -// Console.WriteLine("3"); -// } -// -// private bool TestRun(SchedulerState state, World world, Local index) -// { -// return true; -// } -// -// private bool TestRun2() -// { -// return true; -// } -// -// public void Build(Scheduler scheduler) -// { -// scheduler.AddEvent(); -// } -// -// struct CustomEvent -// { -// public int Value; -// } -// } -// -// } -// -// [TinyPlugin] -// public partial class TestPlugin -// { -// public void Build(Scheduler scheduler) -// { -// -// } -// } + +namespace ANamespace +{ + [TinyPlugin] + public partial class AAA + { + [TinySystem(Stages.Update, ThreadingMode.Single)] + [RunIf(nameof(TestRun))] + [RunIf(nameof(TestRun2))] + void Execute(Query> query) + { + foreach (var (pos, vel) in query) + { + pos.Ref.X *= vel.Ref.X; + pos.Ref.Y *= vel.Ref.Y; + } + } + + + [TinySystem(Stages.OnEnter, ThreadingMode.Single)] + static void CheckState(World world) + { + + } + + [TinySystem] + [RunIf(nameof(TestRun2))] + [RunIf(nameof(TestRun2))] + [RunIf(nameof(TestRun2))] + [RunIf(nameof(TestRun2))] + static void DoThat(Query> query, EventWriter writer) + { + foreach (var (pos, vel) in query) + { + pos.Ref.X *= vel.Ref.X; + pos.Ref.Y *= vel.Ref.Y; + } + } + + + [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Second))] + void First() + { + Console.WriteLine("1"); + } + + [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Third))] + void Second(EventReader reader) + { + Console.WriteLine("2"); + } + + [TinySystem(threadingMode: ThreadingMode.Single), BeforeOf("HELLO")] + void Third() + { + Console.WriteLine("3"); + } + + private bool TestRun(SchedulerState state, World world, Local index) + { + return true; + } + + private bool TestRun2() + { + return true; + } + + public void Build(Scheduler scheduler) + { + scheduler.AddEvent(); + } + + struct CustomEvent + { + public int Value; + } + } + +} + +[TinyPlugin] +public partial class TestPlugin +{ + public void Build(Scheduler scheduler) + { + + } +} diff --git a/src/Bevy.cs b/src/Bevy.cs index b2b08fef..8af96bc9 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -1342,7 +1342,7 @@ public sealed class AfterOf(string systemName) : Attribute } -[AttributeUsage(System.AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)] +[AttributeUsage(System.AttributeTargets.Class | AttributeTargets.Struct)] public sealed class TinyPluginAttribute : Attribute { } From 6133d32b25763979a53b500b3ee070d0a5b577e9 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sun, 7 Sep 2025 16:57:20 +0200 Subject: [PATCH 13/33] + optional parm for TinySystemAttribute --- TinyEcs.slnx | 1 + src/Bevy.cs | 22 ++++++++++++++++--- tools/TinyEcs.SourceGenerator/Program.cs | 18 +++++++-------- .../TinyEcs.SourceGenerator.csproj | 2 +- 4 files changed, 30 insertions(+), 13 deletions(-) diff --git a/TinyEcs.slnx b/TinyEcs.slnx index 5d047c7f..92bf847d 100644 --- a/TinyEcs.slnx +++ b/TinyEcs.slnx @@ -11,5 +11,6 @@ + diff --git a/src/Bevy.cs b/src/Bevy.cs index 8af96bc9..e459ef85 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -1317,10 +1317,26 @@ public bool MoveNext() [AttributeUsage(AttributeTargets.Method)] -public class TinySystemAttribute(Stages stage = Stages.Update, ThreadingMode threadingMode = ThreadingMode.Auto) : Attribute +public class TinySystemAttribute : Attribute { - public Stages Stage { get; } = stage; - public ThreadingMode ThreadingMode { get; } = threadingMode; + public TinySystemAttribute(Stages stage = Stages.Update) + { + Stage = stage; + ThreadingMode = null; + } + + public TinySystemAttribute(ThreadingMode threadingMode) : this(Stages.Update, threadingMode) + { + } + + public TinySystemAttribute(Stages stage, ThreadingMode threadingMode) + { + Stage = stage; + ThreadingMode = threadingMode; + } + + public Stages Stage { get; } + public ThreadingMode? ThreadingMode { get; } } [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index c1b2ebff..ef9cbee2 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -366,16 +366,16 @@ private static (string, string) GenerateOne(IMethodSymbol methodSymbol, List s.AttributeClass.Name.Contains("OnEnter")).ToArray(); var onExitData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("OnExit")).ToArray(); - var arguments = methodSymbol.Parameters.ToList(); + // var arguments = methodSymbol.Parameters.ToList(); - if (systemDescData.ConstructorArguments.Length < 2) - { - Debug.WriteLine($"Method {methodSymbol.Name} does not have the correct number of arguments for TinySystem attribute. Expected 2, got {systemDescData.ConstructorArguments.Length}."); - return (string.Empty, string.Empty); - } - var stage = (systemDescData.ConstructorArguments[0].Type.ToDisplayString(), systemDescData.ConstructorArguments[0].Value); - var threading = (systemDescData.ConstructorArguments[1].Type.ToDisplayString(), systemDescData.ConstructorArguments[1].Value); + var stagee = systemDescData.ConstructorArguments.FirstOrDefault(s => s.Type.ToDisplayString() == "TinyEcs.Stages"); + var threadingg = systemDescData.ConstructorArguments.FirstOrDefault(s => s.Type.ToDisplayString() == "TinyEcs.ThreadingMode"); + + var stage = stagee is { Kind: TypedConstantKind.Error } ? ("TinyEcs.Stages", "TinyEcs.Stages.Update") + : (stagee.Type.ToDisplayString(), stagee.Value); + var threading = threadingg is { Kind: TypedConstantKind.Error } ? ("TinyEcs.ThreadingMode?", "null") + : (threadingg.Type.ToDisplayString(), threadingg.Value); var classSymbol = methodSymbol.ContainingType; var runIfMethods = GetAssociatedMethods(runifData, allMethods, "RunIf", classSymbol); @@ -414,7 +414,7 @@ private static (string, string) GenerateOne(IMethodSymbol methodSymbol, Listenable enable preview - netstandard2.0 + netstandard2.0 From 077696e5bd8415d6ac57acc58d1700e1cf729846 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sun, 7 Sep 2025 18:15:58 +0200 Subject: [PATCH 14/33] + support for new system gen --- samples/MyBattleground/Program.cs | 200 +++++++++------ src/Bevy.cs | 9 +- tools/TinyEcs.SourceGenerator/Program.cs | 306 +++++++++++++++-------- 3 files changed, 326 insertions(+), 189 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index 38e1d552..f5a69110 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -9,6 +9,7 @@ using var ecs = new World(); var scheduler = new Scheduler(ecs); +scheduler.AddResource(new CustomResource()); scheduler.AddPlugin(); scheduler.AddPlugin(); @@ -298,107 +299,140 @@ enum AnotherState // } // } -class TestSys : IPlugin -{ - - public void Build(Scheduler scheduler) - { - scheduler.OnUpdate2(TestMethod); - var xx = TestMethod2; - scheduler.OnUpdate2(xx); - scheduler.OnUpdate2(TestMethod2); - scheduler.OnUpdate2(TestMethod2); - scheduler.OnUpdate2(TestMethod2); - scheduler.OnUpdate2(Palle) - .RunIf((World w) => - { - return true; - }); - - scheduler.OnUpdate2((Local w) => - { - - }); - - - scheduler.OnStartup2((World w) => { }); - } - - void TestMethod(World world, Query> query) - { +// class TestSys : IPlugin +// { +// +// public void Build(Scheduler scheduler) +// { +// scheduler.OnUpdate2(TestMethod); +// var xx = TestMethod2; +// scheduler.OnUpdate2(xx); +// scheduler.OnUpdate2(TestMethod2); +// scheduler.OnUpdate2(TestMethod2); +// scheduler.OnUpdate2(TestMethod2); +// scheduler.OnUpdate2(Palle) +// .RunIf((World w) => +// { +// return true; +// }); +// +// scheduler.OnUpdate2((Local w) => +// { +// +// }); +// +// +// scheduler.OnStartup2((World w) => { }); +// } +// +// void TestMethod(World world, Query> query) +// { +// +// } +// +// void TestMethod2(World world, Query> query) +// { +// +// } +// +// void Palle() +// { +// +// } +// } +// +// class TestSys2 : IPlugin +// { +// +// public void Build(Scheduler scheduler) +// { +// scheduler.OnUpdate2(TestMethod); +// var xx = TestMethod2; +// scheduler.OnUpdate2(xx); +// scheduler.OnUpdate2(TestMethod2, ThreadingMode.Single); +// scheduler.OnUpdate2(TestMethod2); +// scheduler.OnUpdate2(TestMethod2); +// scheduler.OnUpdate2(Palle) +// .RunIf((World w) => +// { +// return true; +// }); +// +// scheduler.OnUpdate2((Local w) => +// { +// +// }); +// } +// +// void TestMethod(World world, Query> query) +// { +// +// } +// +// void TestMethod2(World world, Query> query) +// { +// +// } +// +// void Palle() +// { +// +// } +// } - } - void TestMethod2(World world, Query> query) - { - - } +// namespace TinyEcs +// { +// public delegate void SystemFn(World param1, SchedulerState param2); +// public partial class Scheduler { +// public void OnUpdate2(SystemFn fn) +// { +// +// } +// } +// } - void Palle() - { +// partial class GenSystem : TinySystem2 +// { +// private Query> _query; +// +// public override void Setup(World world) +// { +// _query = (Query>)Query>.Generate(world); +// } +// +// public override void Execute(World world) +// { +// Execute(_query); +// } +// } - } -} -class TestSys2 : IPlugin +[TinySystem] +partial class GenSystem { - - public void Build(Scheduler scheduler) + void Execute(World world, Query> query, Res customRes) { - scheduler.OnUpdate2(TestMethod); - var xx = TestMethod2; - scheduler.OnUpdate2(xx); - scheduler.OnUpdate2(TestMethod2, ThreadingMode.Single); - scheduler.OnUpdate2(TestMethod2); - scheduler.OnUpdate2(TestMethod2); - scheduler.OnUpdate2(Palle) - .RunIf((World w) => - { - return true; - }); - scheduler.OnUpdate2((Local w) => - { - - }); } +} - void TestMethod(World world, Query> query) - { - - } - - void TestMethod2(World world, Query> query) - { - - } - void Palle() +[TinySystem] +partial class HelloSystem +{ + void Execute(Query> query, Local customRes) { } } - -// namespace TinyEcs -// { -// public delegate void SystemFn(World param1, SchedulerState param2); -// public partial class Scheduler { -// public void OnUpdate2(SystemFn fn) -// { -// -// } -// } -// } - - - namespace ANamespace { [TinyPlugin] public partial class AAA { - [TinySystem(Stages.Update, ThreadingMode.Single)] + [TinySystem] [RunIf(nameof(TestRun))] [RunIf(nameof(TestRun2))] void Execute(Query> query) @@ -481,3 +515,9 @@ public void Build(Scheduler scheduler) } } + + +public class CustomResource +{ + +} diff --git a/src/Bevy.cs b/src/Bevy.cs index e459ef85..59fcc734 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -1316,7 +1316,7 @@ public bool MoveNext() } -[AttributeUsage(AttributeTargets.Method)] +[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] public class TinySystemAttribute : Attribute { public TinySystemAttribute(Stages stage = Stages.Update) @@ -1363,4 +1363,11 @@ public sealed class TinyPluginAttribute : Attribute { } + +public abstract class TinySystem2 +{ + public abstract void Setup(World world); + public abstract void Execute(SystemTicks ticks, World world); +} + #endif diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index ef9cbee2..10b65bf8 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -16,111 +16,204 @@ public sealed class SourceGenerator : IIncrementalGenerator { public void Initialize(IncrementalGeneratorInitializationContext context) { - var pluginDeclarations = context.SyntaxProvider.CreateSyntaxProvider( - static (s, _) => s is TypeDeclarationSyntax { AttributeLists.Count: > 0 }, - static (ctx, _) => GetClassDeclarationSymbolIfAttributeOf(ctx, "TinyEcs.TinyPluginAttribute") - ).Where(static m => m is not null)!; - - var compilationAndClasses = context.CompilationProvider.Combine(pluginDeclarations.WithComparer(Comparer.Instance).Collect()); - context.RegisterSourceOutput(compilationAndClasses, (spc, source) => Generate(source.Left, source.Right, spc)); - - - - var pluginDeclarations2 = context.SyntaxProvider.CreateSyntaxProvider( - static (s, _) => s is TypeDeclarationSyntax, - static (ctx, _) => (TypeDeclarationSyntax)ctx.Node - ).Where(static m => m is not null)!; - - - var compilationAndClasses2 = context.CompilationProvider.Combine(pluginDeclarations2.WithComparer(Comparer.Instance).Collect()); - context.RegisterSourceOutput(compilationAndClasses2, (spc, source) => - { - var classes = source.Right; - var compilation = source.Left; - - if (classes.IsDefaultOrEmpty) - return; - - var iPluginSymbol = compilation.GetTypeByMetadataName("TinyEcs.IPlugin"); - if (iPluginSymbol == null) - return; - - var hashDelegates = new HashSet<(string MethodName, string Parameters)>(); - var sb = new StringBuilder(); - var allowedMethodNames = new Dictionary - { - { "OnStartup2", "OnStartup" }, - { "OnFrameStart2", "OnFrameStart" }, - { "OnBeforeUpdate2", "OnBeforeUpdate" }, - { "OnUpdate2", "OnUpdate" }, - { "OnAfterUpdate2", "OnAfterUpdate" }, - { "OnFrameEnd2", "OnFrameEnd" }, - }; - - foreach (var cl in classes) - { - var semanticModel = compilation.GetSemanticModel(cl.SyntaxTree); - - if (ModelExtensions.GetDeclaredSymbol(semanticModel, cl) is not INamedTypeSymbol symbol) - continue; - - var pluginInterfaces = symbol.AllInterfaces - .Where(s => SymbolEqualityComparer.Default.Equals(s, iPluginSymbol)) - .ToArray(); - - if (pluginInterfaces.Length == 0) - continue; - - - var build = symbol.GetMembers("Build") - .OfType() - .SingleOrDefault(m => m.Parameters.Length == 1 && - m.Parameters[0].Type.ToDisplayString() == "TinyEcs.Scheduler"); - - if (build == null) - continue; - - // Step 1: Get the syntax node for the method - if (build.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax buildSyntax) - continue; - - var semanticModel2 = compilation.GetSemanticModel(buildSyntax.SyntaxTree); - - // Step 2: Traverse the method body to find calls - - foreach (var methodName in allowedMethodNames.Keys) - { - var calls = GetMethodCalls(buildSyntax, semanticModel2, methodName); - - foreach (var invocation in calls) - { - ProcessCallInvocations(invocation, semanticModel2, methodName, hashDelegates); - } - } - } - - foreach ((var methodName, var paramList) in hashDelegates) - { - sb.AppendLine($@" - public static FuncSystem {methodName}(this Scheduler scheduler, {paramList} fn, ThreadingMode threading = ThreadingMode.Auto) {{ - return scheduler.{allowedMethodNames[methodName]}(fn, threading); - }}"); - } - - var template = $@" - // This code is auto-generated. - - using TinyEcs; - - public static partial class SchedulerExt {{ - {sb} - }} - "; - - spc.AddSource($"SchedulerExt.g.cs", - CSharpSyntaxTree.ParseText(template).GetRoot().NormalizeWhitespace().ToFullString()); - }); - } + RegisterPluginExtensionsGenerator(context); + RegisterTinySystemGenerator(context); + } + + #region Plugin Extensions Generator + private void RegisterPluginExtensionsGenerator(IncrementalGeneratorInitializationContext context) + { + var pluginDeclarations = context.SyntaxProvider.CreateSyntaxProvider( + static (s, _) => s is TypeDeclarationSyntax { AttributeLists.Count: > 0 }, + static (ctx, _) => GetClassDeclarationSymbolIfAttributeOf(ctx, "TinyEcs.TinyPluginAttribute") + ).Where(static m => m is not null); + + var compilationAndClasses = context.CompilationProvider.Combine(pluginDeclarations.WithComparer(Comparer.Instance).Collect()); + context.RegisterSourceOutput(compilationAndClasses, (spc, source) => Generate(source.Left, source.Right, spc)); + + var pluginDeclarations2 = context.SyntaxProvider.CreateSyntaxProvider( + static (s, _) => s is TypeDeclarationSyntax, + static (ctx, _) => (TypeDeclarationSyntax)ctx.Node + ).Where(static m => m is not null); + + var compilationAndClasses2 = context.CompilationProvider.Combine(pluginDeclarations2.WithComparer(Comparer.Instance).Collect()); + context.RegisterSourceOutput(compilationAndClasses2, (spc, source) => + { + var classes = source.Right; + var compilation = source.Left; + + if (classes.IsDefaultOrEmpty) return; + + var iPluginSymbol = compilation.GetTypeByMetadataName("TinyEcs.IPlugin"); + if (iPluginSymbol == null) return; + + var hashDelegates = new HashSet<(string MethodName, string Parameters)>(); + var sb = new StringBuilder(); + var allowedMethodNames = new Dictionary + { + { "OnStartup2", "OnStartup" }, + { "OnFrameStart2", "OnFrameStart" }, + { "OnBeforeUpdate2", "OnBeforeUpdate" }, + { "OnUpdate2", "OnUpdate" }, + { "OnAfterUpdate2", "OnAfterUpdate" }, + { "OnFrameEnd2", "OnFrameEnd" }, + }; + + foreach (var cl in classes) + { + var semanticModel = compilation.GetSemanticModel(cl.SyntaxTree); + + if (ModelExtensions.GetDeclaredSymbol(semanticModel, cl) is not INamedTypeSymbol symbol) + continue; + + var pluginInterfaces = symbol.AllInterfaces + .Where(s => SymbolEqualityComparer.Default.Equals(s, iPluginSymbol)) + .ToArray(); + + if (pluginInterfaces.Length == 0) + continue; + + + var build = symbol.GetMembers("Build") + .OfType() + .SingleOrDefault(m => m.Parameters.Length == 1 && + m.Parameters[0].Type.ToDisplayString() == "TinyEcs.Scheduler"); + + if (build == null) + continue; + + // Step 1: Get the syntax node for the method + if (build.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax buildSyntax) + continue; + + var semanticModel2 = compilation.GetSemanticModel(buildSyntax.SyntaxTree); + + // Step 2: Traverse the method body to find calls + + foreach (var methodName in allowedMethodNames.Keys) + { + var calls = GetMethodCalls(buildSyntax, semanticModel2, methodName); + + foreach (var invocation in calls) + { + ProcessCallInvocations(invocation, semanticModel2, methodName, hashDelegates); + } + } + } + + foreach ((var methodName, var paramList) in hashDelegates) + { + sb.AppendLine($@" + public static FuncSystem {methodName}(this Scheduler scheduler, {paramList} fn, ThreadingMode threading = ThreadingMode.Auto) {{ + return scheduler.{allowedMethodNames[methodName]}(fn, threading); + }}"); + } + + var template = $@" + // This code is auto-generated. + + using TinyEcs; + + public static partial class SchedulerExt {{ + {sb} + }} + "; + + spc.AddSource($"SchedulerExt.g.cs", + CSharpSyntaxTree.ParseText(template).GetRoot().NormalizeWhitespace().ToFullString()); + }); + } + #endregion + + #region TinySystem Generator + private void RegisterTinySystemGenerator(IncrementalGeneratorInitializationContext context) + { + var tinySystemClasses = context.SyntaxProvider.CreateSyntaxProvider( + static (s, _) => s is TypeDeclarationSyntax t && t.AttributeLists.Count > 0, + static (ctx, _) => + { + var typeSyntax = (TypeDeclarationSyntax)ctx.Node; + var typeSymbol = ctx.SemanticModel.GetDeclaredSymbol(typeSyntax); + if (typeSymbol is not INamedTypeSymbol namedTypeSymbol) return null; + if (!namedTypeSymbol.GetAttributes().Any(a => a.AttributeClass?.ToDisplayString() == "TinyEcs.TinySystemAttribute")) return null; + if (namedTypeSymbol.TypeKind != TypeKind.Class) return null; + return namedTypeSymbol; + } + ).Where(static t => t is not null); + + context.RegisterSourceOutput( + context.CompilationProvider.Combine(tinySystemClasses.Collect()), + (spc, source) => + { + var classes = source.Right; + if (classes.IsDefaultOrEmpty) return; + + foreach (var classSymbolObj in classes) + { + if (classSymbolObj is not null && classSymbolObj is INamedTypeSymbol classSymbol) + { + var className = classSymbol.Name.EndsWith("System") ? classSymbol.Name : classSymbol.Name + "System"; + var ns = classSymbol.ContainingNamespace.IsGlobalNamespace ? "" : classSymbol.ContainingNamespace.ToDisplayString(); + // Find the Execute method in the class + var executeMethodSymbol = classSymbol.GetMembers().OfType().FirstOrDefault(m => m.Name == "Execute" && !m.IsStatic); + if (executeMethodSymbol == null) continue; + + // Accept all parameters for injection + var validParams = executeMethodSymbol.Parameters.ToList(); + + // Declare private fields for each parameter + var fieldDecls = string.Join("\n", validParams.Select(p => $" private {p.Type.ToDisplayString()} _{p.Name};")); + + // Setup method: assign all fields + var setupAssignments = string.Join("\n", validParams.Select(p => $" _{p.Name} = ({p.Type.ToDisplayString()}){p.Type.ToDisplayString()}.Generate(world);")); + var setupCode = $@" public override void Setup(World world) + {{ +{setupAssignments} + }}"; + + var paramNames = string.Join(", ", validParams.Select(p => $"_{p.Name}")); + + var paramLocks = string.Join("\n", validParams.Select(p => $" _{p.Name}.Lock(ticks);")); + var paramUnlocks = string.Join("\n", validParams.Select(p => $" _{p.Name}.Unlock();")); + var executeOverride = $@" public override void Execute(SystemTicks ticks, World world) + {{ +{paramLocks} + world.BeginDeferred(); + Execute({paramNames}); + world.EndDeferred(); +{paramUnlocks} + }}"; + + // Only generate Setup and Execute(World world) + var systemClass = classSymbol.ContainingNamespace.IsGlobalNamespace + ? $@" +using TinyEcs; +public partial class {className} : TinySystem2 +{{ +{fieldDecls} +{setupCode} +{executeOverride} +}}" + : $@" +using TinyEcs; +namespace {ns} +{{ + public partial class {className} : TinySystem2 + {{ +{fieldDecls} +{setupCode} +{executeOverride} + }} +}}"; + + var fileName = $"{className}.g.cs"; + spc.AddSource(fileName, systemClass); + } + } + } + ); + } + #endregion private static InvocationExpressionSyntax[] GetMethodCalls(MethodDeclarationSyntax buildSyntax, SemanticModel semanticModel, string name) { @@ -366,9 +459,6 @@ private static (string, string) GenerateOne(IMethodSymbol methodSymbol, List s.AttributeClass.Name.Contains("OnEnter")).ToArray(); var onExitData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("OnExit")).ToArray(); - // var arguments = methodSymbol.Parameters.ToList(); - - var stagee = systemDescData.ConstructorArguments.FirstOrDefault(s => s.Type.ToDisplayString() == "TinyEcs.Stages"); var threadingg = systemDescData.ConstructorArguments.FirstOrDefault(s => s.Type.ToDisplayString() == "TinyEcs.ThreadingMode"); From 115b77d7b60683fc6d150db4995a90e8d57d81d6 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sun, 7 Sep 2025 20:58:50 +0200 Subject: [PATCH 15/33] + more src gen --- samples/MyBattleground/Program.cs | 329 +++++++++++--------- src/Bevy.cs | 374 ++++++++++++++++++----- tools/TinyEcs.SourceGenerator/Program.cs | 29 +- 3 files changed, 482 insertions(+), 250 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index f5a69110..9a0a71d9 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -9,15 +9,27 @@ using var ecs = new World(); var scheduler = new Scheduler(ecs); + +// var sys = new HelloSystem(); +// sys.Initialize(ecs); +// sys.ExecuteOnReady(ecs, ecs.Ticks.ThisRun); + scheduler.AddResource(new CustomResource()); -scheduler.AddPlugin(); -scheduler.AddPlugin(); + +scheduler.AddSystem2(Stages.Startup, new SetupSystem(ENTITIES_COUNT)); +scheduler.AddSystem2(Stages.Update); +scheduler.AddSystem2(Stages.Update, new HelloSystem()); + + +// scheduler.AddPlugin(); +// scheduler.AddPlugin(); // scheduler.AddPlugin(); // scheduler.AddPlugin(); -scheduler.RunOnce(); - -return; +// scheduler.RunOnce(); +// +// +// return; // ecs.Entity(); // ecs.Entity().Delete(); // var xx = ecs.Entity().Add(); @@ -109,10 +121,10 @@ // while (true) // scheduler.RunOnce(); -for (var i = 0; i < ENTITIES_COUNT; i++) - ecs.Entity() - .Set(new Position()) - .Set(new Velocity()); +// for (var i = 0; i < ENTITIES_COUNT; i++) +// ecs.Entity() +// .Set(new Position()) +// .Set(new Velocity()); // ecs.Entity().Set(new Position()).Set(new Velocity()).Set(new Mass()); @@ -122,57 +134,57 @@ // .Set(new Position()); // ecs.Entity().Set(new Velocity()).Set(new Position()); -scheduler.AddSystem([TinySystem] ( - Query> q, - Query, Added> added -) => -{ - - // foreach ((var pos, var vel) in or) - // { - // if (pos.IsValid()) - // { - // Console.Write("pos is valid "); - // } - // else - // { - - // } - - // if (vel.IsValid()) - // { - // Console.Write("vel is valid"); - // } - // else - // { - - // } - - // Console.WriteLine(); - // } - - foreach (var (pos, vel) in q) - { - pos.Ref.X *= vel.Ref.X; - pos.Ref.Y *= vel.Ref.Y; - - // pos.Ref.X *= vel.Ref.X; - // pos.Ref.Y *= vel.Ref.Y; - - // if (pos.IsChanged) - // pos.ClearState(); - } - - // foreach ((var pos, var vel) in added) - // { - // pos.Ref.X *= vel.Ref.X; - // pos.Ref.Y *= vel.Ref.Y; - - // // if (pos.IsAdded) - // // pos.ClearState(); - // } -}, threadingType: ThreadingMode.Single); - +// scheduler.AddSystem([TinySystem] ( +// Query> q, +// Query, Added> added +// ) => +// { +// +// // foreach ((var pos, var vel) in or) +// // { +// // if (pos.IsValid()) +// // { +// // Console.Write("pos is valid "); +// // } +// // else +// // { +// +// // } +// +// // if (vel.IsValid()) +// // { +// // Console.Write("vel is valid"); +// // } +// // else +// // { +// +// // } +// +// // Console.WriteLine(); +// // } +// +// foreach (var (pos, vel) in q) +// { +// pos.Ref.X *= vel.Ref.X; +// pos.Ref.Y *= vel.Ref.Y; +// +// // pos.Ref.X *= vel.Ref.X; +// // pos.Ref.Y *= vel.Ref.Y; +// +// // if (pos.IsChanged) +// // pos.ClearState(); +// } +// +// // foreach ((var pos, var vel) in added) +// // { +// // pos.Ref.X *= vel.Ref.X; +// // pos.Ref.Y *= vel.Ref.Y; +// +// // // if (pos.IsAdded) +// // // pos.ClearState(); +// // } +// }, threadingType: ThreadingMode.Single); +// var query = ecs.QueryBuilder() .With() @@ -187,9 +199,9 @@ Query, Added> added { for (int i = 0; i < 3600; ++i) { - // scheduler.RunOnce(); + scheduler.RunOnce(); - Execute(query); + // Execute(query); // ExecuteIterator(query); // var it = query.Iter(); @@ -407,115 +419,130 @@ enum AnotherState // } // } - [TinySystem] -partial class GenSystem +partial class SetupSystem(int count) { - void Execute(World world, Query> query, Res customRes) + void OnExecute(World world, World world2) { - + for (var i = 0; i < count; i++) + world.Entity() + .Set(new Position()) + .Set(new Velocity()); } } - [TinySystem] -partial class HelloSystem -{ - void Execute(Query> query, Local customRes) - { - - } -} - -namespace ANamespace +partial class GenSystem { - [TinyPlugin] - public partial class AAA + void OnExecute(World world, Query> query, Res customRes) { - [TinySystem] - [RunIf(nameof(TestRun))] - [RunIf(nameof(TestRun2))] - void Execute(Query> query) - { - foreach (var (pos, vel) in query) - { - pos.Ref.X *= vel.Ref.X; - pos.Ref.Y *= vel.Ref.Y; - } - } - - - [TinySystem(Stages.OnEnter, ThreadingMode.Single)] - static void CheckState(World world) - { - - } - - [TinySystem] - [RunIf(nameof(TestRun2))] - [RunIf(nameof(TestRun2))] - [RunIf(nameof(TestRun2))] - [RunIf(nameof(TestRun2))] - static void DoThat(Query> query, EventWriter writer) - { - foreach (var (pos, vel) in query) - { - pos.Ref.X *= vel.Ref.X; - pos.Ref.Y *= vel.Ref.Y; - } - } - - - [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Second))] - void First() - { - Console.WriteLine("1"); - } - - [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Third))] - void Second(EventReader reader) - { - Console.WriteLine("2"); - } - - [TinySystem(threadingMode: ThreadingMode.Single), BeforeOf("HELLO")] - void Third() - { - Console.WriteLine("3"); - } - - private bool TestRun(SchedulerState state, World world, Local index) + foreach ((var pos, var vel) in query) { - return true; - } - - private bool TestRun2() - { - return true; - } - - public void Build(Scheduler scheduler) - { - scheduler.AddEvent(); - } - - struct CustomEvent - { - public int Value; + pos.Ref.X *= vel.Ref.X; + pos.Ref.Y *= vel.Ref.Y; } } - } -[TinyPlugin] -public partial class TestPlugin + +[TinySystem] +partial class HelloSystem { - public void Build(Scheduler scheduler) + void OnExecute(Query> query, Local customRes) { } } +// namespace ANamespace +// { +// [TinyPlugin] +// public partial class AAA +// { +// [TinySystem] +// [RunIf(nameof(TestRun))] +// [RunIf(nameof(TestRun2))] +// void Execute(Query> query) +// { +// foreach (var (pos, vel) in query) +// { +// pos.Ref.X *= vel.Ref.X; +// pos.Ref.Y *= vel.Ref.Y; +// } +// } +// +// +// [TinySystem(Stages.OnEnter, ThreadingMode.Single)] +// static void CheckState(World world) +// { +// +// } +// +// [TinySystem] +// [RunIf(nameof(TestRun2))] +// [RunIf(nameof(TestRun2))] +// [RunIf(nameof(TestRun2))] +// [RunIf(nameof(TestRun2))] +// static void DoThat(Query> query, EventWriter writer) +// { +// foreach (var (pos, vel) in query) +// { +// pos.Ref.X *= vel.Ref.X; +// pos.Ref.Y *= vel.Ref.Y; +// } +// } +// +// +// [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Second))] +// void First() +// { +// Console.WriteLine("1"); +// } +// +// [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Third))] +// void Second(EventReader reader) +// { +// Console.WriteLine("2"); +// } +// +// [TinySystem(threadingMode: ThreadingMode.Single), BeforeOf("HELLO")] +// void Third() +// { +// Console.WriteLine("3"); +// } +// +// private bool TestRun(SchedulerState state, World world, Local index) +// { +// return true; +// } +// +// private bool TestRun2() +// { +// return true; +// } +// +// public void Build(Scheduler scheduler) +// { +// scheduler.AddEvent(); +// } +// +// struct CustomEvent +// { +// public int Value; +// } +// } +// +// } +// +// [TinyPlugin] +// public partial class TestPlugin +// { +// public void Build(Scheduler scheduler) +// { +// +// } +// } + public class CustomResource { diff --git a/src/Bevy.cs b/src/Bevy.cs index 59fcc734..45953c23 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -6,7 +6,8 @@ namespace TinyEcs; #if NET9_0_OR_GREATER -public sealed partial class FuncSystem where TArg : notnull +public sealed partial class FuncSystem : ITinySystem + where TArg : notnull { private readonly TArg _arg; private readonly Func, bool> _fn; @@ -15,10 +16,6 @@ public sealed partial class FuncSystem where TArg : notnull private readonly Func _checkInUse; private readonly Stages _stage; private readonly ThreadingMode _threadingType; - private readonly LinkedList> _after = new(); - private readonly LinkedList> _before = new(); - internal LinkedListNode>? Node { get; set; } - internal SystemTicks Ticks { get; } = new(); internal FuncSystem(TArg arg, Func, bool> fn, Func checkInUse, Stages stage, ThreadingMode threadingType) @@ -32,21 +29,21 @@ internal FuncSystem(TArg arg, Func RunIf(Func condition) { @@ -54,74 +51,138 @@ public FuncSystem RunIf(Func condition) return this; } - public FuncSystem RunAfter(FuncSystem parent) - { - if (this == parent || Contains(parent, s => s._after)) - throw new InvalidOperationException("Circular dependency detected"); - - Node?.List?.Remove(Node); - Node = parent._after.AddLast(this); + // public FuncSystem RunAfter(ITinySystem parent) + // { + // if (this == parent || Contains(parent, s => s.Configuration.AfterSystems)) + // throw new InvalidOperationException("Circular dependency detected"); + // + // Configuration.Node?.List?.Remove(Configuration.Node); + // Configuration.Node = parent.Configuration.AfterSystems.AddLast(this); + // + // return this; + // } + // + // public FuncSystem RunAfter(params ReadOnlySpan systems) + // { + // foreach (var system in systems) + // system.RunAfter(this); + // + // return this; + // } + // + // public FuncSystem RunBefore(ITinySystem parent) + // { + // if (this == parent || Contains(parent, s => s.Configuration.BeforeSystems)) + // throw new InvalidOperationException("Circular dependency detected"); + // + // Configuration.Node?.List?.Remove(Configuration.Node); + // Configuration.Node = parent.Configuration.BeforeSystems.AddLast(this); + // + // return this; + // } + // + // public FuncSystem RunBefore(params ReadOnlySpan systems) + // { + // foreach (var system in systems) + // system.RunBefore(this); + // + // return this; + // } + // + // private bool Contains(ITinySystem system, Func?> direction) + // { + // ITinySystem? current = this; + // while (current != null) + // { + // if (current == system) + // return true; + // + // var nextNode = direction(current)?.First; + // current = nextNode?.Value; + // } + // return false; + // } - return this; + internal bool IsResourceInUse() + { + return _threadingType switch + { + ThreadingMode.Multi => false, + ThreadingMode.Single => true, + _ or ThreadingMode.Auto => _checkInUse() + }; } - public FuncSystem RunAfter(params ReadOnlySpan> systems) + private bool ValidateConditions(SystemTicks ticks, TArg args) { - foreach (var system in systems) - system.RunAfter(this); - - return this; + foreach (var fn in _conditions) + if (!fn(ticks, args)) + return false; + return true; } - public FuncSystem RunBefore(FuncSystem parent) - { - if (this == parent || Contains(parent, s => s._before)) - throw new InvalidOperationException("Circular dependency detected"); + public ISystemParam[] SystemParams { get; set; } + public SystemTicks Ticks { get; } + public SystemConfiguration Configuration { get; } - Node?.List?.Remove(Node); - Node = parent._before.AddLast(this); + public void Initialize(World world) + { - return this; } - public FuncSystem RunBefore(params ReadOnlySpan> systems) + public bool ExecuteOnReady(World world, uint ticks) { - foreach (var system in systems) - system.RunBefore(this); + Ticks.ThisRun = ticks; + var canRun = true; - return this; - } + var config = Configuration; - private bool Contains(FuncSystem system, Func, LinkedList>> direction) - { - var current = this; - while (current != null) + if (config.BeforeSystems != null) { - if (current == system) - return true; - - var nextNode = direction(current)?.First; - current = nextNode?.Value; + foreach (var beforeSys in config.BeforeSystems) + { + if (!beforeSys.ExecuteOnReady(world, ticks)) + { + canRun = false; + break; + } + } } - return false; - } - internal bool IsResourceInUse() - { - return _threadingType switch + if (canRun) { - ThreadingMode.Multi => false, - ThreadingMode.Single => true, - _ or ThreadingMode.Auto => _checkInUse() - }; + foreach (var conditional in config.Conditionals) + { + if (!conditional.ExecuteOnReady(world, ticks)) + { + canRun = false; + break; + } + } + + _fn(Ticks, _arg, _validator); + + if (config.AfterSystems != null) + { + foreach (var afterSys in config.AfterSystems) + { + if (!afterSys.ExecuteOnReady(world, ticks)) + { + canRun = false; + break; + } + } + } + } + + Ticks.LastRun = Ticks.ThisRun; + + return canRun; } - private bool ValidateConditions(SystemTicks ticks, TArg args) + public bool ParamsAreLocked() { - foreach (var fn in _conditions) - if (!fn(ticks, args)) - return false; - return true; + return IsResourceInUse(); } } @@ -154,18 +215,20 @@ public sealed class SystemTicks public partial class Scheduler { private readonly World _world; - private readonly LinkedList>[] _systems = new LinkedList>[(int)Stages.OnExit + 1]; - private readonly List> _singleThreads = new(); - private readonly List> _multiThreads = new(); + private readonly LinkedList[] _systems = new LinkedList[(int)Stages.OnExit + 1]; + private readonly List _singleThreads = new(); + private readonly List _multiThreads = new(); private readonly Dictionary _events = new(); + private bool _initialized; + public Scheduler(World world, ThreadingMode threadingMode = ThreadingMode.Auto) { _world = world; ThreadingExecutionMode = threadingMode; for (var i = 0; i < _systems.Length; ++i) - _systems[i] = new LinkedList>(); + _systems[i] = new LinkedList(); AddSystemParam(world); AddSystemParam(new SchedulerState(this)); @@ -186,6 +249,18 @@ public void Run(Func checkForExitFn, Action? cleanupFn = null) public void RunOnce() { + if (!_initialized) + { + foreach (var stage in _systems) + { + foreach (var system in stage) + { + system.Initialize(World); + } + } + + _initialized = true; + } var ticks = _world.Update(); foreach ((_, var ev) in _events) @@ -213,7 +288,7 @@ private void RunStage(Stages stage, uint ticks) foreach (var sys in systems) { - if (sys.IsResourceInUse()) + if (sys.ParamsAreLocked()) { _singleThreads.Add(sys); } @@ -227,15 +302,37 @@ private void RunStage(Stages stage, uint ticks) var singlethreading = _singleThreads; if (multithreading.Count > 0) - Parallel.ForEach(multithreading, s => s.Run(ticks)); + Parallel.ForEach(multithreading, s => s.ExecuteOnReady(World, ticks)); foreach (var system in singlethreading) - system.Run(ticks); + system.ExecuteOnReady(World, ticks); + } + + internal void Add(ITinySystem sys, Stages stage) + { + sys.Configuration.Node = _systems[(int)stage].AddLast(sys); + } + + public Scheduler AddSystem2(Stages stage) where T : ITinySystem, new() + { + var system = new T(); + Add(system, stage); + return this; + } + + public Scheduler AddSystem2(Stages stage, ITinySystem system) + { + Add(system, stage); + return this; } - internal void Add(FuncSystem sys, Stages stage) + public Scheduler AddSystems2(Stages stage, params ITinySystem[] systems) { - sys.Node = _systems[(int)stage].AddLast(sys); + foreach (var system in systems) + { + Add(system, stage); + } + return this; } public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) @@ -494,8 +591,6 @@ public static ISystemParam Generate(World arg) partial class World : SystemParam, IIntoSystemParam { - public World() : this(256) { } - public static ISystemParam Generate(World arg) { return arg; @@ -1364,10 +1459,125 @@ public sealed class TinyPluginAttribute : Attribute } -public abstract class TinySystem2 +public sealed class SystemParamBuilder(World world) +{ + private readonly List _params = []; + + public T Add() where T : ISystemParam, IIntoSystemParam + { + var param = (T)T.Generate(world); + _params.Add(param); + return param; + } + + public ISystemParam[] Build() + => _params.ToArray(); +} + +public sealed class SystemConfiguration +{ + public List Conditionals { get; } = []; + public LinkedList BeforeSystems { get; set; } = new(); + public LinkedList AfterSystems { get; set; } = new(); + public LinkedListNode? Node { get; set; } + public ThreadingMode ThreadingMode { get; set; } = ThreadingMode.Auto; + public Stages Stage { get; set; } = Stages.Update; +} + +public interface ITinySystem +{ + ISystemParam[] SystemParams { get; set; } + SystemTicks Ticks { get; } + SystemConfiguration Configuration { get; } + + void Initialize(World world); + + bool ExecuteOnReady(World world, uint ticks); + + bool ParamsAreLocked(); +} + +public abstract class TinySystem2 : ITinySystem { - public abstract void Setup(World world); - public abstract void Execute(SystemTicks ticks, World world); + public ISystemParam[] SystemParams { get; set; } = []; + public SystemTicks Ticks { get; } = new(); + + public SystemConfiguration Configuration { get; } = new(); + + public void Initialize(World world) + { + var builder = new SystemParamBuilder(world); + Setup(builder); + SystemParams = builder.Build(); + } + + public bool ExecuteOnReady(World world, uint ticks) + { + Ticks.ThisRun = ticks; + var canRun = true; + + var config = Configuration; + + foreach (var beforeSys in config.BeforeSystems) + { + if (!beforeSys.ExecuteOnReady(world, ticks)) + { + canRun = false; + break; + } + } + + if (canRun) + { + foreach (var conditional in config.Conditionals) + { + if (!conditional.ExecuteOnReady(world, ticks)) + { + canRun = false; + break; + } + } + + Execute(world); + + foreach (var afterSys in config.AfterSystems) + { + if (!afterSys.ExecuteOnReady(world, ticks)) + { + canRun = false; + break; + } + } + } + + Ticks.LastRun = Ticks.ThisRun; + + return canRun; + } + + public bool ParamsAreLocked() + { + return Configuration.ThreadingMode switch { + ThreadingMode.Single => true, + ThreadingMode.Multi => false, + _ => SystemParams.All(static p => p.UseIndex > 0) + }; + } + + protected void Lock() + { + foreach (var param in SystemParams) + param.Lock(Ticks); + } + + protected void Unlock() + { + foreach (var param in SystemParams) + param.Unlock(); + } + + protected abstract void Setup(SystemParamBuilder builder); + public abstract void Execute(World world); } #endif diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 10b65bf8..42877783 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -150,12 +150,12 @@ private void RegisterTinySystemGenerator(IncrementalGeneratorInitializationConte foreach (var classSymbolObj in classes) { - if (classSymbolObj is not null && classSymbolObj is INamedTypeSymbol classSymbol) + if (classSymbolObj is { } classSymbol) { var className = classSymbol.Name.EndsWith("System") ? classSymbol.Name : classSymbol.Name + "System"; var ns = classSymbol.ContainingNamespace.IsGlobalNamespace ? "" : classSymbol.ContainingNamespace.ToDisplayString(); // Find the Execute method in the class - var executeMethodSymbol = classSymbol.GetMembers().OfType().FirstOrDefault(m => m.Name == "Execute" && !m.IsStatic); + var executeMethodSymbol = classSymbol.GetMembers().OfType().FirstOrDefault(m => m.Name == "OnExecute" && !m.IsStatic); if (executeMethodSymbol == null) continue; // Accept all parameters for injection @@ -165,37 +165,32 @@ private void RegisterTinySystemGenerator(IncrementalGeneratorInitializationConte var fieldDecls = string.Join("\n", validParams.Select(p => $" private {p.Type.ToDisplayString()} _{p.Name};")); // Setup method: assign all fields - var setupAssignments = string.Join("\n", validParams.Select(p => $" _{p.Name} = ({p.Type.ToDisplayString()}){p.Type.ToDisplayString()}.Generate(world);")); - var setupCode = $@" public override void Setup(World world) + var setupAssignments = string.Join("\n", validParams.Select(p => $" _{p.Name} = builder.Add<{p.Type.ToDisplayString()}>();")); + var setupCode = $@" protected override void Setup(SystemParamBuilder builder) {{ {setupAssignments} }}"; - var paramNames = string.Join(", ", validParams.Select(p => $"_{p.Name}")); - - var paramLocks = string.Join("\n", validParams.Select(p => $" _{p.Name}.Lock(ticks);")); - var paramUnlocks = string.Join("\n", validParams.Select(p => $" _{p.Name}.Unlock();")); - var executeOverride = $@" public override void Execute(SystemTicks ticks, World world) + var onExecuteParams = string.Join(", ", validParams.Select(p => $"_{p.Name}")); + var executeOverride = $@" public override void Execute(World world) {{ -{paramLocks} + Lock(); world.BeginDeferred(); - Execute({paramNames}); + OnExecute({onExecuteParams}); world.EndDeferred(); -{paramUnlocks} + Unlock(); }}"; - // Only generate Setup and Execute(World world) + // Only generate Setup, Execute, and OnExecute var systemClass = classSymbol.ContainingNamespace.IsGlobalNamespace - ? $@" -using TinyEcs; + ? $@"using TinyEcs; public partial class {className} : TinySystem2 {{ {fieldDecls} {setupCode} {executeOverride} }}" - : $@" -using TinyEcs; + : $@"using TinyEcs; namespace {ns} {{ public partial class {className} : TinySystem2 From 165b4c7ff825a7e14590a2fa9c46381ad8fa68de Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Mon, 8 Sep 2025 10:22:48 +0200 Subject: [PATCH 16/33] + fix src gen --- tools/TinyEcs.SourceGenerator/Program.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 42877783..aff67c38 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -172,19 +172,20 @@ private void RegisterTinySystemGenerator(IncrementalGeneratorInitializationConte }}"; var onExecuteParams = string.Join(", ", validParams.Select(p => $"_{p.Name}")); - var executeOverride = $@" public override void Execute(World world) + var executeOverride = $@" protected override bool Execute(World world) {{ Lock(); world.BeginDeferred(); OnExecute({onExecuteParams}); world.EndDeferred(); Unlock(); + return true; }}"; // Only generate Setup, Execute, and OnExecute var systemClass = classSymbol.ContainingNamespace.IsGlobalNamespace ? $@"using TinyEcs; -public partial class {className} : TinySystem2 +public partial class {className} : TinySystem {{ {fieldDecls} {setupCode} @@ -193,7 +194,7 @@ public partial class {className} : TinySystem2 : $@"using TinyEcs; namespace {ns} {{ - public partial class {className} : TinySystem2 + public partial class {className} : TinySystem {{ {fieldDecls} {setupCode} From 836d9147a91c911bf78b76f24a78bd3af15399d4 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Mon, 8 Sep 2025 11:56:20 +0200 Subject: [PATCH 17/33] + gen --- .../TinySystemMethodGenerator.cs | 152 ++++++++++++++++++ 1 file changed, 152 insertions(+) create mode 100644 tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs diff --git a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs new file mode 100644 index 00000000..d9a1674f --- /dev/null +++ b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs @@ -0,0 +1,152 @@ +using System; +using System.Collections.Immutable; +using System.Linq; +using System.Text; +using Microsoft.CodeAnalysis; +using Microsoft.CodeAnalysis.CSharp; +using Microsoft.CodeAnalysis.CSharp.Syntax; + +#pragma warning disable IDE0055 +#pragma warning disable IDE0008 +#pragma warning disable IDE0058 + +namespace TinyEcs.SourceGenerator; + +[Generator] +public sealed class TinySystemMethodGenerator : IIncrementalGenerator +{ + public void Initialize(IncrementalGeneratorInitializationContext context) + { + // Find IPlugin classes that contain methods with [TinySystem] attributes + var pluginClassesWithTinySystemMethods = context.SyntaxProvider.CreateSyntaxProvider( + static (s, _) => s is ClassDeclarationSyntax classDecl && + classDecl.Members.OfType() + .Any(m => m.AttributeLists.Count > 0), + static (ctx, _) => + { + var classDecl = (ClassDeclarationSyntax)ctx.Node; + var classSymbol = ctx.SemanticModel.GetDeclaredSymbol(classDecl) as INamedTypeSymbol; + + if (classSymbol == null) return null; + + // Check if the class implements IPlugin + var implementsIPlugin = classSymbol.AllInterfaces.Any(i => + i.ToDisplayString() == "TinyEcs.IPlugin"); + + if (!implementsIPlugin) return null; + + // Find methods with [TinySystem] attribute + var tinySystemMethods = classSymbol.GetMembers() + .OfType() + .Where(m => m.GetAttributes().Any(attr => + attr.AttributeClass?.ToDisplayString() == "TinyEcs.TinySystemAttribute")) + .ToList(); + + if (!tinySystemMethods.Any()) return null; + + return new PluginClassInfo + { + ClassSymbol = classSymbol, + TinySystemMethods = tinySystemMethods + }; + } + ).Where(static info => info != null); + + // Generate adapter classes for each method + context.RegisterSourceOutput( + pluginClassesWithTinySystemMethods.Collect(), + (spc, pluginClasses) => + { + if (pluginClasses.IsDefaultOrEmpty) return; + + foreach (var pluginClass in pluginClasses) + { + foreach (var method in pluginClass.TinySystemMethods) + { + GenerateAdapterClass(spc, pluginClass.ClassSymbol, method); + } + } + } + ); + } + + private static void GenerateAdapterClass(SourceProductionContext context, INamedTypeSymbol pluginClass, IMethodSymbol method) + { + var adapterName = $"{method.Name}Adapter"; + var pluginClassName = pluginClass.ToDisplayString(); + var ns = pluginClass.ContainingNamespace.IsGlobalNamespace ? "" : pluginClass.ContainingNamespace.ToDisplayString(); + + // Get method parameters for dependency injection + var parameters = method.Parameters.ToList(); + var fieldDeclarations = new StringBuilder(); + var setupAssignments = new StringBuilder(); + var methodCallParameters = new StringBuilder(); + + // Generate field declarations and setup assignments + for (int i = 0; i < parameters.Count; i++) + { + var param = parameters[i]; + var fieldName = $"_{param.Name}"; + var paramType = param.Type.ToDisplayString(); + + fieldDeclarations.AppendLine($" private {paramType} {fieldName};"); + setupAssignments.AppendLine($" {fieldName} = builder.Add<{paramType}>();"); + + if (i > 0) methodCallParameters.Append(", "); + methodCallParameters.Append(fieldName); + } + + // Determine how to call the method (static vs instance) + string methodCall; + if (method.IsStatic) + { + methodCall = $"{pluginClassName}.{method.Name}({methodCallParameters})"; + } + else + { + // For instance methods, we need to create an instance of the plugin class + methodCall = $"new {pluginClassName}().{method.Name}({methodCallParameters})"; + } + + // Generate the adapter class + var adapterClass = new StringBuilder(); + + if (!string.IsNullOrEmpty(ns)) + { + adapterClass.AppendLine($"namespace {ns}"); + adapterClass.AppendLine("{"); + } + + adapterClass.AppendLine($@" public class {adapterName} : TinyEcs.TinySystem + {{ +{fieldDeclarations} + protected override void Setup(TinyEcs.SystemParamBuilder builder) + {{ +{setupAssignments} }} + + protected override bool Execute(TinyEcs.World world) + {{ + Lock(); + world.BeginDeferred(); + {methodCall}; + world.EndDeferred(); + Unlock(); + return true; + }} + }}"); + + if (!string.IsNullOrEmpty(ns)) + { + adapterClass.AppendLine("}"); + } + + var sourceText = adapterClass.ToString(); + context.AddSource($"{adapterName}.g.cs", sourceText); + } + + private class PluginClassInfo + { + public INamedTypeSymbol ClassSymbol { get; set; } + public List TinySystemMethods { get; set; } + } +} From 4e4768e06e6fff05b42c90e3053c194139d881a4 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Mon, 8 Sep 2025 16:06:38 +0200 Subject: [PATCH 18/33] + sourc gen --- .../TinySystemMethodGenerator.cs | 45 ++++++++++++++----- 1 file changed, 35 insertions(+), 10 deletions(-) diff --git a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs index d9a1674f..03ec9b8a 100644 --- a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs +++ b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs @@ -76,6 +76,10 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed var pluginClassName = pluginClass.ToDisplayString(); var ns = pluginClass.ContainingNamespace.IsGlobalNamespace ? "" : pluginClass.ContainingNamespace.ToDisplayString(); + // Check if method returns bool (for conditional systems) + var returnsBool = method.ReturnType.SpecialType == SpecialType.System_Boolean; + var conditionalInterface = returnsBool ? ", TinyEcs.ITinyConditionalSystem" : ""; + // Get method parameters for dependency injection var parameters = method.Parameters.ToList(); var fieldDeclarations = new StringBuilder(); @@ -108,6 +112,35 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed methodCall = $"new {pluginClassName}().{method.Name}({methodCallParameters})"; } + // Generate Execute method based on return type + string executeMethod; + if (returnsBool) + { + // For conditional systems, return the method's boolean result + executeMethod = $@" protected override bool Execute(TinyEcs.World world) + {{ + Lock(); + world.BeginDeferred(); + bool result = {methodCall}; + world.EndDeferred(); + Unlock(); + return result; + }}"; + } + else + { + // For regular systems, call method and return true + executeMethod = $@" protected override bool Execute(TinyEcs.World world) + {{ + Lock(); + world.BeginDeferred(); + {methodCall}; + world.EndDeferred(); + Unlock(); + return true; + }}"; + } + // Generate the adapter class var adapterClass = new StringBuilder(); @@ -117,22 +150,14 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed adapterClass.AppendLine("{"); } - adapterClass.AppendLine($@" public class {adapterName} : TinyEcs.TinySystem + adapterClass.AppendLine($@" public class {adapterName} : TinyEcs.TinySystem{conditionalInterface} {{ {fieldDeclarations} protected override void Setup(TinyEcs.SystemParamBuilder builder) {{ {setupAssignments} }} - protected override bool Execute(TinyEcs.World world) - {{ - Lock(); - world.BeginDeferred(); - {methodCall}; - world.EndDeferred(); - Unlock(); - return true; - }} +{executeMethod} }}"); if (!string.IsNullOrEmpty(ns)) From a9bbbe13872d5a25f86f7f15c5f3d9264230fef9 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Mon, 8 Sep 2025 16:06:53 +0200 Subject: [PATCH 19/33] IConditional --- src/Bevy.cs | 79 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 22 deletions(-) diff --git a/src/Bevy.cs b/src/Bevy.cs index 45953c23..5edd9a29 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -137,15 +137,12 @@ public bool ExecuteOnReady(World world, uint ticks) var config = Configuration; - if (config.BeforeSystems != null) + foreach (var beforeSys in config.BeforeSystems) { - foreach (var beforeSys in config.BeforeSystems) + if (!beforeSys.ExecuteOnReady(world, ticks)) { - if (!beforeSys.ExecuteOnReady(world, ticks)) - { - canRun = false; - break; - } + canRun = false; + break; } } @@ -160,9 +157,10 @@ public bool ExecuteOnReady(World world, uint ticks) } } - _fn(Ticks, _arg, _validator); + if (canRun) + canRun = _fn(Ticks, _arg, _validator); - if (config.AfterSystems != null) + if (canRun) { foreach (var afterSys in config.AfterSystems) { @@ -311,6 +309,7 @@ private void RunStage(Stages stage, uint ticks) internal void Add(ITinySystem sys, Stages stage) { sys.Configuration.Node = _systems[(int)stage].AddLast(sys); + sys.Configuration.Stage = stage; } public Scheduler AddSystem2(Stages stage) where T : ITinySystem, new() @@ -1459,6 +1458,12 @@ public sealed class TinyPluginAttribute : Attribute } +[AttributeUsage(AttributeTargets.Class)] +public sealed class TinyConditionalSystemAttribute : Attribute +{ +} + + public sealed class SystemParamBuilder(World world) { private readonly List _params = []; @@ -1476,7 +1481,7 @@ public ISystemParam[] Build() public sealed class SystemConfiguration { - public List Conditionals { get; } = []; + public HashSet Conditionals { get; } = []; public LinkedList BeforeSystems { get; set; } = new(); public LinkedList AfterSystems { get; set; } = new(); public LinkedListNode? Node { get; set; } @@ -1484,6 +1489,10 @@ public sealed class SystemConfiguration public Stages Stage { get; set; } = Stages.Update; } +public interface ITinyConditionalSystem : ITinySystem +{ +} + public interface ITinySystem { ISystemParam[] SystemParams { get; set; } @@ -1497,13 +1506,13 @@ public interface ITinySystem bool ParamsAreLocked(); } -public abstract class TinySystem2 : ITinySystem +public abstract class TinySystem : ITinySystem { public ISystemParam[] SystemParams { get; set; } = []; public SystemTicks Ticks { get; } = new(); - public SystemConfiguration Configuration { get; } = new(); + public void Initialize(World world) { var builder = new SystemParamBuilder(world); @@ -1516,9 +1525,7 @@ public bool ExecuteOnReady(World world, uint ticks) Ticks.ThisRun = ticks; var canRun = true; - var config = Configuration; - - foreach (var beforeSys in config.BeforeSystems) + foreach (var beforeSys in Configuration.BeforeSystems) { if (!beforeSys.ExecuteOnReady(world, ticks)) { @@ -1529,7 +1536,7 @@ public bool ExecuteOnReady(World world, uint ticks) if (canRun) { - foreach (var conditional in config.Conditionals) + foreach (var conditional in Configuration.Conditionals) { if (!conditional.ExecuteOnReady(world, ticks)) { @@ -1538,14 +1545,20 @@ public bool ExecuteOnReady(World world, uint ticks) } } - Execute(world); + if (canRun) + { + canRun = Execute(world); + } - foreach (var afterSys in config.AfterSystems) + if (canRun) { - if (!afterSys.ExecuteOnReady(world, ticks)) + foreach (var afterSys in Configuration.AfterSystems) { - canRun = false; - break; + if (!afterSys.ExecuteOnReady(world, ticks)) + { + canRun = false; + break; + } } } } @@ -1577,7 +1590,29 @@ protected void Unlock() } protected abstract void Setup(SystemParamBuilder builder); - public abstract void Execute(World world); + protected abstract bool Execute(World world); + + + public TinySystem RunIf(ITinyConditionalSystem sys) + { + // TODO: Check for duplicates? + _ = Configuration.Conditionals.Add(sys); + return this; + } + + public TinySystem RunAfter(ITinySystem sys) + { + Configuration.Node?.List?.Remove(Configuration.Node); + Configuration.Node = Configuration.AfterSystems.AddLast(sys); + return this; + } + + public TinySystem RunBefore(ITinySystem sys) + { + Configuration.Node?.List?.Remove(Configuration.Node); + Configuration.Node = Configuration.BeforeSystems.AddLast(sys); + return this; + } } #endif From 6debe49029c127061172b1c8ef5ca7b7c492da03 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Mon, 8 Sep 2025 18:18:27 +0200 Subject: [PATCH 20/33] + src gen again --- src/Bevy.cs | 20 +++++-- .../TinySystemMethodGenerator.cs | 53 +++++++++++++++++-- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/Bevy.cs b/src/Bevy.cs index 5edd9a29..86fc8112 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -404,7 +404,7 @@ public FuncSystem OnExit(TState st, Action system, ThreadingMode? public Scheduler AddPlugin() where T : IPlugin, new() => AddPlugin(new T()); - public Scheduler AddPlugin(T plugin) where T : IPlugin + public Scheduler AddPlugin(IPlugin plugin) { plugin.Build(this); @@ -1518,6 +1518,16 @@ public void Initialize(World world) var builder = new SystemParamBuilder(world); Setup(builder); SystemParams = builder.Build(); + + foreach (var conditional in Configuration.Conditionals) + { + conditional.Initialize(world); + } + + foreach (var afterSys in Configuration.AfterSystems) + { + afterSys.Initialize(world); + } } public bool ExecuteOnReady(World world, uint ticks) @@ -1593,10 +1603,14 @@ protected void Unlock() protected abstract bool Execute(World world); - public TinySystem RunIf(ITinyConditionalSystem sys) + public TinySystem RunIf() where T : ITinyConditionalSystem, new() + => RunIf(new T()); + + public TinySystem RunIf(params ITinyConditionalSystem[] conditionals) { // TODO: Check for duplicates? - _ = Configuration.Conditionals.Add(sys); + foreach (var sys in conditionals) + _ = Configuration.Conditionals.Add(sys); return this; } diff --git a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs index 03ec9b8a..bb361c39 100644 --- a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs +++ b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs @@ -76,6 +76,9 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed var pluginClassName = pluginClass.ToDisplayString(); var ns = pluginClass.ContainingNamespace.IsGlobalNamespace ? "" : pluginClass.ContainingNamespace.ToDisplayString(); + // Validate method requirements for TinySystemAttribute + ValidateMethodRequirements(context, method); + // Check if method returns bool (for conditional systems) var returnsBool = method.ReturnType.SpecialType == SpecialType.System_Boolean; var conditionalInterface = returnsBool ? ", TinyEcs.ITinyConditionalSystem" : ""; @@ -100,16 +103,21 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed methodCallParameters.Append(fieldName); } - // Determine how to call the method (static vs instance) + // For non-static methods, add a field for the plugin instance + string instanceField = ""; + string ctor = ""; string methodCall; + if (method.IsStatic) { methodCall = $"{pluginClassName}.{method.Name}({methodCallParameters})"; } else { - // For instance methods, we need to create an instance of the plugin class - methodCall = $"new {pluginClassName}().{method.Name}({methodCallParameters})"; + // Adapter will accept the instance via constructor + instanceField = $" private readonly {pluginClassName} _instance;"; + ctor = $" public {adapterName}({pluginClassName} instance) {{ _instance = instance; }}"; + methodCall = $"_instance.{method.Name}({methodCallParameters})"; } // Generate Execute method based on return type @@ -150,9 +158,11 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed adapterClass.AppendLine("{"); } - adapterClass.AppendLine($@" public class {adapterName} : TinyEcs.TinySystem{conditionalInterface} + // Make the adapter class sealed and partial + adapterClass.AppendLine($@" public sealed partial class {adapterName} : TinyEcs.TinySystem{conditionalInterface} {{ -{fieldDeclarations} +{fieldDeclarations}{instanceField} +{ctor} protected override void Setup(TinyEcs.SystemParamBuilder builder) {{ {setupAssignments} }} @@ -169,6 +179,39 @@ protected override void Setup(TinyEcs.SystemParamBuilder builder) context.AddSource($"{adapterName}.g.cs", sourceText); } + private static void ValidateMethodRequirements(SourceProductionContext context, IMethodSymbol method) + { + var location = method.Locations.FirstOrDefault() ?? Location.None; + + // Check if method is public + if (method.DeclaredAccessibility != Accessibility.Public) + { + var descriptor = new DiagnosticDescriptor( + "TINYECS001", + "TinySystem method must be public", + "Method '{0}' with [TinySystem] attribute must be public", + "TinyEcs", + DiagnosticSeverity.Error, + isEnabledByDefault: true); + + context.ReportDiagnostic(Diagnostic.Create(descriptor, location, method.Name)); + } + + // Check if method is static + // if (!method.IsStatic) + // { + // var descriptor = new DiagnosticDescriptor( + // "TINYECS002", + // "TinySystem method must be static", + // "Method '{0}' with [TinySystem] attribute must be static", + // "TinyEcs", + // DiagnosticSeverity.Error, + // isEnabledByDefault: true); + // + // context.ReportDiagnostic(Diagnostic.Create(descriptor, location, method.Name)); + // } + } + private class PluginClassInfo { public INamedTypeSymbol ClassSymbol { get; set; } From 0c08186083b60cdfddc77850fe1b7e57b835c13e Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Mon, 8 Sep 2025 18:25:12 +0200 Subject: [PATCH 21/33] + IndentedStringBuilder.cs --- .../IndentedStringBuilder.cs | 460 ++++++++++++++++++ .../TinySystemMethodGenerator.cs | 96 ++-- 2 files changed, 511 insertions(+), 45 deletions(-) create mode 100644 tools/TinyEcs.SourceGenerator/IndentedStringBuilder.cs diff --git a/tools/TinyEcs.SourceGenerator/IndentedStringBuilder.cs b/tools/TinyEcs.SourceGenerator/IndentedStringBuilder.cs new file mode 100644 index 00000000..566c2edc --- /dev/null +++ b/tools/TinyEcs.SourceGenerator/IndentedStringBuilder.cs @@ -0,0 +1,460 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.Text; + +namespace TinyEcs.SourceGenerator; + +public class IndentedStringBuilder +{ + private const byte IndentSize = 4; + private int _indent; + private bool _indentPending = true; + + private readonly StringBuilder _stringBuilder = new(); + + public StringBuilder Inner => _stringBuilder; + + /// + /// Gets the current indent level. + /// + /// The current indent level. + public virtual int IndentCount + { + get => _indent; + set => _indent = value < 0 ? 0 : value; + } + + /// + /// The current length of the built string. + /// + public virtual int Length + { + get => _stringBuilder.Length; + set => _stringBuilder.Length = value; + } + + /// + /// Appends the current indent and then the given string to the string being built. + /// + /// The string to append. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder Append(string value) + { + DoIndent(); + + _stringBuilder.Append(value); + + return this; + } + +#if NET + public virtual IndentedStringBuilder Append(ReadOnlySpan value) + { + DoIndent(); + + _stringBuilder.Append(value); + + return this; + } +#endif + + /// + /// Appends the current indent and then the given string to the string being built. + /// + /// The string to append. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder Append(object value) + { + DoIndent(); + + _stringBuilder.Append(value); + + return this; + } + + /// + /// Appends the current indent and then the given string to the string being built. + /// + /// The string to append. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder Append(FormattableString value) + { + DoIndent(); + + _stringBuilder.Append(value); + + return this; + } + + /// + /// Appends the current indent and then the given char to the string being built. + /// + /// The char to append. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder Append(char value) + { + DoIndent(); + + _stringBuilder.Append(value); + + return this; + } + + /// + /// Appends the current indent and then the given char to the string being built. + /// + /// The char to append. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder Append(int value) + { + DoIndent(); + + _stringBuilder.Append(value); + + return this; + } + + /// + /// Appends the current indent and then the given strings to the string being built. + /// + /// The strings to append. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder Append(IEnumerable value) + { + DoIndent(); + + foreach (var str in value) + { + _stringBuilder.Append(str); + } + + return this; + } + + /// + /// Appends the current indent and then the given chars to the string being built. + /// + /// The chars to append. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder Append(IEnumerable value) + { + DoIndent(); + + foreach (var chr in value) + { + _stringBuilder.Append(chr); + } + + return this; + } + + /// + /// Appends a new line to the string being built. + /// + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder AppendLine() + { + AppendLine(string.Empty); + + return this; + } + + /// + /// Appends the current indent, the given string, and a new line to the string being built. + /// + /// + /// If the given string itself contains a new line, the part of the string after that new line will not be indented. + /// + /// The string to append. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder AppendLine(string value) + { + if (value.Length != 0) + { + DoIndent(); + } + + _stringBuilder.AppendLine(value); + + _indentPending = true; + + return this; + } + + /// + /// Appends the current indent, the given string, and a new line to the string being built. + /// + /// The string to append. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder AppendLine(FormattableString value) + { + DoIndent(); + + _stringBuilder.Append(value); + + _indentPending = true; + + return this; + } + + /// + /// Separates the given string into lines, and then appends each line, prefixed + /// by the current indent and followed by a new line, to the string being built. + /// + /// The string to append. + /// If , then the terminating new line is not added after the last line. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder AppendLines(string value, bool skipFinalNewline = false) + { + using (var reader = new StringReader(value)) + { + var first = true; + string? line; + while ((line = reader.ReadLine()) != null) + { + if (first) + { + first = false; + } + else + { + AppendLine(); + } + + if (line.Length != 0) + { + Append(line); + } + } + } + + if (!skipFinalNewline) + { + AppendLine(); + } + + return this; + } + + /// + /// Concatenates the members of the given collection, using the specified separator between each member, + /// and then appends the resulting string, + /// + /// The values to concatenate. + /// The separator. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder AppendJoin( + IEnumerable values, + string separator = ", ") + { + DoIndent(); + + using var enumerator = values.GetEnumerator(); + + if (!enumerator.MoveNext()) + { + return this; + } + + _stringBuilder.Append(enumerator.Current); + + while (enumerator.MoveNext()) + { + _stringBuilder.Append(separator); + _stringBuilder.Append(enumerator.Current); + } + + return this; + } + + /// + /// Concatenates the members of the given collection, using the specified separator between each member, + /// and then appends the resulting string, + /// + /// The values to concatenate. + /// The separator. + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder AppendJoin( + string separator, + params string[] values) + { + DoIndent(); + + for (var i = 0; i < values.Length; i++) + { + if (i > 0) + { + _stringBuilder.Append(separator); + } + + _stringBuilder.Append(values[i]); + } + + return this; + } + + /// + /// Resets this builder ready to build a new string. + /// + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder Clear() + { + _stringBuilder.Clear(); + _indent = 0; + + return this; + } + + /// + /// Increments the indent. + /// + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder IncrementIndent() + { + _indent++; + + return this; + } + + /// + /// Decrements the indent. + /// + /// This builder so that additional calls can be chained. + public virtual IndentedStringBuilder DecrementIndent() + { + if (_indent > 0) + { + _indent--; + } + + return this; + } + + /// + /// Creates a scoped indenter that will increment the indent, then decrement it when disposed. + /// + /// An indenter. + public virtual IDisposable Block() + { + AppendLine("{"); + return new Indenter(this, "}"); + } + + public virtual IDisposable Block(string startText, string endText) + { + AppendLine(startText); + return new Indenter(this, endText); + } + + public virtual IDisposable Block(string endText) + { + return new Indenter(this, endText); + } + + /// + /// Creates a scoped indenter that will increment the indent, then decrement it when disposed. + /// + /// An indenter. + public virtual IDisposable Indent() + => new Indenter(this); + + /// + /// Temporarily disables all indentation. Restores the original indentation when the returned object is disposed. + /// + /// An object that restores the original indentation when disposed. + public virtual IDisposable SuspendIndent() + => new IndentSuspender(this); + + /// + /// Clones this , copying the built string and current indent level. + /// + /// New instance of . + public virtual IndentedStringBuilder Clone() + { + var result = new IndentedStringBuilder(); + result._stringBuilder.Append(_stringBuilder); + result._indent = _indent; + result._indentPending = _indentPending; + return result; + } + + /// + /// Returns the built string. + /// + /// The built string. + public override string ToString() + => _stringBuilder.ToString(); + + private void DoIndent() + { + if (_indentPending && _indent > 0) + { + _stringBuilder.Append(' ', _indent * IndentSize); + } + + _indentPending = false; + } + + public Resetter CreateResetter() => new(this); + + private sealed class Indenter : IDisposable + { + private readonly IndentedStringBuilder _stringBuilder; + private readonly string? _closeText; + + public Indenter(IndentedStringBuilder stringBuilder, string? closeText = null) + { + _stringBuilder = stringBuilder; + _closeText = closeText; + + _stringBuilder.IncrementIndent(); + } + + public void Dispose() + { + _stringBuilder.DecrementIndent(); + + if (_closeText is not null) + { + _stringBuilder.AppendLine(_closeText); + } + } + } + + private sealed class IndentSuspender : IDisposable + { + private readonly IndentedStringBuilder _stringBuilder; + private readonly int _indent; + + public IndentSuspender(IndentedStringBuilder stringBuilder) + { + _stringBuilder = stringBuilder; + _indent = _stringBuilder._indent; + _stringBuilder._indent = 0; + } + + public void Dispose() + => _stringBuilder._indent = _indent; + } + + public struct Resetter + { + private readonly IndentedStringBuilder _builder; + private readonly int _length; + private readonly int _indent; + + public Resetter(IndentedStringBuilder builder) + { + _builder = builder; + _length = builder.Length; + _indent = builder.IndentCount; + } + + public void Reset() + { + _builder.Length = _length; + _builder.IndentCount = _indent; + } + } +} diff --git a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs index bb361c39..a06ffb66 100644 --- a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs +++ b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs @@ -108,70 +108,76 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed string ctor = ""; string methodCall; + // Use a single IndentedStringBuilder for all code blocks + var adapterClass = new IndentedStringBuilder(); + + if (!string.IsNullOrEmpty(ns)) + { + adapterClass.AppendLine($"namespace {ns}"); + adapterClass.AppendLine("{"); + adapterClass.IncrementIndent(); + } + + // Make the adapter class sealed and partial + adapterClass.AppendLine($"public sealed partial class {adapterName} : TinyEcs.TinySystem{conditionalInterface}"); + adapterClass.AppendLine("{"); + adapterClass.IncrementIndent(); + + // Fields + foreach (var line in fieldDeclarations.ToString().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)) + adapterClass.AppendLine(line); + if (method.IsStatic) { methodCall = $"{pluginClassName}.{method.Name}({methodCallParameters})"; } else { - // Adapter will accept the instance via constructor - instanceField = $" private readonly {pluginClassName} _instance;"; - ctor = $" public {adapterName}({pluginClassName} instance) {{ _instance = instance; }}"; + instanceField = $"private readonly {pluginClassName} _instance;"; + ctor = $"public {adapterName}({pluginClassName} instance) {{ _instance = instance; }}"; methodCall = $"_instance.{method.Name}({methodCallParameters})"; + adapterClass.AppendLine(instanceField); + adapterClass.AppendLine(ctor); } - // Generate Execute method based on return type - string executeMethod; + // Setup method + adapterClass.AppendLine("protected override void Setup(TinyEcs.SystemParamBuilder builder)"); + adapterClass.AppendLine("{"); + adapterClass.IncrementIndent(); + foreach (var line in setupAssignments.ToString().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)) + adapterClass.AppendLine(line); + adapterClass.DecrementIndent(); + adapterClass.AppendLine("}"); + + // Execute method + adapterClass.AppendLine("protected override bool Execute(TinyEcs.World world)"); + adapterClass.AppendLine("{"); + adapterClass.IncrementIndent(); + adapterClass.AppendLine("Lock();"); + adapterClass.AppendLine("world.BeginDeferred();"); if (returnsBool) { - // For conditional systems, return the method's boolean result - executeMethod = $@" protected override bool Execute(TinyEcs.World world) - {{ - Lock(); - world.BeginDeferred(); - bool result = {methodCall}; - world.EndDeferred(); - Unlock(); - return result; - }}"; + adapterClass.AppendLine($"bool result = {methodCall};"); + adapterClass.AppendLine("world.EndDeferred();"); + adapterClass.AppendLine("Unlock();"); + adapterClass.AppendLine("return result;"); } else { - // For regular systems, call method and return true - executeMethod = $@" protected override bool Execute(TinyEcs.World world) - {{ - Lock(); - world.BeginDeferred(); - {methodCall}; - world.EndDeferred(); - Unlock(); - return true; - }}"; + adapterClass.AppendLine($"{methodCall};"); + adapterClass.AppendLine("world.EndDeferred();"); + adapterClass.AppendLine("Unlock();"); + adapterClass.AppendLine("return true;"); } + adapterClass.DecrementIndent(); + adapterClass.AppendLine("}"); - // Generate the adapter class - var adapterClass = new StringBuilder(); - - if (!string.IsNullOrEmpty(ns)) - { - adapterClass.AppendLine($"namespace {ns}"); - adapterClass.AppendLine("{"); - } - - // Make the adapter class sealed and partial - adapterClass.AppendLine($@" public sealed partial class {adapterName} : TinyEcs.TinySystem{conditionalInterface} - {{ -{fieldDeclarations}{instanceField} -{ctor} - protected override void Setup(TinyEcs.SystemParamBuilder builder) - {{ -{setupAssignments} }} - -{executeMethod} - }}"); + adapterClass.DecrementIndent(); + adapterClass.AppendLine("}"); if (!string.IsNullOrEmpty(ns)) { + adapterClass.DecrementIndent(); adapterClass.AppendLine("}"); } From 65b5c8d3ca0b807acd85990b015befd0a5d33169 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Mon, 8 Sep 2025 18:39:22 +0200 Subject: [PATCH 22/33] again src gen --- .../TinySystemMethodGenerator.cs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs index a06ffb66..aafe5819 100644 --- a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs +++ b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs @@ -73,9 +73,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context) private static void GenerateAdapterClass(SourceProductionContext context, INamedTypeSymbol pluginClass, IMethodSymbol method) { var adapterName = $"{method.Name}Adapter"; - var pluginClassName = pluginClass.ToDisplayString(); var ns = pluginClass.ContainingNamespace.IsGlobalNamespace ? "" : pluginClass.ContainingNamespace.ToDisplayString(); + // Use fully-qualified type name (global::Namespace.Type) for instance/static references + var instanceTypeName = pluginClass.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); + // Validate method requirements for TinySystemAttribute ValidateMethodRequirements(context, method); @@ -129,17 +131,22 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed if (method.IsStatic) { - methodCall = $"{pluginClassName}.{method.Name}({methodCallParameters})"; + // Use namespace-qualified type for static call + methodCall = $"{instanceTypeName}.{method.Name}({methodCallParameters})"; } else { - instanceField = $"private readonly {pluginClassName} _instance;"; - ctor = $"public {adapterName}({pluginClassName} instance) {{ _instance = instance; }}"; + // Adapter will accept the instance via constructor using namespace-qualified type + instanceField = $"private readonly {instanceTypeName} _instance;"; + ctor = $"public {adapterName}({instanceTypeName} instance) {{ _instance = instance; }}"; methodCall = $"_instance.{method.Name}({methodCallParameters})"; adapterClass.AppendLine(instanceField); + adapterClass.AppendLine(); adapterClass.AppendLine(ctor); } + adapterClass.AppendLine(); + // Setup method adapterClass.AppendLine("protected override void Setup(TinyEcs.SystemParamBuilder builder)"); adapterClass.AppendLine("{"); @@ -149,6 +156,8 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed adapterClass.DecrementIndent(); adapterClass.AppendLine("}"); + adapterClass.AppendLine(); + // Execute method adapterClass.AppendLine("protected override bool Execute(TinyEcs.World world)"); adapterClass.AppendLine("{"); From c33401498b9acf0af89b2072be649bd6d307b657 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Wed, 10 Sep 2025 20:00:18 +0200 Subject: [PATCH 23/33] + diff between TinySystem and TinyConditionalSystem --- src/Bevy.cs | 195 ++++++++++++------ .../TinySystemMethodGenerator.cs | 4 +- 2 files changed, 129 insertions(+), 70 deletions(-) diff --git a/src/Bevy.cs b/src/Bevy.cs index 86fc8112..63bf6b3d 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -122,29 +122,47 @@ private bool ValidateConditions(SystemTicks ticks, TArg args) } public ISystemParam[] SystemParams { get; set; } - public SystemTicks Ticks { get; } - public SystemConfiguration Configuration { get; } + public SystemTicks Ticks { get; } = new(); + public SystemConfiguration Configuration { get; } = new(); + public SystemOrderConfiguration OrderConfiguration { get; } = new(); public void Initialize(World world) { } - public bool ExecuteOnReady(World world, uint ticks) + public bool BeforeExecute(World world) { - Ticks.ThisRun = ticks; - var canRun = true; - - var config = Configuration; - - foreach (var beforeSys in config.BeforeSystems) + var ticks = Ticks.ThisRun; + foreach (var beforeSys in OrderConfiguration.BeforeSystems) { if (!beforeSys.ExecuteOnReady(world, ticks)) { - canRun = false; - break; + return false; + } + } + return true; + } + + public bool AfterExecute(World world) + { + var ticks = Ticks.ThisRun; + foreach (var afterSys in OrderConfiguration.AfterSystems) + { + if (!afterSys.ExecuteOnReady(world, ticks)) + { + return false; } } + return true; + } + + public bool ExecuteOnReady(World world, uint ticks) + { + Ticks.ThisRun = ticks; + var config = Configuration; + + var canRun = BeforeExecute(world); if (canRun) { @@ -162,14 +180,7 @@ public bool ExecuteOnReady(World world, uint ticks) if (canRun) { - foreach (var afterSys in config.AfterSystems) - { - if (!afterSys.ExecuteOnReady(world, ticks)) - { - canRun = false; - break; - } - } + canRun = AfterExecute(world); } } @@ -306,10 +317,11 @@ private void RunStage(Stages stage, uint ticks) system.ExecuteOnReady(World, ticks); } - internal void Add(ITinySystem sys, Stages stage) + private void Add(ITinySystem sys, Stages stage) { - sys.Configuration.Node = _systems[(int)stage].AddLast(sys); + sys.OrderConfiguration.Node = _systems[(int)stage].AddLast(sys); sys.Configuration.Stage = stage; + sys.Configuration.ThreadingMode ??= ThreadingExecutionMode; } public Scheduler AddSystem2(Stages stage) where T : ITinySystem, new() @@ -319,12 +331,6 @@ internal void Add(ITinySystem sys, Stages stage) return this; } - public Scheduler AddSystem2(Stages stage, ITinySystem system) - { - Add(system, stage); - return this; - } - public Scheduler AddSystems2(Stages stage, params ITinySystem[] systems) { foreach (var system in systems) @@ -1388,7 +1394,11 @@ internal QueryIter(uint lastRun, uint thisRun, QueryIterator iterator) } [UnscopedRef] - public ref D Current => ref _dataIterator; + public ref D Current + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get => ref _dataIterator; + } [MethodImpl(MethodImplOptions.AggressiveInlining)] public bool MoveNext() @@ -1476,45 +1486,63 @@ public T Add() where T : ISystemParam, IIntoSystemParam } public ISystemParam[] Build() - => _params.ToArray(); + => [.. _params]; } public sealed class SystemConfiguration { public HashSet Conditionals { get; } = []; - public LinkedList BeforeSystems { get; set; } = new(); - public LinkedList AfterSystems { get; set; } = new(); - public LinkedListNode? Node { get; set; } - public ThreadingMode ThreadingMode { get; set; } = ThreadingMode.Auto; + public ThreadingMode? ThreadingMode { get; set; } public Stages Stage { get; set; } = Stages.Update; } -public interface ITinyConditionalSystem : ITinySystem +public sealed class SystemOrderConfiguration { + public LinkedList BeforeSystems { get; set; } = new(); + public LinkedList AfterSystems { get; set; } = new(); + public LinkedListNode? Node { get; set; } } -public interface ITinySystem + +public interface ITinyMeta { ISystemParam[] SystemParams { get; set; } SystemTicks Ticks { get; } SystemConfiguration Configuration { get; } - void Initialize(World world); + bool ParamsAreLocked(); + void Initialize(World world); bool ExecuteOnReady(World world, uint ticks); - bool ParamsAreLocked(); + bool BeforeExecute(World world); + bool AfterExecute(World world); } -public abstract class TinySystem : ITinySystem +public interface ITinySystem : ITinyMeta { + SystemOrderConfiguration OrderConfiguration { get; } +} + +public interface ITinyConditionalSystem : ITinyMeta +{ +} + +public abstract class TinySystemBase : ITinyMeta +{ + private bool _initialized; + public ISystemParam[] SystemParams { get; set; } = []; public SystemTicks Ticks { get; } = new(); public SystemConfiguration Configuration { get; } = new(); - - public void Initialize(World world) + public virtual void Initialize(World world) { + if (_initialized) + throw new Exception("Already initialized"); + + _initialized = true; + var builder = new SystemParamBuilder(world); Setup(builder); SystemParams = builder.Build(); @@ -1523,26 +1551,18 @@ public void Initialize(World world) { conditional.Initialize(world); } - - foreach (var afterSys in Configuration.AfterSystems) - { - afterSys.Initialize(world); - } } + public virtual bool BeforeExecute(World world) + => true; + + public virtual bool AfterExecute(World world) + => true; + public bool ExecuteOnReady(World world, uint ticks) { Ticks.ThisRun = ticks; - var canRun = true; - - foreach (var beforeSys in Configuration.BeforeSystems) - { - if (!beforeSys.ExecuteOnReady(world, ticks)) - { - canRun = false; - break; - } - } + var canRun = BeforeExecute(world); if (canRun) { @@ -1562,14 +1582,7 @@ public bool ExecuteOnReady(World world, uint ticks) if (canRun) { - foreach (var afterSys in Configuration.AfterSystems) - { - if (!afterSys.ExecuteOnReady(world, ticks)) - { - canRun = false; - break; - } - } + canRun = AfterExecute(world); } } @@ -1583,7 +1596,7 @@ public bool ParamsAreLocked() return Configuration.ThreadingMode switch { ThreadingMode.Single => true, ThreadingMode.Multi => false, - _ => SystemParams.All(static p => p.UseIndex > 0) + _ => SystemParams.Any(static p => p.UseIndex > 0) }; } @@ -1601,6 +1614,48 @@ protected void Unlock() protected abstract void Setup(SystemParamBuilder builder); protected abstract bool Execute(World world); +} + +public abstract class TinySystem : TinySystemBase, ITinySystem +{ + public SystemOrderConfiguration OrderConfiguration { get; } = new(); + + public override void Initialize(World world) + { + base.Initialize(world); + + foreach (var afterSys in OrderConfiguration.AfterSystems) + { + afterSys.Initialize(world); + } + } + + public override bool BeforeExecute(World world) + { + var ticks = Ticks.ThisRun; + foreach (var beforeSys in OrderConfiguration.BeforeSystems) + { + if (!beforeSys.ExecuteOnReady(world, ticks)) + { + return false; + } + } + return true; + } + + public override bool AfterExecute(World world) + { + var ticks = Ticks.ThisRun; + foreach (var afterSys in OrderConfiguration.AfterSystems) + { + if (!afterSys.ExecuteOnReady(world, ticks)) + { + return false; + } + } + + return true; + } public TinySystem RunIf() where T : ITinyConditionalSystem, new() @@ -1616,17 +1671,21 @@ public TinySystem RunIf(params ITinyConditionalSystem[] conditionals) public TinySystem RunAfter(ITinySystem sys) { - Configuration.Node?.List?.Remove(Configuration.Node); - Configuration.Node = Configuration.AfterSystems.AddLast(sys); + OrderConfiguration.Node?.List?.Remove(OrderConfiguration.Node); + OrderConfiguration.Node = OrderConfiguration.AfterSystems.AddLast(sys); return this; } public TinySystem RunBefore(ITinySystem sys) { - Configuration.Node?.List?.Remove(Configuration.Node); - Configuration.Node = Configuration.BeforeSystems.AddLast(sys); + OrderConfiguration.Node?.List?.Remove(OrderConfiguration.Node); + OrderConfiguration.Node = OrderConfiguration.BeforeSystems.AddLast(sys); return this; } } +public abstract class TinyConditionalSystem : TinySystemBase, ITinyConditionalSystem +{ +} + #endif diff --git a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs index aafe5819..2328070b 100644 --- a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs +++ b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs @@ -83,7 +83,7 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed // Check if method returns bool (for conditional systems) var returnsBool = method.ReturnType.SpecialType == SpecialType.System_Boolean; - var conditionalInterface = returnsBool ? ", TinyEcs.ITinyConditionalSystem" : ""; + var baseClass = returnsBool ? "TinyEcs.TinyConditionalSystem" : "TinyEcs.TinySystem"; // Get method parameters for dependency injection var parameters = method.Parameters.ToList(); @@ -121,7 +121,7 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed } // Make the adapter class sealed and partial - adapterClass.AppendLine($"public sealed partial class {adapterName} : TinyEcs.TinySystem{conditionalInterface}"); + adapterClass.AppendLine($"public sealed partial class {adapterName} : {baseClass}"); adapterClass.AppendLine("{"); adapterClass.IncrementIndent(); From 9bc125cdc94fe6629366b315978bf88ee8e07eac Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Wed, 10 Sep 2025 23:58:13 +0200 Subject: [PATCH 24/33] + move to ITinySystem --- src/Bevy.cs | 215 +++++++------ src/TinyEcs.Systems.Interfaces.g.cs | 340 ++++++++++---------- src/TinyEcs.Systems.Interfaces.tt | 8 +- src/TinyEcs.Systems.Scheduler.g.cs | 66 ++-- src/TinyEcs.Systems.Scheduler.tt | 4 +- src/TinyEcs.Systems.StageSpecific.g.cs | 410 ++++++++++++------------- src/TinyEcs.Systems.StageSpecific.tt | 8 +- src/TinyEcs.Systems.StateHandlers.g.cs | 130 ++++---- src/TinyEcs.Systems.StateHandlers.tt | 8 +- 9 files changed, 603 insertions(+), 586 deletions(-) diff --git a/src/Bevy.cs b/src/Bevy.cs index 63bf6b3d..689892fa 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -6,106 +6,103 @@ namespace TinyEcs; #if NET9_0_OR_GREATER -public sealed partial class FuncSystem : ITinySystem - where TArg : notnull +public sealed partial class FuncSystem : ITinySystem { - private readonly TArg _arg; - private readonly Func, bool> _fn; - private readonly List> _conditions; - private readonly Func _validator; + private readonly World _arg; + private readonly Func, bool> _fn; + private readonly List> _conditions; + private readonly Func _validator; private readonly Func _checkInUse; - private readonly Stages _stage; - private readonly ThreadingMode _threadingType; + + public ISystemParam[] SystemParams { get; set; } = []; + public SystemTicks Ticks { get; } = new(); + public SystemConfiguration Configuration { get; } = new(); + public SystemOrderConfiguration OrderConfiguration { get; } = new(); - internal FuncSystem(TArg arg, Func, bool> fn, Func checkInUse, Stages stage, ThreadingMode threadingType) + internal FuncSystem(World arg, Func, bool> fn, Func checkInUse, Stages stage, ThreadingMode threadingType) { _arg = arg; _fn = fn; _conditions = new(); _validator = ValidateConditions; _checkInUse = checkInUse; - _threadingType = threadingType; - _stage = stage; - } - - // internal void Run(uint ticks) - // { - // Ticks.ThisRun = ticks; - // - // foreach (var s in _before) - // s.Run(ticks); - // - // if (_fn(Ticks, _arg, _validator)) - // { - // foreach (var s in _after) - // s.Run(ticks); - // } - // - // Ticks.LastRun = Ticks.ThisRun; - // } - - public FuncSystem RunIf(Func condition) - { - _conditions.Add((_, _) => condition()); + Configuration.ThreadingMode = threadingType; + Configuration.Stage = stage; + } + + + public ITinySystem RunIf() where T : ITinyConditionalSystem, new() + { + var conditional = new T(); + _conditions.Add((ticks, args) => conditional.ExecuteOnReady(args, ticks.ThisRun)); + + return this; + } + + public ITinySystem RunIf(params ITinyConditionalSystem[] conditionals) + { + foreach (var conditional in conditionals) + _conditions.Add((ticks, args) => conditional.ExecuteOnReady(args, ticks.ThisRun)); + return this; } - // public FuncSystem RunAfter(ITinySystem parent) - // { - // if (this == parent || Contains(parent, s => s.Configuration.AfterSystems)) - // throw new InvalidOperationException("Circular dependency detected"); - // - // Configuration.Node?.List?.Remove(Configuration.Node); - // Configuration.Node = parent.Configuration.AfterSystems.AddLast(this); - // - // return this; - // } - // - // public FuncSystem RunAfter(params ReadOnlySpan systems) - // { - // foreach (var system in systems) - // system.RunAfter(this); - // - // return this; - // } - // - // public FuncSystem RunBefore(ITinySystem parent) - // { - // if (this == parent || Contains(parent, s => s.Configuration.BeforeSystems)) - // throw new InvalidOperationException("Circular dependency detected"); - // - // Configuration.Node?.List?.Remove(Configuration.Node); - // Configuration.Node = parent.Configuration.BeforeSystems.AddLast(this); - // - // return this; - // } - // - // public FuncSystem RunBefore(params ReadOnlySpan systems) - // { - // foreach (var system in systems) - // system.RunBefore(this); - // - // return this; - // } - // - // private bool Contains(ITinySystem system, Func?> direction) - // { - // ITinySystem? current = this; - // while (current != null) - // { - // if (current == system) - // return true; - // - // var nextNode = direction(current)?.First; - // current = nextNode?.Value; - // } - // return false; - // } + public ITinySystem RunAfter(ITinySystem parent) + { + if (this == parent || Contains(parent, s => s.OrderConfiguration.AfterSystems)) + throw new InvalidOperationException("Circular dependency detected"); + + OrderConfiguration.Node?.List?.Remove(OrderConfiguration.Node); + OrderConfiguration.Node = parent.OrderConfiguration.AfterSystems.AddLast(this); + + return this; + } + + public ITinySystem RunAfter(params ReadOnlySpan systems) + { + foreach (var system in systems) + system.RunAfter(this); + + return this; + } + + public ITinySystem RunBefore(ITinySystem parent) + { + if (this == parent || Contains(parent, s => s.OrderConfiguration.BeforeSystems)) + throw new InvalidOperationException("Circular dependency detected"); + + OrderConfiguration.Node?.List?.Remove(OrderConfiguration.Node); + OrderConfiguration.Node = parent.OrderConfiguration.BeforeSystems.AddLast(this); + + return this; + } + + public ITinySystem RunBefore(params ReadOnlySpan systems) + { + foreach (var system in systems) + system.RunBefore(this); + + return this; + } + + private bool Contains(ITinySystem system, Func?> direction) + { + ITinySystem? current = this; + while (current != null) + { + if (current == system) + return true; + + var nextNode = direction(current)?.First; + current = nextNode?.Value; + } + return false; + } internal bool IsResourceInUse() { - return _threadingType switch + return Configuration.ThreadingMode switch { ThreadingMode.Multi => false, ThreadingMode.Single => true, @@ -113,7 +110,7 @@ internal bool IsResourceInUse() }; } - private bool ValidateConditions(SystemTicks ticks, TArg args) + private bool ValidateConditions(SystemTicks ticks, World args) { foreach (var fn in _conditions) if (!fn(ticks, args)) @@ -121,10 +118,6 @@ private bool ValidateConditions(SystemTicks ticks, TArg args) return true; } - public ISystemParam[] SystemParams { get; set; } - public SystemTicks Ticks { get; } = new(); - public SystemConfiguration Configuration { get; } = new(); - public SystemOrderConfiguration OrderConfiguration { get; } = new(); public void Initialize(World world) { @@ -340,12 +333,12 @@ public Scheduler AddSystems2(Stages stage, params ITinySystem[] systems) return this; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) { if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new FuncSystem(_world, (ticks, args, runIf) => { if (runIf?.Invoke(ticks, args) ?? true) { @@ -359,7 +352,7 @@ public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum { if (!threadingType.HasValue) @@ -367,7 +360,7 @@ public FuncSystem OnEnter(TState st, Action system, ThreadingMode var stateChangeId = -1; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new FuncSystem(_world, (ticks, args, runIf) => { if (runIf?.Invoke(ticks, args) ?? true) { @@ -383,7 +376,7 @@ public FuncSystem OnEnter(TState st, Action system, ThreadingMode return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum { if (!threadingType.HasValue) @@ -391,7 +384,7 @@ public FuncSystem OnExit(TState st, Action system, ThreadingMode? var stateChangeId = -1; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new FuncSystem(_world, (ticks, args, runIf) => { if (runIf?.Invoke(ticks, args) ?? true) { @@ -1522,6 +1515,11 @@ public interface ITinyMeta public interface ITinySystem : ITinyMeta { SystemOrderConfiguration OrderConfiguration { get; } + + ITinySystem RunIf() where T : ITinyConditionalSystem, new(); + ITinySystem RunIf(params ITinyConditionalSystem[] conditionals); + ITinySystem RunAfter(ITinySystem sys); + ITinySystem RunBefore(ITinySystem sys); } public interface ITinyConditionalSystem : ITinyMeta @@ -1658,10 +1656,10 @@ public override bool AfterExecute(World world) } - public TinySystem RunIf() where T : ITinyConditionalSystem, new() + public ITinySystem RunIf() where T : ITinyConditionalSystem, new() => RunIf(new T()); - public TinySystem RunIf(params ITinyConditionalSystem[] conditionals) + public ITinySystem RunIf(params ITinyConditionalSystem[] conditionals) { // TODO: Check for duplicates? foreach (var sys in conditionals) @@ -1669,14 +1667,14 @@ public TinySystem RunIf(params ITinyConditionalSystem[] conditionals) return this; } - public TinySystem RunAfter(ITinySystem sys) + public ITinySystem RunAfter(ITinySystem sys) { OrderConfiguration.Node?.List?.Remove(OrderConfiguration.Node); OrderConfiguration.Node = OrderConfiguration.AfterSystems.AddLast(sys); return this; } - public TinySystem RunBefore(ITinySystem sys) + public ITinySystem RunBefore(ITinySystem sys) { OrderConfiguration.Node?.List?.Remove(OrderConfiguration.Node); OrderConfiguration.Node = OrderConfiguration.BeforeSystems.AddLast(sys); @@ -1688,4 +1686,23 @@ public abstract class TinyConditionalSystem : TinySystemBase, ITinyConditionalSy { } +public sealed partial class TinyDelegateSystem : TinySystem +{ + private readonly Func _fn; + + public TinyDelegateSystem(Func fn) + { + _fn = fn; + } + + protected override void Setup(SystemParamBuilder builder) + { + } + + protected override bool Execute(World world) + { + return _fn(world, Ticks); + } +} + #endif diff --git a/src/TinyEcs.Systems.Interfaces.g.cs b/src/TinyEcs.Systems.Interfaces.g.cs index 62acf267..53d517ae 100644 --- a/src/TinyEcs.Systems.Interfaces.g.cs +++ b/src/TinyEcs.Systems.Interfaces.g.cs @@ -1,4 +1,4 @@ -#pragma warning disable 1591 +#pragma warning disable 1591 #nullable enable using System; @@ -9,13 +9,13 @@ namespace TinyEcs { #if NET9_0_OR_GREATER - public sealed partial class FuncSystem + public sealed partial class FuncSystem { - public FuncSystem RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; - var fn = (SystemTicks ticks, TArg args) => { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); return condition(obj0); }; @@ -23,13 +23,13 @@ public FuncSystem RunIf(Func condition) return this; } - public FuncSystem RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; - var fn = (SystemTicks ticks, TArg args) => { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); return condition(obj0, obj1); @@ -38,15 +38,15 @@ public FuncSystem RunIf(Func condition) return this; } - public FuncSystem RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; - var fn = (SystemTicks ticks, TArg args) => { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -56,17 +56,17 @@ public FuncSystem RunIf(Func condition) return this; } - public FuncSystem RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; T3? obj3 = null; - var fn = (SystemTicks ticks, TArg args) => { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -77,19 +77,19 @@ public FuncSystem RunIf(Func conditi return this; } - public FuncSystem RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; T3? obj3 = null; T4? obj4 = null; - var fn = (SystemTicks ticks, TArg args) => { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -101,13 +101,13 @@ public FuncSystem RunIf(Func return this; } - public FuncSystem RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam - where T5 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam + where T5 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; @@ -115,7 +115,7 @@ public FuncSystem RunIf(Func { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -128,14 +128,14 @@ public FuncSystem RunIf(Func RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam - where T5 : class, ISystemParam, IIntoSystemParam - where T6 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam + where T5 : class, ISystemParam, IIntoSystemParam + where T6 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; @@ -144,7 +144,7 @@ public FuncSystem RunIf(Func { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -158,15 +158,15 @@ public FuncSystem RunIf(Func RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam - where T5 : class, ISystemParam, IIntoSystemParam - where T6 : class, ISystemParam, IIntoSystemParam - where T7 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam + where T5 : class, ISystemParam, IIntoSystemParam + where T6 : class, ISystemParam, IIntoSystemParam + where T7 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; @@ -176,7 +176,7 @@ public FuncSystem RunIf(Func { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -191,16 +191,16 @@ public FuncSystem RunIf(Func RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam - where T5 : class, ISystemParam, IIntoSystemParam - where T6 : class, ISystemParam, IIntoSystemParam - where T7 : class, ISystemParam, IIntoSystemParam - where T8 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam + where T5 : class, ISystemParam, IIntoSystemParam + where T6 : class, ISystemParam, IIntoSystemParam + where T7 : class, ISystemParam, IIntoSystemParam + where T8 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; @@ -211,7 +211,7 @@ public FuncSystem RunIf(Func { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -227,17 +227,17 @@ public FuncSystem RunIf(Func RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam - where T5 : class, ISystemParam, IIntoSystemParam - where T6 : class, ISystemParam, IIntoSystemParam - where T7 : class, ISystemParam, IIntoSystemParam - where T8 : class, ISystemParam, IIntoSystemParam - where T9 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam + where T5 : class, ISystemParam, IIntoSystemParam + where T6 : class, ISystemParam, IIntoSystemParam + where T7 : class, ISystemParam, IIntoSystemParam + where T8 : class, ISystemParam, IIntoSystemParam + where T9 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; @@ -249,7 +249,7 @@ public FuncSystem RunIf(Func { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -266,18 +266,18 @@ public FuncSystem RunIf(Func RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam - where T5 : class, ISystemParam, IIntoSystemParam - where T6 : class, ISystemParam, IIntoSystemParam - where T7 : class, ISystemParam, IIntoSystemParam - where T8 : class, ISystemParam, IIntoSystemParam - where T9 : class, ISystemParam, IIntoSystemParam - where T10 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam + where T5 : class, ISystemParam, IIntoSystemParam + where T6 : class, ISystemParam, IIntoSystemParam + where T7 : class, ISystemParam, IIntoSystemParam + where T8 : class, ISystemParam, IIntoSystemParam + where T9 : class, ISystemParam, IIntoSystemParam + where T10 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; @@ -290,7 +290,7 @@ public FuncSystem RunIf(Func< T8? obj8 = null; T9? obj9 = null; T10? obj10 = null; - var fn = (SystemTicks ticks, TArg args) => { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -308,19 +308,19 @@ public FuncSystem RunIf(Func< return this; } - public FuncSystem RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam - where T5 : class, ISystemParam, IIntoSystemParam - where T6 : class, ISystemParam, IIntoSystemParam - where T7 : class, ISystemParam, IIntoSystemParam - where T8 : class, ISystemParam, IIntoSystemParam - where T9 : class, ISystemParam, IIntoSystemParam - where T10 : class, ISystemParam, IIntoSystemParam - where T11 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam + where T5 : class, ISystemParam, IIntoSystemParam + where T6 : class, ISystemParam, IIntoSystemParam + where T7 : class, ISystemParam, IIntoSystemParam + where T8 : class, ISystemParam, IIntoSystemParam + where T9 : class, ISystemParam, IIntoSystemParam + where T10 : class, ISystemParam, IIntoSystemParam + where T11 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; @@ -334,7 +334,7 @@ public FuncSystem RunIf( T9? obj9 = null; T10? obj10 = null; T11? obj11 = null; - var fn = (SystemTicks ticks, TArg args) => { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -353,20 +353,20 @@ public FuncSystem RunIf( return this; } - public FuncSystem RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam - where T5 : class, ISystemParam, IIntoSystemParam - where T6 : class, ISystemParam, IIntoSystemParam - where T7 : class, ISystemParam, IIntoSystemParam - where T8 : class, ISystemParam, IIntoSystemParam - where T9 : class, ISystemParam, IIntoSystemParam - where T10 : class, ISystemParam, IIntoSystemParam - where T11 : class, ISystemParam, IIntoSystemParam - where T12 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam + where T5 : class, ISystemParam, IIntoSystemParam + where T6 : class, ISystemParam, IIntoSystemParam + where T7 : class, ISystemParam, IIntoSystemParam + where T8 : class, ISystemParam, IIntoSystemParam + where T9 : class, ISystemParam, IIntoSystemParam + where T10 : class, ISystemParam, IIntoSystemParam + where T11 : class, ISystemParam, IIntoSystemParam + where T12 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; @@ -381,7 +381,7 @@ public FuncSystem RunIf { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -401,21 +401,21 @@ public FuncSystem RunIf RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam - where T5 : class, ISystemParam, IIntoSystemParam - where T6 : class, ISystemParam, IIntoSystemParam - where T7 : class, ISystemParam, IIntoSystemParam - where T8 : class, ISystemParam, IIntoSystemParam - where T9 : class, ISystemParam, IIntoSystemParam - where T10 : class, ISystemParam, IIntoSystemParam - where T11 : class, ISystemParam, IIntoSystemParam - where T12 : class, ISystemParam, IIntoSystemParam - where T13 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam + where T5 : class, ISystemParam, IIntoSystemParam + where T6 : class, ISystemParam, IIntoSystemParam + where T7 : class, ISystemParam, IIntoSystemParam + where T8 : class, ISystemParam, IIntoSystemParam + where T9 : class, ISystemParam, IIntoSystemParam + where T10 : class, ISystemParam, IIntoSystemParam + where T11 : class, ISystemParam, IIntoSystemParam + where T12 : class, ISystemParam, IIntoSystemParam + where T13 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; @@ -431,7 +431,7 @@ public FuncSystem RunIf { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -452,22 +452,22 @@ public FuncSystem RunIf RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam - where T5 : class, ISystemParam, IIntoSystemParam - where T6 : class, ISystemParam, IIntoSystemParam - where T7 : class, ISystemParam, IIntoSystemParam - where T8 : class, ISystemParam, IIntoSystemParam - where T9 : class, ISystemParam, IIntoSystemParam - where T10 : class, ISystemParam, IIntoSystemParam - where T11 : class, ISystemParam, IIntoSystemParam - where T12 : class, ISystemParam, IIntoSystemParam - where T13 : class, ISystemParam, IIntoSystemParam - where T14 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam + where T5 : class, ISystemParam, IIntoSystemParam + where T6 : class, ISystemParam, IIntoSystemParam + where T7 : class, ISystemParam, IIntoSystemParam + where T8 : class, ISystemParam, IIntoSystemParam + where T9 : class, ISystemParam, IIntoSystemParam + where T10 : class, ISystemParam, IIntoSystemParam + where T11 : class, ISystemParam, IIntoSystemParam + where T12 : class, ISystemParam, IIntoSystemParam + where T13 : class, ISystemParam, IIntoSystemParam + where T14 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; @@ -484,7 +484,7 @@ public FuncSystem RunIf { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -506,23 +506,23 @@ public FuncSystem RunIf RunIf(Func condition) - where T0 : class, ISystemParam, IIntoSystemParam - where T1 : class, ISystemParam, IIntoSystemParam - where T2 : class, ISystemParam, IIntoSystemParam - where T3 : class, ISystemParam, IIntoSystemParam - where T4 : class, ISystemParam, IIntoSystemParam - where T5 : class, ISystemParam, IIntoSystemParam - where T6 : class, ISystemParam, IIntoSystemParam - where T7 : class, ISystemParam, IIntoSystemParam - where T8 : class, ISystemParam, IIntoSystemParam - where T9 : class, ISystemParam, IIntoSystemParam - where T10 : class, ISystemParam, IIntoSystemParam - where T11 : class, ISystemParam, IIntoSystemParam - where T12 : class, ISystemParam, IIntoSystemParam - where T13 : class, ISystemParam, IIntoSystemParam - where T14 : class, ISystemParam, IIntoSystemParam - where T15 : class, ISystemParam, IIntoSystemParam + public ITinySystem RunIf(Func condition) + where T0 : class, ISystemParam, IIntoSystemParam + where T1 : class, ISystemParam, IIntoSystemParam + where T2 : class, ISystemParam, IIntoSystemParam + where T3 : class, ISystemParam, IIntoSystemParam + where T4 : class, ISystemParam, IIntoSystemParam + where T5 : class, ISystemParam, IIntoSystemParam + where T6 : class, ISystemParam, IIntoSystemParam + where T7 : class, ISystemParam, IIntoSystemParam + where T8 : class, ISystemParam, IIntoSystemParam + where T9 : class, ISystemParam, IIntoSystemParam + where T10 : class, ISystemParam, IIntoSystemParam + where T11 : class, ISystemParam, IIntoSystemParam + where T12 : class, ISystemParam, IIntoSystemParam + where T13 : class, ISystemParam, IIntoSystemParam + where T14 : class, ISystemParam, IIntoSystemParam + where T15 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; T1? obj1 = null; @@ -540,7 +540,7 @@ public FuncSystem RunIf { + var fn = (SystemTicks ticks, World args) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); diff --git a/src/TinyEcs.Systems.Interfaces.tt b/src/TinyEcs.Systems.Interfaces.tt index ef509323..1296a12e 100644 --- a/src/TinyEcs.Systems.Interfaces.tt +++ b/src/TinyEcs.Systems.Interfaces.tt @@ -32,23 +32,23 @@ using System.Runtime.CompilerServices; namespace TinyEcs { #if NET9_0_OR_GREATER - public sealed partial class FuncSystem + public sealed partial class FuncSystem { <# for (var i = 0; i < MAX_GENERICS; ++i) { var genericsArgs = GenerateSequence(i + 1, ", ", j => $"T{j}"); - var genericsArgsWhere = GenerateSequence(i + 1, "\n\t\t\t", j => $"where T{j} : class, ISystemParam, IIntoSystemParam"); + var genericsArgsWhere = GenerateSequence(i + 1, "\n\t\t\t", j => $"where T{j} : class, ISystemParam, IIntoSystemParam"); var objs = GenerateSequence(i + 1, "\n\t\t\t", j => $"T{j}? obj{j} = null;"); var objsGen = GenerateSequence(i + 1, "\n\t\t\t\t", j => $"obj{j} ??= (T{j})T{j}.Generate(args);"); var systemCall = GenerateSequence(i + 1, ", ", j => $"obj{j}"); var objsCheckInuse = GenerateSequence(i + 1, " ", j => $"obj{j}?.UseIndex != 0" + (j < i ? "||" : "")); #> - public FuncSystem RunIf<<#= genericsArgs #>>(Func<<#= genericsArgs #>, bool> condition) + public ITinySystem RunIf<<#= genericsArgs #>>(Func<<#= genericsArgs #>, bool> condition) <#= genericsArgsWhere #> { <#= objs #> - var fn = (SystemTicks ticks, TArg args) => { + var fn = (SystemTicks ticks, World args) => { <#= objsGen #> return condition(<#= systemCall #>); }; diff --git a/src/TinyEcs.Systems.Scheduler.g.cs b/src/TinyEcs.Systems.Scheduler.g.cs index 11c52862..4ac21c41 100644 --- a/src/TinyEcs.Systems.Scheduler.g.cs +++ b/src/TinyEcs.Systems.Scheduler.g.cs @@ -1,4 +1,4 @@ -#pragma warning disable 1591 +#pragma warning disable 1591 #nullable enable using System; @@ -12,7 +12,7 @@ namespace TinyEcs #if NET9_0_OR_GREATER public partial class Scheduler { - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam { if (!threadingType.HasValue) @@ -33,12 +33,12 @@ public FuncSystem AddSystem(Action system, Stages stage = Stages. obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam { @@ -64,12 +64,12 @@ public FuncSystem AddSystem(Action system, Stages stage = obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -100,12 +100,12 @@ public FuncSystem AddSystem(Action system, Stages obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -141,12 +141,12 @@ public FuncSystem AddSystem(Action system obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -187,12 +187,12 @@ public FuncSystem AddSystem(Action(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -238,12 +238,12 @@ public FuncSystem AddSystem(Action(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -294,12 +294,12 @@ public FuncSystem AddSystem(Action(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -355,12 +355,12 @@ public FuncSystem AddSystem(Action(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -421,12 +421,12 @@ public FuncSystem AddSystem(Action(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -492,12 +492,12 @@ public FuncSystem AddSystem(Actio obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -568,12 +568,12 @@ public FuncSystem AddSystem( obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -649,12 +649,12 @@ public FuncSystem AddSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -735,12 +735,12 @@ public FuncSystem AddSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -826,12 +826,12 @@ public FuncSystem AddSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -922,12 +922,12 @@ public FuncSystem AddSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1023,7 +1023,7 @@ public FuncSystem AddSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } diff --git a/src/TinyEcs.Systems.Scheduler.tt b/src/TinyEcs.Systems.Scheduler.tt index dbe10f08..06646d38 100644 --- a/src/TinyEcs.Systems.Scheduler.tt +++ b/src/TinyEcs.Systems.Scheduler.tt @@ -47,7 +47,7 @@ namespace TinyEcs var systemCall = GenerateSequence(i + 1, ", ", j => $"obj{j}"); var objsCheckInuse = GenerateSequence(i + 1, " ", j => $"obj{j}?.UseIndex != 0" + (j < i ? "||" : "")); #> - public FuncSystem AddSystem<<#= genericsArgs #>>(Action<<#= genericsArgs #>> system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem<<#= genericsArgs #>>(Action<<#= genericsArgs #>> system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) <#= genericsArgsWhere #> { if (!threadingType.HasValue) @@ -68,7 +68,7 @@ namespace TinyEcs <#= objsUnlock #> return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); Add(sys, stage); return sys; } diff --git a/src/TinyEcs.Systems.StageSpecific.g.cs b/src/TinyEcs.Systems.StageSpecific.g.cs index d954c236..4c206ba9 100644 --- a/src/TinyEcs.Systems.StageSpecific.g.cs +++ b/src/TinyEcs.Systems.StageSpecific.g.cs @@ -1,4 +1,4 @@ -#pragma warning disable 1591 +#pragma warning disable 1591 #nullable enable using System; @@ -12,7 +12,7 @@ namespace TinyEcs #if NET9_0_OR_GREATER public partial class Scheduler { - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam { if (!threadingType.HasValue) @@ -33,12 +33,12 @@ public FuncSystem OnStartup(Action system, ThreadingMode? threadi obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam { @@ -64,12 +64,12 @@ public FuncSystem OnStartup(Action system, ThreadingMode? obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -100,12 +100,12 @@ public FuncSystem OnStartup(Action system, Thread obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -141,12 +141,12 @@ public FuncSystem OnStartup(Action system obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -187,12 +187,12 @@ public FuncSystem OnStartup(Action(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -238,12 +238,12 @@ public FuncSystem OnStartup(Action(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -294,12 +294,12 @@ public FuncSystem OnStartup(Action(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -355,12 +355,12 @@ public FuncSystem OnStartup(Action(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -421,12 +421,12 @@ public FuncSystem OnStartup(Action(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -492,12 +492,12 @@ public FuncSystem OnStartup(Actio obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -568,12 +568,12 @@ public FuncSystem OnStartup( obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -649,12 +649,12 @@ public FuncSystem OnStartup(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -735,12 +735,12 @@ public FuncSystem OnStartup(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -826,12 +826,12 @@ public FuncSystem OnStartup(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -922,12 +922,12 @@ public FuncSystem OnStartup(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnStartup(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1023,12 +1023,12 @@ public FuncSystem OnStartup(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); Add(sys, Stages.Startup); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam { if (!threadingType.HasValue) @@ -1049,12 +1049,12 @@ public FuncSystem OnFrameStart(Action system, ThreadingMode? thre obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam { @@ -1080,12 +1080,12 @@ public FuncSystem OnFrameStart(Action system, ThreadingMo obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1116,12 +1116,12 @@ public FuncSystem OnFrameStart(Action system, Thr obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1157,12 +1157,12 @@ public FuncSystem OnFrameStart(Action sys obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1203,12 +1203,12 @@ public FuncSystem OnFrameStart(Action(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1254,12 +1254,12 @@ public FuncSystem OnFrameStart(Action(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1310,12 +1310,12 @@ public FuncSystem OnFrameStart(Action(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1371,12 +1371,12 @@ public FuncSystem OnFrameStart(Action(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1437,12 +1437,12 @@ public FuncSystem OnFrameStart(Action obj8.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1508,12 +1508,12 @@ public FuncSystem OnFrameStart(Ac obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1584,12 +1584,12 @@ public FuncSystem OnFrameStart(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1665,12 +1665,12 @@ public FuncSystem OnFrameStart(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1751,12 +1751,12 @@ public FuncSystem OnFrameStart(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1842,12 +1842,12 @@ public FuncSystem OnFrameStart(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -1938,12 +1938,12 @@ public FuncSystem OnFrameStart(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2039,12 +2039,12 @@ public FuncSystem OnFrameStart(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); Add(sys, Stages.FrameStart); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam { if (!threadingType.HasValue) @@ -2065,12 +2065,12 @@ public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? th obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam { @@ -2096,12 +2096,12 @@ public FuncSystem OnBeforeUpdate(Action system, Threading obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2132,12 +2132,12 @@ public FuncSystem OnBeforeUpdate(Action system, T obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2173,12 +2173,12 @@ public FuncSystem OnBeforeUpdate(Action s obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2219,12 +2219,12 @@ public FuncSystem OnBeforeUpdate(Action(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2270,12 +2270,12 @@ public FuncSystem OnBeforeUpdate(Action(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2326,12 +2326,12 @@ public FuncSystem OnBeforeUpdate(Action(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2387,12 +2387,12 @@ public FuncSystem OnBeforeUpdate(Action(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2453,12 +2453,12 @@ public FuncSystem OnBeforeUpdate(Acti obj8.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2524,12 +2524,12 @@ public FuncSystem OnBeforeUpdate( obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2600,12 +2600,12 @@ public FuncSystem OnBeforeUpdate(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2681,12 +2681,12 @@ public FuncSystem OnBeforeUpdate(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2767,12 +2767,12 @@ public FuncSystem OnBeforeUpdate(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2858,12 +2858,12 @@ public FuncSystem OnBeforeUpdate(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -2954,12 +2954,12 @@ public FuncSystem OnBeforeUpdate(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3055,12 +3055,12 @@ public FuncSystem OnBeforeUpdate(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); Add(sys, Stages.BeforeUpdate); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam { if (!threadingType.HasValue) @@ -3081,12 +3081,12 @@ public FuncSystem OnUpdate(Action system, ThreadingMode? threadin obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam { @@ -3112,12 +3112,12 @@ public FuncSystem OnUpdate(Action system, ThreadingMode? obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3148,12 +3148,12 @@ public FuncSystem OnUpdate(Action system, Threadi obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3189,12 +3189,12 @@ public FuncSystem OnUpdate(Action system, obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3235,12 +3235,12 @@ public FuncSystem OnUpdate(Action obj4.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3286,12 +3286,12 @@ public FuncSystem OnUpdate(Action(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3342,12 +3342,12 @@ public FuncSystem OnUpdate(Action(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3403,12 +3403,12 @@ public FuncSystem OnUpdate(Action(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3469,12 +3469,12 @@ public FuncSystem OnUpdate(Action(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3540,12 +3540,12 @@ public FuncSystem OnUpdate(Action obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3616,12 +3616,12 @@ public FuncSystem OnUpdate(A obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3697,12 +3697,12 @@ public FuncSystem OnUpdate(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3783,12 +3783,12 @@ public FuncSystem OnUpdate(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3874,12 +3874,12 @@ public FuncSystem OnUpdate(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -3970,12 +3970,12 @@ public FuncSystem OnUpdate(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4071,12 +4071,12 @@ public FuncSystem OnUpdate(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); Add(sys, Stages.Update); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam { if (!threadingType.HasValue) @@ -4097,12 +4097,12 @@ public FuncSystem OnAfterUpdate(Action system, ThreadingMode? thr obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam { @@ -4128,12 +4128,12 @@ public FuncSystem OnAfterUpdate(Action system, ThreadingM obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4164,12 +4164,12 @@ public FuncSystem OnAfterUpdate(Action system, Th obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4205,12 +4205,12 @@ public FuncSystem OnAfterUpdate(Action sy obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4251,12 +4251,12 @@ public FuncSystem OnAfterUpdate(Action(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4302,12 +4302,12 @@ public FuncSystem OnAfterUpdate(Action(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4358,12 +4358,12 @@ public FuncSystem OnAfterUpdate(Action(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4419,12 +4419,12 @@ public FuncSystem OnAfterUpdate(Action(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4485,12 +4485,12 @@ public FuncSystem OnAfterUpdate(Actio obj8.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4556,12 +4556,12 @@ public FuncSystem OnAfterUpdate(A obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4632,12 +4632,12 @@ public FuncSystem OnAfterUpdate(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4713,12 +4713,12 @@ public FuncSystem OnAfterUpdate(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4799,12 +4799,12 @@ public FuncSystem OnAfterUpdate(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4890,12 +4890,12 @@ public FuncSystem OnAfterUpdate(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -4986,12 +4986,12 @@ public FuncSystem OnAfterUpdate(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5087,12 +5087,12 @@ public FuncSystem OnAfterUpdate(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); Add(sys, Stages.AfterUpdate); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam { if (!threadingType.HasValue) @@ -5113,12 +5113,12 @@ public FuncSystem OnFrameEnd(Action system, ThreadingMode? thread obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam { @@ -5144,12 +5144,12 @@ public FuncSystem OnFrameEnd(Action system, ThreadingMode obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5180,12 +5180,12 @@ public FuncSystem OnFrameEnd(Action system, Threa obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5221,12 +5221,12 @@ public FuncSystem OnFrameEnd(Action syste obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5267,12 +5267,12 @@ public FuncSystem OnFrameEnd(Action(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5318,12 +5318,12 @@ public FuncSystem OnFrameEnd(Action(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5374,12 +5374,12 @@ public FuncSystem OnFrameEnd(Action(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5435,12 +5435,12 @@ public FuncSystem OnFrameEnd(Action(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5501,12 +5501,12 @@ public FuncSystem OnFrameEnd(Action(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5572,12 +5572,12 @@ public FuncSystem OnFrameEnd(Acti obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5648,12 +5648,12 @@ public FuncSystem OnFrameEnd obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5729,12 +5729,12 @@ public FuncSystem OnFrameEnd(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5815,12 +5815,12 @@ public FuncSystem OnFrameEnd(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -5906,12 +5906,12 @@ public FuncSystem OnFrameEnd(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -6002,12 +6002,12 @@ public FuncSystem OnFrameEnd(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam @@ -6103,17 +6103,17 @@ public FuncSystem OnFrameEnd(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); Add(sys, Stages.FrameEnd); return sys; } - public FuncSystem OnStartup(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnStartup(Action system, ThreadingMode threadingType = ThreadingMode.Auto) { if (threadingType == ThreadingMode.Auto) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new FuncSystem(_world, (ticks, args, runIf) => { if (runIf?.Invoke(ticks, args) ?? true) { @@ -6126,12 +6126,12 @@ public FuncSystem OnStartup(Action system, ThreadingMode threadingType = return sys; } - public FuncSystem OnFrameStart(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnFrameStart(Action system, ThreadingMode threadingType = ThreadingMode.Auto) { if (threadingType == ThreadingMode.Auto) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new FuncSystem(_world, (ticks, args, runIf) => { if (runIf?.Invoke(ticks, args) ?? true) { @@ -6144,12 +6144,12 @@ public FuncSystem OnFrameStart(Action system, ThreadingMode threadingType return sys; } - public FuncSystem OnBeforeUpdate(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode threadingType = ThreadingMode.Auto) { if (threadingType == ThreadingMode.Auto) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new FuncSystem(_world, (ticks, args, runIf) => { if (runIf?.Invoke(ticks, args) ?? true) { @@ -6162,12 +6162,12 @@ public FuncSystem OnBeforeUpdate(Action system, ThreadingMode threadingTy return sys; } - public FuncSystem OnUpdate(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnUpdate(Action system, ThreadingMode threadingType = ThreadingMode.Auto) { if (threadingType == ThreadingMode.Auto) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new FuncSystem(_world, (ticks, args, runIf) => { if (runIf?.Invoke(ticks, args) ?? true) { @@ -6180,12 +6180,12 @@ public FuncSystem OnUpdate(Action system, ThreadingMode threadingType = T return sys; } - public FuncSystem OnAfterUpdate(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode threadingType = ThreadingMode.Auto) { if (threadingType == ThreadingMode.Auto) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new FuncSystem(_world, (ticks, args, runIf) => { if (runIf?.Invoke(ticks, args) ?? true) { @@ -6198,12 +6198,12 @@ public FuncSystem OnAfterUpdate(Action system, ThreadingMode threadingTyp return sys; } - public FuncSystem OnFrameEnd(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnFrameEnd(Action system, ThreadingMode threadingType = ThreadingMode.Auto) { if (threadingType == ThreadingMode.Auto) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new FuncSystem(_world, (ticks, args, runIf) => { if (runIf?.Invoke(ticks, args) ?? true) { diff --git a/src/TinyEcs.Systems.StageSpecific.tt b/src/TinyEcs.Systems.StageSpecific.tt index 588acaea..dcfcadb8 100644 --- a/src/TinyEcs.Systems.StageSpecific.tt +++ b/src/TinyEcs.Systems.StageSpecific.tt @@ -59,7 +59,7 @@ namespace TinyEcs var systemCall = GenerateSequence(i + 1, ", ", j => $"obj{j}"); var objsCheckInuse = GenerateSequence(i + 1, " ", j => $"obj{j}?.UseIndex != 0" + (j < i ? "||" : "")); #> - public FuncSystem <#= stage.MethodName #><<#= genericsArgs #>>(Action<<#= genericsArgs #>> system, ThreadingMode? threadingType = null) + public ITinySystem <#= stage.MethodName #><<#= genericsArgs #>>(Action<<#= genericsArgs #>> system, ThreadingMode? threadingType = null) <#= genericsArgsWhere #> { if (!threadingType.HasValue) @@ -80,7 +80,7 @@ namespace TinyEcs <#= objsUnlock #> return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.<#= stage.StageName #>, threadingType.Value); + var sys = new FuncSystem(_world, fn, checkInuse, Stages.<#= stage.StageName #>, threadingType.Value); Add(sys, Stages.<#= stage.StageName #>); return sys; } @@ -93,12 +93,12 @@ namespace TinyEcs foreach (var stage in stageNames) { #> - public FuncSystem <#= stage.MethodName #>(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem <#= stage.MethodName #>(Action system, ThreadingMode threadingType = ThreadingMode.Auto) { if (threadingType == ThreadingMode.Auto) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new FuncSystem(_world, (ticks, args, runIf) => { if (runIf?.Invoke(ticks, args) ?? true) { diff --git a/src/TinyEcs.Systems.StateHandlers.g.cs b/src/TinyEcs.Systems.StateHandlers.g.cs index e7f501cf..edb9f721 100644 --- a/src/TinyEcs.Systems.StateHandlers.g.cs +++ b/src/TinyEcs.Systems.StateHandlers.g.cs @@ -1,4 +1,4 @@ -#pragma warning disable 1591 +#pragma warning disable 1591 #nullable enable using System; @@ -12,7 +12,7 @@ namespace TinyEcs #if NET9_0_OR_GREATER public partial class Scheduler { - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam { @@ -35,13 +35,13 @@ public FuncSystem OnEnter(TState st, Action system, Threa obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam { @@ -64,13 +64,13 @@ public FuncSystem OnExit(TState st, Action system, Thread obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -98,13 +98,13 @@ public FuncSystem OnEnter(TState st, Action syste obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -132,13 +132,13 @@ public FuncSystem OnExit(TState st, Action system obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -171,13 +171,13 @@ public FuncSystem OnEnter(TState st, Action(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -210,13 +210,13 @@ public FuncSystem OnExit(TState st, Action(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -254,13 +254,13 @@ public FuncSystem OnEnter(TState st, Action(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -298,13 +298,13 @@ public FuncSystem OnExit(TState st, Action(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -347,13 +347,13 @@ public FuncSystem OnEnter(TState st, Action(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -396,13 +396,13 @@ public FuncSystem OnExit(TState st, Action(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -450,13 +450,13 @@ public FuncSystem OnEnter(TState st, Acti obj5.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -504,13 +504,13 @@ public FuncSystem OnExit(TState st, Actio obj5.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -563,13 +563,13 @@ public FuncSystem OnEnter(TState st, obj6.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -622,13 +622,13 @@ public FuncSystem OnExit(TState st, A obj6.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -686,13 +686,13 @@ public FuncSystem OnEnter(TState obj7.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -750,13 +750,13 @@ public FuncSystem OnExit(TState s obj7.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -819,13 +819,13 @@ public FuncSystem OnEnter(TSt obj8.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -888,13 +888,13 @@ public FuncSystem OnExit(TSta obj8.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -962,13 +962,13 @@ public FuncSystem OnEnter obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1036,13 +1036,13 @@ public FuncSystem OnExit( obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1115,13 +1115,13 @@ public FuncSystem OnEnter(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1194,13 +1194,13 @@ public FuncSystem OnExit(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1278,13 +1278,13 @@ public FuncSystem OnEnter(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1362,13 +1362,13 @@ public FuncSystem OnExit(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1451,13 +1451,13 @@ public FuncSystem OnEnter(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1540,13 +1540,13 @@ public FuncSystem OnExit(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1634,13 +1634,13 @@ public FuncSystem OnEnter(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1728,13 +1728,13 @@ public FuncSystem OnExit(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1827,13 +1827,13 @@ public FuncSystem OnEnter(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1926,13 +1926,13 @@ public FuncSystem OnExit(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public FuncSystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -2030,13 +2030,13 @@ public FuncSystem OnEnter(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -2134,7 +2134,7 @@ public FuncSystem OnExit(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; diff --git a/src/TinyEcs.Systems.StateHandlers.tt b/src/TinyEcs.Systems.StateHandlers.tt index 87eac440..cb53c7ec 100644 --- a/src/TinyEcs.Systems.StateHandlers.tt +++ b/src/TinyEcs.Systems.StateHandlers.tt @@ -47,7 +47,7 @@ namespace TinyEcs var systemCall = GenerateSequence(i + 1, ", ", j => $"obj{j}"); var objsCheckInuse = GenerateSequence(i + 1, " ", j => $"obj{j}?.UseIndex != 0" + (j < i ? "||" : "")); #> - public FuncSystem OnEnter>(TState st, Action<<#= genericsArgs #>> system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter>(TState st, Action<<#= genericsArgs #>> system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum <#= genericsArgsWhere #> { @@ -70,13 +70,13 @@ namespace TinyEcs <#= objsUnlock #> return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public FuncSystem OnExit>(TState st, Action<<#= genericsArgs #>> system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit>(TState st, Action<<#= genericsArgs #>> system, ThreadingMode threadingType = ThreadingMode.Auto) where TState : struct, Enum <#= genericsArgsWhere #> { @@ -99,7 +99,7 @@ namespace TinyEcs <#= objsUnlock #> return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; From bbf698e24b5a8406120fcaf9eae0e0d72991fc6c Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Thu, 11 Sep 2025 00:30:41 +0200 Subject: [PATCH 25/33] + moved to TinyDelegateSystem --- src/Bevy.cs | 226 +----- src/TinyEcs.Systems.Interfaces.g.cs | 98 ++- src/TinyEcs.Systems.Interfaces.tt | 8 +- src/TinyEcs.Systems.Scheduler.g.cs | 160 ++-- src/TinyEcs.Systems.Scheduler.tt | 11 +- src/TinyEcs.Systems.StageSpecific.g.cs | 990 +++++++++---------------- src/TinyEcs.Systems.StageSpecific.tt | 31 +- src/TinyEcs.Systems.StateHandlers.g.cs | 416 ++++------- src/TinyEcs.Systems.StateHandlers.tt | 27 +- 9 files changed, 686 insertions(+), 1281 deletions(-) diff --git a/src/Bevy.cs b/src/Bevy.cs index 689892fa..9846dcb0 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -5,189 +5,6 @@ namespace TinyEcs; // https://promethia-27.github.io/dependency_injection_like_bevy_from_scratch/introductions.html #if NET9_0_OR_GREATER - -public sealed partial class FuncSystem : ITinySystem -{ - private readonly World _arg; - private readonly Func, bool> _fn; - private readonly List> _conditions; - private readonly Func _validator; - private readonly Func _checkInUse; - - public ISystemParam[] SystemParams { get; set; } = []; - public SystemTicks Ticks { get; } = new(); - public SystemConfiguration Configuration { get; } = new(); - public SystemOrderConfiguration OrderConfiguration { get; } = new(); - - - internal FuncSystem(World arg, Func, bool> fn, Func checkInUse, Stages stage, ThreadingMode threadingType) - { - _arg = arg; - _fn = fn; - _conditions = new(); - _validator = ValidateConditions; - _checkInUse = checkInUse; - Configuration.ThreadingMode = threadingType; - Configuration.Stage = stage; - } - - - public ITinySystem RunIf() where T : ITinyConditionalSystem, new() - { - var conditional = new T(); - _conditions.Add((ticks, args) => conditional.ExecuteOnReady(args, ticks.ThisRun)); - - return this; - } - - public ITinySystem RunIf(params ITinyConditionalSystem[] conditionals) - { - foreach (var conditional in conditionals) - _conditions.Add((ticks, args) => conditional.ExecuteOnReady(args, ticks.ThisRun)); - - return this; - } - - public ITinySystem RunAfter(ITinySystem parent) - { - if (this == parent || Contains(parent, s => s.OrderConfiguration.AfterSystems)) - throw new InvalidOperationException("Circular dependency detected"); - - OrderConfiguration.Node?.List?.Remove(OrderConfiguration.Node); - OrderConfiguration.Node = parent.OrderConfiguration.AfterSystems.AddLast(this); - - return this; - } - - public ITinySystem RunAfter(params ReadOnlySpan systems) - { - foreach (var system in systems) - system.RunAfter(this); - - return this; - } - - public ITinySystem RunBefore(ITinySystem parent) - { - if (this == parent || Contains(parent, s => s.OrderConfiguration.BeforeSystems)) - throw new InvalidOperationException("Circular dependency detected"); - - OrderConfiguration.Node?.List?.Remove(OrderConfiguration.Node); - OrderConfiguration.Node = parent.OrderConfiguration.BeforeSystems.AddLast(this); - - return this; - } - - public ITinySystem RunBefore(params ReadOnlySpan systems) - { - foreach (var system in systems) - system.RunBefore(this); - - return this; - } - - private bool Contains(ITinySystem system, Func?> direction) - { - ITinySystem? current = this; - while (current != null) - { - if (current == system) - return true; - - var nextNode = direction(current)?.First; - current = nextNode?.Value; - } - return false; - } - - internal bool IsResourceInUse() - { - return Configuration.ThreadingMode switch - { - ThreadingMode.Multi => false, - ThreadingMode.Single => true, - _ or ThreadingMode.Auto => _checkInUse() - }; - } - - private bool ValidateConditions(SystemTicks ticks, World args) - { - foreach (var fn in _conditions) - if (!fn(ticks, args)) - return false; - return true; - } - - - public void Initialize(World world) - { - - } - - public bool BeforeExecute(World world) - { - var ticks = Ticks.ThisRun; - foreach (var beforeSys in OrderConfiguration.BeforeSystems) - { - if (!beforeSys.ExecuteOnReady(world, ticks)) - { - return false; - } - } - return true; - } - - public bool AfterExecute(World world) - { - var ticks = Ticks.ThisRun; - foreach (var afterSys in OrderConfiguration.AfterSystems) - { - if (!afterSys.ExecuteOnReady(world, ticks)) - { - return false; - } - } - return true; - } - - public bool ExecuteOnReady(World world, uint ticks) - { - Ticks.ThisRun = ticks; - var config = Configuration; - - var canRun = BeforeExecute(world); - - if (canRun) - { - foreach (var conditional in config.Conditionals) - { - if (!conditional.ExecuteOnReady(world, ticks)) - { - canRun = false; - break; - } - } - - if (canRun) - canRun = _fn(Ticks, _arg, _validator); - - if (canRun) - { - canRun = AfterExecute(world); - } - } - - Ticks.LastRun = Ticks.ThisRun; - - return canRun; - } - - public bool ParamsAreLocked() - { - return IsResourceInUse(); - } -} - public enum Stages { Startup, @@ -333,20 +150,17 @@ public Scheduler AddSystems2(Stages stage, params ITinySystem[] systems) return this; } - public FuncSystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) + public ITinySystem AddSystem(Action system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) { if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new TinyDelegateSystem((args, ticks) => { - if (runIf?.Invoke(ticks, args) ?? true) - { - system(); - return true; - } - return false; - }, () => false, stage, threadingType.Value); + system(); + return true; + }); + sys.Configuration.ThreadingMode = threadingType; Add(sys, stage); return sys; @@ -360,15 +174,12 @@ public ITinySystem OnEnter(TState st, Action system, ThreadingMode? thre var stateChangeId = -1; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new TinyDelegateSystem((args, ticks) => { - if (runIf?.Invoke(ticks, args) ?? true) - { - system(); - return true; - } - return false; - }, () => false, Stages.OnEnter, threadingType.Value) + system(); + return true; + }) + { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType.Value } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); @@ -384,15 +195,12 @@ public ITinySystem OnExit(TState st, Action system, ThreadingMode? threa var stateChangeId = -1; - var sys = new FuncSystem(_world, (ticks, args, runIf) => + var sys = new TinyDelegateSystem((args, ticks) => { - if (runIf?.Invoke(ticks, args) ?? true) - { - system(); - return true; - } - return false; - }, () => false, Stages.OnExit, threadingType.Value) + system(); + return true; + }) + { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType.Value } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); @@ -1686,7 +1494,7 @@ public abstract class TinyConditionalSystem : TinySystemBase, ITinyConditionalSy { } -public sealed partial class TinyDelegateSystem : TinySystem +public sealed partial class TinyDelegateSystem : TinySystem, ITinyConditionalSystem { private readonly Func _fn; diff --git a/src/TinyEcs.Systems.Interfaces.g.cs b/src/TinyEcs.Systems.Interfaces.g.cs index 53d517ae..25fcf6c1 100644 --- a/src/TinyEcs.Systems.Interfaces.g.cs +++ b/src/TinyEcs.Systems.Interfaces.g.cs @@ -9,17 +9,19 @@ namespace TinyEcs { #if NET9_0_OR_GREATER - public sealed partial class FuncSystem + public sealed partial class TinyDelegateSystem { public ITinySystem RunIf(Func condition) where T0 : class, ISystemParam, IIntoSystemParam { T0? obj0 = null; - var fn = (SystemTicks ticks, World args) => { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); return condition(obj0); }; - _conditions.Add(fn); + + var sys = new TinyDelegateSystem(fn); + Configuration.Conditionals.Add(sys); return this; } @@ -29,12 +31,14 @@ public ITinySystem RunIf(Func condition) { T0? obj0 = null; T1? obj1 = null; - var fn = (SystemTicks ticks, World args) => { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); return condition(obj0, obj1); }; - _conditions.Add(fn); + + var sys = new TinyDelegateSystem(fn); + Configuration.Conditionals.Add(sys); return this; } @@ -46,13 +50,15 @@ public ITinySystem RunIf(Func condition) T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; - var fn = (SystemTicks ticks, World args) => { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); return condition(obj0, obj1, obj2); }; - _conditions.Add(fn); + + var sys = new TinyDelegateSystem(fn); + Configuration.Conditionals.Add(sys); return this; } @@ -66,14 +72,16 @@ public ITinySystem RunIf(Func condition) T1? obj1 = null; T2? obj2 = null; T3? obj3 = null; - var fn = (SystemTicks ticks, World args) => { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); obj3 ??= (T3)T3.Generate(args); return condition(obj0, obj1, obj2, obj3); }; - _conditions.Add(fn); + + var sys = new TinyDelegateSystem(fn); + Configuration.Conditionals.Add(sys); return this; } @@ -89,7 +97,7 @@ public ITinySystem RunIf(Func cond T2? obj2 = null; T3? obj3 = null; T4? obj4 = null; - var fn = (SystemTicks ticks, World args) => { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -97,7 +105,9 @@ public ITinySystem RunIf(Func cond obj4 ??= (T4)T4.Generate(args); return condition(obj0, obj1, obj2, obj3, obj4); }; - _conditions.Add(fn); + + var sys = new TinyDelegateSystem(fn); + Configuration.Conditionals.Add(sys); return this; } @@ -115,7 +125,7 @@ public ITinySystem RunIf(Func { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -124,7 +134,9 @@ public ITinySystem RunIf(Func(Func { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -154,7 +166,9 @@ public ITinySystem RunIf(Func(Func { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -187,7 +201,9 @@ public ITinySystem RunIf(Func(Func { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -223,7 +239,9 @@ public ITinySystem RunIf(Func(Func { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -262,7 +280,9 @@ public ITinySystem RunIf(Func(Func { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -304,7 +324,9 @@ public ITinySystem RunIf(Func(Func< T9? obj9 = null; T10? obj10 = null; T11? obj11 = null; - var fn = (SystemTicks ticks, World args) => { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -349,7 +371,9 @@ public ITinySystem RunIf(Func< obj11 ??= (T11)T11.Generate(args); return condition(obj0, obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10, obj11); }; - _conditions.Add(fn); + + var sys = new TinyDelegateSystem(fn); + Configuration.Conditionals.Add(sys); return this; } @@ -381,7 +405,7 @@ public ITinySystem RunIf( T10? obj10 = null; T11? obj11 = null; T12? obj12 = null; - var fn = (SystemTicks ticks, World args) => { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -397,7 +421,9 @@ public ITinySystem RunIf( obj12 ??= (T12)T12.Generate(args); return condition(obj0, obj1, obj2, obj3, obj4, obj5, obj6, obj7, obj8, obj9, obj10, obj11, obj12); }; - _conditions.Add(fn); + + var sys = new TinyDelegateSystem(fn); + Configuration.Conditionals.Add(sys); return this; } @@ -431,7 +457,7 @@ public ITinySystem RunIf { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -448,7 +474,9 @@ public ITinySystem RunIf { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -502,7 +530,9 @@ public ITinySystem RunIf { + var fn = (World args, SystemTicks ticks) => { obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -559,7 +589,9 @@ public ITinySystem RunIf { <#= objs #> - var fn = (SystemTicks ticks, World args) => { + var fn = (World args, SystemTicks ticks) => { <#= objsGen #> return condition(<#= systemCall #>); }; - _conditions.Add(fn); + + var sys = new TinyDelegateSystem(fn); + Configuration.Conditionals.Add(sys); return this; } diff --git a/src/TinyEcs.Systems.Scheduler.g.cs b/src/TinyEcs.Systems.Scheduler.g.cs index 4ac21c41..c5d22652 100644 --- a/src/TinyEcs.Systems.Scheduler.g.cs +++ b/src/TinyEcs.Systems.Scheduler.g.cs @@ -19,12 +19,8 @@ public ITinySystem AddSystem(Action system, Stages stage = Stages.Update threadingType = ThreadingExecutionMode; T0? obj0 = null; - var checkInuse = () => obj0?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj0.Lock(ticks); args.BeginDeferred(); @@ -33,7 +29,9 @@ public ITinySystem AddSystem(Action system, Stages stage = Stages.Update obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType.Value; + sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -47,12 +45,8 @@ public ITinySystem AddSystem(Action system, Stages stage = Stage T0? obj0 = null; T1? obj1 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj0.Lock(ticks); @@ -64,7 +58,9 @@ public ITinySystem AddSystem(Action system, Stages stage = Stage obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType.Value; + sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -80,12 +76,8 @@ public ITinySystem AddSystem(Action system, Stages stage T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -100,7 +92,9 @@ public ITinySystem AddSystem(Action system, Stages stage obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType.Value; + sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -118,12 +112,8 @@ public ITinySystem AddSystem(Action system, Stag T1? obj1 = null; T2? obj2 = null; T3? obj3 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -141,7 +131,9 @@ public ITinySystem AddSystem(Action system, Stag obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType.Value; + sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -161,12 +153,8 @@ public ITinySystem AddSystem(Action syst T2? obj2 = null; T3? obj3 = null; T4? obj4 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -187,7 +175,9 @@ public ITinySystem AddSystem(Action syst obj4.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType.Value; + sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -209,12 +199,8 @@ public ITinySystem AddSystem(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -238,7 +224,9 @@ public ITinySystem AddSystem(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -294,7 +278,9 @@ public ITinySystem AddSystem(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -355,7 +337,9 @@ public ITinySystem AddSystem(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -421,7 +401,9 @@ public ITinySystem AddSystem(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -492,7 +470,9 @@ public ITinySystem AddSystem(Action(Action T8? obj8 = null; T9? obj9 = null; T10? obj10 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -568,7 +544,9 @@ public ITinySystem AddSystem(Action obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType.Value; + sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -602,12 +580,8 @@ public ITinySystem AddSystem(A T9? obj9 = null; T10? obj10 = null; T11? obj11 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -649,7 +623,9 @@ public ITinySystem AddSystem(A obj11.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType.Value; + sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -685,12 +661,8 @@ public ITinySystem AddSystem obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -735,7 +707,9 @@ public ITinySystem AddSystem obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -826,7 +796,9 @@ public ITinySystem AddSystem obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -922,7 +890,9 @@ public ITinySystem AddSystem obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0|| obj15?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1023,7 +989,9 @@ public ITinySystem AddSystem $"obj{j}.Lock(ticks);"); var objsUnlock = GenerateSequence(i + 1, "\n\t\t\t\t", j => $"obj{j}.Unlock();"); var systemCall = GenerateSequence(i + 1, ", ", j => $"obj{j}"); - var objsCheckInuse = GenerateSequence(i + 1, " ", j => $"obj{j}?.UseIndex != 0" + (j < i ? "||" : "")); #> public ITinySystem AddSystem<<#= genericsArgs #>>(Action<<#= genericsArgs #>> system, Stages stage = Stages.Update, ThreadingMode? threadingType = null) <#= genericsArgsWhere #> @@ -54,12 +53,8 @@ namespace TinyEcs threadingType = ThreadingExecutionMode; <#= objs #> - var checkInuse = () => <#= objsCheckInuse #>; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - <#= objsGen #> <#= objsLock #> args.BeginDeferred(); @@ -68,7 +63,9 @@ namespace TinyEcs <#= objsUnlock #> return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, stage, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType.Value; + sys.Configuration.Stage = stage; Add(sys, stage); return sys; } diff --git a/src/TinyEcs.Systems.StageSpecific.g.cs b/src/TinyEcs.Systems.StageSpecific.g.cs index 4c206ba9..9ea3e200 100644 --- a/src/TinyEcs.Systems.StageSpecific.g.cs +++ b/src/TinyEcs.Systems.StageSpecific.g.cs @@ -19,12 +19,8 @@ public ITinySystem OnStartup(Action system, ThreadingMode? threadingType threadingType = ThreadingExecutionMode; T0? obj0 = null; - var checkInuse = () => obj0?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj0.Lock(ticks); args.BeginDeferred(); @@ -33,7 +29,8 @@ public ITinySystem OnStartup(Action system, ThreadingMode? threadingType obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Startup); return sys; } @@ -47,12 +44,8 @@ public ITinySystem OnStartup(Action system, ThreadingMode? threa T0? obj0 = null; T1? obj1 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj0.Lock(ticks); @@ -64,7 +57,8 @@ public ITinySystem OnStartup(Action system, ThreadingMode? threa obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Startup); return sys; } @@ -80,12 +74,8 @@ public ITinySystem OnStartup(Action system, ThreadingMod T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -100,7 +90,8 @@ public ITinySystem OnStartup(Action system, ThreadingMod obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Startup); return sys; } @@ -118,12 +109,8 @@ public ITinySystem OnStartup(Action system, Thre T1? obj1 = null; T2? obj2 = null; T3? obj3 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -141,7 +128,8 @@ public ITinySystem OnStartup(Action system, Thre obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Startup); return sys; } @@ -161,12 +149,8 @@ public ITinySystem OnStartup(Action syst T2? obj2 = null; T3? obj3 = null; T4? obj4 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -187,7 +171,8 @@ public ITinySystem OnStartup(Action syst obj4.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Startup); return sys; } @@ -209,12 +194,8 @@ public ITinySystem OnStartup(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -238,7 +219,8 @@ public ITinySystem OnStartup(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -294,7 +272,8 @@ public ITinySystem OnStartup(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -355,7 +330,8 @@ public ITinySystem OnStartup(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -421,7 +393,8 @@ public ITinySystem OnStartup(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -492,7 +461,8 @@ public ITinySystem OnStartup(Action(Action T8? obj8 = null; T9? obj9 = null; T10? obj10 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -568,7 +534,8 @@ public ITinySystem OnStartup(Action obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Startup); return sys; } @@ -602,12 +569,8 @@ public ITinySystem OnStartup(A T9? obj9 = null; T10? obj10 = null; T11? obj11 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -649,7 +612,8 @@ public ITinySystem OnStartup(A obj11.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Startup, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Startup); return sys; } @@ -685,12 +649,8 @@ public ITinySystem OnStartup obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -735,7 +695,8 @@ public ITinySystem OnStartup obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -826,7 +783,8 @@ public ITinySystem OnStartup obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -922,7 +876,8 @@ public ITinySystem OnStartup obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0|| obj15?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1023,7 +974,8 @@ public ITinySystem OnStartup(Action system, ThreadingMode? threadingT threadingType = ThreadingExecutionMode; T0? obj0 = null; - var checkInuse = () => obj0?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj0.Lock(ticks); args.BeginDeferred(); @@ -1049,7 +997,8 @@ public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingT obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameStart); return sys; } @@ -1063,12 +1012,8 @@ public ITinySystem OnFrameStart(Action system, ThreadingMode? th T0? obj0 = null; T1? obj1 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj0.Lock(ticks); @@ -1080,7 +1025,8 @@ public ITinySystem OnFrameStart(Action system, ThreadingMode? th obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameStart); return sys; } @@ -1096,12 +1042,8 @@ public ITinySystem OnFrameStart(Action system, Threading T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1116,7 +1058,8 @@ public ITinySystem OnFrameStart(Action system, Threading obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameStart); return sys; } @@ -1134,12 +1077,8 @@ public ITinySystem OnFrameStart(Action system, T T1? obj1 = null; T2? obj2 = null; T3? obj3 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1157,7 +1096,8 @@ public ITinySystem OnFrameStart(Action system, T obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameStart); return sys; } @@ -1177,12 +1117,8 @@ public ITinySystem OnFrameStart(Action s T2? obj2 = null; T3? obj3 = null; T4? obj4 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1203,7 +1139,8 @@ public ITinySystem OnFrameStart(Action s obj4.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameStart); return sys; } @@ -1225,12 +1162,8 @@ public ITinySystem OnFrameStart(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1254,7 +1187,8 @@ public ITinySystem OnFrameStart(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1310,7 +1240,8 @@ public ITinySystem OnFrameStart(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1371,7 +1298,8 @@ public ITinySystem OnFrameStart(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1437,7 +1361,8 @@ public ITinySystem OnFrameStart(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1508,7 +1429,8 @@ public ITinySystem OnFrameStart(Action(Act T8? obj8 = null; T9? obj9 = null; T10? obj10 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1584,7 +1502,8 @@ public ITinySystem OnFrameStart(Act obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameStart, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameStart); return sys; } @@ -1618,12 +1537,8 @@ public ITinySystem OnFrameStart obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1665,7 +1580,8 @@ public ITinySystem OnFrameStart obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1751,7 +1663,8 @@ public ITinySystem OnFrameStart obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1842,7 +1751,8 @@ public ITinySystem OnFrameStart obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1938,7 +1844,8 @@ public ITinySystem OnFrameStart obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0|| obj15?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2039,7 +1942,8 @@ public ITinySystem OnFrameStart(Action system, ThreadingMode? threadin threadingType = ThreadingExecutionMode; T0? obj0 = null; - var checkInuse = () => obj0?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj0.Lock(ticks); args.BeginDeferred(); @@ -2065,7 +1965,8 @@ public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadin obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.BeforeUpdate); return sys; } @@ -2079,12 +1980,8 @@ public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? T0? obj0 = null; T1? obj1 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj0.Lock(ticks); @@ -2096,7 +1993,8 @@ public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.BeforeUpdate); return sys; } @@ -2112,12 +2010,8 @@ public ITinySystem OnBeforeUpdate(Action system, Threadi T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2132,7 +2026,8 @@ public ITinySystem OnBeforeUpdate(Action system, Threadi obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.BeforeUpdate); return sys; } @@ -2150,12 +2045,8 @@ public ITinySystem OnBeforeUpdate(Action system, T1? obj1 = null; T2? obj2 = null; T3? obj3 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2173,7 +2064,8 @@ public ITinySystem OnBeforeUpdate(Action system, obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.BeforeUpdate); return sys; } @@ -2193,12 +2085,8 @@ public ITinySystem OnBeforeUpdate(Action T2? obj2 = null; T3? obj3 = null; T4? obj4 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2219,7 +2107,8 @@ public ITinySystem OnBeforeUpdate(Action obj4.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.BeforeUpdate); return sys; } @@ -2241,12 +2130,8 @@ public ITinySystem OnBeforeUpdate(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2270,7 +2155,8 @@ public ITinySystem OnBeforeUpdate(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2326,7 +2208,8 @@ public ITinySystem OnBeforeUpdate(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2387,7 +2266,8 @@ public ITinySystem OnBeforeUpdate(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2453,7 +2329,8 @@ public ITinySystem OnBeforeUpdate(Action(Action T7? obj7 = null; T8? obj8 = null; T9? obj9 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2524,7 +2397,8 @@ public ITinySystem OnBeforeUpdate(Action obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.BeforeUpdate); return sys; } @@ -2556,12 +2430,8 @@ public ITinySystem OnBeforeUpdate(A T8? obj8 = null; T9? obj9 = null; T10? obj10 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2600,7 +2470,8 @@ public ITinySystem OnBeforeUpdate(A obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.BeforeUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.BeforeUpdate); return sys; } @@ -2634,12 +2505,8 @@ public ITinySystem OnBeforeUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2681,7 +2548,8 @@ public ITinySystem OnBeforeUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2767,7 +2631,8 @@ public ITinySystem OnBeforeUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2858,7 +2719,8 @@ public ITinySystem OnBeforeUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2954,7 +2812,8 @@ public ITinySystem OnBeforeUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0|| obj15?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3055,7 +2910,8 @@ public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType threadingType = ThreadingExecutionMode; T0? obj0 = null; - var checkInuse = () => obj0?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj0.Lock(ticks); args.BeginDeferred(); @@ -3081,7 +2933,8 @@ public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Update); return sys; } @@ -3095,12 +2948,8 @@ public ITinySystem OnUpdate(Action system, ThreadingMode? thread T0? obj0 = null; T1? obj1 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj0.Lock(ticks); @@ -3112,7 +2961,8 @@ public ITinySystem OnUpdate(Action system, ThreadingMode? thread obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Update); return sys; } @@ -3128,12 +2978,8 @@ public ITinySystem OnUpdate(Action system, ThreadingMode T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3148,7 +2994,8 @@ public ITinySystem OnUpdate(Action system, ThreadingMode obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Update); return sys; } @@ -3166,12 +3013,8 @@ public ITinySystem OnUpdate(Action system, Threa T1? obj1 = null; T2? obj2 = null; T3? obj3 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3189,7 +3032,8 @@ public ITinySystem OnUpdate(Action system, Threa obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Update); return sys; } @@ -3209,12 +3053,8 @@ public ITinySystem OnUpdate(Action syste T2? obj2 = null; T3? obj3 = null; T4? obj4 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3235,7 +3075,8 @@ public ITinySystem OnUpdate(Action syste obj4.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Update); return sys; } @@ -3257,12 +3098,8 @@ public ITinySystem OnUpdate(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3286,7 +3123,8 @@ public ITinySystem OnUpdate(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3342,7 +3176,8 @@ public ITinySystem OnUpdate(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3403,7 +3234,8 @@ public ITinySystem OnUpdate(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3469,7 +3297,8 @@ public ITinySystem OnUpdate(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3540,7 +3365,8 @@ public ITinySystem OnUpdate(Action(Action< T8? obj8 = null; T9? obj9 = null; T10? obj10 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3616,7 +3438,8 @@ public ITinySystem OnUpdate(Action< obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Update); return sys; } @@ -3650,12 +3473,8 @@ public ITinySystem OnUpdate(Ac T9? obj9 = null; T10? obj10 = null; T11? obj11 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3697,7 +3516,8 @@ public ITinySystem OnUpdate(Ac obj11.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.Update, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Update); return sys; } @@ -3733,12 +3553,8 @@ public ITinySystem OnUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3783,7 +3599,8 @@ public ITinySystem OnUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3874,7 +3687,8 @@ public ITinySystem OnUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -3970,7 +3780,8 @@ public ITinySystem OnUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0|| obj15?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4071,7 +3878,8 @@ public ITinySystem OnUpdate(Action system, ThreadingMode? threading threadingType = ThreadingExecutionMode; T0? obj0 = null; - var checkInuse = () => obj0?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj0.Lock(ticks); args.BeginDeferred(); @@ -4097,7 +3901,8 @@ public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threading obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.AfterUpdate); return sys; } @@ -4111,12 +3916,8 @@ public ITinySystem OnAfterUpdate(Action system, ThreadingMode? t T0? obj0 = null; T1? obj1 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj0.Lock(ticks); @@ -4128,7 +3929,8 @@ public ITinySystem OnAfterUpdate(Action system, ThreadingMode? t obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.AfterUpdate); return sys; } @@ -4144,12 +3946,8 @@ public ITinySystem OnAfterUpdate(Action system, Threadin T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4164,7 +3962,8 @@ public ITinySystem OnAfterUpdate(Action system, Threadin obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.AfterUpdate); return sys; } @@ -4182,12 +3981,8 @@ public ITinySystem OnAfterUpdate(Action system, T1? obj1 = null; T2? obj2 = null; T3? obj3 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4205,7 +4000,8 @@ public ITinySystem OnAfterUpdate(Action system, obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.AfterUpdate); return sys; } @@ -4225,12 +4021,8 @@ public ITinySystem OnAfterUpdate(Action T2? obj2 = null; T3? obj3 = null; T4? obj4 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4251,7 +4043,8 @@ public ITinySystem OnAfterUpdate(Action obj4.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.AfterUpdate); return sys; } @@ -4273,12 +4066,8 @@ public ITinySystem OnAfterUpdate(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4302,7 +4091,8 @@ public ITinySystem OnAfterUpdate(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4358,7 +4144,8 @@ public ITinySystem OnAfterUpdate(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4419,7 +4202,8 @@ public ITinySystem OnAfterUpdate(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4485,7 +4265,8 @@ public ITinySystem OnAfterUpdate(Action(Action< T7? obj7 = null; T8? obj8 = null; T9? obj9 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4556,7 +4333,8 @@ public ITinySystem OnAfterUpdate(Action< obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.AfterUpdate); return sys; } @@ -4588,12 +4366,8 @@ public ITinySystem OnAfterUpdate(Ac T8? obj8 = null; T9? obj9 = null; T10? obj10 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4632,7 +4406,8 @@ public ITinySystem OnAfterUpdate(Ac obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.AfterUpdate, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.AfterUpdate); return sys; } @@ -4666,12 +4441,8 @@ public ITinySystem OnAfterUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4713,7 +4484,8 @@ public ITinySystem OnAfterUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4799,7 +4567,8 @@ public ITinySystem OnAfterUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4890,7 +4655,8 @@ public ITinySystem OnAfterUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -4986,7 +4748,8 @@ public ITinySystem OnAfterUpdate obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0|| obj15?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5087,7 +4846,8 @@ public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingTyp threadingType = ThreadingExecutionMode; T0? obj0 = null; - var checkInuse = () => obj0?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj0.Lock(ticks); args.BeginDeferred(); @@ -5113,7 +4869,8 @@ public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingTyp obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameEnd); return sys; } @@ -5127,12 +4884,8 @@ public ITinySystem OnFrameEnd(Action system, ThreadingMode? thre T0? obj0 = null; T1? obj1 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj0.Lock(ticks); @@ -5144,7 +4897,8 @@ public ITinySystem OnFrameEnd(Action system, ThreadingMode? thre obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameEnd); return sys; } @@ -5160,12 +4914,8 @@ public ITinySystem OnFrameEnd(Action system, ThreadingMo T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5180,7 +4930,8 @@ public ITinySystem OnFrameEnd(Action system, ThreadingMo obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameEnd); return sys; } @@ -5198,12 +4949,8 @@ public ITinySystem OnFrameEnd(Action system, Thr T1? obj1 = null; T2? obj2 = null; T3? obj3 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5221,7 +4968,8 @@ public ITinySystem OnFrameEnd(Action system, Thr obj3.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameEnd); return sys; } @@ -5241,12 +4989,8 @@ public ITinySystem OnFrameEnd(Action sys T2? obj2 = null; T3? obj3 = null; T4? obj4 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5267,7 +5011,8 @@ public ITinySystem OnFrameEnd(Action sys obj4.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameEnd); return sys; } @@ -5289,12 +5034,8 @@ public ITinySystem OnFrameEnd(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5318,7 +5059,8 @@ public ITinySystem OnFrameEnd(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5374,7 +5112,8 @@ public ITinySystem OnFrameEnd(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5435,7 +5170,8 @@ public ITinySystem OnFrameEnd(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5501,7 +5233,8 @@ public ITinySystem OnFrameEnd(Action(Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5572,7 +5301,8 @@ public ITinySystem OnFrameEnd(Action(Actio T8? obj8 = null; T9? obj9 = null; T10? obj10 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5648,7 +5374,8 @@ public ITinySystem OnFrameEnd(Actio obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameEnd); return sys; } @@ -5682,12 +5409,8 @@ public ITinySystem OnFrameEnd( T9? obj9 = null; T10? obj10 = null; T11? obj11 = null; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5729,7 +5452,8 @@ public ITinySystem OnFrameEnd( obj11.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.FrameEnd, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameEnd); return sys; } @@ -5765,12 +5489,8 @@ public ITinySystem OnFrameEnd obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5815,7 +5535,8 @@ public ITinySystem OnFrameEnd obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -5906,7 +5623,8 @@ public ITinySystem OnFrameEnd obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -6002,7 +5716,8 @@ public ITinySystem OnFrameEnd obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0|| obj15?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -6103,115 +5814,110 @@ public ITinySystem OnFrameEnd - { - if (runIf?.Invoke(ticks, args) ?? true) - { - system(); - return true; - } - return false; - }, () => false, Stages.Startup, threadingType); + var fn = (World args, SystemTicks ticks) => + { + system(); + return true; + }; + + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Startup); return sys; } - public ITinySystem OnFrameStart(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnFrameStart(Action system, ThreadingMode? threadingType = null) { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => - { - if (runIf?.Invoke(ticks, args) ?? true) - { - system(); - return true; - } - return false; - }, () => false, Stages.FrameStart, threadingType); + var fn = (World args, SystemTicks ticks) => + { + system(); + return true; + }; + + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameStart); return sys; } - public ITinySystem OnBeforeUpdate(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType = null) { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => - { - if (runIf?.Invoke(ticks, args) ?? true) - { - system(); - return true; - } - return false; - }, () => false, Stages.BeforeUpdate, threadingType); + var fn = (World args, SystemTicks ticks) => + { + system(); + return true; + }; + + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.BeforeUpdate); return sys; } - public ITinySystem OnUpdate(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnUpdate(Action system, ThreadingMode? threadingType = null) { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => - { - if (runIf?.Invoke(ticks, args) ?? true) - { - system(); - return true; - } - return false; - }, () => false, Stages.Update, threadingType); + var fn = (World args, SystemTicks ticks) => + { + system(); + return true; + }; + + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.Update); return sys; } - public ITinySystem OnAfterUpdate(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingType = null) { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => - { - if (runIf?.Invoke(ticks, args) ?? true) - { - system(); - return true; - } - return false; - }, () => false, Stages.AfterUpdate, threadingType); + var fn = (World args, SystemTicks ticks) => + { + system(); + return true; + }; + + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.AfterUpdate); return sys; } - public ITinySystem OnFrameEnd(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnFrameEnd(Action system, ThreadingMode? threadingType = null) { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => - { - if (runIf?.Invoke(ticks, args) ?? true) - { - system(); - return true; - } - return false; - }, () => false, Stages.FrameEnd, threadingType); + var fn = (World args, SystemTicks ticks) => + { + system(); + return true; + }; + + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.FrameEnd); return sys; } diff --git a/src/TinyEcs.Systems.StageSpecific.tt b/src/TinyEcs.Systems.StageSpecific.tt index dcfcadb8..a1e9c0d0 100644 --- a/src/TinyEcs.Systems.StageSpecific.tt +++ b/src/TinyEcs.Systems.StageSpecific.tt @@ -57,7 +57,6 @@ namespace TinyEcs var objsLock = GenerateSequence(i + 1, "\n\t\t\t\t", j => $"obj{j}.Lock(ticks);"); var objsUnlock = GenerateSequence(i + 1, "\n\t\t\t\t", j => $"obj{j}.Unlock();"); var systemCall = GenerateSequence(i + 1, ", ", j => $"obj{j}"); - var objsCheckInuse = GenerateSequence(i + 1, " ", j => $"obj{j}?.UseIndex != 0" + (j < i ? "||" : "")); #> public ITinySystem <#= stage.MethodName #><<#= genericsArgs #>>(Action<<#= genericsArgs #>> system, ThreadingMode? threadingType = null) <#= genericsArgsWhere #> @@ -66,12 +65,8 @@ namespace TinyEcs threadingType = ThreadingExecutionMode; <#= objs #> - var checkInuse = () => <#= objsCheckInuse #>; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - <#= objsGen #> <#= objsLock #> args.BeginDeferred(); @@ -80,7 +75,8 @@ namespace TinyEcs <#= objsUnlock #> return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.<#= stage.StageName #>, threadingType.Value); + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.<#= stage.StageName #>); return sys; } @@ -93,20 +89,19 @@ namespace TinyEcs foreach (var stage in stageNames) { #> - public ITinySystem <#= stage.MethodName #>(Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem <#= stage.MethodName #>(Action system, ThreadingMode? threadingType = null) { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; - var sys = new FuncSystem(_world, (ticks, args, runIf) => - { - if (runIf?.Invoke(ticks, args) ?? true) - { - system(); - return true; - } - return false; - }, () => false, Stages.<#= stage.StageName #>, threadingType); + var fn = (World args, SystemTicks ticks) => + { + system(); + return true; + }; + + var sys = new TinyDelegateSystem(fn); + sys.Configuration.ThreadingMode = threadingType; Add(sys, Stages.<#= stage.StageName #>); return sys; } diff --git a/src/TinyEcs.Systems.StateHandlers.g.cs b/src/TinyEcs.Systems.StateHandlers.g.cs index edb9f721..5d5dc3d2 100644 --- a/src/TinyEcs.Systems.StateHandlers.g.cs +++ b/src/TinyEcs.Systems.StateHandlers.g.cs @@ -12,21 +12,17 @@ namespace TinyEcs #if NET9_0_OR_GREATER public partial class Scheduler { - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj0.Lock(ticks); args.BeginDeferred(); @@ -35,27 +31,24 @@ public ITinySystem OnEnter(TState st, Action system, ThreadingMo obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj0.Lock(ticks); args.BeginDeferred(); @@ -64,29 +57,26 @@ public ITinySystem OnExit(TState st, Action system, ThreadingMod obj0.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; T1? obj1 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj0.Lock(ticks); @@ -98,29 +88,26 @@ public ITinySystem OnEnter(TState st, Action system, Thr obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; T1? obj1 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj0.Lock(ticks); @@ -132,31 +119,28 @@ public ITinySystem OnExit(TState st, Action system, Thre obj1.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -171,31 +155,28 @@ public ITinySystem OnEnter(TState st, Action sys obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; T1? obj1 = null; T2? obj2 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -210,20 +191,21 @@ public ITinySystem OnExit(TState st, Action syst obj2.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam where T3 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -231,12 +213,8 @@ public ITinySystem OnEnter(TState st, Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -254,20 +232,21 @@ public ITinySystem OnEnter(TState st, Action state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam where T2 : class, ISystemParam, IIntoSystemParam where T3 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -275,12 +254,8 @@ public ITinySystem OnExit(TState st, Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -298,13 +273,14 @@ public ITinySystem OnExit(TState st, Action state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -312,7 +288,7 @@ public ITinySystem OnEnter(TState st, Action, IIntoSystemParam where T4 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -321,12 +297,8 @@ public ITinySystem OnEnter(TState st, Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -347,13 +319,14 @@ public ITinySystem OnEnter(TState st, Action state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -361,7 +334,7 @@ public ITinySystem OnExit(TState st, Action, IIntoSystemParam where T4 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -370,12 +343,8 @@ public ITinySystem OnExit(TState st, Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -396,13 +365,14 @@ public ITinySystem OnExit(TState st, Action state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -411,7 +381,7 @@ public ITinySystem OnEnter(TState st, Action, IIntoSystemParam where T5 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -421,12 +391,8 @@ public ITinySystem OnEnter(TState st, Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -450,13 +416,14 @@ public ITinySystem OnEnter(TState st, Action state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -465,7 +432,7 @@ public ITinySystem OnExit(TState st, Action, IIntoSystemParam where T5 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -475,12 +442,8 @@ public ITinySystem OnExit(TState st, Action obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -504,13 +467,14 @@ public ITinySystem OnExit(TState st, Action state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -520,7 +484,7 @@ public ITinySystem OnEnter(TState st, Action where T5 : class, ISystemParam, IIntoSystemParam where T6 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -531,12 +495,8 @@ public ITinySystem OnEnter(TState st, Action T5? obj5 = null; T6? obj6 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -563,13 +523,14 @@ public ITinySystem OnEnter(TState st, Action obj6.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -579,7 +540,7 @@ public ITinySystem OnExit(TState st, Action< where T5 : class, ISystemParam, IIntoSystemParam where T6 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -590,12 +551,8 @@ public ITinySystem OnExit(TState st, Action< T5? obj5 = null; T6? obj6 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -622,13 +579,14 @@ public ITinySystem OnExit(TState st, Action< obj6.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -639,7 +597,7 @@ public ITinySystem OnEnter(TState st, Ac where T6 : class, ISystemParam, IIntoSystemParam where T7 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -651,12 +609,8 @@ public ITinySystem OnEnter(TState st, Ac T6? obj6 = null; T7? obj7 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -686,13 +640,14 @@ public ITinySystem OnEnter(TState st, Ac obj7.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -703,7 +658,7 @@ public ITinySystem OnExit(TState st, Act where T6 : class, ISystemParam, IIntoSystemParam where T7 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -715,12 +670,8 @@ public ITinySystem OnExit(TState st, Act T6? obj6 = null; T7? obj7 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -750,13 +701,14 @@ public ITinySystem OnExit(TState st, Act obj7.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -768,7 +720,7 @@ public ITinySystem OnEnter(TState st where T7 : class, ISystemParam, IIntoSystemParam where T8 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -781,12 +733,8 @@ public ITinySystem OnEnter(TState st T7? obj7 = null; T8? obj8 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -819,13 +767,14 @@ public ITinySystem OnEnter(TState st obj8.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -837,7 +786,7 @@ public ITinySystem OnExit(TState st, where T7 : class, ISystemParam, IIntoSystemParam where T8 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -850,12 +799,8 @@ public ITinySystem OnExit(TState st, T7? obj7 = null; T8? obj8 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -888,13 +833,14 @@ public ITinySystem OnExit(TState st, obj8.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -907,7 +853,7 @@ public ITinySystem OnEnter(TStat where T8 : class, ISystemParam, IIntoSystemParam where T9 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -921,12 +867,8 @@ public ITinySystem OnEnter(TStat T8? obj8 = null; T9? obj9 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -962,13 +904,14 @@ public ITinySystem OnEnter(TStat obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -981,7 +924,7 @@ public ITinySystem OnExit(TState where T8 : class, ISystemParam, IIntoSystemParam where T9 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -995,12 +938,8 @@ public ITinySystem OnExit(TState T8? obj8 = null; T9? obj9 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1036,13 +975,14 @@ public ITinySystem OnExit(TState obj9.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1056,7 +996,7 @@ public ITinySystem OnEnter( where T9 : class, ISystemParam, IIntoSystemParam where T10 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -1071,12 +1011,8 @@ public ITinySystem OnEnter( T9? obj9 = null; T10? obj10 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1115,13 +1051,14 @@ public ITinySystem OnEnter( obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1135,7 +1072,7 @@ public ITinySystem OnExit(T where T9 : class, ISystemParam, IIntoSystemParam where T10 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -1150,12 +1087,8 @@ public ITinySystem OnExit(T T9? obj9 = null; T10? obj10 = null; var stateChangeId = -1; - var checkInuse = () => obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1194,13 +1127,14 @@ public ITinySystem OnExit(T obj10.Unlock(); return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1215,7 +1149,7 @@ public ITinySystem OnEnter, IIntoSystemParam where T11 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -1231,12 +1165,8 @@ public ITinySystem OnEnter obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1278,13 +1208,14 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1299,7 +1230,7 @@ public ITinySystem OnExit, IIntoSystemParam where T11 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -1315,12 +1246,8 @@ public ITinySystem OnExit obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1362,13 +1289,14 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1384,7 +1312,7 @@ public ITinySystem OnEnter, IIntoSystemParam where T12 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -1401,12 +1329,8 @@ public ITinySystem OnEnter obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1451,13 +1375,14 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1473,7 +1398,7 @@ public ITinySystem OnExit, IIntoSystemParam where T12 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -1490,12 +1415,8 @@ public ITinySystem OnExit obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1540,13 +1461,14 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1563,7 +1485,7 @@ public ITinySystem OnEnter, IIntoSystemParam where T13 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -1581,12 +1503,8 @@ public ITinySystem OnEnter obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1634,13 +1552,14 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1657,7 +1576,7 @@ public ITinySystem OnExit, IIntoSystemParam where T13 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -1675,12 +1594,8 @@ public ITinySystem OnExit obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1728,13 +1643,14 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1752,7 +1668,7 @@ public ITinySystem OnEnter, IIntoSystemParam where T14 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -1771,12 +1687,8 @@ public ITinySystem OnEnter obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1827,13 +1739,14 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1851,7 +1764,7 @@ public ITinySystem OnExit, IIntoSystemParam where T14 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -1870,12 +1783,8 @@ public ITinySystem OnExit obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -1926,13 +1835,14 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; } - public ITinySystem OnEnter(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -1951,7 +1861,7 @@ public ITinySystem OnEnter, IIntoSystemParam where T15 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -1971,12 +1881,8 @@ public ITinySystem OnEnter obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0|| obj15?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2030,13 +1936,14 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit(TState st, Action system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit(TState st, Action system, ThreadingMode? threadingType = null) where TState : struct, Enum where T0 : class, ISystemParam, IIntoSystemParam where T1 : class, ISystemParam, IIntoSystemParam @@ -2055,7 +1962,7 @@ public ITinySystem OnExit, IIntoSystemParam where T15 : class, ISystemParam, IIntoSystemParam { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; T0? obj0 = null; @@ -2075,12 +1982,8 @@ public ITinySystem OnExit obj0?.UseIndex != 0|| obj1?.UseIndex != 0|| obj2?.UseIndex != 0|| obj3?.UseIndex != 0|| obj4?.UseIndex != 0|| obj5?.UseIndex != 0|| obj6?.UseIndex != 0|| obj7?.UseIndex != 0|| obj8?.UseIndex != 0|| obj9?.UseIndex != 0|| obj10?.UseIndex != 0|| obj11?.UseIndex != 0|| obj12?.UseIndex != 0|| obj13?.UseIndex != 0|| obj14?.UseIndex != 0|| obj15?.UseIndex != 0; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - obj0 ??= (T0)T0.Generate(args); obj1 ??= (T1)T1.Generate(args); obj2 ??= (T2)T2.Generate(args); @@ -2134,7 +2037,8 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; diff --git a/src/TinyEcs.Systems.StateHandlers.tt b/src/TinyEcs.Systems.StateHandlers.tt index cb53c7ec..ba7db8e3 100644 --- a/src/TinyEcs.Systems.StateHandlers.tt +++ b/src/TinyEcs.Systems.StateHandlers.tt @@ -45,23 +45,18 @@ namespace TinyEcs var objsLock = GenerateSequence(i + 1, "\n\t\t\t\t", j => $"obj{j}.Lock(ticks);"); var objsUnlock = GenerateSequence(i + 1, "\n\t\t\t\t", j => $"obj{j}.Unlock();"); var systemCall = GenerateSequence(i + 1, ", ", j => $"obj{j}"); - var objsCheckInuse = GenerateSequence(i + 1, " ", j => $"obj{j}?.UseIndex != 0" + (j < i ? "||" : "")); #> - public ITinySystem OnEnter>(TState st, Action<<#= genericsArgs #>> system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnEnter>(TState st, Action<<#= genericsArgs #>> system, ThreadingMode? threadingType = null) where TState : struct, Enum <#= genericsArgsWhere #> { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; <#= objs #> var stateChangeId = -1; - var checkInuse = () => <#= objsCheckInuse #>; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - <#= objsGen #> <#= objsLock #> args.BeginDeferred(); @@ -70,27 +65,24 @@ namespace TinyEcs <#= objsUnlock #> return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnEnter, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; } - public ITinySystem OnExit>(TState st, Action<<#= genericsArgs #>> system, ThreadingMode threadingType = ThreadingMode.Auto) + public ITinySystem OnExit>(TState st, Action<<#= genericsArgs #>> system, ThreadingMode? threadingType = null) where TState : struct, Enum <#= genericsArgsWhere #> { - if (threadingType == ThreadingMode.Auto) + if (!threadingType.HasValue) threadingType = ThreadingExecutionMode; <#= objs #> var stateChangeId = -1; - var checkInuse = () => <#= objsCheckInuse #>; - var fn = (SystemTicks ticks, World args, Func runIf) => + var fn = (World args, SystemTicks ticks) => { - if (runIf != null && !runIf.Invoke(ticks, args)) - return false; - <#= objsGen #> <#= objsLock #> args.BeginDeferred(); @@ -99,7 +91,8 @@ namespace TinyEcs <#= objsUnlock #> return true; }; - var sys = new FuncSystem(_world, fn, checkInuse, Stages.OnExit, threadingType) + var sys = new TinyDelegateSystem(fn) + { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; From 96d22ba5177d3a24804f74a34c602226b1f20d90 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Thu, 11 Sep 2025 18:34:02 +0200 Subject: [PATCH 26/33] + stages --- src/Bevy.cs | 272 ++++++++++++++++++++----- src/TinyEcs.QueryIteratorEach.g.cs | 2 +- src/TinyEcs.Systems.DataAndFilter.g.cs | 2 +- src/TinyEcs.Systems.Scheduler.g.cs | 16 -- src/TinyEcs.Systems.Scheduler.tt | 1 - src/TinyEcs.Systems.StateHandlers.g.cs | 64 +++--- src/TinyEcs.Systems.StateHandlers.tt | 4 +- 7 files changed, 261 insertions(+), 100 deletions(-) diff --git a/src/Bevy.cs b/src/Bevy.cs index 9846dcb0..51a25af2 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -5,6 +5,8 @@ namespace TinyEcs; // https://promethia-27.github.io/dependency_injection_like_bevy_from_scratch/introductions.html #if NET9_0_OR_GREATER + +[Obsolete("Use the Stage class instead.")] public enum Stages { Startup, @@ -31,14 +33,147 @@ public sealed class SystemTicks public uint ThisRun { get; set; } } +public record Stage(string Name, bool RunOnce) +{ + public static readonly Stage Startup = new(nameof(Startup), true); + public static readonly Stage FrameStart = new(nameof(FrameStart), false); + public static readonly Stage BeforeUpdate = new(nameof(BeforeUpdate), false); + public static readonly Stage Update = new(nameof(Update), false); + public static readonly Stage AfterUpdate = new(nameof(AfterUpdate), false); + public static readonly Stage FrameEnd = new(nameof(FrameEnd), false); + public static readonly Stage OnEnter = new(nameof(OnEnter), false); + public static readonly Stage OnExit = new(nameof(OnExit), false); +} + +internal sealed class StageHandler(Stage stage) +{ + private bool _initialized; + public Stage Stage { get; } = stage; + public List Systems { get; } = new(); + + public void AddSystem(ITinySystem system) + => Systems.Add(system); + + public void Initialize(RunHandler runner, World world) + { + if (!_initialized) + { + runner.Initialize(Systems, world); + + _initialized = true; + } + } + + public void Run(RunHandler runner, World world, uint ticks) + { + runner.Run(Systems, world, ticks); + + if (Stage.RunOnce) + Systems.Clear(); + } +} + +public interface IRunHandler +{ + void Initialize(IEnumerable systems, World world); + void Run(IEnumerable systems, World world, uint ticks); +} + +public sealed class RunHandler : IRunHandler +{ + public void Initialize(IEnumerable systems, World world) + { + foreach (var sys in systems) + sys.Initialize(world); + } + + public void Run(IEnumerable systems, World world, uint ticks) + { + foreach (var system in systems) + system.ExecuteOnReady(world, ticks); + } +} + +internal sealed class StageContainer +{ + public List Stages { get; } = new(); + public Dictionary StageMap { get; } = new(); + + public Stage Get(string name) + { + if (StageMap.TryGetValue(name, out var handler)) + return handler.Stage; + throw new InvalidOperationException($"Stage '{name}' not found."); + } + + internal void Add(Stage stage) + { + if (!Contains(stage)) + { + var handler = new StageHandler(stage); + Stages.Add(handler); + StageMap.Add(stage.Name, handler); + } + else + { + throw new InvalidOperationException($"Stage '{stage.Name}' already exists."); + } + } + + public void AddBeforeOf(Stage parent, Stage stage) + { + if (!Contains(parent)) + throw new InvalidOperationException($"Parent stage '{parent.Name}' not found."); + + if (Contains(stage)) + throw new InvalidOperationException($"Stage '{stage.Name}' already exists."); + + var index = Stages.FindIndex(s => s.Stage == parent); + if (index == -1) + throw new InvalidOperationException($"Parent stage '{parent.Name}' not found."); + + var handler = new StageHandler(stage); + Stages.Insert(index, handler); + StageMap.Add(stage.Name, handler); + } + + public void AddAfterOf(Stage parent, Stage stage) + { + if (!Contains(parent)) + throw new InvalidOperationException($"Parent stage '{parent.Name}' not found."); + + if (Contains(stage)) + throw new InvalidOperationException($"Stage '{stage.Name}' already exists."); + + var index = Stages.FindIndex(s => s.Stage == parent); + if (index == -1) + throw new InvalidOperationException($"Parent stage '{parent.Name}' not found."); + + var handler = new StageHandler(stage); + Stages.Insert(index + 1, handler); + StageMap.Add(stage.Name, handler); + } + + public void AddSystem(ITinySystem system, Stage stage) + { + if (!StageMap.TryGetValue(stage.Name, out var handler)) + throw new InvalidOperationException($"Stage '{stage.Name}' not found."); + + handler.AddSystem(system); + } + + private bool Contains(Stage stage) + => StageMap.ContainsKey(stage.Name); +} + public partial class Scheduler { private readonly World _world; - private readonly LinkedList[] _systems = new LinkedList[(int)Stages.OnExit + 1]; private readonly List _singleThreads = new(); private readonly List _multiThreads = new(); private readonly Dictionary _events = new(); - + private readonly StageContainer _stageContainer = new(); + private readonly RunHandler _runHandler = new(); private bool _initialized; public Scheduler(World world, ThreadingMode threadingMode = ThreadingMode.Auto) @@ -46,8 +181,14 @@ public Scheduler(World world, ThreadingMode threadingMode = ThreadingMode.Auto) _world = world; ThreadingExecutionMode = threadingMode; - for (var i = 0; i < _systems.Length; ++i) - _systems[i] = new LinkedList(); + _stageContainer.Add(Stage.Startup); + _stageContainer.Add(Stage.OnExit); + _stageContainer.Add(Stage.OnEnter); + _stageContainer.Add(Stage.FrameStart); + _stageContainer.Add(Stage.BeforeUpdate); + _stageContainer.Add(Stage.Update); + _stageContainer.Add(Stage.AfterUpdate); + _stageContainer.Add(Stage.FrameEnd); AddSystemParam(world); AddSystemParam(new SchedulerState(this)); @@ -58,6 +199,22 @@ public Scheduler(World world, ThreadingMode threadingMode = ThreadingMode.Auto) public ThreadingMode ThreadingExecutionMode { get; } + public Stage AddStageBeforeOf(Stage parent, string name, bool oneShot = false) + { + var stage = new Stage(name, oneShot); + _stageContainer.AddBeforeOf(parent, stage); + + return stage; + } + + public Stage AddStageAfterOf(Stage parent, string name, bool oneShot = false) + { + var stage = new Stage(name, oneShot); + _stageContainer.AddAfterOf(parent, stage); + + return stage; + } + public void Run(Func checkForExitFn, Action? cleanupFn = null) { while (!checkForExitFn()) @@ -70,78 +227,99 @@ public void RunOnce() { if (!_initialized) { - foreach (var stage in _systems) - { - foreach (var system in stage) - { - system.Initialize(World); - } - } + foreach (var stageHandler in _stageContainer.Stages) + stageHandler.Initialize(_runHandler, _world); _initialized = true; } + var ticks = _world.Update(); foreach ((_, var ev) in _events) ev.Clear(); - RunStage(Stages.Startup, ticks); - _systems[(int)Stages.Startup].Clear(); + foreach (var stageHandler in _stageContainer.Stages) + stageHandler.Run(_runHandler, _world, ticks); + + // RunStage(Stages.Startup, ticks); + // _systems[(int)Stages.Startup].Clear(); - RunStage(Stages.OnExit, ticks); - RunStage(Stages.OnEnter, ticks); + // RunStage(Stages.OnExit, ticks); + // RunStage(Stages.OnEnter, ticks); - for (var stage = Stages.FrameStart; stage <= Stages.FrameEnd; stage += 1) - RunStage(stage, ticks); + // for (var stage = Stages.FrameStart; stage <= Stages.FrameEnd; stage += 1) + // RunStage(stage, ticks); } private void RunStage(Stages stage, uint ticks) { - _singleThreads.Clear(); - _multiThreads.Clear(); + // _singleThreads.Clear(); + // _multiThreads.Clear(); - var systems = _systems[(int)stage]; + // var systems = _systems[(int)stage]; - if (systems.Count == 0) - return; + // if (systems.Count == 0) + // return; - foreach (var sys in systems) - { - if (sys.ParamsAreLocked()) - { - _singleThreads.Add(sys); - } - else - { - _multiThreads.Add(sys); - } - } + // foreach (var sys in systems) + // { + // if (sys.ParamsAreLocked()) + // { + // _singleThreads.Add(sys); + // } + // else + // { + // _multiThreads.Add(sys); + // } + // } + + // var multithreading = _multiThreads; + // var singlethreading = _singleThreads; - var multithreading = _multiThreads; - var singlethreading = _singleThreads; + // if (multithreading.Count > 0) + // Parallel.ForEach(multithreading, s => s.ExecuteOnReady(World, ticks)); - if (multithreading.Count > 0) - Parallel.ForEach(multithreading, s => s.ExecuteOnReady(World, ticks)); + // foreach (var system in singlethreading) + // system.ExecuteOnReady(World, ticks); + } - foreach (var system in singlethreading) - system.ExecuteOnReady(World, ticks); + private static Stage GetStage(Stages stage) + { + return stage switch + { + Stages.Startup => Stage.Startup, + Stages.FrameStart => Stage.FrameStart, + Stages.BeforeUpdate => Stage.BeforeUpdate, + Stages.Update => Stage.Update, + Stages.AfterUpdate => Stage.AfterUpdate, + Stages.FrameEnd => Stage.FrameEnd, + Stages.OnEnter => Stage.OnEnter, + Stages.OnExit => Stage.OnExit, + _ => throw new ArgumentOutOfRangeException(nameof(stage), stage, null) + }; } private void Add(ITinySystem sys, Stages stage) { - sys.OrderConfiguration.Node = _systems[(int)stage].AddLast(sys); - sys.Configuration.Stage = stage; sys.Configuration.ThreadingMode ??= ThreadingExecutionMode; + _stageContainer.AddSystem(sys, GetStage(stage)); } - public Scheduler AddSystem2(Stages stage) where T : ITinySystem, new() + private void Add(ITinySystem sys, Stage stage) + { + sys.Configuration.ThreadingMode ??= ThreadingExecutionMode; + + _stageContainer.AddSystem(sys, stage); + } + + public Scheduler AddSystem2(Stage stage) where T : ITinySystem, new() { var system = new T(); Add(system, stage); return this; } - public Scheduler AddSystems2(Stages stage, params ITinySystem[] systems) + public Scheduler AddSystems2(Stage stage, params ITinySystem[] systems) { foreach (var system in systems) { @@ -179,7 +357,7 @@ public ITinySystem OnEnter(TState st, Action system, ThreadingMode? thre system(); return true; }) - { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType.Value } } + { Configuration = { ThreadingMode = threadingType.Value } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); @@ -200,7 +378,7 @@ public ITinySystem OnExit(TState st, Action system, ThreadingMode? threa system(); return true; }) - { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType.Value } } + { Configuration = { ThreadingMode = threadingType.Value } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); @@ -1294,7 +1472,6 @@ public sealed class SystemConfiguration { public HashSet Conditionals { get; } = []; public ThreadingMode? ThreadingMode { get; set; } - public Stages Stage { get; set; } = Stages.Update; } public sealed class SystemOrderConfiguration @@ -1399,7 +1576,8 @@ public bool ExecuteOnReady(World world, uint ticks) public bool ParamsAreLocked() { - return Configuration.ThreadingMode switch { + return Configuration.ThreadingMode switch + { ThreadingMode.Single => true, ThreadingMode.Multi => false, _ => SystemParams.Any(static p => p.UseIndex > 0) diff --git a/src/TinyEcs.QueryIteratorEach.g.cs b/src/TinyEcs.QueryIteratorEach.g.cs index 14bb32fe..6120af7b 100644 --- a/src/TinyEcs.QueryIteratorEach.g.cs +++ b/src/TinyEcs.QueryIteratorEach.g.cs @@ -1,4 +1,4 @@ -#pragma warning disable 1591 +#pragma warning disable 1591 #nullable enable using System; diff --git a/src/TinyEcs.Systems.DataAndFilter.g.cs b/src/TinyEcs.Systems.DataAndFilter.g.cs index df6c25c9..e00864e0 100644 --- a/src/TinyEcs.Systems.DataAndFilter.g.cs +++ b/src/TinyEcs.Systems.DataAndFilter.g.cs @@ -1,4 +1,4 @@ -#pragma warning disable 1591 +#pragma warning disable 1591 #nullable enable using System; diff --git a/src/TinyEcs.Systems.Scheduler.g.cs b/src/TinyEcs.Systems.Scheduler.g.cs index c5d22652..e6384acf 100644 --- a/src/TinyEcs.Systems.Scheduler.g.cs +++ b/src/TinyEcs.Systems.Scheduler.g.cs @@ -31,7 +31,6 @@ public ITinySystem AddSystem(Action system, Stages stage = Stages.Update }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType.Value; - sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -60,7 +59,6 @@ public ITinySystem AddSystem(Action system, Stages stage = Stage }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType.Value; - sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -94,7 +92,6 @@ public ITinySystem AddSystem(Action system, Stages stage }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType.Value; - sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -133,7 +130,6 @@ public ITinySystem AddSystem(Action system, Stag }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType.Value; - sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -177,7 +173,6 @@ public ITinySystem AddSystem(Action syst }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType.Value; - sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -226,7 +221,6 @@ public ITinySystem AddSystem(Action(Action(Action(Action(Action(Action }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType.Value; - sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -625,7 +614,6 @@ public ITinySystem AddSystem(A }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType.Value; - sys.Configuration.Stage = stage; Add(sys, stage); return sys; } @@ -709,7 +697,6 @@ public ITinySystem AddSystem(TState st, Action system, ThreadingMo return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -58,7 +58,7 @@ public ITinySystem OnExit(TState st, Action system, ThreadingMod return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -89,7 +89,7 @@ public ITinySystem OnEnter(TState st, Action system, Thr return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -120,7 +120,7 @@ public ITinySystem OnExit(TState st, Action system, Thre return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -156,7 +156,7 @@ public ITinySystem OnEnter(TState st, Action sys return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -192,7 +192,7 @@ public ITinySystem OnExit(TState st, Action syst return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -233,7 +233,7 @@ public ITinySystem OnEnter(TState st, Action state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -274,7 +274,7 @@ public ITinySystem OnExit(TState st, Action state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -320,7 +320,7 @@ public ITinySystem OnEnter(TState st, Action state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -366,7 +366,7 @@ public ITinySystem OnExit(TState st, Action state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -417,7 +417,7 @@ public ITinySystem OnEnter(TState st, Action state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -468,7 +468,7 @@ public ITinySystem OnExit(TState st, Action state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -524,7 +524,7 @@ public ITinySystem OnEnter(TState st, Action return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -580,7 +580,7 @@ public ITinySystem OnExit(TState st, Action< return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -641,7 +641,7 @@ public ITinySystem OnEnter(TState st, Ac return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -702,7 +702,7 @@ public ITinySystem OnExit(TState st, Act return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -768,7 +768,7 @@ public ITinySystem OnEnter(TState st return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -834,7 +834,7 @@ public ITinySystem OnExit(TState st, return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -905,7 +905,7 @@ public ITinySystem OnEnter(TStat return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -976,7 +976,7 @@ public ITinySystem OnExit(TState return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -1052,7 +1052,7 @@ public ITinySystem OnEnter( return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -1128,7 +1128,7 @@ public ITinySystem OnExit(T return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -1209,7 +1209,7 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -1290,7 +1290,7 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -1376,7 +1376,7 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -1462,7 +1462,7 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -1553,7 +1553,7 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -1644,7 +1644,7 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -1740,7 +1740,7 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -1836,7 +1836,7 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; @@ -1937,7 +1937,7 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -2038,7 +2038,7 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; diff --git a/src/TinyEcs.Systems.StateHandlers.tt b/src/TinyEcs.Systems.StateHandlers.tt index ba7db8e3..c121ee69 100644 --- a/src/TinyEcs.Systems.StateHandlers.tt +++ b/src/TinyEcs.Systems.StateHandlers.tt @@ -66,7 +66,7 @@ namespace TinyEcs return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnEnter, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); Add(sys, Stages.OnEnter); return sys; @@ -92,7 +92,7 @@ namespace TinyEcs return true; }; var sys = new TinyDelegateSystem(fn) - { Configuration = { Stage = Stages.OnExit, ThreadingMode = threadingType } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); Add(sys, Stages.OnExit); return sys; From 9a111b513e67328d110392648becda790cdc4060 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Fri, 12 Sep 2025 17:39:19 +0200 Subject: [PATCH 27/33] + again src gen --- src/Bevy.cs | 192 ++--- src/TinyEcs.Systems.StageSpecific.g.cs | 204 ++--- src/TinyEcs.Systems.StageSpecific.tt | 4 +- tools/TinyEcs.SourceGenerator/Program.cs | 700 +++++------------- .../TinySystemMethodGenerator.cs | 235 ------ 5 files changed, 400 insertions(+), 935 deletions(-) delete mode 100644 tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs diff --git a/src/Bevy.cs b/src/Bevy.cs index 51a25af2..5c5897c6 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -29,12 +29,15 @@ public enum ThreadingMode public sealed class SystemTicks { - public uint LastRun { get; set; } - public uint ThisRun { get; set; } + public uint LastRun { get; internal set; } + public uint ThisRun { get; internal set; } } -public record Stage(string Name, bool RunOnce) +public sealed class Stage(string name, bool runOnce) { + public string Name { get; } = name; + public bool RunOnce { get; } = runOnce; + public static readonly Stage Startup = new(nameof(Startup), true); public static readonly Stage FrameStart = new(nameof(FrameStart), false); public static readonly Stage BeforeUpdate = new(nameof(BeforeUpdate), false); @@ -48,8 +51,10 @@ public record Stage(string Name, bool RunOnce) internal sealed class StageHandler(Stage stage) { private bool _initialized; + public Stage Stage { get; } = stage; - public List Systems { get; } = new(); + internal List Systems { get; } = []; + public void AddSystem(ITinySystem system) => Systems.Add(system); @@ -73,14 +78,19 @@ public void Run(RunHandler runner, World world, uint ticks) } } -public interface IRunHandler +internal interface IRunHandler { void Initialize(IEnumerable systems, World world); void Run(IEnumerable systems, World world, uint ticks); } -public sealed class RunHandler : IRunHandler +internal sealed class RunHandler : IRunHandler { + private readonly List _singleThreads = new(); + private readonly List _multiThreads = new(); + + private static readonly int ProcessorCount = Environment.ProcessorCount; + public void Initialize(IEnumerable systems, World world) { foreach (var sys in systems) @@ -89,15 +99,39 @@ public void Initialize(IEnumerable systems, World world) public void Run(IEnumerable systems, World world, uint ticks) { - foreach (var system in systems) + _singleThreads.Clear(); + _multiThreads.Clear(); + + foreach (var sys in systems) + { + if (ProcessorCount <= 1 || sys.ParamsAreLocked()) + { + _singleThreads.Add(sys); + } + else + { + _multiThreads.Add(sys); + } + } + + if (_multiThreads.Count != 0) + Parallel.ForEach(_multiThreads, system => system.ExecuteOnReady(world, ticks)); + + foreach (var system in _singleThreads) system.ExecuteOnReady(world, ticks); } } +sealed class ThreadSchedule +{ + +} + + internal sealed class StageContainer { - public List Stages { get; } = new(); - public Dictionary StageMap { get; } = new(); + internal List Stages { get; } = new(); + internal Dictionary StageMap { get; } = new(); public Stage Get(string name) { @@ -169,8 +203,6 @@ private bool Contains(Stage stage) public partial class Scheduler { private readonly World _world; - private readonly List _singleThreads = new(); - private readonly List _multiThreads = new(); private readonly Dictionary _events = new(); private readonly StageContainer _stageContainer = new(); private readonly RunHandler _runHandler = new(); @@ -199,21 +231,6 @@ public Scheduler(World world, ThreadingMode threadingMode = ThreadingMode.Auto) public ThreadingMode ThreadingExecutionMode { get; } - public Stage AddStageBeforeOf(Stage parent, string name, bool oneShot = false) - { - var stage = new Stage(name, oneShot); - _stageContainer.AddBeforeOf(parent, stage); - - return stage; - } - - public Stage AddStageAfterOf(Stage parent, string name, bool oneShot = false) - { - var stage = new Stage(name, oneShot); - _stageContainer.AddAfterOf(parent, stage); - - return stage; - } public void Run(Func checkForExitFn, Action? cleanupFn = null) { @@ -240,91 +257,78 @@ public void RunOnce() foreach (var stageHandler in _stageContainer.Stages) stageHandler.Run(_runHandler, _world, ticks); - - // RunStage(Stages.Startup, ticks); - // _systems[(int)Stages.Startup].Clear(); - - // RunStage(Stages.OnExit, ticks); - // RunStage(Stages.OnEnter, ticks); - - // for (var stage = Stages.FrameStart; stage <= Stages.FrameEnd; stage += 1) - // RunStage(stage, ticks); } - private void RunStage(Stages stage, uint ticks) + public Stage AddStageBeforeOf(string parentStageName, string name, bool oneShot = false) { - // _singleThreads.Clear(); - // _multiThreads.Clear(); - - // var systems = _systems[(int)stage]; + var stage = _stageContainer.Get(parentStageName); + if (stage == null) + throw new InvalidOperationException($"Stage '{parentStageName}' not found."); - // if (systems.Count == 0) - // return; - - // foreach (var sys in systems) - // { - // if (sys.ParamsAreLocked()) - // { - // _singleThreads.Add(sys); - // } - // else - // { - // _multiThreads.Add(sys); - // } - // } - - // var multithreading = _multiThreads; - // var singlethreading = _singleThreads; + return AddStageBeforeOf(stage, name, oneShot); + } - // if (multithreading.Count > 0) - // Parallel.ForEach(multithreading, s => s.ExecuteOnReady(World, ticks)); + public Stage AddStageBeforeOf(Stage parent, string name, bool oneShot = false) + { + var stage = new Stage(name, oneShot); + _stageContainer.AddBeforeOf(parent, stage); - // foreach (var system in singlethreading) - // system.ExecuteOnReady(World, ticks); + return stage; } - private static Stage GetStage(Stages stage) + public Stage AddStageAfterOf(string parentStageName, string name, bool oneShot = false) { - return stage switch - { - Stages.Startup => Stage.Startup, - Stages.FrameStart => Stage.FrameStart, - Stages.BeforeUpdate => Stage.BeforeUpdate, - Stages.Update => Stage.Update, - Stages.AfterUpdate => Stage.AfterUpdate, - Stages.FrameEnd => Stage.FrameEnd, - Stages.OnEnter => Stage.OnEnter, - Stages.OnExit => Stage.OnExit, - _ => throw new ArgumentOutOfRangeException(nameof(stage), stage, null) - }; + var stage = _stageContainer.Get(parentStageName); + if (stage == null) + throw new InvalidOperationException($"Stage '{parentStageName}' not found."); + return AddStageAfterOf(stage, name, oneShot); } - private void Add(ITinySystem sys, Stages stage) + public Stage AddStageAfterOf(Stage parent, string name, bool oneShot = false) { - sys.Configuration.ThreadingMode ??= ThreadingExecutionMode; - _stageContainer.AddSystem(sys, GetStage(stage)); + var stage = new Stage(name, oneShot); + _stageContainer.AddAfterOf(parent, stage); + + return stage; } + private void Add(ITinySystem sys, Stages stage) + => Add(sys, GetStage(stage)); + private void Add(ITinySystem sys, Stage stage) { sys.Configuration.ThreadingMode ??= ThreadingExecutionMode; - _stageContainer.AddSystem(sys, stage); } - public Scheduler AddSystem2(Stage stage) where T : ITinySystem, new() + public Scheduler AddSystem(Stage stage) where T : ITinySystem, new() { var system = new T(); Add(system, stage); return this; } - public Scheduler AddSystems2(Stage stage, params ITinySystem[] systems) + public Scheduler AddSystem(string stageName) where T : ITinySystem, new() + { + var stage = _stageContainer.Get(stageName); + if (stage == null) + throw new InvalidOperationException($"Stage '{stageName}' not found."); + return AddSystem(stage); + } + + public Scheduler AddSystems(string stageName, params ITinySystem[] systems) + { + var stage = _stageContainer.Get(stageName); + if (stage == null) + throw new InvalidOperationException($"Stage '{stageName}' not found."); + return AddSystems(stage, systems); + } + + public Scheduler AddSystems(Stage stage, params ITinySystem[] systems) { foreach (var system in systems) - { Add(system, stage); - } + return this; } @@ -357,10 +361,10 @@ public ITinySystem OnEnter(TState st, Action system, ThreadingMode? thre system(); return true; }) - { Configuration = { ThreadingMode = threadingType.Value } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnter); return sys; } @@ -378,10 +382,10 @@ public ITinySystem OnExit(TState st, Action system, ThreadingMode? threa system(); return true; }) - { Configuration = { ThreadingMode = threadingType.Value } } + { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExit); return sys; } @@ -435,6 +439,22 @@ internal bool InState(T state) where T : struct, Enum return false; return _world.Entity>>().Get>>().Value.InState(state); } + + private static Stage GetStage(Stages stage) + { + return stage switch + { + Stages.Startup => Stage.Startup, + Stages.FrameStart => Stage.FrameStart, + Stages.BeforeUpdate => Stage.BeforeUpdate, + Stages.Update => Stage.Update, + Stages.AfterUpdate => Stage.AfterUpdate, + Stages.FrameEnd => Stage.FrameEnd, + Stages.OnEnter => Stage.OnEnter, + Stages.OnExit => Stage.OnExit, + _ => throw new ArgumentOutOfRangeException(nameof(stage), stage, null) + }; + } } internal struct Placeholder where T : ISystemParam { public T Value; } diff --git a/src/TinyEcs.Systems.StageSpecific.g.cs b/src/TinyEcs.Systems.StageSpecific.g.cs index 9ea3e200..f90a94ae 100644 --- a/src/TinyEcs.Systems.StageSpecific.g.cs +++ b/src/TinyEcs.Systems.StageSpecific.g.cs @@ -31,7 +31,7 @@ public ITinySystem OnStartup(Action system, ThreadingMode? threadingType }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Startup); + Add(sys, Stage.Startup); return sys; } @@ -59,7 +59,7 @@ public ITinySystem OnStartup(Action system, ThreadingMode? threa }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Startup); + Add(sys, Stage.Startup); return sys; } @@ -92,7 +92,7 @@ public ITinySystem OnStartup(Action system, ThreadingMod }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Startup); + Add(sys, Stage.Startup); return sys; } @@ -130,7 +130,7 @@ public ITinySystem OnStartup(Action system, Thre }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Startup); + Add(sys, Stage.Startup); return sys; } @@ -173,7 +173,7 @@ public ITinySystem OnStartup(Action syst }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Startup); + Add(sys, Stage.Startup); return sys; } @@ -221,7 +221,7 @@ public ITinySystem OnStartup(Action(Action(Action(Action(Action(Action }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Startup); + Add(sys, Stage.Startup); return sys; } @@ -614,7 +614,7 @@ public ITinySystem OnStartup(A }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Startup); + Add(sys, Stage.Startup); return sys; } @@ -697,7 +697,7 @@ public ITinySystem OnStartup(Action system, ThreadingMode? threadingT }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameStart); + Add(sys, Stage.FrameStart); return sys; } @@ -1027,7 +1027,7 @@ public ITinySystem OnFrameStart(Action system, ThreadingMode? th }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameStart); + Add(sys, Stage.FrameStart); return sys; } @@ -1060,7 +1060,7 @@ public ITinySystem OnFrameStart(Action system, Threading }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameStart); + Add(sys, Stage.FrameStart); return sys; } @@ -1098,7 +1098,7 @@ public ITinySystem OnFrameStart(Action system, T }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameStart); + Add(sys, Stage.FrameStart); return sys; } @@ -1141,7 +1141,7 @@ public ITinySystem OnFrameStart(Action s }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameStart); + Add(sys, Stage.FrameStart); return sys; } @@ -1189,7 +1189,7 @@ public ITinySystem OnFrameStart(Action(Action(Action(Action(Action(Act }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameStart); + Add(sys, Stage.FrameStart); return sys; } @@ -1582,7 +1582,7 @@ public ITinySystem OnFrameStart(Action system, ThreadingMode? threadin }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.BeforeUpdate); + Add(sys, Stage.BeforeUpdate); return sys; } @@ -1995,7 +1995,7 @@ public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.BeforeUpdate); + Add(sys, Stage.BeforeUpdate); return sys; } @@ -2028,7 +2028,7 @@ public ITinySystem OnBeforeUpdate(Action system, Threadi }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.BeforeUpdate); + Add(sys, Stage.BeforeUpdate); return sys; } @@ -2066,7 +2066,7 @@ public ITinySystem OnBeforeUpdate(Action system, }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.BeforeUpdate); + Add(sys, Stage.BeforeUpdate); return sys; } @@ -2109,7 +2109,7 @@ public ITinySystem OnBeforeUpdate(Action }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.BeforeUpdate); + Add(sys, Stage.BeforeUpdate); return sys; } @@ -2157,7 +2157,7 @@ public ITinySystem OnBeforeUpdate(Action(Action(Action(Action(Action }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.BeforeUpdate); + Add(sys, Stage.BeforeUpdate); return sys; } @@ -2472,7 +2472,7 @@ public ITinySystem OnBeforeUpdate(A }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.BeforeUpdate); + Add(sys, Stage.BeforeUpdate); return sys; } @@ -2550,7 +2550,7 @@ public ITinySystem OnBeforeUpdate(Action system, ThreadingMode? threadingType }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Update); + Add(sys, Stage.Update); return sys; } @@ -2963,7 +2963,7 @@ public ITinySystem OnUpdate(Action system, ThreadingMode? thread }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Update); + Add(sys, Stage.Update); return sys; } @@ -2996,7 +2996,7 @@ public ITinySystem OnUpdate(Action system, ThreadingMode }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Update); + Add(sys, Stage.Update); return sys; } @@ -3034,7 +3034,7 @@ public ITinySystem OnUpdate(Action system, Threa }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Update); + Add(sys, Stage.Update); return sys; } @@ -3077,7 +3077,7 @@ public ITinySystem OnUpdate(Action syste }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Update); + Add(sys, Stage.Update); return sys; } @@ -3125,7 +3125,7 @@ public ITinySystem OnUpdate(Action(Action(Action(Action(Action(Action< }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Update); + Add(sys, Stage.Update); return sys; } @@ -3518,7 +3518,7 @@ public ITinySystem OnUpdate(Ac }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.Update); + Add(sys, Stage.Update); return sys; } @@ -3601,7 +3601,7 @@ public ITinySystem OnUpdate(Action system, ThreadingMode? threading }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.AfterUpdate); + Add(sys, Stage.AfterUpdate); return sys; } @@ -3931,7 +3931,7 @@ public ITinySystem OnAfterUpdate(Action system, ThreadingMode? t }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.AfterUpdate); + Add(sys, Stage.AfterUpdate); return sys; } @@ -3964,7 +3964,7 @@ public ITinySystem OnAfterUpdate(Action system, Threadin }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.AfterUpdate); + Add(sys, Stage.AfterUpdate); return sys; } @@ -4002,7 +4002,7 @@ public ITinySystem OnAfterUpdate(Action system, }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.AfterUpdate); + Add(sys, Stage.AfterUpdate); return sys; } @@ -4045,7 +4045,7 @@ public ITinySystem OnAfterUpdate(Action }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.AfterUpdate); + Add(sys, Stage.AfterUpdate); return sys; } @@ -4093,7 +4093,7 @@ public ITinySystem OnAfterUpdate(Action(Action(Action(Action(Action< }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.AfterUpdate); + Add(sys, Stage.AfterUpdate); return sys; } @@ -4408,7 +4408,7 @@ public ITinySystem OnAfterUpdate(Ac }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.AfterUpdate); + Add(sys, Stage.AfterUpdate); return sys; } @@ -4486,7 +4486,7 @@ public ITinySystem OnAfterUpdate(Action system, ThreadingMode? threadingTyp }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameEnd); + Add(sys, Stage.FrameEnd); return sys; } @@ -4899,7 +4899,7 @@ public ITinySystem OnFrameEnd(Action system, ThreadingMode? thre }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameEnd); + Add(sys, Stage.FrameEnd); return sys; } @@ -4932,7 +4932,7 @@ public ITinySystem OnFrameEnd(Action system, ThreadingMo }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameEnd); + Add(sys, Stage.FrameEnd); return sys; } @@ -4970,7 +4970,7 @@ public ITinySystem OnFrameEnd(Action system, Thr }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameEnd); + Add(sys, Stage.FrameEnd); return sys; } @@ -5013,7 +5013,7 @@ public ITinySystem OnFrameEnd(Action sys }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameEnd); + Add(sys, Stage.FrameEnd); return sys; } @@ -5061,7 +5061,7 @@ public ITinySystem OnFrameEnd(Action(Action(Action(Action(Action(Actio }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameEnd); + Add(sys, Stage.FrameEnd); return sys; } @@ -5454,7 +5454,7 @@ public ITinySystem OnFrameEnd( }; var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.FrameEnd); + Add(sys, Stage.FrameEnd); return sys; } @@ -5537,7 +5537,7 @@ public ITinySystem OnFrameEnd); + Add(sys, Stage.<#= stage.StageName #>); return sys; } @@ -102,7 +102,7 @@ namespace TinyEcs var sys = new TinyDelegateSystem(fn); sys.Configuration.ThreadingMode = threadingType; - Add(sys, Stages.<#= stage.StageName #>); + Add(sys, Stage.<#= stage.StageName #>); return sys; } diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index aff67c38..8122332a 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -1,5 +1,6 @@ +using System; using System.Collections.Immutable; -using System.Diagnostics; +using System.Linq; using System.Text; using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.CSharp; @@ -12,544 +13,223 @@ namespace TinyEcs.SourceGenerator; [Generator] -public sealed class SourceGenerator : IIncrementalGenerator +public sealed class Program : IIncrementalGenerator { - public void Initialize(IncrementalGeneratorInitializationContext context) - { - RegisterPluginExtensionsGenerator(context); - RegisterTinySystemGenerator(context); - } - - #region Plugin Extensions Generator - private void RegisterPluginExtensionsGenerator(IncrementalGeneratorInitializationContext context) + public void Initialize(IncrementalGeneratorInitializationContext context) { - var pluginDeclarations = context.SyntaxProvider.CreateSyntaxProvider( - static (s, _) => s is TypeDeclarationSyntax { AttributeLists.Count: > 0 }, - static (ctx, _) => GetClassDeclarationSymbolIfAttributeOf(ctx, "TinyEcs.TinyPluginAttribute") - ).Where(static m => m is not null); + // Find IPlugin classes that contain methods with [TinySystem] attributes + var pluginClassesWithTinySystemMethods = context.SyntaxProvider.CreateSyntaxProvider( + static (s, _) => s is ClassDeclarationSyntax classDecl && + classDecl.Members.OfType() + .Any(m => m.AttributeLists.Count > 0), + static (ctx, _) => + { + var classDecl = (ClassDeclarationSyntax)ctx.Node; + var classSymbol = ctx.SemanticModel.GetDeclaredSymbol(classDecl) as INamedTypeSymbol; - var compilationAndClasses = context.CompilationProvider.Combine(pluginDeclarations.WithComparer(Comparer.Instance).Collect()); - context.RegisterSourceOutput(compilationAndClasses, (spc, source) => Generate(source.Left, source.Right, spc)); + if (classSymbol == null) return null; - var pluginDeclarations2 = context.SyntaxProvider.CreateSyntaxProvider( - static (s, _) => s is TypeDeclarationSyntax, - static (ctx, _) => (TypeDeclarationSyntax)ctx.Node - ).Where(static m => m is not null); + // Check if the class implements IPlugin + var implementsIPlugin = classSymbol.AllInterfaces.Any(i => + i.ToDisplayString() == "TinyEcs.IPlugin"); - var compilationAndClasses2 = context.CompilationProvider.Combine(pluginDeclarations2.WithComparer(Comparer.Instance).Collect()); - context.RegisterSourceOutput(compilationAndClasses2, (spc, source) => - { - var classes = source.Right; - var compilation = source.Left; + if (!implementsIPlugin) return null; + + // Find methods with [TinySystem] attribute + var tinySystemMethods = classSymbol.GetMembers() + .OfType() + .Where(m => m.GetAttributes().Any(attr => + attr.AttributeClass?.ToDisplayString() == "TinyEcs.TinySystemAttribute")) + .ToList(); - if (classes.IsDefaultOrEmpty) return; + if (!tinySystemMethods.Any()) return null; - var iPluginSymbol = compilation.GetTypeByMetadataName("TinyEcs.IPlugin"); - if (iPluginSymbol == null) return; + return new PluginClassInfo + { + ClassSymbol = classSymbol, + TinySystemMethods = tinySystemMethods + }; + } + ).Where(static info => info != null); - var hashDelegates = new HashSet<(string MethodName, string Parameters)>(); - var sb = new StringBuilder(); - var allowedMethodNames = new Dictionary - { - { "OnStartup2", "OnStartup" }, - { "OnFrameStart2", "OnFrameStart" }, - { "OnBeforeUpdate2", "OnBeforeUpdate" }, - { "OnUpdate2", "OnUpdate" }, - { "OnAfterUpdate2", "OnAfterUpdate" }, - { "OnFrameEnd2", "OnFrameEnd" }, - }; - - foreach (var cl in classes) + // Generate adapter classes for each method + context.RegisterSourceOutput( + pluginClassesWithTinySystemMethods.Collect(), + (spc, pluginClasses) => { - var semanticModel = compilation.GetSemanticModel(cl.SyntaxTree); + if (pluginClasses.IsDefaultOrEmpty) return; - if (ModelExtensions.GetDeclaredSymbol(semanticModel, cl) is not INamedTypeSymbol symbol) - continue; + foreach (var pluginClass in pluginClasses) + { + foreach (var method in pluginClass.TinySystemMethods) + { + GenerateAdapterClass(spc, pluginClass.ClassSymbol, method); + } + } + } + ); + } - var pluginInterfaces = symbol.AllInterfaces - .Where(s => SymbolEqualityComparer.Default.Equals(s, iPluginSymbol)) - .ToArray(); + private static void GenerateAdapterClass(SourceProductionContext context, INamedTypeSymbol pluginClass, IMethodSymbol method) + { + var adapterName = $"{method.Name}Adapter"; + var ns = pluginClass.ContainingNamespace.IsGlobalNamespace ? "" : pluginClass.ContainingNamespace.ToDisplayString(); - if (pluginInterfaces.Length == 0) - continue; + // Use fully-qualified type name (global::Namespace.Type) for instance/static references + var instanceTypeName = pluginClass.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); + // Validate method requirements for TinySystemAttribute + ValidateMethodRequirements(context, method); - var build = symbol.GetMembers("Build") - .OfType() - .SingleOrDefault(m => m.Parameters.Length == 1 && - m.Parameters[0].Type.ToDisplayString() == "TinyEcs.Scheduler"); + // Check if method returns bool (for conditional systems) + var returnsBool = method.ReturnType.SpecialType == SpecialType.System_Boolean; + var baseClass = returnsBool ? "TinyEcs.TinyConditionalSystem" : "TinyEcs.TinySystem"; - if (build == null) - continue; + // Get method parameters for dependency injection + var parameters = method.Parameters.ToList(); + var fieldDeclarations = new StringBuilder(); + var setupAssignments = new StringBuilder(); + var methodCallParameters = new StringBuilder(); - // Step 1: Get the syntax node for the method - if (build.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is not MethodDeclarationSyntax buildSyntax) - continue; + // Generate field declarations and setup assignments + for (int i = 0; i < parameters.Count; i++) + { + var param = parameters[i]; + var fieldName = $"_{param.Name}"; + var paramType = param.Type.ToDisplayString(); - var semanticModel2 = compilation.GetSemanticModel(buildSyntax.SyntaxTree); + fieldDeclarations.AppendLine($" private {paramType} {fieldName};"); + setupAssignments.AppendLine($" {fieldName} = builder.Add<{paramType}>();"); - // Step 2: Traverse the method body to find calls + if (i > 0) methodCallParameters.Append(", "); + methodCallParameters.Append(fieldName); + } - foreach (var methodName in allowedMethodNames.Keys) - { - var calls = GetMethodCalls(buildSyntax, semanticModel2, methodName); + // For non-static methods, add a field for the plugin instance + string instanceField = ""; + string ctor = ""; + string methodCall; - foreach (var invocation in calls) - { - ProcessCallInvocations(invocation, semanticModel2, methodName, hashDelegates); - } - } - } + // Use a single IndentedStringBuilder for all code blocks + var adapterClass = new IndentedStringBuilder(); - foreach ((var methodName, var paramList) in hashDelegates) - { - sb.AppendLine($@" - public static FuncSystem {methodName}(this Scheduler scheduler, {paramList} fn, ThreadingMode threading = ThreadingMode.Auto) {{ - return scheduler.{allowedMethodNames[methodName]}(fn, threading); - }}"); - } + if (!string.IsNullOrEmpty(ns)) + { + adapterClass.AppendLine($"namespace {ns}"); + adapterClass.AppendLine("{"); + adapterClass.IncrementIndent(); + } - var template = $@" - // This code is auto-generated. + // Make the adapter class sealed and partial + adapterClass.AppendLine($"public sealed partial class {adapterName} : {baseClass}"); + adapterClass.AppendLine("{"); + adapterClass.IncrementIndent(); - using TinyEcs; + // Fields + foreach (var line in fieldDeclarations.ToString().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)) + adapterClass.AppendLine(line); - public static partial class SchedulerExt {{ - {sb} - }} - "; + if (method.IsStatic) + { + // Use namespace-qualified type for static call + methodCall = $"{instanceTypeName}.{method.Name}({methodCallParameters})"; + } + else + { + // Adapter will accept the instance via constructor using namespace-qualified type + instanceField = $"private readonly {instanceTypeName} _instance;"; + ctor = $"public {adapterName}({instanceTypeName} instance) {{ _instance = instance; }}"; + methodCall = $"_instance.{method.Name}({methodCallParameters})"; + adapterClass.AppendLine(instanceField); + adapterClass.AppendLine(); + adapterClass.AppendLine(ctor); + } + + adapterClass.AppendLine(); + + // Setup method + adapterClass.AppendLine("protected override void Setup(TinyEcs.SystemParamBuilder builder)"); + adapterClass.AppendLine("{"); + adapterClass.IncrementIndent(); + foreach (var line in setupAssignments.ToString().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)) + adapterClass.AppendLine(line); + adapterClass.DecrementIndent(); + adapterClass.AppendLine("}"); + + adapterClass.AppendLine(); + + // Execute method + adapterClass.AppendLine("protected override bool Execute(TinyEcs.World world)"); + adapterClass.AppendLine("{"); + adapterClass.IncrementIndent(); + adapterClass.AppendLine("Lock();"); + adapterClass.AppendLine("world.BeginDeferred();"); + if (returnsBool) + { + adapterClass.AppendLine($"bool result = {methodCall};"); + adapterClass.AppendLine("world.EndDeferred();"); + adapterClass.AppendLine("Unlock();"); + adapterClass.AppendLine("return result;"); + } + else + { + adapterClass.AppendLine($"{methodCall};"); + adapterClass.AppendLine("world.EndDeferred();"); + adapterClass.AppendLine("Unlock();"); + adapterClass.AppendLine("return true;"); + } + adapterClass.DecrementIndent(); + adapterClass.AppendLine("}"); + + adapterClass.DecrementIndent(); + adapterClass.AppendLine("}"); + + if (!string.IsNullOrEmpty(ns)) + { + adapterClass.DecrementIndent(); + adapterClass.AppendLine("}"); + } - spc.AddSource($"SchedulerExt.g.cs", - CSharpSyntaxTree.ParseText(template).GetRoot().NormalizeWhitespace().ToFullString()); - }); + var sourceText = adapterClass.ToString(); + context.AddSource($"{adapterName}.g.cs", sourceText); } - #endregion - #region TinySystem Generator - private void RegisterTinySystemGenerator(IncrementalGeneratorInitializationContext context) + private static void ValidateMethodRequirements(SourceProductionContext context, IMethodSymbol method) { - var tinySystemClasses = context.SyntaxProvider.CreateSyntaxProvider( - static (s, _) => s is TypeDeclarationSyntax t && t.AttributeLists.Count > 0, - static (ctx, _) => - { - var typeSyntax = (TypeDeclarationSyntax)ctx.Node; - var typeSymbol = ctx.SemanticModel.GetDeclaredSymbol(typeSyntax); - if (typeSymbol is not INamedTypeSymbol namedTypeSymbol) return null; - if (!namedTypeSymbol.GetAttributes().Any(a => a.AttributeClass?.ToDisplayString() == "TinyEcs.TinySystemAttribute")) return null; - if (namedTypeSymbol.TypeKind != TypeKind.Class) return null; - return namedTypeSymbol; - } - ).Where(static t => t is not null); + var location = method.Locations.FirstOrDefault() ?? Location.None; - context.RegisterSourceOutput( - context.CompilationProvider.Combine(tinySystemClasses.Collect()), - (spc, source) => - { - var classes = source.Right; - if (classes.IsDefaultOrEmpty) return; + // Check if method is public + if (method.DeclaredAccessibility != Accessibility.Public) + { + var descriptor = new DiagnosticDescriptor( + "TINYECS001", + "TinySystem method must be public", + "Method '{0}' with [TinySystem] attribute must be public", + "TinyEcs", + DiagnosticSeverity.Error, + isEnabledByDefault: true); + + context.ReportDiagnostic(Diagnostic.Create(descriptor, location, method.Name)); + } + + // Check if method is static + // if (!method.IsStatic) + // { + // var descriptor = new DiagnosticDescriptor( + // "TINYECS002", + // "TinySystem method must be static", + // "Method '{0}' with [TinySystem] attribute must be static", + // "TinyEcs", + // DiagnosticSeverity.Error, + // isEnabledByDefault: true); + // + // context.ReportDiagnostic(Diagnostic.Create(descriptor, location, method.Name)); + // } + } - foreach (var classSymbolObj in classes) - { - if (classSymbolObj is { } classSymbol) - { - var className = classSymbol.Name.EndsWith("System") ? classSymbol.Name : classSymbol.Name + "System"; - var ns = classSymbol.ContainingNamespace.IsGlobalNamespace ? "" : classSymbol.ContainingNamespace.ToDisplayString(); - // Find the Execute method in the class - var executeMethodSymbol = classSymbol.GetMembers().OfType().FirstOrDefault(m => m.Name == "OnExecute" && !m.IsStatic); - if (executeMethodSymbol == null) continue; - - // Accept all parameters for injection - var validParams = executeMethodSymbol.Parameters.ToList(); - - // Declare private fields for each parameter - var fieldDecls = string.Join("\n", validParams.Select(p => $" private {p.Type.ToDisplayString()} _{p.Name};")); - - // Setup method: assign all fields - var setupAssignments = string.Join("\n", validParams.Select(p => $" _{p.Name} = builder.Add<{p.Type.ToDisplayString()}>();")); - var setupCode = $@" protected override void Setup(SystemParamBuilder builder) - {{ -{setupAssignments} - }}"; - - var onExecuteParams = string.Join(", ", validParams.Select(p => $"_{p.Name}")); - var executeOverride = $@" protected override bool Execute(World world) - {{ - Lock(); - world.BeginDeferred(); - OnExecute({onExecuteParams}); - world.EndDeferred(); - Unlock(); - return true; - }}"; - - // Only generate Setup, Execute, and OnExecute - var systemClass = classSymbol.ContainingNamespace.IsGlobalNamespace - ? $@"using TinyEcs; -public partial class {className} : TinySystem -{{ -{fieldDecls} -{setupCode} -{executeOverride} -}}" - : $@"using TinyEcs; -namespace {ns} -{{ - public partial class {className} : TinySystem - {{ -{fieldDecls} -{setupCode} -{executeOverride} - }} -}}"; - - var fileName = $"{className}.g.cs"; - spc.AddSource(fileName, systemClass); - } - } - } - ); + private class PluginClassInfo + { + public INamedTypeSymbol ClassSymbol { get; set; } + public List TinySystemMethods { get; set; } } - #endregion - - private static InvocationExpressionSyntax[] GetMethodCalls(MethodDeclarationSyntax buildSyntax, SemanticModel semanticModel, string name) - { - var calls = buildSyntax.DescendantNodes() - .OfType() - .Where(invocation => - { - // Get the expression being called - if (invocation.Expression is MemberAccessExpressionSyntax memberAccess) - { - var methodName = memberAccess.Name.Identifier.Text; - if (methodName != name) - return false; - - // Ensure the receiver is 'scheduler' - var symbolInfo = semanticModel.GetSymbolInfo(memberAccess.Expression).Symbol; - if (symbolInfo is IParameterSymbol parameter && - parameter.Name == "scheduler") - { - return true; - } - - // Optional: match by string name instead (less reliable) - // return memberAccess.Expression.ToString() == "scheduler"; - } - - return false; - }) - .ToArray(); - - return calls; - } - - private static void ProcessCallInvocations( - InvocationExpressionSyntax invocation, - SemanticModel semanticModel, - string methodName, - HashSet<(string ,string)> cacheDelegates - ) - { - var argument = invocation.ArgumentList.Arguments.FirstOrDefault()?.Expression; - if (argument is null) - return; - - var symbolInfo = semanticModel.GetSymbolInfo(argument); - var found = symbolInfo.Symbol ?? symbolInfo.CandidateSymbols.FirstOrDefault(); - - if (found is not IMethodSymbol method) - return; - - // if (!cache.Add(method.Name)) - // return; - - var j = string.Join(", ", method.Parameters.Select(s => s.Type.ToDisplayString())); - var act = $"{(method.Parameters.Any() ? $"Action<{j}>" : "Action")}"; - // var p = string.Join(", ", method.Parameters.Select(s => s.ToDisplayString())); - if (!cacheDelegates.Add((methodName, act))) - return; - - // var fileName = $"SchedulerExt_{found.Name}"; - - -// var template = $@" -// using TinyEcs; -// -// public static class {fileName} {{ -// public static void OnUpdate2(this Scheduler scheduler, {act} fn) -// {{ -// var system = scheduler.OnUpdate(fn); -// }} -// }} -// "; -// -// context.AddSource($"{fileName}.g.cs", -// CSharpSyntaxTree.ParseText(template).GetRoot().NormalizeWhitespace().ToFullString()); - } - - private static void ProcessLocalFunction(LocalFunctionStatementSyntax localFunc, INamedTypeSymbol pluginClass, GeneratorExecutionContext context) - { - var methodName = $"OnUpdate_{Guid.NewGuid():N}"; - - var parameters = localFunc.ParameterList.Parameters; - var body = localFunc.Body ?? SyntaxFactory.Block(SyntaxFactory.ExpressionStatement(localFunc.ExpressionBody!.Expression)); - - var methodSource = new StringBuilder(); - methodSource.AppendLine($"private static void {methodName}({string.Join(", ", parameters.Select(p => p.ToFullString()))})"); - methodSource.AppendLine(body.ToFullString()); - -// var ns = pluginClass.ContainingNamespace.ToDisplayString(); -// var className = pluginClass.Name; -// -// var fullSource = $@" -// namespace {ns} -// {{ -// public partial class {className} -// {{ -// {methodSource} -// }} -// }} -// "; -// -// context.AddSource($"{className}_{methodName}.g.cs", SourceText.From(fullSource, Encoding.UTF8)); - } - - - private static void ProcessLambda( - LambdaExpressionSyntax lambda, - InvocationExpressionSyntax originalInvocation, - INamedTypeSymbol pluginClassSymbol, - GeneratorExecutionContext context) - { - var parameters = lambda switch - { - SimpleLambdaExpressionSyntax simple => new[] { simple.Parameter }, - ParenthesizedLambdaExpressionSyntax complex => complex.ParameterList.Parameters.ToArray(), - _ => Array.Empty() - }; - - var lambdaBody = lambda.Body; - - var methodName = $"OnUpdate_{Guid.NewGuid().ToString("N")}"; - - var methodText = new StringBuilder(); - methodText.AppendLine($"private static void {methodName}({string.Join(", ", parameters.Select(p => p.ToFullString()))})"); - - if (lambdaBody is BlockSyntax block) - methodText.AppendLine(block.ToFullString()); - else if (lambdaBody is ExpressionSyntax expr) - methodText.AppendLine($"{{ return {expr.ToFullString()}; }}"); - -// // Create the partial class -// var ns = pluginClassSymbol.ContainingNamespace.ToDisplayString(); -// var className = pluginClassSymbol.Name; -// -// var fullSource = $@" -// namespace {ns} -// {{ -// public partial class {className} -// {{ -// {methodText} -// }} -// }} -// "; -// -// context.AddSource($"{className}_{methodName}.g.cs", SourceText.From(fullSource, Encoding.UTF8)); - - // Optionally: replace the lambda with the method name in a syntax rewriter - } - - - - private static TypeDeclarationSyntax? GetClassDeclarationSymbolIfAttributeOf(GeneratorSyntaxContext context, string name) - { - var enumDeclarationSyntax = (TypeDeclarationSyntax)context.Node; - - foreach (var attributeListSyntax in enumDeclarationSyntax.AttributeLists) - { - foreach (var attributeSyntax in attributeListSyntax.Attributes) - { - if (ModelExtensions.GetSymbolInfo(context.SemanticModel, attributeSyntax).Symbol is not IMethodSymbol attributeSymbol) continue; - - var attributeContainingTypeSymbol = attributeSymbol.ContainingType; - var fullName = attributeContainingTypeSymbol.ToDisplayString(); - - if (fullName != name) continue; - return enumDeclarationSyntax; - } - } - - return null; - } - - private void Generate(Compilation compilation, ImmutableArray classes, SourceProductionContext context) - { - if (classes.IsDefaultOrEmpty) return; - - foreach (var cl in classes) - { - INamedTypeSymbol? nameSymbol = null; - try - { - var semanticModel = compilation.GetSemanticModel(cl.SyntaxTree); - nameSymbol = ModelExtensions.GetDeclaredSymbol(semanticModel, cl) as INamedTypeSymbol; - } - catch - { - continue; - } - - if (nameSymbol == null) - continue; - - var allMethods = nameSymbol.GetMembers().OfType().ToList(); - var allSystems = allMethods.Where(s => s.GetAttributes() - .Any(k => k.AttributeClass.ToDisplayString() is "TinyEcs.TinySystemAttribute")) - .ToList(); - - var sbDeclarations = new StringBuilder(); - var sbSystemsOrder = new StringBuilder(); - var cache = new HashSet(); - foreach (var method in allSystems) - { - (var systems, var systemsOrder) = GenerateOne(method, allMethods, cache); - sbDeclarations.AppendLine(systems); - sbSystemsOrder.AppendLine(systemsOrder); - } - - var @namespace = nameSymbol.ContainingNamespace.ToString(); - var className = nameSymbol.Name.Substring(nameSymbol.Name.LastIndexOf('.') + 1); - var typeName = nameSymbol.TypeKind switch - { - TypeKind.Class => "class", - TypeKind.Struct => "struct", - _ => throw new InvalidOperationException($"Unsupported type kind: {nameSymbol.TypeKind}") - }; - - var template = $@" - using TinyEcs; - {(nameSymbol.ContainingNamespace.IsGlobalNamespace ? "" : $"namespace {@namespace} {{")} - {(nameSymbol.IsStatic ? "static" : "")} partial {typeName} {className} : IPlugin {{ - void IPlugin.Build(Scheduler scheduler) - {{ - this.Build(scheduler); - - {sbDeclarations} - {sbSystemsOrder} - }} - }} - {(nameSymbol.ContainingNamespace.IsGlobalNamespace ? "" : "}")}"; - - var fileName = nameSymbol.ToDisplayString(SymbolDisplayFormat.CSharpShortErrorMessageFormat).Replace('<', '{').Replace('>', '}'); - context.AddSource($"{fileName}.g.cs", - CSharpSyntaxTree.ParseText(template).GetRoot().NormalizeWhitespace().ToFullString()); - } - } - - private static (string, string) GenerateOne(IMethodSymbol methodSymbol, List allMethods, HashSet cache) - { - var systemDescData = GetAttributeData(methodSymbol, "TinySystem"); - var runifData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("RunIf")).ToArray(); - var beforeOfData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("BeforeOf")).ToArray(); - var afterOfData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("AfterOf")).ToArray(); - var onEnterData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("OnEnter")).ToArray(); - var onExitData = methodSymbol.GetAttributes().Where(s => s.AttributeClass.Name.Contains("OnExit")).ToArray(); - - var stagee = systemDescData.ConstructorArguments.FirstOrDefault(s => s.Type.ToDisplayString() == "TinyEcs.Stages"); - var threadingg = systemDescData.ConstructorArguments.FirstOrDefault(s => s.Type.ToDisplayString() == "TinyEcs.ThreadingMode"); - - var stage = stagee is { Kind: TypedConstantKind.Error } ? ("TinyEcs.Stages", "TinyEcs.Stages.Update") - : (stagee.Type.ToDisplayString(), stagee.Value); - var threading = threadingg is { Kind: TypedConstantKind.Error } ? ("TinyEcs.ThreadingMode?", "null") - : (threadingg.Type.ToDisplayString(), threadingg.Value); - - var classSymbol = methodSymbol.ContainingType; - var runIfMethods = GetAssociatedMethods(runifData, allMethods, "RunIf", classSymbol); - var beforeMethodsTargets = GetAssociatedMethods(beforeOfData, allMethods, "BeforeOf", classSymbol); - var afterMethodsTargets = GetAssociatedMethods(afterOfData, allMethods, "AfterOf", classSymbol); - var onEnterMethodsTargets = GetAssociatedMethods(afterOfData, allMethods, "OnEnter", classSymbol); - var onExitMethodsTargets = GetAssociatedMethods(afterOfData, allMethods, "OnExit", classSymbol); - - var sbRunIfFns = new StringBuilder(); - var sbRunIfActions = new StringBuilder(); - foreach (var runIf in runIfMethods) - { - sbRunIfActions.AppendLine($".RunIf({runIf.Name}fn)"); - - if (cache.Add(runIf.Name)) - { - sbRunIfFns.AppendLine($"var {runIf.Name}fn = {runIf.Name};"); - } - } - - var sb = new StringBuilder(); - - foreach (var after in afterMethodsTargets) - { - sb.AppendLine($"{methodSymbol.Name}System.RunAfter({after.Name}System);"); - } - - foreach (var before in beforeMethodsTargets) - { - sb.AppendLine($"{methodSymbol.Name}System.RunBefore({before.Name}System);"); - } - - var method = $@" - {sbRunIfFns} - var {methodSymbol.Name}fn = {methodSymbol.Name}; - var {methodSymbol.Name}System = scheduler.AddSystem( - {methodSymbol.Name}fn, - stage: ({stage.Item1}){stage.Value}, - threadingType: ({threading.Item1}){threading.Value ?? "null"} - ) - {sbRunIfActions};"; - - return (method, sb.ToString()); - } - - private static List GetAssociatedMethods(AttributeData[] attributeData, List allMethods, string attributeType, INamedTypeSymbol classSymbol) - { - var associatedMethods = new List(); - foreach (var attr in attributeData.Where(s => !s.ConstructorArguments.IsEmpty)) - { - var methodName = attr.ConstructorArguments[0].Value?.ToString(); - if (string.IsNullOrEmpty(methodName)) continue; - - var method = allMethods.FirstOrDefault(m => m.Name == methodName); - if (method != null) - associatedMethods.Add(method); - else - Debug.WriteLine($"{attributeType} method {methodName} not found in class {classSymbol.Name}."); - } - return associatedMethods; - } - - private static AttributeData GetAttributeData(IMethodSymbol ms, string name) - { - foreach (var attribute in ms.GetAttributes()) - { - var classSymbol = attribute.AttributeClass; - if(!classSymbol.Name.Contains(name)) continue; - - return attribute; - } - - return default; - } - - - private class Comparer : IEqualityComparer - { - public static readonly Comparer Instance = new(); - - public bool Equals(T x, T y) - { - return x.Equals(y); - } - - public int GetHashCode(T obj) - { - return obj.GetHashCode(); - } - } } diff --git a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs b/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs deleted file mode 100644 index 2328070b..00000000 --- a/tools/TinyEcs.SourceGenerator/TinySystemMethodGenerator.cs +++ /dev/null @@ -1,235 +0,0 @@ -using System; -using System.Collections.Immutable; -using System.Linq; -using System.Text; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; - -#pragma warning disable IDE0055 -#pragma warning disable IDE0008 -#pragma warning disable IDE0058 - -namespace TinyEcs.SourceGenerator; - -[Generator] -public sealed class TinySystemMethodGenerator : IIncrementalGenerator -{ - public void Initialize(IncrementalGeneratorInitializationContext context) - { - // Find IPlugin classes that contain methods with [TinySystem] attributes - var pluginClassesWithTinySystemMethods = context.SyntaxProvider.CreateSyntaxProvider( - static (s, _) => s is ClassDeclarationSyntax classDecl && - classDecl.Members.OfType() - .Any(m => m.AttributeLists.Count > 0), - static (ctx, _) => - { - var classDecl = (ClassDeclarationSyntax)ctx.Node; - var classSymbol = ctx.SemanticModel.GetDeclaredSymbol(classDecl) as INamedTypeSymbol; - - if (classSymbol == null) return null; - - // Check if the class implements IPlugin - var implementsIPlugin = classSymbol.AllInterfaces.Any(i => - i.ToDisplayString() == "TinyEcs.IPlugin"); - - if (!implementsIPlugin) return null; - - // Find methods with [TinySystem] attribute - var tinySystemMethods = classSymbol.GetMembers() - .OfType() - .Where(m => m.GetAttributes().Any(attr => - attr.AttributeClass?.ToDisplayString() == "TinyEcs.TinySystemAttribute")) - .ToList(); - - if (!tinySystemMethods.Any()) return null; - - return new PluginClassInfo - { - ClassSymbol = classSymbol, - TinySystemMethods = tinySystemMethods - }; - } - ).Where(static info => info != null); - - // Generate adapter classes for each method - context.RegisterSourceOutput( - pluginClassesWithTinySystemMethods.Collect(), - (spc, pluginClasses) => - { - if (pluginClasses.IsDefaultOrEmpty) return; - - foreach (var pluginClass in pluginClasses) - { - foreach (var method in pluginClass.TinySystemMethods) - { - GenerateAdapterClass(spc, pluginClass.ClassSymbol, method); - } - } - } - ); - } - - private static void GenerateAdapterClass(SourceProductionContext context, INamedTypeSymbol pluginClass, IMethodSymbol method) - { - var adapterName = $"{method.Name}Adapter"; - var ns = pluginClass.ContainingNamespace.IsGlobalNamespace ? "" : pluginClass.ContainingNamespace.ToDisplayString(); - - // Use fully-qualified type name (global::Namespace.Type) for instance/static references - var instanceTypeName = pluginClass.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); - - // Validate method requirements for TinySystemAttribute - ValidateMethodRequirements(context, method); - - // Check if method returns bool (for conditional systems) - var returnsBool = method.ReturnType.SpecialType == SpecialType.System_Boolean; - var baseClass = returnsBool ? "TinyEcs.TinyConditionalSystem" : "TinyEcs.TinySystem"; - - // Get method parameters for dependency injection - var parameters = method.Parameters.ToList(); - var fieldDeclarations = new StringBuilder(); - var setupAssignments = new StringBuilder(); - var methodCallParameters = new StringBuilder(); - - // Generate field declarations and setup assignments - for (int i = 0; i < parameters.Count; i++) - { - var param = parameters[i]; - var fieldName = $"_{param.Name}"; - var paramType = param.Type.ToDisplayString(); - - fieldDeclarations.AppendLine($" private {paramType} {fieldName};"); - setupAssignments.AppendLine($" {fieldName} = builder.Add<{paramType}>();"); - - if (i > 0) methodCallParameters.Append(", "); - methodCallParameters.Append(fieldName); - } - - // For non-static methods, add a field for the plugin instance - string instanceField = ""; - string ctor = ""; - string methodCall; - - // Use a single IndentedStringBuilder for all code blocks - var adapterClass = new IndentedStringBuilder(); - - if (!string.IsNullOrEmpty(ns)) - { - adapterClass.AppendLine($"namespace {ns}"); - adapterClass.AppendLine("{"); - adapterClass.IncrementIndent(); - } - - // Make the adapter class sealed and partial - adapterClass.AppendLine($"public sealed partial class {adapterName} : {baseClass}"); - adapterClass.AppendLine("{"); - adapterClass.IncrementIndent(); - - // Fields - foreach (var line in fieldDeclarations.ToString().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)) - adapterClass.AppendLine(line); - - if (method.IsStatic) - { - // Use namespace-qualified type for static call - methodCall = $"{instanceTypeName}.{method.Name}({methodCallParameters})"; - } - else - { - // Adapter will accept the instance via constructor using namespace-qualified type - instanceField = $"private readonly {instanceTypeName} _instance;"; - ctor = $"public {adapterName}({instanceTypeName} instance) {{ _instance = instance; }}"; - methodCall = $"_instance.{method.Name}({methodCallParameters})"; - adapterClass.AppendLine(instanceField); - adapterClass.AppendLine(); - adapterClass.AppendLine(ctor); - } - - adapterClass.AppendLine(); - - // Setup method - adapterClass.AppendLine("protected override void Setup(TinyEcs.SystemParamBuilder builder)"); - adapterClass.AppendLine("{"); - adapterClass.IncrementIndent(); - foreach (var line in setupAssignments.ToString().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)) - adapterClass.AppendLine(line); - adapterClass.DecrementIndent(); - adapterClass.AppendLine("}"); - - adapterClass.AppendLine(); - - // Execute method - adapterClass.AppendLine("protected override bool Execute(TinyEcs.World world)"); - adapterClass.AppendLine("{"); - adapterClass.IncrementIndent(); - adapterClass.AppendLine("Lock();"); - adapterClass.AppendLine("world.BeginDeferred();"); - if (returnsBool) - { - adapterClass.AppendLine($"bool result = {methodCall};"); - adapterClass.AppendLine("world.EndDeferred();"); - adapterClass.AppendLine("Unlock();"); - adapterClass.AppendLine("return result;"); - } - else - { - adapterClass.AppendLine($"{methodCall};"); - adapterClass.AppendLine("world.EndDeferred();"); - adapterClass.AppendLine("Unlock();"); - adapterClass.AppendLine("return true;"); - } - adapterClass.DecrementIndent(); - adapterClass.AppendLine("}"); - - adapterClass.DecrementIndent(); - adapterClass.AppendLine("}"); - - if (!string.IsNullOrEmpty(ns)) - { - adapterClass.DecrementIndent(); - adapterClass.AppendLine("}"); - } - - var sourceText = adapterClass.ToString(); - context.AddSource($"{adapterName}.g.cs", sourceText); - } - - private static void ValidateMethodRequirements(SourceProductionContext context, IMethodSymbol method) - { - var location = method.Locations.FirstOrDefault() ?? Location.None; - - // Check if method is public - if (method.DeclaredAccessibility != Accessibility.Public) - { - var descriptor = new DiagnosticDescriptor( - "TINYECS001", - "TinySystem method must be public", - "Method '{0}' with [TinySystem] attribute must be public", - "TinyEcs", - DiagnosticSeverity.Error, - isEnabledByDefault: true); - - context.ReportDiagnostic(Diagnostic.Create(descriptor, location, method.Name)); - } - - // Check if method is static - // if (!method.IsStatic) - // { - // var descriptor = new DiagnosticDescriptor( - // "TINYECS002", - // "TinySystem method must be static", - // "Method '{0}' with [TinySystem] attribute must be static", - // "TinyEcs", - // DiagnosticSeverity.Error, - // isEnabledByDefault: true); - // - // context.ReportDiagnostic(Diagnostic.Create(descriptor, location, method.Name)); - // } - } - - private class PluginClassInfo - { - public INamedTypeSymbol ClassSymbol { get; set; } - public List TinySystemMethods { get; set; } - } -} From f42ca5fc58e062cdb8d541c9c7a2c834e67ef88d Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sat, 13 Sep 2025 00:24:48 +0200 Subject: [PATCH 28/33] + onenter/onexit --- samples/MyBattleground/Program.cs | 333 ++++++----------------- src/Bevy.cs | 207 +++++++++----- src/TinyEcs.Systems.StateHandlers.g.cs | 64 ++--- src/TinyEcs.Systems.StateHandlers.tt | 4 +- tools/TinyEcs.SourceGenerator/Program.cs | 105 +++++-- 5 files changed, 347 insertions(+), 366 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index 9a0a71d9..f7c604a6 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -7,8 +7,7 @@ const int ENTITIES_COUNT = (524_288 * 2 * 1); using var ecs = new World(); -var scheduler = new Scheduler(ecs); - +var scheduler = new Scheduler(ecs, ThreadingMode.Single); // var sys = new HelloSystem(); // sys.Initialize(ecs); @@ -16,9 +15,12 @@ scheduler.AddResource(new CustomResource()); -scheduler.AddSystem2(Stages.Startup, new SetupSystem(ENTITIES_COUNT)); -scheduler.AddSystem2(Stages.Update); -scheduler.AddSystem2(Stages.Update, new HelloSystem()); +// scheduler.AddSystem2(Stages.Startup, new SetupSystem(ENTITIES_COUNT)); +// scheduler.AddSystem2(Stages.Update); +// +// scheduler.AddSystem2(Stages.Update, new HelloSystem()); + +scheduler.AddPlugin(new TestPlugin() { Count = ENTITIES_COUNT }); // scheduler.AddPlugin(); @@ -240,27 +242,28 @@ static void Execute(Query query) } } -[TinySystem] -static void ExecuteIterator(Query query) -{ - var it = query.Iter(); - while (it.Next()) - { - var span0 = it.Data(0); - var span1 = it.Data(1); - var count = it.Count; - - for (var i = 0; i < count; ++i) - { - ref var pos = ref span0[i]; - ref var vel = ref span1[i]; - - pos.X *= vel.X; - pos.Y *= vel.Y; - } - } -} + +// static void ExecuteIterator(Query query) +// { +// var it = query.Iter(); +// +// while (it.Next()) +// { +// var span0 = it.Data(0); +// var span1 = it.Data(1); +// var count = it.Count; +// +// for (var i = 0; i < count; ++i) +// { +// ref var pos = ref span0[i]; +// ref var vel = ref span1[i]; +// +// pos.X *= vel.X; +// pos.Y *= vel.Y; +// } +// } +// } public struct Position { @@ -291,150 +294,51 @@ enum AnotherState } +class SYSTEMS +{ + [TinySystem] + public bool CheckThis() + { + new CheckThisAdapter(this); + return true; + } +} -// [TinyPlugin] -// partial struct BBB -// { -// [TinySystem(Stages.Update, ThreadingMode.Single)] -// void Execute(Query> query) -// { -// foreach (var (pos, vel) in query) -// { -// pos.Ref.X *= vel.Ref.X; -// pos.Ref.Y *= vel.Ref.Y; -// } -// } -// -// public void Build(Scheduler scheduler) -// { -// -// } -// } -// class TestSys : IPlugin -// { -// -// public void Build(Scheduler scheduler) -// { -// scheduler.OnUpdate2(TestMethod); -// var xx = TestMethod2; -// scheduler.OnUpdate2(xx); -// scheduler.OnUpdate2(TestMethod2); -// scheduler.OnUpdate2(TestMethod2); -// scheduler.OnUpdate2(TestMethod2); -// scheduler.OnUpdate2(Palle) -// .RunIf((World w) => -// { -// return true; -// }); -// -// scheduler.OnUpdate2((Local w) => -// { -// -// }); -// -// -// scheduler.OnStartup2((World w) => { }); -// } -// -// void TestMethod(World world, Query> query) -// { -// -// } -// -// void TestMethod2(World world, Query> query) -// { -// -// } -// -// void Palle() -// { -// -// } -// } -// -// class TestSys2 : IPlugin -// { -// -// public void Build(Scheduler scheduler) -// { -// scheduler.OnUpdate2(TestMethod); -// var xx = TestMethod2; -// scheduler.OnUpdate2(xx); -// scheduler.OnUpdate2(TestMethod2, ThreadingMode.Single); -// scheduler.OnUpdate2(TestMethod2); -// scheduler.OnUpdate2(TestMethod2); -// scheduler.OnUpdate2(Palle) -// .RunIf((World w) => -// { -// return true; -// }); -// -// scheduler.OnUpdate2((Local w) => -// { -// -// }); -// } -// -// void TestMethod(World world, Query> query) -// { -// -// } -// -// void TestMethod2(World world, Query> query) -// { -// -// } -// -// void Palle() -// { -// -// } -// } +internal sealed class TestPlugin : IPlugin +{ + public int Count { get; init; } + public void Build(Scheduler scheduler) + { + scheduler.AddState(GameState.Loading); -// namespace TinyEcs -// { -// public delegate void SystemFn(World param1, SchedulerState param2); -// public partial class Scheduler { -// public void OnUpdate2(SystemFn fn) -// { -// -// } -// } -// } + scheduler + .AddSystems(Stage.Startup, new SetupAdapter(this)) + .AddSystems(Stage.Update, new MoveEntitiesAdapter() + .RunIf(new CanRunAdapter(this))) + .AddSystem(Stage.Update); -// partial class GenSystem : TinySystem2 -// { -// private Query> _query; -// -// public override void Setup(World world) -// { -// _query = (Query>)Query>.Generate(world); -// } -// -// public override void Execute(World world) -// { -// Execute(_query); -// } -// } + scheduler.AddStageAfterOf(Stage.Update, "AFTER_UPDATE"); + scheduler.AddSystems("AFTER_UPDATE", new AcceptAdapter(this)); -[TinySystem] -partial class SetupSystem(int count) -{ - void OnExecute(World world, World world2) + //scheduler.AddSystems(Stage.OnEnter, new TinyStateSystemAdapter(GameState.Playing)); + scheduler.AddSystems(Stage.OnEnter(GameState.Playing), new OnEnterAdapter(this)); + scheduler.AddSystems(Stage.OnExit(GameState.Loading), new OnExitAdapter(this)); + } + + + [TinySystem] + public void Setup(World world) { - for (var i = 0; i < count; i++) + for (var i = 0; i < Count; i++) world.Entity() .Set(new Position()) .Set(new Velocity()); } -} -[TinySystem] -partial class GenSystem -{ - void OnExecute(World world, Query> query, Res customRes) + [TinySystem] + public static void MoveEntities(Query> query) { foreach ((var pos, var vel) in query) { @@ -442,106 +346,35 @@ void OnExecute(World world, Query> query, Res state) + { + state.Set(GameState.Playing); + } -[TinySystem] -partial class HelloSystem -{ - void OnExecute(Query> query, Local customRes) + [TinySystem] + public void OnEnter(State state) { + Console.WriteLine("on enter {0}", state.Current); + } + + [TinySystem] + public void OnExit(State state) + { + Console.WriteLine("on exit {0}", state.Current); + } + [TinySystem] + public bool CanRun() + => true; + + [TinySystem] + public void Accept() + { } } -// namespace ANamespace -// { -// [TinyPlugin] -// public partial class AAA -// { -// [TinySystem] -// [RunIf(nameof(TestRun))] -// [RunIf(nameof(TestRun2))] -// void Execute(Query> query) -// { -// foreach (var (pos, vel) in query) -// { -// pos.Ref.X *= vel.Ref.X; -// pos.Ref.Y *= vel.Ref.Y; -// } -// } -// -// -// [TinySystem(Stages.OnEnter, ThreadingMode.Single)] -// static void CheckState(World world) -// { -// -// } -// -// [TinySystem] -// [RunIf(nameof(TestRun2))] -// [RunIf(nameof(TestRun2))] -// [RunIf(nameof(TestRun2))] -// [RunIf(nameof(TestRun2))] -// static void DoThat(Query> query, EventWriter writer) -// { -// foreach (var (pos, vel) in query) -// { -// pos.Ref.X *= vel.Ref.X; -// pos.Ref.Y *= vel.Ref.Y; -// } -// } -// -// -// [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Second))] -// void First() -// { -// Console.WriteLine("1"); -// } -// -// [TinySystem(threadingMode: ThreadingMode.Single), AfterOf(nameof(Third))] -// void Second(EventReader reader) -// { -// Console.WriteLine("2"); -// } -// -// [TinySystem(threadingMode: ThreadingMode.Single), BeforeOf("HELLO")] -// void Third() -// { -// Console.WriteLine("3"); -// } -// -// private bool TestRun(SchedulerState state, World world, Local index) -// { -// return true; -// } -// -// private bool TestRun2() -// { -// return true; -// } -// -// public void Build(Scheduler scheduler) -// { -// scheduler.AddEvent(); -// } -// -// struct CustomEvent -// { -// public int Value; -// } -// } -// -// } -// -// [TinyPlugin] -// public partial class TestPlugin -// { -// public void Build(Scheduler scheduler) -// { -// -// } -// } public class CustomResource diff --git a/src/Bevy.cs b/src/Bevy.cs index 5c5897c6..275970dd 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -33,10 +33,20 @@ public sealed class SystemTicks public uint ThisRun { get; internal set; } } -public sealed class Stage(string name, bool runOnce) + +public class Stage { - public string Name { get; } = name; - public bool RunOnce { get; } = runOnce; + internal Stage(string name, bool runOnce) + { + if (string.IsNullOrWhiteSpace(name)) + throw new ArgumentException("Stage name cannot be null or whitespace.", nameof(name)); + Name = name; + RunOnce = runOnce; + } + + public string Name { get; } + public bool RunOnce { get; } + public static readonly Stage Startup = new(nameof(Startup), true); public static readonly Stage FrameStart = new(nameof(FrameStart), false); @@ -44,10 +54,49 @@ public sealed class Stage(string name, bool runOnce) public static readonly Stage Update = new(nameof(Update), false); public static readonly Stage AfterUpdate = new(nameof(AfterUpdate), false); public static readonly Stage FrameEnd = new(nameof(FrameEnd), false); - public static readonly Stage OnEnter = new(nameof(OnEnter), false); - public static readonly Stage OnExit = new(nameof(OnExit), false); + + // These are useful just to headers for the custom OnEnter/OnExit stages + internal static readonly Stage OnEnterInner = new(nameof(OnEnterInner), false); + internal static readonly Stage OnExitInner = new(nameof(OnExitInner), false); + + public static Stage OnEnter(TState state) + where TState : struct, Enum + { + var stage = new OnEnterStage(state, $"__pvt_on_enter_{typeof(TState).ToString()}.{Enum.GetName(state)}", false); + return stage; + } + + public static Stage OnExit(TState state) + where TState : struct, Enum + { + var stage = new OnExitStage(state, $"__pvt_on_exit_{typeof(TState).ToString()}.{Enum.GetName(state)}", false); + return stage; + } +} + +public interface IStateStage +{ + ITinySystem CreateSystem(ITinySystem sys); +} + +public interface IStateEnter : IStateStage { } +public interface IStateExit : IStateStage { } + +internal sealed class OnEnterStage(TState state, string name, bool runOnce = false) : Stage(name, runOnce), IStateEnter + where TState : struct, Enum +{ + public ITinySystem CreateSystem(ITinySystem sys) + => new TinyOnEnterSystem(state, sys); +} + +internal sealed class OnExitStage(TState state, string name, bool runOnce = false) : Stage(name, runOnce), IStateExit + where TState : struct, Enum +{ + public ITinySystem CreateSystem(ITinySystem sys) + => new TinyOnExitSystem(state, sys); } + internal sealed class StageHandler(Stage stage) { private bool _initialized; @@ -154,7 +203,7 @@ internal void Add(Stage stage) } } - public void AddBeforeOf(Stage parent, Stage stage) + public StageHandler AddBeforeOf(Stage parent, Stage stage) { if (!Contains(parent)) throw new InvalidOperationException($"Parent stage '{parent.Name}' not found."); @@ -169,9 +218,10 @@ public void AddBeforeOf(Stage parent, Stage stage) var handler = new StageHandler(stage); Stages.Insert(index, handler); StageMap.Add(stage.Name, handler); + return handler; } - public void AddAfterOf(Stage parent, Stage stage) + public StageHandler AddAfterOf(Stage parent, Stage stage) { if (!Contains(parent)) throw new InvalidOperationException($"Parent stage '{parent.Name}' not found."); @@ -186,12 +236,33 @@ public void AddAfterOf(Stage parent, Stage stage) var handler = new StageHandler(stage); Stages.Insert(index + 1, handler); StageMap.Add(stage.Name, handler); + return handler; } public void AddSystem(ITinySystem system, Stage stage) { if (!StageMap.TryGetValue(stage.Name, out var handler)) - throw new InvalidOperationException($"Stage '{stage.Name}' not found."); + { + // TODO: is there a better way to handle these special cases? + // the AddAfterOf makes the current stage to be added on head instead of tail. + if (stage is IStateEnter) + { + handler = AddAfterOf(Stage.OnEnterInner, stage); + } + else if (stage is IStateExit) + { + handler = AddAfterOf(Stage.OnExitInner, stage); + } + else + { + throw new InvalidOperationException($"Stage '{stage.Name}' not found."); + } + } + + if (stage is IStateStage stStage) + { + system = stStage.CreateSystem(system); + } handler.AddSystem(system); } @@ -214,8 +285,8 @@ public Scheduler(World world, ThreadingMode threadingMode = ThreadingMode.Auto) ThreadingExecutionMode = threadingMode; _stageContainer.Add(Stage.Startup); - _stageContainer.Add(Stage.OnExit); - _stageContainer.Add(Stage.OnEnter); + _stageContainer.Add(Stage.OnExitInner); + _stageContainer.Add(Stage.OnEnterInner); _stageContainer.Add(Stage.FrameStart); _stageContainer.Add(Stage.BeforeUpdate); _stageContainer.Add(Stage.Update); @@ -364,7 +435,7 @@ public ITinySystem OnEnter(TState st, Action system, ThreadingMode? thre { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stage.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -385,7 +456,7 @@ public ITinySystem OnExit(TState st, Action system, ThreadingMode? threa { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stage.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -450,8 +521,8 @@ private static Stage GetStage(Stages stage) Stages.Update => Stage.Update, Stages.AfterUpdate => Stage.AfterUpdate, Stages.FrameEnd => Stage.FrameEnd, - Stages.OnEnter => Stage.OnEnter, - Stages.OnExit => Stage.OnExit, + Stages.OnEnter => Stage.OnEnterInner, + Stages.OnExit => Stage.OnExitInner, _ => throw new ArgumentOutOfRangeException(nameof(stage), stage, null) }; } @@ -1419,60 +1490,11 @@ public bool MoveNext() } -[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] -public class TinySystemAttribute : Attribute +[AttributeUsage(AttributeTargets.Method)] +public sealed class TinySystemAttribute : Attribute { - public TinySystemAttribute(Stages stage = Stages.Update) - { - Stage = stage; - ThreadingMode = null; - } - - public TinySystemAttribute(ThreadingMode threadingMode) : this(Stages.Update, threadingMode) - { - } - - public TinySystemAttribute(Stages stage, ThreadingMode threadingMode) - { - Stage = stage; - ThreadingMode = threadingMode; - } - - public Stages Stage { get; } - public ThreadingMode? ThreadingMode { get; } } -[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] -public sealed class RunIf(string systemName) : Attribute -{ - public string SystemName { get; } = systemName; -} - -[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] -public sealed class BeforeOf(string systemName) : Attribute -{ - public string SystemName { get; } = systemName; -} - -[AttributeUsage(System.AttributeTargets.Method, AllowMultiple = true)] -public sealed class AfterOf(string systemName) : Attribute -{ - public string SystemName { get; } = systemName; -} - - -[AttributeUsage(System.AttributeTargets.Class | AttributeTargets.Struct)] -public sealed class TinyPluginAttribute : Attribute -{ -} - - -[AttributeUsage(AttributeTargets.Class)] -public sealed class TinyConditionalSystemAttribute : Attribute -{ -} - - public sealed class SystemParamBuilder(World world) { private readonly List _params = []; @@ -1644,6 +1666,7 @@ public override bool BeforeExecute(World world) return false; } } + return true; } @@ -1711,4 +1734,58 @@ protected override bool Execute(World world) } } +internal abstract class TinyStateSystemAdapter(ITinySystem sys) : TinySystem + where TState : struct, Enum +{ + protected State _state; + protected int _stateChangedId = -1; + + public override void Initialize(World world) + { + base.Initialize(world); + sys.Initialize(world); + } + + protected override void Setup(SystemParamBuilder builder) + { + _state = builder.Add>(); + } +} + +internal sealed class TinyOnEnterSystem(TState st, ITinySystem sys) : TinyStateSystemAdapter(sys) + where TState : struct, Enum +{ + protected override bool Execute(World world) + { + Lock(); + world.BeginDeferred(); + var result = _state.ShouldEnter(st, ref _stateChangedId); + if (result) + { + _ = sys.ExecuteOnReady(world, Ticks.ThisRun); + } + world.EndDeferred(); + Unlock(); + return result; + } +} + +internal sealed class TinyOnExitSystem(TState st, ITinySystem sys) : TinyStateSystemAdapter(sys) + where TState : struct, Enum +{ + protected override bool Execute(World world) + { + Lock(); + world.BeginDeferred(); + var result = _state.ShouldExit(st, ref _stateChangedId); + if (result) + { + _ = sys.ExecuteOnReady(world, Ticks.ThisRun); + } + world.EndDeferred(); + Unlock(); + return result; + } +} + #endif diff --git a/src/TinyEcs.Systems.StateHandlers.g.cs b/src/TinyEcs.Systems.StateHandlers.g.cs index 0461f54b..bd956496 100644 --- a/src/TinyEcs.Systems.StateHandlers.g.cs +++ b/src/TinyEcs.Systems.StateHandlers.g.cs @@ -34,7 +34,7 @@ public ITinySystem OnEnter(TState st, Action system, ThreadingMo var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -60,7 +60,7 @@ public ITinySystem OnExit(TState st, Action system, ThreadingMod var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -91,7 +91,7 @@ public ITinySystem OnEnter(TState st, Action system, Thr var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -122,7 +122,7 @@ public ITinySystem OnExit(TState st, Action system, Thre var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -158,7 +158,7 @@ public ITinySystem OnEnter(TState st, Action sys var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -194,7 +194,7 @@ public ITinySystem OnExit(TState st, Action syst var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -235,7 +235,7 @@ public ITinySystem OnEnter(TState st, Action state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -276,7 +276,7 @@ public ITinySystem OnExit(TState st, Action state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -322,7 +322,7 @@ public ITinySystem OnEnter(TState st, Action state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -368,7 +368,7 @@ public ITinySystem OnExit(TState st, Action state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -419,7 +419,7 @@ public ITinySystem OnEnter(TState st, Action state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -470,7 +470,7 @@ public ITinySystem OnExit(TState st, Action state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -526,7 +526,7 @@ public ITinySystem OnEnter(TState st, Action var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -582,7 +582,7 @@ public ITinySystem OnExit(TState st, Action< var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -643,7 +643,7 @@ public ITinySystem OnEnter(TState st, Ac var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -704,7 +704,7 @@ public ITinySystem OnExit(TState st, Act var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -770,7 +770,7 @@ public ITinySystem OnEnter(TState st var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -836,7 +836,7 @@ public ITinySystem OnExit(TState st, var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -907,7 +907,7 @@ public ITinySystem OnEnter(TStat var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -978,7 +978,7 @@ public ITinySystem OnExit(TState var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -1054,7 +1054,7 @@ public ITinySystem OnEnter( var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -1130,7 +1130,7 @@ public ITinySystem OnExit(T var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -1211,7 +1211,7 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -1292,7 +1292,7 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -1378,7 +1378,7 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -1464,7 +1464,7 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -1555,7 +1555,7 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -1646,7 +1646,7 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -1742,7 +1742,7 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -1838,7 +1838,7 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } @@ -1939,7 +1939,7 @@ public ITinySystem OnEnter state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -2040,7 +2040,7 @@ public ITinySystem OnExit state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } diff --git a/src/TinyEcs.Systems.StateHandlers.tt b/src/TinyEcs.Systems.StateHandlers.tt index c121ee69..56d41915 100644 --- a/src/TinyEcs.Systems.StateHandlers.tt +++ b/src/TinyEcs.Systems.StateHandlers.tt @@ -68,7 +68,7 @@ namespace TinyEcs var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldEnter(st, ref stateChangeId)); - Add(sys, Stages.OnEnter); + Add(sys, Stage.OnEnterInner); return sys; } @@ -94,7 +94,7 @@ namespace TinyEcs var sys = new TinyDelegateSystem(fn) { Configuration = { ThreadingMode = threadingType } } .RunIf((State state) => state.ShouldExit(st, ref stateChangeId)); - Add(sys, Stages.OnExit); + Add(sys, Stage.OnExitInner); return sys; } diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 8122332a..1c128376 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -44,39 +44,92 @@ public void Initialize(IncrementalGeneratorInitializationContext context) if (!tinySystemMethods.Any()) return null; - return new PluginClassInfo + return new SystemMethodInfo { - ClassSymbol = classSymbol, - TinySystemMethods = tinySystemMethods + ContainingType = classSymbol, + TinySystemMethods = tinySystemMethods, + IsFromPlugin = true }; } ).Where(static info => info != null); + // Find all methods with [TinySystem] attributes (including those outside IPlugin classes) + var allTinySystemMethods = context.SyntaxProvider.CreateSyntaxProvider( + static (s, _) => s is MethodDeclarationSyntax methodDecl && + methodDecl.AttributeLists.Count > 0, + static (ctx, _) => + { + var methodDecl = (MethodDeclarationSyntax)ctx.Node; + var methodSymbol = ctx.SemanticModel.GetDeclaredSymbol(methodDecl) as IMethodSymbol; + + if (methodSymbol == null) return null; + + // Check if method has [TinySystem] attribute + var hasTinySystemAttribute = methodSymbol.GetAttributes().Any(attr => + attr.AttributeClass?.ToDisplayString() == "TinyEcs.TinySystemAttribute"); + + if (!hasTinySystemAttribute) return null; + + var containingType = methodSymbol.ContainingType; + + // Check if the containing class implements IPlugin + var implementsIPlugin = containingType.AllInterfaces.Any(i => + i.ToDisplayString() == "TinyEcs.IPlugin"); + + // Only include if it's NOT from an IPlugin class (those are handled separately) + if (implementsIPlugin) return null; + + return new SystemMethodInfo + { + ContainingType = containingType, + TinySystemMethods = new List { methodSymbol }, + IsFromPlugin = false + }; + } + ).Where(static info => info != null); + + // Combine both providers + var allSystemMethods = pluginClassesWithTinySystemMethods.Collect() + .Combine(allTinySystemMethods.Collect()) + .Select(static (combined, _) => + { + var (pluginMethods, standaloneMethods) = combined; + var result = new List(); + + if (!pluginMethods.IsDefaultOrEmpty) + result.AddRange(pluginMethods); + + if (!standaloneMethods.IsDefaultOrEmpty) + result.AddRange(standaloneMethods); + + return result.ToImmutableArray(); + }); + // Generate adapter classes for each method context.RegisterSourceOutput( - pluginClassesWithTinySystemMethods.Collect(), - (spc, pluginClasses) => + allSystemMethods, + (spc, systemMethods) => { - if (pluginClasses.IsDefaultOrEmpty) return; + if (systemMethods.IsDefaultOrEmpty) return; - foreach (var pluginClass in pluginClasses) + foreach (var systemMethod in systemMethods) { - foreach (var method in pluginClass.TinySystemMethods) + foreach (var method in systemMethod.TinySystemMethods) { - GenerateAdapterClass(spc, pluginClass.ClassSymbol, method); + GenerateAdapterClass(spc, systemMethod.ContainingType, method, systemMethod.IsFromPlugin); } } } ); } - private static void GenerateAdapterClass(SourceProductionContext context, INamedTypeSymbol pluginClass, IMethodSymbol method) + private static void GenerateAdapterClass(SourceProductionContext context, INamedTypeSymbol containingType, IMethodSymbol method, bool isFromPlugin) { var adapterName = $"{method.Name}Adapter"; - var ns = pluginClass.ContainingNamespace.IsGlobalNamespace ? "" : pluginClass.ContainingNamespace.ToDisplayString(); + var ns = containingType.ContainingNamespace.IsGlobalNamespace ? "" : containingType.ContainingNamespace.ToDisplayString(); // Use fully-qualified type name (global::Namespace.Type) for instance/static references - var instanceTypeName = pluginClass.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); + var instanceTypeName = containingType.ToDisplayString(SymbolDisplayFormat.FullyQualifiedFormat); // Validate method requirements for TinySystemAttribute ValidateMethodRequirements(context, method); @@ -85,6 +138,9 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed var returnsBool = method.ReturnType.SpecialType == SpecialType.System_Boolean; var baseClass = returnsBool ? "TinyEcs.TinyConditionalSystem" : "TinyEcs.TinySystem"; + // Determine class visibility based on the containing type's visibility + var classVisibility = GetClassVisibility(containingType); + // Get method parameters for dependency injection var parameters = method.Parameters.ToList(); var fieldDeclarations = new StringBuilder(); @@ -105,7 +161,7 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed methodCallParameters.Append(fieldName); } - // For non-static methods, add a field for the plugin instance + // For non-static methods, add a field for the instance string instanceField = ""; string ctor = ""; string methodCall; @@ -120,8 +176,8 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed adapterClass.IncrementIndent(); } - // Make the adapter class sealed and partial - adapterClass.AppendLine($"public sealed partial class {adapterName} : {baseClass}"); + // Make the adapter class sealed and partial with visibility matching the containing type + adapterClass.AppendLine($"{classVisibility} sealed partial class {adapterName} : {baseClass}"); adapterClass.AppendLine("{"); adapterClass.IncrementIndent(); @@ -194,6 +250,20 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed context.AddSource($"{adapterName}.g.cs", sourceText); } + private static string GetClassVisibility(INamedTypeSymbol containingType) + { + return containingType.DeclaredAccessibility switch + { + Accessibility.Public => "public", + Accessibility.Internal => "internal", + Accessibility.Private => "private", + Accessibility.Protected => "protected", + Accessibility.ProtectedAndInternal => "private protected", + Accessibility.ProtectedOrInternal => "protected internal", + _ => "internal" // Default to internal if accessibility is not recognized + }; + } + private static void ValidateMethodRequirements(SourceProductionContext context, IMethodSymbol method) { var location = method.Locations.FirstOrDefault() ?? Location.None; @@ -227,9 +297,10 @@ private static void ValidateMethodRequirements(SourceProductionContext context, // } } - private class PluginClassInfo + private class SystemMethodInfo { - public INamedTypeSymbol ClassSymbol { get; set; } + public INamedTypeSymbol ContainingType { get; set; } public List TinySystemMethods { get; set; } + public bool IsFromPlugin { get; set; } } } From da717b6e570862cc4c8637c587099ddb28677a80 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sat, 13 Sep 2025 15:54:06 +0200 Subject: [PATCH 29/33] + convert TinyEcsGame to source gen --- TinyEcs.slnx | 1 - samples/TinyEcsGame/Program.cs | 86 ++++++++++++++---------- samples/TinyEcsGame/TinyEcsGame.csproj | 1 + tools/TinyEcs.SourceGenerator/Program.cs | 25 +++---- 4 files changed, 65 insertions(+), 48 deletions(-) diff --git a/TinyEcs.slnx b/TinyEcs.slnx index 92bf847d..33561469 100644 --- a/TinyEcs.slnx +++ b/TinyEcs.slnx @@ -10,7 +10,6 @@ - diff --git a/samples/TinyEcsGame/Program.cs b/samples/TinyEcsGame/Program.cs index 3aaa1490..e9e872c2 100644 --- a/samples/TinyEcsGame/Program.cs +++ b/samples/TinyEcsGame/Program.cs @@ -3,7 +3,7 @@ using Raylib_cs; -using var app = new App(); +using var app = new App(ThreadingMode.Multi); app.AddPlugin(new RaylibPlugin() { WindowSize = new() { Value = { X = 800, Y = 600 } }, @@ -17,14 +17,14 @@ Velocity = 250 }); -app.Run(() => Raylib.WindowShouldClose(), Raylib.CloseWindow); +app.Run(() => Raylib.IsWindowReady() && Raylib.WindowShouldClose(), Raylib.CloseWindow); // ================================================================================= sealed class App : Scheduler, IDisposable { - public App() : base(new()) { } + public App(ThreadingMode threadingMode = ThreadingMode.Auto) : base(new(), threadingMode) { } public void Dispose() => World?.Dispose(); } @@ -54,6 +54,17 @@ struct RaylibPlugin : IPlugin public bool VSync { get; set; } public readonly void Build(Scheduler scheduler) + { + scheduler.AddSystemParam(new Time()); + scheduler.AddResource(WindowSize); + scheduler.AddResource(new AssetsManager()); + + scheduler.AddSystems(Stage.Startup, new CreateWindowAdapter(this) { Configuration = { ThreadingMode = ThreadingMode.Single } }); + scheduler.AddSystems(Stage.BeforeUpdate, new UpdateTimeAdapter(this)); + } + + [TinySystem] + public void CreateWindow() { ConfigFlags flags = 0; if (VSync) @@ -61,16 +72,13 @@ public readonly void Build(Scheduler scheduler) Raylib.SetConfigFlags(flags); Raylib.InitWindow((int)WindowSize.Value.X, (int)WindowSize.Value.Y, Title); + } - scheduler.AddSystemParam(new Time()); - scheduler.AddResource(WindowSize); - scheduler.AddResource(new AssetsManager()); - - scheduler.AddSystem((Time time) => - { - time.Frame = Raylib.GetFrameTime(); - time.Total += time.Frame; - }, Stages.BeforeUpdate); + [TinySystem] + public void UpdateTime(Time time) + { + time.Frame = Raylib.GetFrameTime(); + time.Total += time.Frame; } } // ================================================================================= @@ -93,16 +101,14 @@ struct GameplayPlugin : IPlugin public void Build(Scheduler scheduler) { - var init = SpawnEntities; - var fn0 = MoveSystem; - var fn1 = CheckBounds; - - scheduler.AddSystem(init, Stages.Startup); - scheduler.AddSystem(fn0); - scheduler.AddSystem(fn1); + scheduler + .AddSystems(Stage.Startup, new SpawnEntitiesAdapter(this) { Configuration = { ThreadingMode = ThreadingMode.Single } } + .RunIf(new CheckIfWindowReadyAdapter())) + .AddSystems(Stage.Update, new MoveSystemAdapter(), new CheckBoundsAdapter()); } - static void MoveSystem(Time time, Query> query) + [TinySystem] + public static void MoveSystem(Time time, Query> query) { foreach ((var pos, var vel, var rot) in query) { @@ -111,7 +117,8 @@ static void MoveSystem(Time time, Query> quer } } - static void CheckBounds(Query> query, Res windowSize) + [TinySystem] + public static void CheckBounds(Query> query, Res windowSize) { foreach ((var pos, var vel) in query) { @@ -139,7 +146,14 @@ static void CheckBounds(Query> query, Res w } } - void SpawnEntities(World ecs, SchedulerState scheduler, Res size, Res assetsManager) + [TinySystem] + public static bool CheckIfWindowReady() + { + return Raylib.IsWindowReady(); + } + + [TinySystem] + public void SpawnEntities(World ecs, SchedulerState scheduler, Res size, Res assetsManager) { var rnd = Random.Shared; var texture = Raylib.LoadTexture(Path.Combine(AppContext.BaseDirectory, "Content", "pepe.png")); @@ -189,29 +203,30 @@ void SpawnEntities(World ecs, SchedulerState scheduler, Res size, Re { public void Build(Scheduler scheduler) { - var fn2 = BeginRenderer; - var fn3 = RenderEntities; - var fn4 = DrawText; - var fn5 = EndRenderer; - - var begin = scheduler.AddSystem(fn2, stage: Stages.FrameEnd, threadingType: ThreadingMode.Single); - var renderEntities = scheduler.AddSystem(fn3, stage: Stages.FrameEnd, threadingType: ThreadingMode.Single); - var renderText = scheduler.AddSystem(fn4, stage: Stages.FrameEnd, threadingType: ThreadingMode.Single); - var end = scheduler.AddSystem(fn5, stage: Stages.FrameEnd, threadingType: ThreadingMode.Single); + scheduler + .AddSystems(Stage.FrameEnd, + new BeginRendererAdapter() { Configuration = { ThreadingMode = ThreadingMode.Single } }.RunIf(new CheckIfWindowReadyAdapter()), + new RenderEntitiesAdapter() { Configuration = { ThreadingMode = ThreadingMode.Single } }.RunIf(new CheckIfWindowReadyAdapter()), + new DrawTextAdapter() { Configuration = { ThreadingMode = ThreadingMode.Single } }.RunIf(new CheckIfWindowReadyAdapter()), + new EndRendererAdapter() { Configuration = { ThreadingMode = ThreadingMode.Single } }.RunIf(new CheckIfWindowReadyAdapter()) + ); } - static void BeginRenderer() + [TinySystem] + public static void BeginRenderer() { Raylib.BeginDrawing(); Raylib.ClearBackground(Color.Black); } - static void EndRenderer() + [TinySystem] + public static void EndRenderer() { Raylib.EndDrawing(); } - static void RenderEntities(Query> query, Res assetsManager) + [TinySystem] + public static void RenderEntities(Query> query, Res assetsManager) { var currTextureId = 0u; Texture2D? texture = null; @@ -229,7 +244,8 @@ static void RenderEntities(Query> query, Res text, Local timeout) + [TinySystem] + public static void DrawText(World ecs, Time time, Local text, Local timeout) { //if (time.Total > timeout) { diff --git a/samples/TinyEcsGame/TinyEcsGame.csproj b/samples/TinyEcsGame/TinyEcsGame.csproj index b2e49a5e..bd3745c2 100644 --- a/samples/TinyEcsGame/TinyEcsGame.csproj +++ b/samples/TinyEcsGame/TinyEcsGame.csproj @@ -40,6 +40,7 @@ + diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 1c128376..e1de8038 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -19,24 +19,25 @@ public void Initialize(IncrementalGeneratorInitializationContext context) { // Find IPlugin classes that contain methods with [TinySystem] attributes var pluginClassesWithTinySystemMethods = context.SyntaxProvider.CreateSyntaxProvider( - static (s, _) => s is ClassDeclarationSyntax classDecl && - classDecl.Members.OfType() + static (s, _) => (s is ClassDeclarationSyntax || s is StructDeclarationSyntax) && + s is TypeDeclarationSyntax typeDecl && + typeDecl.Members.OfType() .Any(m => m.AttributeLists.Count > 0), static (ctx, _) => { - var classDecl = (ClassDeclarationSyntax)ctx.Node; - var classSymbol = ctx.SemanticModel.GetDeclaredSymbol(classDecl) as INamedTypeSymbol; + var typeDecl = (TypeDeclarationSyntax)ctx.Node; + var typeSymbol = ctx.SemanticModel.GetDeclaredSymbol(typeDecl) as INamedTypeSymbol; - if (classSymbol == null) return null; + if (typeSymbol == null) return null; - // Check if the class implements IPlugin - var implementsIPlugin = classSymbol.AllInterfaces.Any(i => + // Check if the type implements IPlugin + var implementsIPlugin = typeSymbol.AllInterfaces.Any(i => i.ToDisplayString() == "TinyEcs.IPlugin"); if (!implementsIPlugin) return null; // Find methods with [TinySystem] attribute - var tinySystemMethods = classSymbol.GetMembers() + var tinySystemMethods = typeSymbol.GetMembers() .OfType() .Where(m => m.GetAttributes().Any(attr => attr.AttributeClass?.ToDisplayString() == "TinyEcs.TinySystemAttribute")) @@ -46,14 +47,14 @@ public void Initialize(IncrementalGeneratorInitializationContext context) return new SystemMethodInfo { - ContainingType = classSymbol, + ContainingType = typeSymbol, TinySystemMethods = tinySystemMethods, IsFromPlugin = true }; } ).Where(static info => info != null); - // Find all methods with [TinySystem] attributes (including those outside IPlugin classes) + // Find all methods with [TinySystem] attributes (including those outside IPlugin classes/structs) var allTinySystemMethods = context.SyntaxProvider.CreateSyntaxProvider( static (s, _) => s is MethodDeclarationSyntax methodDecl && methodDecl.AttributeLists.Count > 0, @@ -72,11 +73,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context) var containingType = methodSymbol.ContainingType; - // Check if the containing class implements IPlugin + // Check if the containing type implements IPlugin var implementsIPlugin = containingType.AllInterfaces.Any(i => i.ToDisplayString() == "TinyEcs.IPlugin"); - // Only include if it's NOT from an IPlugin class (those are handled separately) + // Only include if it's NOT from an IPlugin type (those are handled separately) if (implementsIPlugin) return null; return new SystemMethodInfo From 83b01e6a2fc2fe789fa466a9f638243018cf85fa Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sun, 14 Sep 2025 11:51:49 +0200 Subject: [PATCH 30/33] + SystemOrder.Chain --- samples/TinyEcsGame/Program.cs | 13 ++++++++----- src/Bevy.cs | 28 ++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/samples/TinyEcsGame/Program.cs b/samples/TinyEcsGame/Program.cs index e9e872c2..7dc4e3b7 100644 --- a/samples/TinyEcsGame/Program.cs +++ b/samples/TinyEcsGame/Program.cs @@ -205,11 +205,14 @@ public void Build(Scheduler scheduler) { scheduler .AddSystems(Stage.FrameEnd, - new BeginRendererAdapter() { Configuration = { ThreadingMode = ThreadingMode.Single } }.RunIf(new CheckIfWindowReadyAdapter()), - new RenderEntitiesAdapter() { Configuration = { ThreadingMode = ThreadingMode.Single } }.RunIf(new CheckIfWindowReadyAdapter()), - new DrawTextAdapter() { Configuration = { ThreadingMode = ThreadingMode.Single } }.RunIf(new CheckIfWindowReadyAdapter()), - new EndRendererAdapter() { Configuration = { ThreadingMode = ThreadingMode.Single } }.RunIf(new CheckIfWindowReadyAdapter()) - ); + SystemOrder.Chain + ( + new BeginRendererAdapter(), + new RenderEntitiesAdapter(), + new DrawTextAdapter(), + new EndRendererAdapter() + ) + .RunIf(new CheckIfWindowReadyAdapter())); } [TinySystem] diff --git a/src/Bevy.cs b/src/Bevy.cs index 275970dd..d5bb152b 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -1516,11 +1516,23 @@ public sealed class SystemConfiguration public ThreadingMode? ThreadingMode { get; set; } } -public sealed class SystemOrderConfiguration +public sealed class SystemOrder { - public LinkedList BeforeSystems { get; set; } = new(); - public LinkedList AfterSystems { get; set; } = new(); - public LinkedListNode? Node { get; set; } + public LinkedList BeforeSystems { get; internal set; } = new(); + public LinkedList AfterSystems { get; internal set; } = new(); + public LinkedListNode? Node { get; internal set; } + + + public static ITinySystem Chain(params ITinySystem[] systems) + { + if (systems.Length == 0) + throw new ArgumentException("At least one system is required to create a chain.", nameof(systems)); + + var first = systems[0]; + for (var i = 1; i < systems.Length; i++) + systems[i].RunAfter(first); + return first; + } } @@ -1541,7 +1553,7 @@ public interface ITinyMeta public interface ITinySystem : ITinyMeta { - SystemOrderConfiguration OrderConfiguration { get; } + SystemOrder OrderConfiguration { get; } ITinySystem RunIf() where T : ITinyConditionalSystem, new(); ITinySystem RunIf(params ITinyConditionalSystem[] conditionals); @@ -1644,7 +1656,7 @@ protected void Unlock() public abstract class TinySystem : TinySystemBase, ITinySystem { - public SystemOrderConfiguration OrderConfiguration { get; } = new(); + public SystemOrder OrderConfiguration { get; } = new(); public override void Initialize(World world) { @@ -1699,14 +1711,14 @@ public ITinySystem RunIf(params ITinyConditionalSystem[] conditionals) public ITinySystem RunAfter(ITinySystem sys) { OrderConfiguration.Node?.List?.Remove(OrderConfiguration.Node); - OrderConfiguration.Node = OrderConfiguration.AfterSystems.AddLast(sys); + OrderConfiguration.Node = sys.OrderConfiguration.AfterSystems.AddLast(this); return this; } public ITinySystem RunBefore(ITinySystem sys) { OrderConfiguration.Node?.List?.Remove(OrderConfiguration.Node); - OrderConfiguration.Node = OrderConfiguration.BeforeSystems.AddLast(sys); + OrderConfiguration.Node = sys.OrderConfiguration.BeforeSystems.AddLast(this); return this; } } From b63dc200ac0102dbb377ba45096e8af8939ecc9b Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Thu, 18 Sep 2025 22:13:59 +0200 Subject: [PATCH 31/33] + get system params --- src/Bevy.cs | 66 +++++++++++++++++------- tools/TinyEcs.SourceGenerator/Program.cs | 13 ++--- 2 files changed, 51 insertions(+), 28 deletions(-) diff --git a/src/Bevy.cs b/src/Bevy.cs index d5bb152b..669d44e8 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -171,11 +171,6 @@ public void Run(IEnumerable systems, World world, uint ticks) } } -sealed class ThreadSchedule -{ - -} - internal sealed class StageContainer { @@ -528,7 +523,7 @@ private static Stage GetStage(Stages stage) } } -internal struct Placeholder where T : ISystemParam { public T Value; } +internal struct Placeholder where T : ISystemParam { public T Value; } public interface IPlugin @@ -567,7 +562,6 @@ public interface ISystemParam public interface ISystemParam : ISystemParam { - } public interface IIntoSystemParam @@ -886,12 +880,11 @@ public sealed class Res : SystemParam, IIntoSystemParam public static ISystemParam Generate(World arg) { - if (arg.Entity>>().Has>>()) - return arg.Entity>>().Get>>().Value; + var ent = arg.Entity>>(); + if (ent.Has>>()) + return ent.Get>>().Value; - var res = new Res(); - arg.Entity>>().Set(new Placeholder>() { Value = res }); - return res; + return null; } public static implicit operator T?(Res reference) @@ -1497,17 +1490,28 @@ public sealed class TinySystemAttribute : Attribute public sealed class SystemParamBuilder(World world) { - private readonly List _params = []; + private readonly List> _params = []; + private readonly List>> _paramsFns = []; public T Add() where T : ISystemParam, IIntoSystemParam { var param = (T)T.Generate(world); _params.Add(param); + _paramsFns.Add(() => (T)T.Generate(world)); return param; } - public ISystemParam[] Build() - => [.. _params]; + public (ISystemParam[], Func>[]) Build() + => ([.. _params], [.. _paramsFns]); +} + +public sealed class SystemParamRef where T : ISystemParam, IIntoSystemParam +{ + private readonly World _world; + private T? _t; + internal SystemParamRef(World world) => _world = world; + + public T Get() => _t ??= (T)T.Generate(_world); } public sealed class SystemConfiguration @@ -1538,7 +1542,7 @@ public static ITinySystem Chain(params ITinySystem[] systems) public interface ITinyMeta { - ISystemParam[] SystemParams { get; set; } + ISystemParam[] SystemParams { get; set; } SystemTicks Ticks { get; } SystemConfiguration Configuration { get; } @@ -1555,6 +1559,7 @@ public interface ITinySystem : ITinyMeta { SystemOrder OrderConfiguration { get; } + bool ParamsAreReady(); ITinySystem RunIf() where T : ITinyConditionalSystem, new(); ITinySystem RunIf(params ITinyConditionalSystem[] conditionals); ITinySystem RunAfter(ITinySystem sys); @@ -1565,11 +1570,13 @@ public interface ITinyConditionalSystem : ITinyMeta { } + public abstract class TinySystemBase : ITinyMeta { private bool _initialized; + private Func>[] _paramsFns = []; - public ISystemParam[] SystemParams { get; set; } = []; + public ISystemParam[] SystemParams { get; set; } = []; public SystemTicks Ticks { get; } = new(); public SystemConfiguration Configuration { get; } = new(); @@ -1582,7 +1589,7 @@ public virtual void Initialize(World world) var builder = new SystemParamBuilder(world); Setup(builder); - SystemParams = builder.Build(); + (SystemParams, _paramsFns) = builder.Build(); foreach (var conditional in Configuration.Conditionals) { @@ -1598,6 +1605,20 @@ public virtual bool AfterExecute(World world) public bool ExecuteOnReady(World world, uint ticks) { + if (!ParamsAreReady()) + { + for (var i = 0; i < SystemParams.Length; i++) + { + if (SystemParams[i] == null && i < _paramsFns.Length) + { + SystemParams[i] = _paramsFns[i](); + } + } + + if (!ParamsAreReady()) + return false; + } + Ticks.ThisRun = ticks; var canRun = BeforeExecute(world); @@ -1628,6 +1649,15 @@ public bool ExecuteOnReady(World world, uint ticks) return canRun; } + public bool ParamsAreReady() + { + foreach (var param in SystemParams) + if (param == null) + return false; + + return true; + } + public bool ParamsAreLocked() { return Configuration.ThreadingMode switch diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index e1de8038..36730d63 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -144,22 +144,19 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed // Get method parameters for dependency injection var parameters = method.Parameters.ToList(); - var fieldDeclarations = new StringBuilder(); var setupAssignments = new StringBuilder(); var methodCallParameters = new StringBuilder(); - // Generate field declarations and setup assignments + // Generate setup assignments and method call parameters for (int i = 0; i < parameters.Count; i++) { var param = parameters[i]; - var fieldName = $"_{param.Name}"; var paramType = param.Type.ToDisplayString(); - fieldDeclarations.AppendLine($" private {paramType} {fieldName};"); - setupAssignments.AppendLine($" {fieldName} = builder.Add<{paramType}>();"); + setupAssignments.AppendLine($" builder.Add<{paramType}>();"); if (i > 0) methodCallParameters.Append(", "); - methodCallParameters.Append(fieldName); + methodCallParameters.Append($"({paramType})SystemParams[{i}]"); } // For non-static methods, add a field for the instance @@ -182,10 +179,6 @@ private static void GenerateAdapterClass(SourceProductionContext context, INamed adapterClass.AppendLine("{"); adapterClass.IncrementIndent(); - // Fields - foreach (var line in fieldDeclarations.ToString().Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries)) - adapterClass.AppendLine(line); - if (method.IsStatic) { // Use namespace-qualified type for static call From 34c0cd62f86ddde43d7e87ab235c1c0bdbb7487b Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Fri, 19 Sep 2025 16:27:56 +0200 Subject: [PATCH 32/33] Update tools/TinyEcs.SourceGenerator/Program.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- tools/TinyEcs.SourceGenerator/Program.cs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tools/TinyEcs.SourceGenerator/Program.cs b/tools/TinyEcs.SourceGenerator/Program.cs index 36730d63..48e7301f 100644 --- a/tools/TinyEcs.SourceGenerator/Program.cs +++ b/tools/TinyEcs.SourceGenerator/Program.cs @@ -277,18 +277,6 @@ private static void ValidateMethodRequirements(SourceProductionContext context, } // Check if method is static - // if (!method.IsStatic) - // { - // var descriptor = new DiagnosticDescriptor( - // "TINYECS002", - // "TinySystem method must be static", - // "Method '{0}' with [TinySystem] attribute must be static", - // "TinyEcs", - // DiagnosticSeverity.Error, - // isEnabledByDefault: true); - // - // context.ReportDiagnostic(Diagnostic.Create(descriptor, location, method.Name)); - // } } private class SystemMethodInfo From fba7eb476a01528b9d5c412e9b0e5442939fa0b3 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Wed, 24 Sep 2025 08:33:26 +0200 Subject: [PATCH 33/33] null check --- src/Bevy.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bevy.cs b/src/Bevy.cs index 669d44e8..588e0817 100644 --- a/src/Bevy.cs +++ b/src/Bevy.cs @@ -1664,7 +1664,7 @@ public bool ParamsAreLocked() { ThreadingMode.Single => true, ThreadingMode.Multi => false, - _ => SystemParams.Any(static p => p.UseIndex > 0) + _ => SystemParams.Any(static p => p is { UseIndex: > 0 }) }; }