Skip to content

Commit ad71503

Browse files
Merge pull request #36 from Lordfirespeed/main
Restructure the patcher dir, fix type references
2 parents 41ad211 + 888f5b1 commit ad71503

File tree

12 files changed

+206
-30
lines changed

12 files changed

+206
-30
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ UnityProject/ProjectSettings/*
5151
/NetcodePatcher*/[Oo]bj/
5252
/NetcodePatcher*/[Dd]ist/
5353

54+
### Unity.Netcode.Runtime Project
55+
!/Unity.Netcode.Runtime/
56+
/Unity.Netcode.Runtime/[Bb]in/
57+
/Unity.Netcode.Runtime/[Oo]bj/
58+
/Unity.Netcode.Runtime/[Dd]ist/
59+
5460
# Explicit exceptions/ignores
5561
**.user
5662
!**.template.*.user

Directory.Build.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,6 @@
3333
<ItemGroup>
3434
<None Include="$(ProjectDir)../README.md" Pack="true" PackagePath="/"/>
3535
</ItemGroup>
36+
37+
<Import Project="$(SolutionDir)UnityNetcodePatcher.props.user" Condition="$(CI) != 'true'"/>
3638
</Project>

NetcodePatcher.Build/Program.cs

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,22 @@ public class BuildContext : FrostingContext
3333
public DirectoryPath PatcherProjectDirectory => RootDirectory.Combine("NetcodePatcher");
3434
public FilePath PatcherProjectFile => PatcherProjectDirectory.CombineWithFilePath("NetcodePatcher.csproj");
3535

36+
public DirectoryPath NetcodeRuntimeProjectDirectory => RootDirectory.Combine("Unity.Netcode.Runtime");
37+
public FilePath NetcodeRuntimeProjectFile => NetcodeRuntimeProjectDirectory.CombineWithFilePath("Unity.Netcode.Runtime.csproj");
38+
3639
public Version UnityVersion { get; }
3740
public Version UnityNetcodeVersion { get; }
3841
public Version UnityTransportVersion { get; }
3942
public bool UnityNetcodeNativeCollectionSupport { get; }
4043

41-
public string PatcherAssemblyName => $"NetcodePatcher.uv{UnityVersion.Major}.{UnityVersion.Minor}.nv{UnityNetcodeVersion}.tv{UnityTransportVersion}.{(UnityNetcodeNativeCollectionSupport ? "withNativeCollectionSupport" : "withoutNativeCollectionSupport")}";
44+
public DirectoryPath PatcherCommonOutputDirectory => PatcherProjectDirectory
45+
.Combine("dist")
46+
.Combine($"unity-v{UnityVersion.Major}.{UnityVersion.Minor}")
47+
.Combine($"unity-transport-v{UnityTransportVersion}");
48+
49+
public DirectoryPath PatcherSpecificOutputDirectory => PatcherCommonOutputDirectory
50+
.Combine($"netcode-v{UnityNetcodeVersion}")
51+
.Combine(UnityNetcodeNativeCollectionSupport ? "with-native-collection-support" : "without-native-collection-support");
4252

4353
public DirectoryPath? UnityEditorDir { get; }
4454

@@ -116,8 +126,16 @@ public sealed class CleanTask : FrostingTask<BuildContext>
116126
{
117127
public override void Run(BuildContext context)
118128
{
119-
context.CleanDirectories(context.RootDirectory.Combine("NetcodePatcher/bin").FullPath);
120-
context.CleanDirectories(context.RootDirectory.Combine("NetcodePatcher/obj").FullPath);
129+
CleanProject(context.PatcherProjectDirectory);
130+
CleanProject(context.NetcodeRuntimeProjectDirectory);
131+
return;
132+
133+
void CleanProject(DirectoryPath projectDirectory)
134+
{
135+
context.CleanDirectories(projectDirectory.Combine("bin").FullPath);
136+
context.CleanDirectories(projectDirectory.Combine("obj").FullPath);
137+
context.CleanDirectories(projectDirectory.Combine("dist").FullPath);
138+
}
121139
}
122140
}
123141

