casualexpiesource

cu-trainer/Ultra Paradise Application/src/Music.cs

201 lines · 8.5 KB · plain text · download

1using System;
2using System.Collections.Generic;
3using System.Globalization;
4using System.IO;
5using System.Reflection;
6using System.Text;
7using UnityEngine;
8using UnityEngine.Networking;
9using Object = UnityEngine.Object;
10
11namespace UltraParadise
12{
13 internal static class GameMusic
14 {
15 private const char REC = '\u001d', SEP = '\u001c';
16 private static readonly FieldInfo sourceField = typeof(MusicManager).GetField("source", BindingFlags.NonPublic | BindingFlags.Instance);
17 private static readonly Dictionary<string, AudioClip> cache = new Dictionary<string, AudioClip>();
18 private static AudioClip[] builtIn;
19 private static UnityWebRequest loading;
20 private static string loadingName;
21 private static float nextSend;
22 private static string lastSent;
23 private static bool on;
24
25 private static string Folder()
26 {
27 return Path.Combine(Application.dataPath, "custommusic");
28 }
29
30 private static AudioSource Source()
31 {
32 if (MusicManager.main == null || sourceField == null) return null;
33 return sourceField.GetValue(MusicManager.main) as AudioSource;
34 }
35
36 private static Body Player()
37 {
38 return PlayerCamera.main != null && WorldGeneration.world != null ? PlayerCamera.main.body : null;
39 }
40
41 private static string Clean(string s)
42 {
43 return (s ?? "").Replace('\r', ' ').Replace('\n', ' ').Replace(REC, ' ').Replace(SEP, ' ');
44 }
45
46 private static void Player(out bool has, out bool held)
47 {
48 has = false;
49 held = false;
50 Body b = Player();
51 if (b == null || b.slots == null) return;
52 try
53 {
54 Item h = b.GetItem(b.handSlot);
55 if (h != null && h.id == "mp3player") { held = true; has = true; return; }
56 }
57 catch (Exception) { }
58 for (int i = 0; i < b.slots.Length; i++)
59 {
60 if (b.slots[i] == null) continue;
61 Item[] items = b.slots[i].GetComponentsInChildren<Item>(true);
62 for (int k = 0; k < items.Length; k++)
63 if (items[k] != null && items[k].id == "mp3player") { has = true; return; }
64 }
65 }
66
67 private static AudioClip[] BuiltIn()
68 {
69 if (builtIn == null) builtIn = Resources.LoadAll<AudioClip>("Sounds/music");
70 return builtIn;
71 }
72
73 internal static void Tick()
74 {
75 if (loading != null && loading.isDone)
76 {
77 UnityWebRequest r = loading;
78 string name = loadingName;
79 loading = null;
80 loadingName = null;
81 try
82 {
83 if (r.result == UnityWebRequest.Result.Success)
84 {
85 AudioClip c = DownloadHandlerAudioClip.GetContent(r);
86 if (c != null)
87 {
88 c.name = Path.GetFileNameWithoutExtension(name);
89 cache[name] = c;
90 Play(c);
91 Link.Send("MSG Playing " + Clean(name) + " in the game");
92 }
93 }
94 else Link.Send("MSG The game couldn't load " + Clean(name) + ": " + Clean(r.error));
95 }
96 finally { r.Dispose(); }
97 lastSent = null;
98 }
99 if (!on || Time.unscaledTime < nextSend) return;
100 nextSend = Time.unscaledTime + 0.5f;
101 bool has, held;
102 Player(out has, out held);
103 AudioSource s = Source();
104 bool playing = s != null && s.clip != null && s.isPlaying;
105 string song = playing ? s.clip.name : "";
106 StringBuilder sb = new StringBuilder();
107 sb.Append(Player() != null ? "1" : "0").Append(SEP)
108 .Append(has ? "1" : "0").Append(SEP)
109 .Append(held ? "1" : "0").Append(SEP)
110 .Append(playing ? "1" : "0").Append(SEP)
111 .Append(Clean(song)).Append(SEP)
112 .Append(playing ? ((int)s.time).ToString(CultureInfo.InvariantCulture) : "0").Append(SEP)
113 .Append(playing ? ((int)s.clip.length).ToString(CultureInfo.InvariantCulture) : "0").Append(SEP)
114 .Append(Clean(loadingName));
115 string msg = sb.ToString();
116 if (msg == lastSent) return;
117 lastSent = msg;
118 Link.Send("MUSIC " + msg);
119 }
120
121 private static void Play(AudioClip c)
122 {
123 if (MusicManager.main == null) { Link.Send("MSG Start a run first: the game's music player only works in a run."); return; }
124 MusicManager.main.PlaySong(c);
125 }
126
127 internal static bool Handle(string cmd, string[] p)
128 {
129 switch (cmd)
130 {
131 case "MUSIC":
132 on = p.Length >= 2 && p[1] == "1";
133 lastSent = null;
134 return true;
135 case "MUSICLIST":
136 {
137 StringBuilder sb = new StringBuilder();
138 AudioClip[] all = BuiltIn();
139 for (int i = 0; i < all.Length; i++)
140 {
141 if (all[i] == null) continue;
142 if (sb.Length > 0) sb.Append(SEP);
143 sb.Append(Clean(all[i].name));
144 }
145 Link.Send("MUSICLIST " + sb);
146 return true;
147 }
148 case "MUSICPLAY":
149 {
150 if (p.Length < 3) return true;
151 string kind = p[1], name = p[2].Trim();
152 if (kind == "b")
153 {
154 foreach (AudioClip c in BuiltIn())
155 if (c != null && c.name == name) { Play(c); lastSent = null; return true; }
156 Link.Send("MSG The game doesn't have a song called " + Clean(name));
157 return true;
158 }
159 if (name.Length == 0 || name.IndexOfAny(new char[] { '/', '\\', ':' }) >= 0 || name.Contains("..")) return true;
160 string path = Path.Combine(Folder(), name);
161 if (!File.Exists(path)) { Link.Send("MSG " + Clean(name) + " isn't in the custommusic folder anymore"); return true; }
162 AudioClip hit;
163 if (cache.TryGetValue(name, out hit) && hit != null) { Play(hit); lastSent = null; return true; }
164 if (loading != null) { Link.Send("MSG Still loading " + Clean(loadingName) + ", try again in a moment"); return true; }
165 if (MusicManager.main == null) { Link.Send("MSG Start a run first: the game's music player only works in a run."); return true; }
166 string ext = Path.GetExtension(name).ToLowerInvariant();
167 AudioType at = ext == ".mp3" ? AudioType.MPEG : ext == ".ogg" ? AudioType.OGGVORBIS : ext == ".wav" ? AudioType.WAV : AudioType.UNKNOWN;
168 loading = UnityWebRequestMultimedia.GetAudioClip(new Uri(path).AbsoluteUri, at);
169 loadingName = name;
170 loading.SendWebRequest();
171 lastSent = null;
172 return true;
173 }
174 case "MUSICSTOP":
175 if (MusicManager.main != null) MusicManager.main.StopSong();
176 lastSent = null;
177 return true;
178 case "MUSICNEXT":
179 if (MusicManager.main != null && WorldGeneration.world != null) MusicManager.main.PlayLayerSong();
180 lastSent = null;
181 return true;
182 case "MUSICSEEK":
183 {
184 float d;
185 AudioSource s = Source();
186 if (p.Length < 2 || s == null || s.clip == null || !float.TryParse(p[1], NumberStyles.Float, CultureInfo.InvariantCulture, out d)) return true;
187 s.time = Mathf.Clamp(s.time + d, 0f, Mathf.Max(0f, s.clip.length - 1f));
188 lastSent = null;
189 return true;
190 }
191 }
192 return false;
193 }
194
195 internal static void Reset()
196 {
197 on = false;
198 lastSent = null;
199 }
200 }
201}