ILPostProcessorFromFile.cs 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // helper function to use ILPostProcessor for an assembly from file.
  2. // we keep this in Weaver folder because we can access CompilationPipleine here.
  3. // in tests folder we can't, unless we rename to "Unity.*.CodeGen",
  4. // but then tests wouldn't be weaved anymore.
  5. #if UNITY_2020_3_OR_NEWER
  6. using System;
  7. using System.IO;
  8. using Unity.CompilationPipeline.Common.Diagnostics;
  9. using Unity.CompilationPipeline.Common.ILPostProcessing;
  10. namespace Mirror.Weaver
  11. {
  12. public static class ILPostProcessorFromFile
  13. {
  14. // read, weave, write file via ILPostProcessor
  15. public static void ILPostProcessFile(string assemblyPath, string[] references, Action<string> OnWarning, Action<string> OnError)
  16. {
  17. // we COULD Weave() with a test logger manually.
  18. // but for test result consistency on all platforms,
  19. // let's invoke the ILPostProcessor here too.
  20. CompiledAssemblyFromFile assembly = new CompiledAssemblyFromFile(assemblyPath);
  21. assembly.References = references;
  22. // create ILPP and check WillProcess like Unity would.
  23. ILPostProcessorHook ilpp = new ILPostProcessorHook();
  24. if (ilpp.WillProcess(assembly))
  25. {
  26. //Debug.Log($"Will Process: {assembly.Name}");
  27. // process it like Unity would
  28. ILPostProcessResult result = ilpp.Process(assembly);
  29. // handle the error messages like Unity would
  30. foreach (DiagnosticMessage message in result.Diagnostics)
  31. {
  32. if (message.DiagnosticType == DiagnosticType.Warning)
  33. {
  34. OnWarning(message.MessageData);
  35. }
  36. else if (message.DiagnosticType == DiagnosticType.Error)
  37. {
  38. OnError(message.MessageData);
  39. }
  40. }
  41. // save the weaved assembly to file.
  42. // some tests open it and check for certain IL code.
  43. File.WriteAllBytes(assemblyPath, result.InMemoryAssembly.PeData);
  44. }
  45. }
  46. }
  47. }
  48. #endif