Ozone-Runtime_Netcode_Patcher icon

Runtime Netcode Patcher

Patches Netcode RPC methods during runtime utilizing Harmony

Last updated 10 months ago
Total downloads 5499575
Total rating 40 
Categories Tools Libraries BepInEx
Dependency string Ozone-Runtime_Netcode_Patcher-0.2.5
Dependants 5389 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

[RNV] Runtime Netcode Validator

Build

A BepInEx plugin utilizing HarmonyX to patch methods labeled with [ServerRpc] or [ClientRpc], inside a specified type that derives from NetworkBehaviour, to run the same format of checks NGO patches in during compile-time.

F.A.Q.

  • Why is my NetworkBehaviour added to a pre-existing NetworkObject not working?
    • RNV used to auto-magically patch your NetworkBehaviour::Constructor to call a custom method to synchronize with the parent NetworkObject (something that registering a new NetworkObject w/ NetworkPrefab, and .Spawn()ing avoids). To prevent confusion this is now a manual process but is still possible.
  • What does Runtime Netcode Validator mean?
    • In simplest terms, 'Netcode Validation' is a way to describe the process of verifying the associated information that makes a RPC(Remote Procedure Call) transmit it's information across the network and then doing that transmission. This is universal, the Runtime is what sets this apart, as it does it's "code insertion" at runtime.
  • How does this differ from what NGO (or implementations of the NGO IL emitter such as Weaver) does to my RPC methods?
    • This utilizes HarmonyX to patch your methods at runtime and add this 'validation' check in the form of a custom method with as little overhead as possible (along with other QoL NGO features). This comes with some benefits, like adding custom serialization (discussed later), as well as allowing you to un-patch your RPC methods.
  • What are the implications of runtime patching?
    • Arguments could be made about the memory overhead that patching causes but this should be minimal and no worse than if you were to patch any other method. The actual check itself is designed to be no more intrusive than the NGO checks and can be manually reviewed as the method that is called (Determining if your rpc should proceed or if we should transmit across the network) is under NetworkBehaviourExtensions::MethodPatchInternal
  • What can I put in the RPC parameters?
    • Typically NGO only allows you to send anything that implements INetworkSerializable which usually is fine; However if you want a simple struct or class and writing a whole INetworkSerializable implementation method just for a few variables is too much, then you can mark the object with a [System.Serializable] attribute and RNV will handle serializing it over the network as well as your normal INetworkSerializable parameters.

Table of Contents

Getting Started

  • Reference Runtime Netcode RPC Validator by installing the NuGet package from the terminal (in your projects directory):

    dotnet add package NicholaScott.BepInEx.RuntimeNetcodeRPCValidator

  • Add a BepInDependency attribute to your BaseUnityPlugin.

    [BepInDependency(RuntimeNetcodeRPCValidator.MyPluginInfo.PLUGIN_GUID, RuntimeNetcodeRPCValidator.MyPluginInfo.PLUGIN_VERSION)]

  • Instantiate NetcodeValidator: Create and maintain a reference to an instance of NetcodeValidator and call NetcodeValidator.PatchAll(). If, and only if, you wish to revert any patches applied you can call Dispose(), or UnpatchSelf() if you want to keep the instance for re-patching.

  • Define and Use RPCs: Ensure your Remote Procedure Calls on your NetworkBehaviours have the correct attribute and end their name with ServerRpc/ClientRpc.

Examples

For more robust examples check the Github Repo of the UnitTester plugin, which is used during development to verify codebase.

// Example of using NetcodeValidator
namespace SomePlugin {
    [BepInPlugin("My.Plugin.Guid", "My Plugin Name", "0.1.1")]
    [BepInDependency(RuntimeNetcodeRPCValidator.MyPluginInfo.PLUGIN_GUID, RuntimeNetcodeRPCValidator.MyPluginInfo.PLUGIN_VERSION)]
    public class MyPlugin : BaseUnityPlugin {
        private NetcodeValidator netcodeValidator;
        
        private void Awake()
        {
            netcodeValidator = new NetcodeValidator("My.Plugin.Guid");
            netcodeValidator.PatchAll();
            
            netcodeValidator.BindToPreExistingObjectByBehaviour<PluginNetworkingInstance, Terminal>();
        }
    }
}
// Example of using Server or Client RPCs. Naming conventions require the method to end with the corresponding attribute name.
namespace SomePlugin {
    public class PluginNetworkingInstance : NetworkBehaviour {
        [ServerRpc]
        public void SendUsDataServerRpc() {
            // Log the received name
            Debug.Log(name);
            // Tell all clients what the sender told us
            TellAllOtherClientsClientRpc(NetworkBehaviourExtensions.LastSenderId, name);
        }
        [ClientRpc]
        public void TellAllOtherClientsClientRpc(ulong senderId, string name) {
            Debug.Log(StartOfRound.Instance.allPlayerScripts.First(playerController => playerController.actualClientId == senderId).playerUsername + " is now " + name);
        }
        [ClientRpc]
        public void RunClientRpc() {
            // Send to the server what our preferred name is, f.e.
            SendPreferredNameServerRpc("Nicki");
        }
        private void Awake()
        {
            if (!IsHost) // Any clients should ask for sync of something :shrug:
                StartCoroutine(WaitForSomeTime());
        }

        private IEnumerator WaitForSomeTime()
        {
            // We need to wait because sending an RPC before a NetworkObject is spawned results in errors.
            yield return new WaitUntil(() => NetworkObject.IsSpawned);
        
            // Tell all clients to run this method.
            SendUsDataServerRpc();
        } 
    }
}

Notes

Utilize the NetworkBehaviourExtensions.LastSenderId property to retrieve the ID of the last RPC sender. This will always be NetworkManager.ServerClientId on the clients.

Pre-Existing NetworkObject

So you don't wanna make a prefab eh? Don't feel like registering it with the network? Afraid of what might come? Fear no more, as you can bind your NetworkBehaviour to a pre-existing (native) NetworkBehaviour utilizing a method anytime before NetworkManager is initialized. Generally this would be in your Plugins Awake, right after you create and patch with your NetcodeValidator. See the Examples above for usage and below for a detailed signature.

public void NetcodeValidator::BindToPreExistingObjectByBehaviour<TCustomBehaviour, TNativeBehaviour>()

Version Compliance

Acknowledgments

  • @Lordfirespeed for invaluable support and insights throughout the development.

Contributing

We welcome contributions! If you would like to help improve the RNV, please submit pull requests, and report bugs or suggestions in the issues section of this repository.

Contact

Discord: @Day