mirror of
https://github.com/claunia/SabreTools.git
synced 2025-12-16 19:14:27 +00:00
Create DictionaryBase to hold helpers
This commit is contained in:
@@ -1,4 +1,3 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
using System.Xml.Serialization;
|
using System.Xml.Serialization;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
@@ -7,7 +6,7 @@ namespace SabreTools.Models.Internal
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Format-agnostic representation of item data
|
/// Format-agnostic representation of item data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class DatItem : Dictionary<string, object?>
|
public class DatItem : DictionaryBase
|
||||||
{
|
{
|
||||||
#region Common Keys
|
#region Common Keys
|
||||||
|
|
||||||
@@ -24,57 +23,5 @@ namespace SabreTools.Models.Internal
|
|||||||
get => ContainsKey(TypeKey) ? this[TypeKey] as ItemType? : null;
|
get => ContainsKey(TypeKey) ? this[TypeKey] as ItemType? : null;
|
||||||
set => this[TypeKey] = value;
|
set => this[TypeKey] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
#region Reading Helpers
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read a key as a bool, returning null on error
|
|
||||||
/// </summary>
|
|
||||||
public bool? ReadBool(string key)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(key))
|
|
||||||
return null;
|
|
||||||
if (!ContainsKey(key))
|
|
||||||
return null;
|
|
||||||
return this[key] as bool?;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read a key as a double, returning null on error
|
|
||||||
/// </summary>
|
|
||||||
public double? ReadDouble(string key)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(key))
|
|
||||||
return null;
|
|
||||||
if (!ContainsKey(key))
|
|
||||||
return null;
|
|
||||||
return this[key] as double?;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read a key as a long, returning null on error
|
|
||||||
/// </summary>
|
|
||||||
public long? ReadLong(string key)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(key))
|
|
||||||
return null;
|
|
||||||
if (!ContainsKey(key))
|
|
||||||
return null;
|
|
||||||
return this[key] as long?;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read a key as a string, returning null on error
|
|
||||||
/// </summary>
|
|
||||||
public string? ReadString(string key)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(key))
|
|
||||||
return null;
|
|
||||||
if (!ContainsKey(key))
|
|
||||||
return null;
|
|
||||||
return this[key] as string;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
47
SabreTools.Models/Internal/DictionaryBase.cs
Normal file
47
SabreTools.Models/Internal/DictionaryBase.cs
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
namespace SabreTools.Models.Internal
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Specialized dictionary base for item types
|
||||||
|
/// </summary>
|
||||||
|
public abstract class DictionaryBase : Dictionary<string, object?>
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Read a key as the specified type, returning null on error
|
||||||
|
/// </summary>
|
||||||
|
public T? Read<T>(string key)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrWhiteSpace(key))
|
||||||
|
return default;
|
||||||
|
if (!ContainsKey(key))
|
||||||
|
return default;
|
||||||
|
return (T?)this[key];
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a key as a bool, returning null on error
|
||||||
|
/// </summary>
|
||||||
|
public bool? ReadBool(string key) => Read<bool>(key);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a key as a double, returning null on error
|
||||||
|
/// </summary>
|
||||||
|
public double? ReadDouble(string key) => Read<double>(key);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a key as a long, returning null on error
|
||||||
|
/// </summary>
|
||||||
|
public long? ReadLong(string key) => Read<long>(key);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a key as a string, returning null on error
|
||||||
|
/// </summary>
|
||||||
|
public string? ReadString(string key) => Read<string>(key);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Read a key as a string[], returning null on error
|
||||||
|
/// </summary>
|
||||||
|
public string[]? ReadStringArray(string key) => Read<string[]>(key);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,9 @@
|
|||||||
using System.Collections.Generic;
|
|
||||||
|
|
||||||
namespace SabreTools.Models.Internal
|
namespace SabreTools.Models.Internal
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Format-agnostic representation of game, machine, and set data
|
/// Format-agnostic representation of game, machine, and set data
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class Machine : Dictionary<string, object?>
|
public class Machine : DictionaryBase
|
||||||
{
|
{
|
||||||
#region Keys
|
#region Keys
|
||||||
|
|
||||||
@@ -239,69 +237,5 @@ namespace SabreTools.Models.Internal
|
|||||||
public const string YearKey = "year";
|
public const string YearKey = "year";
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
#region Reading Helpers
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read a key as a bool, returning null on error
|
|
||||||
/// </summary>
|
|
||||||
public bool? ReadBool(string key)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(key))
|
|
||||||
return null;
|
|
||||||
if (!ContainsKey(key))
|
|
||||||
return null;
|
|
||||||
return this[key] as bool?;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read a key as a double, returning null on error
|
|
||||||
/// </summary>
|
|
||||||
public double? ReadDouble(string key)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(key))
|
|
||||||
return null;
|
|
||||||
if (!ContainsKey(key))
|
|
||||||
return null;
|
|
||||||
return this[key] as double?;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read a key as a long, returning null on error
|
|
||||||
/// </summary>
|
|
||||||
public long? ReadLong(string key)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(key))
|
|
||||||
return null;
|
|
||||||
if (!ContainsKey(key))
|
|
||||||
return null;
|
|
||||||
return this[key] as long?;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read a key as a string, returning null on error
|
|
||||||
/// </summary>
|
|
||||||
public string? ReadString(string key)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(key))
|
|
||||||
return null;
|
|
||||||
if (!ContainsKey(key))
|
|
||||||
return null;
|
|
||||||
return this[key] as string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Read a key as a string[], returning null on error
|
|
||||||
/// </summary>
|
|
||||||
public string[]? ReadStringArray(string key)
|
|
||||||
{
|
|
||||||
if (string.IsNullOrWhiteSpace(key))
|
|
||||||
return null;
|
|
||||||
if (!ContainsKey(key))
|
|
||||||
return null;
|
|
||||||
return this[key] as string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user