Skip to content

Commit

Permalink
improve automapping
Browse files Browse the repository at this point in the history
  • Loading branch information
Deinonychus71 committed Jul 22, 2021
1 parent aa1186c commit 15dbd6d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 79 deletions.
67 changes: 42 additions & 25 deletions Sma5h/Mods/Sma5h.Mods.Music/Sma5hMusic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<BgmDbRootEntry> 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);
}
Expand All @@ -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<BgmDbRootEntry> 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<BgmDbRootEntry> 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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sma5h/Mods/Sma5h.Mods.Music/Sma5hMusicOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Sma5hMusicOptionsAutoPlaylistsSection
[JsonConverter(typeof(StringEnumConverter))]
public PlaylistGeneration GenerationMode { get; set; }
public ushort AutoMappingIncidence { get; set; }
public Dictionary<string, string> Mapping { get; set; }
public Dictionary<string, string> AutoMapping { get; set; }
}

public enum PlaylistGeneration
Expand Down
4 changes: 2 additions & 2 deletions Sma5hMusic.GUI/Models/AutoMapper/MappingViewModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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))
Expand Down
106 changes: 55 additions & 51 deletions Sma5hMusic.GUI/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -143,60 +146,61 @@ private static Dictionary<string, string> 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<string, string[]> GetPlaylistMappingConfig()
private static Dictionary<string, string> GetPlaylistMappingConfig()
{
return new Dictionary<string, string[]>()
var playlistMapping = new Dictionary<string, string[]>()
{
{ "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<string, string>();
foreach (var playlistConfig in playlistMapping)
output.Add($"Sma5hMusic:PlaylistMapping:AutoMapping:{playlistConfig.Key}", string.Join(",", playlistConfig.Value));

return output;
}
}
}

0 comments on commit 15dbd6d

Please sign in to comment.