EvaisaDev / UnityNetcodePatcher

An assembly patcher which replicates the ILPostProcessing step unity does in order to make NetworkBehaviours work
MIT License
51 stars 6 forks source link

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.

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

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]

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

MSBuild

[!IMPORTANT] Since Visual Studio still uses 'full' MSBuild (which is based on .NET Framework), some dependencies targeting .NET Standard 2.1 cannot be loaded into the build host process. Using the CLI tool and post build event is recommended if you are using Visual Studio. You can use both the SDK and CLI tool depending on your build environemnt. See the example under "Usage with Visual Studio" below. 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>
MSBuild options ```xml // Output to `[assembly]_patched.dll` instead of renaming original assembly true // Don't publicize in parallel true // Override patched output path ./bin/foo/bar ```
Usage with Visual Studio If you want to support building in both environments (e.g. Visual Studio and `dotnet`) you can use CLI tool for Visual Studio builds, with a `Condition="'$(MSBuildRuntimeType)' != 'Core'"`. ```xml $(MSBuildWarningsAsMessages);NCP0001 ```

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