From 8ded3f7708a1f7e4fb147f3c412f9b85c3ba378d Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Wed, 29 Nov 2023 01:25:58 +0000 Subject: [PATCH] Source generated queries are unusable if multiple worlds are being used. This is because the cached `Query` object will be used with the wrong `World`, leading to bad query results. This fixes that by storing which world the cache is relevant for. This is only a partial fix, the cache is still broken in the case of multithreaded access. --- Arch.System.SourceGenerator/Query.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Arch.System.SourceGenerator/Query.cs b/Arch.System.SourceGenerator/Query.cs index 8aea46a..e6395d7 100644 --- a/Arch.System.SourceGenerator/Query.cs +++ b/Arch.System.SourceGenerator/Query.cs @@ -260,15 +260,15 @@ partial class {{queryMethod.ClassName}}{ Exclusive = {{exclusiveTypeArray}} }; - private {{staticModifier}} bool _{{queryMethod.MethodName}}_Initialized; + private {{staticModifier}} World? _{{queryMethod.MethodName}}_Initialized; private {{staticModifier}} Query _{{queryMethod.MethodName}}_Query; [MethodImpl(MethodImplOptions.AggressiveInlining)] public {{staticModifier}} void {{queryMethod.MethodName}}Query(World world {{data}}){ - if(!_{{queryMethod.MethodName}}_Initialized){ + if(!ReferenceEquals(_{{queryMethod.MethodName}}_Initialized, world)) { _{{queryMethod.MethodName}}_Query = world.Query(in {{queryMethod.MethodName}}_QueryDescription); - _{{queryMethod.MethodName}}_Initialized = true; + _{{queryMethod.MethodName}}_Initialized = world; } foreach(ref var chunk in _{{queryMethod.MethodName}}_Query.GetChunkIterator()){