SongBrowserModel.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 Dictionary<String, double> _cachedLastWriteTimes;
  18. private SongBrowserSettings _settings;
  19. //private SongSelectionMasterViewController _songSelectionMasterView;
  20. //private SongListViewController _songListViewController;
  21. private IBeatSaberSongList _beatSaberSongAccessor;
  22. private List<LevelStaticData> _sortedSongs;
  23. private List<LevelStaticData> _originalSongs;
  24. private SongSortMode _cachedSortMode = default(SongSortMode);
  25. private DateTime _cachedCustomSongDirLastWriteTIme = DateTime.MinValue;
  26. public SongBrowserSettings Settings
  27. {
  28. get
  29. {
  30. return _settings;
  31. }
  32. }
  33. public List<LevelStaticData> SortedSongList
  34. {
  35. get
  36. {
  37. return _sortedSongs;
  38. }
  39. }
  40. /// <summary>
  41. /// Constructor.
  42. /// </summary>
  43. public SongBrowserModel()
  44. {
  45. _cachedLastWriteTimes = new Dictionary<String, double>();
  46. }
  47. /// <summary>
  48. /// Init this model.
  49. /// </summary>
  50. /// <param name="songSelectionMasterView"></param>
  51. /// <param name="songListViewController"></param>
  52. public void Init(IBeatSaberSongList beatSaberSongAccessor /*SongSelectionMasterViewController songSelectionMasterView, SongListViewController songListViewController*/)
  53. {
  54. _beatSaberSongAccessor = beatSaberSongAccessor;
  55. _settings = SongBrowserSettings.Load();
  56. //_songSelectionMasterView = songSelectionMasterView;
  57. //_songListViewController = songListViewController;
  58. }
  59. /// <summary>
  60. /// Get the song cache from the game.
  61. /// </summary>
  62. public void UpdateSongLists(bool updateSongInfos)
  63. {
  64. String customSongsPath = Path.Combine(Environment.CurrentDirectory, "CustomSongs");
  65. DateTime currentLastWriteTIme = File.GetLastWriteTimeUtc(customSongsPath);
  66. if (_cachedCustomSongDirLastWriteTIme == null || DateTime.Compare(currentLastWriteTIme, _cachedCustomSongDirLastWriteTIme) != 0)
  67. {
  68. _log.Debug("Custom Song directory has changed. Fetching new songs. Sorting song list.");
  69. // Get LastWriteTimes
  70. var Epoch = new DateTime(1970, 1, 1);
  71. string[] directories = Directory.GetDirectories(customSongsPath);
  72. //_log.Debug("Directories: " + directories);
  73. foreach (string dir in directories)
  74. {
  75. // Flip slashes, match SongLoaderPlugin
  76. string slashed_dir = dir.Replace("\\", "/");
  77. //_log.Debug("Fetching LastWriteTime for {0}", slashed_dir);
  78. _cachedLastWriteTimes[slashed_dir] = (File.GetLastWriteTimeUtc(dir) - Epoch).TotalMilliseconds;
  79. }
  80. // Update song Infos
  81. if (updateSongInfos)
  82. {
  83. this.UpdateSongInfos();
  84. }
  85. // Get new songs
  86. _cachedCustomSongDirLastWriteTIme = currentLastWriteTIme;
  87. _cachedSortMode = _settings.sortMode;
  88. _originalSongs = this._beatSaberSongAccessor.AcquireSongList();
  89. this.ProcessSongList();
  90. }
  91. else if (_settings.sortMode != _cachedSortMode)
  92. {
  93. _log.Debug("Sort mode has changed. Sorting song list.");
  94. _cachedSortMode = _settings.sortMode;
  95. this.ProcessSongList();
  96. }
  97. else
  98. {
  99. _log.Debug("Songs List and/or sort mode has not changed.");
  100. }
  101. }
  102. /// <summary>
  103. /// Get the song infos from SongLoaderPluging
  104. /// </summary>
  105. private void UpdateSongInfos()
  106. {
  107. _log.Debug("Attempting to fetch song infos from song loader plugin.");
  108. _customSongInfos = SongLoaderPlugin.SongLoader.CustomSongInfos;
  109. _levelIdToCustomSongInfo = _customSongInfos.ToDictionary(x => x.levelId, x => x);
  110. /*_customSongInfos.ForEach(x =>
  111. {
  112. _log.Debug("path={0}", x.levelId);
  113. });*/
  114. }
  115. /// <summary>
  116. /// Sort the song list based on the settings.
  117. /// </summary>
  118. private void ProcessSongList()
  119. {
  120. _log.Debug("ProcessSongList()");
  121. Stopwatch stopwatch = Stopwatch.StartNew();
  122. // Weights used for keeping the original songs in order
  123. // Invert the weights from the game so we can order by descending and make LINQ work with us...
  124. /* Level4, Level2, Level9, Level5, Level10, Level6, Level7, Level1, Level3, Level8, */
  125. Dictionary<string, int> weights = new Dictionary<string, int>
  126. {
  127. ["Level4"] = 10,
  128. ["Level2"] = 9,
  129. ["Level9"] = 8,
  130. ["Level5"] = 7,
  131. ["Level10"] = 6,
  132. ["Level6"] = 5,
  133. ["Level7"] = 4,
  134. ["Level1"] = 3,
  135. ["Level3"] = 2,
  136. ["Level8"] = 1
  137. };
  138. /*_originalSongs.ForEach(x =>
  139. {
  140. if (_levelIdToCustomSongInfo.ContainsKey(x.levelId))
  141. {
  142. _log.Debug("_levelIdToCustomSongInfo.HasKey({0})", x.levelId);
  143. }
  144. else
  145. {
  146. _log.Debug("!_levelIdToCustomSongInfo.HasKey({0})", x.levelId);
  147. }
  148. });*/
  149. switch (_settings.sortMode)
  150. {
  151. case SongSortMode.Favorites:
  152. _log.Debug("Sorting song list as favorites");
  153. _sortedSongs = _originalSongs
  154. .AsQueryable()
  155. .OrderBy(x => _settings.favorites.Contains(x.levelId) == false)
  156. .ThenBy(x => x.songName)
  157. .ThenBy(x => x.authorName)
  158. .ToList();
  159. break;
  160. case SongSortMode.Original:
  161. _log.Debug("Sorting song list as original");
  162. _sortedSongs = _originalSongs
  163. .AsQueryable()
  164. .OrderByDescending(x => weights.ContainsKey(x.levelId) ? weights[x.levelId] : 0)
  165. .ThenBy(x => x.songName)
  166. .ToList();
  167. break;
  168. case SongSortMode.Newest:
  169. _log.Debug("Sorting song list as newest.");
  170. _sortedSongs = _originalSongs
  171. .AsQueryable()
  172. .OrderBy(x => weights.ContainsKey(x.levelId) ? weights[x.levelId] : 0)
  173. .ThenByDescending(x => x.levelId.StartsWith("Level") ? weights[x.levelId] : _cachedLastWriteTimes[_levelIdToCustomSongInfo[x.levelId].path])
  174. .ToList();
  175. break;
  176. case SongSortMode.Default:
  177. default:
  178. _log.Debug("Sorting song list as default");
  179. _sortedSongs = _originalSongs
  180. .AsQueryable()
  181. .OrderBy(x => x.authorName)
  182. .ThenBy(x => x.songName)
  183. .ToList();
  184. break;
  185. }
  186. stopwatch.Stop();
  187. _log.Info("Sorting songs took {0}ms", stopwatch.ElapsedMilliseconds);
  188. this._beatSaberSongAccessor.OverwriteBeatSaberSongList(_sortedSongs);
  189. }
  190. }
  191. }