ResourceRedirector.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using BepInEx;
  2. using BepInEx.Common;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEngine;
  7. namespace ResourceRedirector
  8. {
  9. public class ResourceRedirector : BaseUnityPlugin
  10. {
  11. public override string ID => "com.bepis.bepinex.resourceredirector";
  12. public override string Name => "Asset Emulator";
  13. public override Version Version => new Version("1.3");
  14. public static string EmulatedDir => Path.Combine(Utility.ExecutingDirectory, "abdata-emulated");
  15. public static bool EmulationEnabled;
  16. public delegate bool AssetHandler(string assetBundleName, string assetName, Type type, string manifestAssetBundleName, out AssetBundleLoadAssetOperation result);
  17. public static List<AssetHandler> AssetResolvers = new List<AssetHandler>();
  18. void Awake()
  19. {
  20. Hooks.InstallHooks();
  21. EmulationEnabled = Directory.Exists(EmulatedDir);
  22. AssetResolvers.Add(BGMLoader.HandleAsset);
  23. }
  24. public static AssetBundleLoadAssetOperation HandleAsset(string assetBundleName, string assetName, Type type, string manifestAssetBundleName, ref AssetBundleLoadAssetOperation __result)
  25. {
  26. foreach (var handler in AssetResolvers)
  27. {
  28. if (handler.Invoke(assetBundleName, assetName, type, manifestAssetBundleName, out AssetBundleLoadAssetOperation result))
  29. return result;
  30. }
  31. //emulate asset load
  32. string dir = Path.Combine(EmulatedDir, assetBundleName.Replace('/', '\\').Replace(".unity3d", ""));
  33. if (Directory.Exists(dir))
  34. {
  35. if (type == typeof(Texture2D))
  36. {
  37. string path = Path.Combine(dir, $"{assetName}.png");
  38. if (!File.Exists(path))
  39. return __result;
  40. BepInLogger.Log($"Loading emulated asset {path}");
  41. return new AssetBundleLoadAssetOperationSimulation(AssetLoader.LoadTexture(path));
  42. }
  43. else if (type == typeof(AudioClip))
  44. {
  45. string path = Path.Combine(dir, $"{assetName}.wav");
  46. if (!File.Exists(path))
  47. return __result;
  48. BepInLogger.Log($"Loading emulated asset {path}");
  49. return new AssetBundleLoadAssetOperationSimulation(AssetLoader.LoadAudioClip(path, AudioType.WAV));
  50. }
  51. }
  52. //otherwise return normal asset
  53. return __result;
  54. }
  55. }
  56. }