Evaisa.NetcodePatcher.Cli 4.1.1

There is a newer version of this package available.
See the version list below for details.
dotnet tool install --global Evaisa.NetcodePatcher.Cli --version 4.1.1                
This package contains a .NET tool you can call from the shell/command line.
dotnet new tool-manifest # if you are setting up this repo
dotnet tool install --local Evaisa.NetcodePatcher.Cli --version 4.1.1                
This package contains a .NET tool you can call from the shell/command line.
#tool dotnet:?package=Evaisa.NetcodePatcher.Cli&version=4.1.1                
nuke :add-package Evaisa.NetcodePatcher.Cli --version 4.1.1                

Unity Netcode Patcher

GitHub Build Status NetcodePatcher Nuget NetcodePatcher.MSBuild Nuget NetcodePatcher.Cli Nuget

This is an assembly patcher which replicates the IL Post Processing that unity does with it's Netcode For Gameobjects Package, allowing you to create custom NetworkBehaviours in mods as if you were doing it in a Unity project.

  • This was originally written for Lethal Company modding, and has only been tested with com.unity.netcode.gameobjects@1.5.2

Note, this is intended to be a tool for modders, mods should be shipped after patching and this tool should not be installed by users.

Preparing mods for patching

  • Make sure Debug Symbols are set to Portable or Embedded and not Full.
  • To ensure that the patched NetworkBehaviours are initialized properly, add the following code snippet to your mod, in a place where it will only run once, such as Awake()
    • It is very important that it only runs once!

    • If you have soft dependencies of some kind, you might need to wrap this in a try catch block.

      var types = Assembly.GetExecutingAssembly().GetTypes();
      foreach (var type in types)
      {
          var methods = type.GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static);
          foreach (var method in methods)
          {
              var attributes = method.GetCustomAttributes(typeof(RuntimeInitializeOnLoadMethodAttribute), false);
              if (attributes.Length > 0)
              {
                  method.Invoke(null, null);
              }
          }
      }
      
    • The reason we need to do this is because NetcodePatcher generates methods marked with [RuntimeInitializeOnLoadMethod] for initializing the RPCs, which normally get ran when the class gets loaded. However because the mod assembly is not managed by unity, these methods will not be ran automatically. So using this snippet we manually run every method marked with [RuntimeInitializeOnLoadMethodAttribute].

  • Make you register any custom NetworkObject prefabs with the unity NetworkManager.
    • networkManager.GetComponent<NetworkManager>().AddNetworkPrefab(prefab);

Usage

CLI

The CLI is available as a .NET 7/8 tool. Install it using dotnet:

dotnet tool install -g Evaisa.NetcodePatcher.Cli

Then use the netcode-patch command to patch your plugin.

netcode-patch -nv 1.5.2 [plugin] [dependencies]
  • plugin should be the path to the patch target (your plugin assembly .dll)
  • dependencies should be a path (or list of paths) containing all assemblies referenced by your project, the Unity.Netcode.Runtime assembly, etc. (e.g. the Managed folder of a game installation)

Run netcode-patch --help for usage information and available options.

MSBuild

[!IMPORTANT] Due to issues with Visual Studio the MSBuild plugin is not currently working properly with it, using the CLI tool and post build event is recommended if you are using Visual Studio. Alternatively you can manually run dotnet build from commandline if you do want to use MSBuild.

NetcodePatcher has an MSBuild plugin that can be applied with minimal configuration. Add the following snippet within the root <Project> tag of your .csproj project file to automatically netcode patch the project's output assemblies.

<Sdk Name="Evaisa.NetcodePatcher.MSBuild" Version="4.*" />
<ItemGroup>
  <NetcodePatch Include="$(TargetPath)" />
</ItemGroup>

<details> <summary>MSBuild options</summary>

<Project>
  <PropertyGroup>
    // Output to `[assembly]_patched.dll` instead of renaming original assembly
    <NetcodePatcherNoOverwrite>true</NetcodePatcherNoOverwrite>
    // Don't publicize in parallel
    <NetcodePatcherDisableParallel>true</NetcodePatcherDisableParallel>
  </PropertyGroup>

  <ItemGroup>
    <NetcodePatch Include="$(TargetPath)">
      // Override patched output path
      <OutputPath>./bin/foo/bar</OutputPath>
    </NetcodePatch>
  </ItemGroup>
</Project>

</details>

Manual

  1. Download the latest release asset for your platform.
  2. Unpack the .zip archive to a memorable location
  3. Copy-paste the contents of your game's [game]_Data/Managed directory into the extracted deps folder
  4. Place your patch target plugins in the extracted plugins folder
  5. Use the extracted executable file (assuming your CWD is the extracted directory):
    NetcodePatcher(.exe) -nv 1.5.2 plugins deps
    

Programmatic API

NetcodePatcher is also available programmatically.

Add the dotnet-tools NuGet source to your NuGet.Config:

<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />

Then Add a package reference to Evaisa.NetcodePatcher to your .csproj project:

<ItemGroup>
  <PackageReference Include="Evaisa.NetcodePatcher" Version="3.*" />
</ItemGroup>
using NetcodePatcher;

Patcher.Patch(string inputPath, string outputPath, string[] dependencyPaths);

Usage as a Post Build Event

To ensure quotes are not escaped incorrectly, it is recommended you add this target by manually editing your .csproj project file as opposed to using Visual Studio UI to add a post-build command.

[!IMPORTANT] if you installed the CLI tool locally instead of globally, you need to add dotnet infront of the command, so dotnet netcode-patch

<Target Name="NetcodePatch" AfterTargets="PostBuildEvent">
    <Exec Command="netcode-patch -nv 1.5.2 &quot;$(TargetPath)&quot; @(ReferencePathWithRefAssemblies->'&quot;%(Identity)&quot;', ' ')"/>
</Target>

Contributing

You will need to git submodule update --init --recursive to fetch submodules, and create a .csproj.user file to tell the NetcodePatcher plugin where Unity Editor is installed.

Template NetcodePatcher/NetcodePatcher.csproj.user

<?xml version="1.0" encoding="utf-8"?>
<Project>
  <PropertyGroup>
    <UnityEditorDir>$(ProgramFiles)/Unity/Hub/Editor/2022.3.9f1/Editor</UnityEditorDir>
  </PropertyGroup>
</Project>

Credits

  • nickklmao
    • for helping me test and find issues with the patcher.
  • Lordfirespeed
Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

Version Downloads Last updated
4.3.0 2,989 6/23/2024
4.2.0 1,990 3/30/2024
4.1.1 511 3/28/2024
4.1.0 903 3/10/2024
4.0.2 530 3/2/2024
4.0.1 340 3/2/2024
4.0.0 283 3/2/2024
3.3.4 1,358 1/14/2024
3.3.3 846 1/11/2024
3.3.2 188 1/11/2024
3.3.1 249 1/11/2024
3.3.0 179 1/11/2024
3.2.0 183 1/10/2024
3.1.1 229 1/8/2024
3.1.0 168 1/8/2024
3.0.4 188 1/7/2024
3.0.3 168 1/7/2024