From 3c1ce1de6e334b305ffefb3226f2940e2fe87d67 Mon Sep 17 00:00:00 2001 From: andreakarasho Date: Sat, 1 Jun 2024 01:34:00 +0200 Subject: [PATCH] check mismatched sign --- samples/MyBattleground/Program.cs | 2 +- src/Lookup.cs | 2 +- src/Match.cs | 1 + src/Query.cs | 9 +++++--- src/Term.cs | 1 + tools/TinyEcs.Generator/Program.cs | 34 ++++++++++++++++++++++++++++-- 6 files changed, 42 insertions(+), 7 deletions(-) diff --git a/samples/MyBattleground/Program.cs b/samples/MyBattleground/Program.cs index 323951f..e5f08f6 100644 --- a/samples/MyBattleground/Program.cs +++ b/samples/MyBattleground/Program.cs @@ -40,7 +40,7 @@ Or<(Without, With)> )> >() - .Each((EntityView e, ref Position maybe) => { + .Each((EntityView e, ref Position maybe, ref ManagedData data) => { var isNull = Unsafe.IsNullRef(ref maybe); Console.WriteLine("is null {0} - {1}", isNull, e.Name()); diff --git a/src/Lookup.cs b/src/Lookup.cs index 66cb369..a6d8f51 100644 --- a/src/Lookup.cs +++ b/src/Lookup.cs @@ -82,7 +82,7 @@ static Component() if (Size > 0) { - _typesConvertion.Add(typeof(T), new (Value.ID, TermOp.With)); + _typesConvertion.Add(typeof(T), new (Value.ID, TermOp.DataAccess)); _typesConvertion.Add(typeof(Optional), new (Value.ID, TermOp.Optional)); } diff --git a/src/Match.cs b/src/Match.cs index fe1df65..baa971a 100644 --- a/src/Match.cs +++ b/src/Match.cs @@ -11,6 +11,7 @@ public static int Validate(IComparer comparer, EcsID[] ids, ReadOnlySpan< { switch (term.Op) { + case TermOp.DataAccess: case TermOp.With: if (ids.All(id => ComponentComparer.CompareTerms(null, id, term.Id) != 0)) { diff --git a/src/Query.cs b/src/Query.cs index 7e93a6c..12564a4 100644 --- a/src/Query.cs +++ b/src/Query.cs @@ -168,8 +168,12 @@ internal Query(World world, ImmutableArray terms) .ToImmutableSortedSet() .ToImmutableArray(); + TermsAccess = terms.Where(s => s.Op == TermOp.DataAccess || s.Op == TermOp.Optional) + .ToImmutableArray(); + ref var subQuery = ref _subQuery; - foreach (var or in terms.OfType() + foreach (var or in terms + .OfType() .Where(s => s.Op == TermOp.Or)) { subQuery = World.GetQuery @@ -186,8 +190,7 @@ internal Query(World world, ImmutableArray terms) public World World { get; internal set; } internal CountdownEvent ThreadCounter { get; } = new CountdownEvent(1); - - + internal ImmutableArray TermsAccess { get; } public void Dispose() { diff --git a/src/Term.cs b/src/Term.cs index b357068..057e3bb 100644 --- a/src/Term.cs +++ b/src/Term.cs @@ -25,6 +25,7 @@ public class ContainerQueryTerm(QueryTerm[] terms, TermOp op) : QueryTerm(0, op) public enum TermOp : byte { + DataAccess, With, Without, Optional, diff --git a/tools/TinyEcs.Generator/Program.cs b/tools/TinyEcs.Generator/Program.cs index fd494d7..3fdde45 100644 --- a/tools/TinyEcs.Generator/Program.cs +++ b/tools/TinyEcs.Generator/Program.cs @@ -190,13 +190,29 @@ public partial {className} var getQuery = withFilter ? $"Query<{(i > 0 ? "(" : "")}{typeParams}{(i > 0 ? ")" : "")}>()" : "this"; var worldLock = !withFilter ? "World." : ""; var incFieldList = GenerateSequence(i + 1, "\n", j => $"var inc{j} = column{j} < 0 ? 0 : 1;"); + var validationTypes = GenerateSequence(i + 1, "\n", j => { + if (i + 1 <= 1) + { + return ""; + } + + var str = $"EcsAssert.Panic(query.TermsAccess[{j}].Id == query.World.Entity().ID," + + "$\"'{typeof("+ $"T{j}" + ")}' doesn't match the QueryData sign\");"; + + return str; + }); sb.AppendLine($@" public void Each<{typeParams}>({delegateName}<{typeParams}> fn) {whereParams} {{ + var query = {getQuery}; + + {($"EcsAssert.Panic(query.TermsAccess.Length == {i + 1}, \"mismatched sign\");")} + {validationTypes} + {worldLock}BeginDeferred(); - foreach (var arch in {getQuery}) + foreach (var arch in query) {{ {columnIndices} {incFieldList} @@ -260,12 +276,26 @@ public partial {className} var getQuery = withFilter ? $"Query<{(i > 0 ? "(" : "")}{typeParams}{(i > 0 ? ")" : "")}>()" : "this"; var worldLock = !withFilter ? "World." : ""; var incFieldList = GenerateSequence(i + 1, "\n", j => $"var inc{j} = column{j} < 0 ? 0 : 1;"); + var validationTypes = GenerateSequence(i + 1, "\n", j => { + if (i + 1 <= 1) + { + return ""; + } + + var str = $"EcsAssert.Panic(query.TermsAccess[{j}].Id == query.World.Entity().ID," + + "$\"'{typeof("+ $"T{j}" + ")}' doesn't match the QueryData sign\");"; + + return str; + }); sb.AppendLine($@" public void EachJob<{typeParams}>({delegateName}<{typeParams}> fn) {whereParams} {{ - {worldLock}BeginDeferred(); var query = {getQuery}; + {($"EcsAssert.Panic(query.TermsAccess.Length == {i + 1}, \"mismatched sign\");")} + {validationTypes} + + {worldLock}BeginDeferred(); var cde = query.ThreadCounter; cde.Reset(); foreach (var arch in query)