Skip to content

Commit 188e755

Browse files
Merge pull request #12 from Lordfirespeed/dev
Improve MSBuild task logging, fix overzealous error handling issue
2 parents 00b2ee3 + 0fa0371 commit 188e755

File tree

7 files changed

+144
-47
lines changed

7 files changed

+144
-47
lines changed

NetcodePatcher.Cli/NetcodePatchCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private static void Handle(FileSystemInfo plugin, FileSystemInfo[] dependencies,
4444
var toolVersion = typeof(Program).Assembly
4545
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!
4646
.InformationalVersion;
47-
Log.Information("Initializing NetcodePatcher v{Version}", toolVersion);
47+
Log.Information("Initializing NetcodePatcher v{Version:l}", toolVersion);
4848

4949
Log.Debug("Provided 'plugins' input: {Plugins}", plugin);
5050
Log.Debug("Provided 'dependencies' input: {Dependencies}", dependencies);

NetcodePatcher.Cli/NetcodePatcher.Cli.csproj

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
<!-- Runtime dependencies -->
1616
<ItemGroup>
1717
<PackageReference Include="Serilog" Version="3.1.1"/>
18-
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0"/>
19-
<PackageReference Include="Serilog.Sinks.File" Version="4.1.0"/>
18+
<PackageReference Include="Serilog.Sinks.Console" Version="5.0.1"/>
19+
<PackageReference Include="Serilog.Sinks.File" Version="5.0.0"/>
2020
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1"/>
2121
<PackageReference Include="System.CommandLine.NamingConventionBinder" Version="2.0.0-beta4.22272.1"/>
2222
</ItemGroup>
@@ -25,9 +25,6 @@
2525
<ItemGroup>
2626
<ProjectReference Include="$(ProjectDir)../NetcodePatcher/NetcodePatcher.csproj"/>
2727
</ItemGroup>
28-
<ItemGroup>
29-
<Folder Include="dist\" />
30-
</ItemGroup>
3128

3229
<Target Name="BuildLegacyPackage" AfterTargets="Pack">
3330
<PropertyGroup>

NetcodePatcher.MSBuild/NetcodePatchTask.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Linq;
5+
using System.Reflection;
46
using System.Threading.Tasks;
57
using Microsoft.Build.Framework;
8+
using Serilog;
69
using Task = Microsoft.Build.Utilities.Task;
710

811
namespace NetcodePatcher.MSBuild;
@@ -21,6 +24,16 @@ public class NetcodePatchTask : Task
2124

2225
public override bool Execute()
2326
{
27+
Serilog.Log.Logger = new LoggerConfiguration()
28+
.MinimumLevel.Information()
29+
.WriteTo.TaskLoggingHelper(Log)
30+
.CreateLogger();
31+
32+
var toolVersion = typeof(NetcodePatchTask).Assembly
33+
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()!
34+
.InformationalVersion;
35+
Serilog.Log.Information("Initializing NetcodePatcher v{Version:l}", toolVersion);
36+
2437
var noOverwrite = false;
2538
if (!string.IsNullOrEmpty(NoOverwrite))
2639
{
@@ -33,6 +46,8 @@ public override bool Execute()
3346
disableParallel = bool.Parse(DisableParallel);
3447
}
3548

49+
var stopwatch = Stopwatch.StartNew();
50+
3651
void RunPatch(ITaskItem patchSpecifier)
3752
{
3853
var pluginAssembly = new FileInfo(patchSpecifier.ItemSpec);
@@ -50,7 +65,7 @@ void RunPatch(ITaskItem patchSpecifier)
5065

5166
if (Path.GetFileNameWithoutExtension(pluginAssembly.Name).EndsWith("_original"))
5267
{
53-
Log.LogMessage("Skipping : {FileName}", pluginAssembly.Name);
68+
Serilog.Log.Information("Skipping : {FileName}", pluginAssembly.Name);
5469
return;
5570
}
5671

@@ -79,11 +94,13 @@ void RunPatch(ITaskItem patchSpecifier)
7994
}
8095
catch (Exception exception)
8196
{
82-
Log.LogError("Netcode patching failed with exception:");
83-
Log.LogErrorFromException(exception, true);
97+
Serilog.Log.Fatal(exception, "Netcode patching failed!");
8498
return false;
8599
}
86100

101+
stopwatch.Stop();
102+
Serilog.Log.Information("Done in {Time}", stopwatch.Elapsed);
103+
87104
return true;
88105
}
89106
}

NetcodePatcher.MSBuild/NetcodePatcher.MSBuild.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<!-- Runtime dependencies -->
2222
<ItemGroup>
2323
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="17.*"/>
24+
<PackageReference Include="Serilog" Version="3.1.1"/>
2425
</ItemGroup>
2526

