From ed3a41c9b27c225f30a0ee79d8f05a20d8b6ff21 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 23 May 2016 13:45:12 -0700 Subject: [PATCH] Refactor write start game --- SabreHelper/Output.cs | 167 ++++++++++++++++++++++++++++++------------ 1 file changed, 120 insertions(+), 47 deletions(-) diff --git a/SabreHelper/Output.cs b/SabreHelper/Output.cs index 936de860..df911a7e 100644 --- a/SabreHelper/Output.cs +++ b/SabreHelper/Output.cs @@ -147,29 +147,7 @@ namespace SabreTools.Helper // If we have a new game, output the beginning of the new item if (lastgame == null || lastgame.ToLowerInvariant() != rom.Game.ToLowerInvariant()) { - switch (datdata.OutputFormat) - { - case OutputFormat.ClrMamePro: - state += "game (\n\tname \"" + rom.Game + "\"\n" + - "\tdescription \"" + rom.Game + "\"\n"; - break; - case OutputFormat.SabreDat: - for (int i = (last == -1 ? 0 : last); i < newsplit.Count; i++) - { - for (int j = 0; j < depth - last + i - (lastgame == null ? 1 : 0); j++) - { - state += "\t"; - } - state += "\n"; - } - depth = depth - (last == -1 ? 0 : last) + newsplit.Count; - break; - case OutputFormat.Xml: - state += "\t\n" + - "\t\t" + HttpUtility.HtmlEncode(rom.Game) + "\n"; - break; - } + WriteStartGame(sw, rom, newsplit, lastgame, datdata, depth, last, logger); } // If we have a "null" game (created by DATFromDir or something similar), log it to file @@ -206,30 +184,9 @@ namespace SabreTools.Helper } } - string footer = ""; - switch (datdata.OutputFormat) - { - case OutputFormat.ClrMamePro: - footer = ")"; - break; - case OutputFormat.SabreDat: - for (int i = depth - 1; i >= 2; i--) - { - // Print out the number of tabs and the end folder - for (int j = 0; j < i; j++) - { - footer += "\t"; - } - footer += "\n"; - } - footer += "\t\n"; - break; - case OutputFormat.Xml: - footer = "\t\n"; - break; - } - - sw.Write(footer); + // Write the file footer out + WriteFooter(sw, datdata, depth, logger); + logger.Log("File written!" + Environment.NewLine); sw.Close(); fs.Close(); @@ -243,6 +200,13 @@ namespace SabreTools.Helper return true; } + /// + /// Write out DAT header using the supplied StreamWriter + /// + /// StreamWriter to output to + /// DatData object representing DAT information + /// Logger object for file and console output + /// True if the data was written, false on error public static bool WriteHeader(StreamWriter sw, DatData datdata, Logger logger) { try @@ -326,6 +290,68 @@ namespace SabreTools.Helper return true; } + /// + /// Write out Game start using the supplied StreamWriter + /// + /// StreamWriter to output to + /// RomData object to be output + /// Split path representing the parent game (SabreDAT only) + /// The name of the last game to be output + /// DatData object representing DAT information + /// Current depth to output file at (SabreDAT only) + /// Last known depth to cycle back from (SabreDAT only) + /// Logger object for file and console output + /// The new depth of the tag + public static int WriteStartGame(StreamWriter sw, RomData rom, List newsplit, string lastgame, DatData datdata, int depth, int last, Logger logger) + { + try + { + string state = ""; + switch (datdata.OutputFormat) + { + case OutputFormat.ClrMamePro: + state += "game (\n\tname \"" + rom.Game + "\"\n" + + "\tdescription \"" + rom.Game + "\"\n"; + break; + case OutputFormat.SabreDat: + for (int i = (last == -1 ? 0 : last); i < newsplit.Count; i++) + { + for (int j = 0; j < depth - last + i - (lastgame == null ? 1 : 0); j++) + { + state += "\t"; + } + state += "\n"; + } + depth = depth - (last == -1 ? 0 : last) + newsplit.Count; + break; + case OutputFormat.Xml: + state += "\t\n" + + "\t\t" + HttpUtility.HtmlEncode(rom.Game) + "\n"; + break; + } + + sw.Write(state); + } + catch (Exception ex) + { + logger.Error(ex.ToString()); + return depth; + } + + return depth; + } + + /// + /// Write out RomData using the supplied StreamWriter + /// + /// StreamWriter to output to + /// RomData object to be output + /// The name of the last game to be output + /// DatData object representing DAT information + /// Current depth to output file at (SabreDAT only) + /// Logger object for file and console output + /// True if the data was written, false on error public static bool WriteRomData(StreamWriter sw, RomData rom, string lastgame, DatData datdata, int depth, Logger logger) { try @@ -442,5 +468,52 @@ namespace SabreTools.Helper return true; } + + /// + /// Write out DAT footer using the supplied StreamWriter + /// + /// StreamWriter to output to + /// DatData object representing DAT information + /// /// Current depth to output file at (SabreDAT only) + /// Logger object for file and console output + /// True if the data was written, false on error + public static bool WriteFooter(StreamWriter sw, DatData datdata, int depth, Logger logger) + { + try + { + string footer = ""; + switch (datdata.OutputFormat) + { + case OutputFormat.ClrMamePro: + footer = ")"; + break; + case OutputFormat.SabreDat: + for (int i = depth - 1; i >= 2; i--) + { + // Print out the number of tabs and the end folder + for (int j = 0; j < i; j++) + { + footer += "\t"; + } + footer += "\n"; + } + footer += "\t\n"; + break; + case OutputFormat.Xml: + footer = "\t\n"; + break; + } + + // Write the footer out + sw.Write(footer); + } + catch (Exception ex) + { + logger.Error(ex.ToString()); + return false; + } + + return true; + } } }