using System;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace StardewModdingAPI
{
/// A manifest which describes a mod for SMAPI.
public class Manifest : Config
{
/*********
** Accessors
*********/
/// The mod name.
public virtual string Name { get; set; }
/// The mod author's name.
public virtual string Author { get; set; }
/// Obsolete.
[Obsolete("Use 'Author'.")]
public virtual string Authour
{
get { return this.Author; }
set { this.Author = value; }
}
/// The mod version.
public virtual Version Version { get; set; }
/// A brief description of the mod.
public virtual string Description { get; set; }
/// The unique mod ID.
public virtual string UniqueID { get; set; }
/// Whether the mod uses per-save config files.
public virtual bool PerSaveConfigs { get; set; }
/// The name of the DLL in the directory that has the method.
public virtual string EntryDll { get; set; }
/*********
** Public methods
*********/
/// Get the default config values.
public override T GenerateDefaultConfig()
{
this.Name = "";
this.Author = "";
this.Version = new Version(0, 0, 0, "");
this.Description = "";
this.UniqueID = Guid.NewGuid().ToString();
this.PerSaveConfigs = false;
this.EntryDll = "";
return this as T;
}
/// Load the config from the JSON file, saving it to disk if needed.
/// The config class type.
public override T LoadConfig()
{
if (File.Exists(this.ConfigLocation))
{
try
{
JsonConvert.DeserializeObject(File.ReadAllText(this.ConfigLocation));
}
catch
{
//Invalid json blob. Try to remove version?
try
{
JObject j = JObject.Parse(File.ReadAllText(this.ConfigLocation));
if (!j.GetValue("Version").Contains("{"))
{
Log.AsyncC("INVALID JSON VERSION. TRYING TO REMOVE SO A NEW CAN BE AUTO-GENERATED");
j.Remove("Version");
File.WriteAllText(this.ConfigLocation, j.ToString());
}
}
catch (Exception)
{
// ignored
}
}
}
return base.LoadConfig();
}
}
}