From 15dbd6d4fd2e31c5f35206a52628c33e3d7f31b6 Mon Sep 17 00:00:00 2001 From: Deinonychus71 Date: Thu, 22 Jul 2021 02:47:23 -0500 Subject: [PATCH] improve automapping --- Sma5h/Mods/Sma5h.Mods.Music/Sma5hMusic.cs | 67 ++++++----- .../Sma5h.Mods.Music/Sma5hMusicOptions.cs | 2 +- .../Models/AutoMapper/MappingViewModels.cs | 4 +- Sma5hMusic.GUI/Program.cs | 106 +++++++++--------- 4 files changed, 100 insertions(+), 79 deletions(-) diff --git a/Sma5h/Mods/Sma5h.Mods.Music/Sma5hMusic.cs b/Sma5h/Mods/Sma5h.Mods.Music/Sma5hMusic.cs index 36259e3..4af0959 100644 --- a/Sma5h/Mods/Sma5h.Mods.Music/Sma5hMusic.cs +++ b/Sma5h/Mods/Sma5h.Mods.Music/Sma5hMusic.cs @@ -156,14 +156,14 @@ private bool ProcessPlaylistAutoMapping() if (generationMode == Sma5hMusicOptions.PlaylistGeneration.OnlyMissingSongs || generationMode == Sma5hMusicOptions.PlaylistGeneration.AllSongs) { var configIncidence = _config.CurrentValue.Sma5hMusic.PlaylistMapping.AutoMappingIncidence; - var configMapping = _config.CurrentValue.Sma5hMusic.PlaylistMapping.Mapping.ToDictionary(p => p.Key, p => p.Value != null ? p.Value.Split(',', System.StringSplitOptions.RemoveEmptyEntries) : new string[0]); + var configMapping = _config.CurrentValue.Sma5hMusic.PlaylistMapping.AutoMapping.ToDictionary(p => p.Key, p => p.Value != null ? p.Value.Split(',', System.StringSplitOptions.RemoveEmptyEntries) : new string[0]); var playlists = _audioStateService.GetPlaylists().ToDictionary(p => p.Id, p => p); IEnumerable songsToProcess = null; if (generationMode == Sma5hMusicOptions.PlaylistGeneration.AllSongs) { //Select all visible songs + clear all tracks - var allAffectedPlaylists = configMapping.Values.SelectMany(p => p).Distinct().ToHashSet(); + var allAffectedPlaylists = configMapping.Keys.ToHashSet(); playlists.Where(p => allAffectedPlaylists.Contains(p.Key)).ToList().ForEach(p => p.Value.Tracks.Clear()); songsToProcess = _audioStateService.GetBgmDbRootEntries().Where(p => p.TestDispOrder >= 0).OrderBy(p => p.TestDispOrder); } @@ -176,29 +176,43 @@ private bool ProcessPlaylistAutoMapping() //Get series from BGM var gameToSeries = _audioStateService.GetGameTitleEntries().ToDictionary(p => p.UiGameTitleId, p => p.UiSeriesId); - foreach (var configMappingEntry in configMapping) + + //Enumerate playlist + foreach (var configMappingPlaylist in configMapping) { - //Filter songs per series/game - IEnumerable songsToProcessMapping = null; - if (configMappingEntry.Key.StartsWith(MusicConstants.InternalIds.GAME_SERIES_ID_PREFIX)) - { - songsToProcessMapping = songsToProcess.Where(p => p.UiGameTitleId != null && gameToSeries.ContainsKey(p.UiGameTitleId) && configMappingEntry.Key == gameToSeries[p.UiGameTitleId]); - } - else if (configMappingEntry.Key.StartsWith(MusicConstants.InternalIds.GAME_TITLE_ID_PREFIX)) - { - songsToProcessMapping = songsToProcess.Where(p => p.UiGameTitleId != null && configMappingEntry.Key == p.UiGameTitleId); - } - else - { - continue; - } + var mappingPlaylist = configMappingPlaylist.Key; + var bgmplaylist = playlists.ContainsKey(mappingPlaylist) ? playlists[mappingPlaylist] : null; - //Add to playlist - foreach (var mappingPlaylist in configMappingEntry.Value) + if (bgmplaylist != null) { - var bgmplaylist = playlists.ContainsKey(mappingPlaylist) ? playlists[mappingPlaylist] : null; - if (bgmplaylist != null) + var songsToProcessPlaylist = songsToProcess.ToList(); + + //Enumerate mapping + foreach (var configMappingEntry in configMappingPlaylist.Value) { + var configMappingEntryTrim = configMappingEntry.Trim().ToLower(); + + //Filter songs per series/game + IEnumerable songsToProcessMapping = null; + if (configMappingEntryTrim.StartsWith(MusicConstants.InternalIds.GAME_SERIES_ID_PREFIX)) + { + songsToProcessMapping = songsToProcessPlaylist.Where(p => p.UiGameTitleId != null && gameToSeries.ContainsKey(p.UiGameTitleId) && configMappingEntryTrim == gameToSeries[p.UiGameTitleId]); + } + else if (configMappingEntryTrim.StartsWith(MusicConstants.InternalIds.GAME_TITLE_ID_PREFIX)) + { + songsToProcessMapping = songsToProcessPlaylist.Where(p => p.UiGameTitleId != null && configMappingEntryTrim == p.UiGameTitleId); + } + else if (configMappingEntryTrim.StartsWith(MusicConstants.InternalIds.UI_BGM_ID_PREFIX)) + { + songsToProcessMapping = songsToProcessPlaylist.Where(p => p.UiBgmId != null && configMappingEntryTrim == p.UiBgmId); + } + else + { + _logger.LogWarning($"Playlist Auto-Mapping: Mapping Entry {configMappingEntryTrim} is invalid. Skipping..."); + continue; + } + + //Add to playlist foreach (var songToProcessMapping in songsToProcessMapping) { songToProcessMapping.IsSelectableOriginal = true; @@ -240,11 +254,14 @@ private bool ProcessPlaylistAutoMapping() }); _logger.LogInformation("Playlist Auto-Mapping: Added BGM {UiBgmId} to Playlist {BgmPlaylist}.", songToProcessMapping.UiBgmId, mappingPlaylist); } + + //Remove processed songs from this playlist + songsToProcessPlaylist.RemoveAll(p => songsToProcessMapping.Contains(p)); } - else - { - _logger.LogInformation("Playlist {PlaylistId} wasn't found. Skipping...", bgmplaylist); - } + } + else + { + _logger.LogInformation("Playlist Auto-Mapping: Playlist {PlaylistId} wasn't found. Skipping...", bgmplaylist); } } diff --git a/Sma5h/Mods/Sma5h.Mods.Music/Sma5hMusicOptions.cs b/Sma5h/Mods/Sma5h.Mods.Music/Sma5hMusicOptions.cs index b063b2e..ffbaf70 100644 --- a/Sma5h/Mods/Sma5h.Mods.Music/Sma5hMusicOptions.cs +++ b/Sma5h/Mods/Sma5h.Mods.Music/Sma5hMusicOptions.cs @@ -24,7 +24,7 @@ public class Sma5hMusicOptionsAutoPlaylistsSection [JsonConverter(typeof(StringEnumConverter))] public PlaylistGeneration GenerationMode { get; set; } public ushort AutoMappingIncidence { get; set; } - public Dictionary Mapping { get; set; } + public Dictionary AutoMapping { get; set; } } public enum PlaylistGeneration diff --git a/Sma5hMusic.GUI/Models/AutoMapper/MappingViewModels.cs b/Sma5hMusic.GUI/Models/AutoMapper/MappingViewModels.cs index 781d187..774c1d5 100644 --- a/Sma5hMusic.GUI/Models/AutoMapper/MappingViewModels.cs +++ b/Sma5hMusic.GUI/Models/AutoMapper/MappingViewModels.cs @@ -28,7 +28,7 @@ public MappingViewModels() { GenerationMode = (Sma5hMusicOptions.PlaylistGeneration)p.PlaylistGenerationMode, AutoMappingIncidence = p.PlaylistAutoMappingIncidence, - Mapping = p.PlaylistAutoMapping.ToDictionary(k => k.Key, p => string.Join(",", p.Value)) + AutoMapping = p.PlaylistAutoMapping.ToDictionary(k => k.Key, p => string.Join(",", p.Value)) } })) .ForMember(i => i.Sma5hMusicGUI, me => me.MapFrom(p => new ApplicationSettings.Sma5hMusicGuiOptionsSection() @@ -66,7 +66,7 @@ public MappingViewModels() .ForMember(i => i.EnableAudioCaching, me => me.MapFrom(p => p.Sma5hMusic.EnableAudioCaching)) .ForMember(i => i.PlaylistGenerationMode, me => me.MapFrom(p => p.Sma5hMusic.PlaylistMapping.GenerationMode)) .ForMember(i => i.PlaylistAutoMappingIncidence, me => me.MapFrom(p => p.Sma5hMusic.PlaylistMapping.AutoMappingIncidence)) - .ForMember(i => i.PlaylistAutoMapping, me => me.MapFrom(p => p.Sma5hMusic.PlaylistMapping.Mapping.ToDictionary(k => k.Key, p => p.Value.Split(',', StringSplitOptions.RemoveEmptyEntries)))) + .ForMember(i => i.PlaylistAutoMapping, me => me.MapFrom(p => p.Sma5hMusic.PlaylistMapping.AutoMapping.ToDictionary(k => k.Key, p => p.Value.Split(',', StringSplitOptions.RemoveEmptyEntries)))) .ForMember(i => i.ModPath, me => me.MapFrom(p => p.Sma5hMusic.ModPath)) .ForMember(i => i.UIScale, me => me.MapFrom(p => p.Sma5hMusicGUI.UIScale)) .ForMember(i => i.UITheme, me => me.MapFrom(p => p.Sma5hMusicGUI.UITheme)) diff --git a/Sma5hMusic.GUI/Program.cs b/Sma5hMusic.GUI/Program.cs index 2635aa4..697b2ba 100644 --- a/Sma5hMusic.GUI/Program.cs +++ b/Sma5hMusic.GUI/Program.cs @@ -45,11 +45,14 @@ public static void ConfigureServices() { var services = new ServiceCollection(); - var configuration = new ConfigurationBuilder() + var configurationBuilder = new ConfigurationBuilder() .SetBasePath(GetBasePath()) .AddInMemoryCollection(GetDefaultConfiguration()) - .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) - .Build(); + .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); + if (!configurationBuilder.Build().GetSection("Sma5hMusic:PlaylistMapping:AutoMapping").Exists()) + configurationBuilder.AddInMemoryCollection(GetPlaylistMappingConfig()); + + var configuration = configurationBuilder.Build(); Configuration = configuration; var loggerFactory = LoggerFactory.Create(builder => builder @@ -143,60 +146,61 @@ private static Dictionary GetDefaultConfiguration() { "Sma5hMusicOverride:ModPath", $"Mods{Path.DirectorySeparatorChar}MusicOverride" }, }; - var playlistMapping = GetPlaylistMappingConfig(); - - foreach (var playlistConfig in playlistMapping) - defaultConfig.Add($"Sma5hMusic:PlaylistMapping:Mapping:{playlistConfig.Key}", string.Join(",", playlistConfig.Value)); - return defaultConfig; } - private static Dictionary GetPlaylistMappingConfig() + private static Dictionary GetPlaylistMappingConfig() { - return new Dictionary() + var playlistMapping = new Dictionary() { - { "ui_series_none", new string[] { } }, - { "ui_series_mario", new string[] { "bgmmario" } }, - { "ui_series_mariokart", new string[] { "bgmmkart" } }, - { "ui_series_wreckingcrew", new string[] { "bgmother" } }, - { "ui_series_etc", new string[] { "bgmother" } }, - { "ui_series_donkeykong", new string[] { "bgmdk" } }, - { "ui_series_zelda", new string[] { "bgmzelda" } }, - { "ui_series_metroid", new string[] { "bgmmetroid" } }, - { "ui_series_yoshi", new string[] { "bgmyoshi" } }, - { "ui_series_kirby", new string[] { "bgmkirby" } }, - { "ui_series_starfox", new string[] { "bgmfox" } }, - { "ui_series_pokemon", new string[] { "bgmpokemon" } }, - { "ui_series_fzero", new string[] { "bgmfzero" } }, - { "ui_series_mother", new string[] { "bgmmother" } }, - { "ui_series_fireemblem", new string[] { "bgmfe", "bgmmaster" } }, - { "ui_series_gamewatch", new string[] { "bgmgamewatch" } }, - { "ui_series_palutena", new string[] { "bgmicaros" } }, - { "ui_series_wario", new string[] { "bgmwario" } }, - { "ui_series_pikmin", new string[] { "bgmpikmin" } }, - { "ui_series_famicomrobot", new string[] { "bgmother" } }, - { "ui_series_doubutsu", new string[] { "bgmanimal" } }, - { "ui_series_wiifit", new string[] { "bgmwiifit" } }, - { "ui_series_punchout", new string[] { "bgmpunchout" } }, - { "ui_series_xenoblade", new string[] { "bgmxenoblade", "bgmelement" } }, - { "ui_series_metalgear", new string[] { "bgmmetalgear" } }, - { "ui_series_sonic", new string[] { "bgmsonic" } }, - { "ui_series_rockman", new string[] { "bgmrockman" } }, - { "ui_series_pacman", new string[] { "bgmpacman" } }, - { "ui_series_streetfighter", new string[] { "bgmsf" } }, - { "ui_series_finalfantasy", new string[] { "bgmff", "bgmedge" } }, - { "ui_series_bayonetta", new string[] { "bgmbeyo" } }, - { "ui_series_splatoon", new string[] { "bgmspla" } }, - { "ui_series_castlevania", new string[] { "bgmdracula" } }, - { "ui_series_smashbros", new string[] { "bgmsmashbtl" } }, - { "ui_series_arms", new string[] { "bgmtantan" } }, - { "ui_series_persona", new string[] { "bgmjack" } }, - { "ui_series_dragonquest", new string[] { "bgmbrave" } }, - { "ui_series_banjokazooie", new string[] { "bgmbuddy" } }, - { "ui_series_fatalfury", new string[] { "bgmdolly" } }, - { "ui_series_minecraft", new string[] { "bgmpickel" } }, - { "ui_series_tekken", new string[] { "bgmdemon" } } + { "bgmsmashbtl", new string[] { "ui_series_smashbros" } }, + { "bgmmario", new string[] { "ui_series_mario" } }, + { "bgmmkart", new string[] { "ui_series_mariokart" } }, + { "bgmdk", new string[] { "ui_series_donkeykong" } }, + { "bgmzelda", new string[] { "ui_series_zelda" } }, + { "bgmmetroid", new string[] { "ui_series_metroid" } }, + { "bgmyoshi", new string[] { "ui_series_yoshi" } }, + { "bgmkirby", new string[] { "ui_series_kirby" } }, + { "bgmfox", new string[] { "ui_series_starfox" } }, + { "bgmpokemon", new string[] { "ui_series_pokemon" } }, + { "bgmfzero", new string[] { "ui_series_fzero" } }, + { "bgmmother", new string[] { "ui_series_mother", "ui_series_wreckingcrew", "ui_series_famicomrobot" } }, + { "bgmfe", new string[] { "ui_series_fireemblem" } }, + { "bgmmaster", new string[] { "ui_gametitle_fire_emblem_three_houses_en", "ui_gametitle_fire_emblem_three_houses_jp", "ui_series_fireemblem" } }, + { "bgmgamewatch", new string[] { "ui_series_gamewatch" } }, + { "bgmicaros", new string[] { "ui_series_palutena" } }, + { "bgmwario", new string[] { "ui_series_wario" } }, + { "bgmpikmin", new string[] { "ui_series_pikmin" } }, + { "bgmanimal", new string[] { "ui_series_doubutsu" } }, + { "bgmwiifit", new string[] { "ui_series_wiifit" } }, + { "bgmpunchout", new string[] { "ui_series_punchout" } }, + { "bgmxenoblade", new string[] { "ui_series_xenoblade" } }, + { "bgmelement", new string[] { "ui_gametitle_xenoblade2", "ui_series_xenoblade" } }, + { "bgmmetalgear", new string[] { "ui_series_metalgear" } }, + { "bgmsonic", new string[] { "ui_series_sonic" } }, + { "bgmrockman", new string[] { "ui_series_rockman" } }, + { "bgmpacman", new string[] { "ui_series_pacman" } }, + { "bgmsf", new string[] { "ui_series_streetfighter" } }, + { "bgmff", new string[] { "ui_series_finalfantasy" } }, + { "bgmedge", new string[] { "ui_series_finalfantasy" } }, + { "bgmbeyo", new string[] { "ui_series_bayonetta" } }, + { "bgmspla", new string[] { "ui_series_splatoon" } }, + { "bgmdracula", new string[] { "ui_series_castlevania" } }, + { "bgmjack", new string[] { "ui_series_persona" } }, + { "bgmbrave", new string[] { "ui_series_dragonquest" } }, + { "bgmbuddy", new string[] { "ui_series_banjokazooie" } }, + { "bgmdolly", new string[] { "ui_series_fatalfury" } }, + { "bgmtantan", new string[] { "ui_series_arms" } }, + { "bgmpickel", new string[] { "ui_series_minecraft" } }, + { "bgmdemon", new string[] { "ui_series_tekken" } }, + { "bgmother", new string[] { "ui_series_etc" } } }; + + var output = new Dictionary(); + foreach (var playlistConfig in playlistMapping) + output.Add($"Sma5hMusic:PlaylistMapping:AutoMapping:{playlistConfig.Key}", string.Join(",", playlistConfig.Value)); + + return output; } } } \ No newline at end of file