using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using StardewModdingAPI.Web.Framework.LogParsing.Models;
namespace StardewModdingAPI.Web.ViewModels
{
/// The view model for the log parser page.
public class LogParserModel
{
/*********
** Fields
*********/
/// A regex pattern matching characters to remove from a mod name to create the slug ID.
private readonly Regex SlugInvalidCharPattern = new Regex("[^a-z0-9]", RegexOptions.Compiled | RegexOptions.IgnoreCase);
/*********
** Accessors
*********/
/// The root URL for the log parser controller.
public string SectionUrl { get; set; }
/// The paste ID.
public string PasteID { get; set; }
/// The parsed log info.
public ParsedLog ParsedLog { get; set; }
/// Whether to show the raw unparsed log.
public bool ShowRaw { get; set; }
/// An error which occurred while uploading the log to Pastebin.
public string UploadError { get; set; }
/// An error which occurred while parsing the log file.
public string ParseError => this.ParsedLog?.Error;
/*********
** Public methods
*********/
/// Construct an instance.
public LogParserModel() { }
/// Construct an instance.
/// The root URL for the log parser controller.
/// The paste ID.
public LogParserModel(string sectionUrl, string pasteID)
{
this.SectionUrl = sectionUrl;
this.PasteID = pasteID;
this.ParsedLog = null;
this.ShowRaw = false;
}
/// Construct an instance.
/// The root URL for the log parser controller.
/// The paste ID.
/// The parsed log info.
/// Whether to show the raw unparsed log.
public LogParserModel(string sectionUrl, string pasteID, ParsedLog parsedLog, bool showRaw)
: this(sectionUrl, pasteID)
{
this.ParsedLog = parsedLog;
this.ShowRaw = showRaw;
}
/// Get all content packs in the log grouped by the mod they're for.
public IDictionary GetContentPacksByMod()
{
// get all mods & content packs
LogModInfo[] mods = this.ParsedLog?.Mods;
if (mods == null || !mods.Any())
return new Dictionary();
// group by mod
return mods
.Where(mod => mod.ContentPackFor != null)
.GroupBy(mod => mod.ContentPackFor)
.ToDictionary(group => group.Key, group => group.ToArray());
}
/// Get a sanitised mod name that's safe to use in anchors, attributes, and URLs.
/// The mod name.
public string GetSlug(string modName)
{
return this.SlugInvalidCharPattern.Replace(modName, "");
}
}
}