Skip to content

Commit b66459a

Browse files
committed
Add value handling for invalid and default values
This should probably be handled better once the config system gets a overhaul.
1 parent 5b83df7 commit b66459a

1 file changed

Lines changed: 90 additions & 4 deletions

File tree

src/Configuration/OxideConfig.cs

Lines changed: 90 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
extern alias References;
1+
extern alias References;
22

33
using References::Newtonsoft.Json;
44
using System.Collections;
55
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Net;
68

79
namespace Oxide.Core.Configuration
810
{
@@ -145,9 +147,93 @@ public class OxideRcon
145147
/// </summary>
146148
public OxideConfig(string filename) : base(filename)
147149
{
148-
Options = new OxideOptions { Modded = true, PluginWatchers = true, DefaultGroups = new DefaultGroups { Administrators = "admin", Players = "default" }, WebRequestIP = "0.0.0.0" };
149-
Console = new OxideConsole { Enabled = true, MinimalistMode = true, ShowStatusBar = true };
150-
Rcon = new OxideRcon { Enabled = false, ChatPrefix = "[Server Console]", Port = 25580, Password = string.Empty };
150+
InitializeDefaultValues();
151+
}
152+
153+
public override void Load(string filename = null)
154+
{
155+
base.Load(filename);
156+
157+
if (InitializeDefaultValues())
158+
{
159+
Save();
160+
}
161+
162+
if (Compiler.PreprocessorDirectives.Count > 0)
163+
{
164+
Compiler.PreprocessorDirectives = Compiler.PreprocessorDirectives
165+
.Select(s => s.ToUpperInvariant().Replace(" ", "_"))
166+
.Distinct()
167+
.ToList();
168+
}
169+
170+
Commands.ChatPrefix = Commands.ChatPrefix.Distinct().ToList();
171+
}
172+
173+
private bool InitializeDefaultValues()
174+
{
175+
bool changed = false;
176+
if (Options == null)
177+
{
178+
Options = new OxideOptions();
179+
changed = true;
180+
}
181+
182+
if (Commands == null)
183+
{
184+
Commands = new CommandOptions();
185+
changed = true;
186+
}
187+
188+
if (Commands.ChatPrefix == null)
189+
{
190+
Commands.ChatPrefix = new List<string>() { "/" };
191+
changed = true;
192+
}
193+
194+
if (Commands.ChatPrefix.Count == 0)
195+
{
196+
Commands.ChatPrefix.Add("/");
197+
changed = true;
198+
}
199+
200+
if (Options.DefaultGroups == null)
201+
{
202+
Options.DefaultGroups = new DefaultGroups();
203+
changed = true;
204+
}
205+
206+
if (string.IsNullOrEmpty(Options.WebRequestIP) || !IPAddress.TryParse(Options.WebRequestIP, out IPAddress address))
207+
{
208+
Options.WebRequestIP = "0.0.0.0";
209+
changed = true;
210+
}
211+
212+
if (Console == null)
213+
{
214+
Console = new OxideConsole();
215+
changed = true;
216+
}
217+
218+
if (Rcon == null)
219+
{
220+
Rcon = new OxideRcon();
221+
changed = true;
222+
}
223+
224+
if (Compiler == null)
225+
{
226+
Compiler = new CompilerOptions();
227+
changed = true;
228+
}
229+
230+
if (Compiler.PreprocessorDirectives == null)
231+
{
232+
Compiler.PreprocessorDirectives = new List<string>();
233+
changed = true;
234+
}
235+
236+
return changed;
151237
}
152238
}
153239
}

0 commit comments

Comments
 (0)