Make IDDB ParseAddHelper implementation use source index

This commit is contained in:
Matt Nadareski
2024-03-20 01:15:07 -04:00
parent ea659cd691
commit b48fbcc04d
4 changed files with 221 additions and 212 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -207,10 +207,11 @@ namespace SabreTools.DatFiles
/// </summary> /// </summary>
/// <param name="item">Item data to check against</param> /// <param name="item">Item data to check against</param>
/// <param name="machineIndex">Index of the machine to map the DatItem to</param> /// <param name="machineIndex">Index of the machine to map the DatItem to</param>
/// <param name="sourceIndex">Index of the source to map the DatItem to</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param> /// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <returns>The key for the item</returns> /// <returns>The key for the item</returns>
protected long ParseAddHelper(DatItem item, long machineIndex, bool statsOnly) protected long ParseAddHelper(DatItem item, long machineIndex, long sourceIndex, bool statsOnly)
=> ItemsDB.AddItem(item, machineIndex, statsOnly: statsOnly); => ItemsDB.AddItem(item, machineIndex, sourceIndex, statsOnly);
#endregion #endregion

View File

@@ -31,8 +31,10 @@ namespace SabreTools.DatFiles.Formats
public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false) public override void ParseFile(string filename, int indexId, bool keep, bool statsOnly = false, bool throwOnError = false)
{ {
// Prepare all internal variables // Prepare all internal variables
StreamReader sr = new(System.IO.File.OpenRead(filename), new UTF8Encoding(false)); var sr = new StreamReader(System.IO.File.OpenRead(filename), new UTF8Encoding(false));
JsonTextReader jtr = new(sr); var jtr = new JsonTextReader(sr);
var source = new Source { Index = indexId, Name = filename };
long sourceIndex = ItemsDB.AddSource(source);
// If we got a null reader, just return // If we got a null reader, just return
if (jtr == null) if (jtr == null)
@@ -61,7 +63,7 @@ namespace SabreTools.DatFiles.Formats
// Machine array // Machine array
case "machines": case "machines":
ReadMachines(jtr, statsOnly, filename, indexId); ReadMachines(jtr, statsOnly, source, sourceIndex);
jtr.Read(); jtr.Read();
break; break;
@@ -99,11 +101,11 @@ namespace SabreTools.DatFiles.Formats
/// <summary> /// <summary>
/// Read machine array information /// Read machine array information
/// </summary> /// </summary>
/// <param name="itr">JsonTextReader to use to parse the machine</param> /// <param name="jtr">JsonTextReader to use to parse the machine</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param> /// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="source">Source representing the DAT</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="sourceIndex">Index of the Source representing the DAT</param>
private void ReadMachines(JsonTextReader jtr, bool statsOnly, string filename, int indexId) private void ReadMachines(JsonTextReader jtr, bool statsOnly, Source source, long sourceIndex)
{ {
// If the reader is invalid, skip // If the reader is invalid, skip
if (jtr == null) if (jtr == null)
@@ -117,7 +119,7 @@ namespace SabreTools.DatFiles.Formats
// Loop through each machine object and process // Loop through each machine object and process
foreach (JObject machineObj in (machineArray ?? []).Cast<JObject>()) foreach (JObject machineObj in (machineArray ?? []).Cast<JObject>())
{ {
ReadMachine(machineObj, statsOnly, filename, indexId); ReadMachine(machineObj, statsOnly, source, sourceIndex);
} }
} }
@@ -126,9 +128,9 @@ namespace SabreTools.DatFiles.Formats
/// </summary> /// </summary>
/// <param name="machineObj">JObject representing a single machine</param> /// <param name="machineObj">JObject representing a single machine</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param> /// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="source">Source representing the DAT</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="sourceIndex">Index of the Source representing the DAT</param>
private void ReadMachine(JObject machineObj, bool statsOnly, string filename, int indexId) private void ReadMachine(JObject machineObj, bool statsOnly, Source source, long sourceIndex)
{ {
// If object is invalid, skip it // If object is invalid, skip it
if (machineObj == null) if (machineObj == null)
@@ -148,7 +150,7 @@ namespace SabreTools.DatFiles.Formats
// Read items, if possible // Read items, if possible
if (machineObj.ContainsKey("items")) if (machineObj.ContainsKey("items"))
ReadItems(machineObj["items"] as JArray, statsOnly, filename, indexId, machine, machineIndex); ReadItems(machineObj["items"] as JArray, statsOnly, source, sourceIndex, machine, machineIndex);
} }
/// <summary> /// <summary>
@@ -156,8 +158,8 @@ namespace SabreTools.DatFiles.Formats
/// </summary> /// </summary>
/// <param name="itemsArr">JArray representing the items list</param> /// <param name="itemsArr">JArray representing the items list</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param> /// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="source">Source representing the DAT</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="sourceIndex">Index of the Source representing the DAT</param>
/// <param name="machine">Machine information to add to the parsed items</param> /// <param name="machine">Machine information to add to the parsed items</param>
/// <param name="machineIndex">Index of the Machine to add to the parsed items</param> /// <param name="machineIndex">Index of the Machine to add to the parsed items</param>
private void ReadItems( private void ReadItems(
@@ -165,8 +167,8 @@ namespace SabreTools.DatFiles.Formats
bool statsOnly, bool statsOnly,
// Standard Dat parsing // Standard Dat parsing
string filename, Source source,
int indexId, long sourceIndex,
// Miscellaneous // Miscellaneous
Machine? machine, Machine? machine,
@@ -179,7 +181,7 @@ namespace SabreTools.DatFiles.Formats
// Loop through each datitem object and process // Loop through each datitem object and process
foreach (JObject itemObj in itemsArr.Cast<JObject>()) foreach (JObject itemObj in itemsArr.Cast<JObject>())
{ {
ReadItem(itemObj, statsOnly, filename, indexId, machine, machineIndex); ReadItem(itemObj, statsOnly, source, sourceIndex, machine, machineIndex);
} }
} }
@@ -188,8 +190,8 @@ namespace SabreTools.DatFiles.Formats
/// </summary> /// </summary>
/// <param name="itemObj">JObject representing a single datitem</param> /// <param name="itemObj">JObject representing a single datitem</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param> /// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="source">Source representing the DAT</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="sourceIndex">Index of the Source representing the DAT</param>
/// <param name="machine">Machine information to add to the parsed items</param> /// <param name="machine">Machine information to add to the parsed items</param>
/// <param name="machineIndex">Index of the Machine to add to the parsed items</param> /// <param name="machineIndex">Index of the Machine to add to the parsed items</param>
private void ReadItem( private void ReadItem(
@@ -197,8 +199,8 @@ namespace SabreTools.DatFiles.Formats
bool statsOnly, bool statsOnly,
// Standard Dat parsing // Standard Dat parsing
string filename, Source source,
int indexId, long sourceIndex,
// Miscellaneous // Miscellaneous
Machine? machine, Machine? machine,
@@ -353,9 +355,9 @@ namespace SabreTools.DatFiles.Formats
if (datItem != null) if (datItem != null)
{ {
datItem.CopyMachineInformation(machine); datItem.CopyMachineInformation(machine);
datItem.SetFieldValue<Source?>(DatItem.SourceKey, new Source { Index = indexId, Name = filename }); datItem.SetFieldValue<Source?>(DatItem.SourceKey, source);
ParseAddHelper(datItem, statsOnly); ParseAddHelper(datItem, statsOnly);
ParseAddHelper(datItem, machineIndex, statsOnly); ParseAddHelper(datItem, machineIndex, sourceIndex, statsOnly);
} }
} }

View File

@@ -38,6 +38,8 @@ namespace SabreTools.DatFiles.Formats
ValidationFlags = XmlSchemaValidationFlags.None, ValidationFlags = XmlSchemaValidationFlags.None,
ValidationType = ValidationType.None, ValidationType = ValidationType.None,
}); });
var source = new Source { Index = indexId, Name = filename };
long sourceIndex = ItemsDB.AddSource(source);
// If we got a null reader, just return // If we got a null reader, just return
if (xtr == null) if (xtr == null)
@@ -66,7 +68,7 @@ namespace SabreTools.DatFiles.Formats
break; break;
case "directory": case "directory":
ReadDirectory(xtr.ReadSubtree(), statsOnly, filename, indexId); ReadDirectory(xtr.ReadSubtree(), statsOnly, source, sourceIndex);
// Skip the directory node now that we've processed it // Skip the directory node now that we've processed it
xtr.Read(); xtr.Read();
@@ -95,9 +97,9 @@ namespace SabreTools.DatFiles.Formats
/// </summary> /// </summary>
/// <param name="xtr">XmlReader to use to parse the header</param> /// <param name="xtr">XmlReader to use to parse the header</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param> /// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="source">Source representing the DAT</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="sourceIndex">Index of the Source representing the DAT</param>
private void ReadDirectory(XmlReader xtr, bool statsOnly, string filename, int indexId) private void ReadDirectory(XmlReader xtr, bool statsOnly, Source source, long sourceIndex)
{ {
// If the reader is invalid, skip // If the reader is invalid, skip
if (xtr == null) if (xtr == null)
@@ -130,7 +132,7 @@ namespace SabreTools.DatFiles.Formats
break; break;
case "files": case "files":
ReadFiles(xtr.ReadSubtree(), machine, machineIndex, statsOnly, filename, indexId); ReadFiles(xtr.ReadSubtree(), machine, machineIndex, statsOnly, source, sourceIndex);
// Skip the directory node now that we've processed it // Skip the directory node now that we've processed it
xtr.Read(); xtr.Read();
@@ -149,9 +151,9 @@ namespace SabreTools.DatFiles.Formats
/// <param name="machine">Machine to copy information from</param> /// <param name="machine">Machine to copy information from</param>
/// <param name="machineIndex">Index of the Machine to add to the parsed items</param> /// <param name="machineIndex">Index of the Machine to add to the parsed items</param>
/// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param> /// <param name="statsOnly">True to only add item statistics while parsing, false otherwise</param>
/// <param name="filename">Name of the file to be parsed</param> /// <param name="source">Source representing the DAT</param>
/// <param name="indexId">Index ID for the DAT</param> /// <param name="sourceIndex">Index of the Source representing the DAT</param>
private void ReadFiles(XmlReader xtr, Machine? machine, long machineIndex, bool statsOnly, string filename, int indexId) private void ReadFiles(XmlReader xtr, Machine? machine, long machineIndex, bool statsOnly, Source source, long sourceIndex)
{ {
// If the reader is invalid, skip // If the reader is invalid, skip
if (xtr == null) if (xtr == null)
@@ -175,9 +177,9 @@ namespace SabreTools.DatFiles.Formats
if (xs.Deserialize(xtr.ReadSubtree()) is DatItem item) if (xs.Deserialize(xtr.ReadSubtree()) is DatItem item)
{ {
item.CopyMachineInformation(machine); item.CopyMachineInformation(machine);
item.SetFieldValue<Source?>(DatItem.SourceKey, new Source { Index = indexId, Name = filename }); item.SetFieldValue<Source?>(DatItem.SourceKey, source);
ParseAddHelper(item, statsOnly); ParseAddHelper(item, statsOnly);
ParseAddHelper(item, machineIndex, statsOnly); ParseAddHelper(item, machineIndex, sourceIndex, statsOnly);
} }
xtr.Skip(); xtr.Skip();
break; break;