Skip to content

Commit cf81e59

Browse files
committed
run reformatter
1 parent d0aabf8 commit cf81e59

File tree

4 files changed

+113
-86
lines changed

4 files changed

+113
-86
lines changed

NetcodePatcher/CodeGen/ApplyPatchedAttributeILPP.cs

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using Cecilifier.Runtime;
55
using Mono.Cecil;
66
using Mono.Cecil.Cil;
7-
using Mono.Cecil.Rocks;
87
using Unity.CompilationPipeline.Common.Diagnostics;
98
using Unity.CompilationPipeline.Common.ILPostProcessing;
109
using Unity.Netcode.Editor.CodeGen;
@@ -18,24 +17,30 @@ public class ApplyPatchedAttributeILPP : ILPostProcessor
1817
public static readonly string AttributeNamespaceSuffix = "NetcodePatcher";
1918

2019
public static readonly string AttributeName = "NetcodePatchedAssemblyAttribute";
21-
22-
public override ILPostProcessor GetInstance() => this;
23-
24-
public override bool WillProcess(ICompiledAssembly compiledAssembly) => true;
2520

2621
private readonly List<DiagnosticMessage> m_Diagnostics = [];
2722
private PostProcessorAssemblyResolver m_AssemblyResolver;
28-
23+
24+
public override ILPostProcessor GetInstance()
25+
{
26+
return this;
27+
}
28+
29+
public override bool WillProcess(ICompiledAssembly compiledAssembly)
30+
{
31+
return true;
32+
}
33+
2934
// This function's implementation was written with the help of https://cecilifier.me/
3035
public override ILPostProcessResult? Process(ICompiledAssembly compiledAssembly)
3136
{
3237
if (!WillProcess(compiledAssembly)) return null;
33-
38+
3439
m_Diagnostics.Clear();
35-
36-
// read
40+
41+
// read
3742
var assemblyDefinition = CodeGenHelpers.AssemblyDefinitionFor(compiledAssembly, out m_AssemblyResolver);
38-
if (assemblyDefinition == null)
43+
if (assemblyDefinition is null)
3944
{
4045
m_Diagnostics.AddError($"Cannot read assembly definition: {compiledAssembly.Name}");
4146
return null;
@@ -49,25 +54,42 @@ public class ApplyPatchedAttributeILPP : ILPostProcessor
4954
assemblyDefinition.MainModule.ImportReference(typeof(Attribute))
5055
);
5156
assemblyDefinition.MainModule.Types.Add(cls_NetcodePatchedAttribute);
52-
57+
5358
// Add AttributeUsage(AttributeTargets.Assembly) to NetcodePatchedAttribute
54-
var attr_AttributeUsage = new CustomAttribute(assemblyDefinition.MainModule.ImportReference(typeof(AttributeUsageAttribute).GetConstructor([typeof(AttributeTargets)])));
55-
attr_AttributeUsage.ConstructorArguments.Add(new CustomAttributeArgument(assemblyDefinition.MainModule.ImportReference(typeof(AttributeTargets)), 4));
59+
var attr_AttributeUsage = new CustomAttribute(
60+
assemblyDefinition.MainModule.ImportReference(
61+
typeof(AttributeUsageAttribute).GetConstructor([typeof(AttributeTargets)])
62+
)
63+
);
64+
attr_AttributeUsage.ConstructorArguments.Add(
65+
new CustomAttributeArgument(assemblyDefinition.MainModule.ImportReference(typeof(AttributeTargets)), 4)
66+
);
5667
cls_NetcodePatchedAttribute.CustomAttributes.Add(attr_AttributeUsage);
57-
68+
5869
// Method : NetcodePatchedAttribute.ctor
59-
var ctor_NetcodePatchedAttribute = new MethodDefinition(".ctor", MethodAttributes.Assembly | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName | MethodAttributes.HideBySig, assemblyDefinition.MainModule.TypeSystem.Void);
70+
var ctor_NetcodePatchedAttribute = new MethodDefinition(
71+
".ctor",
72+
MethodAttributes.Assembly | MethodAttributes.RTSpecialName | MethodAttributes.SpecialName |
73+
MethodAttributes.HideBySig, assemblyDefinition.MainModule.TypeSystem.Void
74+
);
6075
cls_NetcodePatchedAttribute.Methods.Add(ctor_NetcodePatchedAttribute);
6176
ctor_NetcodePatchedAttribute.Body.InitLocals = true;
6277
var il_ctor_NetcodePatchedAttribute = ctor_NetcodePatchedAttribute.Body.GetILProcessor();
6378
il_ctor_NetcodePatchedAttribute.Emit(OpCodes.Ldarg_0);
64-
il_ctor_NetcodePatchedAttribute.Emit(OpCodes.Call, assemblyDefinition.MainModule.ImportReference(TypeHelpers.DefaultCtorFor(cls_NetcodePatchedAttribute.BaseType)));
79+
il_ctor_NetcodePatchedAttribute.Emit(
80+
OpCodes.Call,
81+
assemblyDefinition.MainModule.ImportReference(
82+
TypeHelpers.DefaultCtorFor(cls_NetcodePatchedAttribute.BaseType)
83+
)
84+
);
6585
il_ctor_NetcodePatchedAttribute.Emit(OpCodes.Ret);
66-
86+
6787
// Add NetcodePatchedAttribute to assembly definition
68-
var attribute = new CustomAttribute(assemblyDefinition.MainModule.ImportReference(TypeHelpers.DefaultCtorFor(cls_NetcodePatchedAttribute)));
88+
var attribute = new CustomAttribute(
89+
assemblyDefinition.MainModule.ImportReference(TypeHelpers.DefaultCtorFor(cls_NetcodePatchedAttribute))
90+
);
6991
assemblyDefinition.CustomAttributes.Add(attribute);
70-
92+
7193
// write
7294
var pe = new MemoryStream();
7395
var pdb = new MemoryStream();
@@ -80,7 +102,7 @@ public class ApplyPatchedAttributeILPP : ILPostProcessor
80102
};
81103

