Runtime Netcode Patcher
Patches Netcode RPC methods during runtime utilizing Harmony
Date uploaded | 9 months ago |
Version | 0.2.0 |
Download link | Ozone-Runtime_Netcode_Patcher-0.2.0.zip |
Downloads | 1306 |
Dependency string | Ozone-Runtime_Netcode_Patcher-0.2.0 |
This mod requires the following mods to function
BepInEx-BepInExPack
BepInEx pack for Mono Unity games. Preconfigured and ready to use.
Preferred version: 5.4.2100README
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:
- Reference Runtime Netcode RPC Validator: Either by utilizing a NuGet package inside visual studio
dotnet add package NicholaScott.BepInEx.RuntimeNetcodeRPCValidator --version 0.2.0
and add an[BepInDependency(RuntimeNetcodeRPCValidator.MyPluginInfo.PLUGIN_GUID, RuntimeNetcodeRPCValidator.MyPluginInfo.PLUGIN_VERSION)]
attribute to your[BepInPlugin]
. - Instantiate NetcodeValidator: Create and maintain a reference to an instance of
NetcodeValidator
and callNetcodeValidator.PatchAll()
. When you wish to revert any patches applied callDispose()
, orUnpatchSelf()
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();
}
// [[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) {
// Log the received name
Debug.Log(name);
// Tell all clients what the sender told us
TellAllOtherClients(NetworkBehaviourExtensions.LastSenderId, name);
}
[ClientRpc]
public void TellAllOtherClients(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()
{
// Are we a server instance?
if (IsHost)
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.
RunClientRpc();
}
}
}
Prerequisites
Ensure you have the following components within the environment:
- Unity's Netcode for GameObjects (NGO): For handling networked entities and communications.
- Harmony: A powerful library for patching, replacing and decorating .NET and Mono methods during runtime.
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
- Harmony - For runtime method patching.
- Unity's Netcode for GameObjects (NGO) - For robust networking in Unity.
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.
Contact
Discord: www.day.dream
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 anew NetcodeValidator(YourPluginGUID)
instead ofnew 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
- Pretty big README.md fix!
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 inNetworkBehaviourExtensions.cs
to streamline error logging. - Implemented a new network logger in
Plugin.cs
.
Changed
- Reorganized error reporting in
NetworkBehaviourExtensions.cs
, now utilizingLogErrorAndReturn
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 andRpcState
has been moved insideRpcData
. EnumeratorRpcState
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.