use new helper to read manifest (#159)

This commit is contained in:
Jesse Plamondon-Willard 2016-11-05 16:14:38 -04:00
parent a929d70433
commit 0749fdcbe5
2 changed files with 27 additions and 76 deletions

View File

@ -1,22 +1,19 @@
using System; using System;
using System.IO;
using System.Linq;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace StardewModdingAPI namespace StardewModdingAPI
{ {
/// <summary>A manifest which describes a mod for SMAPI.</summary> /// <summary>A manifest which describes a mod for SMAPI.</summary>
public class Manifest : Config public class Manifest
{ {
/********* /*********
** Accessors ** Accessors
*********/ *********/
/// <summary>The mod name.</summary> /// <summary>The mod name.</summary>
public virtual string Name { get; set; } public virtual string Name { get; set; } = "";
/// <summary>The mod author's name.</summary> /// <summary>The mod author's name.</summary>
public virtual string Author { get; set; } public virtual string Author { get; set; } = "";
/// <summary>Obsolete.</summary> /// <summary>Obsolete.</summary>
[Obsolete("Use 'Author'.")] [Obsolete("Use 'Author'.")]
@ -27,68 +24,18 @@ namespace StardewModdingAPI
} }
/// <summary>The mod version.</summary> /// <summary>The mod version.</summary>
public virtual Version Version { get; set; } public virtual Version Version { get; set; } = new Version(0, 0, 0, "");
/// <summary>A brief description of the mod.</summary> /// <summary>A brief description of the mod.</summary>
public virtual string Description { get; set; } public virtual string Description { get; set; } = "";
/// <summary>The unique mod ID.</summary> /// <summary>The unique mod ID.</summary>
public virtual string UniqueID { get; set; } public virtual string UniqueID { get; set; } = Guid.NewGuid().ToString();
/// <summary>Whether the mod uses per-save config files.</summary> /// <summary>Whether the mod uses per-save config files.</summary>
public virtual bool PerSaveConfigs { get; set; } public virtual bool PerSaveConfigs { get; set; }
/// <summary>The name of the DLL in the directory that has the <see cref="Mod.Entry"/> method.</summary> /// <summary>The name of the DLL in the directory that has the <see cref="Mod.Entry"/> method.</summary>
public virtual string EntryDll { get; set; } public virtual string EntryDll { get; set; } = "";
/*********
** Public methods
*********/
/// <summary>Get the default config values.</summary>
public override T GenerateDefaultConfig<T>()
{
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;
}
/// <summary>Load the config from the JSON file, saving it to disk if needed.</summary>
/// <typeparam name="T">The config class type.</typeparam>
public override T LoadConfig<T>()
{
if (File.Exists(this.ConfigLocation))
{
try
{
JsonConvert.DeserializeObject<Manifest>(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<T>();
}
} }
} }

View File

@ -240,7 +240,7 @@ namespace StardewModdingAPI
{ {
foreach (string manifestPath in Directory.GetFiles(directory, "manifest.json")) foreach (string manifestPath in Directory.GetFiles(directory, "manifest.json"))
{ {
// error format ModHelper helper = new ModHelper(directory);
string errorPrefix = $"Couldn't load mod for manifest '{manifestPath}'"; string errorPrefix = $"Couldn't load mod for manifest '{manifestPath}'";
// read manifest // read manifest
@ -256,7 +256,12 @@ namespace StardewModdingAPI
} }
// deserialise manifest // deserialise manifest
manifest = manifest.InitializeConfig(manifestPath); manifest = helper.ReadJsonFile<Manifest>("manifest.json");
if (manifest == null)
{
Log.Error($"{errorPrefix}: the manifest file does not exist.");
continue;
}
if (string.IsNullOrEmpty(manifest.EntryDll)) if (string.IsNullOrEmpty(manifest.EntryDll))
{ {
Log.Error($"{errorPrefix}: manifest doesn't specify an entry DLL."); Log.Error($"{errorPrefix}: manifest doesn't specify an entry DLL.");
@ -270,12 +275,11 @@ namespace StardewModdingAPI
} }
// create per-save directory // create per-save directory
string targDir = Path.GetDirectoryName(manifestPath);
string psDir = Path.Combine(targDir, "psconfigs");
try
{
if (manifest.PerSaveConfigs) if (manifest.PerSaveConfigs)
{ {
try
{
string psDir = Path.Combine(directory, "psconfigs");
Directory.CreateDirectory(psDir); Directory.CreateDirectory(psDir);
if (!Directory.Exists(psDir)) if (!Directory.Exists(psDir))
{ {
@ -283,18 +287,18 @@ namespace StardewModdingAPI
continue; continue;
} }
} }
}
catch (Exception ex) catch (Exception ex)
{ {
Log.Error($"{errorPrefix}: couldm't create the per-save configuration directory ('psconfigs') requested by this mod.\n{ex}"); Log.Error($"{errorPrefix}: couldm't create the per-save configuration directory ('psconfigs') requested by this mod.\n{ex}");
continue; continue;
} }
}
// load DLL & hook up mod // load DLL & hook up mod
string targDll = string.Empty; string targDll = string.Empty;
try try
{ {
targDll = Path.Combine(targDir, manifest.EntryDll); targDll = Path.Combine(directory, manifest.EntryDll);
if (!File.Exists(targDll)) if (!File.Exists(targDll))
{ {
Log.Error($"{errorPrefix}: target DLL '{targDll}' does not exist."); Log.Error($"{errorPrefix}: target DLL '{targDll}' does not exist.");
@ -304,12 +308,12 @@ namespace StardewModdingAPI
Assembly modAssembly = Assembly.UnsafeLoadFrom(targDll); Assembly modAssembly = Assembly.UnsafeLoadFrom(targDll);
if (modAssembly.DefinedTypes.Count(x => x.BaseType == typeof(Mod)) > 0) if (modAssembly.DefinedTypes.Count(x => x.BaseType == typeof(Mod)) > 0)
{ {
TypeInfo tar = modAssembly.DefinedTypes.First(x => x.BaseType == typeof(Mod)); TypeInfo modEntryType = modAssembly.DefinedTypes.First(x => x.BaseType == typeof(Mod));
Mod modEntry = (Mod)modAssembly.CreateInstance(tar.ToString()); Mod modEntry = (Mod)modAssembly.CreateInstance(modEntryType.ToString());
if (modEntry != null) if (modEntry != null)
{ {
modEntry.Helper = new ModHelper(targDir); modEntry.Helper = helper;
modEntry.PathOnDisk = targDir; modEntry.PathOnDisk = directory;
modEntry.Manifest = manifest; modEntry.Manifest = manifest;
Log.Info($"Loaded mod: {modEntry.Manifest.Name} by {modEntry.Manifest.Author}, v{modEntry.Manifest.Version} | {modEntry.Manifest.Description}\n@ {targDll}"); Log.Info($"Loaded mod: {modEntry.Manifest.Name} by {modEntry.Manifest.Author}, v{modEntry.Manifest.Version} | {modEntry.Manifest.Description}\n@ {targDll}");
Program.ModsLoaded += 1; Program.ModsLoaded += 1;