82104
assemblyDefinition.Write(pe, writerParameters);
83-
105+
84106
return new ILPostProcessResult(new InMemoryAssembly(pe.ToArray(), pdb.ToArray()), m_Diagnostics);
85107
}
86-
}
108+
}

NetcodePatcher/CodeGen/CompiledAssemblyFromFile.cs

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,60 @@ namespace NetcodePatcher.CodeGen;
1010

1111
public class CompiledAssemblyFromFile : ICompiledAssembly
1212
{
13-
readonly string _assemblyPath;
13+
private readonly string _assemblyPath;
14+
15+
public CompiledAssemblyFromFile(string assemblyPath)
16+
{
17+
_assemblyPath = assemblyPath;
18+
using var peSrcStream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read);
19+
20+
var pdbData = ReadPdb(peSrcStream);
21+
22+
peSrcStream.Seek(0, SeekOrigin.Begin);
23+
using var peStream = new MemoryStream();
24+
peSrcStream.CopyTo(peStream);
25+
var peData = peStream.ToArray();
26+
27+
InMemoryAssembly = new InMemoryAssembly(peData, pdbData);
28+
}
29+
30+
public bool DebugSymbolsAreEmbedded { get; private set; }
1431

1532
public string Name => Path.GetFileNameWithoutExtension(_assemblyPath);
1633
public string[] References { get; set; } = Array.Empty<string>();
1734
public string[] Defines { get; set; } = Array.Empty<string>();
18-
public bool DebugSymbolsAreEmbedded { get; private set; } = false;
1935
public InMemoryAssembly InMemoryAssembly { get; }
2036

2137
public byte[] ReadPdb(FileStream peStream)
2238
{
2339
using var peReader = new PEReader(peStream, PEStreamOptions.LeaveOpen);
24-
string assemblyName = Path.GetFileNameWithoutExtension(_assemblyPath);
25-
string pdbPath = $"{assemblyName}.pdb" + ".pdb";
26-
40+
var assemblyName = Path.GetFileNameWithoutExtension(_assemblyPath);
41+
var pdbPath = $"{assemblyName}.pdb" + ".pdb";
42+
2743
if (File.Exists(pdbPath))
2844
{
2945
Log.Information("Found debug info : ({PdbFileName})", Path.GetFileName(pdbPath));
3046
using var srcPdbStream = new FileStream(pdbPath, FileMode.Open, FileAccess.Read);
3147
if (!PdbConverter.IsPortable(srcPdbStream))
3248
throw new ArgumentException("Unsupported debug symbol type - should be 'portable'");
33-
49+
3450
srcPdbStream.Seek(0, SeekOrigin.Begin);
3551
using var pdbDataStream = new MemoryStream();
3652
srcPdbStream.CopyTo(pdbDataStream);
3753
return pdbDataStream.ToArray();
3854
}
39-
55+
4056
if (peReader.TryOpenAssociatedPortablePdb(_assemblyPath, File.OpenRead, out var pdbReaderProvider, out _))
4157
{
4258
var pdbReader = pdbReaderProvider!.GetMetadataReader();
43-
59+
4460
Log.Information("Found embedded debug info : ({AssemblyName})", assemblyName);
45-
61+
4662
DebugSymbolsAreEmbedded = true;
4763
return pdbReader.ReadAllBytes();
4864
}
4965

50-
throw new InvalidDataException($"Failed to discover portable debug information for {Path.GetFileName(_assemblyPath)}");
51-
}
52-
53-
public CompiledAssemblyFromFile(string assemblyPath)
54-
{
55-
_assemblyPath = assemblyPath;
56-
using var peSrcStream = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read);
57-
58-
byte[] pdbData = ReadPdb(peSrcStream);
59-
60-
peSrcStream.Seek(0, SeekOrigin.Begin);
61-
using var peStream = new MemoryStream();
62-
peSrcStream.CopyTo(peStream);
63-
byte[] peData = peStream.ToArray();
64-
65-
InMemoryAssembly = new InMemoryAssembly(peData, pdbData);
66+
throw new InvalidDataException(
67+
$"Failed to discover portable debug information for {Path.GetFileName(_assemblyPath)}");
6668
}
67-
}
69+
}
Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
using System;
22
using Unity.CompilationPipeline.Common.ILPostProcessing;
33

