Skip to content

Commit

Permalink
FIX: comma should be escaped in filename when writing to .osu
Browse files Browse the repository at this point in the history
  • Loading branch information
QingQiz committed Jul 4, 2022
1 parent ca8a39d commit fe8e992
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 74 deletions.
14 changes: 7 additions & 7 deletions BmsToOsu/Converter/Osu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public static (string, HashSet<string> fileToCp) ToOsuBeatMap(
if (!string.IsNullOrEmpty(bg))
{
fileToCp.Add(Path.GetFileName(bg));
bd.AppendLine($"0,0,\"{bg}\",0,0");
bd.AppendLine($"0,0,\"{bg.Escape()}\",0,0");
}

// bga
Expand Down Expand Up @@ -103,12 +103,12 @@ public static (string, HashSet<string> fileToCp) ToOsuBeatMap(

if (vExt is not (".wmv" or ".mpg" or ".avi" or ".mp4" or ".webm" or ".mkv"))
{
bd.AppendLine($"Sprite,{layer},CentreRight,\"{bga.File}\",600,240");
bd.AppendLine($"Sprite,{layer},CentreRight,\"{bga.File.Escape()}\",600,240");
bd.AppendLine($" F,0,{(int)bga.StartTime},{(int)endTime},1");
}
else
{
bd.AppendLine($"Video,{(int)bga.StartTime},\"{bga.File}\"");
bd.AppendLine($"Video,{(int)bga.StartTime},\"{bga.File.Escape()}\"");
}

if (!string.IsNullOrEmpty(bga.File)) fileToCp.Add(bga.File);
Expand All @@ -117,7 +117,7 @@ public static (string, HashSet<string> fileToCp) ToOsuBeatMap(
// sound effect
foreach (var sfx in data.SoundEffects)
{
bd.AppendLine($"Sample,{(int)sfx.StartTime},0,\"{sfx.SoundFile}\",100");
bd.AppendLine($"Sample,{(int)sfx.StartTime},0,\"{sfx.SoundFile.Escape()}\",100");

fileToCp.Add(sfx.SoundFile);
}
Expand All @@ -126,7 +126,7 @@ public static (string, HashSet<string> fileToCp) ToOsuBeatMap(
{
foreach (var hitObj in data.HitObject.Values.SelectMany(obj => obj))
{
bd.AppendLine($"Sample,{(int)hitObj.StartTime},0,\"{hitObj.HitSoundFile}\",100");
bd.AppendLine($"Sample,{(int)hitObj.StartTime},0,\"{hitObj.HitSoundFile.Escape()}\",100");
}
}

Expand Down Expand Up @@ -182,8 +182,8 @@ public static (string, HashSet<string> fileToCp) ToOsuBeatMap(

bd.AppendLine(
obj.IsLongNote
? $"{xPos},192,{(int)obj.StartTime},{objType},0,{(int)obj.EndTime}:0:0:0:0:{hitSound}"
: $"{xPos},192,{(int)obj.StartTime},{1 << 0},0,0:0:0:0:{hitSound}");
? $"{xPos},192,{(int)obj.StartTime},{objType},0,{(int)obj.EndTime}:0:0:0:0:{hitSound.Escape()}"
: $"{xPos},192,{(int)obj.StartTime},{1 << 0},0,0:0:0:0:{hitSound.Escape()}");
}
}

Expand Down
156 changes: 89 additions & 67 deletions BmsToOsu/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
using BmsToOsu;
using BmsToOsu.Converter;
using BmsToOsu.Entity;
using BmsToOsu.Utils;
using CommandLine;
using CommandLine.Text;
using log4net;

Logger.Config();
Expand All @@ -14,91 +16,111 @@
".bms", ".bml", ".bme", ".bmx"
};

Parser.Default.ParseArguments<Option>(args)
.WithParsed(o =>
var parser = new Parser(with =>
{
with.AutoVersion = false;
with.AutoHelp = true;
with.HelpWriter = null;
});

var result = parser.ParseArguments<Option>(args);

result.WithParsed(o =>
{
var osz = o.OutPath + ".osz";

// avoid removing existing folder
if (Directory.Exists(o.OutPath) && !o.NoRemove)
{
var osz = o.OutPath + ".osz";
logger.Warn($"{o.OutPath} exists, `--no-remove` will be appended to the parameter");
o.NoRemove = true;
}

// avoid removing existing folder
if (Directory.Exists(o.OutPath) && !o.NoRemove)
{
logger.Warn($"{o.OutPath} exists, `--no-remove` will be appended to the parameter");
o.NoRemove = true;
}
// avoid removing after generation
if (o.NoZip && !o.NoRemove)
{
logger.Warn("`--no-remove` is appended to the parameter");
o.NoRemove = true;
}

// avoid duplication
if (File.Exists(osz))
{
logger.Warn($"{osz} exists, ignoring...");
return;
}

// avoid removing after generation
if (o.NoZip && !o.NoRemove)
var ftc = new HashSet<string>();

var bms = Directory
.GetFiles(o.InputPath, "*.*", SearchOption.AllDirectories)
.Where(f => availableBmsExt.Any(ext => f.EndsWith(ext, StringComparison.OrdinalIgnoreCase)));

Parallel.ForEach(bms, fp =>
{
logger.Info($"Processing {fp}");

var dir = Path.GetDirectoryName(fp) ?? "";

BmsFileData data;

try
{
logger.Warn("`--no-remove` is appended to the parameter");
o.NoRemove = true;
data = BmsFileData.FromFile(fp);
}

// avoid duplication
if (File.Exists(osz))
catch (InvalidDataException)
{
logger.Warn($"{osz} exists, ignoring...");
return;
}

var ftc = new HashSet<string>();

var bms = Directory
.GetFiles(o.InputPath, "*.*", SearchOption.AllDirectories)
.Where(f => availableBmsExt.Any(ext => f.EndsWith(ext, StringComparison.OrdinalIgnoreCase)));
var (osu, ftc2) = data.ToOsuBeatMap(dir);

Parallel.ForEach(bms, fp =>
lock (ftc)
{
logger.Info($"Processing {fp}");

var dir = Path.GetDirectoryName(fp) ?? "";
foreach (var c in ftc2) ftc.Add(Path.Join(dir, c));
}

BmsFileData data;
var dest = dir.Replace(o.InputPath, o.OutPath);

try
{
data = BmsFileData.FromFile(fp);
}
catch (InvalidDataException)
{
return;
}
Directory.CreateDirectory(dest);
File.WriteAllText(Path.Join(dest, Path.GetFileNameWithoutExtension(fp) + ".osu"), osu);
});

var (osu, ftc2) = data.ToOsuBeatMap(dir);
if (!o.NoCopy)
{
logger.Info("Copying sound files");
Parallel.ForEach(ftc, c =>
{
var dest = c.Replace(o.InputPath, o.OutPath);
dest = Path.Join(Path.GetDirectoryName(dest), Path.GetFileName(dest).Escape());

lock (ftc)
if (!File.Exists(dest))
{
foreach (var c in ftc2) ftc.Add(Path.Join(dir, c));
File.Copy(c, dest, true);
}

var dest = dir.Replace(o.InputPath, o.OutPath);

Directory.CreateDirectory(dest);
File.WriteAllText(Path.Join(dest, Path.GetFileNameWithoutExtension(fp) + ".osu"), osu);
});
}

if (!o.NoCopy)
{
logger.Info("Copying sound files");
Parallel.ForEach(ftc, c =>
{
var dest = c.Replace(o.InputPath, o.OutPath);

if (!File.Exists(dest))
{
File.Copy(c, dest, true);
}
});
}
if (!o.NoZip && Directory.Exists(o.OutPath))
{
logger.Info($"Creating {osz}");
ZipFile.CreateFromDirectory(o.OutPath, osz, CompressionLevel.Fastest, false);
}

if (!o.NoZip && Directory.Exists(o.OutPath))
{
logger.Info($"Creating {osz}");
ZipFile.CreateFromDirectory(o.OutPath, osz, CompressionLevel.Fastest, false);
}
if (!o.NoRemove)
{
logger.Info($"Removing {o.OutPath}");
Directory.Delete(o.OutPath, true);
}
});

if (!o.NoRemove)
{
logger.Info($"Removing {o.OutPath}");
Directory.Delete(o.OutPath, true);
}
});
result.WithNotParsed(errs =>
{
var helpText = HelpText.AutoBuild(result, h =>
{
h.AutoHelp = true;
h.AutoVersion = false;
return HelpText.DefaultParsingErrorsHandler(result, h);
}, e => e);
Console.WriteLine(helpText);
});
5 changes: 5 additions & 0 deletions BmsToOsu/Utils/PathExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,9 @@ public static Dictionary<string, string> FixSoundPath(this Dictionary<string, st
.Where(kv => !string.IsNullOrEmpty(kv.Value))
);
}

public static string Escape(this string path)
{
return path.Replace(",", "-comma-");
}
}

0 comments on commit fe8e992

Please sign in to comment.