2016-03-21 03:43:31 +08:00
|
|
|
|
/*
|
|
|
|
|
Copyright 2016 Zoey (Zoryn)
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
|
|
namespace StardewModdingAPI
|
|
|
|
|
{
|
2016-03-24 01:43:11 +08:00
|
|
|
|
public class Config
|
2016-03-21 03:43:31 +08:00
|
|
|
|
{
|
|
|
|
|
[JsonIgnore]
|
2016-03-23 16:22:47 +08:00
|
|
|
|
public virtual string ConfigLocation { get; protected internal set; }
|
2016-03-21 03:43:31 +08:00
|
|
|
|
|
|
|
|
|
[JsonIgnore]
|
2016-03-23 16:22:47 +08:00
|
|
|
|
public virtual string ConfigDir => Path.GetDirectoryName(ConfigLocation);
|
|
|
|
|
|
|
|
|
|
public virtual Config Instance<T>() where T : Config => Activator.CreateInstance<T>();
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-03-27 13:09:09 +08:00
|
|
|
|
/// Loads the config from the json blob on disk, updating and re-writing to the disk if needed.
|
2016-03-23 16:22:47 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
2016-03-24 00:28:50 +08:00
|
|
|
|
public virtual T LoadConfig<T>() where T : Config
|
2016-03-23 16:22:47 +08:00
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(ConfigLocation))
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncR("A config tried to load without specifying a location on the disk.");
|
2016-03-23 16:22:47 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T ret = null;
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(ConfigLocation))
|
|
|
|
|
{
|
|
|
|
|
//no config exists, generate default values
|
2016-03-27 13:09:09 +08:00
|
|
|
|
var c = GenerateDefaultConfig<T>();
|
2016-03-23 16:22:47 +08:00
|
|
|
|
c.ConfigLocation = ConfigLocation;
|
|
|
|
|
ret = c;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//try to load the config from a json blob on disk
|
2016-03-27 13:09:09 +08:00
|
|
|
|
var c = JsonConvert.DeserializeObject<T>(File.ReadAllText(ConfigLocation), new JsonSerializerSettings {ContractResolver = new JsonResolver()});
|
2016-03-23 16:22:47 +08:00
|
|
|
|
|
|
|
|
|
c.ConfigLocation = ConfigLocation;
|
|
|
|
|
|
|
|
|
|
//update the config with default values if needed
|
|
|
|
|
ret = c.UpdateConfig<T>();
|
|
|
|
|
|
|
|
|
|
c = null;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncR($"Invalid JSON ({GetType().Name}): {ConfigLocation} \n{ex}");
|
2016-03-24 01:43:11 +08:00
|
|
|
|
return GenerateDefaultConfig<T>();
|
2016-03-23 16:22:47 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret.WriteConfig();
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-03-27 13:09:09 +08:00
|
|
|
|
/// MUST be implemented in inheriting class!
|
2016-03-23 16:22:47 +08:00
|
|
|
|
/// </summary>
|
2016-03-24 00:28:50 +08:00
|
|
|
|
public virtual T GenerateDefaultConfig<T>() where T : Config
|
2016-03-23 16:22:47 +08:00
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-03-27 13:09:09 +08:00
|
|
|
|
/// Merges a default-value config with the user-config on disk.
|
2016-03-23 16:22:47 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <returns></returns>
|
2016-03-24 00:28:50 +08:00
|
|
|
|
public virtual T UpdateConfig<T>() where T : Config
|
2016-03-23 16:22:47 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
//default config
|
2016-03-27 13:09:09 +08:00
|
|
|
|
var b = JObject.FromObject(Instance<T>().GenerateDefaultConfig<T>(), new JsonSerializer {ContractResolver = new JsonResolver()});
|
2016-03-23 16:22:47 +08:00
|
|
|
|
|
|
|
|
|
//user config
|
2016-03-27 13:09:09 +08:00
|
|
|
|
var u = JObject.FromObject(this, new JsonSerializer {ContractResolver = new JsonResolver()});
|
2016-03-23 16:22:47 +08:00
|
|
|
|
|
|
|
|
|
//overwrite default values with user values
|
|
|
|
|
b.Merge(u, new JsonMergeSettings {MergeArrayHandling = MergeArrayHandling.Replace});
|
|
|
|
|
|
|
|
|
|
//cast json object to config
|
2016-03-27 13:09:09 +08:00
|
|
|
|
var c = b.ToObject<T>();
|
2016-03-23 16:22:47 +08:00
|
|
|
|
|
|
|
|
|
//re-write the location on disk to the object
|
|
|
|
|
c.ConfigLocation = ConfigLocation;
|
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncR("An error occured when updating a config: " + ex);
|
2016-03-23 16:22:47 +08:00
|
|
|
|
return this as T;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static class ConfigExtensions
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
2016-03-27 13:09:09 +08:00
|
|
|
|
/// Initializes an instance of any class that inherits from Config.
|
|
|
|
|
/// This method performs the loading, saving, and merging of the config on the disk and in memory at a default state.
|
|
|
|
|
/// This method should not be used to re-load or to re-save a config.
|
|
|
|
|
/// NOTE: You MUST set your config EQUAL to the return of this method!
|
2016-03-23 16:22:47 +08:00
|
|
|
|
/// </summary>
|
2016-03-24 01:43:11 +08:00
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
/// <param name="baseConfig"></param>
|
|
|
|
|
/// <param name="configLocation"></param>
|
|
|
|
|
/// <returns></returns>
|
2016-03-23 16:22:47 +08:00
|
|
|
|
public static T InitializeConfig<T>(this T baseConfig, string configLocation) where T : Config
|
|
|
|
|
{
|
|
|
|
|
if (baseConfig == null)
|
|
|
|
|
{
|
|
|
|
|
baseConfig = Activator.CreateInstance<T>();
|
|
|
|
|
/*
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncR("A config tried to initialize whilst being null.");
|
2016-03-23 16:22:47 +08:00
|
|
|
|
return null;
|
|
|
|
|
*/
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(configLocation))
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncR("A config tried to initialize without specifying a location on the disk.");
|
2016-03-23 16:22:47 +08:00
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
baseConfig.ConfigLocation = configLocation;
|
2016-03-27 13:09:09 +08:00
|
|
|
|
var c = baseConfig.LoadConfig<T>();
|
2016-03-23 16:22:47 +08:00
|
|
|
|
|
|
|
|
|
return c;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-03-27 13:09:09 +08:00
|
|
|
|
/// Writes a config to a json blob on the disk specified in the config's properties.
|
2016-03-23 16:22:47 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public static void WriteConfig<T>(this T baseConfig) where T : Config
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(baseConfig?.ConfigLocation) || string.IsNullOrEmpty(baseConfig.ConfigDir))
|
|
|
|
|
{
|
2016-03-27 13:09:09 +08:00
|
|
|
|
Log.AsyncR("A config attempted to save when it itself or it's location were null.");
|
2016-03-23 16:22:47 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-27 13:09:09 +08:00
|
|
|
|
var s = JsonConvert.SerializeObject(baseConfig, typeof (T), Formatting.Indented, new JsonSerializerSettings {ContractResolver = new JsonResolver()});
|
2016-03-23 16:22:47 +08:00
|
|
|
|
|
|
|
|
|
if (!Directory.Exists(baseConfig.ConfigDir))
|
|
|
|
|
Directory.CreateDirectory(baseConfig.ConfigDir);
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(baseConfig.ConfigLocation) || !File.ReadAllText(baseConfig.ConfigLocation).SequenceEqual(s))
|
|
|
|
|
File.WriteAllText(baseConfig.ConfigLocation, s);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2016-03-27 13:09:09 +08:00
|
|
|
|
/// Re-reads the json blob on the disk and merges its values with a default config
|
|
|
|
|
/// NOTE: You MUST set your config EQUAL to the return of this method!
|
2016-03-23 16:22:47 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
public static T ReloadConfig<T>(this T baseConfig) where T : Config
|
|
|
|
|
{
|
|
|
|
|
return baseConfig.UpdateConfig<T>();
|
|
|
|
|
}
|
2016-03-21 03:43:31 +08:00
|
|
|
|
}
|
|
|
|
|
}
|