diff --git a/scripts/Rhythia.cs b/scripts/Rhythia.cs index 15e26357..185e8ea6 100644 --- a/scripts/Rhythia.cs +++ b/scripts/Rhythia.cs @@ -56,6 +56,7 @@ public override async void _Ready() Stats.Instance.GamesOpened++; // Map import + Directory.CreateDirectory($"{Constants.USER_FOLDER}/maps"); var nonConvertedMaps = Directory .EnumerateFiles($"{Constants.USER_FOLDER}/maps", $"*.*", SearchOption.AllDirectories) .Where(f => diff --git a/scripts/SoundManager.cs b/scripts/SoundManager.cs index 969bc506..ce39a631 100644 --- a/scripts/SoundManager.cs +++ b/scripts/SoundManager.cs @@ -261,7 +261,7 @@ public static void PlayJukebox(Map map, bool setRichPresence = true) JukeboxIndex = MapManager.Maps.FindIndex(x => x.Id == map.Id); - Song.Stream = util.Audio.LoadFromFile($"{MapUtil.MapsFolder}/{map.Name}/audio.{map.AudioExt}"); + Song.Stream = util.Audio.LoadFromFile($"{map.FolderPath}/audio.{map.AudioExt}"); Song.Play(); Instance.JukeboxPlayed?.Invoke(map); @@ -320,7 +320,7 @@ public static void StartMapSelectionPlayback(Map map, bool setRichPresence = tru } } - Song.Stream = util.Audio.LoadFromFile($"{MapUtil.MapsFolder}/{map.Name}/audio.{map.AudioExt}"); + Song.Stream = util.Audio.LoadFromFile($"{map.FolderPath}/audio.{map.AudioExt}"); Song.Play(0); Instance.JukeboxPlayed?.Invoke(map); diff --git a/scripts/map/Map.cs b/scripts/map/Map.cs index 370c1eeb..bcaf92e3 100644 --- a/scripts/map/Map.cs +++ b/scripts/map/Map.cs @@ -34,7 +34,11 @@ public partial class Map : RefCounted [Ignore] public MapSet MapSet { get; set; } - public string FolderPath { get; set; } = string.Empty; + public string FolderPath + { + get; + set => field = value?.Replace('\\', '/'); + } = string.Empty; public bool Favorite { get; set; } @@ -103,7 +107,7 @@ public Note[] TryParseNotes() { try { - notes = MapParser.DecodePHXMO($"{MapUtil.MapsFolder}/{Name}/objects.phxmo"); + notes = MapParser.DecodePHXMO($"{FolderPath}/objects.phxmo"); return notes; } catch @@ -125,7 +129,7 @@ public void UnloadCover() private Texture2D getCover() { - string path = $"{MapUtil.MapsFolder}/{Name}"; + string path = FolderPath; if (cover == DefaultCover && File.Exists($"{path}/cover.png")) { diff --git a/scripts/map/MapCache.cs b/scripts/map/MapCache.cs index 65375195..24c49b3a 100644 --- a/scripts/map/MapCache.cs +++ b/scripts/map/MapCache.cs @@ -28,17 +28,22 @@ public static void Load(bool fullSync) try { + Directory.CreateDirectory(MapUtil.MapsFolder); + + EnumerationOptions options = new() { RecurseSubdirectories = true, MaxRecursionDepth = 1 }; + // Map files (.phxm, .sspm, etc) go first since they will be encoded to folders after they get parsed in MapParser.cs -fog List mapsList = [ - .. Directory.GetFiles(MapUtil.MapsFolder, $"*.{Constants.DEFAULT_MAP_EXT}", SearchOption.AllDirectories), - .. Directory.GetDirectories(MapUtil.MapsFolder, "*", SearchOption.AllDirectories), + .. Directory.GetFiles(MapUtil.MapsFolder, $"*.{Constants.DEFAULT_MAP_EXT}", options), + .. Directory.GetDirectories(MapUtil.MapsFolder, "*", options).Where(isMapFolder), ]; string[] toParseMaps = [.. mapsList]; if (fullSync) { + removeDuplicateMaps(); syncFiles(toParseMaps); addNonCachedFiles(toParseMaps); @@ -55,12 +60,33 @@ .. Directory.GetDirectories(MapUtil.MapsFolder, "*", SearchOption.AllDirectories OrderAndSetMaps(); } - catch + catch (Exception exception) { + Logger.Error(exception); OrderAndSetMaps(); } } + private static bool isMapFolder(string path) => + File.Exists(Path.Combine(path, "metadata.json")) && File.Exists(Path.Combine(path, "objects.phxmo")); + + private static void removeDuplicateMaps() + { + foreach (var duplicates in FetchAll().GroupBy(x => x.FolderPath).Where(x => x.Count() > 1)) + { + var kept = duplicates.OrderBy(x => x.Id).First(); + + foreach (var duplicate in duplicates.Where(x => x != kept)) + { + kept.Favorite |= duplicate.Favorite; + kept.PlayCount += duplicate.PlayCount; + RemoveMap(duplicate); + } + + UpdateMap(kept); + } + } + private static void syncFiles(string[] toParseMaps) { var maps = FetchAll(); @@ -320,6 +346,12 @@ public static void OrderAndSetMaps() { var maps = FetchAll(); + foreach (var missing in maps.Where(x => !Directory.Exists(x.FolderPath) && !File.Exists(x.FolderPath)).ToList()) + { + RemoveMap(missing); + maps.Remove(missing); + } + // only covers on screen are loaded to prevent excessive VRAM usage if (maps.Count < 1)