using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
using StardewModdingAPI.Toolkit.Framework.Clients.Wiki;
namespace StardewModdingAPI.Web.ViewModels
{
/// Metadata about a mod.
public class ModModel
{
/*********
** Accessors
*********/
/// The mod name.
public string? Name { get; }
/// The mod's alternative names, if any.
public string AlternateNames { get; }
/// The mod author's name.
public string? Author { get; }
/// The mod author's alternative names, if any.
public string AlternateAuthors { get; }
/// The GitHub repo, if any.
public string? GitHubRepo { get; }
/// The URL to the mod's source code, if any.
public string? SourceUrl { get; }
/// The compatibility status for the stable version of the game.
public ModCompatibilityModel Compatibility { get; }
/// The compatibility status for the beta version of the game.
public ModCompatibilityModel? BetaCompatibility { get; }
/// Links to the available mod pages.
public ModLinkModel[] ModPages { get; }
/// The human-readable warnings for players about this mod.
public string[] Warnings { get; }
/// The URL of the pull request which submits changes for an unofficial update to the author, if any.
public string? PullRequestUrl { get; }
/// Special notes intended for developers who maintain unofficial updates or submit pull requests.
public string? DevNote { get; }
/// A unique identifier for the mod that can be used in an anchor URL.
public string? Slug { get; }
/// The sites where the mod can be downloaded.
public string[] ModPageSites => this.ModPages.Select(p => p.Text).ToArray();
/*********
** Public methods
*********/
/// Construct an instance.
/// The mod name.
/// The mod's alternative names, if any.
/// The mod author's name.
/// The mod author's alternative names, if any.
/// The GitHub repo, if any.
/// The URL to the mod's source code, if any.
/// The compatibility status for the stable version of the game.
/// The compatibility status for the beta version of the game.
/// Links to the available mod pages.
/// The human-readable warnings for players about this mod.
/// The URL of the pull request which submits changes for an unofficial update to the author, if any.
/// Special notes intended for developers who maintain unofficial updates or submit pull requests.
/// A unique identifier for the mod that can be used in an anchor URL.
[JsonConstructor]
public ModModel(string? name, string alternateNames, string author, string alternateAuthors, string gitHubRepo, string sourceUrl, ModCompatibilityModel compatibility, ModCompatibilityModel betaCompatibility, ModLinkModel[] modPages, string[] warnings, string pullRequestUrl, string devNote, string slug)
{
this.Name = name;
this.AlternateNames = alternateNames;
this.Author = author;
this.AlternateAuthors = alternateAuthors;
this.GitHubRepo = gitHubRepo;
this.SourceUrl = sourceUrl;
this.Compatibility = compatibility;
this.BetaCompatibility = betaCompatibility;
this.ModPages = modPages;
this.Warnings = warnings;
this.PullRequestUrl = pullRequestUrl;
this.DevNote = devNote;
this.Slug = slug;
}
/// Construct an instance.
/// The mod metadata.
public ModModel(WikiModEntry entry)
{
// basic info
this.Name = entry.Name.FirstOrDefault();
this.AlternateNames = string.Join(", ", entry.Name.Skip(1).ToArray());
this.Author = entry.Author.FirstOrDefault();
this.AlternateAuthors = string.Join(", ", entry.Author.Skip(1).ToArray());
this.GitHubRepo = entry.GitHubRepo;
this.SourceUrl = this.GetSourceUrl(entry);
this.Compatibility = new ModCompatibilityModel(entry.Compatibility);
this.BetaCompatibility = entry.BetaCompatibility != null ? new ModCompatibilityModel(entry.BetaCompatibility) : null;
this.ModPages = this.GetModPageUrls(entry).ToArray();
this.Warnings = entry.Warnings;
this.PullRequestUrl = entry.PullRequestUrl;
this.DevNote = entry.DevNote;
this.Slug = entry.Anchor;
}
/*********
** Private methods
*********/
/// Get the web URL for the mod's source code repository, if any.
/// The mod metadata.
private string? GetSourceUrl(WikiModEntry entry)
{
if (!string.IsNullOrWhiteSpace(entry.GitHubRepo))
return $"https://github.com/{entry.GitHubRepo}";
if (!string.IsNullOrWhiteSpace(entry.CustomSourceUrl))
return entry.CustomSourceUrl;
return null;
}
/// Get the web URLs for the mod pages, if any.
/// The mod metadata.
private IEnumerable GetModPageUrls(WikiModEntry entry)
{
bool anyFound = false;
// normal mod pages
if (entry.NexusID.HasValue)
{
anyFound = true;
yield return new ModLinkModel($"https://www.nexusmods.com/stardewvalley/mods/{entry.NexusID}", "Nexus");
}
if (entry.ModDropID.HasValue)
{
anyFound = true;
yield return new ModLinkModel($"https://www.moddrop.com/stardew-valley/mod/{entry.ModDropID}", "ModDrop");
}
if (!string.IsNullOrWhiteSpace(entry.CurseForgeKey))
{
anyFound = true;
yield return new ModLinkModel($"https://www.curseforge.com/stardewvalley/mods/{entry.CurseForgeKey}", "CurseForge");
}
if (entry.ChucklefishID.HasValue)
{
anyFound = true;
yield return new ModLinkModel($"https://community.playstarbound.com/resources/{entry.ChucklefishID}", "Chucklefish");
}
// fallback
if (!anyFound && !string.IsNullOrWhiteSpace(entry.CustomUrl))
{
anyFound = true;
yield return new ModLinkModel(entry.CustomUrl, "custom");
}
if (!anyFound && !string.IsNullOrWhiteSpace(entry.GitHubRepo))
yield return new ModLinkModel($"https://github.com/{entry.GitHubRepo}/releases", "GitHub");
}
}
}