using System;
namespace StardewModdingAPI.Toolkit.Framework.UpdateData
{
/// A namespaced mod ID which uniquely identifies a mod within a mod repository.
public class UpdateKey
{
/*********
** Accessors
*********/
/// The raw update key text.
public string RawText { get; }
/// The mod repository containing the mod.
public ModRepositoryKey Repository { get; }
/// The mod ID within the repository.
public string ID { get; }
/// Whether the update key seems to be valid.
public bool LooksValid { get; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The raw update key text.
/// The mod repository containing the mod.
/// The mod ID within the repository.
public UpdateKey(string rawText, ModRepositoryKey repository, string id)
{
this.RawText = rawText;
this.Repository = repository;
this.ID = id;
this.LooksValid =
repository != ModRepositoryKey.Unknown
&& !string.IsNullOrWhiteSpace(id);
}
/// Parse a raw update key.
/// The raw update key to parse.
public static UpdateKey Parse(string raw)
{
// split parts
string[] parts = raw?.Split(':');
if (parts == null || parts.Length != 2)
return new UpdateKey(raw, ModRepositoryKey.Unknown, null);
// extract parts
string repositoryKey = parts[0].Trim();
string id = parts[1].Trim();
if (string.IsNullOrWhiteSpace(id))
id = null;
// parse
if (!Enum.TryParse(repositoryKey, true, out ModRepositoryKey repository))
return new UpdateKey(raw, ModRepositoryKey.Unknown, id);
if (id == null)
return new UpdateKey(raw, repository, null);
return new UpdateKey(raw, repository, id);
}
/// Get a string that represents the current object.
public override string ToString()
{
return this.LooksValid
? $"{this.Repository}:{this.ID}"
: this.RawText;
}
}
}