@@ -139,21 +157,43 @@ public override void Run(BuildContext context)
139157
{
140158
var buildSettings = new DotNetPublishSettings {
141159
Configuration = "Release",
142-
OutputDirectory = context.PatcherProjectDirectory.Combine($"dist/uv{context.UnityVersion.Major}.{context.UnityVersion.Minor}/tv{context.UnityTransportVersion}"),
160+
OutputDirectory = context.PatcherCommonOutputDirectory,
143161
MSBuildSettings = new() {
144162
Properties = {
145163
{"DefineConstants", [ string.Join("%3B", context.ComputeAllMSBuildConstants().ToArray()) ] },
146-
{"AssemblyName", [ context.PatcherAssemblyName ]}
147164
},
148165
},
149166
};
150167

151-
if (context.UnityEditorDir is not null) {
168+
if (context.UnityEditorDir is not null)
169+
{
152170
buildSettings.MSBuildSettings = buildSettings.MSBuildSettings
153171
.WithProperty("UnityEditorDir", context.UnityEditorDir.FullPath);
154172
}
155173

156174
context.DotNetPublish(context.PatcherProjectFile.FullPath, buildSettings);
175+
176+
context.EnsureDirectoryExists(context.PatcherSpecificOutputDirectory);
177+
MoveFileToSpecificOutputDirectory(CommonOutputFilePath("NetcodePatcher.deps.json"));
178+
MoveAssemblyToSpecificOutputDirectory("NetcodePatcher");
179+
MoveAssemblyToSpecificOutputDirectory("Unity.Netcode.Runtime");
180+
return;
181+
182+
void MoveAssemblyToSpecificOutputDirectory(string assemblyName)
183+
{
184+
MoveFileToSpecificOutputDirectory(CommonOutputFilePath($"{assemblyName}.dll"));
185+
MoveFileToSpecificOutputDirectory(CommonOutputFilePath($"{assemblyName}.pdb"));
186+
}
187+
188+
void MoveFileToSpecificOutputDirectory(FilePath file)
189+
{
190+
context.MoveFileToDirectory(file, context.PatcherSpecificOutputDirectory);
191+
}
192+
193+
FilePath CommonOutputFilePath(string outputFileName)
194+
{
195+
return context.PatcherCommonOutputDirectory.CombineWithFilePath(outputFileName);
196+
}
157197
}
158198
}
159199

NetcodePatcher.Tools.Common/PatcherConfiguration.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,37 @@
33

44
namespace NetcodePatcher.Tools.Common;
55

6-
public record PatcherConfiguration()
6+
public record PatcherConfiguration
77
{
88
public required Version UnityVersion { get; init; }
99
public required Version NetcodeVersion { get; init; }
1010
public required Version TransportVersion { get; init; }
1111
public required bool NativeCollectionSupport { get; init; }
1212

13-
public string PatcherAssemblyFileName => Path.Combine(
14-
$"uv{UnityVersion.Major}.{UnityVersion.Minor}",
15-
$"tv{TransportVersion}",
16-
$"NetcodePatcher.uv{UnityVersion.Major}.{UnityVersion.Minor}.nv{NetcodeVersion}.tv{TransportVersion}.{(NativeCollectionSupport ? "withNativeCollectionSupport" : "withoutNativeCollectionSupport")}.dll"
13+
private readonly Lazy<string> _executingDir = new(
14+
() => Path.GetFullPath(Path.GetDirectoryName(typeof(PatcherConfiguration).Assembly.Location)!)
15+
);
16+
public string ExecutingDir => _executingDir.Value;
17+
18+
public string PatcherCommonAssemblyDir => Path.Combine(
19+
ExecutingDir,
20+
$"unity-v{UnityVersion.Major}.{UnityVersion.Minor}",
21+
$"unity-transport-v{TransportVersion}"
22+
);
23+
24+
public string PatcherNetcodeSpecificAssemblyDir => Path.Combine(
25+
PatcherCommonAssemblyDir,
26+
$"netcode-v{NetcodeVersion}",
27+
NativeCollectionSupport ? "with-native-collection-support" : "without-native-collection-support"
28+
);
29+
30+
public string PatcherAssemblyFile => Path.Combine(
31+
PatcherNetcodeSpecificAssemblyDir,
32+
"NetcodePatcher.dll"
1733
);
1834

1935
public override string ToString()
2036
{
2137
return $"PatcherConfiguration {{\nUnity {UnityVersion},\nNetcode {NetcodeVersion},\nTransport {TransportVersion},\nNative collection support? {NativeCollectionSupport}\n}}";
2238
}
23-
};
39+
}
Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Diagnostics.CodeAnalysis;
23
using System.IO;
34
using System.Linq;
45
using System.Reflection;
@@ -8,12 +9,13 @@ namespace NetcodePatcher.Tools.Common;
89

