Files
SabreTools/SabreTools.DatFiles/Setter.cs

474 lines
16 KiB
C#
Raw Normal View History

2021-02-01 14:07:50 -08:00
using System.Collections.Generic;
2024-03-05 17:17:40 -05:00
using System.Linq;
using SabreTools.Core.Filter;
2021-02-01 14:07:50 -08:00
using SabreTools.Core.Tools;
using SabreTools.DatItems;
2021-02-02 10:23:43 -08:00
using SabreTools.DatItems.Formats;
2021-02-01 14:07:50 -08:00
using SabreTools.Logging;
namespace SabreTools.DatFiles
{
/// <summary>
/// Set fields on DatItems
/// </summary>
2021-02-01 14:17:25 -08:00
/// TODO: Figure out how to get this into the Filtering namespace
2021-02-01 14:07:50 -08:00
public class Setter
{
#region Fields
/// <summary>
/// Mappings to set DatHeader fields
/// </summary>
2024-03-05 17:17:40 -05:00
public Dictionary<string, string> HeaderFieldMappings { get; } = [];
2021-02-01 14:07:50 -08:00
/// <summary>
/// Mappings to set Machine fields
/// </summary>
2024-03-05 17:17:40 -05:00
public Dictionary<string, string> MachineFieldMappings { get; } = [];
2021-02-01 14:07:50 -08:00
/// <summary>
/// Mappings to set DatItem fields
/// </summary>
2024-03-05 17:17:40 -05:00
public Dictionary<(string, string), string> ItemFieldMappings { get; } = [];
2021-02-01 14:07:50 -08:00
#endregion
2023-03-26 21:47:17 -04:00
2021-02-01 14:07:50 -08:00
#region Logging
/// <summary>
/// Logging object
/// </summary>
private readonly Logger logger = new();
2021-02-01 14:07:50 -08:00
#endregion
#region Population
2024-03-05 17:17:40 -05:00
/// <summary>
/// Populate the setters using a field name and a value
/// </summary>
/// <param name="field">Field name</param>
/// <param name="value">Field value</param>
public void PopulateSetters(string field, string value)
=> PopulateSettersFromList([field], [value]);
2021-02-01 14:07:50 -08:00
/// <summary>
/// Populate the setters using a set of field names
/// </summary>
/// <param name="fields">List of field names</param>
2024-03-05 17:17:40 -05:00
/// <param name="values">List of field values</param>
public void PopulateSettersFromList(List<string> fields, List<string> values)
2021-02-01 14:07:50 -08:00
{
// If the list is null or empty, just return
2024-03-05 17:17:40 -05:00
if (values == null || values.Count == 0)
2021-02-01 14:07:50 -08:00
return;
2024-03-05 17:17:40 -05:00
var watch = new InternalStopwatch("Populating setters from list");
2021-02-01 14:07:50 -08:00
// Now we loop through and get values for everything
2024-03-05 17:17:40 -05:00
for (int i = 0; i < fields.Count; i++)
2021-02-01 14:07:50 -08:00
{
string field = fields[i];
2024-03-05 17:17:40 -05:00
string value = values[i];
2021-02-01 14:07:50 -08:00
2024-03-05 17:17:40 -05:00
if (!SetSetter(field, value))
logger.Warning($"The value {value} did not match any known field names. Please check the wiki for more details on supported field names.");
}
2021-02-01 14:07:50 -08:00
2024-03-05 17:17:40 -05:00
watch.Stop();
}
2024-03-05 17:33:02 -05:00
/// <summary>
/// Populate the setters using a set of field names
/// </summary>
/// <param name="mappings">Dictionary of mappings</param>
public void PopulateSettersFromDictionary(Dictionary<(string, string), string>? mappings)
{
// If the dictionary is null or empty, just return
if (mappings == null || mappings.Count == 0)
return;
var watch = new InternalStopwatch("Populating setters from dictionary");
// Now we loop through and get values for everything
foreach (var mapping in mappings)
{
string field = $"{mapping.Key.Item1}.{mapping.Key.Item2}";
string value = mapping.Value;
if (!SetSetter(field, value))
logger.Warning($"The value {value} did not match any known field names. Please check the wiki for more details on supported field names.");
}
watch.Stop();
}
2024-03-05 17:17:40 -05:00
/// <summary>
/// Set remover from a value
/// </summary>
/// <param name="field">Key for the remover to be set</param>
private bool SetSetter(string field, string value)
{
// If the key is null or empty, return false
if (string.IsNullOrEmpty(field))
return false;
// Get the parser pair out of it, if possible
(string? type, string? key) = FilterParser.ParseFilterId(field);
if (type == null || key == null)
return false;
2021-02-01 14:07:50 -08:00
2024-03-05 17:17:40 -05:00
switch (type)
{
case Models.Metadata.MetadataFile.HeaderKey:
HeaderFieldMappings[key] = value;
return true;
case Models.Metadata.MetadataFile.MachineKey:
MachineFieldMappings[key] = value;
return true;
default:
ItemFieldMappings[(type, key)] = value;
return true;
2021-02-01 14:07:50 -08:00
}
}
#endregion
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="datHeader">DatHeader to set fields on</param>
public void SetFields(DatHeader datHeader)
{
2024-03-05 17:17:40 -05:00
// If we have an invalid input, return
if (datHeader == null || HeaderFieldMappings.Count == 0)
2021-02-01 14:07:50 -08:00
return;
2024-03-06 00:33:45 -05:00
foreach (var kvp in HeaderFieldMappings)
2024-03-05 17:17:40 -05:00
{
2024-03-06 00:33:45 -05:00
datHeader.SetField(kvp.Key, kvp.Value);
2024-03-05 17:17:40 -05:00
}
}
2021-02-01 14:07:50 -08:00
2024-03-05 17:17:40 -05:00
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="machine">Machine to set fields on</param>
public void SetFields(Machine? machine)
{
// If we have an invalid input, return
if (machine == null || MachineFieldMappings.Count == 0)
2024-03-05 17:17:40 -05:00
return;
2021-02-01 14:07:50 -08:00
2024-03-05 17:17:40 -05:00
foreach (var kvp in MachineFieldMappings)
{
machine.SetField(kvp.Key, kvp.Value);
}
2021-02-01 14:07:50 -08:00
}
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="datItem">DatItem to set fields on</param>
public void SetFields(DatItem datItem)
{
2024-03-05 20:07:38 -05:00
// If we have an invalid input, return
2024-03-05 17:17:40 -05:00
if (datItem == null)
2021-02-01 14:07:50 -08:00
return;
#region Common
2021-02-01 14:07:50 -08:00
2024-03-05 17:17:40 -05:00
// Handle Machine fields
if (MachineFieldMappings.Count > 0 && datItem.GetFieldValue<Machine>(DatItem.MachineKey) != null)
2024-03-10 16:49:07 -04:00
SetFields(datItem.GetFieldValue<Machine>(DatItem.MachineKey)!);
2024-03-05 17:17:40 -05:00
// If there are no field names, return
if (ItemFieldMappings == null || ItemFieldMappings.Count == 0)
2024-03-05 17:17:40 -05:00
return;
// If there are no field names for this type or generic, return
2024-03-11 16:26:28 -04:00
string? itemType = datItem.GetStringFieldValue(Models.Metadata.DatItem.TypeKey).AsEnumValue<ItemType>().AsStringValue();
2024-03-05 17:17:40 -05:00
if (itemType == null || (!ItemFieldMappings.Keys.Any(kvp => kvp.Item1 == itemType) && !ItemFieldMappings.Keys.Any(kvp => kvp.Item1 == "item")))
return;
// Get the combined list of fields to remove
var fieldMappings = new Dictionary<string, string>();
foreach (var mapping in ItemFieldMappings.Where(kvp => kvp.Key.Item1 == "item").ToDictionary(kvp => kvp.Key.Item2, kvp => kvp.Value))
{
fieldMappings[mapping.Key] = mapping.Value;
}
foreach (var mapping in ItemFieldMappings.Where(kvp => kvp.Key.Item1 == itemType).ToDictionary(kvp => kvp.Key.Item2, kvp => kvp.Value))
{
fieldMappings[mapping.Key] = mapping.Value;
}
// If the field specifically contains Name, set it separately
if (fieldMappings.Keys.Contains(Models.Metadata.Rom.NameKey))
2024-03-10 20:39:54 -04:00
{
2024-03-05 17:17:40 -05:00
datItem.SetName(fieldMappings[Models.Metadata.Rom.NameKey]);
2024-03-10 20:39:54 -04:00
fieldMappings.Remove(Models.Metadata.Rom.NameKey);
}
2021-02-01 14:07:50 -08:00
#endregion
#region Item-Specific
2024-03-05 02:20:12 -05:00
// Handle unnested sets first
2024-03-05 17:17:40 -05:00
foreach (var kvp in fieldMappings)
2024-03-05 02:20:12 -05:00
{
datItem.SetField(kvp.Key, kvp.Value);
}
2024-03-05 17:17:40 -05:00
2024-03-05 02:20:12 -05:00
// Handle nested sets
switch (datItem)
{
case Adjuster adjuster: SetFields(adjuster); break;
case Configuration condition: SetFields(condition); break;
case ConfSetting confSetting: SetFields(confSetting); break;
case Device device: SetFields(device); break;
case DipSwitch dipSwitch: SetFields(dipSwitch); break;
case DipValue dipValue: SetFields(dipValue); break;
case Disk disk: SetFields(disk); break;
case Input input: SetFields(input); break;
case Part part: SetFields(part); break;
case Port port: SetFields(port); break;
case Rom rom: SetFields(rom); break;
case Slot slot: SetFields(slot); break;
}
2021-02-01 14:07:50 -08:00
#endregion
}
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="adjuster">Adjuster to remove replace fields in</param>
private void SetFields(Adjuster adjuster)
{
// Field.DatItem_Conditions does not apply here
if (adjuster.ConditionsSpecified)
{
2024-03-08 21:12:13 -05:00
foreach (Condition subCondition in adjuster.GetFieldValue<Condition[]?>(Models.Metadata.Adjuster.ConditionKey)!)
2021-02-01 14:07:50 -08:00
{
2024-03-05 02:20:12 -05:00
SetFields(subCondition);
2021-02-01 14:07:50 -08:00
}
}
}
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="configuration">Configuration to remove replace fields in</param>
private void SetFields(Configuration configuration)
{
if (configuration.ConditionsSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (Condition subCondition in configuration.GetFieldValue<Condition[]?>(Models.Metadata.Configuration.ConditionKey)!)
2021-02-01 14:07:50 -08:00
{
2024-03-05 02:20:12 -05:00
SetFields(subCondition);
2021-02-01 14:07:50 -08:00
}
}
if (configuration.LocationsSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (ConfLocation subLocation in configuration.GetFieldValue<ConfLocation[]?>(Models.Metadata.Configuration.ConfLocationKey)!)
2021-02-01 14:07:50 -08:00
{
SetFields(subLocation);
}
}
if (configuration.SettingsSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (ConfSetting subSetting in configuration.GetFieldValue<ConfSetting[]?>(Models.Metadata.Configuration.ConfSettingKey)!)
2021-02-01 14:07:50 -08:00
{
2024-03-05 02:20:12 -05:00
SetFields(subSetting as DatItem);
2021-02-01 14:07:50 -08:00
}
}
}
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="confSetting">ConfSetting to remove replace fields in</param>
private void SetFields(ConfSetting confSetting)
{
if (confSetting.ConditionsSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (Condition subCondition in confSetting.GetFieldValue<Condition[]?>(Models.Metadata.ConfSetting.ConditionKey)!)
{
2024-03-05 02:20:12 -05:00
SetFields(subCondition);
}
}
}
2021-02-01 14:07:50 -08:00
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="device">Device to remove replace fields in</param>
private void SetFields(Device device)
{
if (device.ExtensionsSpecified)
2021-02-01 14:07:50 -08:00
{
2024-03-09 21:34:26 -05:00
foreach (Extension subExtension in device.GetFieldValue<Extension[]?>(Models.Metadata.Device.ExtensionKey)!)
2021-02-01 14:07:50 -08:00
{
SetFields(subExtension);
2021-02-01 14:07:50 -08:00
}
}
if (device.InstancesSpecified)
2021-02-01 14:07:50 -08:00
{
2024-03-09 21:34:26 -05:00
foreach (Instance subInstance in device.GetFieldValue<Instance[]?>(Models.Metadata.Device.InstanceKey)!)
2021-02-01 14:07:50 -08:00
{
SetFields(subInstance);
2021-02-01 14:07:50 -08:00
}
}
}
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="dipSwitch">DipSwitch to remove replace fields in</param>
private void SetFields(DipSwitch dipSwitch)
{
if (dipSwitch.ConditionsSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (Condition subCondition in dipSwitch.GetFieldValue<Condition[]?>(Models.Metadata.DipSwitch.ConditionKey)!)
2021-02-01 14:07:50 -08:00
{
2024-03-05 02:20:12 -05:00
SetFields(subCondition);
2021-02-01 14:07:50 -08:00
}
}
if (dipSwitch.LocationsSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (DipLocation subLocation in dipSwitch.GetFieldValue<DipLocation[]?>(Models.Metadata.DipSwitch.DipLocationKey)!)
2021-02-01 14:07:50 -08:00
{
SetFields(subLocation);
}
}
if (dipSwitch.ValuesSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (DipValue subValue in dipSwitch.GetFieldValue<DipValue[]?>(Models.Metadata.DipSwitch.DipValueKey)!)
2021-02-01 14:07:50 -08:00
{
2024-03-05 02:20:12 -05:00
SetFields(subValue as DatItem);
2021-02-01 14:07:50 -08:00
}
}
2024-03-09 21:34:26 -05:00
if (!dipSwitch.PartSpecified)
2024-03-10 16:49:07 -04:00
dipSwitch.SetFieldValue<Part?>(DipSwitch.PartKey, new Part());
2024-03-09 21:34:26 -05:00
2024-03-10 16:49:07 -04:00
SetFields((dipSwitch.GetFieldValue<Part?>(DipSwitch.PartKey) as DatItem)!);
2021-02-01 14:07:50 -08:00
}
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="dipValue">DipValue to remove replace fields in</param>
private void SetFields(DipValue dipValue)
{
if (dipValue.ConditionsSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (Condition subCondition in dipValue.GetFieldValue<Condition[]?>(Models.Metadata.DipValue.ConditionKey)!)
{
2024-03-05 02:20:12 -05:00
SetFields(subCondition);
}
}
}
2021-02-01 14:07:50 -08:00
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="disk">Disk to remove replace fields in</param>
private void SetFields(Disk disk)
{
2024-03-09 21:34:26 -05:00
if (!disk.DiskAreaSpecified)
2024-03-10 16:49:07 -04:00
disk.SetFieldValue<DiskArea?>(Disk.DiskAreaKey, new DiskArea());
2024-03-09 21:34:26 -05:00
2024-03-10 16:49:07 -04:00
SetFields(disk.GetFieldValue<DiskArea?>(Disk.DiskAreaKey)! as DatItem);
2024-03-09 21:34:26 -05:00
if (!disk.PartSpecified)
2024-03-10 16:49:07 -04:00
disk.SetFieldValue<Part?>(Disk.PartKey, new Part());
2021-02-01 14:07:50 -08:00
2024-03-10 16:49:07 -04:00
SetFields(disk.GetFieldValue<Part?>(Disk.PartKey)! as DatItem);
2021-02-01 14:07:50 -08:00
}
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="input">Input to remove replace fields in</param>
private void SetFields(Input input)
{
if (input.ControlsSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (Control subControl in input.GetFieldValue<Control[]?>(Models.Metadata.Input.ControlKey)!)
2021-02-01 14:07:50 -08:00
{
SetFields(subControl);
}
}
}
2024-03-05 02:20:12 -05:00
/// <summary>s
2021-02-01 14:07:50 -08:00
/// Set fields with given values
/// </summary>
/// <param name="part">Part to remove replace fields in</param>
private void SetFields(Part part)
{
if (part.FeaturesSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (PartFeature subPartFeature in part.GetFieldValue<PartFeature[]?>(Models.Metadata.Part.FeatureKey)!)
2021-02-01 14:07:50 -08:00
{
SetFields(subPartFeature);
}
}
}
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="port">Port to remove replace fields in</param>
private void SetFields(Port port)
{
if (port.AnalogsSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (Analog subAnalog in port.GetFieldValue<Analog[]?>(Models.Metadata.Port.AnalogKey)!)
2021-02-01 14:07:50 -08:00
{
SetFields(subAnalog);
}
}
}
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="rom">Rom to remove replace fields in</param>
private void SetFields(Rom rom)
{
2024-03-09 21:34:26 -05:00
if (!rom.DataAreaSpecified)
2024-03-10 16:49:07 -04:00
rom.SetFieldValue<DataArea?>(Rom.DataAreaKey, new DataArea());
2024-03-09 21:34:26 -05:00
2024-03-10 16:49:07 -04:00
SetFields(rom.GetFieldValue<DataArea?>(Rom.DataAreaKey)! as DatItem);
2024-03-09 21:34:26 -05:00
if (!rom.PartSpecified)
2024-03-10 16:49:07 -04:00
rom.SetFieldValue<Part?>(Rom.PartKey, new Part());
2021-02-01 14:07:50 -08:00
2024-03-10 16:49:07 -04:00
SetFields(rom.GetFieldValue<Part?>(Rom.PartKey)! as DatItem);
2021-02-01 14:07:50 -08:00
}
/// <summary>
/// Set fields with given values
/// </summary>
/// <param name="slot">Slot to remove replace fields in</param>
private void SetFields(Slot slot)
{
if (slot.SlotOptionsSpecified)
{
2024-03-09 21:34:26 -05:00
foreach (SlotOption subSlotOption in slot.GetFieldValue<SlotOption[]?>(Models.Metadata.Slot.SlotOptionKey)!)
2021-02-01 14:07:50 -08:00
{
SetFields(subSlotOption);
}
}
}
}
}