#nullable disable
using System.Linq;
namespace StardewModdingAPI.Toolkit.Framework.ModData
{
/// A versioned mod metadata field.
public class ModDataField
{
/*********
** Accessors
*********/
/// The field key.
public ModDataFieldKey Key { get; }
/// The field value.
public string Value { get; }
/// Whether this field should only be applied if it's not already set.
public bool IsDefault { get; }
/// The lowest version in the range, or null for all past versions.
public ISemanticVersion LowerVersion { get; }
/// The highest version in the range, or null for all future versions.
public ISemanticVersion UpperVersion { get; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The field key.
/// The field value.
/// Whether this field should only be applied if it's not already set.
/// The lowest version in the range, or null for all past versions.
/// The highest version in the range, or null for all future versions.
public ModDataField(ModDataFieldKey key, string value, bool isDefault, ISemanticVersion lowerVersion, ISemanticVersion upperVersion)
{
this.Key = key;
this.Value = value;
this.IsDefault = isDefault;
this.LowerVersion = lowerVersion;
this.UpperVersion = upperVersion;
}
/// Get whether this data field applies for the given manifest.
/// The mod manifest.
public bool IsMatch(IManifest manifest)
{
return
manifest?.Version != null // ignore invalid manifest
&& (!this.IsDefault || !this.HasFieldValue(manifest, this.Key))
&& (this.LowerVersion == null || !manifest.Version.IsOlderThan(this.LowerVersion))
&& (this.UpperVersion == null || !manifest.Version.IsNewerThan(this.UpperVersion));
}
/*********
** Private methods
*********/
/// Get whether a manifest field has a meaningful value for the purposes of enforcing .
/// The mod manifest.
/// The field key matching .
private bool HasFieldValue(IManifest manifest, ModDataFieldKey key)
{
switch (key)
{
// update key
case ModDataFieldKey.UpdateKey:
return manifest.UpdateKeys.Any(p => !string.IsNullOrWhiteSpace(p));
// non-manifest fields
case ModDataFieldKey.StatusReasonPhrase:
case ModDataFieldKey.StatusReasonDetails:
case ModDataFieldKey.Status:
return false;
default:
return false;
}
}
}
}