910
class PatcherLoadContext : AssemblyLoadContext
1011
{
12+
private static readonly HashSet<string> SpecificAssemblyNames = [ "NetcodePatcher", "Unity.Netcode.Runtime" ];
1113
private static readonly HashSet<string> SharedDependencyAssemblyNames = [ "Serilog" ];
12-
private readonly AssemblyDependencyResolver _resolver;
14+
private readonly PatcherConfiguration _configuration;
1315

14-
public PatcherLoadContext(string name, string pluginPath) : base(name)
16+
public PatcherLoadContext(string name, PatcherConfiguration configuration) : base(name)
1517
{
16-
_resolver = new AssemblyDependencyResolver(pluginPath);
18+
_configuration = configuration;
1719
}
1820

1921
protected override Assembly? Load(AssemblyName assemblyName)
@@ -22,13 +24,49 @@ public PatcherLoadContext(string name, string pluginPath) : base(name)
2224
.FirstOrDefault(assembly => assembly.GetName() == assemblyName);
2325
if (loadedAssembly is not null) return loadedAssembly;
2426

25-
string? assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
27+
if (assemblyName.Name is null) return null;
28+
29+
if (SharedDependencyAssemblyNames.Contains(assemblyName.Name))
30+
return Default.LoadFromAssemblyName(assemblyName);
31+
32+
string? assemblyPath = ResolveAssemblyToPath(assemblyName);
2633
if (assemblyPath is null) return null;
34+
return LoadFromAssemblyPath(assemblyPath);
35+
}
2736

28-
if (SharedDependencyAssemblyNames.Contains(Path.GetFileNameWithoutExtension(assemblyPath))) {
29-
return Default.LoadFromAssemblyPath(assemblyPath);
37+
private string? ResolveAssemblyToPath(AssemblyName assemblyName)
38+
{
39+
if (assemblyName.Name is null) return null;
40+
41+
if (SpecificAssemblyNames.Contains(assemblyName.Name))
42+
{
43+
if (TryResolveSpecificAssemblyToPath(assemblyName, out var specificPath))
44+
return specificPath;
3045
}
3146

32-
return LoadFromAssemblyPath(assemblyPath);
47+
if (TryResolveCommonAssemblyToPath(assemblyName, out var commonPath))
48+
return commonPath;
49+
50+
return null;
51+
52+
bool TryResolveCommonAssemblyToPath(AssemblyName assemblyName, [MaybeNullWhen(false)] out string path)
53+
{
54+
path = Path.Combine(_configuration.PatcherCommonAssemblyDir, $"{assemblyName.Name}.dll");
55+
if (File.Exists(path))
56+
return true;
57+
58+
path = null;
59+
return false;
60+
}
61+
62+
bool TryResolveSpecificAssemblyToPath(AssemblyName assemblyName, [MaybeNullWhen(false)] out string path)
63+
{
64+
path = Path.Combine(_configuration.PatcherNetcodeSpecificAssemblyDir, $"{assemblyName.Name}.dll");
65+
if (File.Exists(path))
66+
return true;
67+
68+
path = null;
69+
return false;
70+
}
3371
}
3472
}

NetcodePatcher.Tools.Common/PatcherLoader.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ namespace NetcodePatcher.Tools.Common;
77

