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 { /********* ** Properties *********/ /// 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; } /// 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. /// The parsed log info. public LogParserModel(string sectionUrl, string pasteID, ParsedLog parsedLog) { this.SectionUrl = sectionUrl; this.PasteID = pasteID; this.ParsedLog = parsedLog; } /// 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, ""); } } }