cu-trainer/Ultra Paradise Application/src/MusicTab.cs
| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Diagnostics; |
| 4 | using System.Globalization; |
| 5 | using System.IO; |
| 6 | using System.Runtime.InteropServices; |
| 7 | using System.Threading; |
| 8 | using System.Windows.Forms; |
| 9 | |
| 10 | namespace UltraParadise |
| 11 | { |
| 12 | internal partial class MainForm |
| 13 | { |
| 14 | [DllImport("user32.dll")] private static extern void keybd_event(byte vk, byte scan, uint flags, UIntPtr extra); |
| 15 | |
| 16 | private const char MSEP = '\u001c'; |
| 17 | private Tab musicTab; |
| 18 | private volatile bool extScanning; |
| 19 | private bool extFound, jamShown; |
| 20 | private string extApp = "", extTitle = ""; |
| 21 | private DateTime extNext = DateTime.MinValue; |
| 22 | private bool gmRun, gmHas, gmHeld, gmPlaying; |
| 23 | private string gmSong = "", gmLoading = ""; |
| 24 | private int gmPos, gmLen; |
| 25 | private readonly List<string> gameSongs = new List<string>(); |
| 26 | private bool gameSongsAsked; |
| 27 | |
| 28 | private static readonly string[] SpotifyNames = { "spotify" }; |
| 29 | private static readonly string[] YtmNames = { "youtube music desktop app", "youtube music", "youtube-music-desktop-app", "youtube-music", "ytmdesktop", "ytmapp", "ytmusic" }; |
| 30 | |
| 31 | private bool MusicVisible { get { return musicTab != null && view.Tab == musicTab; } } |
| 32 | |
| 33 | private void SetupMusic() |
| 34 | { |
| 35 | foreach (Tab x in tabs) if (x.Music) musicTab = x; |
| 36 | SetupNowPlaying(); |
| 37 | RebuildMusic(); |
| 38 | } |
| 39 | |
| 40 | private string MusicFolder() |
| 41 | { |
| 42 | string g = GameDir(); |
| 43 | return string.IsNullOrEmpty(g) ? null : Path.Combine(Path.Combine(g, "CasualtiesUnknown_Data"), "custommusic"); |
| 44 | } |
| 45 | |
| 46 | private static Option MInfo(Func<string> text, bool local) |
| 47 | { |
| 48 | Option o = new Option(); o.Kind = Kind.Info; o.Id = "music:info"; o.Local = local; |
| 49 | o.Text = delegate (Dictionary<string, string> d) { return text(); }; |
| 50 | return o; |
| 51 | } |
| 52 | |
| 53 | private static Option MButtons(string label, bool local, Func<bool> show, params Btn[] b) |
| 54 | { |
| 55 | Option o = new Option(); o.Kind = Kind.Buttons; o.Id = "music:btn"; o.Name = label; o.Local = local; o.Show = show; |
| 56 | o.Buttons = new List<Btn>(b); |
| 57 | return o; |
| 58 | } |
| 59 | |
| 60 | private static string Clock(int s) |
| 61 | { |
| 62 | return (s / 60).ToString(CultureInfo.InvariantCulture) + ":" + (s % 60).ToString("00", CultureInfo.InvariantCulture); |
| 63 | } |
| 64 | |
| 65 | private static string ShortName(string s, int max) |
| 66 | { |
| 67 | return s.Length <= max ? s : s.Substring(0, max - 1) + "…"; |
| 68 | } |
| 69 | |
| 70 | private void RebuildMusic() |
| 71 | { |
| 72 | if (musicTab == null) return; |
| 73 | foreach (List<Section> c in musicTab.Cols) c.Clear(); |
| 74 | |
| 75 | Section s = new Section("GAME MP3 PLAYER"); |
| 76 | musicTab.Cols[1].Add(s); |
| 77 | s.Rows.Add(MInfo(delegate |
| 78 | { |
| 79 | if (!gmRun) return "Start a run to use the game's music player."; |
| 80 | return gmHeld ? "You're holding the MP3 player." : gmHas ? "The MP3 player is in your inventory." : "You don't have an MP3 player."; |
| 81 | }, false)); |
| 82 | s.Rows.Add(MInfo(delegate |
| 83 | { |
| 84 | if (!gmRun) return ""; |
| 85 | if (gmLoading.Length > 0) return "Loading " + ShortName(gmLoading, 40) + "..."; |
| 86 | return gmPlaying ? "Now playing: " + ShortName(gmSong, 34) + " " + Clock(gmPos) + " / " + Clock(gmLen) : "Nothing playing"; |
| 87 | }, false)); |
| 88 | s.Rows.Add(MButtons(null, false, null, new Btn("Stop", "@m:stop", null), new Btn("-10s", "@m:seek", "-10"), new Btn("+10s", "@m:seek", "10"), new Btn("Layer Song", "@m:layer", null))); |
| 89 | s.Rows.Add(MButtons(null, false, delegate { return gmRun && !gmHas; }, new Btn("Give Me an MP3 Player", "@m:give", null))); |
| 90 | s.Rows.Add(NoteRow("Songs you play here go through the game's own music player, the same one the MP3 player uses, so they mix with the game's sound settings. You don't need the MP3 player to play them from the trainer.")); |
| 91 | |
| 92 | s = new Section("YOUR SONGS"); |
| 93 | musicTab.Cols[2].Add(s); |
| 94 | s.Rows.Add(MButtons(null, true, null, new Btn("Open Folder", "@m:folder", null), new Btn("Add Songs...", "@m:add", null), new Btn("Refresh", "@m:refresh", null))); |
| 95 | string dir = MusicFolder(); |
| 96 | List<string> files = new List<string>(); |
| 97 | if (dir != null && Directory.Exists(dir)) |
| 98 | { |
| 99 | try |
| 100 | { |
| 101 | foreach (string f in Directory.GetFiles(dir)) |
| 102 | { |
| 103 | string e = Path.GetExtension(f).ToLowerInvariant(); |
| 104 | if (e == ".mp3" || e == ".ogg" || e == ".wav") files.Add(Path.GetFileName(f)); |
| 105 | } |
| 106 | } |
| 107 | catch (Exception) { } |
| 108 | } |
| 109 | files.Sort(StringComparer.OrdinalIgnoreCase); |
| 110 | if (dir == null) s.Rows.Add(NoteRow("Start the game once so the trainer knows where it is.")); |
| 111 | else if (files.Count == 0) s.Rows.Add(NoteRow("No songs yet. Click Add Songs (mp3, ogg or wav), or drop them in the game's custommusic folder. The MP3 player in the game picks them up too.")); |
| 112 | foreach (string f in files) s.Rows.Add(MButtons(ShortName(f, 38), false, null, new Btn("Play", "@m:playf", f))); |
| 113 | |
| 114 | if (gameSongs.Count > 0) |
| 115 | { |
| 116 | s = new Section("GAME SONGS"); |
| 117 | musicTab.Cols[2].Add(s); |
| 118 | foreach (string g in gameSongs) s.Rows.Add(MButtons(ShortName(g, 38), false, null, new Btn("Play", "@m:playb", g))); |
| 119 | } |
| 120 | if (MusicVisible) view.Invalidate(); |
| 121 | } |
| 122 | |
| 123 | private void MusicButton(Btn b) |
| 124 | { |
| 125 | switch (b.Act) |
| 126 | { |
| 127 | case "@m:key": |
| 128 | { |
| 129 | byte vk = byte.Parse(b.Arg, CultureInfo.InvariantCulture); |
| 130 | keybd_event(vk, 0, 1, UIntPtr.Zero); |
| 131 | keybd_event(vk, 0, 3, UIntPtr.Zero); |
| 132 | extNext = DateTime.UtcNow.AddMilliseconds(700); |
| 133 | return; |
| 134 | } |
| 135 | case "@m:folder": |
| 136 | { |
| 137 | string d = MusicFolder(); |
| 138 | if (d == null) { SetMessage("Start the game once so the trainer knows where it is.", true); return; } |
| 139 | try { Directory.CreateDirectory(d); Process.Start("explorer.exe", "\"" + d + "\""); } catch (Exception) { SetMessage("Couldn't open the folder.", true); } |
| 140 | return; |
| 141 | } |
| 142 | case "@m:add": |
| 143 | { |
| 144 | string d = MusicFolder(); |
| 145 | if (d == null) { SetMessage("Start the game once so the trainer knows where it is.", true); return; } |
| 146 | using (OpenFileDialog ofd = new OpenFileDialog()) |
| 147 | { |
| 148 | ofd.Title = "Add songs to the game"; |
| 149 | ofd.Filter = "Songs (*.mp3;*.ogg;*.wav)|*.mp3;*.ogg;*.wav"; |
| 150 | ofd.Multiselect = true; |
| 151 | if (ofd.ShowDialog(this) != DialogResult.OK) return; |
| 152 | int n = 0; |
| 153 | try |
| 154 | { |
| 155 | Directory.CreateDirectory(d); |
| 156 | foreach (string f in ofd.FileNames) |
| 157 | { |
| 158 | string to = Path.Combine(d, Path.GetFileName(f)); |
| 159 | if (File.Exists(to)) continue; |
| 160 | File.Copy(f, to); |
| 161 | n++; |
| 162 | } |
| 163 | } |
| 164 | catch (Exception e) { SetMessage("Couldn't copy the songs: " + e.Message, true); } |
| 165 | if (n > 0) SetMessage("Added " + n + " song" + (n == 1 ? "" : "s") + ". The game's own MP3 player sees them after a game restart; the trainer can play them right away.", false); |
| 166 | } |
| 167 | RebuildMusic(); |
| 168 | return; |
| 169 | } |
| 170 | case "@m:refresh": |
| 171 | gameSongsAsked = false; |
| 172 | if (link != null && link.IsConnected) { link.Send("MUSICLIST"); gameSongsAsked = true; } |
| 173 | RebuildMusic(); |
| 174 | return; |
| 175 | } |
| 176 | if (link == null || !link.IsConnected) { SetMessage("Attach to the game first.", true); return; } |
| 177 | switch (b.Act) |
| 178 | { |
| 179 | case "@m:stop": link.Send("MUSICSTOP"); break; |
| 180 | case "@m:seek": link.Send("MUSICSEEK " + b.Arg); break; |
| 181 | case "@m:layer": link.Send("MUSICNEXT"); break; |
| 182 | case "@m:give": link.Send("SPAWN mp3player 1"); break; |
| 183 | case "@m:playf": link.Send("MUSICPLAY f " + b.Arg); break; |
| 184 | case "@m:playb": link.Send("MUSICPLAY b " + b.Arg); break; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | private void MusicTabShown(bool shown) |
| 189 | { |
| 190 | if (link == null || !link.IsConnected) return; |
| 191 | link.Send("MUSIC " + (shown ? "1" : "0")); |
| 192 | if (shown && !gameSongsAsked) { gameSongsAsked = true; link.Send("MUSICLIST"); } |
| 193 | } |
| 194 | |
| 195 | private void MusicLinked() |
| 196 | { |
| 197 | gameSongsAsked = false; |
| 198 | if (MusicVisible) MusicTabShown(true); |
| 199 | } |
| 200 | |
| 201 | private void MusicUnlinked() |
| 202 | { |
| 203 | gmRun = gmHas = gmHeld = gmPlaying = false; |
| 204 | gmSong = gmLoading = ""; |
| 205 | if (MusicVisible) view.Invalidate(); |
| 206 | } |
| 207 | |
| 208 | private void OnMusicLine(string rest) |
| 209 | { |
| 210 | string[] f = rest.Split(MSEP); |
| 211 | if (f.Length < 8) return; |
| 212 | gmRun = f[0] == "1"; |
| 213 | gmHas = f[1] == "1"; |
| 214 | gmHeld = f[2] == "1"; |
| 215 | gmPlaying = f[3] == "1"; |
| 216 | gmSong = f[4]; |
| 217 | int.TryParse(f[5], NumberStyles.Integer, CultureInfo.InvariantCulture, out gmPos); |
| 218 | gmStamp = DateTime.UtcNow; |
| 219 | int.TryParse(f[6], NumberStyles.Integer, CultureInfo.InvariantCulture, out gmLen); |
| 220 | gmLoading = f[7]; |
| 221 | if (MusicVisible) view.Invalidate(); |
| 222 | } |
| 223 | |
| 224 | private void OnMusicList(string rest) |
| 225 | { |
| 226 | gameSongs.Clear(); |
| 227 | foreach (string s in rest.Split(MSEP)) if (s.Trim().Length > 0) gameSongs.Add(s.Trim()); |
| 228 | gameSongs.Sort(StringComparer.OrdinalIgnoreCase); |
| 229 | RebuildMusic(); |
| 230 | } |
| 231 | |
| 232 | private static bool NameIn(string n, string[] list) |
| 233 | { |
| 234 | foreach (string x in list) if (n == x) return true; |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | private void MusicTick() |
| 239 | { |
| 240 | if (extScanning || DateTime.UtcNow < extNext) return; |
| 241 | extNext = DateTime.UtcNow.AddSeconds(2); |
| 242 | extScanning = true; |
| 243 | Thread t = new Thread(delegate () |
| 244 | { |
| 245 | bool found = false; |
| 246 | string app = "", title = ""; |
| 247 | try |
| 248 | { |
| 249 | foreach (Process p in Process.GetProcesses()) |
| 250 | { |
| 251 | try |
| 252 | { |
| 253 | string n = p.ProcessName.ToLowerInvariant(); |
| 254 | bool sp = NameIn(n, SpotifyNames), yt = !sp && NameIn(n, YtmNames); |
| 255 | if (!sp && !yt) continue; |
| 256 | if (!found || (title.Length == 0 && sp == (app == "Spotify"))) |
| 257 | { |
| 258 | found = true; |
| 259 | app = sp ? "Spotify" : "YouTube Music Desktop"; |
| 260 | } |
| 261 | string w = ""; |
| 262 | try { w = p.MainWindowTitle ?? ""; } catch (Exception) { } |
| 263 | if (!sp) { int bar = w.LastIndexOf(" | ", StringComparison.Ordinal); if (bar > 0 && w.Substring(bar + 3).Trim().ToLowerInvariant().StartsWith("ytm", StringComparison.Ordinal)) w = w.Substring(0, bar).Trim(); } |
| 264 | if (w.Length > 0 && TrackTitle(w, sp)) { app = sp ? "Spotify" : "YouTube Music Desktop"; title = w; } |
| 265 | } |
| 266 | catch (Exception) { } |
| 267 | finally { p.Dispose(); } |
| 268 | } |
| 269 | } |
| 270 | catch (Exception) { } |
| 271 | Ui(delegate |
| 272 | { |
| 273 | extScanning = false; |
| 274 | bool was = extFound; |
| 275 | bool changed = was != found || extApp != app || extTitle != title; |
| 276 | extFound = found; |
| 277 | extApp = app; |
| 278 | extTitle = title; |
| 279 | if (found && !was && !jamShown) |
| 280 | { |
| 281 | jamShown = true; |
| 282 | SetMessage("Jamming? Control your media here! (Music tab)", false); |
| 283 | } |
| 284 | if (!found) jamShown = false; |
| 285 | np.Found = found; |
| 286 | np.FallbackApp = app; |
| 287 | np.FallbackTitle = title; |
| 288 | if (changed && MusicVisible) { view.Invalidate(); np.Invalidate(); } |
| 289 | }); |
| 290 | }); |
| 291 | t.IsBackground = true; |
| 292 | t.Start(); |
| 293 | } |
| 294 | |
| 295 | private static bool TrackTitle(string w, bool spotify) |
| 296 | { |
| 297 | string l = w.ToLowerInvariant(); |
| 298 | if (spotify) return w.Contains(" - ") || w.Contains(" – "); |
| 299 | return l != "youtube music" && l != "youtube music desktop app" && !l.StartsWith("ytm", StringComparison.Ordinal); |
| 300 | } |
| 301 | } |
| 302 | } |