ClipManager.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using Il2Cpp;
  2. using MelonLoader;
  3. using System.Collections;
  4. using UnityEngine;
  5. using UnityEngine.Networking;
  6. namespace AudioMgr
  7. {
  8. public class ClipManager
  9. {
  10. private Dictionary<string, Clip> _loadedClips = new Dictionary<string, Clip>();
  11. public enum LoadType { Compressed, Decompressed, Stream };
  12. private string downloaderPath = @"file:///" + Application.dataPath + @"/../Mods";
  13. public Clip GetClip(string clipName)
  14. {
  15. if (_loadedClips.ContainsKey(clipName))
  16. {
  17. return _loadedClips[clipName];
  18. }
  19. else
  20. {
  21. MelonLogger.Msg("Warning: Trying to retrieve non-existent audioclip " + clipName);
  22. return null;
  23. }
  24. }
  25. public Clip GetClipAtIndex(int index)
  26. {
  27. if (_loadedClips.ElementAt(index).Key != null)
  28. {
  29. return _loadedClips.ElementAt(index).Value;
  30. }
  31. else
  32. {
  33. MelonLogger.Msg("Warning: Trying to retrieve non-existent audioclip at index " + index);
  34. return null;
  35. }
  36. }
  37. public void UnloadClip(string clipName)
  38. {
  39. if (_loadedClips.ContainsKey(clipName))
  40. {
  41. _loadedClips[clipName].Unload();
  42. _loadedClips.Remove(clipName);
  43. }
  44. }
  45. public void UnloadAllClips()
  46. {
  47. foreach (KeyValuePair<string, Clip> singleClip in _loadedClips)
  48. {
  49. singleClip.Value.Unload();
  50. _loadedClips.Remove(singleClip.Key);
  51. }
  52. }
  53. public void LoadClipFromBundle(string newClipName, string clipInBundle, AssetBundle assetBundle)
  54. {
  55. if (_loadedClips.ContainsKey(newClipName))
  56. {
  57. return;
  58. }
  59. //MelonCoroutines.Start(LoadClipFromBundleRoutine(newClipName, clipInBundle, assetBundle));
  60. LoadClipFromBundleRoutine(newClipName, clipInBundle, assetBundle);
  61. }
  62. public void LoadAllClipsFromBundle(AssetBundle assetBundle)
  63. {
  64. string[] clipNamesInBundle = assetBundle.AllAssetNames();
  65. foreach(string singleName in clipNamesInBundle)
  66. {
  67. if(singleName.Contains(".ogg") || singleName.Contains(".wav") || singleName.Contains(".mp3"))
  68. {
  69. string[] clipNameSplit;
  70. string tmpClipName;
  71. clipNameSplit = singleName.Split('/');
  72. tmpClipName = clipNameSplit[clipNameSplit.Length-1];
  73. if(AudioMain._debug)
  74. {
  75. MelonLogger.Msg("Importing clip to clipmanager: " + FileNameCutter(tmpClipName));
  76. }
  77. if (!_loadedClips.ContainsKey(tmpClipName))
  78. {
  79. LoadClipFromBundle(FileNameCutter(tmpClipName), singleName, assetBundle);
  80. }
  81. }
  82. }
  83. }
  84. //private IEnumerator LoadClipFromBundleRoutine(string newClipName, string clipInBundle, AssetBundle assetBundle)
  85. private void LoadClipFromBundleRoutine(string newClipName, string clipInBundle, AssetBundle assetBundle)
  86. {
  87. _loadedClips.Add(newClipName, new Clip(assetBundle.LoadAsset<AudioClip>(clipInBundle), newClipName));
  88. }
  89. public void LoadClipsFromDir(string directory, LoadType loadType)
  90. {
  91. string[] allTheFilesInDir = Directory.GetFiles(Application.dataPath + "/../Mods/" + directory, "*", SearchOption.TopDirectoryOnly);
  92. foreach(string singleFile in allTheFilesInDir)
  93. {
  94. LoadClipFromFile(FileNameCutter(ClipStringCutter(singleFile)), directory + "/" + ClipStringCutter(singleFile), loadType);
  95. }
  96. }
  97. public void LoadClipFromFile(string newClipName, string fileName, LoadType loadType)
  98. {
  99. if (_loadedClips.ContainsKey(newClipName))
  100. {
  101. return;
  102. }
  103. //MelonCoroutines.Start(LoadClipFromFileRoutine(newClipName, fileName, loadType));
  104. LoadClipFromFileRoutine(newClipName, fileName, loadType);
  105. }
  106. //private IEnumerator LoadClipFromFileRoutine(string clipName, string fileName, LoadType loadType)
  107. private void LoadClipFromFileRoutine(string clipName, string fileName, LoadType loadType)
  108. {
  109. bool compressed = true;
  110. bool stream = false;
  111. if (loadType == LoadType.Compressed)
  112. {
  113. compressed = true;
  114. stream = false;
  115. }
  116. if (loadType == LoadType.Decompressed)
  117. {
  118. compressed = false;
  119. stream = false;
  120. }
  121. if (loadType == LoadType.Stream)
  122. {
  123. compressed = false;
  124. stream = true;
  125. }
  126. _loadedClips.Add(clipName, null);
  127. UnityWebRequest www;
  128. www = UnityWebRequest.Get(downloaderPath + @"/" + fileName);
  129. www.SendWebRequest();
  130. //while (!www.isDone) yield return null;
  131. while (!www.isDone)
  132. if (!www.isNetworkError && !www.isHttpError)
  133. {
  134. _loadedClips[clipName] = new Clip(WebRequestWWW.InternalCreateAudioClipUsingDH(www.downloadHandler, www.url, stream, compressed, AudioType.UNKNOWN), clipName);
  135. }
  136. else
  137. {
  138. _loadedClips.Remove(clipName);
  139. MelonLogger.Msg("Error while loading audioclip. Skipping " + www.error);
  140. }
  141. }
  142. private string ClipStringCutter(string stringToCut)
  143. {
  144. string[] clipNameSplit;
  145. string tmpClipName;
  146. clipNameSplit = stringToCut.Split('\\');
  147. tmpClipName = clipNameSplit[clipNameSplit.Length-1];
  148. return (tmpClipName);
  149. }
  150. private string FileNameCutter(string stringToCut)
  151. {
  152. string[] clipNameSplit;
  153. string tmpClipName;
  154. clipNameSplit = stringToCut.Split('.');
  155. tmpClipName = clipNameSplit[0];
  156. return tmpClipName;
  157. }
  158. public int clipCount
  159. {
  160. get
  161. {
  162. return _loadedClips.Count;
  163. }
  164. }
  165. }
  166. }