PatchScriptingAssembliesJsonHook.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 || UNITY_WEIXINMINIGAME)
  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. var cacheDir = $"{Application.dataPath}/../Library/PlayerDataCache";
  34. if (Directory.Exists(cacheDir))
  35. {
  36. foreach (var tempJsonPath in Directory.GetDirectories(cacheDir, "*", SearchOption.TopDirectoryOnly))
  37. {
  38. string dirName = Path.GetFileName(tempJsonPath);
  39. #if UNITY_WEIXINMINIGAME
  40. if (!dirName.Contains("WeixinMiniGame"))
  41. {
  42. continue;
  43. }
  44. #else
  45. if (!dirName.Contains("WebGL"))
  46. {
  47. continue;
  48. }
  49. #endif
  50. var patcher = new PatchScriptingAssemblyList();
  51. patcher.PathScriptingAssembilesFile(tempJsonPath);
  52. }
  53. }
  54. string newTitle = BuildMainWindowTitleProxy();
  55. return newTitle;
  56. }
  57. [MethodImpl(MethodImplOptions.NoOptimization)]
  58. private static string BuildMainWindowTitleProxy()
  59. {
  60. // 为满足MonoHook要求的最小代码长度而特地加入的无用填充代码,
  61. UnityEngine.Debug.Log(12345.ToString());
  62. return string.Empty;
  63. }
  64. }
  65. #endif
  66. }