-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPlugin.cs
245 lines (197 loc) · 7.28 KB
/
Plugin.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
using BepInEx;
using BepInEx.Configuration;
using HarmonyLib;
using Reptile;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using UnityEngine.Networking;
namespace BombRushRadio;
[BepInPlugin(PluginInfo.PLUGIN_GUID, PluginInfo.PLUGIN_NAME, PluginInfo.PLUGIN_VERSION)]
public class BombRushRadio : BaseUnityPlugin
{
public static ConfigEntry<bool> StreamAudio;
public static ConfigEntry<KeyCode> ReloadKey;
public static MusicPlayer MInstance;
public static List<MusicTrack> Audios = new();
public int ShouldBeDone;
public int Done;
private static readonly List<string> Loaded = new();
public static bool InMainMenu = false;
public static bool Loading;
private readonly string _songFolder = Path.Combine(Application.streamingAssetsPath, "Mods", "BombRushRadio", "Songs");
private readonly string _cachePath = Path.Combine(Paths.CachePath, "BombRushRadio");
public void SanitizeSongs()
{
if (Core.Instance == null || Core.Instance.audioManager == null)
{
return;
}
if (Core.Instance.audioManager.musicPlayer != null)
{
var toRemove = new List<MusicTrack>();
int idx = 0;
foreach (MusicTrack tr in Audios)
{
if (MInstance.musicTrackQueue.currentMusicTracks.Contains(tr))
{
MInstance.musicTrackQueue.currentMusicTracks.Remove(tr);
}
else
{
Logger.LogInfo("[BRR] Adding " + tr.Title);
}
if (Loaded.FirstOrDefault(l => l == Helpers.FormatMetadata(new []{tr.Artist, tr.Title}, "dash")) == null)
{
Logger.LogInfo("[BRR] Removing " + tr.Title);
toRemove.Add(tr);
}
MInstance.musicTrackQueue.currentMusicTracks.Insert(1 + idx, tr);
idx++;
}
foreach (MusicTrack tr in toRemove)
{
Audios.Remove(tr);
tr.AudioClip.UnloadAudioData();
}
}
}
public IEnumerator LoadAudioFile(string filePath, AudioType type)
{
string[] metadata = Helpers.GetMetadata(filePath, false);
string songName = Helpers.FormatMetadata(metadata, "dash");
// Escape special characters so we don't get an HTML error when we send the request
filePath = UnityWebRequest.EscapeURL(filePath);
using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip("file:///" + filePath, type))
{
yield return www.SendWebRequest();
if (www.result == UnityWebRequest.Result.ConnectionError)
{
Logger.LogError(www.error);
}
else
{
Done++;
MusicTrack musicTrack = ScriptableObject.CreateInstance<MusicTrack>();
musicTrack.AudioClip = null;
musicTrack.Artist = metadata[0];
musicTrack.Title = metadata[1];
musicTrack.isRepeatable = false;
var downloadHandler = (DownloadHandlerAudioClip) www.downloadHandler;
if (StreamAudio.Value)
{
downloadHandler.streamAudio = true;
}
AudioClip myClip = downloadHandler.audioClip;
myClip.name = filePath;
musicTrack.AudioClip = myClip;
Audios.Add(musicTrack);
Logger.LogInfo($"[BRR] Loaded {Helpers.FormatMetadata(metadata, "by")} ({Done}/{ShouldBeDone})");
Loaded.Add(songName);
}
}
}
public IEnumerator LoadFile(string f)
{
string extension = Path.GetExtension(f).ToLowerInvariant().Substring(1);
if (extension is "cache" or "tag")
{
File.Delete(f); // Remove old cache files
yield return null;
}
string[] metadata = Helpers.GetMetadata(f, false);
if (Audios.Find(m => m.Artist == metadata[0] && m.Title == metadata[1]))
{
string songName = Helpers.FormatMetadata(metadata, "dash");
Loaded.Add(songName);
Logger.LogInfo("[BRR] " + songName + " is already loaded, skipping.");
}
else
{
AudioType type = extension switch
{
"aif" => AudioType.AIFF,
"aiff" => AudioType.AIFF,
"it" => AudioType.IT,
"mod" => AudioType.MOD,
"mp2" => AudioType.MPEG,
"mp3" => AudioType.MPEG,
"ogg" => AudioType.OGGVORBIS,
"s3m" => AudioType.S3M,
"wav" => AudioType.WAV,
"xm" => AudioType.XM,
"flac" => AudioType.UNKNOWN,
_ => AudioType.UNKNOWN
};
ShouldBeDone++;
StartCoroutine(LoadAudioFile(f, type));
}
yield return null;
}
public IEnumerator SearchDirectories(string path = "")
{
string p = path.Length == 0 ? _songFolder : path;
foreach (string f in Directory.GetDirectories(p))
{
Logger.LogInfo("[BRR] Searching directory " + f);
StartCoroutine(SearchDirectories(f));
}
foreach (string f in Directory.GetFiles(p))
{
StartCoroutine(LoadFile(f));
}
yield return null;
}
public IEnumerator ReloadSongs()
{
Loaded.Clear();
Loading = true;
if (Audios.Count > 0)
{
if (Core.Instance.audioManager.musicPlayer.IsPlaying && MInstance != null)
{
Core.Instance.audioManager.musicPlayer.ForcePaused();
}
}
Logger.LogInfo("[BRR] Loading songs...");
ShouldBeDone = 0;
Done = 0;
yield return StartCoroutine(SearchDirectories());
Logger.LogInfo("[BRR] TOTAL SONGS LOADED: " + Audios.Count);
Logger.LogInfo("[BRR] Bomb Rush Radio has been loaded!");
Loading = false;
Audios.Sort((t, t2) => string.Compare(t.AudioClip.name, t2.AudioClip.name, StringComparison.OrdinalIgnoreCase));
SanitizeSongs();
}
private void Awake()
{
// setup mod directory
if (!Directory.Exists(_songFolder))
{
Directory.CreateDirectory(_songFolder);
}
// purge cache files
if (Directory.Exists(_cachePath))
{
Directory.Delete(_cachePath, true);
}
// bind to config
StreamAudio = Config.Bind("Settings", "Stream Audio", true, "Whether to stream audio from disk or load at runtime (Streaming is faster but more CPU intensive)");
ReloadKey = Config.Bind("Settings", "Reload Key", KeyCode.F1, "Keybind used for reloading songs.");
// load em
StartCoroutine(ReloadSongs());
var harmony = new Harmony("kade.bombrushradio");
harmony.PatchAll();
Logger.LogInfo("[BRR] Patched...");
Core.OnUpdate += () =>
{
if (Input.GetKeyDown(ReloadKey.Value) && !InMainMenu) // reload songs
{
StartCoroutine(ReloadSongs());
}
};
}
}