Skip to content

Commit 3dac836

Browse files
committed
tweak patcher loader, configuration, and load context to load patcher from new folder structure
1 parent 6830026 commit 3dac836

File tree

3 files changed

+79
-20
lines changed

3 files changed

+79
-20
lines changed

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);

0 commit comments

Comments
 (0)