SongBrowserModel.cs 6.6 KB

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