SongBrowserModel.cs 8.6 KB

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