using System;
using System.IO;
using Newtonsoft.Json;
using StardewModdingAPI.Advanced;
using StardewModdingAPI.Framework.Reflection;
namespace StardewModdingAPI.Framework
{
/// Provides simplified APIs for writing mods.
internal class ModHelper : IModHelper
{
/*********
** Properties
*********/
/// The JSON settings to use when serialising and deserialising files.
private readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ObjectCreationHandling = ObjectCreationHandling.Replace // avoid issue where default ICollection values are duplicated each time the config is loaded
};
/*********
** Accessors
*********/
/// The mod directory path.
public string DirectoryPath { get; }
/// Simplifies access to private game code.
public IReflectionHelper Reflection { get; } = new ReflectionHelper();
/// Metadata about loaded mods.
public IModRegistry ModRegistry { get; }
/*********
** Public methods
*********/
/// Construct an instance.
/// The mod directory path.
/// Metadata about loaded mods.
/// An argument is null or invalid.
/// The path does not exist on disk.
public ModHelper(string modDirectory, IModRegistry modRegistry)
{
// validate
if (modRegistry == null)
throw new ArgumentException("The mod registry cannot be null.");
if (string.IsNullOrWhiteSpace(modDirectory))
throw new ArgumentException("The mod directory cannot be empty.");
if (!Directory.Exists(modDirectory))
throw new InvalidOperationException("The specified mod directory does not exist.");
// initialise
this.DirectoryPath = modDirectory;
this.ModRegistry = modRegistry;
}
/****
** Mod config file
****/
/// Read the mod's configuration file (and create it if needed).
/// The config class type. This should be a plain class that has public properties for the settings you want. These can be complex types.
public TConfig ReadConfig()
where TConfig : class, new()
{
var config = this.ReadJsonFile("config.json") ?? new TConfig();
this.WriteConfig(config); // create file or fill in missing fields
return config;
}
/// Save to the mod's configuration file.
/// The config class type.
/// The config settings to save.
public void WriteConfig(TConfig config)
where TConfig : class, new()
{
this.WriteJsonFile("config.json", config);
}
/****
** Generic JSON files
****/
/// Read a JSON file.
/// The model type.
/// The file path relative to the mod directory.
/// Returns the deserialised model, or null if the file doesn't exist or is empty.
public TModel ReadJsonFile(string path)
where TModel : class
{
// read file
string fullPath = Path.Combine(this.DirectoryPath, path);
string json;
try
{
json = File.ReadAllText(fullPath);
}
catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException)
{
return null;
}
// deserialise model
TModel model = JsonConvert.DeserializeObject(json, this.JsonSettings);
if (model is IConfigFile)
{
var wrapper = (IConfigFile)model;
wrapper.ModHelper = this;
wrapper.FilePath = path;
}
return model;
}
/// Save to a JSON file.
/// The model type.
/// The file path relative to the mod directory.
/// The model to save.
public void WriteJsonFile(string path, TModel model)
where TModel : class
{
path = Path.Combine(this.DirectoryPath, path);
// create directory if needed
string dir = Path.GetDirectoryName(path);
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
// write file
string json = JsonConvert.SerializeObject(model, this.JsonSettings);
File.WriteAllText(path, json);
}
}
}