Files
SabreTools/SabreTools.Library/DatFiles/Json.cs
2020-08-24 11:56:49 -07:00

609 lines
21 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using SabreTools.Library.Data;
using SabreTools.Library.DatItems;
using SabreTools.Library.IO;
using SabreTools.Library.Tools;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace SabreTools.Library.DatFiles
{
/// <summary>
/// Represents parsing and writing of a JSON DAT
/// </summary>
internal class Json : DatFile
{
/// <summary>
/// Constructor designed for casting a base DatFile
/// </summary>
/// <param name="datFile">Parent DatFile to copy from</param>
public Json(DatFile datFile)
: base(datFile)
{
}
/// <summary>
/// Parse a Logiqx XML DAT and return all found games and roms within
/// </summary>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
protected override void ParseFile(
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
bool keep)
{
// Prepare all internal variables
StreamReader sr = new StreamReader(FileExtensions.TryOpenRead(filename), new UTF8Encoding(false));
JsonTextReader jtr = new JsonTextReader(sr);
// If we got a null reader, just return
if (jtr == null)
return;
// Otherwise, read the file to the end
try
{
jtr.Read();
while (!sr.EndOfStream)
{
// Skip everything not a property name
if (jtr.TokenType != JsonToken.PropertyName)
{
jtr.Read();
continue;
}
switch (jtr.Value)
{
// Header value
case "header":
ReadHeader(jtr);
jtr.Read();
break;
// Machine array
case "machines":
ReadMachines(sr, jtr, filename, indexId);
jtr.Read();
break;
default:
jtr.Read();
break;
}
}
}
catch (Exception ex)
{
Globals.Logger.Warning($"Exception found while parsing '{filename}': {ex}");
}
jtr.Close();
}
/// <summary>
/// Read header information
/// </summary>
/// <param name="jtr">JsonTextReader to use to parse the header</param>
private void ReadHeader(JsonTextReader jtr)
{
// If the reader is invalid, skip
if (jtr == null)
return;
// Read in the header and apply any new fields
jtr.Read();
JsonSerializer js = new JsonSerializer();
DatHeader header = js.Deserialize<DatHeader>(jtr);
Header.ConditionalCopy(header);
}
/// <summary>
/// Read machine array information
/// </summary>
/// <param name="sr">StreamReader to use to parse the header</param>
/// <param name="itr">JsonTextReader to use to parse the machine</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
private void ReadMachines(
StreamReader sr,
JsonTextReader jtr,
// Standard Dat parsing
string filename,
int indexId)
{
// If the reader is invalid, skip
if (jtr == null)
return;
jtr.Read();
while (!sr.EndOfStream)
{
// If we hit the end of an array, we want to return
if (jtr.TokenType == JsonToken.EndArray)
return;
// We don't care about anything except start object
if (jtr.TokenType != JsonToken.StartObject)
{
jtr.Read();
continue;
}
ReadMachine(sr, jtr, filename, indexId);
jtr.Read();
}
}
/// <summary>
/// Read machine object information
/// </summary>
/// <param name="sr">StreamReader to use to parse the header</param>
/// <param name="itr">JsonTextReader to use to parse the machine</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
private void ReadMachine(
StreamReader sr,
JsonTextReader jtr,
// Standard Dat parsing
string filename,
int indexId)
{
// If we have an empty machine, skip it
if (jtr == null)
return;
// Prepare internal variables
JsonSerializer js = new JsonSerializer();
Machine machine = null;
jtr.Read();
while (!sr.EndOfStream)
{
// If we hit the end of the machine, return
if (jtr.TokenType == JsonToken.EndObject)
return;
// We don't care about anything except properties
if (jtr.TokenType != JsonToken.PropertyName)
{
jtr.Read();
continue;
}
switch (jtr.Value)
{
case "machine":
jtr.Read();
machine = js.Deserialize<Machine>(jtr);
break;
case "items":
ReadItems(sr, jtr, filename, indexId, machine);
break;
default:
break;
}
jtr.Read();
}
}
/// <summary>
/// Read item array information
/// </summary>
/// <param name="sr">StreamReader to use to parse the header</param>
/// <param name="jtr">JsonTextReader to use to parse the machine</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="machine">Machine information to add to the parsed items</param>
private void ReadItems(
StreamReader sr,
JsonTextReader jtr,
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
Machine machine)
{
// If the reader is invalid, skip
if (jtr == null)
return;
jtr.Read();
while (!sr.EndOfStream)
{
// If we hit the end of an array, we want to return
if (jtr.TokenType == JsonToken.EndArray)
return;
// We don't care about anything except start object
if (jtr.TokenType != JsonToken.StartObject)
{
jtr.Read();
continue;
}
ReadItem(sr, jtr, filename, indexId, machine);
jtr.Read();
}
}
/// <summary>
/// Read item information
/// </summary>
/// <param name="sr">StreamReader to use to parse the header</param>
/// <param name="jtr">JsonTextReader to use to parse the machine</param>
/// <param name="filename">Name of the file to be parsed</param>
/// <param name="indexId">Index ID for the DAT</param>
/// <param name="machine">Machine information to add to the parsed items</param>
private void ReadItem(
StreamReader sr,
JsonTextReader jtr,
// Standard Dat parsing
string filename,
int indexId,
// Miscellaneous
Machine machine)
{
// If we have an empty machine, skip it
if (jtr == null)
return;
// Prepare internal variables
JsonSerializer js = new JsonSerializer();
DatItem datItem = null;
jtr.Read();
while (!sr.EndOfStream)
{
// If we hit the end of the machine - create, add, and return
if (jtr.TokenType == JsonToken.EndObject)
{
// If we didn't read something valid, just return
if (datItem == null)
return;
datItem.CopyMachineInformation(machine);
datItem.Source = new Source { Index = indexId, Name = filename };
ParseAddHelper(datItem);
return;
}
// We don't care about anything except properties
if (jtr.TokenType != JsonToken.PropertyName)
{
jtr.Read();
continue;
}
switch (jtr.Value)
{
case "datitem":
jtr.Read();
JObject datItemObj = js.Deserialize<JObject>(jtr);
switch (datItemObj.Value<string>("type").AsItemType())
{
case ItemType.Archive:
datItem = datItemObj.ToObject<Archive>();
break;
case ItemType.BiosSet:
datItem = datItemObj.ToObject<BiosSet>();
break;
case ItemType.Blank:
datItem = datItemObj.ToObject<Blank>();
break;
case ItemType.Disk:
datItem = datItemObj.ToObject<Disk>();
break;
case ItemType.Release:
datItem = datItemObj.ToObject<Release>();
break;
case ItemType.Rom:
datItem = datItemObj.ToObject<Rom>();
break;
case ItemType.Sample:
datItem = datItemObj.ToObject<Sample>();
break;
}
break;
default:
break;
}
jtr.Read();
}
}
/// <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>
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
{
try
{
Globals.Logger.User($"Opening file for writing: {outfile}");
FileStream fs = FileExtensions.TryCreate(outfile);
// If we get back null for some reason, just log and return
if (fs == null)
{
Globals.Logger.Warning($"File '{outfile}' could not be created for writing! Please check to see if the file is writable");
return false;
}
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false));
JsonTextWriter jtw = new JsonTextWriter(sw)
{
Formatting = Formatting.Indented,
IndentChar = '\t',
Indentation = 1
};
// Write out the header
WriteHeader(jtw);
// Write out each of the machines and roms
string lastgame = null;
// Use a sorted list of games to output
foreach (string key in Items.SortedKeys)
{
List<DatItem> roms = Items[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.Machine.Name == 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.Machine.Name.ToLowerInvariant())
WriteEndGame(jtw);
// If we have a new game, output the beginning of the new item
if (lastgame == null || lastgame.ToLowerInvariant() != rom.Machine.Name.ToLowerInvariant())
WriteStartGame(jtw, rom);
// If we have a "null" game (created by DATFromDir or something similar), log it to file
if (rom.ItemType == ItemType.Rom
&& ((Rom)rom).Size == -1
&& ((Rom)rom).CRC == "null")
{
Globals.Logger.Verbose($"Empty folder found: {rom.Machine.Name}");
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;
#if NET_FRAMEWORK
((Rom)rom).RIPEMD160 = ((Rom)rom).RIPEMD160 == "null" ? Constants.RIPEMD160Zero : null;
#endif
((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
WriteDatItem(jtw, rom, ignoreblanks);
// Set the new data to compare against
lastgame = rom.Machine.Name;
}
}
// Write the file footer out
WriteFooter(jtw);
Globals.Logger.Verbose("File written!" + Environment.NewLine);
jtw.Close();
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="jtw">JsonTextWriter to output to</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteHeader(JsonTextWriter jtw)
{
try
{
jtw.WriteStartObject();
// Write the DatHeader
jtw.WritePropertyName("header");
JsonSerializer js = new JsonSerializer() { Formatting = Formatting.Indented };
js.Serialize(jtw, Header);
jtw.WritePropertyName("machines");
jtw.WriteStartArray();
jtw.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="jtw">JsonTextWriter to output to</param>
/// <param name="datItem">DatItem object to be output</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteStartGame(JsonTextWriter jtw, DatItem datItem)
{
try
{
// No game should start with a path separator
datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
// Build the state
jtw.WriteStartObject();
// Write the Machine
jtw.WritePropertyName("machine");
JsonSerializer js = new JsonSerializer() { Formatting = Formatting.Indented };
js.Serialize(jtw, datItem.Machine);
jtw.WritePropertyName("items");
jtw.WriteStartArray();
jtw.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="jtw">JsonTextWriter to output to</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteEndGame(JsonTextWriter jtw)
{
try
{
// End items
jtw.WriteEndArray();
// End machine
jtw.WriteEndObject();
jtw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
/// <summary>
/// Write out DatItem using the supplied StreamWriter
/// </summary>
/// <param name="jtw">JsonTextWriter to output to</param>
/// <param name="datItem">DatItem object 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>
private bool WriteDatItem(JsonTextWriter jtw, DatItem datItem, bool ignoreblanks = false)
{
// If we are in ignore blanks mode AND we have a blank (0-size) rom, skip
if (ignoreblanks && (datItem.ItemType == ItemType.Rom && ((datItem as Rom).Size == 0 || (datItem as Rom).Size == -1)))
return true;
// If we have the blank item type somehow, skip
if (datItem.ItemType == ItemType.Blank)
return true;
try
{
// Pre-process the item name
ProcessItemName(datItem, true);
// Build the state
jtw.WriteStartObject();
// Write the DatItem
jtw.WritePropertyName("datitem");
JsonSerializer js = new JsonSerializer() { ContractResolver = new BaseFirstContractResolver(), Formatting = Formatting.Indented };
js.Serialize(jtw, datItem);
// End item
jtw.WriteEndObject();
jtw.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="jtw">JsonTextWriter to output to</param>
/// <returns>True if the data was written, false on error</returns>
private bool WriteFooter(JsonTextWriter jtw)
{
try
{
// End items
jtw.WriteEndArray();
// End machine
jtw.WriteEndObject();
// End machines
jtw.WriteEndArray();
// End file
jtw.WriteEndObject();
jtw.Flush();
}
catch (Exception ex)
{
Globals.Logger.Error(ex.ToString());
return false;
}
return true;
}
}
}