From c1565a5af2d8339a5f21669514c50cf8972caa09 Mon Sep 17 00:00:00 2001 From: Matt Nadareski Date: Mon, 23 May 2016 13:50:33 -0700 Subject: [PATCH] Refactor write end game --- SabreHelper/Output.cs | 111 +++++++++++++++++++++++++++--------------- 1 file changed, 71 insertions(+), 40 deletions(-) diff --git a/SabreHelper/Output.cs b/SabreHelper/Output.cs index df911a7e..600e40e0 100644 --- a/SabreHelper/Output.cs +++ b/SabreHelper/Output.cs @@ -103,51 +103,13 @@ namespace SabreTools.Helper // If we have a different game and we're not at the start of the list, output the end of last item if (lastgame != null && lastgame.ToLowerInvariant() != rom.Game.ToLowerInvariant()) { - switch (datdata.OutputFormat) - { - case OutputFormat.ClrMamePro: - state += ")\n"; - break; - case OutputFormat.SabreDat: - if (splitpath != null) - { - for (int i = 0; i < newsplit.Count && i < splitpath.Count; i++) - { - // Always keep track of the last seen item - last = i; - - // If we find a difference, break - if (newsplit[i] != splitpath[i]) - { - break; - } - } - - // Now that we have the last known position, take down all open folders - for (int i = depth - 1; i > last + 1; i--) - { - // Print out the number of tabs and the end folder - for (int j = 0; j < i; j++) - { - state += "\t"; - } - state += "\n"; - } - - // Reset the current depth - depth = 2 + last; - } - break; - case OutputFormat.Xml: - state += "\t\n"; - break; - } + depth = WriteEndGame(sw, rom, splitpath, newsplit, lastgame, datdata, depth, last, logger); } // If we have a new game, output the beginning of the new item if (lastgame == null || lastgame.ToLowerInvariant() != rom.Game.ToLowerInvariant()) { - WriteStartGame(sw, rom, newsplit, lastgame, datdata, depth, last, logger); + depth = 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 @@ -342,6 +304,75 @@ namespace SabreTools.Helper return depth; } + /// + /// Write out Game start using the supplied StreamWriter + /// + /// StreamWriter to output to + /// RomData object to be output + /// Split path representing last kwown parent game (SabreDAT only) + /// 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 WriteEndGame(StreamWriter sw, RomData rom, List splitpath, List newsplit, string lastgame, DatData datdata, int depth, int last, Logger logger) + { + try + { + string state = ""; + switch (datdata.OutputFormat) + { + case OutputFormat.ClrMamePro: + state += ")\n"; + break; + case OutputFormat.SabreDat: + if (splitpath != null) + { + for (int i = 0; i < newsplit.Count && i < splitpath.Count; i++) + { + // Always keep track of the last seen item + last = i; + + // If we find a difference, break + if (newsplit[i] != splitpath[i]) + { + break; + } + } + + // Now that we have the last known position, take down all open folders + for (int i = depth - 1; i > last + 1; i--) + { + // Print out the number of tabs and the end folder + for (int j = 0; j < i; j++) + { + state += "\t"; + } + state += "\n"; + } + + // Reset the current depth + depth = 2 + last; + } + break; + case OutputFormat.Xml: + state += "\t\n"; + break; + } + + sw.Write(state); + } + catch (Exception ex) + { + logger.Error(ex.ToString()); + return depth; + } + + return depth; + } + /// /// Write out RomData using the supplied StreamWriter ///