Skip to content

Commit

Permalink
check mismatched sign
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed May 31, 2024
1 parent da75c7f commit 3c1ce1d
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
2 changes: 1 addition & 1 deletion samples/MyBattleground/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
Or<(Without<Networked>, With<Position>)>
)>
>()
.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());
Expand Down
2 changes: 1 addition & 1 deletion src/Lookup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>), new (Value.ID, TermOp.Optional));
}

Expand Down
1 change: 1 addition & 0 deletions src/Match.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static int Validate(IComparer<ulong> comparer, EcsID[] ids, ReadOnlySpan<
{
switch (term.Op)
{
case TermOp.DataAccess:
case TermOp.With:
if (ids.All(id => ComponentComparer.CompareTerms(null, id, term.Id) != 0))

Check warning on line 16 in src/Match.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.

Check warning on line 16 in src/Match.cs

View workflow job for this annotation

GitHub Actions / build

Cannot convert null literal to non-nullable reference type.
{
Expand Down
9 changes: 6 additions & 3 deletions src/Query.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,12 @@ internal Query(World world, ImmutableArray<QueryTerm> 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<ContainerQueryTerm>()
foreach (var or in terms
.OfType<ContainerQueryTerm>()
.Where(s => s.Op == TermOp.Or))
{
subQuery = World.GetQuery
Expand All @@ -186,8 +190,7 @@ internal Query(World world, ImmutableArray<QueryTerm> terms)
public World World { get; internal set; }

internal CountdownEvent ThreadCounter { get; } = new CountdownEvent(1);


internal ImmutableArray<QueryTerm> TermsAccess { get; }

public void Dispose()
{
Expand Down
1 change: 1 addition & 0 deletions src/Term.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class ContainerQueryTerm(QueryTerm[] terms, TermOp op) : QueryTerm(0, op)

public enum TermOp : byte
{
DataAccess,
With,
Without,
Optional,
Expand Down
34 changes: 32 additions & 2 deletions tools/TinyEcs.Generator/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T{j}>().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}
Expand Down Expand Up @@ -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<T{j}>().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)
Expand Down

0 comments on commit 3c1ce1d

Please sign in to comment.