Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scripts/Rhythia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand Down
4 changes: 2 additions & 2 deletions scripts/SoundManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions scripts/map/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down Expand Up @@ -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
Expand All @@ -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"))
{
Expand Down
38 changes: 35 additions & 3 deletions scripts/map/MapCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> 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);

Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand Down
Loading