forked from Skippeh/Oxide.GettingOverItMP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ServerConfig.cs
113 lines (93 loc) · 3.58 KB
/
ServerConfig.cs
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using ServerShared.Logging;
using ServerShared.Player;
namespace ServerShared
{
public class ServerConfig
{
private static readonly JsonSerializerSettings serializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
[JsonIgnore]
public string Directory { get; private set; }
public List<PlayerBan> Bans { get; private set; } = new List<PlayerBan>();
public List<PlayerAccessLevelIdentity> AccessLevels { get; private set; } = new List<PlayerAccessLevelIdentity>();
private ServerConfig(string directory)
{
Directory = directory ?? throw new ArgumentNullException(nameof(directory));
}
/// <summary>Do not use. Used for deserialization.</summary>
[JsonConstructor]
private ServerConfig() { }
public bool Save()
{
return SaveConfig(Directory, this);
}
public static bool LoadConfig(string directory, out ServerConfig config)
{
CheckDirectory(directory);
string savePath = Path.Combine(directory, "config.json");
Logger.LogDebug($"Loading config from {savePath}");
if (!File.Exists(savePath))
{
config = new ServerConfig(directory);
bool saveSuccessful = config.Save();
if (saveSuccessful)
Logger.LogWarning($"Created new config at {savePath}");
else
Logger.LogError($"Failed to create new config at {savePath}");
return saveSuccessful;
}
try
{
string json = File.ReadAllText(savePath);
config = JsonConvert.DeserializeObject<ServerConfig>(json, serializerSettings);
config.Directory = directory;
// Remove expired bans and save config.
if (config.RemoveExpiredBans() > 0)
config.Save();
return true;
}
catch (Exception ex) when (ex is IOException || ex is JsonException)
{
Logger.LogException("Failed to load player bans.", ex);
config = null;
return false;
}
}
public static bool SaveConfig(string directory, ServerConfig config)
{
CheckDirectory(directory);
try
{
using (var writer = File.CreateText(Path.Combine(directory, "config.json")))
{
string json = JsonConvert.SerializeObject(config, serializerSettings);
writer.Write(json);
}
return true;
}
catch (Exception ex) when (ex is IOException || ex is JsonException || ex is Exception)
{
Logger.LogException("Failed to save player bans.", ex);
return false;
}
}
private static void CheckDirectory(string directory)
{
if (!System.IO.Directory.Exists(directory))
System.IO.Directory.CreateDirectory(directory);
}
/// <summary>Removes all expired bans and returns the amount of bans removed.</summary>
public int RemoveExpiredBans()
{
return Bans.RemoveAll(ban => ban.Expired());
}
}
}