2023-08-07 21:10:47 -04:00
|
|
|
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)
|
|
|
|
|
{
|
2023-08-11 12:47:59 -04:00
|
|
|
if (!ValidateKey(key))
|
2023-08-07 21:10:47 -04:00
|
|
|
return default;
|
|
|
|
|
return (T?)this[key];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read a key as a bool, returning null on error
|
|
|
|
|
/// </summary>
|
2023-08-11 11:58:54 -04:00
|
|
|
public bool? ReadBool(string key)
|
|
|
|
|
{
|
2023-08-11 12:47:59 -04:00
|
|
|
if (!ValidateKey(key))
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-08-11 11:58:54 -04:00
|
|
|
bool? asBool = Read<bool>(key);
|
|
|
|
|
if (asBool != null)
|
|
|
|
|
return asBool;
|
|
|
|
|
|
|
|
|
|
string? asString = Read<string>(key);
|
|
|
|
|
return asString?.ToLowerInvariant() switch
|
|
|
|
|
{
|
|
|
|
|
"true" => true,
|
|
|
|
|
"yes" => true,
|
|
|
|
|
"false" => false,
|
|
|
|
|
"no" => false,
|
|
|
|
|
_ => null,
|
|
|
|
|
};
|
|
|
|
|
}
|
2023-08-07 21:10:47 -04:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read a key as a double, returning null on error
|
|
|
|
|
/// </summary>
|
2023-08-11 11:58:54 -04:00
|
|
|
public double? ReadDouble(string key)
|
|
|
|
|
{
|
2023-08-11 12:47:59 -04:00
|
|
|
if (!ValidateKey(key))
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-08-11 11:58:54 -04:00
|
|
|
double? asDouble = Read<double>(key);
|
|
|
|
|
if (asDouble != null)
|
|
|
|
|
return asDouble;
|
|
|
|
|
|
|
|
|
|
string? asString = Read<string>(key);
|
|
|
|
|
if (asString != null && double.TryParse(asString, out double asStringDouble))
|
|
|
|
|
return asStringDouble;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-08-07 21:10:47 -04:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read a key as a long, returning null on error
|
|
|
|
|
/// </summary>
|
2023-08-11 11:58:54 -04:00
|
|
|
public long? ReadLong(string key)
|
|
|
|
|
{
|
2023-08-11 12:47:59 -04:00
|
|
|
if (!ValidateKey(key))
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-08-11 11:58:54 -04:00
|
|
|
long? asLong = Read<long>(key);
|
|
|
|
|
if (asLong != null)
|
|
|
|
|
return asLong;
|
|
|
|
|
|
|
|
|
|
string? asString = Read<string>(key);
|
|
|
|
|
if (asString != null && long.TryParse(asString, out long asStringLong))
|
|
|
|
|
return asStringLong;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-08-07 21:10:47 -04:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read a key as a string, returning null on error
|
|
|
|
|
/// </summary>
|
2023-08-11 11:58:54 -04:00
|
|
|
public string? ReadString(string key)
|
|
|
|
|
{
|
2023-08-11 12:47:59 -04:00
|
|
|
if (!ValidateKey(key))
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-08-11 11:58:54 -04:00
|
|
|
string? asString = Read<string>(key);
|
|
|
|
|
if (asString != null)
|
|
|
|
|
return asString;
|
|
|
|
|
|
|
|
|
|
string[]? asArray = Read<string[]>(key);
|
|
|
|
|
if (asArray != null)
|
|
|
|
|
return string.Join(',', asArray);
|
|
|
|
|
|
2023-08-11 12:47:59 -04:00
|
|
|
return this[key]!.ToString();
|
2023-08-11 11:58:54 -04:00
|
|
|
}
|
2023-08-07 21:10:47 -04:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Read a key as a string[], returning null on error
|
|
|
|
|
/// </summary>
|
2023-08-11 11:58:54 -04:00
|
|
|
public string[]? ReadStringArray(string key)
|
|
|
|
|
{
|
2023-08-11 12:47:59 -04:00
|
|
|
if (!ValidateKey(key))
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-08-11 11:58:54 -04:00
|
|
|
string[]? asArray = Read<string[]>(key);
|
|
|
|
|
if (asArray != null)
|
|
|
|
|
return asArray;
|
|
|
|
|
|
|
|
|
|
string? asString = Read<string>(key);
|
|
|
|
|
if (asString != null)
|
|
|
|
|
return new string[] { asString };
|
|
|
|
|
|
2023-08-11 12:47:59 -04:00
|
|
|
asString = this[key]!.ToString();
|
|
|
|
|
if (asString != null)
|
|
|
|
|
return new string[] { asString };
|
|
|
|
|
|
2023-08-11 11:58:54 -04:00
|
|
|
return null;
|
|
|
|
|
}
|
2023-08-11 12:47:59 -04:00
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Check if a key is valid
|
|
|
|
|
/// </summary>
|
|
|
|
|
private bool ValidateKey(string key)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrWhiteSpace(key))
|
|
|
|
|
return false;
|
|
|
|
|
else if (!ContainsKey(key))
|
|
|
|
|
return false;
|
|
|
|
|
else if (this[key] == null)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2023-08-07 21:10:47 -04:00
|
|
|
}
|
|
|
|
|
}
|