using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Reflection; using System.Text; using UnityEngine; using UnityEngine.Networking; using Object = UnityEngine.Object; namespace UltraParadise { internal static class GameMusic { private const char REC = '\u001d', SEP = '\u001c'; private static readonly FieldInfo sourceField = typeof(MusicManager).GetField("source", BindingFlags.NonPublic | BindingFlags.Instance); private static readonly Dictionary cache = new Dictionary(); private static AudioClip[] builtIn; private static UnityWebRequest loading; private static string loadingName; private static float nextSend; private static string lastSent; private static bool on; private static string Folder() { return Path.Combine(Application.dataPath, "custommusic"); } private static AudioSource Source() { if (MusicManager.main == null || sourceField == null) return null; return sourceField.GetValue(MusicManager.main) as AudioSource; } private static Body Player() { return PlayerCamera.main != null && WorldGeneration.world != null ? PlayerCamera.main.body : null; } private static string Clean(string s) { return (s ?? "").Replace('\r', ' ').Replace('\n', ' ').Replace(REC, ' ').Replace(SEP, ' '); } private static void Player(out bool has, out bool held) { has = false; held = false; Body b = Player(); if (b == null || b.slots == null) return; try { Item h = b.GetItem(b.handSlot); if (h != null && h.id == "mp3player") { held = true; has = true; return; } } catch (Exception) { } for (int i = 0; i < b.slots.Length; i++) { if (b.slots[i] == null) continue; Item[] items = b.slots[i].GetComponentsInChildren(true); for (int k = 0; k < items.Length; k++) if (items[k] != null && items[k].id == "mp3player") { has = true; return; } } } private static AudioClip[] BuiltIn() { if (builtIn == null) builtIn = Resources.LoadAll("Sounds/music"); return builtIn; } internal static void Tick() { if (loading != null && loading.isDone) { UnityWebRequest r = loading; string name = loadingName; loading = null; loadingName = null; try { if (r.result == UnityWebRequest.Result.Success) { AudioClip c = DownloadHandlerAudioClip.GetContent(r); if (c != null) { c.name = Path.GetFileNameWithoutExtension(name); cache[name] = c; Play(c); Link.Send("MSG Playing " + Clean(name) + " in the game"); } } else Link.Send("MSG The game couldn't load " + Clean(name) + ": " + Clean(r.error)); } finally { r.Dispose(); } lastSent = null; } if (!on || Time.unscaledTime < nextSend) return; nextSend = Time.unscaledTime + 0.5f; bool has, held; Player(out has, out held); AudioSource s = Source(); bool playing = s != null && s.clip != null && s.isPlaying; string song = playing ? s.clip.name : ""; StringBuilder sb = new StringBuilder(); sb.Append(Player() != null ? "1" : "0").Append(SEP) .Append(has ? "1" : "0").Append(SEP) .Append(held ? "1" : "0").Append(SEP) .Append(playing ? "1" : "0").Append(SEP) .Append(Clean(song)).Append(SEP) .Append(playing ? ((int)s.time).ToString(CultureInfo.InvariantCulture) : "0").Append(SEP) .Append(playing ? ((int)s.clip.length).ToString(CultureInfo.InvariantCulture) : "0").Append(SEP) .Append(Clean(loadingName)); string msg = sb.ToString(); if (msg == lastSent) return; lastSent = msg; Link.Send("MUSIC " + msg); } private static void Play(AudioClip c) { if (MusicManager.main == null) { Link.Send("MSG Start a run first: the game's music player only works in a run."); return; } MusicManager.main.PlaySong(c); } internal static bool Handle(string cmd, string[] p) { switch (cmd) { case "MUSIC": on = p.Length >= 2 && p[1] == "1"; lastSent = null; return true; case "MUSICLIST": { StringBuilder sb = new StringBuilder(); AudioClip[] all = BuiltIn(); for (int i = 0; i < all.Length; i++) { if (all[i] == null) continue; if (sb.Length > 0) sb.Append(SEP); sb.Append(Clean(all[i].name)); } Link.Send("MUSICLIST " + sb); return true; } case "MUSICPLAY": { if (p.Length < 3) return true; string kind = p[1], name = p[2].Trim(); if (kind == "b") { foreach (AudioClip c in BuiltIn()) if (c != null && c.name == name) { Play(c); lastSent = null; return true; } Link.Send("MSG The game doesn't have a song called " + Clean(name)); return true; } if (name.Length == 0 || name.IndexOfAny(new char[] { '/', '\\', ':' }) >= 0 || name.Contains("..")) return true; string path = Path.Combine(Folder(), name); if (!File.Exists(path)) { Link.Send("MSG " + Clean(name) + " isn't in the custommusic folder anymore"); return true; } AudioClip hit; if (cache.TryGetValue(name, out hit) && hit != null) { Play(hit); lastSent = null; return true; } if (loading != null) { Link.Send("MSG Still loading " + Clean(loadingName) + ", try again in a moment"); return true; } if (MusicManager.main == null) { Link.Send("MSG Start a run first: the game's music player only works in a run."); return true; } string ext = Path.GetExtension(name).ToLowerInvariant(); AudioType at = ext == ".mp3" ? AudioType.MPEG : ext == ".ogg" ? AudioType.OGGVORBIS : ext == ".wav" ? AudioType.WAV : AudioType.UNKNOWN; loading = UnityWebRequestMultimedia.GetAudioClip(new Uri(path).AbsoluteUri, at); loadingName = name; loading.SendWebRequest(); lastSent = null; return true; } case "MUSICSTOP": if (MusicManager.main != null) MusicManager.main.StopSong(); lastSent = null; return true; case "MUSICNEXT": if (MusicManager.main != null && WorldGeneration.world != null) MusicManager.main.PlayLayerSong(); lastSent = null; return true; case "MUSICSEEK": { float d; AudioSource s = Source(); if (p.Length < 2 || s == null || s.clip == null || !float.TryParse(p[1], NumberStyles.Float, CultureInfo.InvariantCulture, out d)) return true; s.time = Mathf.Clamp(s.time + d, 0f, Mathf.Max(0f, s.clip.length - 1f)); lastSent = null; return true; } } return false; } internal static void Reset() { on = false; lastSent = null; } } }