4-
namespace NetcodePatcher.CodeGen
4+
namespace NetcodePatcher.CodeGen;
5+
6+
public class CompiledAssemblyFromInMemoryAssembly : ICompiledAssembly
57
{
6-
public class CompiledAssemblyFromInMemoryAssembly : ICompiledAssembly
8+
public CompiledAssemblyFromInMemoryAssembly(InMemoryAssembly inMemoryAssembly, string name = "")
79
{
8-
readonly string _assemblyName;
9-
public string Name => _assemblyName;
10-
public string[] References { get; set; } = Array.Empty<string>();
11-
public string[] Defines { get; set; } = Array.Empty<string>();
12-
public InMemoryAssembly InMemoryAssembly { get; }
10+
InMemoryAssembly = inMemoryAssembly;
11+
Name = name;
12+
}
1313

14-
public CompiledAssemblyFromInMemoryAssembly(InMemoryAssembly inMemoryAssembly, string name = "")
15-
{
16-
InMemoryAssembly = inMemoryAssembly;
17-
_assemblyName = name;
18-
}
14+
public string Name { get; }
1915

20-
}
16+
public string[] References { get; set; } = Array.Empty<string>();
17+
public string[] Defines { get; set; } = Array.Empty<string>();
18+
public InMemoryAssembly InMemoryAssembly { get; }
2119
}

NetcodePatcher/CodeGen/NetcodeILPPApplicator.cs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ namespace NetcodePatcher.CodeGen;
1212

