diff --git a/src/addoncreator/Program.cs b/src/addoncreator/Program.cs index 7a1f67c..b7c6c15 100644 --- a/src/addoncreator/Program.cs +++ b/src/addoncreator/Program.cs @@ -2,6 +2,7 @@ using System.Diagnostics; using System.IO; using System.Linq; +using System.Text; using GarrysMod.AddonCreator.Addon; namespace GarrysMod.AddonCreator @@ -10,128 +11,138 @@ namespace GarrysMod.AddonCreator { private static void Main(string[] args) { - switch (args.Length == 0 ? "" : args[0]) + while (args.Any()) { - case "create": + switch (args.Length == 0 ? "" : args[0]) { - if (args.Length < 3) + case "create": { - goto default; - } - - var folder = new DirectoryInfo(args[1]); - var output = args[2]; - var addon = new AddonFile(); - - if (!folder.Exists) - { - Console.Error.WriteLine( - "ERROR: Input folder needs to exist and needs to contain appropriate data."); - return; - } - - // recursively add files - foreach (var file in folder.EnumerateFiles("*", SearchOption.AllDirectories)) - { - var relpath = - MakeRelativePath(folder.FullName, file.FullName).Replace(Path.DirectorySeparatorChar, '/'); - Console.WriteLine("Adding: {0}", relpath); - - addon.Files.Add(relpath, new PhysicalAddonFileInfo(file.FullName)); - } - - // create addon - Console.WriteLine("Exporting addon..."); - addon.Export(output); - - Console.WriteLine("Done."); - break; - } - case "extract": - { - if (args.Length < 3) - { - goto default; - } - - var gma = args[1]; - var folder = new DirectoryInfo(args[2]); - - if (!File.Exists(gma)) - { - Console.Error.WriteLine("ERROR: Input GMA file needs to exist."); - return; - } - - var addon = new AddonFile(); - try - { - addon.Import(gma); - } - catch (Exception err) - { - Console.Error.WriteLine("ERROR: Input GMA file could not be read - {0}", err.Message); -#if DEBUG - Debugger.Break(); -#endif - return; - } - - Console.WriteLine("## Addon information ##"); - Console.WriteLine(addon.Title); - Console.WriteLine("\tVersion {0}", addon.Version); - Console.WriteLine("\tby {0}", addon.Author); - Console.WriteLine(); - Console.WriteLine(addon.Description); - Console.WriteLine(); - - // extract files - foreach (var file in addon.Files) - { - var relpath = file.Key; - var targetFile = - new FileInfo(Path.Combine(folder.FullName, relpath.Replace('/', Path.DirectorySeparatorChar))); - - Console.WriteLine("Extracting: {0}", relpath); - - // create directory - var dir = targetFile.Directory; - if (dir == null) - continue; // I still need to think about the weird logic here - dir.Create(); - - // create file - using (var fs = targetFile.Create()) + if (args.Length < 3) { - var buffer = file.Value.GetContents(); + goto default; + } - // long-compatible copy algorithm - for (long i = 0; i < buffer.LongLength; i += int.MaxValue) + var folder = new DirectoryInfo(args[1]); + var output = args[2]; + var addon = new AddonFile(); + + if (!folder.Exists) + { + Console.Error.WriteLine( + "ERROR: Input folder needs to exist and needs to contain appropriate data."); + return; + } + + // recursively add files + foreach (var file in folder.EnumerateFiles("*", SearchOption.AllDirectories)) + { + var relpath = + MakeRelativePath(folder.FullName, file.FullName) + .Replace(Path.DirectorySeparatorChar, '/'); + + addon.Files.Add(relpath, new PhysicalAddonFileInfo(file.FullName)); + } + + // create addon + Console.WriteLine("Exporting addon..."); + addon.Export(output); + + Console.WriteLine("Done."); + args = args.Skip(3).ToArray(); + break; + } + + case "extract": + { + if (args.Length < 3) + { + goto default; + } + + var gma = args[1]; + var folder = new DirectoryInfo(args[2]); + + if (!File.Exists(gma)) + { + Console.Error.WriteLine("ERROR: Input GMA file needs to exist."); + return; + } + + var addon = new AddonFile(); + try + { + addon.Import(gma); + } + catch (Exception err) + { + Console.Error.WriteLine("ERROR: Input GMA file could not be read - {0}", err.Message); +#if DEBUG + Debugger.Break(); +#endif + return; + } + + Console.WriteLine("## Addon information ##"); + Console.WriteLine(addon.Title); + Console.WriteLine("\tVersion {0}", addon.Version); + Console.WriteLine("\tby {0}", addon.Author); + Console.WriteLine(); + Console.WriteLine(addon.Description); + Console.WriteLine(); + + // extract files + foreach (var file in addon.Files) + { + var relpath = file.Key; + var targetFile = + new FileInfo(Path.Combine(folder.FullName, + relpath.Replace('/', Path.DirectorySeparatorChar))); + + Console.WriteLine("Extracting: {0}", relpath); + + // create directory + var dir = targetFile.Directory; + if (dir == null) + continue; // I still need to think about the weird logic here + dir.Create(); + + // create file + using (var fs = targetFile.Create()) { - var toWrite = (int) Math.Min(int.MaxValue, buffer.LongLength - i); - var toWriteBuf = buffer.AsEnumerable(); - for (long j = 0; j < i; j += int.MaxValue) + var buffer = file.Value.GetContents(); + + // long-compatible copy algorithm + for (long i = 0; i < buffer.LongLength; i += int.MaxValue) { - toWriteBuf = toWriteBuf.Skip(int.MaxValue); + var toWrite = (int) Math.Min(int.MaxValue, buffer.LongLength - i); + var toWriteBuf = buffer.AsEnumerable(); + for (long j = 0; j < i; j += int.MaxValue) + { + toWriteBuf = toWriteBuf.Skip(int.MaxValue); + } + fs.Write(toWriteBuf.ToArray(), 0, toWrite); } - fs.Write(toWriteBuf.ToArray(), 0, toWrite); + + fs.Flush(); } } + + Console.WriteLine("Done."); + args = args.Skip(3).ToArray(); + break; } - Console.WriteLine("Done."); - break; + default: + Console.WriteLine("Usage: {0} ", Process.GetCurrentProcess().ProcessName); + Console.WriteLine(); + Console.WriteLine("Commands:"); + Console.WriteLine("\t{0}\t{1}", "extract", "Extracts a GMA file and shows information about it."); + Console.WriteLine("\t\tArguments: Input GMA file path, output folder path"); + Console.WriteLine("\t{0}\t{1}", "create", "Creates a GMA file."); + Console.WriteLine("\t\tArguments: Input folder path, output GMA file path"); + Console.WriteLine(); + return; } - default: - Console.WriteLine("Usage: {0} ", Process.GetCurrentProcess().ProcessName); - Console.WriteLine(); - Console.WriteLine("Commands:"); - Console.WriteLine("\t{0}\t{1}", "extract", "Extracts a GMA file and shows information about it."); - Console.WriteLine("\t\tArguments: Input GMA file path, output folder path"); - Console.WriteLine("\t{0}\t{1}", "create", "Creates a GMA file."); - Console.WriteLine("\t\tArguments: Input folder path, output GMA file path"); - Console.WriteLine(); - break; } }