1
0

ClipManager.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 (!_loadedClips.ContainsKey(tmpClipName))
  74. {
  75. LoadClipFromBundle(FileNameCutter(tmpClipName), singleName, assetBundle);
  76. }
  77. }
  78. }
  79. }
  80. //private IEnumerator LoadClipFromBundleRoutine(string newClipName, string clipInBundle, AssetBundle assetBundle)
  81. private void LoadClipFromBundleRoutine(string newClipName, string clipInBundle, AssetBundle assetBundle)
  82. {
  83. _loadedClips.Add(newClipName, new Clip(assetBundle.LoadAsset<AudioClip>(clipInBundle), newClipName));
  84. }
  85. public void LoadClipsFromDir(string directory, LoadType loadType)
  86. {
  87. string[] allTheFilesInDir = Directory.GetFiles(Application.dataPath + "/../Mods/" + directory, "*", SearchOption.TopDirectoryOnly);
  88. foreach(string singleFile in allTheFilesInDir)
  89. {
  90. LoadClipFromFile(FileNameCutter(ClipStringCutter(singleFile)), directory + "/" + ClipStringCutter(singleFile), loadType);
  91. }
  92. }
  93. public void LoadClipFromFile(string newClipName, string fileName, LoadType loadType)
  94. {
  95. if (_loadedClips.ContainsKey(newClipName))
  96. {
  97. return;
  98. }
  99. //MelonCoroutines.Start(LoadClipFromFileRoutine(newClipName, fileName, loadType));
  100. LoadClipFromFileRoutine(newClipName, fileName, loadType);
  101. }
  102. //private IEnumerator LoadClipFromFileRoutine(string clipName, string fileName, LoadType loadType)
  103. private void LoadClipFromFileRoutine(string clipName, string fileName, LoadType loadType)
  104. {
  105. bool compressed = true;
  106. bool stream = false;
  107. if (loadType == LoadType.Compressed)
  108. {
  109. compressed = true;
  110. stream = false;
  111. }
  112. if (loadType == LoadType.Decompressed)
  113. {
  114. compressed = false;
  115. stream = false;
  116. }
  117. if (loadType == LoadType.Stream)
  118. {
  119. compressed = false;
  120. stream = true;
  121. }
  122. _loadedClips.Add(clipName, null);
  123. UnityWebRequest www;
  124. www = UnityWebRequest.Get(downloaderPath + @"/" + fileName);
  125. www.SendWebRequest();
  126. //while (!www.isDone) yield return null;
  127. while (!www.isDone)
  128. if (!www.isNetworkError && !www.isHttpError)
  129. {
  130. _loadedClips[clipName] = new Clip(WebRequestWWW.InternalCreateAudioClipUsingDH(www.downloadHandler, www.url, stream, compressed, AudioType.UNKNOWN), clipName);
  131. }
  132. else
  133. {
  134. _loadedClips.Remove(clipName);
  135. MelonLogger.Msg("Error while loading audioclip. Skipping " + www.error);
  136. }
  137. }
  138. private string ClipStringCutter(string stringToCut)
  139. {
  140. string[] clipNameSplit;
  141. string tmpClipName;
  142. clipNameSplit = stringToCut.Split('\\');
  143. tmpClipName = clipNameSplit[clipNameSplit.Length-1];
  144. return (tmpClipName);
  145. }
  146. private string FileNameCutter(string stringToCut)
  147. {
  148. string[] clipNameSplit;
  149. string tmpClipName;
  150. clipNameSplit = stringToCut.Split('.');
  151. tmpClipName = clipNameSplit[0];
  152. return tmpClipName;
  153. }
  154. public int clipCount
  155. {
  156. get
  157. {
  158. return _loadedClips.Count;
  159. }
  160. }
  161. }
  162. }