/// <summary> /// Method that gets executed on the "new" command /// </summary> /// <param name="options">The arguments that have been provided with the "new" command</param> /// <returns><see cref="EXIT_SUCCESS"/> and <see cref="EXIT_FAILURE"/> for being successful and failing respectively</returns> private static int New(NewOptions options) { //TODO: this should probably create a new Program instance, with just the properties that it needs UndertaleData data = UndertaleData.CreateNew(); // If stdout flag is set, write new data to stdout and quit if (options.Stdout) { if (options.Verbose) { Console.WriteLine("Attempting to write new Data file to STDOUT..."); } using MemoryStream ms = new MemoryStream(); UndertaleIO.Write(ms, data); Console.OpenStandardOutput().Write(ms.ToArray(), 0, (int)ms.Length); Console.Out.Flush(); if (options.Verbose) { Console.WriteLine("Successfully wrote new Data file to STDOUT."); } return(EXIT_SUCCESS); } // If not STDOUT, write to file instead. Check first if we have permission to overwrite if (options.Output.Exists && !options.Overwrite) { Console.Error.WriteLine($"'{options.Output}' already exists. Pass --overwrite to overwrite"); return(EXIT_FAILURE); } // We're not writing to STDOUT, and overwrite flag was given, so we write to specified file. if (options.Verbose) { Console.WriteLine($"Attempting to write new Data file to '{options.Output}'..."); } using FileStream fs = options.Output.OpenWrite(); UndertaleIO.Write(fs, data); if (options.Verbose) { Console.WriteLine($"Successfully wrote new Data file to '{options.Output}'."); } return(EXIT_SUCCESS); }
public static int New(NewOptions options) { var data = UndertaleData.CreateNew(); if (options.Stdout) { WriteStdout(); } else { if (WriteFile() == EXIT_FAILURE) { return(EXIT_FAILURE); } } return(EXIT_SUCCESS); int WriteFile() { if (options.Output.Exists && !options.Overwrite) { Console.Error.WriteLine($"{options.Output} already exists. Pass --overwrite to overwrite"); return(EXIT_FAILURE); } using (var fs = options.Output.OpenWrite()) { UndertaleIO.Write(fs, data); return(EXIT_SUCCESS); } } void WriteStdout() { using (var ms = new MemoryStream()) { UndertaleIO.Write(ms, data); System.Console.OpenStandardOutput().Write(ms.ToArray(), 0, (int)ms.Length); System.Console.Out.Flush(); } } }