PatchScriptingAssembliesJsonHook.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. 
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Reflection;
  6. using UnityEngine;
  7. using UnityEditor;
  8. using System.Runtime.CompilerServices;
  9. using MonoHook;
  10. using HybridCLR.Editor.BuildProcessors;
  11. using System.IO;
  12. namespace HybridCLR.MonoHook
  13. {
  14. #if UNITY_2021_1_OR_NEWER && UNITY_WEBGL
  15. [InitializeOnLoad]
  16. public class PatchScriptingAssembliesJsonHook
  17. {
  18. private static MethodHook _hook;
  19. static PatchScriptingAssembliesJsonHook()
  20. {
  21. if (_hook == null)
  22. {
  23. Type type = typeof(UnityEditor.EditorApplication);
  24. MethodInfo miTarget = type.GetMethod("BuildMainWindowTitle", BindingFlags.Static | BindingFlags.NonPublic);
  25. MethodInfo miReplacement = new Func<string>(BuildMainWindowTitle).Method;
  26. MethodInfo miProxy = new Func<string>(BuildMainWindowTitleProxy).Method;
  27. _hook = new MethodHook(miTarget, miReplacement, miProxy);
  28. _hook.Install();
  29. }
  30. }
  31. private static string BuildMainWindowTitle()
  32. {
  33. string tempJsonPath = $"{Application.dataPath}/../Library/PlayerDataCache/WebGL/Data/ScriptingAssemblies.json";
  34. if (File.Exists(tempJsonPath))
  35. {
  36. var patcher = new PatchScriptingAssemblyList();
  37. patcher.PathScriptingAssembilesFile(Path.GetDirectoryName(tempJsonPath));
  38. }
  39. string newTitle = BuildMainWindowTitleProxy();
  40. return newTitle;
  41. }
  42. [MethodImpl(MethodImplOptions.NoOptimization)]
  43. private static string BuildMainWindowTitleProxy()
  44. {
  45. // 为满足MonoHook要求的最小代码长度而特地加入的无用填充代码,
  46. UnityEngine.Debug.Log(12345.ToString());
  47. return string.Empty;
  48. }
  49. }
  50. #endif
  51. }