Skip to main content

Redirect metadata

The anonymous classes and anonymous functions generated by the compiler are unstable. Sometimes, just because a new type or function is added to the assembly, the names of a large number of anonymous classes and anonymous functions will change.

Even if the code of these anonymous classes themselves has not changed, because their names have changed, these anonymous classes and functions will be judged as changes when generating Dhao, causing these anonymous functions to be executed in an interpreted manner, which ultimately affects the running performance.

Issues with functions marked with [BurstCompile]

If the game uses burst-related technologies, the burst-related package will modify the functions marked with [BurstCompile] during compilation, for example:


[BurstCompile]
private static int DecryptNumber(int number, int mulFactor, int addFactor)
{
return (number - addFactor) / mulFactor;
}

Finally it will compile to


[BurstCompile]
private static int DecryptNumber(int number, int mulFactor, int addFactor)
{
return DecryptNumber_00000029$BurstDirectCall.Invoke(number, mulFactor, addFactor);
}

public delegate int DecryptNumber_00000029$PostfixBurstDelegate(int number, int mulFactor, int addFactor); internal static class DecryptNumber_00000029$BurstDirectCall { // Ignore some code... public unsafe static int Invoke(int number, int mulFactor, int addFactor) { if (BurstCompiler.IsEnabled) { IntPtr functionPointer = GetFunctionPointer(); if (functionPointer != (IntPtr)0) { return ((delegate* unmanaged[Cdecl]<int, int, int, int>)functionPointer)(number, mulFactor, addFactor); } } return DecryptNumber$BurstManaged(number, mulFactor, addFactor); }
}

If you add some other types to the assembly, even if you do not modify the code of DecryptNumber, the next time you compile, DecryptNumber_00000029$BurstDirectCall and DecryptNumber_00000029$PostfixBurstDelegate may become DecryptNumber_0000002A$BurstDirectCall and DecryptNumber_0000002A$PostfixBurstDelegate.

This not only causes DecryptNumber to be incorrectly executed in interpreted mode, but also causes runtime errors or even crashes on hybridclr versions lower than 7.4.0!

Redirect anonymous type names and anonymous function names

Since the ultimate version v7.4.0, HybridCLR.Editor.AssemblyMetaRetarget is provided for redirecting metadata to enhance the stability of metadata related to anonymous classes and anonymous functions.

Usage is as follows:

        private static void RetargetAssembly()
{
string dllName = "HotUpdate";

string dllDir = $"{Application.dataPath}/../Dlls";

string oldDllFile = $"{dllDir}/{dllName}.old.dll.bytes";
string newDllFile = $"{dllDir}/{dllName}.new.dll.bytes";
byte[] oldDllBytes = File.ReadAllBytes(oldDllFile);
byte[] newDllBytes = File.ReadAllBytes(newDllFile);

var retarget = new AssemblyMetaRetarget(oldDllBytes, newDllBytes);
retarget.Retarget();
retarget.Save($"{dllDir}/{dllName}.retargeted.dll.bytes");
}

Other notes

Redirecting metadata is optional. If you do not use [BurstCompile], you generally do not need to perform this operation.

After redirecting the metadata, please treat the generated assembly as the latest hot update assembly to generate dhao.