SMAPI/StardewModdingAPI/Manifest.cs

89 lines
2.6 KiB
C#
Raw Normal View History

using System;
2016-03-27 19:13:16 +08:00
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace StardewModdingAPI
{
2016-03-23 08:36:04 +08:00
public class Manifest : Config
{
/// <summary>
/// The name of your mod.
/// </summary>
public virtual string Name { get; set; }
/// <summary>
/// The name of the mod's authour.
/// </summary>
public virtual string Authour { get; set; }
/// <summary>
/// The version of the mod.
/// </summary>
2016-03-24 01:43:11 +08:00
public virtual Version Version { get; set; }
/// <summary>
/// A description of the mod.
/// </summary>
public virtual string Description { get; set; }
2016-03-23 08:36:04 +08:00
/// <summary>
/// The unique ID of the mod. It doesn't *need* to be anything.
2016-03-23 08:36:04 +08:00
/// </summary>
public virtual string UniqueID { get; set; }
/// <summary>
/// Whether or not the mod uses per-save-config files.
2016-03-23 08:36:04 +08:00
/// </summary>
public virtual bool PerSaveConfigs { get; set; }
/// <summary>
/// The name of the DLL in the directory that has the Entry() method.
2016-03-23 08:36:04 +08:00
/// </summary>
public virtual string EntryDll { get; set; }
2016-03-24 01:43:11 +08:00
public override T GenerateDefaultConfig<T>()
2016-03-23 08:36:04 +08:00
{
Name = "";
Authour = "";
2016-03-24 01:43:11 +08:00
Version = new Version(0, 0, 0, "");
2016-03-23 08:36:04 +08:00
Description = "";
UniqueID = Guid.NewGuid().ToString();
PerSaveConfigs = false;
EntryDll = "";
return this as T;
2016-03-23 08:36:04 +08:00
}
2016-03-27 19:13:16 +08:00
public override T LoadConfig<T>()
{
if (File.Exists(ConfigLocation))
{
try
{
Manifest m = JsonConvert.DeserializeObject<Manifest>(File.ReadAllText(ConfigLocation));
}
catch
{
//Invalid json blob. Try to remove version?
try
{
JObject j = JObject.Parse(File.ReadAllText(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(ConfigLocation, j.ToString());
}
}
2016-03-27 23:13:58 +08:00
catch (Exception)
2016-03-27 19:13:16 +08:00
{
2016-03-27 23:13:58 +08:00
// ignored
2016-03-27 19:13:16 +08:00
}
}
}
return base.LoadConfig<T>();
}
}
}