Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

The question about weaving and cecil.

Discussion in 'Scripting' started by watsonsong, Feb 9, 2020.

  1. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    I find the HLAPI use the cecil to weaving dll, and I try build my custom weaver to modify the UnityEngine.UI.dll, but find a problem.
    I want to create a delegate so I import an Action:
    Code (CSharp):
    1.  
    2. var handlerType = module.ImportReference(typeof(Action));
    3. var attrs = FieldAttributes.Private;
    4. var field = new FieldDefinition("m_Handler", attrs, type);
    5. @class.Fields.Add(field);
    6.  
    This works well, but if I change the signature of the Action, for example:
    Code (CSharp):
    1.  
    2. var handlerType = module.ImportReference(
    3.     typeof(Action<GameObject, BaseEventData, object>));
    4. var attrs = FieldAttributes.Private;
    5. var field = new FieldDefinition("m_Handler", attrs, type);
    6. @class.Fields.Add(field);
    7.  
    The specify signature seems broken the UnityEngine.UI.dll, and any other dll dependent on the UnityEngine.UI.dll is fail to load.
    Code (CSharp):
    1.  
    2. Error: Could not load signature of MyDLL.MyClass.UseTheUGUI:  due to: Could not load file or assembly 'UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. assembly:UnityEngine.UI, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null type:<unknown type> member:(null) signature:<none>
    3.  
    I don't know what the reason about it. Maybe the no parameter Action is already imported. But when I import an Action with specify generic signature, it change the dependency.

    I am really a newbie to the Cecil and dlls. Is there anyone can help me explain this, or I should avoid use ImportReference when I weaving some dlls?
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Not sure why you're taking this approach. Are you aware that the UnityEngine.UI DLL is almost entirely open-source? You can just download it here and get at whatever source you need to inspect, and/or even make your own custom versions with extra public hooks to get at.

    https://bitbucket.org/Unity-Technologies/ui/src/2019.1/
     
  3. watsonsong

    watsonsong

    Joined:
    May 13, 2015
    Posts:
    555
    Thanks, I just do not want to modify the UGUI package but able to use another package to modify it. This make it possible to use the package widely for variant projects I think.

    And I find the problem because I 'ImportReference' this type ‘BaseEventData’,but this type is inside this module, so it no need to import anymore. If I import a reference that existed in the same assembly, it seems make the assembly dependency on it self, and may cause some error. Just find in the module and use it, will not cause this error.