2627
<!-- Project dependencies -->
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
using System;
2+
using Microsoft.Build.Framework;
3+
using Microsoft.Build.Utilities;
4+
using Serilog;
5+
using Serilog.Configuration;
6+
using Serilog.Core;
7+
using Serilog.Events;
8+
9+
namespace NetcodePatcher.MSBuild;
10+
11+
public class TaskLogSink : ILogEventSink
12+
{
13+
private readonly IFormatProvider? _formatProvider = null;
14+
private readonly TaskLoggingHelper _taskLoggingHelper;
15+
16+
public TaskLogSink(TaskLoggingHelper taskLoggingHelper, IFormatProvider? formatProvider)
17+
{
18+
_taskLoggingHelper = taskLoggingHelper;
19+
_formatProvider = formatProvider;
20+
}
21+
22+
public void Emit(LogEvent logEvent)
23+
{
24+
var message = logEvent.RenderMessage(_formatProvider);
25+
26+
switch (logEvent.Level)
27+
{
28+
case LogEventLevel.Debug:
29+
_taskLoggingHelper.LogMessage(MessageImportance.Low, message);
30+
break;
31+
case LogEventLevel.Verbose:
32+
_taskLoggingHelper.LogMessage(MessageImportance.Normal, message);
33+
break;
34+
case LogEventLevel.Information:
35+
_taskLoggingHelper.LogMessage(MessageImportance.High, message);
36+
break;
37+
case LogEventLevel.Warning:
38+
_taskLoggingHelper.LogWarning(message);
39+
if (logEvent.Exception is not null)
40+
_taskLoggingHelper.LogWarningFromException(logEvent.Exception);
41+
break;
42+
case LogEventLevel.Error:
43+
case LogEventLevel.Fatal:
44+
_taskLoggingHelper.LogError(message);
45+
if (logEvent.Exception is not null)
46+
_taskLoggingHelper.LogErrorFromException(logEvent.Exception);
47+
break;
48+
}
49+
}
50+
}
51+
52+
public static class TaskLogSinkExtensions
53+
{
54+
public static LoggerConfiguration TaskLoggingHelper(
55+
this LoggerSinkConfiguration loggerConfiguration,
56+
TaskLoggingHelper taskLoggingHelper,
57+
IFormatProvider? formatProvider = null
58+
) {
59+
return loggerConfiguration.Sink(new TaskLogSink(taskLoggingHelper, formatProvider));
60+
}
61+
}

NetcodePatcher/CodeGen/ILPostProcessorFromFile.cs

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,54 +16,57 @@ public static bool HasNetcodePatchedAttribute(ICompiledAssembly assembly)
1616
// read
1717
AssemblyDefinition? assemblyDefinition = CodeGenHelpers.AssemblyDefinitionFor(assembly, out _);
1818
if (assemblyDefinition == null) return false;
19-
19+
2020
return assemblyDefinition.CustomAttributes.Any(
2121
attribute => attribute.Constructor.DeclaringType.FullName.EndsWith($".{ApplyPatchedAttributeILPP.AttributeNamespaceSuffix}.{ApplyPatchedAttributeILPP.AttributeName}")
2222
);
2323
}
24-
24+
2525
public static void ILPostProcessFile(string assemblyPath, string outputPath, string[] references, Action<string> onWarning, Action<string> onError)
2626
{
2727
var assemblyName = Path.GetFileNameWithoutExtension(assemblyPath);
2828
var assemblyDirectoryName = Path.GetDirectoryName(assemblyPath)!;
2929
var pdbPath = Path.Combine(assemblyDirectoryName, $"{assemblyName}.pdb");
30-
30+
3131
Log.Information("Reading : {FileName}", Path.GetFileName(assemblyPath));
32-
32+
3333
// read the original assembly from file
3434
ICompiledAssembly assembly = new CompiledAssemblyFromFile(assemblyPath) {
3535
References = references
3636
};
37-
37+
3838
if (HasNetcodePatchedAttribute(assembly))
3939
{
4040
Log.Warning("Skipping {FileName} as it has already been patched.", Path.GetFileName(assemblyPath));
4141
return;
4242
}
43-
43+
4444
Log.Information("Patching : {FileName}", Path.GetFileName(assemblyPath));
45-
45+
46+
string? renameAssemblyPath = null;
47+
string? renamePdbPath = null;
48+
4649
if (assemblyPath == outputPath)
4750
{
4851
// remove files with _original.dll and _original.pdb
52+
53+
renameAssemblyPath = Path.Combine(assemblyDirectoryName, $"{assemblyName}_original.dll");
54+
renamePdbPath = Path.Combine(assemblyDirectoryName, $"{assemblyName}_original.pdb");
4955

50-
var newAssemblyPath = Path.Combine(assemblyDirectoryName, $"{assemblyName}_original.dll");
51-
var newPdbPath = Path.Combine(assemblyDirectoryName, $"{assemblyName}_original.pdb");
52-
53-
if (File.Exists(newAssemblyPath))
56+
if (File.Exists(renameAssemblyPath))
5457
{
55-
Log.Information("Deleting : {FileName}", Path.GetFileName(newAssemblyPath));
56-
File.Delete(newAssemblyPath);
58+
Log.Information("Deleting : {FileName}", Path.GetFileName(renameAssemblyPath));
59+
File.Delete(renameAssemblyPath);
5760
}
58-
59-
if (File.Exists(newPdbPath))
61+
62+
if (File.Exists(renamePdbPath))
6063
{
61-
Log.Information("Deleting : {FileName}", Path.GetFileName(newPdbPath));
62-
File.Delete(newPdbPath);
64+
Log.Information("Deleting : {FileName}", Path.GetFileName(renamePdbPath));
65+
File.Delete(renamePdbPath);
6366
}
6467

65-
File.Move(assemblyPath, newAssemblyPath);
66-
File.Move(pdbPath, newPdbPath);
68+
File.Move(assemblyPath, renameAssemblyPath);
69+
File.Move(pdbPath, renamePdbPath);
6770
}
6871

6972
ICompiledAssembly ApplyProcess<TProcessor>(ICompiledAssembly assemblyToApplyProcessTo) where TProcessor : ILPostProcessor, new()
@@ -92,20 +95,41 @@ public static void ILPostProcessFile(string assemblyPath, string outputPath, str
9295
};
9396
}
9497

