using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Web; using SabreTools.Library.Data; using SabreTools.Library.Items; using SabreTools.Library.Tools; #if MONO using System.IO; #else using Alphaleonis.Win32.Filesystem; using FileStream = System.IO.FileStream; using StreamWriter = System.IO.StreamWriter; #endif using NaturalSort; namespace SabreTools.Library.DatFiles { /// /// Represents parsing and writing of an SabreDat XML DAT /// public class SoftwareList { /// /// Parse an SabreDat XML DAT and return all found games and roms within /// /// DatFile to populate with the read information /// Name of the file to be parsed /// System ID for the DAT /// Source ID for the DAT /// True if full pathnames are to be kept, false otherwise (default) /// True if game names are sanitized, false otherwise (default) /// True if we should remove non-ASCII characters from output, false otherwise (default) /// /// public static void Parse( DatFile datFile, // Standard Dat parsing string filename, int sysid, int srcid, // Miscellaneous bool keep, bool clean, bool remUnicode) { // All XML-derived DATs share a lot in common so it just calls one implementation Logiqx.Parse(datFile, filename, sysid, srcid, keep, clean, remUnicode); } /// /// Create and open an output file for writing direct from a dictionary /// /// DatFile to write out from /// Name of the file to write to /// True if blank roms should be skipped on output, false otherwise (default) /// True if the DAT was written correctly, false otherwise public static bool WriteToFile(DatFile datFile, string outfile, bool ignoreblanks = false) { try { Globals.Logger.User("Opening file for writing: {0}", outfile); FileStream fs = FileTools.TryCreate(outfile); // If we get back null for some reason, just log and return if (fs == null) { Globals.Logger.Warning("File '{0}' could not be created for writing! Please check to see if the file is writable", outfile); return false; } StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(true)); // Write out the header WriteHeader(datFile, sw); // Write out each of the machines and roms string lastgame = null; // Get a properly sorted set of keys List keys = datFile.Keys.ToList(); keys.Sort(new NaturalComparer()); foreach (string key in keys) { List roms = datFile[key]; // Resolve the names in the block roms = DatItem.ResolveNames(roms); for (int index = 0; index < roms.Count; index++) { DatItem rom = roms[index]; // There are apparently times when a null rom can skip by, skip them if (rom.Name == null || rom.MachineName == null) { Globals.Logger.Warning("Null rom found!"); continue; } // 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.MachineName.ToLowerInvariant()) { WriteEndGame(sw); } // If we have a new game, output the beginning of the new item if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant()) { WriteStartGame(datFile, sw, rom); } // If we have a "null" game (created by DATFromDir or something similar), log it to file if (rom.Type == ItemType.Rom && ((Rom)rom).Size == -1 && ((Rom)rom).CRC == "null") { Globals.Logger.Verbose("Empty folder found: {0}", rom.MachineName); lastgame = rom.MachineName; continue; } // Now, output the rom data WriteRomData(sw, rom, ignoreblanks); // Set the new data to compare against lastgame = rom.MachineName; } } // Write the file footer out WriteFooter(sw); Globals.Logger.Verbose("File written!" + Environment.NewLine); sw.Dispose(); fs.Dispose(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } /// /// Write out DAT header using the supplied StreamWriter /// /// DatFile to write out from /// StreamWriter to output to /// True if the data was written, false on error private static bool WriteHeader(DatFile datFile, StreamWriter sw) { try { string header = "\n" + "\n\n" + "\n\n"; // Write the header out sw.Write(header); sw.Flush(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } /// /// Write out Game start using the supplied StreamWriter /// /// DatFile to write out from /// StreamWriter to output to /// RomData object to be output /// True if the data was written, false on error private static bool WriteStartGame(DatFile datFile, StreamWriter sw, DatItem rom) { try { // No game should start with a path separator if (rom.MachineName.StartsWith(Path.DirectorySeparatorChar.ToString())) { rom.MachineName = rom.MachineName.Substring(1); } string state = "\t\n" + "\t\t" + HttpUtility.HtmlEncode(rom.MachineDescription) + "\n" + (rom.Year != null ? "\t\t" + HttpUtility.HtmlEncode(rom.Year) + "\n" : "") + (rom.Publisher != null ? "\t\t" + HttpUtility.HtmlEncode(rom.Publisher) + "\n" : ""); foreach (Tuple kvp in rom.Infos) { state += "\t\t\n"; } sw.Write(state); sw.Flush(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } /// /// Write out Game start using the supplied StreamWriter /// /// StreamWriter to output to /// True if the data was written, false on error private static bool WriteEndGame(StreamWriter sw) { try { string state = "\t\n\n"; sw.Write(state); sw.Flush(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } /// /// Write out RomData using the supplied StreamWriter /// /// StreamWriter to output to /// RomData object to be output /// True if blank roms should be skipped on output, false otherwise (default) /// True if the data was written, false on error private static bool WriteRomData(StreamWriter sw, DatItem rom, bool ignoreblanks = false) { // If we are in ignore blanks mode AND we have a blank (0-size) rom, skip if (ignoreblanks && (rom.Type == ItemType.Rom && (((Rom)rom).Size == 0 || ((Rom)rom).Size == -1))) { return true; } try { string state = ""; state += "\t\t\n"; foreach (Tuple kvp in rom.Features) { state += "\t\t\t\n"; } switch (rom.Type) { case ItemType.Archive: state += "\t\t\t\n" + "\t\t\t\t\n" + "\t\t\t\n"; break; case ItemType.BiosSet: state += "\t\t\t\n" + "\t\t\t\t\n" + "\t\t\t\n"; break; case ItemType.Disk: state += "\t\t\t\n" + "\t\t\t\t\n" + "\t\t\t\n"; break; case ItemType.Release: state += "\t\t\t\n" + "\t\t\t\t\n" + "\t\t\t\n"; break; case ItemType.Rom: state += "\t\t\t\n" + "\t\t\t\t\n" + "\t\t\t\n"; break; case ItemType.Sample: state += "\t\t\t\n" + "\t\t\t\t\n" + "\t\t\t\n"; break; } state += "\t\t\n"; sw.Write(state); sw.Flush(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } /// /// Write out DAT footer using the supplied StreamWriter /// /// StreamWriter to output to /// True if the data was written, false on error private static bool WriteFooter(StreamWriter sw) { try { string footer = "\t\n\n\n"; // Write the footer out sw.Write(footer); sw.Flush(); } catch (Exception ex) { Globals.Logger.Error(ex.ToString()); return false; } return true; } } }