Prechádzať zdrojové kódy

Add CustomDetailUrl and CustomArchiveUrl.
CustomDetailUrl expects an end-point similar to BeatSaver.com.
CustomArchiveUrl expects a URL with a wildcard to be replaced by the song
key, can point directly to a song archive.
Bump Version.
Code Cleanup, refactor.

Stephen Damm 6 rokov pred
rodič
commit
68270eb99c

+ 8 - 5
SongBrowserPlugin/DataAccess/Playlist.cs

@@ -8,11 +8,14 @@ namespace SongBrowserPlugin.DataAccess
 {
     public class Playlist
     {
-        public String playlistTitle { get; set; }
-        public String playlistAuthor { get; set; }
-        public string image { get; set; }
-        public List<PlaylistSong> songs { get; set; }
+        public String Title { get; set; }
+        public String Author { get; set; }
+        public string Image { get; set; }
+        public List<PlaylistSong> Songs { get; set; }
 
-        public String playlistPath;
+        public String Path;
+
+        public string CustomDetailUrl { get; set; }
+        public string CustomArchiveUrl { get; set; }
     }
 }

+ 22 - 10
SongBrowserPlugin/DataAccess/PlaylistReader.cs

@@ -83,21 +83,33 @@ namespace SongBrowserPlugin.DataAccess
 
                 JSONNode playlistNode = JSON.Parse(json);
 
-                playlist.image = playlistNode["image"];
-                playlist.playlistTitle = playlistNode["playlistTitle"];
-                playlist.playlistAuthor = playlistNode["playlistAuthor"];
-                playlist.songs = new List<PlaylistSong>();
-
+                playlist.Image = playlistNode["image"];
+                playlist.Title = playlistNode["playlistTitle"];
+                playlist.Author = playlistNode["playlistAuthor"];
+                playlist.Songs = new List<PlaylistSong>();
+                playlist.CustomDetailUrl = playlistNode["customDetailUrl"];
+                playlist.CustomArchiveUrl = playlistNode["customArchiveUrl"];
+                if (!string.IsNullOrEmpty(playlist.CustomDetailUrl))
+                {
+                    if (!playlist.CustomDetailUrl.EndsWith("/"))
+                    {
+                        playlist.CustomDetailUrl += "/";
+                    }
+                    _log.Debug("Found playlist with custom URL! Name: " + playlist.Title + ", CustomDetailURL: " + playlist.CustomDetailUrl);
+                }
                 foreach (JSONNode node in playlistNode["songs"].AsArray)
                 {
-                    PlaylistSong song = new PlaylistSong();
-                    song.Key = node["key"];
-                    song.SongName = node["songName"];
+                    PlaylistSong song = new PlaylistSong
+                    {
+                        Key = node["key"],
+                        SongName = node["songName"],
+                        LevelId = node["levelId"]
+                    };
 
-                    playlist.songs.Add(song);
+                    playlist.Songs.Add(song);
                 }
 
-                playlist.playlistPath = path;
+                playlist.Path = path;
                 return playlist;
             }
             catch (Exception e)

+ 1 - 0
SongBrowserPlugin/DataAccess/PlaylistSong.cs

@@ -10,6 +10,7 @@ namespace SongBrowserPlugin.DataAccess
     {
         public String Key { get; set; }
         public String SongName { get; set; }
+        public string LevelId { get; set; }
 
         // Set by playlist downloading
         [NonSerialized]

+ 3 - 3
SongBrowserPlugin/DataAccess/SongBrowserModel.cs

@@ -158,7 +158,7 @@ namespace SongBrowserPlugin
 
             set
             {
-                _settings.currentPlaylistFile = value.playlistPath;
+                _settings.currentPlaylistFile = value.Path;
                 _currentPlaylist = value;
             }
         }
@@ -716,8 +716,8 @@ namespace SongBrowserPlugin
                 return;
             }
 
-            _log.Debug("Filtering songs for playlist: {0}", this.CurrentPlaylist.playlistTitle);            
-            List<String> playlistKeysOrdered = this.CurrentPlaylist.songs.Select(x => x.Key).Distinct().ToList();
+            _log.Debug("Filtering songs for playlist: {0}", this.CurrentPlaylist.Title);            
+            List<String> playlistKeysOrdered = this.CurrentPlaylist.Songs.Select(x => x.Key).Distinct().ToList();
             Dictionary<String, int>playlistKeyToIndex = playlistKeysOrdered.Select((val, index) => new { Index = index, Value = val }).ToDictionary(i => i.Value, i => i.Index);
             LevelCollectionsForGameplayModes levelCollections = Resources.FindObjectsOfTypeAll<LevelCollectionsForGameplayModes>().FirstOrDefault();
             var levels = levelCollections.GetLevels(_currentGamePlayMode);

+ 1 - 1
SongBrowserPlugin/Plugin.cs

@@ -13,7 +13,7 @@ namespace SongBrowserPlugin
 
         public string Version
         {
-            get { return "v2.3.1"; }
+            get { return "v2.3.2"; }
         }
 
         public void OnApplicationStart()

