You are viewing a potentially older version of this package. View all versions.
Ozone-Runtime_Netcode_Patcher-0.1.1 icon

Runtime Netcode Patcher

Patches Netcode RPC methods during runtime utilizing Harmony

Date uploaded 6 months ago
Version 0.1.1
Download link Ozone-Runtime_Netcode_Patcher-0.1.1.zip
Downloads 280
Dependency string Ozone-Runtime_Netcode_Patcher-0.1.1

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

Runtime Unity Netcode Patcher

This plugin offers an easy-to-use solution for Netcode's NetworkBehaviour class, streamlining the approach to networking mods with Server and Client RPCs. By utilizing the CustomMessagingHandler of Netcode, it networks RPCs and their System.Serializable (Marked with [Serializable]) or INetworkSerializable parameters. While this is currently only in the Lethal Company directory, it can be expanded to other games upon request. Please reach out on Discord or via an issue here on Github for questions or contact.

Table of Contents

Getting Started

To integrate Runtime Unity Netcode Patcher in your Unity project, follow these steps:

  1. Reference the Output DLL: Include the output DLL in your project and add an [BepInDependency(RuntimeNetcodeRPCValidator.PluginInfo.GUID)] attribute to your [BepInPlugin].
  2. Instantiate NetcodeValidator: Create and maintain a reference to an instance of NetcodeValidator && call NetcodeValidator.PatchAll(). When you wish to revert any patches applied, simply call Dispose(), or UnpatchSelf() if you want to keep the instance, on the instance. A new instance can then be created to reapply the netcode patching.
  3. Define and Use RPCs: Ensure your Remote Procedure Calls on your NetworkBehaviours have the correct attribute and end their name with ServerRpc/ClientRpc.

Examples

// Example of using NetcodeValidator
namespace SomePlugin {
    public class MyPlugin : BaseUnityPlugin {
        private NetcodeValidator netcodeValidator;
        
        private void Awake()
        {
            netcodeValidator = new NetcodeValidator(this);
            netcodeValidator.PatchAll();
        }
        
        // [[OPTIONAL DISPOSE TO UNPATCH]]
        private void OnDestroy()
        {
            netcodeValidator.Dispose();
        }
    }
}
// Example of using Server or Client RPCs. Naming conventions require the method to end with the corresponding attribute name.
namespace SomePlugin {
    // This assumes you've declared a BaseUnityPlugin and Harmony instance elsewhere. Including the previous snippet about NetcodeValidator.
    [HarmonyPatch(typeof(Terminal), "Start")]
    private static class Patch {
        [HarmonyPrefix]
        private static void AddToTerminalObject(Terminal __instance) {
            __instance.gameObject.AddComponent<PluginNetworkingInstance>();
        }
    }
    public class PluginNetworkingInstance : NetworkBehaviour {
        [ServerRpc]
        public void SendPreferredNameServerRpc(string name) {
            Debug.Log(name);
            TellAllOtherClients(NetworkBehaviourExtensions.LastSenderId, name);
        }
        [ClientRpc]
        public void TellEveryoneClientRpc(ulong senderId, string name) {
            Debug.Log(StartOfRound.Instance.allPlayerScripts.First(playerController => playerController.actualClientId == senderId).playerUsername + " is now " + name);
        }
        [ClientRpc]
        public void RunClientRpc() {
            SendPreferredNameServerRpc("Nicki");
        }
        public void Awake()
        {
            if (NetworkManager.Instance.IsHost)
                RunClientRpc();
        }
    }
}

Prerequisites

Ensure you have the following components within the environment:

Notes

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

Built With

Acknowledgments

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

Contributing

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

CHANGELOG

Changelog

v0.2.0

Changed

  • The final changes to the NetcodeValidator class occur w/ this update. This will be the final form, the only change on your end is a new NetcodeValidator(YourPluginGUID) instead of new NetcodeValidator(YourPlugin). This is to be more in line with how Harmony instances are handled and to make things easier on the back-end. This includes a few new methods listed below following the pattern of the Harmony class.
  • NetcodeValidator.Patch(Type type)
  • NetcodeValidator.Patch(Assembly assembly)
  • NetcodeValidator.PatchAll()

v0.1.8

Fixed

v0.1.7

Fixed

  • Incorrect error logging in a few places.
  • Project structure was out of control. It's now.. In control.
  • Git fixes.

v0.1.1

Added

  • Introduced a new LogErrorAndReturn method in NetworkBehaviourExtensions.cs to streamline error logging.
  • Implemented a new network logger in Plugin.cs.

Changed

  • Reorganized error reporting in NetworkBehaviourExtensions.cs, now utilizing LogErrorAndReturn to improve coding practices.
  • Pruned extraneous variables in RpcData struct.
  • Modified package dependencies in .csproj file, removing unnecessary ones and adding a supplemental package reference.

v0.1.0

Removed

  • Imports: The namespaces System.Collections.Generic, System.IO, System.Runtime.Serialization.Formatters.Binary, and HarmonyLib have been removed.
  • Data structures: The dictionaries NetRPCStates, NetRPCData, NetRPCParams, NetRPCSender have been removed which were being used to track RPC state, data, params, and senders.
  • Methods: The methods GetNetworkState, GetNetworkData, LastRPCSender, VerifyAsRegisteredWithNetworkObject, ValidateRPCExecution, PrepareRPCParamsForSending, SendRPC, ProcessRPC have been removed.

Added

  • Classes: New inner class RpcData has been added.
  • Enumerators: An Enumerator RpcSource has been added to the RpcData class and RpcState has been moved inside RpcData. Enumerator RpcState values changed from[AwaitingMessage, MessageReceived] to [FromUser, FromNetworking].
  • Methods: The method LastSenderId has been added to get the last SenderId from RpcData.
  • MethodPatchInternal Updates: The logic of MethodPatchInternal has been changed quite significantly.