www_Day_Dream-Network_Behaviour_Event_Injector icon

Network Behaviour Event Injector

A preload patcher that injects virtual NetworkBehaviour methods on all sub-classes.

Last updated 10 months ago
Total downloads 370
Total rating 0 
Categories Libraries
Dependency string www_Day_Dream-Network_Behaviour_Event_Injector-1.0.0
Dependants 0 other packages depend on this package

This mod requires the following mods to function

BepInEx-BepInExPack-5.4.2100 icon
BepInEx-BepInExPack

BepInEx pack for Mono Unity games. Preconfigured and ready to use.

Preferred version: 5.4.2100

README

Network Behaviour Event Preloader

A Preload Patcher that adds the methods OnNetworkSpawn, OnNetworkDespawn, OnGainedOwnership, OnLostOwnership, OnNetworkObjectParentChanged, and OnDestroy to all NetworkBehaviours in 'Assembly-CSharp.dll'.

How It's Implemented

Besides sanity checks, all methods are implemented with the following code snippet (TLDR it calls base.OnXYZ() and returns):

private static void CreateEventMethod(AssemblyDefinition assemblyDefinition, MethodReference methodReference,
        TypeDefinition typeDef)
{
    var newMethod = new MethodDefinition(
        methodReference.Name,
        MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.Virtual,
        assemblyDefinition.MainModule.TypeSystem.Void);
    typeDef.Methods.Add(newMethod);
    newMethod.Body.InitLocals = true;
                
    foreach (var paramRef in methodReference.Parameters)
        newMethod.Parameters.Add(paramRef.Resolve());
                
    var processor = newMethod.Body.GetILProcessor();
                
    processor.Emit(OpCodes.Ldarg_0);
    for (var i = 0; i < methodReference.Parameters.Count; i++)
        processor.Emit(OpCodes.Ldarg, i + 1);
    processor.Emit(OpCodes.Call, methodReference);
    processor.Emit(OpCodes.Ret);
}

References to NetworkBehaviour are resolved from the 'Assembly-CSharp'.