public static void UpdateCoverImage(BeatSaberPlaylist playlist, OpContext context, AssetsFile songsAssetFile) { CustomLevelLoader loader = new CustomLevelLoader(songsAssetFile, context.Config); if (playlist.CoverImageBytes != null && playlist.CoverImageBytes.Length > 0) { Log.LogMsg($"Loading cover art for playlist ID '{playlist.PlaylistID}'"); var oldCoverImage = playlist?.LevelPackObject?.CoverImage; var oldTex = playlist?.LevelPackObject?.CoverImage?.Object?.RenderData?.Texture; //todo: verify this is a good place to delete stuff try { playlist.CoverArtSprite = loader.LoadPackCover(playlist.PlaylistID, playlist.CoverImageBytes); playlist.LevelPackObject.CoverImage = playlist.CoverArtSprite.PtrFrom(playlist.LevelPackObject); } catch (Exception ex) { Log.LogErr("Exception in step 1!", ex); throw; } try { //don't erase base content from the assets, although this definitely leaks textures if you keep switching the stock level pack picture if (!BSConst.KnownLevelPackIDs.Contains(playlist.PlaylistID)) { if (oldTex != null) { if (oldTex?.Object != null) { songsAssetFile.DeleteObject(oldTex.Object); } oldTex.Dispose(); } if (oldCoverImage != null) { if (oldCoverImage.Object != null) { songsAssetFile.DeleteObject(oldCoverImage.Object); } oldCoverImage.Dispose(); } } } catch (Exception ex) { Log.LogErr("Exception trying to clean up playlist cover art! This may leak cover images!", ex); } } else { try { if (playlist.LevelPackObject.CoverImage != null) { playlist.CoverArtSprite = playlist.LevelPackObject.CoverImage.Object; } else { playlist.CoverArtSprite = loader.LoadPackCover(playlist.PlaylistID, null); } } catch (Exception ex) { Log.LogErr("Exception in the cover art sprite part!", ex); throw; } try { playlist.LevelPackObject.CoverImage = playlist.CoverArtSprite.PtrFrom(playlist.LevelPackObject); } catch (Exception ex) { Log.LogErr("Exception in the final step!", ex); throw; } } //try queueing a file write op to output the playlist cover image try { var tex = playlist.LevelPackObject?.CoverImage?.Object?.RenderData?.Texture?.Object; if (tex == null) { throw new Exception("Texture couldn't be loaded from the playlist even though it should have just been set..."); } var qfo = new QueuedFileOp() { TargetPath = context.Config.PlaylistsPath.CombineFwdSlash(playlist.PlaylistID + ".png"), Type = QueuedFileOperationType.WriteFile, SourceData = Utils.ImageUtils.Instance.TextureToPngBytes(tex) }; context.Engine.QueuedFileOperations.Add(qfo); } catch (Exception ex) { Log.LogErr($"Exception queueing write op for playlist art on {playlist.PlaylistID}!", ex); } }
private void UpdatePlaylistConfig(AssetsManager manager, BeatSaberPlaylist playlist) { Log.LogMsg($"Processing playlist ID {playlist.PlaylistID}..."); var songsAssetFile = manager.GetAssetsFile(BSConst.KnownFiles.SongsAssetsFilename); CustomLevelLoader loader = new CustomLevelLoader(songsAssetFile); BeatmapLevelPackObject levelPack = songsAssetFile.FindAsset <BeatmapLevelPackObject>(x => x.Object.PackID == playlist.PlaylistID)?.Object; //create a new level pack if one waasn't found if (levelPack == null) { Log.LogMsg($"Level pack for playlist '{playlist.PlaylistID}' was not found and will be created"); levelPack = new BeatmapLevelPackObject(songsAssetFile) { Enabled = 1, GameObject = null, IsPackAlwaysOwned = true, PackID = playlist.PlaylistID, Name = playlist.PlaylistID + BSConst.NameSuffixes.LevelPack, PackName = playlist.PlaylistName }; songsAssetFile.AddObject(levelPack, true); var col = new BeatmapLevelCollectionObject(songsAssetFile) { Name = playlist.PlaylistID + BSConst.NameSuffixes.LevelCollection }; songsAssetFile.AddObject(col, true); levelPack.BeatmapLevelCollection = col.PtrFrom(levelPack); } playlist.LevelPackObject = levelPack; levelPack.PackName = playlist.PlaylistName ?? levelPack.PackName; if (playlist.CoverArt != null) { Log.LogMsg($"Loading cover art for playlist ID '{playlist.PlaylistID}'"); playlist.CoverArtSprite = loader.LoadPackCover(playlist.PlaylistID, playlist.CoverArt); playlist.LevelPackObject.CoverImage = playlist.CoverArtSprite.PtrFrom(playlist.LevelPackObject); } else { if (playlist.LevelPackObject.CoverImage != null) { playlist.CoverArtSprite = playlist.LevelPackObject.CoverImage.Object; } else { playlist.CoverArtSprite = loader.LoadPackCover(playlist.PlaylistID, null); } playlist.LevelPackObject.CoverImage = playlist.CoverArtSprite.PtrFrom(playlist.LevelPackObject); } //clear out any levels, we'll add them back var levelCollection = levelPack.BeatmapLevelCollection.Object; levelCollection.BeatmapLevels.ForEach(x => x.Dispose()); levelCollection.BeatmapLevels.Clear(); int songCount = 0; Log.LogMsg($"Processing songs for playlist ID {playlist.PlaylistID}..."); var totalSongs = playlist.SongList.Count(); var songMod = Math.Ceiling((double)totalSongs / (double)10); if (songMod < 1) { songMod = 1; } foreach (var song in playlist.SongList.ToList()) { songCount++; if (songCount % songMod == 0) { Console.WriteLine($"{songCount.ToString().PadLeft(5)} of {totalSongs}..."); } if (UpdateSongConfig(manager, song, loader)) { if (levelCollection.BeatmapLevels.Any(x => x.Object.LevelID == song.LevelData.LevelID)) { Log.LogErr($"Playlist ID '{playlist.PlaylistID}' already contains song ID '{song.SongID}' once, removing the second link"); } else { levelCollection.BeatmapLevels.Add(song.LevelData.PtrFrom(levelCollection)); continue; } } playlist.SongList.Remove(song); } Console.WriteLine($"Proccessed {totalSongs} for playlist ID {playlist.PlaylistID}"); }