using System; using System.Collections.Generic; using System.IO; using System.Linq; using Newtonsoft.Json; namespace GarrysMod.AddonCreator.Addon { /// /// Represents information about an addon. /// public class AddonJson { /// /// Creates an instance of . /// public AddonJson() { Version = 1; } /// /// The title of the addon. /// [JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)] public string Title { get; set; } /// /// A description of the addon. /// [JsonProperty("description")] public string Description { get; set; } /// /// The type of the addon. /// [JsonProperty("type")] public string Type { get; set; } /// /// The assigned tags of the addon. /// [JsonProperty("tags")] public List Tags { get; set; } /// /// A list of patterns of files to ignore when exporting the addon. /// [JsonProperty("ignore", NullValueHandling = NullValueHandling.Ignore)] public List Ignores { get; set; } /// /// The addon's version. /// [JsonProperty("version")] public int Version { get; set; } /// /// The author of the addon. /// [JsonProperty("author")] public string Author { get; set; } /// /// Validates the addon information for mistakes. This includes missing/empty title, missing/empty/invalid description and if the type is missing/empty. /// internal void CheckForErrors() { if (string.IsNullOrEmpty(Title)) { throw new MissingFieldException("Title is empty or not specified."); } if (!string.IsNullOrEmpty(Description) && Description.Contains('\0')) { throw new InvalidDataException("Description contains NULL character."); } if (string.IsNullOrEmpty(Type)) { throw new MissingFieldException("Type is empty or not specified."); } // TODO: Validate tags using a predefined list. } /// /// Removes files matching any of the ignore patterns from a prepared file dictionary. /// /// The file dictionary to scan for files to remove public void RemoveIgnoredFiles(ref Dictionary files) { foreach (var key in files.Keys.ToArray()) // ToArray makes a shadow copy of Keys to avoid "mid-loop-removal" conflicts { if (Ignores.Any(w => w.WildcardRegex().IsMatch(key))) files.Remove(key); } } } }