+ 1 - 1
SongBrowserPlugin/UI/Browser/SongBrowserUI.cs

@@ -692,7 +692,7 @@ namespace SongBrowserPlugin.UI
 
             if (p != null)
             {
-                _log.Debug("Showing songs for playlist: {0}", p.playlistTitle);
+                _log.Debug("Showing songs for playlist: {0}", p.Title);
                 _model.Settings.filterMode = SongFilterMode.Playlist;
                 _model.CurrentPlaylist = p;
                 _model.Settings.Save();

+ 6 - 6
SongBrowserPlugin/UI/Playlists/PlaylistDetailViewController.cs

@@ -47,21 +47,21 @@ namespace SongBrowserPlugin.UI
         public void Init(Playlist playlist, int missingCount)
         {
             _playlistTitleText = UIBuilder.CreateText(this.transform as RectTransform,
-                playlist.playlistTitle,
+                playlist.Title,
                 new Vector2(0, -20),
                 new Vector2(60f, 10f)
             );
             _playlistTitleText.alignment = TextAlignmentOptions.Center;
 
             _playlistAuthorText = UIBuilder.CreateText(this.transform as RectTransform,
-                playlist.playlistAuthor,
+                playlist.Author,
                 new Vector2(0, -30),
                 new Vector2(60f, 10f)
             );
             _playlistAuthorText.alignment = TextAlignmentOptions.Center;
 
             _playlistNumberOfSongs = UIBuilder.CreateText(this.transform as RectTransform,
-                playlist.songs.Count.ToString(),
+                playlist.Songs.Count.ToString(),
                 new Vector2(0, -40),
                 new Vector2(60f, 10f)
             );
@@ -95,9 +95,9 @@ namespace SongBrowserPlugin.UI
         public virtual void SetContent(Playlist p, int missingCount)
         {
             _selectedPlaylist = p;
-            _playlistTitleText.text = _selectedPlaylist.playlistTitle;
-            _playlistAuthorText.text = _selectedPlaylist.playlistAuthor;
-            _playlistNumberOfSongs.text = "Song Count: " + _selectedPlaylist.songs.Count.ToString();
+            _playlistTitleText.text = _selectedPlaylist.Title;
+            _playlistAuthorText.text = _selectedPlaylist.Author;
+            _playlistNumberOfSongs.text = "Song Count: " + _selectedPlaylist.Songs.Count.ToString();
             _playlistMissingSongCount.text = "Missing Count: " + missingCount;
         }
 

+ 37 - 15
SongBrowserPlugin/UI/Playlists/PlaylistFlowCoordinator.cs

@@ -19,8 +19,7 @@ namespace SongBrowserPlugin.UI
     {
         public static string beatsaverURL = "https://beatsaver.com";
 
-        public const String Name = "PlaylistFlowCoordinator";
-        private Logger _log = new Logger(Name);
+        private Logger _log = new Logger("PlaylistFlowCoordinator");
 
         private PlaylistSelectionNavigationController _playlistNavigationController;
         private PlaylistSelectionListViewController _playlistListViewController;
@@ -95,7 +94,7 @@ namespace SongBrowserPlugin.UI
         /// <param name="songListViewController"></param>
         public virtual void HandlePlaylistListDidSelectPlaylist(PlaylistSelectionListViewController playlistListViewController)
         {
-            _log.Debug("Selected Playlist: {0}", playlistListViewController.SelectedPlaylist.playlistTitle);
+            _log.Debug("Selected Playlist: {0}", playlistListViewController.SelectedPlaylist.Title);
 
             int missingCount = CountMissingSongs(playlistListViewController.SelectedPlaylist);
 
@@ -164,13 +163,13 @@ namespace SongBrowserPlugin.UI
         /// <returns></returns>
         private void FilterSongsForPlaylist(Playlist playlist)
         {
-            if (!playlist.songs.All(x => x.Level != null))
+            if (!playlist.Songs.All(x => x.Level != null))
             {
-                playlist.songs.ForEach(x =>
+                playlist.Songs.ForEach(x =>
                 {
                     if (x.Level == null)
                     {
-                        x.Level = SongLoader.CustomLevels.FirstOrDefault(y => y.customSongInfo.path.Contains(x.Key) && Directory.Exists(y.customSongInfo.path));
+                        x.Level = SongLoader.CustomLevels.FirstOrDefault(y => (y.customSongInfo.path.Contains(x.Key) && Directory.Exists(y.customSongInfo.path)) || (string.IsNullOrEmpty(x.LevelId) ? false : y.levelID.StartsWith(x.LevelId)));
                     }
                 });
             }
@@ -183,7 +182,7 @@ namespace SongBrowserPlugin.UI
         /// <returns></returns>
         private int CountMissingSongs(Playlist playlist)
         {
-            return playlist.songs.Count - playlist.songs.Count(x => SongLoader.CustomLevels.Any(y => y.customSongInfo.path.Contains(x.Key)));
+            return playlist.Songs.Count - playlist.Songs.Count(x => SongLoader.CustomLevels.Any(y => y.customSongInfo.path.Contains(x.Key)));
         }
 
         /// <summary>
@@ -208,9 +207,8 @@ namespace SongBrowserPlugin.UI
         /// <returns></returns>
         public IEnumerator DownloadPlaylist(Playlist playlist)
         {
-            Playlist selectedPlaylist = this._playlistListViewController.SelectedPlaylist;
-            this.FilterSongsForPlaylist(selectedPlaylist);
-            List<PlaylistSong> playlistSongsToDownload = selectedPlaylist.songs.Where(x => x.Level == null).ToList();
+            this.FilterSongsForPlaylist(playlist);
+            List<PlaylistSong> playlistSongsToDownload = playlist.Songs.Where(x => x.Level == null).ToList();
 
             List<Song> beatSaverSongs = new List<Song>();
 
@@ -220,10 +218,28 @@ namespace SongBrowserPlugin.UI
 
             foreach (var item in playlistSongsToDownload)
             {
-                _log.Debug("Obtaining hash and url for " + item.Key + ": " + item.SongName);
-                yield return GetSongByPlaylistSong(item);
+                if (String.IsNullOrEmpty(playlist.CustomArchiveUrl))
+                {
+                    _log.Debug("Obtaining hash and url for " + item.Key + ": " + item.SongName);
+                    yield return GetSongByPlaylistSong(playlist, item);
+
+                    _log.Debug("Song is null: " + (_lastRequestedSong == null) + "\n Level is downloaded: " + (SongLoader.CustomLevels.Any(x => x.levelID.Substring(0, 32) == _lastRequestedSong.hash.ToUpper())));
+                }
+                else
+                {
+                    // stamp archive url
+                    String archiveUrl = playlist.CustomArchiveUrl.Replace("[KEY]", item.Key);
 
-                _log.Debug("Song is null: " + (_lastRequestedSong == null) + "\n Level is downloaded: " + (SongLoader.CustomLevels.Any(x => x.levelID.Substring(0, 32) == _lastRequestedSong.hash.ToUpper())));
+                    // Create fake song with what we know...
+                    // TODO - update this if we ever know more...
+                    _lastRequestedSong = new Song()
+                    {
+                        songName = item.SongName,
+                        downloadingProgress = 0f,
+                        hash = "",
+                        downloadUrl = archiveUrl
+                    };
+                }
 
                 if (_lastRequestedSong != null && !SongLoader.CustomLevels.Any(x => x.levelID.Substring(0, 32) == _lastRequestedSong.hash.ToUpper()))
                 {
@@ -270,12 +286,18 @@ namespace SongBrowserPlugin.UI
         /// </summary>
         /// <param name="song"></param>
         /// <returns></returns>
-        public IEnumerator GetSongByPlaylistSong(PlaylistSong song)
+        public IEnumerator GetSongByPlaylistSong(Playlist playlist, PlaylistSong song)
         {
             UnityWebRequest wwwId = null;
             try
             {
                 wwwId = UnityWebRequest.Get($"{PlaylistFlowCoordinator.beatsaverURL}/api/songs/detail/" + song.Key);
+                string url = PlaylistFlowCoordinator.beatsaverURL + $"/api/songs/detail/" + song.Key;
+                if (!string.IsNullOrEmpty(playlist.CustomDetailUrl))
+                {
+                    url = playlist.CustomDetailUrl + song.Key;
+                }
+                wwwId = UnityWebRequest.Get(url);
                 wwwId.timeout = 10;
             }
             catch
@@ -290,7 +312,7 @@ namespace SongBrowserPlugin.UI
             if (wwwId.isNetworkError || wwwId.isHttpError)
             {
                 _log.Error(wwwId.error);
-                _log.Error($"Song {song.SongName} doesn't exist on BeatSaver!");
+                _log.Error($"Song {song.Key}({song.SongName}) doesn't exist!");
                 _lastRequestedSong = new Song() { songName = song.SongName, songQueueState = SongQueueState.Error, downloadingProgress = 1f, hash = "" };
             }
             else

+ 3 - 3
SongBrowserPlugin/UI/Playlists/PlaylistTableView.cs

@@ -159,9 +159,9 @@ namespace SongBrowserPlugin.UI
             {
                 Playlist p = _reader.Playlists[row];
                 StandardLevelListTableCell tableCell = Instantiate(_cellInstanceTemplate, this._tableView.transform, false);
-                tableCell.coverImage = Base64Sprites.Base64ToSprite(p.image);
-                tableCell.songName = p.playlistTitle;
-                tableCell.author = p.playlistAuthor;
+                tableCell.coverImage = Base64Sprites.Base64ToSprite(p.Image);
+                tableCell.songName = p.Title;
+                tableCell.author = p.Author;
 
                 return tableCell;
             }