1313
public class NetcodeILPPApplicator
1414
{
15+
public NetcodeILPPApplicator(string assemblyPath, string outputPath, string[] references)
16+
{
17+
AssemblyPath = assemblyPath;
18+
OutputPath = outputPath;
19+
References = references;
20+
}
21+
1522
public Action<string> OnWarning { get; set; } = _ => { };
1623
public Action<string> OnError { get; set; } = _ => { };
1724

@@ -23,22 +30,16 @@ public class NetcodeILPPApplicator
2330
private string AssemblyFileName => Path.GetFileName(AssemblyPath);
2431
private string AssemblyDirName => Path.GetDirectoryName(AssemblyPath)!;
2532
private string PdbPath => Path.Combine(AssemblyDirName, $"{AssemblyName}.pdb");
26-
27-
public NetcodeILPPApplicator(string assemblyPath, string outputPath, string[] references)
28-
{
29-
AssemblyPath = assemblyPath;
30-
OutputPath = outputPath;
31-
References = references;
32-
}
33-
33+
3434
public static bool HasNetcodePatchedAttribute(ICompiledAssembly assembly)
3535
{
3636
// read
37-
AssemblyDefinition? assemblyDefinition = CodeGenHelpers.AssemblyDefinitionFor(assembly, out _);
37+
var assemblyDefinition = CodeGenHelpers.AssemblyDefinitionFor(assembly, out _);
3838
if (assemblyDefinition is null) return false;
3939

4040
return assemblyDefinition.CustomAttributes.Any(
41-
attribute => attribute.Constructor.DeclaringType.FullName.EndsWith($".{ApplyPatchedAttributeILPP.AttributeNamespaceSuffix}.{ApplyPatchedAttributeILPP.AttributeName}")
41+
attribute => attribute.Constructor.DeclaringType.FullName.EndsWith(
42+
$".{ApplyPatchedAttributeILPP.AttributeNamespaceSuffix}.{ApplyPatchedAttributeILPP.AttributeName}")
4243
);
4344
}
4445

@@ -50,7 +51,8 @@ public void ApplyProcesses()
5051
try
5152
{
5253
// read the original assembly from file
53-
assemblyFromFile = new CompiledAssemblyFromFile(AssemblyPath) {
54+
assemblyFromFile = new CompiledAssemblyFromFile(AssemblyPath)
55+
{
5456
References = References
5557
};
5658
}
@@ -62,9 +64,9 @@ public void ApplyProcesses()
6264

6365
var debugSymbolsAreEmbedded = assemblyFromFile.DebugSymbolsAreEmbedded;
6466
ICompiledAssembly assembly = assemblyFromFile;
65-
67+
6668
if (HasNetcodePatchedAttribute(assembly))
67-
{
69+
{
6870
Log.Warning("Skipping {FileName} as it has already been patched.", Path.GetFileName(AssemblyPath));
6971
return;
7072
}
@@ -77,7 +79,7 @@ public void ApplyProcesses()
7779
if (AssemblyPath == OutputPath)
7880
{
7981
// remove files with _original.dll and _original.pdb
80-
82+
8183
renameAssemblyPath = Path.Combine(AssemblyDirName, $"{AssemblyName}_original.dll");
8284
renamePdbPath = Path.Combine(AssemblyDirName, $"{AssemblyName}_original.pdb");
8385

@@ -98,19 +100,19 @@ public void ApplyProcesses()
98100
File.Move(PdbPath, renamePdbPath);
99101
}
100102

101-
ICompiledAssembly ApplyProcess<TProcessor>(ICompiledAssembly assemblyToApplyProcessTo) where TProcessor : ILPostProcessor, new()
103+
ICompiledAssembly ApplyProcess<TProcessor>(ICompiledAssembly assemblyToApplyProcessTo)
104+
where TProcessor : ILPostProcessor, new()
102105
{
103106
var ilpp = new TProcessor();
104107
if (!ilpp.WillProcess(assembly)) return assemblyToApplyProcessTo;
105108

106-
ILPostProcessResult result = ilpp.Process(assembly);
109+
var result = ilpp.Process(assembly);
107110

108111
if (result is null)
109112
return assemblyToApplyProcessTo;
110113

111114
// handle the error messages like Unity would
112-
foreach (DiagnosticMessage message in result.Diagnostics)
113-
{
115+
foreach (var message in result.Diagnostics)
114116
switch (message.DiagnosticType)
115117
{
116118
case DiagnosticType.Warning:
@@ -120,9 +122,9 @@ public void ApplyProcesses()
120122
OnError(message.MessageData + $"{message.File}:{message.Line}");
121123
continue;
122124
}
123-
}
124125

125-
return new CompiledAssemblyFromInMemoryAssembly(result.InMemoryAssembly, assemblyToApplyProcessTo.Name) {
126+
return new CompiledAssemblyFromInMemoryAssembly(result.InMemoryAssembly, assemblyToApplyProcessTo.Name)
127+
{
126128
References = References
127129
};
128130
}
@@ -137,19 +139,22 @@ public void ApplyProcesses()
137139
using var peStream = new MemoryStream(assembly.InMemoryAssembly.PeData);
138140
using var symbolStream = new MemoryStream(assembly.InMemoryAssembly.PdbData);
139141

140-
var assemblyDefinition = AssemblyDefinition.ReadAssembly(peStream, new ReaderParameters()
142+
var assemblyDefinition = AssemblyDefinition.ReadAssembly(peStream, new ReaderParameters
141143
{
142144
ReadSymbols = true,
143-
SymbolStream = symbolStream,
145+
SymbolStream = symbolStream
144146
});
145147

146148
assemblyDefinition.Write(OutputPath, new WriterParameters
147149
{
148-
SymbolWriterProvider = debugSymbolsAreEmbedded ? new EmbeddedPortablePdbWriterProvider() : new DefaultSymbolWriterProvider(),
149-
WriteSymbols = true,
150+
SymbolWriterProvider = debugSymbolsAreEmbedded
151+
? new EmbeddedPortablePdbWriterProvider()
152+
: new DefaultSymbolWriterProvider(),
153+
WriteSymbols = true
150154
});
151155

152-
Log.Information("Patched successfully : {FileName} -> {OutputPath}", Path.GetFileName(AssemblyPath), Path.GetFileName(OutputPath));
156+
Log.Information("Patched successfully : {FileName} -> {OutputPath}", Path.GetFileName(AssemblyPath),
157+
Path.GetFileName(OutputPath));
153158
}
154159
catch (Exception)
155160
{
@@ -172,4 +177,4 @@ public void ApplyProcesses()
172177
throw;
173178
}
174179
}
175-
}
180+
}

0 commit comments

Comments
 (0)