2017-10-09 18:04:49 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
using SabreTools.Library.Data;
|
2017-11-02 15:44:15 -07:00
|
|
|
|
using SabreTools.Library.DatItems;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents parsing and writing of a DosCenter DAT
|
|
|
|
|
|
/// </summary>
|
2017-10-09 20:25:46 -07:00
|
|
|
|
public class DosCenter : DatFile
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
2017-10-16 13:52:56 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor designed for casting a base DatFile
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="datFile">Parent DatFile to copy from</param>
|
|
|
|
|
|
public DosCenter(DatFile datFile)
|
|
|
|
|
|
{
|
|
|
|
|
|
this._datHeader = datFile._datHeader;
|
|
|
|
|
|
this._items = datFile._items;
|
|
|
|
|
|
this._sortedBy = datFile._sortedBy;
|
|
|
|
|
|
this._datStats = datFile._datStats;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Parse a DosCenter DAT and return all found games and roms within
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
|
|
|
|
|
/// <param name="sysid">System ID for the DAT</param>
|
|
|
|
|
|
/// <param name="srcid">Source ID for the DAT</param>
|
|
|
|
|
|
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
|
|
|
|
|
|
/// <param name="clean">True if game names are sanitized, false otherwise (default)</param>
|
|
|
|
|
|
/// <param name="remUnicode">True if we should remove non-ASCII characters from output, false otherwise (default)</param>
|
2017-10-09 20:25:46 -07:00
|
|
|
|
public void Parse(
|
2017-10-09 18:04:49 -07:00
|
|
|
|
// Standard Dat parsing
|
|
|
|
|
|
string filename,
|
|
|
|
|
|
int sysid,
|
|
|
|
|
|
int srcid,
|
|
|
|
|
|
|
|
|
|
|
|
// Miscellaneous
|
|
|
|
|
|
bool keep,
|
|
|
|
|
|
bool clean,
|
|
|
|
|
|
bool remUnicode)
|
|
|
|
|
|
{
|
|
|
|
|
|
// ClrMamePro and DosCenter parsing are identical so it just calls one implementation
|
2017-10-09 20:25:46 -07:00
|
|
|
|
(this as DatFile as ClrMamePro).Parse(filename, sysid, srcid, keep, clean, remUnicode);
|
2017-10-09 18:04:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create and open an output file for writing direct from a dictionary
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="outfile">Name of the file to write to</param>
|
|
|
|
|
|
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
|
|
|
|
|
|
/// <returns>True if the DAT was written correctly, false otherwise</returns>
|
2017-10-09 20:25:46 -07:00
|
|
|
|
public bool WriteToFile(string outfile, bool ignoreblanks = false)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.User("Opening file for writing: {0}", outfile);
|
2017-11-08 00:27:00 -08:00
|
|
|
|
FileStream fs = Utilities.TryCreate(outfile);
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 10:24:08 -08:00
|
|
|
|
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false));
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
// Write out the header
|
2017-10-09 20:25:46 -07:00
|
|
|
|
WriteHeader(sw);
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
// Write out each of the machines and roms
|
|
|
|
|
|
string lastgame = null;
|
|
|
|
|
|
|
|
|
|
|
|
// Get a properly sorted set of keys
|
2017-10-30 21:15:37 -07:00
|
|
|
|
List<string> keys = Keys;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
keys.Sort(new NaturalComparer());
|
|
|
|
|
|
|
|
|
|
|
|
foreach (string key in keys)
|
|
|
|
|
|
{
|
2017-10-09 20:25:46 -07:00
|
|
|
|
List<DatItem> roms = this[key];
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
List<string> newsplit = rom.MachineName.Split('\\').ToList();
|
|
|
|
|
|
|
|
|
|
|
|
// 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, rom);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// If we have a new game, output the beginning of the new item
|
|
|
|
|
|
if (lastgame == null || lastgame.ToLowerInvariant() != rom.MachineName.ToLowerInvariant())
|
|
|
|
|
|
{
|
|
|
|
|
|
WriteStartGame(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);
|
|
|
|
|
|
|
|
|
|
|
|
rom.Name = (rom.Name == "null" ? "-" : rom.Name);
|
|
|
|
|
|
((Rom)rom).Size = Constants.SizeZero;
|
|
|
|
|
|
((Rom)rom).CRC = ((Rom)rom).CRC == "null" ? Constants.CRCZero : null;
|
|
|
|
|
|
((Rom)rom).MD5 = ((Rom)rom).MD5 == "null" ? Constants.MD5Zero : null;
|
|
|
|
|
|
((Rom)rom).SHA1 = ((Rom)rom).SHA1 == "null" ? Constants.SHA1Zero : null;
|
|
|
|
|
|
((Rom)rom).SHA256 = ((Rom)rom).SHA256 == "null" ? Constants.SHA256Zero : null;
|
|
|
|
|
|
((Rom)rom).SHA384 = ((Rom)rom).SHA384 == "null" ? Constants.SHA384Zero : null;
|
|
|
|
|
|
((Rom)rom).SHA512 = ((Rom)rom).SHA512 == "null" ? Constants.SHA512Zero : null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Now, output the rom data
|
2017-10-30 21:49:55 -07:00
|
|
|
|
WriteDatItem(sw, rom, ignoreblanks);
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out DAT header using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sw">StreamWriter to output to</param>
|
|
|
|
|
|
/// <returns>True if the data was written, false on error</returns>
|
2017-10-09 20:25:46 -07:00
|
|
|
|
private bool WriteHeader(StreamWriter sw)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string header = "DOSCenter (\n" +
|
2017-10-09 20:25:46 -07:00
|
|
|
|
"\tName: " + Name + "\n" +
|
|
|
|
|
|
"\tDescription: " + Description + "\n" +
|
|
|
|
|
|
"\tVersion: " + Version + "\n" +
|
|
|
|
|
|
"\tDate: " + Date + "\n" +
|
|
|
|
|
|
"\tAuthor: " + Author + "\n" +
|
|
|
|
|
|
"\tHomepage: " + Homepage + "\n" +
|
|
|
|
|
|
"\tComment: " + Comment + "\n" +
|
2017-10-09 18:04:49 -07:00
|
|
|
|
")\n";
|
|
|
|
|
|
|
|
|
|
|
|
// Write the header out
|
|
|
|
|
|
sw.Write(header);
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out Game start using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sw">StreamWriter to output to</param>
|
2017-10-30 21:49:55 -07:00
|
|
|
|
/// <param name="rom">DatItem object to be output</param>
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// <returns>True if the data was written, false on error</returns>
|
2017-10-09 20:25:46 -07:00
|
|
|
|
private bool WriteStartGame(StreamWriter sw, DatItem rom)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// No game should start with a path separator
|
|
|
|
|
|
if (rom.MachineName.StartsWith(Path.DirectorySeparatorChar.ToString()))
|
|
|
|
|
|
{
|
|
|
|
|
|
rom.MachineName = rom.MachineName.Substring(1);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string state = "game (\n\tname \"" + rom.MachineName + ".zip\"\n";
|
|
|
|
|
|
|
|
|
|
|
|
sw.Write(state);
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out Game end using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sw">StreamWriter to output to</param>
|
2017-10-30 21:49:55 -07:00
|
|
|
|
/// <param name="rom">DatItem object to be output</param>
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// <returns>True if the data was written, false on error</returns>
|
2017-10-09 20:25:46 -07:00
|
|
|
|
private bool WriteEndGame(StreamWriter sw, DatItem rom)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string state = (String.IsNullOrEmpty(rom.SampleOf) ? "" : "\tsampleof \"" + rom.SampleOf + "\"\n") + ")\n";
|
|
|
|
|
|
|
|
|
|
|
|
sw.Write(state);
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-10-30 21:49:55 -07:00
|
|
|
|
/// Write out DatItem using the supplied StreamWriter
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sw">StreamWriter to output to</param>
|
2017-10-30 21:49:55 -07:00
|
|
|
|
/// <param name="rom">DatItem object to be output</param>
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// <param name="ignoreblanks">True if blank roms should be skipped on output, false otherwise (default)</param>
|
|
|
|
|
|
/// <returns>True if the data was written, false on error</returns>
|
2017-10-30 21:49:55 -07:00
|
|
|
|
private bool WriteDatItem(StreamWriter sw, DatItem rom, bool ignoreblanks = false)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
// 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 = "";
|
|
|
|
|
|
switch (rom.Type)
|
|
|
|
|
|
{
|
|
|
|
|
|
case ItemType.Archive:
|
|
|
|
|
|
case ItemType.BiosSet:
|
|
|
|
|
|
case ItemType.Disk:
|
|
|
|
|
|
case ItemType.Release:
|
|
|
|
|
|
case ItemType.Sample:
|
|
|
|
|
|
// We don't output these at all for DosCenter
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ItemType.Rom:
|
|
|
|
|
|
state += "\tfile ( name " + ((Rom)rom).Name
|
|
|
|
|
|
+ (((Rom)rom).Size != -1 ? " size " + ((Rom)rom).Size : "")
|
|
|
|
|
|
+ (!String.IsNullOrEmpty(((Rom)rom).Date) ? " date " + ((Rom)rom).Date : "")
|
|
|
|
|
|
+ (!String.IsNullOrEmpty(((Rom)rom).CRC) ? " crc " + ((Rom)rom).CRC.ToLowerInvariant() : "")
|
|
|
|
|
|
+ " )\n";
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sw.Write(state);
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Write out DAT footer using the supplied StreamWriter
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sw">StreamWriter to output to</param>
|
|
|
|
|
|
/// <returns>True if the data was written, false on error</returns>
|
2017-10-09 20:25:46 -07:00
|
|
|
|
private bool WriteFooter(StreamWriter sw)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
string footer = ")\n";
|
|
|
|
|
|
|
|
|
|
|
|
// Write the footer out
|
|
|
|
|
|
sw.Write(footer);
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|