2017-10-09 18:04:49 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
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 Missfile
|
|
|
|
|
|
/// </summary>
|
2017-11-17 14:33:36 -08:00
|
|
|
|
internal class Missfile : 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 Missfile(DatFile datFile)
|
2017-12-14 16:40:56 -08:00
|
|
|
|
: base(datFile, cloneHeader: false)
|
2017-10-16 13:52:56 -07:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// <summary>
|
2017-11-17 14:58:54 -08:00
|
|
|
|
/// Parse a Missfile and return all found games and roms within
|
2017-10-09 18:04:49 -07:00
|
|
|
|
/// </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-11-17 14:58:54 -08:00
|
|
|
|
public override void ParseFile(
|
2017-10-09 18:04:49 -07:00
|
|
|
|
// Standard Dat parsing
|
|
|
|
|
|
string filename,
|
|
|
|
|
|
int sysid,
|
|
|
|
|
|
int srcid,
|
|
|
|
|
|
|
|
|
|
|
|
// Miscellaneous
|
|
|
|
|
|
bool keep,
|
|
|
|
|
|
bool clean,
|
|
|
|
|
|
bool remUnicode)
|
|
|
|
|
|
{
|
|
|
|
|
|
// There is no consistent way to parse a missfile...
|
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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-11-17 14:58:54 -08:00
|
|
|
|
public override 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 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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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
|
2017-10-30 21:49:55 -07:00
|
|
|
|
WriteDatItem(sw, rom, lastgame, ignoreblanks);
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
// Set the new data to compare against
|
|
|
|
|
|
lastgame = rom.MachineName;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Globals.Logger.Verbose("File written!" + Environment.NewLine);
|
|
|
|
|
|
sw.Dispose();
|
|
|
|
|
|
fs.Dispose();
|
|
|
|
|
|
}
|
|
|
|
|
|
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="lastgame">The name of the last game to be output</param>
|
|
|
|
|
|
/// <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, string lastgame, 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 = "", name = "", pre = "", post = "";
|
2018-02-24 13:14:58 -08:00
|
|
|
|
|
2018-02-24 12:54:36 -08:00
|
|
|
|
pre = CreatePrefixPostfix(rom, true);
|
|
|
|
|
|
post = CreatePrefixPostfix(rom, false);
|
2017-10-09 18:04:49 -07:00
|
|
|
|
|
|
|
|
|
|
// If we're in Romba mode, the state is consistent
|
2017-10-09 20:25:46 -07:00
|
|
|
|
if (Romba)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
if (rom.Type == ItemType.Rom)
|
|
|
|
|
|
{
|
|
|
|
|
|
// We can only write out if there's a SHA-1
|
2017-12-14 13:22:22 -08:00
|
|
|
|
if (!String.IsNullOrWhiteSpace(((Rom)rom).SHA1))
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
name = ((Rom)rom).SHA1.Substring(0, 2)
|
|
|
|
|
|
+ "/" + ((Rom)rom).SHA1.Substring(2, 2)
|
|
|
|
|
|
+ "/" + ((Rom)rom).SHA1.Substring(4, 2)
|
|
|
|
|
|
+ "/" + ((Rom)rom).SHA1.Substring(6, 2)
|
|
|
|
|
|
+ "/" + ((Rom)rom).SHA1 + ".gz";
|
|
|
|
|
|
state += pre + name + post + "\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (rom.Type == ItemType.Disk)
|
|
|
|
|
|
{
|
|
|
|
|
|
// We can only write out if there's a SHA-1
|
2017-12-14 13:22:22 -08:00
|
|
|
|
if (!String.IsNullOrWhiteSpace(((Disk)rom).SHA1))
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
name = ((Disk)rom).SHA1.Substring(0, 2)
|
|
|
|
|
|
+ "/" + ((Disk)rom).SHA1.Substring(2, 2)
|
|
|
|
|
|
+ "/" + ((Disk)rom).SHA1.Substring(4, 2)
|
|
|
|
|
|
+ "/" + ((Disk)rom).SHA1.Substring(6, 2)
|
|
|
|
|
|
+ "/" + ((Disk)rom).SHA1 + ".gz";
|
|
|
|
|
|
state += pre + name + post + "\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, use any flags
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2017-12-14 23:14:13 -08:00
|
|
|
|
name = (UseRomName ? rom.Name : rom.MachineName);
|
2017-12-14 13:22:22 -08:00
|
|
|
|
if (!String.IsNullOrWhiteSpace(ReplaceExtension) || RemoveExtension)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
2017-12-05 18:04:11 -08:00
|
|
|
|
if (RemoveExtension)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
2017-12-05 18:04:11 -08:00
|
|
|
|
ReplaceExtension = "";
|
2017-10-09 18:04:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string dir = Path.GetDirectoryName(name);
|
|
|
|
|
|
dir = (dir.StartsWith(Path.DirectorySeparatorChar.ToString()) ? dir.Remove(0, 1) : dir);
|
2017-12-05 18:04:11 -08:00
|
|
|
|
name = Path.Combine(dir, Path.GetFileNameWithoutExtension(name) + ReplaceExtension);
|
2017-10-09 18:04:49 -07:00
|
|
|
|
}
|
2017-12-14 13:22:22 -08:00
|
|
|
|
if (!String.IsNullOrWhiteSpace(AddExtension))
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
2017-12-05 18:04:11 -08:00
|
|
|
|
name += AddExtension;
|
2017-10-09 18:04:49 -07:00
|
|
|
|
}
|
2017-12-14 23:14:13 -08:00
|
|
|
|
if (UseRomName && GameName)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
name = Path.Combine(rom.MachineName, name);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-12-14 23:14:13 -08:00
|
|
|
|
if (!UseRomName && rom.MachineName != lastgame)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
state += pre + name + post + "\n";
|
|
|
|
|
|
lastgame = rom.MachineName;
|
|
|
|
|
|
}
|
2017-12-14 23:14:13 -08:00
|
|
|
|
else if (UseRomName)
|
2017-10-09 18:04:49 -07:00
|
|
|
|
{
|
|
|
|
|
|
state += pre + name + post + "\n";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sw.Write(state);
|
|
|
|
|
|
sw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|