2020-06-15 22:31:46 -07:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
2020-08-28 15:06:07 -07:00
|
|
|
|
using System.Linq;
|
2020-06-15 22:31:46 -07:00
|
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
|
|
|
|
using SabreTools.Library.Data;
|
|
|
|
|
|
using SabreTools.Library.DatItems;
|
2020-08-01 23:04:11 -07:00
|
|
|
|
using SabreTools.Library.IO;
|
2020-06-15 22:31:46 -07:00
|
|
|
|
using SabreTools.Library.Tools;
|
|
|
|
|
|
using Newtonsoft.Json;
|
2020-08-24 11:56:49 -07:00
|
|
|
|
using Newtonsoft.Json.Linq;
|
2020-06-15 22:31:46 -07:00
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Library.DatFiles
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2020-08-24 20:23:57 -07:00
|
|
|
|
/// Represents parsing and writing of a reference JSON DAT
|
2020-06-15 22:31:46 -07:00
|
|
|
|
/// </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)
|
2020-07-15 09:41:59 -07:00
|
|
|
|
: base(datFile)
|
2020-06-15 22:31:46 -07:00
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-06-16 11:27:36 -07:00
|
|
|
|
/// <summary>
|
2020-08-24 20:23:57 -07:00
|
|
|
|
/// Parse a reference JSON DAT and return all found games and roms within
|
2020-06-16 11:27:36 -07:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="indexId">Index ID for the DAT</param>
|
2020-06-16 11:27:36 -07:00
|
|
|
|
/// <param name="keep">True if full pathnames are to be kept, false otherwise (default)</param>
|
2020-08-28 15:06:07 -07:00
|
|
|
|
protected override void ParseFile(string filename, int indexId, bool keep)
|
2020-06-16 11:27:36 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Prepare all internal variables
|
2020-07-15 09:41:59 -07:00
|
|
|
|
StreamReader sr = new StreamReader(FileExtensions.TryOpenRead(filename), new UTF8Encoding(false));
|
2020-06-16 11:27:36 -07:00
|
|
|
|
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":
|
2020-08-24 11:56:49 -07:00
|
|
|
|
ReadHeader(jtr);
|
2020-06-16 11:27:36 -07:00
|
|
|
|
jtr.Read();
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
// Machine array
|
|
|
|
|
|
case "machines":
|
2020-08-24 14:29:00 -07:00
|
|
|
|
ReadMachines(jtr, filename, indexId);
|
2020-06-16 11:27:36 -07:00
|
|
|
|
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>
|
2020-08-24 11:56:49 -07:00
|
|
|
|
private void ReadHeader(JsonTextReader jtr)
|
2020-06-16 11:27:36 -07:00
|
|
|
|
{
|
|
|
|
|
|
// If the reader is invalid, skip
|
|
|
|
|
|
if (jtr == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2020-08-24 11:56:49 -07:00
|
|
|
|
// Read in the header and apply any new fields
|
2020-06-16 11:27:36 -07:00
|
|
|
|
jtr.Read();
|
2020-08-24 11:56:49 -07:00
|
|
|
|
JsonSerializer js = new JsonSerializer();
|
|
|
|
|
|
DatHeader header = js.Deserialize<DatHeader>(jtr);
|
|
|
|
|
|
Header.ConditionalCopy(header);
|
2020-06-16 11:27:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Read machine array information
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="itr">JsonTextReader to use to parse the machine</param>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
|
|
|
|
|
/// <param name="indexId">Index ID for the DAT</param>
|
2020-08-28 15:06:07 -07:00
|
|
|
|
private void ReadMachines(JsonTextReader jtr, string filename, int indexId)
|
2020-06-16 11:27:36 -07:00
|
|
|
|
{
|
|
|
|
|
|
// If the reader is invalid, skip
|
|
|
|
|
|
if (jtr == null)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
2020-08-24 14:29:00 -07:00
|
|
|
|
// Read in the machine array
|
2020-06-16 11:27:36 -07:00
|
|
|
|
jtr.Read();
|
2020-08-24 14:29:00 -07:00
|
|
|
|
JsonSerializer js = new JsonSerializer();
|
|
|
|
|
|
JArray machineArray = js.Deserialize<JArray>(jtr);
|
2020-06-16 11:27:36 -07:00
|
|
|
|
|
2020-08-24 14:29:00 -07:00
|
|
|
|
// Loop through each machine object and process
|
|
|
|
|
|
foreach (JObject machineObj in machineArray)
|
|
|
|
|
|
{
|
|
|
|
|
|
ReadMachine(machineObj, filename, indexId);
|
2020-06-16 11:27:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-24 11:56:49 -07:00
|
|
|
|
/// Read machine object information
|
2020-06-16 11:27:36 -07:00
|
|
|
|
/// </summary>
|
2020-08-24 14:29:00 -07:00
|
|
|
|
/// <param name="machineObj">JObject representing a single machine</param>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <param name="filename">Name of the file to be parsed</param>
|
|
|
|
|
|
/// <param name="indexId">Index ID for the DAT</param>
|
2020-08-28 15:06:07 -07:00
|
|
|
|
private void ReadMachine(JObject machineObj, string filename, int indexId)
|
2020-06-16 11:27:36 -07:00
|
|
|
|
{
|
2020-08-24 14:29:00 -07:00
|
|
|
|
// If object is invalid, skip it
|
|
|
|
|
|
if (machineObj == null)
|
2020-06-16 11:27:36 -07:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare internal variables
|
2020-08-24 11:56:49 -07:00
|
|
|
|
JsonSerializer js = new JsonSerializer();
|
|
|
|
|
|
Machine machine = null;
|
2020-06-16 11:27:36 -07:00
|
|
|
|
|
2020-08-24 14:29:00 -07:00
|
|
|
|
// Read the machine info, if possible
|
|
|
|
|
|
if (machineObj.ContainsKey("machine"))
|
|
|
|
|
|
machine = machineObj["machine"].ToObject<Machine>();
|
2020-06-16 11:27:36 -07:00
|
|
|
|
|
2020-08-24 14:29:00 -07:00
|
|
|
|
// Read items, if possible
|
|
|
|
|
|
if (machineObj.ContainsKey("items"))
|
|
|
|
|
|
ReadItems(machineObj["items"] as JArray, filename, indexId, machine);
|
2020-06-16 11:27:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Read item array information
|
|
|
|
|
|
/// </summary>
|
2020-08-24 14:29:00 -07:00
|
|
|
|
/// <param name="itemsArr">JArray representing the items list</param>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <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>
|
2020-06-16 11:27:36 -07:00
|
|
|
|
private void ReadItems(
|
2020-08-24 14:29:00 -07:00
|
|
|
|
JArray itemsArr,
|
2020-06-16 11:27:36 -07:00
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
// Standard Dat parsing
|
|
|
|
|
|
string filename,
|
|
|
|
|
|
int indexId,
|
|
|
|
|
|
|
2020-06-16 11:27:36 -07:00
|
|
|
|
// Miscellaneous
|
|
|
|
|
|
Machine machine)
|
|
|
|
|
|
{
|
2020-08-24 14:29:00 -07:00
|
|
|
|
// If the array is invalid, skip
|
|
|
|
|
|
if (itemsArr == null)
|
2020-06-16 11:27:36 -07:00
|
|
|
|
return;
|
|
|
|
|
|
|
2020-08-24 14:29:00 -07:00
|
|
|
|
// Loop through each datitem object and process
|
|
|
|
|
|
foreach (JObject itemObj in itemsArr)
|
2020-06-16 11:27:36 -07:00
|
|
|
|
{
|
2020-08-24 14:29:00 -07:00
|
|
|
|
ReadItem(itemObj, filename, indexId, machine);
|
2020-06-16 11:27:36 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Read item information
|
|
|
|
|
|
/// </summary>
|
2020-08-24 14:29:00 -07:00
|
|
|
|
/// <param name="machineObj">JObject representing a single datitem</param>
|
2020-07-15 09:41:59 -07:00
|
|
|
|
/// <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>
|
2020-06-16 11:27:36 -07:00
|
|
|
|
private void ReadItem(
|
2020-08-24 14:29:00 -07:00
|
|
|
|
JObject itemObj,
|
2020-06-16 11:27:36 -07:00
|
|
|
|
|
2020-07-15 09:41:59 -07:00
|
|
|
|
// Standard Dat parsing
|
|
|
|
|
|
string filename,
|
|
|
|
|
|
int indexId,
|
|
|
|
|
|
|
2020-06-16 11:27:36 -07:00
|
|
|
|
// Miscellaneous
|
|
|
|
|
|
Machine machine)
|
|
|
|
|
|
{
|
2020-08-24 14:29:00 -07:00
|
|
|
|
// If we have an empty item, skip it
|
|
|
|
|
|
if (itemObj == null)
|
2020-06-16 11:27:36 -07:00
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Prepare internal variables
|
2020-08-24 11:56:49 -07:00
|
|
|
|
DatItem datItem = null;
|
2020-06-16 11:27:36 -07:00
|
|
|
|
|
2020-08-24 14:29:00 -07:00
|
|
|
|
// Read the datitem info, if possible
|
|
|
|
|
|
if (itemObj.ContainsKey("datitem"))
|
2020-06-16 11:27:36 -07:00
|
|
|
|
{
|
2020-08-24 14:29:00 -07:00
|
|
|
|
JToken datItemObj = itemObj["datitem"];
|
|
|
|
|
|
switch (datItemObj.Value<string>("type").AsItemType())
|
2020-06-16 11:27:36 -07:00
|
|
|
|
{
|
2020-09-01 11:34:52 -07:00
|
|
|
|
case ItemType.Adjuster:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Adjuster>();
|
|
|
|
|
|
break;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
case ItemType.Analog:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Analog>();
|
|
|
|
|
|
break;
|
2020-08-24 14:29:00 -07:00
|
|
|
|
case ItemType.Archive:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Archive>();
|
2020-06-16 11:27:36 -07:00
|
|
|
|
break;
|
2020-08-24 14:29:00 -07:00
|
|
|
|
case ItemType.BiosSet:
|
|
|
|
|
|
datItem = datItemObj.ToObject<BiosSet>();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ItemType.Blank:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Blank>();
|
|
|
|
|
|
break;
|
2020-08-25 22:48:46 -07:00
|
|
|
|
case ItemType.Chip:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Chip>();
|
|
|
|
|
|
break;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
case ItemType.Condition:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Condition>();
|
|
|
|
|
|
break;
|
2020-09-01 12:04:35 -07:00
|
|
|
|
case ItemType.Configuration:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Configuration>();
|
|
|
|
|
|
break;
|
2020-09-02 17:09:19 -07:00
|
|
|
|
case ItemType.Device:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Device>();
|
|
|
|
|
|
break;
|
2020-08-31 23:01:51 -07:00
|
|
|
|
case ItemType.DeviceReference:
|
|
|
|
|
|
datItem = datItemObj.ToObject<DeviceReference>();
|
|
|
|
|
|
break;
|
2020-09-01 13:36:32 -07:00
|
|
|
|
case ItemType.DipSwitch:
|
|
|
|
|
|
datItem = datItemObj.ToObject<DipSwitch>();
|
|
|
|
|
|
break;
|
2020-08-24 14:29:00 -07:00
|
|
|
|
case ItemType.Disk:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Disk>();
|
|
|
|
|
|
break;
|
2020-09-02 15:38:10 -07:00
|
|
|
|
case ItemType.Driver:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Driver>();
|
|
|
|
|
|
break;
|
2020-09-02 16:37:01 -07:00
|
|
|
|
case ItemType.Extension:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Extension>();
|
|
|
|
|
|
break;
|
2020-09-02 13:31:50 -07:00
|
|
|
|
case ItemType.Feature:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Feature>();
|
|
|
|
|
|
break;
|
2020-09-02 16:46:17 -07:00
|
|
|
|
case ItemType.Instance:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Instance>();
|
|
|
|
|
|
break;
|
2020-08-27 16:57:22 -07:00
|
|
|
|
case ItemType.Media:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Media>();
|
|
|
|
|
|
break;
|
2020-09-01 11:34:52 -07:00
|
|
|
|
case ItemType.RamOption:
|
|
|
|
|
|
datItem = datItemObj.ToObject<RamOption>();
|
|
|
|
|
|
break;
|
2020-08-24 14:29:00 -07:00
|
|
|
|
case ItemType.Release:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Release>();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ItemType.Rom:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Rom>();
|
|
|
|
|
|
break;
|
|
|
|
|
|
case ItemType.Sample:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Sample>();
|
2020-08-20 21:15:37 -07:00
|
|
|
|
break;
|
2020-09-01 16:21:55 -07:00
|
|
|
|
case ItemType.Slot:
|
|
|
|
|
|
datItem = datItemObj.ToObject<Slot>();
|
|
|
|
|
|
break;
|
2020-08-31 23:26:07 -07:00
|
|
|
|
case ItemType.SoftwareList:
|
|
|
|
|
|
datItem = datItemObj.ToObject<DatItems.SoftwareList>();
|
|
|
|
|
|
break;
|
2020-09-02 12:51:21 -07:00
|
|
|
|
case ItemType.Sound:
|
2020-09-02 13:31:50 -07:00
|
|
|
|
datItem = datItemObj.ToObject<Sound>();
|
2020-09-02 12:51:21 -07:00
|
|
|
|
break;
|
2020-08-24 11:56:49 -07:00
|
|
|
|
}
|
2020-08-24 14:29:00 -07:00
|
|
|
|
}
|
2020-08-20 21:15:37 -07:00
|
|
|
|
|
2020-08-24 14:29:00 -07:00
|
|
|
|
// If we got a valid datitem, copy machine info and add
|
|
|
|
|
|
if (datItem != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
datItem.CopyMachineInformation(machine);
|
|
|
|
|
|
datItem.Source = new Source { Index = indexId, Name = filename };
|
|
|
|
|
|
ParseAddHelper(datItem);
|
2020-08-24 11:56:49 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-08-20 21:15:37 -07:00
|
|
|
|
|
2020-08-24 11:56: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>
|
|
|
|
|
|
public override bool WriteToFile(string outfile, bool ignoreblanks = false)
|
|
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.User($"Opening file for writing: {outfile}");
|
|
|
|
|
|
FileStream fs = FileExtensions.TryCreate(outfile);
|
2020-08-21 23:48:35 -07:00
|
|
|
|
|
2020-08-24 11:56:49 -07:00
|
|
|
|
// 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;
|
|
|
|
|
|
}
|
2020-06-15 22:31:46 -07:00
|
|
|
|
|
|
|
|
|
|
StreamWriter sw = new StreamWriter(fs, new UTF8Encoding(false));
|
2020-07-15 09:41:59 -07:00
|
|
|
|
JsonTextWriter jtw = new JsonTextWriter(sw)
|
|
|
|
|
|
{
|
|
|
|
|
|
Formatting = Formatting.Indented,
|
|
|
|
|
|
IndentChar = '\t',
|
|
|
|
|
|
Indentation = 1
|
|
|
|
|
|
};
|
2020-06-15 22:31:46 -07:00
|
|
|
|
|
|
|
|
|
|
// Write out the header
|
|
|
|
|
|
WriteHeader(jtw);
|
|
|
|
|
|
|
|
|
|
|
|
// Write out each of the machines and roms
|
|
|
|
|
|
string lastgame = null;
|
|
|
|
|
|
|
2020-07-26 21:00:30 -07:00
|
|
|
|
// Use a sorted list of games to output
|
2020-07-26 22:34:45 -07:00
|
|
|
|
foreach (string key in Items.SortedKeys)
|
2020-06-15 22:31:46 -07:00
|
|
|
|
{
|
2020-08-28 15:06:07 -07:00
|
|
|
|
List<DatItem> datItems = Items.FilteredItems(key);
|
2020-06-15 22:31:46 -07:00
|
|
|
|
|
|
|
|
|
|
// Resolve the names in the block
|
2020-08-28 15:06:07 -07:00
|
|
|
|
datItems = DatItem.ResolveNames(datItems);
|
2020-06-15 22:31:46 -07:00
|
|
|
|
|
2020-08-28 15:06:07 -07:00
|
|
|
|
for (int index = 0; index < datItems.Count; index++)
|
2020-06-15 22:31:46 -07:00
|
|
|
|
{
|
2020-08-28 15:06:07 -07:00
|
|
|
|
DatItem datItem = datItems[index];
|
2020-06-15 22:31:46 -07:00
|
|
|
|
|
|
|
|
|
|
// If we have a different game and we're not at the start of the list, output the end of last item
|
2020-08-28 15:06:07 -07:00
|
|
|
|
if (lastgame != null && lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
|
2020-06-15 22:31:46 -07:00
|
|
|
|
WriteEndGame(jtw);
|
|
|
|
|
|
|
|
|
|
|
|
// If we have a new game, output the beginning of the new item
|
2020-08-28 15:06:07 -07:00
|
|
|
|
if (lastgame == null || lastgame.ToLowerInvariant() != datItem.Machine.Name.ToLowerInvariant())
|
|
|
|
|
|
WriteStartGame(jtw, datItem);
|
|
|
|
|
|
|
|
|
|
|
|
// Check for a "null" item
|
|
|
|
|
|
datItem = ProcessNullifiedItem(datItem);
|
|
|
|
|
|
|
|
|
|
|
|
// Write out the item if we're not ignoring
|
|
|
|
|
|
if (!ShouldIgnore(datItem, ignoreblanks))
|
|
|
|
|
|
WriteDatItem(jtw, datItem);
|
2020-06-15 22:31:46 -07:00
|
|
|
|
|
|
|
|
|
|
// Set the new data to compare against
|
2020-08-28 15:06:07 -07:00
|
|
|
|
lastgame = datItem.Machine.Name;
|
2020-06-15 22:31:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 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>
|
2020-08-24 20:23:57 -07:00
|
|
|
|
/// Write out DAT header using the supplied JsonTextWriter
|
2020-06-15 22:31:46 -07:00
|
|
|
|
/// </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();
|
|
|
|
|
|
|
2020-08-24 11:56:49 -07:00
|
|
|
|
// Write the DatHeader
|
2020-06-15 22:31:46 -07:00
|
|
|
|
jtw.WritePropertyName("header");
|
2020-08-24 11:56:49 -07:00
|
|
|
|
JsonSerializer js = new JsonSerializer() { Formatting = Formatting.Indented };
|
|
|
|
|
|
js.Serialize(jtw, Header);
|
2020-06-15 22:31:46 -07:00
|
|
|
|
|
|
|
|
|
|
jtw.WritePropertyName("machines");
|
|
|
|
|
|
jtw.WriteStartArray();
|
|
|
|
|
|
|
|
|
|
|
|
jtw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-24 20:23:57 -07:00
|
|
|
|
/// Write out Game start using the supplied JsonTextWriter
|
2020-06-15 22:31:46 -07:00
|
|
|
|
/// </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
|
2020-08-20 13:17:14 -07:00
|
|
|
|
datItem.Machine.Name = datItem.Machine.Name.TrimStart(Path.DirectorySeparatorChar);
|
2020-06-15 22:31:46 -07:00
|
|
|
|
|
2020-08-23 22:23:55 -07:00
|
|
|
|
// Build the state
|
2020-06-15 22:31:46 -07:00
|
|
|
|
jtw.WriteStartObject();
|
|
|
|
|
|
|
2020-08-24 11:56:49 -07:00
|
|
|
|
// Write the Machine
|
|
|
|
|
|
jtw.WritePropertyName("machine");
|
|
|
|
|
|
JsonSerializer js = new JsonSerializer() { Formatting = Formatting.Indented };
|
|
|
|
|
|
js.Serialize(jtw, datItem.Machine);
|
2020-08-20 22:42:04 -07:00
|
|
|
|
|
2020-06-15 22:31:46 -07:00
|
|
|
|
jtw.WritePropertyName("items");
|
|
|
|
|
|
jtw.WriteStartArray();
|
|
|
|
|
|
|
|
|
|
|
|
jtw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-24 20:23:57 -07:00
|
|
|
|
/// Write out Game end using the supplied JsonTextWriter
|
2020-06-15 22:31:46 -07:00
|
|
|
|
/// </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>
|
2020-08-24 20:23:57 -07:00
|
|
|
|
/// Write out DatItem using the supplied JsonTextWriter
|
2020-06-15 22:31:46 -07:00
|
|
|
|
/// </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>
|
2020-08-28 15:06:07 -07:00
|
|
|
|
private bool WriteDatItem(JsonTextWriter jtw, DatItem datItem)
|
2020-06-15 22:31:46 -07:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
// Pre-process the item name
|
|
|
|
|
|
ProcessItemName(datItem, true);
|
|
|
|
|
|
|
2020-08-23 22:23:55 -07:00
|
|
|
|
// Build the state
|
2020-06-15 22:31:46 -07:00
|
|
|
|
jtw.WriteStartObject();
|
|
|
|
|
|
|
2020-08-24 11:56:49 -07:00
|
|
|
|
// Write the DatItem
|
|
|
|
|
|
jtw.WritePropertyName("datitem");
|
|
|
|
|
|
JsonSerializer js = new JsonSerializer() { ContractResolver = new BaseFirstContractResolver(), Formatting = Formatting.Indented };
|
|
|
|
|
|
js.Serialize(jtw, datItem);
|
2020-08-21 23:48:35 -07:00
|
|
|
|
|
2020-06-15 22:31:46 -07:00
|
|
|
|
// End item
|
|
|
|
|
|
jtw.WriteEndObject();
|
|
|
|
|
|
|
|
|
|
|
|
jtw.Flush();
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
{
|
|
|
|
|
|
Globals.Logger.Error(ex.ToString());
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2020-08-24 20:23:57 -07:00
|
|
|
|
/// Write out DAT footer using the supplied JsonTextWriter
|
2020-06-15 22:31:46 -07:00
|
|
|
|
/// </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;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|