summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Advanced/ConfigFile.cs
blob: 1aba2f2c1e8cc53f2204ae3fe42a989bc928d6f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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 IModHelper 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);
        }
    }
}