Skip to content

Commit 3802033

Browse files
committed
update submodule
1 parent 1e03f59 commit 3802033

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/OTAPI.UnifiedServerProcess/ModAssemblyMerger.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,20 @@
77
using OTAPI.UnifiedServerProcess.Extensions;
88
using System;
99
using System.Collections.Generic;
10+
using System.Collections.Immutable;
1011
using System.Linq;
1112

1213
namespace OTAPI.UnifiedServerProcess
1314
{
15+
public record struct MergeOption(ImmutableArray<string> IgnoreExistingMethods);
1416
public class ModAssemblyMerger
1517
{
18+
readonly HashSet<string> IgnoreExistingMethods;
1619
readonly Dictionary<string, ModuleDefinition> modModules = [];
17-
public ModAssemblyMerger(params System.Reflection.Assembly[] mods) {
20+
public ModAssemblyMerger(MergeOption option, params System.Reflection.Assembly[] mods) {
21+
22+
IgnoreExistingMethods = [.. option.IgnoreExistingMethods];
23+
1824
foreach (System.Reflection.Assembly assembly in mods) {
1925
var mod = AssemblyDefinition.ReadAssembly(assembly.Location);
2026
modModules.TryAdd(mod.FullName, mod.MainModule);
@@ -270,7 +276,7 @@ static MethodReference RedirectElementMethodRef(ModuleDefinition target, ModuleD
270276

271277
return mappedMethod;
272278
}
273-
static void SetModTypePlaceholder(ModuleDefinition module, Dictionary<string, TypeDefinition> uspTypes, TypeDefinition modType, TypeDefinition? declaringType) {
279+
void SetModTypePlaceholder(ModuleDefinition module, Dictionary<string, TypeDefinition> uspTypes, TypeDefinition modType, TypeDefinition? declaringType) {
274280
if (!uspTypes.TryGetValue(modType.FullName, out TypeDefinition? target)) {
275281
target = new TypeDefinition(modType.Namespace, modType.Name, modType.Attributes, modType.BaseType) {
276282
Attributes = modType.Attributes,
@@ -315,7 +321,8 @@ static void SetModTypePlaceholder(ModuleDefinition module, Dictionary<string, Ty
315321
SetModTypePlaceholder(module, uspTypes, nested, target);
316322
}
317323
}
318-
static void PrepareMethod(TypeDefinition targetType, MethodDefinition modMethod, MethodDefinition? originalMethod) {
324+
void PrepareMethod(TypeDefinition targetType, MethodDefinition modMethod, MethodDefinition? originalMethod) {
325+
bool ignored = false;
319326
if (modMethod.IsConstructor && !modMethod.IsStatic) {
320327
int instCount = 0;
321328
foreach (Instruction? inst in modMethod.Body.Instructions) {
@@ -324,14 +331,21 @@ static void PrepareMethod(TypeDefinition targetType, MethodDefinition modMethod,
324331
}
325332
}
326333
if (instCount <= 3 && originalMethod is not null) {
327-
TypeReference attType_ctor = modMethod.Module.ImportReference(typeof(MonoMod.MonoModIgnore));
328-
modMethod.CustomAttributes.Add(new CustomAttribute(new MethodReference(".ctor", modMethod.Module.TypeSystem.Void, attType_ctor) { HasThis = true }));
334+
if (!ignored) {
335+
ignored = true;
336+
TypeReference attType_ctor = modMethod.Module.ImportReference(typeof(MonoMod.MonoModIgnore));
337+
modMethod.CustomAttributes.Add(new CustomAttribute(new MethodReference(".ctor", modMethod.Module.TypeSystem.Void, attType_ctor) { HasThis = true }));
338+
}
329339
}
330340
else {
331341
TypeReference attType_ctor = modMethod.Module.ImportReference(typeof(MonoMod.MonoModConstructor));
332342
modMethod.CustomAttributes.Add(new CustomAttribute(new MethodReference(".ctor", modMethod.Module.TypeSystem.Void, attType_ctor) { HasThis = true }));
333343
}
334344
}
345+
if (!ignored && originalMethod is not null && IgnoreExistingMethods.Contains(modMethod.Name)) {
346+
TypeReference attType_ctor = modMethod.Module.ImportReference(typeof(MonoMod.MonoModIgnore));
347+
modMethod.CustomAttributes.Add(new CustomAttribute(new MethodReference(".ctor", modMethod.Module.TypeSystem.Void, attType_ctor) { HasThis = true }));
348+
}
335349
}
336350
static void SetMemberReplace(ModuleDefinition module, Collection<CustomAttribute> attributes, bool isEnum) {
337351
Type type = isEnum ? typeof(MonoMod.MonoModEnumReplace) : typeof(MonoMod.MonoModReplace);

src/OTAPI.UnifiedServerProcess/PatchExecutor.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,10 @@ public bool Patch(DirectoryInfo outputDir) {
103103

104104
var logger = new DefaultLogger(Logger.DEBUG);
105105

106-
new ModAssemblyMerger(typeof(TrProtocol.MessageID).Assembly)
106+
new ModAssemblyMerger(new(
107+
IgnoreExistingMethods: [nameof(ToString)]
108+
),
109+
typeof(TrProtocol.MessageID).Assembly)
107110
.Attach(modcontext);
108111

109112
modcontext.OnApply += (modType, modder) => {

src/TrProtocol

0 commit comments

Comments
 (0)