95-
assembly = ApplyProcess<NetworkBehaviourILPP>(assembly);
96-
assembly = ApplyProcess<INetworkMessageILPP>(assembly);
97-
assembly = ApplyProcess<INetworkSerializableILPP>(assembly);
98-
assembly = ApplyProcess<ApplyPatchedAttributeILPP>(assembly);
99-
100-
var outputAssemblyName = Path.GetFileNameWithoutExtension(outputPath);
101-
var outputDirectoryName = Path.GetDirectoryName(outputPath)!;
102-
var outputPdbPath = Path.Combine(outputDirectoryName, $"{outputAssemblyName}.pdb");
103-
104-
// save the weaved assembly to file.
105-
// some tests open it and check for certain IL code.
106-
File.WriteAllBytes(outputPath, assembly.InMemoryAssembly.PeData);
107-
File.WriteAllBytes(outputPdbPath, assembly.InMemoryAssembly.PdbData);
108-
109-
Log.Information("Patched successfully : {FileName} -> {OutputPath}", Path.GetFileName(assemblyPath), Path.GetFileName(outputPath));
98+
try
99+
{
100+
assembly = ApplyProcess<NetworkBehaviourILPP>(assembly);
101+
assembly = ApplyProcess<INetworkMessageILPP>(assembly);
102+
assembly = ApplyProcess<INetworkSerializableILPP>(assembly);
103+
assembly = ApplyProcess<ApplyPatchedAttributeILPP>(assembly);
104+
105+
var outputAssemblyName = Path.GetFileNameWithoutExtension(outputPath);
106+
var outputDirectoryName = Path.GetDirectoryName(outputPath)!;
107+
var outputPdbPath = Path.Combine(outputDirectoryName, $"{outputAssemblyName}.pdb");
108+
109+
// save the weaved assembly to file.
110+
// some tests open it and check for certain IL code.
111+
File.WriteAllBytes(outputPath, assembly.InMemoryAssembly.PeData);
112+
File.WriteAllBytes(outputPdbPath, assembly.InMemoryAssembly.PdbData);
113+
114+
Log.Information("Patched successfully : {FileName} -> {OutputPath}", Path.GetFileName(assemblyPath), Path.GetFileName(outputPath));
115+
}
116+
catch (Exception)
117+
{
118+
if (assemblyPath == outputPath)
119+
{
120+
// rename file from _original.dll to .dll
121+
if (File.Exists(renameAssemblyPath))
122+
{
123+
File.Move(renameAssemblyPath!, assemblyPath);
124+
}
125+
126+
if (File.Exists(renamePdbPath!))
127+
{
128+
File.Move(renamePdbPath!, pdbPath);
129+
}
130+
}
131+
132+
throw;
133+
}
110134
}
111135
}

NetcodePatcher/NetcodePatcher.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,7 @@ void OnError(string error)
6060
catch (Exception exception)
6161
{
6262
Log.Error($"Failed to patch ({Path.GetFileName(assemblyPath)}): {exception}");
63-
64-
// rename file from _original.dll to .dll
65-
File.Move(assemblyPath.Replace(".dll", "_original.dll"), assemblyPath);
66-
File.Move(assemblyPath.Replace(".dll", "_original.pdb"), assemblyPath.Replace(".dll", ".pdb"));
63+
throw;
6764
}
6865
}
6966
}

0 commit comments

Comments
 (0)