knah / Il2CppAssemblyUnhollower

A tool to generate Managed->IL2CPP proxy assemblies
GNU Lesser General Public License v3.0
499 stars 88 forks source link

add operator+ and operator- to MulticastDelegate types #72

Closed HookedBehemoth closed 2 years ago

HookedBehemoth commented 3 years ago

This allows simplifying code like this:

SceneManager.sceneLoaded = (
    (SceneManager.sceneLoaded == null)
    ? new Action<Scene, LoadSceneMode>(OnSceneLoad)
    : Il2CppSystem.Delegate.Combine(SceneManager.sceneLoaded, (UnityAction<Scene, LoadSceneMode>)new Action<Scene, LoadSceneMode>(OnSceneLoad)).Cast<UnityAction<Scene, LoadSceneMode>>()
    );

or:

SceneManager.sceneLoaded = Il2CppSystem.Delegate.Combine(SceneManager.sceneLoaded, new UnityAction<Scene, LoadSceneMode>(SceneManager.SceneLoaded)).Cast<UnityAction<Scene, LoadSceneMode>>();

into this:

SceneManager.sceneLoaded += new Action<Scene, LoadSceneMode>(OnSceneLoad);

Sadly C# was made by monkeys which can't draw a line between standard library and compiler so these operators don't exist by default. The language specification asks us to null-check parameters but luckily that's exactly what Delegate.Combine and Delegate.Remove do too.