Skip to content
Open
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
7 changes: 1 addition & 6 deletions scripts/Rhythia.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,7 @@ public override async void _Ready()
&& MapParser.IsValidExt(f.GetExtension().ToLower())
);

await MapParser.BulkImport([.. nonConvertedMaps], notify: true);

foreach (string file in nonConvertedMaps)
{
File.Delete(file);
}
await MapParser.BulkImport([.. nonConvertedMaps], notify: true, migrateLegacyMaps: true);

// Temporary map testing support
string[] cmdArgs = OS.GetCmdlineArgs();
Expand Down
68 changes: 60 additions & 8 deletions scripts/map/MapParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public override void _Ready()

public static bool IsValidExt(string ext) => ext == "phxm" || ext == "sspm" || ext == "txt" || ext == "rhm";

public static async Task BulkImport(string[] files, bool notify = false)
public static async Task BulkImport(string[] files, bool notify = false, bool migrateLegacyMaps = false)
{
if (files.Length == 0 || files == null)
if (files == null || files.Length == 0)
return;

if (notify)
Expand All @@ -43,6 +43,7 @@ await Task.Run(() =>
int good = 0;
int corrupted = 0;
var maps = new ConcurrentBag<Map>();
object destinationLock = new();

Callable.From(() => Instance.EmitSignal(SignalName.MapsImportStarted)).CallDeferred();
Parallel.ForEach(
Expand All @@ -52,14 +53,20 @@ await Task.Run(() =>
{
try
{
var map = Decode(file, null, false, false);
var map = Decode(file);

if (map != null)
if (map == null)
{
Encode(map);
maps.Add(map);
System.Threading.Interlocked.Increment(ref corrupted);
return;
}

if (migrateLegacyMaps)
migrateLegacyMap(map, file, destinationLock);
else
Encode(map);

maps.Add(map);
System.Threading.Interlocked.Increment(ref good);
Callable.From(() => Instance.EmitSignal(SignalName.MapImported, map)).CallDeferred();
}
Expand All @@ -80,6 +87,49 @@ await Task.Run(() =>
_ = ToastNotification.Notify($"Finished importing {files.Length} map(s)");
}

private static void migrateLegacyMap(Map map, string sourcePath, object destinationLock)
{
string mapsDirectory = MapUtil.MapsFolder;
string originalName = map.Name;
string destination = Path.Combine(mapsDirectory, originalName);

lock (destinationLock)
{
for (int suffix = 2; Directory.Exists(destination) || File.Exists(destination); suffix++)
{
map.Name = $"{originalName}-{suffix}";
destination = Path.Combine(mapsDirectory, map.Name);
}

Directory.CreateDirectory(destination);
}

try
{
if (Encode(map) < 0)
throw new IOException($"Could not cache converted map {sourcePath}");
}
catch
{
if (Directory.Exists(destination))
Directory.Delete(destination, recursive: true);
throw;
}

try
{
File.Delete(sourcePath);
}
catch (IOException exception)
{
Logger.Error(exception);
}
catch (UnauthorizedAccessException exception)
{
Logger.Error(exception);
}
}

public static void ExportEncode(Map map)
{
/*
Expand Down Expand Up @@ -156,7 +206,7 @@ void addAsset(string name, byte[] buffer)
File.WriteAllBytes(exportFilePath, ms.ToArray());
}

public static void Encode(Map map, bool logBenchmark = false)
public static int Encode(Map map, bool logBenchmark = false)
{
double start = Time.GetTicksUsec();

Expand Down Expand Up @@ -242,12 +292,14 @@ void addAsset(string name, byte[] buffer)

map.FolderPath = mapFolderPath;

MapCache.InsertMap(map);
int cacheId = MapCache.InsertMap(map);

if (logBenchmark)
{
Logger.Log($"ENCODING {Constants.DEFAULT_MAP_EXT.ToUpper()}: {(Time.GetTicksUsec() - start) / 1000}ms");
}

return cacheId;
}

public static Map Decode(string path, string audio = null, bool logBenchmark = false, bool save = false)
Expand Down