ClipManager.cs 6.0 KB

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