Files
SabreTools/SabreTools.Filtering/ExtraIni.cs

236 lines
7.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using SabreTools.Core.Filter;
using SabreTools.DatFiles;
using SabreTools.DatItems;
2024-10-24 00:36:44 -04:00
using SabreTools.IO.Logging;
2020-08-21 10:54:51 -07:00
2020-12-08 13:48:57 -08:00
namespace SabreTools.Filtering
2020-08-21 10:15:38 -07:00
{
public class ExtraIni
{
2020-08-21 10:38:42 -07:00
#region Fields
2020-08-21 10:15:38 -07:00
/// <summary>
/// List of extras to apply
/// </summary>
2024-10-24 02:47:30 -04:00
public readonly List<ExtraIniItem> Items = [];
2020-08-21 10:38:42 -07:00
#endregion
#region Logging
/// <summary>
/// Logging object
/// </summary>
private readonly Logger logger;
#endregion
#region Constructors
/// <summary>
/// Constructor
/// </summary>
public ExtraIni()
{
logger = new Logger(this);
}
#endregion
#region Population
2020-08-21 10:38:42 -07:00
/// <summary>
/// Populate item using field:file inputs
/// </summary>
/// <param name="inputs">Field and file combinations</param>
public void PopulateFromList(List<string> inputs)
{
2021-02-09 22:25:43 -08:00
// If there are no inputs, just skip
if (inputs == null || inputs.Count == 0)
2021-02-09 22:25:43 -08:00
return;
InternalStopwatch watch = new("Populating extras from list");
2021-02-02 14:09:49 -08:00
2020-08-21 10:38:42 -07:00
foreach (string input in inputs)
{
2020-08-21 10:54:51 -07:00
// If we don't even have a possible field and file combination
if (!input.Contains(":"))
2020-08-21 10:54:51 -07:00
{
logger.Warning($"'{input}` is not a valid INI extras string. Valid INI extras strings are of the form 'key:value'. Please refer to README.1ST or the help feature for more details.");
2020-08-21 10:54:51 -07:00
return;
}
string inputTrimmed = input.Trim('"', ' ', '\t');
string fieldString = inputTrimmed.Split(':')[0].ToLowerInvariant().Trim('"', ' ', '\t');
2024-02-28 23:09:31 -05:00
string fileString = inputTrimmed.Substring(fieldString.Length + 1).Trim('"', ' ', '\t');
2020-08-21 10:54:51 -07:00
2024-10-24 01:33:41 -04:00
FilterParser.ParseFilterId(fieldString, out string itemName, out string fieldName);
2024-10-24 02:47:30 -04:00
var item = new ExtraIniItem(itemName, fieldName);
2020-08-21 10:54:51 -07:00
if (item.PopulateFromFile(fileString))
Items.Add(item);
2020-08-21 10:38:42 -07:00
}
2021-02-02 14:09:49 -08:00
watch.Stop();
2020-08-21 10:38:42 -07:00
}
#endregion
2024-03-05 17:33:02 -05:00
#region Running
/// <summary>
/// Apply a set of Extra INIs on the DatFile
/// </summary>
/// <param name="datFile">Current DatFile object to run operations on</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>True if the extras were applied, false on error</returns>
public bool ApplyExtras(DatFile datFile, bool throwOnError = false)
{
// If we have no extras, don't attempt to apply and just return true
if (Items == null || Items.Count == 0)
return true;
2024-03-05 17:17:40 -05:00
var watch = new InternalStopwatch("Applying extra mappings to DAT");
2021-02-02 14:09:49 -08:00
try
{
// Bucket by game first
datFile.Items.BucketBy(ItemKey.Machine, DedupeType.None);
2021-02-01 14:57:01 -08:00
// Create mappings based on the extra items
2024-03-05 17:33:02 -05:00
var combinedMaps = CombineExtras();
var machines = combinedMaps.Keys;
2021-02-01 14:57:01 -08:00
// Apply the mappings
foreach (string machine in machines)
{
// If the key doesn't exist, continue
2021-02-01 14:57:01 -08:00
if (!datFile.Items.ContainsKey(machine))
continue;
2021-02-01 14:57:01 -08:00
// Get the list of DatItems for the machine
2024-02-28 19:19:50 -05:00
var datItems = datFile.Items[machine];
if (datItems == null)
continue;
2021-02-01 14:57:01 -08:00
// Try to get the map values, if possible
2024-03-05 17:33:02 -05:00
combinedMaps.TryGetValue(machine, out var mappings);
2021-02-01 14:57:01 -08:00
// Create a setter with the new mappings
2024-03-05 17:17:40 -05:00
var setter = new Setter();
2024-03-05 17:33:02 -05:00
setter.PopulateSettersFromDictionary(mappings);
2021-02-01 14:57:01 -08:00
// Loop through and set the fields accordingly
foreach (var datItem in datItems)
{
2024-03-10 16:49:07 -04:00
setter.SetFields(datItem.GetFieldValue<Machine>(DatItem.MachineKey));
2021-02-01 14:07:50 -08:00
setter.SetFields(datItem);
}
}
}
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
return false;
}
2021-02-02 14:09:49 -08:00
finally
{
2024-03-19 21:55:55 -04:00
watch.Stop();
}
return true;
}
/// <summary>
/// Apply a set of Extra INIs on the DatFile
/// </summary>
/// <param name="datFile">Current DatFile object to run operations on</param>
/// <param name="throwOnError">True if the error that is thrown should be thrown back to the caller, false otherwise</param>
/// <returns>True if the extras were applied, false on error</returns>
public bool ApplyExtrasDB(DatFile datFile, bool throwOnError = false)
{
// If we have no extras, don't attempt to apply and just return true
if (Items == null || Items.Count == 0)
2024-03-19 21:55:55 -04:00
return true;
var watch = new InternalStopwatch("Applying extra mappings to DAT");
try
{
// Bucket by game first
datFile.ItemsDB.BucketBy(ItemKey.Machine, DedupeType.None);
// Create mappings based on the extra items
var combinedMaps = CombineExtras();
var games = combinedMaps.Keys;
// Apply the mappings
foreach (string game in games)
{
// Get the list of DatItems for the machine
var datItems = datFile.ItemsDB.GetItemsForBucket(game);
2024-03-19 21:55:55 -04:00
if (datItems == null)
continue;
// Try to get the map values, if possible
combinedMaps.TryGetValue(game, out var mappings);
// Create a setter with the new mappings
var setter = new Setter();
setter.PopulateSettersFromDictionary(mappings);
// Loop through and set the fields accordingly
foreach (var datItem in datItems)
{
var machine = datFile.ItemsDB.GetMachineForItem(datItem.Item1);
if (machine.Item2 != null)
setter.SetFields(machine.Item2);
setter.SetFields(datItem.Item2);
}
}
}
catch (Exception ex) when (!throwOnError)
{
logger.Error(ex);
return false;
}
finally
{
2021-02-02 14:09:49 -08:00
watch.Stop();
}
return true;
}
2024-03-05 17:17:40 -05:00
/// <summary>
/// Combine ExtraIni fields
/// </summary>
/// <returns>Mapping dictionary from machine name to field mapping</returns>
2024-10-24 03:16:45 -04:00
private Dictionary<string, Dictionary<FilterKey, string>> CombineExtras()
2024-03-05 17:17:40 -05:00
{
2024-10-24 03:16:45 -04:00
var machineMap = new Dictionary<string, Dictionary<FilterKey, string>>();
2024-03-05 17:17:40 -05:00
// Loop through each of the extras
foreach (ExtraIniItem item in Items)
{
foreach (var mapping in item.Mappings)
{
string machineName = mapping.Key;
string value = mapping.Value;
2024-03-05 17:33:02 -05:00
if (!machineMap.ContainsKey(machineName))
machineMap[machineName] = [];
2024-03-05 17:17:40 -05:00
2024-10-24 02:47:30 -04:00
machineMap[machineName][item.Key] = value;
2021-02-01 14:57:01 -08:00
}
}
return machineMap;
}
#endregion
2020-08-21 10:15:38 -07:00
}
}