summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Advanced
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI/Advanced')
-rw-r--r--src/StardewModdingAPI/Advanced/ConfigFile.cs35
-rw-r--r--src/StardewModdingAPI/Advanced/IConfigFile.cs25
2 files changed, 60 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Advanced/ConfigFile.cs b/src/StardewModdingAPI/Advanced/ConfigFile.cs
new file mode 100644
index 00000000..000eaf4c
--- /dev/null
+++ b/src/StardewModdingAPI/Advanced/ConfigFile.cs
@@ -0,0 +1,35 @@
+using System.IO;
+using Newtonsoft.Json;
+
+namespace StardewModdingAPI.Advanced
+{
+ /// <summary>Wraps a configuration file with IO methods for convenience.</summary>
+ public abstract class ConfigFile : IConfigFile
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>Provides methods for interacting with the mod directory, including read/writing the config file.</summary>
+ public ModHelper ModHelper { get; set; }
+
+ /// <summary>The file path from which the model was loaded, relative to the mod directory.</summary>
+ public string FilePath { get; set; }
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Reparse the underlying file and update this model.</summary>
+ public void Reload()
+ {
+ string json = File.ReadAllText(Path.Combine(this.ModHelper.DirectoryPath, this.FilePath));
+ JsonConvert.PopulateObject(json, this);
+ }
+
+ /// <summary>Save this model to the underlying file.</summary>
+ public void Save()
+ {
+ this.ModHelper.WriteJsonFile(this.FilePath, this);
+ }
+ }
+} \ No newline at end of file
diff --git a/src/StardewModdingAPI/Advanced/IConfigFile.cs b/src/StardewModdingAPI/Advanced/IConfigFile.cs
new file mode 100644
index 00000000..78fd44a6
--- /dev/null
+++ b/src/StardewModdingAPI/Advanced/IConfigFile.cs
@@ -0,0 +1,25 @@
+namespace StardewModdingAPI.Advanced
+{
+ /// <summary>Wraps a configuration file with IO methods for convenience.</summary>
+ public interface IConfigFile
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>Provides methods for interacting with the mod directory, including read/writing the config file.</summary>
+ ModHelper ModHelper { get; set; }
+
+ /// <summary>The file path from which the model was loaded, relative to the mod directory.</summary>
+ string FilePath { get; set; }
+
+
+ /*********
+ ** Methods
+ *********/
+ /// <summary>Reparse the underlying file and update this model.</summary>
+ void Reload();
+
+ /// <summary>Save this model to the underlying file.</summary>
+ void Save();
+ }
+}