This repository has been archived by the owner on Jan 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
BoomboxControllerOLD.cs
385 lines (315 loc) · 12.6 KB
/
BoomboxControllerOLD.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// OLD CONTROLLER BEFORE USING NETWORKED OBJECT
//using BepInEx.Logging;
//using BepInEx;
//using LC_API.ServerAPI;
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Security.Policy;
//using System.Text;
//using System.Threading.Tasks;
//using System.IO;
//using YoutubeDLSharp;
//using static YoutubeBoombox.YoutubeBoombox;
//using UnityEngine;
//using System.Collections;
//using UnityEngine.InputSystem.XR;
//using System.Runtime.InteropServices;
//namespace YoutubeBoombox
//{
// public class BoomboxController
// {
// private static Dictionary<BoomboxItem, BoomboxController> controllers = new Dictionary<BoomboxItem, BoomboxController>();
// public static void Download(BoomboxItem boombox, string id, string type, bool broadcast = false)
// {
// BoomboxController controller = null;
// if (!controllers.ContainsKey(boombox))
// {
// controller = new BoomboxController(boombox);
// controllers.Add(boombox, controller);
// }
// else
// {
// controller = controllers[boombox];
// }
// controller.ProcessRequest(id, type, broadcast);
// }
// public static void AddReadyClient(BoomboxItem boombox, bool self = false)
// {
// BoomboxController controller = null;
// if (!controllers.ContainsKey(boombox))
// {
// controller = new BoomboxController(boombox);
// controllers.Add(boombox, controller);
// }
// else
// {
// controller = controllers[boombox];
// }
// controller.AddReadyClient(self);
// }
// public static void SetIsPlaylist(BoomboxItem boombox, bool isPlaylist)
// {
// if (controllers.TryGetValue(boombox, out BoomboxController controller))
// {
// controller.IsPlaylist = isPlaylist;
// }
// else
// {
// BoomboxController newController = new BoomboxController(boombox);
// controller.IsPlaylist = isPlaylist;
// controllers.Add(boombox, newController);
// }
// }
// public static void SetCurrentUrl(BoomboxItem boombox, string url)
// {
// if (controllers.TryGetValue(boombox, out BoomboxController controller))
// {
// controller.CurrentUrl = url;
// }
// else
// {
// BoomboxController newController = new BoomboxController(boombox);
// controller.CurrentUrl = url;
// controllers.Add(boombox, newController);
// }
// }
// public static void SetCurrentId(BoomboxItem boombox, string id)
// {
// if (controllers.TryGetValue(boombox, out BoomboxController controller))
// {
// controller.CurrentId = id;
// }
// else
// {
// BoomboxController newController = new BoomboxController(boombox);
// controller.CurrentId = id;
// controllers.Add(boombox, newController);
// }
// }
// public static void ResetReadyClients(BoomboxItem boombox)
// {
// if (controllers.TryGetValue(boombox, out BoomboxController controller))
// {
// controller.ReadyClients = 0;
// }
// }
// public static void Clear()
// {
// controllers.Clear();
// }
// public BoomboxItem Boombox { get; private set; }
// public bool LocalPlayed { get; set; }
// public string CurrentUrl { get; set; }
// public string CurrentId { get; set; }
// public bool IsPlaylist { get; set; } = false;
// public int PlaylistCurrentIndex { get; set; } = 0;
// public int ReadyClients { get; set; } = 0;
// public BoomboxController(BoomboxItem boombox)
// {
// Boombox = boombox;
// }
// public void AddReadyClient(bool self = false)
// {
// ReadyClients++;
// if (self) Networking.Broadcast((int)Boombox.NetworkObjectId, NetworkingSignatures.BOOMBOX_READY_CLIENT_SIG);
// DebugLog($"READY CLIENT {ReadyClients}/{StartOfRound.Instance.connectedPlayersAmount + 1} | IS SELF?: {self}", EnableDebugLogs.Value);
// if (ReadyClients >= StartOfRound.Instance.connectedPlayersAmount + 1)
// {
// ReadyClients = 0;
// Boombox.boomboxAudio.loop = true;
// Boombox.isBeingUsed = true;
// Boombox.isPlayingMusic = true;
// Boombox.boomboxAudio.Play();
// if (IsPlaylist)
// {
// Boombox.boomboxAudio.loop = false;
// Boombox.StartCoroutine(PlaylistCoroutine());
// }
// }
// }
// public IEnumerator PlaylistCoroutine()
// {
// PrepareNextSongInPlaylist();
// while (Boombox.boomboxAudio.isPlaying)
// {
// yield return new WaitForSeconds(1);
// }
// IncrementPlaylistIndex();
// }
// public IEnumerator LoadSongCoroutine(string path)
// {
// if (PathsThisSession.Contains(path)) PathsThisSession.Remove(path);
// PathsThisSession.Insert(0, path);
// if (PathsThisSession.Count > MaxCachedDownloads.Value)
// {
// File.Delete(PathsThisSession[PathsThisSession.Count - 1]);
// PathsThisSession.RemoveAt(PathsThisSession.Count - 1);
// }
// string url = string.Format("file://{0}", path);
// WWW www = new WWW(url);
// yield return www;
// Boombox.boomboxAudio.clip = www.GetAudioClip(false, false);
// Boombox.boomboxAudio.pitch = 1f;
// YoutubeBoombox.DebugLog("BOOMBOX READY!", EnableDebugLogs.Value);
// AddReadyClient(true);
// }
// public void IncrementPlaylistIndex()
// {
// Boombox.boomboxAudio.Stop();
// PlaylistCurrentIndex++;
// ReadyClients = 0;
// if (InfoCache.PlaylistCache.TryGetValue(CurrentId, out List<string> videoIds))
// {
// if (PlaylistCurrentIndex < videoIds.Count)
// {
// string id = videoIds[PlaylistCurrentIndex];
// string url = $"https://youtube.com/watch?v={id}";
// string newPath = Path.Combine(DownloadsPath, $"{id}.mp3");
// DownloadVideo(id, url, newPath);
// }
// }
// }
// public void DownloadCurrentVideo(string newPath)
// {
// DownloadVideo(CurrentId, CurrentUrl, newPath);
// }
// public async void DownloadVideo(string id, string url, string newPath)
// {
// if (File.Exists(newPath))
// {
// Boombox.StartCoroutine(LoadSongCoroutine(newPath));
// return;
// }
// if (InfoCache.DurationCache.TryGetValue(id, out float duration))
// {
// if (duration > MaxSongDuration.Value)
// {
// AddReadyClient(true);
// return;
// }
// }
// else
// {
// var videoDataResult = await YoutubeBoombox.YoutubeDL.RunVideoDataFetch(url);
// if (videoDataResult.Success && videoDataResult.Data.Duration != null)
// {
// InfoCache.DurationCache.Add(id, (float)videoDataResult.Data.Duration);
// // Skip downloading videos that are too long
// if (videoDataResult.Data.Duration > MaxSongDuration.Value)
// {
// AddReadyClient(true);
// return;
// }
// }
// else
// {
// AddReadyClient(true);
// return;
// }
// }
// var res = await YoutubeBoombox.YoutubeDL.RunAudioDownload(url, YoutubeDLSharp.Options.AudioConversionFormat.Mp3);
// if (res.Success)
// {
// YoutubeBoombox.DebugLog(res.Data, EnableDebugLogs.Value);
// YoutubeBoombox.DebugLog(newPath, EnableDebugLogs.Value);
// File.Move(res.Data, newPath);
// Boombox.StartCoroutine(LoadSongCoroutine(newPath));
// }
// }
// public void PrepareNextSongInPlaylist()
// {
// if (InfoCache.PlaylistCache.TryGetValue(CurrentId, out List<string> videoIds))
// {
// if (PlaylistCurrentIndex + 1 < videoIds.Count)
// {
// string id = videoIds[PlaylistCurrentIndex + 1];
// string url = $"https://youtube.com/watch?v={id}";
// string newPath = Path.Combine(DownloadsPath, $"{id}.mp3");
// PrepareSong(id, url, newPath);
// }
// }
// }
// public async void PrepareSong(string id, string url, string newPath)
// {
// if (File.Exists(newPath)) return;
// if (InfoCache.DurationCache.TryGetValue(id, out float duration))
// {
// if (duration > MaxSongDuration.Value) return;
// }
// else
// {
// var videoDataResult = await YoutubeBoombox.YoutubeDL.RunVideoDataFetch(url);
// if (videoDataResult.Success && videoDataResult.Data.Duration != null)
// {
// InfoCache.DurationCache.Add(id, (float)videoDataResult.Data.Duration);
// // Skip preparing videos that are too long
// if (videoDataResult.Data.Duration > MaxSongDuration.Value) return;
// }
// else
// {
// return;
// }
// }
// var res = await YoutubeBoombox.YoutubeDL.RunAudioDownload(url, YoutubeDLSharp.Options.AudioConversionFormat.Mp3);
// if (res.Success) File.Move(res.Data, newPath);
// }
// public async void DownloadCurrentPlaylist()
// {
// PlaylistCurrentIndex = 0;
// if (!InfoCache.PlaylistCache.TryGetValue(CurrentId, out List<string> videoIds))
// {
// var playlistResult = await YoutubeBoombox.YoutubeDL.RunVideoPlaylistDownload(CurrentUrl, 1, null, null, "bestvideo+bestaudio/best",
// YoutubeDLSharp.Options.VideoRecodeFormat.None, default, null, new InfoCache(CurrentId),
// new YoutubeDLSharp.Options.OptionSet()
// {
// FlatPlaylist = true,
// DumpJson = true
// });
// if (!playlistResult.Success)
// {
// AddReadyClient(Boombox, true);
// return;
// }
// else
// {
// videoIds = InfoCache.PlaylistCache[CurrentId];
// }
// }
// if (videoIds.Count == 0)
// {
// AddReadyClient(true);
// return;
// }
// string id = videoIds[0];
// string url = $"https://youtube.com/watch?v={id}";
// string newPath = Path.Combine(DownloadsPath, $"{id}.mp3");
// DownloadVideo(id, url, newPath);
// }
// public void ProcessRequest(string id, string type, bool broadcast = false)
// {
// if (broadcast)
// {
// LocalPlayed = true;
// Networking.Broadcast($"{id}|{type}|{Boombox.NetworkObjectId}", NetworkingSignatures.BOOMBOX_SIG);
// }
// else
// {
// LocalPlayed = false;
// }
// string newPath = Path.Combine(DownloadsPath, $"{id}.mp3");
// string url = type == "video" ? $"https://youtube.com/watch?v={id}" : $"https://youtube.com/playlist?list={id}";
// CurrentUrl = url;
// IsPlaylist = type != "video";
// CurrentId = id;
// if (!IsPlaylist)
// {
// DownloadCurrentVideo(newPath);
// }
// else
// {
// DownloadCurrentPlaylist();
// }
// }
// }
//}