SongBrowserModel.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. using HMUI;
  2. using SongBrowserPlugin.DataAccess;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using UnityEngine;
  10. namespace SongBrowserPlugin
  11. {
  12. public class SongBrowserModel
  13. {
  14. private Logger _log = new Logger("SongBrowserModel");
  15. private List<SongLoaderPlugin.CustomSongInfo> _customSongInfos;
  16. private Dictionary<String, SongLoaderPlugin.CustomSongInfo> _levelIdToCustomSongInfo;
  17. private SongBrowserSettings _settings;
  18. //private SongSelectionMasterViewController _songSelectionMasterView;
  19. //private SongListViewController _songListViewController;
  20. private IBeatSaberSongList _beatSaberSongAccessor;
  21. private List<LevelStaticData> _sortedSongs;
  22. private List<LevelStaticData> _originalSongs;
  23. private SongSortMode _cachedSortMode = default(SongSortMode);
  24. private DateTime _cachedCustomSongDirLastWriteTIme = DateTime.MinValue;
  25. public SongBrowserSettings Settings
  26. {
  27. get
  28. {
  29. return _settings;
  30. }
  31. }
  32. public List<LevelStaticData> SortedSongList
  33. {
  34. get
  35. {
  36. return _sortedSongs;
  37. }
  38. }
  39. /// <summary>
  40. /// Constructor.
  41. /// </summary>
  42. public SongBrowserModel()
  43. {
  44. }
  45. /// <summary>
  46. /// Init this model.
  47. /// </summary>
  48. /// <param name="songSelectionMasterView"></param>
  49. /// <param name="songListViewController"></param>
  50. public void Init(IBeatSaberSongList beatSaberSongAccessor /*SongSelectionMasterViewController songSelectionMasterView, SongListViewController songListViewController*/)
  51. {
  52. _beatSaberSongAccessor = beatSaberSongAccessor;
  53. _settings = SongBrowserSettings.Load();
  54. //_songSelectionMasterView = songSelectionMasterView;
  55. //_songListViewController = songListViewController;
  56. }
  57. /// <summary>
  58. /// Get the song cache from the game.
  59. /// </summary>
  60. public void UpdateSongLists(bool updateSongInfos)
  61. {
  62. String customSongsPath = Path.Combine(Environment.CurrentDirectory, "CustomSongs");
  63. DateTime currentLastWriteTIme = File.GetLastWriteTimeUtc(customSongsPath);
  64. if (_cachedCustomSongDirLastWriteTIme == null || DateTime.Compare(currentLastWriteTIme, _cachedCustomSongDirLastWriteTIme) != 0)
  65. {
  66. _log.Debug("Custom Song directory has changed. Fetching new songs. Sorting song list.");
  67. _cachedCustomSongDirLastWriteTIme = currentLastWriteTIme;
  68. _cachedSortMode = _settings.sortMode;
  69. _originalSongs = this._beatSaberSongAccessor.AcquireSongList();
  70. if (updateSongInfos)
  71. {
  72. this.UpdateSongInfos();
  73. }
  74. this.ProcessSongList();
  75. }
  76. else if (_settings.sortMode != _cachedSortMode)
  77. {
  78. _log.Debug("Sort mode has changed. Sorting song list.");
  79. _cachedSortMode = _settings.sortMode;
  80. this.ProcessSongList();
  81. }
  82. else
  83. {
  84. _log.Debug("Songs List and/or sort mode has not changed.");
  85. }
  86. }
  87. /// <summary>
  88. /// Get the song infos from SongLoaderPluging
  89. /// </summary>
  90. private void UpdateSongInfos()
  91. {
  92. _customSongInfos = ReflectionUtil.InvokeMethod<SongLoaderPlugin.SongLoader>(SongLoaderPlugin.SongLoader.Instance, "RetrieveAllSongs", null) as List<SongLoaderPlugin.CustomSongInfo>;
  93. _levelIdToCustomSongInfo = _customSongInfos.ToDictionary(x => x.GetIdentifier(), x => x);
  94. }
  95. /// <summary>
  96. /// Sort the song list based on the settings.
  97. /// </summary>
  98. private void ProcessSongList()
  99. {
  100. _log.Debug("ProcessSongList()");
  101. Stopwatch stopwatch = Stopwatch.StartNew();
  102. // Weights used for keeping the original songs in order
  103. // Invert the weights from the game so we can order by descending and make LINQ work with us...
  104. /* Level4, Level2, Level9, Level5, Level10, Level6, Level7, Level1, Level3, Level8, */
  105. Dictionary<string, int> weights = new Dictionary<string, int>
  106. {
  107. ["Level4"] = 10,
  108. ["Level2"] = 9,
  109. ["Level9"] = 8,
  110. ["Level5"] = 7,
  111. ["Level10"] = 6,
  112. ["Level6"] = 5,
  113. ["Level7"] = 4,
  114. ["Level1"] = 3,
  115. ["Level3"] = 2,
  116. ["Level8"] = 1
  117. };
  118. switch (_settings.sortMode)
  119. {
  120. case SongSortMode.Favorites:
  121. _log.Debug("Sorting song list as favorites");
  122. _sortedSongs = _originalSongs
  123. .AsQueryable()
  124. .OrderBy(x => _settings.favorites.Contains(x.levelId) == false)
  125. .ThenBy(x => x.songName)
  126. .ThenBy(x => x.authorName)
  127. .ToList();
  128. break;
  129. case SongSortMode.Original:
  130. _log.Debug("Sorting song list as original");
  131. _sortedSongs = _originalSongs
  132. .AsQueryable()
  133. .OrderByDescending(x => weights.ContainsKey(x.levelId) ? weights[x.levelId] : 0)
  134. .ThenBy(x => x.songName)
  135. .ToList();
  136. break;
  137. case SongSortMode.Newest:
  138. _log.Debug("Sorting song list as newest.");
  139. _sortedSongs = _originalSongs
  140. .AsQueryable()
  141. .OrderBy(x => weights.ContainsKey(x.levelId) ? weights[x.levelId] : 0)
  142. .ThenByDescending(x => x.levelId.StartsWith("Level") ? DateTime.MinValue.Millisecond : File.GetLastWriteTimeUtc(_levelIdToCustomSongInfo[x.levelId].path).Millisecond)
  143. .ToList();
  144. break;
  145. case SongSortMode.Default:
  146. default:
  147. _log.Debug("Sorting song list as default");
  148. _sortedSongs = _originalSongs
  149. .AsQueryable()
  150. .OrderBy(x => x.authorName)
  151. .ThenBy(x => x.songName)
  152. .ToList();
  153. break;
  154. }
  155. stopwatch.Stop();
  156. _log.Info("Sorting songs took {0}ms", stopwatch.ElapsedMilliseconds);
  157. this._beatSaberSongAccessor.OverwriteBeatSaberSongList(_sortedSongs);
  158. }
  159. }
  160. }