Browse Source

External music redirection

Bepis 7 years ago
parent
commit
7ec24db6bd
2 changed files with 79 additions and 6 deletions
  1. 4 1
      BepInEx/BepInEx.csproj
  2. 75 5
      Plugins/ResourceRedirector/ResourceRedirector.cs

+ 4 - 1
BepInEx/BepInEx.csproj

@@ -42,10 +42,12 @@
     <Reference Include="Assembly-CSharp, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\lib\Assembly-CSharp.dll</HintPath>
+      <Private>False</Private>
     </Reference>
     <Reference Include="Assembly-CSharp-firstpass, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
       <HintPath>..\lib\Assembly-CSharp-firstpass.dll</HintPath>
+      <Private>False</Private>
     </Reference>
     <Reference Include="System" />
     <Reference Include="System.Windows.Forms" />
@@ -54,6 +56,7 @@
     <Reference Include="UnityEngine, Version=0.0.0.0, Culture=neutral, processorArchitecture=MSIL">
       <SpecificVersion>False</SpecificVersion>
       <HintPath>D:\koikatu\KoikatuTrial_Data\Managed\UnityEngine.dll</HintPath>
+      <Private>False</Private>
     </Reference>
   </ItemGroup>
   <ItemGroup>
@@ -75,4 +78,4 @@
   </ItemGroup>
   <Import Project="..\BepInEx.Common\BepInEx.Common.projitems" Label="Shared" />
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
-</Project>
+</Project>

+ 75 - 5
Plugins/ResourceRedirector/ResourceRedirector.cs

@@ -1,8 +1,9 @@
 using BepInEx;
+using Harmony;
 using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Text;
+using System.Collections;
+using System.Reflection;
+using UnityEngine;
 using UnityEngine.SceneManagement;
 
 namespace ResourceRedirector
@@ -11,11 +12,80 @@ namespace ResourceRedirector
     {
         public override string Name => "Resource Redirector";
 
-        protected override void LevelFinishedLoading(Scene scene, LoadSceneMode mode)
+        public ResourceRedirector()
         {
-            
+            var harmony = HarmonyInstance.Create("com.bepis.bepinex.resourceredirector");
+
+
+
+            MethodInfo original = AccessTools.Method(typeof(AssetBundleManager), "LoadAsset", new[] { typeof(string), typeof(string), typeof(Type), typeof(string) });
+
+            HarmonyMethod postfix = new HarmonyMethod(typeof(ResourceRedirector).GetMethod("LoadAssetPostHook"));
+
+            harmony.Patch(original, null, postfix);
+
+
+
+
+            original = AccessTools.Method(typeof(AssetBundleManager), "LoadAllAsset", new[] { typeof(string), typeof(Type), typeof(string) });
+
+            postfix = new HarmonyMethod(typeof(ResourceRedirector).GetMethod("LoadAllAssetPostHook"));
+
+            harmony.Patch(original, null, postfix);
+
+
+
+            original = AccessTools.Method(typeof(Manager.Sound), "Bind");
+
+            var prefix = new HarmonyMethod(typeof(ResourceRedirector).GetMethod("BindPreHook"));
+
+            harmony.Patch(original, prefix, null);
         }
 
+        public static void LoadAssetPostHook(string assetBundleName, string assetName, Type type, string manifestAssetBundleName)
+        {
+            //Console.WriteLine($"{assetBundleName} : {assetName} : {type.FullName} : {manifestAssetBundleName ?? ""}");
+        }
+
+        public static void LoadAllAssetPostHook(string assetBundleName, Type type, string manifestAssetBundleName = null)
+        {
+            //Console.WriteLine($"{assetBundleName} : {type.FullName} : {manifestAssetBundleName ?? ""}");
+        }
+
+        public static FieldInfo f_typeObjects = typeof(Manager.Sound).GetField("typeObjects", BindingFlags.Instance | BindingFlags.NonPublic);
+        public static FieldInfo f_settingObjects = typeof(Manager.Sound).GetField("settingObjects", BindingFlags.Instance | BindingFlags.NonPublic);
+        public static PropertyInfo f_clip = typeof(LoadAudioBase).GetProperty("clip", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
+        
+
+        public static void BindPreHook(ref LoadSound script)
+        {
+            if (script.audioSource == null)
+            {
+                int type = (int)script.type;
 
+                Transform[] typeObjects = (Transform[])f_typeObjects.GetValue(Manager.Game.Instance);
+                GameObject[] settingObjects = (GameObject[])f_settingObjects.GetValue(Manager.Game.Instance);
+
+                Manager.Sound.Instance.SetParent(typeObjects[type], script, settingObjects[type]);
+            }
+
+            if (true) //script.clip.StartsWith("bgm"))
+            {
+                string path = "file://" + BepInEx.Common.Utility.PluginsDirectory.Replace('\\', '/') + "/music.ogg";
+                Console.WriteLine(path);
+                WWW loadGachi = new WWW(path);
+
+                
+                AudioClip clip = loadGachi.GetAudioClip(false);
+
+                //force single threaded loading instead of using a coroutine
+                while (!clip.isReadyToPlay) { }
+
+
+                script.audioSource.clip = clip;
+
+                f_clip.SetValue(script, clip, null);
+            }
+        }
     }
 }