EvaisaDev / UnityNetcodePatcher

An assembly patcher which replicates the ILPostProcessing step unity does in order to make NetworkBehaviours work
MIT License
51 stars 6 forks source link

Fix potential Serilog load error, enable debug logging into msbuild #40

Closed wherget closed 5 months ago

wherget commented 6 months ago

As is, Default.LoadFromAssemblyName may fail to load the Serilog dependency when using the MSBuild SDK, leading to exceptions like:

Sdk.targets(6,5): error : [15:53:41 FTL] Netcode patching failed! 
Sdk.targets(6,5): error : System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. 
Sdk.targets(6,5): error :    at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr) 
Sdk.targets(6,5): error :    at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
Sdk.targets(6,5): error :    at NetcodePatcher.MSBuild.NetcodePatchTask.<>c__DisplayClass32_0.<Execute>g__RunPatch|0(ITaskItem patchSpecifier) in /home/runner/work/UnityNetcodePatcher/UnityNetcodePatcher/NetcodePatcher.MSBuild.Tasks/NetcodePatchTask.cs:line 96 
Sdk.targets(6,5): error :    at NetcodePatcher.MSBuild.NetcodePatchTask.Execute() in /home/runner/work/UnityNetcodePatcher/UnityNetcodePatcher/NetcodePatcher.MSBuild.Tasks/NetcodePatchTask.cs:line 105 
Sdk.targets(6,5): error : Caused by: System.IO.FileNotFoundException: Could not load file or assembly 'Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10'. The system cannot find the file specified. 
Sdk.targets(6,5): error :  
Sdk.targets(6,5): error :    at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) 
Sdk.targets(6,5): error :    at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr) 
Sdk.targets(6,5): error : Caused by: System.IO.FileNotFoundException: Could not load file or assembly 'Serilog, Version=2.0.0.0, Culture=neutral, PublicKeyToken=24c2f752a8e58a10'. The system cannot find the file specified. 
Sdk.targets(6,5): error :  
Sdk.targets(6,5): error :    at System.Reflection.RuntimeAssembly.InternalLoad(AssemblyName assemblyName, StackCrawlMark& stackMark, AssemblyLoadContext assemblyLoadContext, RuntimeAssembly requestingAssembly, Boolean throwOnFileNotFound) 
Sdk.targets(6,5): error :    at System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyName(AssemblyName assemblyName) 
Sdk.targets(6,5): error :    at NetcodePatcher.Tools.Common.PatcherLoadContext.Load(AssemblyName assemblyName) in /home/runner/work/UnityNetcodePatcher/UnityNetcodePatcher/NetcodePatcher.Tools.Common/PatcherLoadContext.cs:line 30 
Sdk.targets(6,5): error :    at System.Runtime.Loader.AssemblyLoadContext.ResolveUsingLoad(AssemblyName assemblyName) 
Sdk.targets(6,5): error :    at System.Runtime.Loader.AssemblyLoadContext.Resolve(IntPtr gchManagedAssemblyLoadContext, AssemblyName assemblyName) 

This PR adds code to attempt to manually resolve the assembly from the PatcherCommonAssemblyDir before deferring to the Default Context.

Additionally it changes the LoggingConfiguarion for the MSBuild Task to also emit Debug messages. These are only shown when MSBuild'ing with verbosity detailed or higher anyway, but filtering them out in the Task leaves them completely inaccessible.

wherget commented 6 months ago

Would any of you mind having a look at this PR @EvaisaDev or @Lordfirespeed? You can check my usage from the mentioned PR in LLL. If this is indeed an issue (and I'm not just weirdly holding it wrong, I feel like I can't be the first person to use the MSBuild patcher o.O), is this even the right way to go about fixing it?