2023-08-11 13:46:27 -04:00
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2023-08-11 22:58:05 -04:00
|
|
|
using System.Text.RegularExpressions;
|
2023-09-04 23:51:37 -04:00
|
|
|
using SabreTools.Models.Metadata;
|
2023-08-11 13:46:27 -04:00
|
|
|
|
|
|
|
|
namespace SabreTools.Filter
|
|
|
|
|
{
|
|
|
|
|
public static class FieldManipulator
|
|
|
|
|
{
|
2023-08-11 22:58:05 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Regex pattern to match scene dates
|
|
|
|
|
/// </summary>
|
|
|
|
|
private const string _sceneDateRegex = @"([0-9]{2}\.[0-9]{2}\.[0-9]{2}-)(.*?-.*?)";
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Replace the machine name with the description
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static (bool Success, string? OriginalName) DescriptionToName(Machine? machine)
|
|
|
|
|
{
|
|
|
|
|
// If the machine is missing, we can't do anything
|
|
|
|
|
if (machine == null)
|
|
|
|
|
return (false, null);
|
|
|
|
|
|
|
|
|
|
// Get both the current name and description
|
|
|
|
|
string? name = machine.ReadString(Header.NameKey);
|
|
|
|
|
string? description = machine.ReadString(Header.DescriptionKey);
|
|
|
|
|
|
|
|
|
|
// Replace the name with the description
|
|
|
|
|
machine[Header.NameKey] = description;
|
|
|
|
|
return (true, name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Remove a field from a given DictionaryBase
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool RemoveField(DictionaryBase? dictionaryBase, string? fieldName)
|
|
|
|
|
{
|
|
|
|
|
// If the item or field name are missing, we can't do anything
|
2024-02-28 20:14:45 -05:00
|
|
|
if (dictionaryBase == null || string.IsNullOrEmpty(fieldName))
|
2023-08-11 22:58:05 -04:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// If the key doesn't exist, then it's already removed
|
2024-02-28 20:14:45 -05:00
|
|
|
if (!dictionaryBase.ContainsKey(fieldName!))
|
2023-08-11 22:58:05 -04:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
// Remove the key
|
2024-02-28 20:14:45 -05:00
|
|
|
dictionaryBase.Remove(fieldName!);
|
2023-08-11 22:58:05 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-11 13:46:27 -04:00
|
|
|
/// <summary>
|
|
|
|
|
/// Set a field in a given DictionaryBase
|
|
|
|
|
/// </summary>
|
|
|
|
|
public static bool SetField(DictionaryBase? dictionaryBase, string? fieldName, object value)
|
|
|
|
|
{
|
2023-08-11 22:58:05 -04:00
|
|
|
// If the item or field name are missing, we can't do anything
|
2024-02-28 20:14:45 -05:00
|
|
|
if (dictionaryBase == null || string.IsNullOrEmpty(fieldName))
|
2023-08-11 13:46:27 -04:00
|
|
|
return false;
|
|
|
|
|
|
2023-08-11 22:58:05 -04:00
|
|
|
// Retrieve the list of valid fields for the item and validate
|
2023-08-11 22:30:02 -04:00
|
|
|
var constants = TypeHelper.GetConstants(dictionaryBase.GetType());
|
2023-08-14 13:17:51 -04:00
|
|
|
if (constants == null || !constants.Any(c => string.Equals(c, fieldName, StringComparison.OrdinalIgnoreCase)))
|
2023-08-11 13:46:27 -04:00
|
|
|
return false;
|
|
|
|
|
|
2023-08-11 22:58:05 -04:00
|
|
|
// Set the field with the new value
|
2024-02-28 20:14:45 -05:00
|
|
|
dictionaryBase[fieldName!] = value;
|
2023-08-11 13:46:27 -04:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2023-08-11 22:58:05 -04:00
|
|
|
/// Strip the dates from the beginning of scene-style machine name and description
|
2023-08-11 13:46:27 -04:00
|
|
|
/// </summary>
|
2023-08-11 22:58:05 -04:00
|
|
|
public static bool StripSceneDates(Machine? machine)
|
2023-08-11 13:46:27 -04:00
|
|
|
{
|
2023-08-11 22:58:05 -04:00
|
|
|
// If the machine is missing, we can't do anything
|
|
|
|
|
if (machine == null)
|
2023-08-11 13:46:27 -04:00
|
|
|
return false;
|
|
|
|
|
|
2023-08-11 22:58:05 -04:00
|
|
|
// Strip dates from the name field
|
|
|
|
|
string? name = machine.ReadString(Header.NameKey);
|
|
|
|
|
if (name != null && Regex.IsMatch(name, _sceneDateRegex))
|
|
|
|
|
machine[Header.NameKey] = Regex.Replace(name, _sceneDateRegex, @"$2");
|
|
|
|
|
|
|
|
|
|
// Strip dates from the description field
|
|
|
|
|
string? description = machine.ReadString(Header.DescriptionKey);
|
|
|
|
|
if (description != null && Regex.IsMatch(description, _sceneDateRegex))
|
|
|
|
|
machine[Header.DescriptionKey] = Regex.Replace(description, _sceneDateRegex, @"$2");
|
2023-08-11 13:46:27 -04:00
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|