-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathConfigFile.cs
More file actions
61 lines (55 loc) · 1.84 KB
/
ConfigFile.cs
File metadata and controls
61 lines (55 loc) · 1.84 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
extern alias References;
using System;
using System.Globalization;
using System.IO;
using References::Newtonsoft.Json;
namespace Oxide.Core.Configuration
{
/// <summary>
/// Represents a config file
/// </summary>
public abstract class ConfigFile
{
private static JsonSerializerSettings SerializerSettings = new JsonSerializerSettings()
{
DefaultValueHandling = DefaultValueHandling.Populate,
Culture = CultureInfo.InvariantCulture,
Formatting = Formatting.Indented,
MissingMemberHandling = MissingMemberHandling.Ignore
};
[JsonIgnore]
public string Filename { get; private set; }
protected ConfigFile(string filename)
{
Filename = filename;
}
/// <summary>
/// Loads a config from the specified file
/// </summary>
/// <param name="filename"></param>
public static T Load<T>(string filename) where T : ConfigFile
{
T config = (T)Activator.CreateInstance(typeof(T), filename);
config.Load();
return config;
}
/// <summary>
/// Loads this config from the specified file
/// </summary>
/// <param name="filename"></param>
public virtual void Load(string filename = null)
{
string source = File.ReadAllText(filename ?? Filename);
JsonConvert.PopulateObject(source, this, SerializerSettings);
}
/// <summary>
/// Saves this config to the specified file
/// </summary>
/// <param name="filename"></param>
public virtual void Save(string filename = null)
{
string source = JsonConvert.SerializeObject(this, SerializerSettings);
File.WriteAllText(filename ?? Filename, source);
}
}
}