Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug [Entities 0.50] Codegen doesn't respect | in WithEntityQueryOptions

Discussion in 'Entity Component System' started by iamarugin, Apr 23, 2022.

  1. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    863
    Code:

    Code (CSharp):
    1.     Entities
    2.                 .WithEntityQueryOptions(EntityQueryOptions.FilterWriteGroup | EntityQueryOptions.IncludeDisabled)
    3.                 .WithName("DestroyEntitiesWithLinkedEntityGroup")
    4.                 .WithAll<LinkedEntityGroup>()
    5.                 .ForEach((Entity e, ref DestroyTag destroy) => { // do not forget, that write group should be requested with write permissions
    6.                         ecb.DestroyEntity(e);
    7.                 }).Schedule();
    Codegen result:

    Code (CSharp):
    1.         protected override void OnCreateForCompiler()
    2.         {
    3.             base.OnCreateForCompiler();
    4.             DestroyEntitiesWithLinkedEntityGroup_Query = GetEntityQuery(new Unity.Entities.EntityQueryDesc{All = new Unity.Entities.ComponentType[]{ComponentType.ReadOnly<Common.DestroyTag>(), ComponentType.ReadOnly<Unity.Entities.LinkedEntityGroup>()}, Any = new Unity.Entities.ComponentType[]{}, None = new Unity.Entities.ComponentType[]{}, Options = Unity.Entities.EntityQueryOptions.Default | Unity.Entities.EntityQueryOptions.IncludeDisabled});
    5.         }
     
    Last edited: Apr 23, 2022
  2. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    863
    Update: actually it can't handle | expression. If I wrote EntityQueryOptions.IncludeDisabled | EntityQueryOptions.FilterWriteGroup codegen generates ptions = Unity.Entities.EntityQueryOptions.Default | Unity.Entities.EntityQueryOptions.FilterWriteGroup
     
    Last edited: Apr 23, 2022
  3. Zec_

    Zec_

    Joined:
    Feb 9, 2017
    Posts:
    148
    I also noticed this but didn't get around to reporting it. I made a fix for it in their code generation that I can share. You'll need a local copy of the package in your project to make the fix though.

    In com.unity.entities@0.50.0-preview.24/Unity.Entities/SourceGenerators/Source~/Common/SourceGenHelpers.cs the original function looks like this, and it does not support parsing how the flag strings are structured:
    Code (csharp):
    1. public static bool TryParseQualifiedEnumValue<TEnum>(string value, out TEnum result) where TEnum : struct
    2. {
    3.     string parseString = value;
    4.     int loc = value.LastIndexOf('.');
    5.     if (loc > 0)
    6.         parseString = value.Substring(loc + 1);
    7.     return Enum.TryParse(parseString, out result) && Enum.IsDefined(typeof(TEnum), result);
    8. }
    9.  
    I rewrote it like this (not the most beautiful but it solves the job):
    Code (csharp):
    1. public static bool TryParseQualifiedEnumValue<TEnum>(string value, out TEnum result) where TEnum : struct
    2. {
    3.     var flags = value.Split('|');
    4.     var trimmedParseableFlags = flags.Select(x => x.Substring(x.LastIndexOf(".") + 1).Trim());
    5.     foreach (string flag in trimmedParseableFlags)
    6.     {
    7.         if (Enum.IsDefined(typeof(TEnum), flag) == false)
    8.         {
    9.             result = default;
    10.             return false;
    11.         }
    12.     }
    13.  
    14.     string parseString = string.Join(",", trimmedParseableFlags);
    15.     return Enum.TryParse(parseString, out result);
    16. }
    17.  
    After doing that, you'll need to run com.unity.entities@0.50.0-preview.24/Unity.Entities/SourceGenerators/Source~/bee.exe which rebuilds their code generation DLL's. After that you'll need to reload inside your project (potentially restart the editor, I don't know) and the next time code generation is triggered it will work.
     
    Last edited: Apr 25, 2022
  4. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    863
    Looks great! I hope this issue will be noticed by Unity.
     
  5. iamarugin

    iamarugin

    Joined:
    Dec 17, 2014
    Posts:
    863
    Just in case it was missed.