88
public class PatcherLoader
99
{
10+
private const string NetcodePatcherAssemblyNameString = "NetcodePatcher";
11+
private static readonly AssemblyName NetcodePatcherAssemblyName = new(NetcodePatcherAssemblyNameString);
12+
1013
private Type? _patcher;
1114

1215
public Type Patcher => _patcher ??= LoadPatcher();
@@ -33,9 +36,14 @@ private Type LoadPatcher() => LoadPatcherAssembly(_configuration)
3336

3437
private static Assembly LoadPatcherAssembly(PatcherConfiguration configuration)
3538
{
36-
try {
39+
try
40+
{
3741
return LoadPatcherAssemblyUnsafe(configuration);
3842
}
43+
catch (ArgumentException)
44+
{
45+
throw;
46+
}
3947
catch (FileNotFoundException exc) {
4048
throw new ArgumentException($"The supplied patch configuration is either unknown or unsupported.\n{configuration}", exc);
4149
}
@@ -46,15 +54,12 @@ private static Assembly LoadPatcherAssembly(PatcherConfiguration configuration)
4654

4755
private static Assembly LoadPatcherAssemblyUnsafe(PatcherConfiguration configuration)
4856
{
49-
var executingAssemblyDir = Path.GetDirectoryName(typeof(PatcherLoader).Assembly.Location)!;
50-
var patcherLocation = Path.GetFullPath(Path.Combine(executingAssemblyDir, configuration.PatcherAssemblyFileName));
51-
Log.Debug("Loading patcher from {PatcherLocation}", patcherLocation);
52-
PatcherLoadContext patcherLoadContext = new PatcherLoadContext("PatcherLoadContext", patcherLocation);
53-
var patcherAssemblyName = new AssemblyName(Path.GetFileNameWithoutExtension(patcherLocation));
57+
Log.Debug("Loading patcher from {PatcherLocation}", configuration.PatcherAssemblyFile);
58+
PatcherLoadContext patcherLoadContext = new PatcherLoadContext("PatcherLoadContext", configuration);
5459

5560
Assembly patcherAssembly;
5661
try {
57-
patcherAssembly = patcherLoadContext.LoadFromAssemblyName(patcherAssemblyName);
62+
patcherAssembly = patcherLoadContext.LoadFromAssemblyName(NetcodePatcherAssemblyName);
5863
}
5964
catch (FileNotFoundException exc) {
6065
throw new ArgumentException($"The supplied patch configuration is either unknown or unsupported.\n{configuration}", exc);

NetcodePatcher/NetcodePatcher.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<OutputType>Library</OutputType>
66

77
<RootNamespace></RootNamespace>
8+
<AssemblyName>NetcodePatcher</AssemblyName>
89
<TargetFramework>netstandard2.1</TargetFramework>
910
<DefineConstants>UNITY_2021_1_OR_NEWER; UNITY_2021_2_OR_NEWER; UNITY_2022_3_OR_NEWER; UNITY_EDITOR; UNITY_INCLUDE_TESTS</DefineConstants>
1011
<Nullable>disable</Nullable>
@@ -15,7 +16,7 @@
1516

1617
<!-- Development dependencies -->
1718
<ItemGroup>
18-
<PackageReference Include="BepInEx.AssemblyPublicizer.MSBuild" Version="0.4.1" PrivateAssets="all"/>
19+
1920
</ItemGroup>
2021

2122
<!-- Runtime dependencies -->
@@ -56,6 +57,7 @@
5657
<Reference Include="UnityEngine.TextRenderingModule">
5758
<HintPath>$(UnityEditorDir)/Data/Managed/UnityEngine/UnityEngine.TextRenderingModule.dll</HintPath>
5859
</Reference>
60+
<ProjectReference Include="$(ProjectDir)../Unity.Netcode.Runtime/Unity.Netcode.Runtime.csproj"/>
5961
</ItemGroup>
6062

6163
</Project>

NetcodePatcher/NetcodePatcher/CodeGen/ReplacePatcherReferencesILPP.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,16 @@ public override bool WillProcess(ICompiledAssembly compiledAssembly)
4646

4747
#region Process
4848

49-
Log.Information("Now processing");
49+
Log.Debug("Looking for reference to remove...");
5050

51+
var thisAssemblyName = GetType().Assembly.GetName().Name;
5152
var thisReference = assemblyDefinition.MainModule.AssemblyReferences
52-
.FirstOrDefault(reference => reference.Name == GetType().Assembly.GetName().Name);
53+
.FirstOrDefault(reference => reference.Name == thisAssemblyName);
5354

5455
if (thisReference is not null)
5556
{
5657
assemblyDefinition.MainModule.AssemblyReferences.Remove(thisReference);
57-
Log.Information("Removed shit reference");
58+
Log.Debug("Removed reference to {AssemblyName}", thisAssemblyName);
5859
}
5960

6061
#endregion
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Library</OutputType>
5+
6+
<RootNamespace></RootNamespace>
7+
<AssemblyName>Unity.Netcode.Runtime</AssemblyName>
8+
<TargetFramework>netstandard2.1</TargetFramework>
9+
<DefineConstants>UNITY_2021_1_OR_NEWER; UNITY_2021_2_OR_NEWER; UNITY_2022_3_OR_NEWER; UNITY_EDITOR; UNITY_INCLUDE_TESTS</DefineConstants>
10+
<Nullable>disable</Nullable>
11+
12+
<IsPackable>false</IsPackable>
13+
</PropertyGroup>
14+
15+
<ItemGroup>
16+
<InternalsVisibleTo Include="NetcodePatcher" />
17+
</ItemGroup>
18+
19+
<!-- Runtime dependencies -->
20+
<ItemGroup>
21+
<Reference Include="Unity.Burst">
22+
<HintPath>$(ProjectDir)../UnityProject/Library/ScriptAssemblies/Unity.Burst.dll</HintPath>
23+
</Reference>
24+
<Reference Include="Unity.Collections">
25+
<HintPath>$(ProjectDir)../UnityProject/Library/ScriptAssemblies/Unity.Collections.dll</HintPath>
26+
</Reference>
27+
<Reference Include="Unity.Collections.LowLevel.ILSupport">
28+
<HintPath>$(ProjectDir)../UnityProject/Library/ScriptAssemblies/Unity.Collections.LowLevel.ILSupport.dll</HintPath>
29+
</Reference>
30+
<Reference Include="Unity.CompilationPipeline.Common">
31+
<HintPath>$(UnityEditorDir)/Data/Managed/Unity.CompilationPipeline.Common.dll</HintPath>
32+
</Reference>
33+
<Reference Include="Unity.Networking.Transport">
34+
<HintPath>$(ProjectDir)../UnityProject/Library/ScriptAssemblies/Unity.Networking.Transport.dll</HintPath>
35+
</Reference>
36+
<Reference Include="Unity.Netcode.Components">
37+
<HintPath>$(ProjectDir)../UnityProject/Library/ScriptAssemblies/Unity.Netcode.Components.dll</HintPath>
38+
</Reference>
39+
<Reference Include="UnityEditor">
40+
<HintPath>$(UnityEditorDir)/Data/Managed/UnityEditor.dll</HintPath>
41+
</Reference>
42+
<Reference Include="UnityEngine">
43+
<HintPath>$(UnityEditorDir)/Data/Managed/UnityEngine/UnityEngine.dll</HintPath>
44+
</Reference>
45+
<Reference Include="UnityEngine.CoreModule">
46+
<HintPath>$(UnityEditorDir)/Data/Managed/UnityEngine/UnityEngine.CoreModule.dll</HintPath>
47+
</Reference>
48+
<Reference Include="UnityEngine.IMGUIModule">
49+
<HintPath>$(UnityEditorDir)/Data/Managed/UnityEngine/UnityEngine.IMGUIModule.dll</HintPath>
50+
</Reference>
51+
<Reference Include="UnityEngine.TextRenderingModule">
52+
<HintPath>$(UnityEditorDir)/Data/Managed/UnityEngine/UnityEngine.TextRenderingModule.dll</HintPath>
53+
</Reference>
54+
</ItemGroup>
55+
56+
</Project>
File renamed without changes.

0 commit comments

Comments
 (0)