Hooks.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using BepInEx;
  2. using Harmony;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Reflection;
  8. using System.Text;
  9. using UnityEngine;
  10. namespace ResourceRedirector
  11. {
  12. static class Hooks
  13. {
  14. public static void InstallHooks()
  15. {
  16. var harmony = HarmonyInstance.Create("com.bepis.bepinex.resourceredirector");
  17. MethodInfo original = AccessTools.Method(typeof(AssetBundleManager), "LoadAsset", new[] { typeof(string), typeof(string), typeof(Type), typeof(string) });
  18. HarmonyMethod postfix = new HarmonyMethod(typeof(Hooks).GetMethod("LoadAssetPostHook"));
  19. harmony.Patch(original, null, postfix);
  20. original = AccessTools.Method(typeof(AssetBundleManager), "LoadAssetBundle", new[] { typeof(string), typeof(bool), typeof(string) });
  21. postfix = new HarmonyMethod(typeof(Hooks).GetMethod("LoadAssetBundlePostHook"));
  22. harmony.Patch(original, null, postfix);
  23. original = AccessTools.Method(typeof(AssetBundleManager), "LoadAssetAsync", new[] { typeof(string), typeof(string), typeof(Type), typeof(string) });
  24. postfix = new HarmonyMethod(typeof(Hooks).GetMethod("LoadAssetAsyncPostHook"));
  25. harmony.Patch(original, null, postfix);
  26. original = AccessTools.Method(typeof(AssetBundleManager), "LoadAllAsset", new[] { typeof(string), typeof(Type), typeof(string) });
  27. postfix = new HarmonyMethod(typeof(Hooks).GetMethod("LoadAllAssetPostHook"));
  28. harmony.Patch(original, null, postfix);
  29. //Singleton<Manager.Character>.Instance.chaListCtrl.LoadListInfoAll();
  30. }
  31. public static void LoadAssetPostHook(ref AssetBundleLoadAssetOperation __result, string assetBundleName, string assetName, Type type, string manifestAssetBundleName)
  32. {
  33. //Console.WriteLine($"{assetBundleName} : {assetName} : {type.FullName} : {manifestAssetBundleName ?? ""}");
  34. __result = ResourceRedirector.HandleAsset(assetBundleName, assetName, type, manifestAssetBundleName, ref __result);
  35. }
  36. public static void LoadAssetBundlePostHook(string assetBundleName, bool isAsync, string manifestAssetBundleName)
  37. {
  38. //Console.WriteLine($"{assetBundleName} : {manifestAssetBundleName} : {isAsync}");
  39. }
  40. public static void LoadAssetAsyncPostHook(ref AssetBundleLoadAssetOperation __result, string assetBundleName, string assetName, Type type, string manifestAssetBundleName)
  41. {
  42. //Chainloader.Log($"{assetBundleName} : {assetName} : {type.FullName} : {manifestAssetBundleName ?? ""}", true);
  43. __result = ResourceRedirector.HandleAsset(assetBundleName, assetName, type, manifestAssetBundleName, ref __result);
  44. }
  45. public static void LoadAllAssetPostHook(ref AssetBundleLoadAssetOperation __result, string assetBundleName, Type type, string manifestAssetBundleName = null)
  46. {
  47. Chainloader.Log($"{assetBundleName} : {type.FullName} : {manifestAssetBundleName ?? ""}");
  48. if (assetBundleName == "sound/data/systemse/brandcall/00.unity3d" ||
  49. assetBundleName == "sound/data/systemse/titlecall/00.unity3d")
  50. {
  51. string dir = $"{BepInEx.Common.Utility.PluginsDirectory}\\introclips";
  52. if (!Directory.Exists(dir))
  53. Directory.CreateDirectory(dir);
  54. var files = Directory.GetFiles(dir, "*.wav");
  55. if (files.Length == 0)
  56. return;
  57. List<UnityEngine.Object> loadedClips = new List<UnityEngine.Object>();
  58. foreach (string path in files)
  59. loadedClips.Add(AssetLoader.LoadAudioClip(path, AudioType.WAV));
  60. __result = new AssetBundleLoadAssetOperationSimulation(loadedClips.ToArray());
  61. }
  62. if (type == typeof(TextAsset))
  63. {
  64. string dir = Path.Combine(ResourceRedirector.EmulatedDir, assetBundleName.Replace('/', '\\').Replace(".unity3d", ""));
  65. List<UnityEngine.Object> go = new List<UnityEngine.Object>();
  66. foreach (TextAsset t in __result.GetAllAssets<TextAsset>())
  67. {
  68. string path = Path.Combine(dir, $"{t.name}.txt");
  69. //Chainloader.Log(path);
  70. if (File.Exists(path))
  71. {
  72. Chainloader.Log($"Loading emulated asset {path}");
  73. go.Add(AssetLoader.LoadTextAsset(path));
  74. }
  75. else
  76. {
  77. go.Add(t);
  78. }
  79. }
  80. __result = new AssetBundleLoadAssetOperationSimulation(go.ToArray());
  81. }
  82. }
  83. }
  84. }