GMSAudioConverter/src/Program.cs

99 lines
3.6 KiB
C#

using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;
using System.Text;
using System.Text.Json;
namespace GMSAudioConverter
{
class Program
{
static int Main(string[] args)
{
var project = new Command("project")
{
new Argument<FileInfo>(
"project",
"Path to a GMS2.3 yyp file."
)
};
var root = new RootCommand{
project
};
project.Handler = CommandHandler.Create<FileInfo, IConsole>(HandleProject);
return root.Invoke(args);
}
static void HandleProject(FileInfo project, IConsole console)
{
var jsonReaderOptions = new JsonDocumentOptions();
jsonReaderOptions.AllowTrailingCommas = true;
var gmProject = Importer.ImportProject(project);
var projectDir = project.Directory;
console.Out.Write("Parsing: " + gmProject.Name);
if (Directory.Exists("output"))
{
Directory.Delete("output", true);
}
var outputDir = Directory.CreateDirectory("output");
var musicDir = outputDir.CreateSubdirectory("music");
var soundDir = outputDir.CreateSubdirectory("sound");
foreach (var resource in gmProject.Resources)
{
var path = Path.Combine(projectDir.FullName, resource.ID.Path);
var file = new FileInfo(path);
var resourceJsonString = File.ReadAllText(path);
var document = JsonDocument.Parse(resourceJsonString, jsonReaderOptions);
if (document.RootElement.GetProperty("resourceType").GetString() == "GMSound")
{
var soundMetadata = Importer.ImportSound(file);
var soundName = soundMetadata.Name;
var soundPath = path.Replace(".yy", "");
/* it's a WAV file! */
if (File.Exists(soundPath + ".wav"))
{
File.Copy(soundPath + ".wav", Path.Combine(soundDir.FullName, soundName + ".wav"), false);
}
/* it's an OGG file! */
else if (File.Exists(soundPath + ".ogg"))
{
File.Copy(soundPath + ".ogg", Path.Combine(musicDir.FullName, soundName + ".ogg"), false);
}
else
{
/* time to do some detection */
var header = new byte[4];
var stream = File.OpenRead(soundPath);
stream.Read(header, 0, 4);
var headerString = Encoding.UTF8.GetString(header, 0, 4);
if (headerString.Contains("RIFF"))
{
/* it's a WAV file! */
File.Copy(soundPath, Path.Combine(soundDir.FullName, soundName + ".wav"), false);
}
else if (headerString.Contains("OggS"))
{
/* it's an OGG file! */
File.Copy(soundPath, Path.Combine(musicDir.FullName, soundName + ".ogg"), false);
}
else
{
console.Out.Write("unrecognized header: " + headerString + "\n");
}
}
}
}
}
}
}