SongBrowserModel.cs 7.7 KB

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