2014-10-22 14:29:51 +00:00
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using Newtonsoft.Json ;
2014-10-22 20:56:05 +00:00
namespace GarrysMod.AddonCreator.Addon
2014-10-22 14:29:51 +00:00
{
2014-12-11 01:48:31 +00:00
/// <summary>
/// Represents information about an addon.
/// </summary>
2014-10-22 14:29:51 +00:00
public class AddonJson
{
2014-12-11 01:48:31 +00:00
/// <summary>
/// Creates an instance of <see cref="AddonJson"/>.
/// </summary>
2014-10-22 17:47:00 +00:00
public AddonJson ( )
{
Version = 1 ;
}
2014-12-11 01:48:31 +00:00
/// <summary>
/// The title of the addon.
/// </summary>
2014-10-22 17:48:30 +00:00
[JsonProperty("title", NullValueHandling = NullValueHandling.Ignore)]
2014-10-22 14:29:51 +00:00
public string Title { get ; set ; }
2014-12-11 01:48:31 +00:00
/// <summary>
/// A description of the addon.
/// </summary>
2014-10-22 14:29:51 +00:00
[JsonProperty("description")]
public string Description { get ; set ; }
2014-12-11 01:48:31 +00:00
/// <summary>
/// The type of the addon.
/// </summary>
2014-10-22 14:29:51 +00:00
[JsonProperty("type")]
public string Type { get ; set ; }
2014-12-11 01:48:31 +00:00
/// <summary>
/// The assigned tags of the addon.
/// </summary>
2014-10-22 14:29:51 +00:00
[JsonProperty("tags")]
public List < string > Tags { get ; set ; }
2014-12-11 01:48:31 +00:00
/// <summary>
/// A list of patterns of files to ignore when exporting the addon.
/// </summary>
2014-10-22 17:47:00 +00:00
[JsonProperty("ignore", NullValueHandling = NullValueHandling.Ignore)]
2014-10-22 14:29:51 +00:00
public List < string > Ignores { get ; set ; }
2014-12-11 01:48:31 +00:00
/// <summary>
/// The addon's version.
/// </summary>
2014-10-22 17:42:25 +00:00
[JsonProperty("version")]
public int Version { get ; set ; }
2014-12-11 01:48:31 +00:00
/// <summary>
/// The author of the addon.
/// </summary>
2014-10-22 17:51:18 +00:00
[JsonProperty("author")]
public string Author { get ; set ; }
2014-12-11 01:48:31 +00:00
/// <summary>
/// Validates the addon information for mistakes. This includes missing/empty title, missing/empty/invalid description and if the type is missing/empty.
/// </summary>
2014-10-22 17:42:25 +00:00
internal void CheckForErrors ( )
2014-10-22 14:29:51 +00:00
{
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." ) ;
}
2014-12-11 01:48:31 +00:00
// TODO: Validate tags using a predefined list.
2014-10-22 14:29:51 +00:00
}
2014-12-11 01:48:31 +00:00
/// <summary>
/// Removes files matching any of the ignore patterns from a prepared file dictionary.
/// </summary>
/// <param name="files">The file dictionary to scan for files to remove</param>
2014-10-22 14:29:51 +00:00
public void RemoveIgnoredFiles ( ref Dictionary < string , AddonFileInfo > 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 ) ;
}
}
}
2014-10-22 17:48:30 +00:00
}