This repository has been archived by the owner on Nov 9, 2021. It is now read-only.
/
PluginUI.cs
513 lines (408 loc) · 20.1 KB
/
PluginUI.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
using HMUI;
using SimpleJSON;
using SongLoaderPlugin;
using SongLoaderPlugin.OverrideClasses;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
namespace BeatSaverDownloader.PluginUI
{
class PluginUI : MonoBehaviour
{
public static PluginUI _instance;
private VotingUI _votingUI;
private SongListUITweaks _tweaks;
private Logger log = new Logger("BeatSaverDownloader");
public BeatSaverMasterViewController _beatSaverViewController;
private RectTransform _mainMenuRectTransform;
private StandardLevelSelectionFlowCoordinator _standardLevelSelectionFlowCoordinator;
private GameplayMode _gameplayMode;
private MainMenuViewController _mainMenuViewController;
private StandardLevelDetailViewController _songDetailViewController;
private Button _deleteButton;
private Button _playButton;
private Button _favButton;
private Prompt _confirmDeleteState;
public static string playerId;
private bool isDeleting;
public LevelCollectionsForGameplayModes _levelCollections;
public List<LevelCollectionsForGameplayModes.LevelCollectionForGameplayMode> _levelCollectionsForGameModes;
private bool _deleting
{
get { return isDeleting; }
set
{
isDeleting = value;
if (value)
{
_playButton.interactable = false;
}
else
{
_playButton.interactable = true;
}
}
}
public static void OnLoad()
{
if (_instance != null)
{
return;
}
new GameObject("BeatSaver Plugin").AddComponent<PluginUI>();
}
public void Awake()
{
_instance = this;
_votingUI = gameObject.AddComponent<VotingUI>();
_tweaks = gameObject.AddComponent<SongListUITweaks>();
}
public void Start()
{
playerId = ReflectionUtil.GetPrivateField<string>(PersistentSingleton<PlatformLeaderboardsModel>.instance, "_playerId");
StartCoroutine(_votingUI.WaitForResults());
if (!PluginConfig.disableSongListTweaks)
{
_votingUI.continuePressed += _votingUI_continuePressed;
StartCoroutine(WaitForSongListUI());
}
if (SongLoader.AreSongsLoaded)
{
SongLoader_SongsLoadedEvent(null, SongLoader.CustomLevels);
}
else
{
SongLoader.SongsLoadedEvent += SongLoader_SongsLoadedEvent;
}
try
{
_mainMenuViewController = Resources.FindObjectsOfTypeAll<MainMenuViewController>().First();
_mainMenuRectTransform = _mainMenuViewController.transform as RectTransform;
_standardLevelSelectionFlowCoordinator = Resources.FindObjectsOfTypeAll<StandardLevelSelectionFlowCoordinator>().First();
_gameplayMode = ReflectionUtil.GetPrivateField<GameplayMode>(_standardLevelSelectionFlowCoordinator, "_gameplayMode");
if (!PluginConfig.disableSongListTweaks)
{
ReflectionUtil.GetPrivateField<StandardLevelListViewController>(_standardLevelSelectionFlowCoordinator, "_levelListViewController").didSelectLevelEvent += PluginUI_didSelectSongEvent;
}
CreateBeatSaverButton();
}
catch (Exception e)
{
log.Exception("EXCEPTION ON AWAKE(TRY CREATE BUTTON): " + e);
}
}
private void SongLoader_SongsLoadedEvent(SongLoader arg1, List<CustomLevel> arg2)
{
_levelCollections = Resources.FindObjectsOfTypeAll<LevelCollectionsForGameplayModes>().FirstOrDefault();
_levelCollectionsForGameModes = ReflectionUtil.GetPrivateField<LevelCollectionsForGameplayModes.LevelCollectionForGameplayMode[]>(_levelCollections, "_collections").ToList();
}
private void PluginUI_didSelectSongEvent(StandardLevelListViewController sender, IStandardLevel level)
{
UpdateDetailsUI(sender, level.levelID);
}
public void Update()
{
if (Input.GetKeyDown(KeyCode.B))
{
PluginConfig.LoadOrCreateConfig();
}
}
private IEnumerator WaitForSongListUI()
{
log.Log("Waiting for song list...");
yield return new WaitUntil(delegate () { return Resources.FindObjectsOfTypeAll<StandardLevelSelectionFlowCoordinator>().Any(); });
log.Log("Found song list!");
_tweaks.SongListUIFound();
}
private void _votingUI_continuePressed(string selectedLevelId)
{
UpdateDetailsUI(null, selectedLevelId);
if (SongListUITweaks.lastSortMode != SortMode.All)
{
_tweaks.ShowLevels(SongListUITweaks.lastSortMode);
}
}
private void UpdateDetailsUI(StandardLevelListViewController sender, string selectedLevel)
{
if (_deleting)
{
_confirmDeleteState = Prompt.No;
}
if (_songDetailViewController == null)
{
_songDetailViewController = ReflectionUtil.GetPrivateField<StandardLevelDetailViewController>(_standardLevelSelectionFlowCoordinator, "_levelDetailViewController");
}
RectTransform detailsRectTransform = _songDetailViewController.GetComponent<RectTransform>();
if (_deleteButton == null)
{
_deleteButton = BeatSaberUI.CreateUIButton(detailsRectTransform, "PlayButton");
BeatSaberUI.SetButtonText(_deleteButton, "Delete");
(_deleteButton.transform as RectTransform).anchoredPosition = new Vector2(27f, 11f);
(_deleteButton.transform as RectTransform).sizeDelta = new Vector2(18f, 10f);
_deleteButton.onClick.RemoveAllListeners();
_deleteButton.onClick.AddListener(delegate ()
{
StartCoroutine(DeleteSong(selectedLevel));
});
if (selectedLevel.Length <= 32)
{
_deleteButton.interactable = false;
}
}
else
{
if(selectedLevel.Length > 32)
{
_deleteButton.onClick.RemoveAllListeners();
_deleteButton.onClick.AddListener(delegate ()
{
StartCoroutine(DeleteSong(selectedLevel));
});
_deleteButton.interactable = true;
}
else
{
_deleteButton.interactable = false;
}
}
if(_playButton == null)
{
_playButton = _songDetailViewController.GetComponentInChildren<Button>();
(_playButton.transform as RectTransform).sizeDelta = new Vector2(30f, 10f);
(_playButton.transform as RectTransform).anchoredPosition = new Vector2(2f, 6f);
_playButton.interactable = true;
}
if (_favButton == null)
{
_favButton = BeatSaberUI.CreateUIButton(detailsRectTransform, "ApplyButton");
RectTransform iconTransform = _favButton.GetComponentsInChildren<RectTransform>(true).First(x => x.name == "Icon");
iconTransform.gameObject.SetActive(true);
Destroy(iconTransform.parent.GetComponent<HorizontalLayoutGroup>());
iconTransform.sizeDelta = new Vector2(8f, 8f);
Destroy(_favButton.GetComponentsInChildren<RectTransform>(true).First(x => x.name == "Text").gameObject);
BeatSaberUI.SetButtonText(_favButton, "");
BeatSaberUI.SetButtonIcon(_favButton, Base64ToSprite(PluginConfig.favoriteSongs.Contains(selectedLevel) ? Base64Sprites.RemoveFromFavorites : Base64Sprites.AddToFavorites));
(_favButton.transform as RectTransform).anchoredPosition = new Vector2(-24f, 6f);
(_favButton.transform as RectTransform).sizeDelta = new Vector2(10f, 10f);
_favButton.onClick.RemoveAllListeners();
_favButton.onClick.AddListener(delegate ()
{
if (PluginConfig.favoriteSongs.Contains(selectedLevel))
{
PluginConfig.favoriteSongs.Remove(selectedLevel);
}
else
{
PluginConfig.favoriteSongs.Add(selectedLevel);
}
BeatSaberUI.SetButtonIcon(_favButton, Base64ToSprite(PluginConfig.favoriteSongs.Contains(selectedLevel) ? Base64Sprites.RemoveFromFavorites : Base64Sprites.AddToFavorites));
PluginConfig.SaveConfig();
});
}
else
{
BeatSaberUI.SetButtonIcon(_favButton, Base64ToSprite(PluginConfig.favoriteSongs.Contains(selectedLevel) ? Base64Sprites.RemoveFromFavorites : Base64Sprites.AddToFavorites));
_favButton.onClick.RemoveAllListeners();
_favButton.onClick.AddListener(delegate ()
{
if (PluginConfig.favoriteSongs.Contains(selectedLevel))
{
PluginConfig.favoriteSongs.Remove(selectedLevel);
}
else
{
PluginConfig.favoriteSongs.Add(selectedLevel);
}
BeatSaberUI.SetButtonIcon(_favButton, Base64ToSprite(PluginConfig.favoriteSongs.Contains(selectedLevel) ? Base64Sprites.RemoveFromFavorites : Base64Sprites.AddToFavorites));
PluginConfig.SaveConfig();
});
}
}
IEnumerator DeleteSong(string levelId)
{
IStandardLevel[] _levelsForGamemode = ReflectionUtil.GetPrivateField<IStandardLevel[]>(ReflectionUtil.GetPrivateField<StandardLevelListViewController>(_standardLevelSelectionFlowCoordinator, "_levelListViewController"), "_levels");
if (levelId.Length > 32 && _levelsForGamemode.Any(x => x.levelID == levelId) )
{
int currentSongIndex = _levelsForGamemode.ToList().FindIndex(x => x.levelID == levelId);
currentSongIndex += (currentSongIndex == 0) ? 1 : -1;
string nextLevelId = _levelsForGamemode[currentSongIndex].levelID;
bool zippedSong = false;
_deleting = true;
string _songPath = SongLoader.CustomLevels.First(x => x.levelID == levelId).customSongInfo.path;
if (!string.IsNullOrEmpty(_songPath) && _songPath.Contains("/.cache/"))
{
zippedSong = true;
}
if (string.IsNullOrEmpty(_songPath))
{
log.Error("Song path is null or empty!");
_playButton.interactable = true;
yield break;
}
if (!Directory.Exists(_songPath))
{
log.Error("Song folder does not exists!");
_playButton.interactable = true;
yield break;
}
yield return PromptDeleteFolder(_songPath);
if (_confirmDeleteState == Prompt.Yes)
{
if (zippedSong)
{
log.Log("Deleting \"" + _songPath.Substring(_songPath.LastIndexOf('/')) + "\"...");
Directory.Delete(_songPath, true);
string songHash = Directory.GetParent(_songPath).Name;
if (Directory.GetFileSystemEntries(_songPath.Substring(0, _songPath.LastIndexOf('/'))).Length == 0)
{
log.Log("Deleting empty folder \"" + _songPath.Substring(0, _songPath.LastIndexOf('/')) + "\"...");
Directory.Delete(_songPath.Substring(0, _songPath.LastIndexOf('/')), false);
}
string docPath = Application.dataPath;
docPath = docPath.Substring(0, docPath.Length - 5);
docPath = docPath.Substring(0, docPath.LastIndexOf("/"));
string customSongsPath = docPath + "/CustomSongs/";
string hash = "";
foreach (string file in Directory.GetFiles(customSongsPath, "*.zip"))
{
if (CreateMD5FromFile(file, out hash))
{
if (hash == songHash)
{
File.Delete(file);
break;
}
}
}
}
else
{
log.Log("Deleting \"" + _songPath.Substring(_songPath.LastIndexOf('/')) + "\"...");
Directory.Delete(_songPath, true);
if (Directory.GetFileSystemEntries(_songPath.Substring(0, _songPath.LastIndexOf('/'))).Length == 0)
{
log.Log("Deleting empty folder \"" + _songPath.Substring(0, _songPath.LastIndexOf('/')) + "\"...");
Directory.Delete(_songPath.Substring(0, _songPath.LastIndexOf('/')), false);
}
}
SongLoader.Instance.RemoveSongWithLevelID(levelId);
StandardLevelListViewController songListViewController = ReflectionUtil.GetPrivateField<StandardLevelListViewController>(_standardLevelSelectionFlowCoordinator, "_levelListViewController");
ReflectionUtil.SetPrivateField(songListViewController.GetComponentInChildren<StandardLevelListTableView>(), "_levels", _tweaks.GetSortedLevels(SongListUITweaks.lastSortMode));
ReflectionUtil.SetPrivateField(songListViewController, "_levels", _tweaks.GetSortedLevels(SongListUITweaks.lastSortMode));
TableView _songListTableView = songListViewController.GetComponentInChildren<TableView>();
_songListTableView.ReloadData();
int row = songListViewController.GetComponentInChildren<StandardLevelListTableView>().RowNumberForLevelID(nextLevelId);
_songListTableView.SelectRow(row, true);
_songListTableView.ScrollToRow(row, true);
}
_confirmDeleteState = Prompt.NotSelected;
_deleting = false;
}
else
{
yield return null;
}
}
IEnumerator PromptDeleteFolder(string dirName)
{
TextMeshProUGUI _deleteText = BeatSaberUI.CreateText(_songDetailViewController.rectTransform, String.Format("Delete folder \"{0}\"?", dirName.Substring(dirName.LastIndexOf('/')).Trim('/')), new Vector2(18f, -64f));
Button _confirmDelete = BeatSaberUI.CreateUIButton(_songDetailViewController.rectTransform, "ApplyButton");
BeatSaberUI.SetButtonText(_confirmDelete, "Yes");
(_confirmDelete.transform as RectTransform).sizeDelta = new Vector2(15f, 10f);
(_confirmDelete.transform as RectTransform).anchoredPosition = new Vector2(-13f, 6f);
_confirmDelete.onClick.AddListener(delegate () { _confirmDeleteState = Prompt.Yes; });
Button _discardDelete = BeatSaberUI.CreateUIButton(_songDetailViewController.rectTransform, "ApplyButton");
BeatSaberUI.SetButtonText(_discardDelete, "No");
(_discardDelete.transform as RectTransform).sizeDelta = new Vector2(15f, 10f);
(_discardDelete.transform as RectTransform).anchoredPosition = new Vector2(2f, 6f);
_discardDelete.onClick.AddListener(delegate () { _confirmDeleteState = Prompt.No; });
(_playButton.transform as RectTransform).anchoredPosition = new Vector2(2f, -10f);
yield return new WaitUntil(delegate () { return (_confirmDeleteState == Prompt.Yes || _confirmDeleteState == Prompt.No); });
(_playButton.transform as RectTransform).anchoredPosition = new Vector2(2f, 6f);
Destroy(_deleteText.gameObject);
Destroy(_confirmDelete.gameObject);
Destroy(_discardDelete.gameObject);
}
private void CreateBeatSaverButton()
{
Button _beatSaverButton = BeatSaberUI.CreateUIButton(_mainMenuRectTransform, "QuitButton");
try
{
(_beatSaverButton.transform as RectTransform).anchoredPosition = new Vector2(30f, 7f);
(_beatSaverButton.transform as RectTransform).sizeDelta = new Vector2(28f, 10f);
BeatSaberUI.SetButtonText(_beatSaverButton, "BeatSaver");
//BeatSaberUI.SetButtonIcon(_beatSaverButton, BeatSaberUI.icons.First(x => x.name == "SettingsIcon"));
_beatSaverButton.onClick.AddListener(delegate () {
try
{
if (_beatSaverViewController == null)
{
_beatSaverViewController = BeatSaberUI.CreateViewController<BeatSaverMasterViewController>();
}
_mainMenuViewController.PresentModalViewController(_beatSaverViewController, null, false);
}
catch (Exception e)
{
log.Exception("EXCETPION IN BUTTON: " + e.Message);
}
});
}
catch (Exception e)
{
log.Exception("EXCEPTION: " + e.Message);
}
}
public static bool CreateMD5FromFile(string path, out string hash)
{
hash = "";
if (!File.Exists(path)) return false;
using (MD5 md5 = MD5.Create())
{
using (var stream = File.OpenRead(path))
{
byte[] hashBytes = md5.ComputeHash(stream);
StringBuilder sb = new StringBuilder();
foreach (byte hashByte in hashBytes)
{
sb.Append(hashByte.ToString("X2"));
}
hash = sb.ToString();
return true;
}
}
}
public static Sprite Base64ToSprite(string base64)
{
Texture2D tex = Base64ToTexture2D(base64);
return Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), (Vector2.one / 2f));
}
public static Texture2D Base64ToTexture2D(string encodedData)
{
byte[] imageData = Convert.FromBase64String(encodedData);
int width, height;
GetImageSize(imageData, out width, out height);
Texture2D texture = new Texture2D(width, height, TextureFormat.ARGB32, false, true);
texture.hideFlags = HideFlags.HideAndDontSave;
texture.filterMode = FilterMode.Trilinear;
texture.LoadImage(imageData);
return texture;
}
private static void GetImageSize(byte[] imageData, out int width, out int height)
{
width = ReadInt(imageData, 3 + 15);
height = ReadInt(imageData, 3 + 15 + 2 + 2);
}
private static int ReadInt(byte[] imageData, int offset)
{
return (imageData[offset] << 8) | imageData[offset + 1];
}
}
}