CLre/CLre_server/API/Config/CLreConfig.cs

80 lines
2.2 KiB
C#
Raw Normal View History

using System;
using System.IO;
using System.Text;
namespace CLre_server.API.Config
{
[Serializable]
public struct CLreConfig
{
public bool clre_clients_only;
public bool web_server;
public bool terrain_exclusion_zone;
public bool chat_commands;
public string email_address;
public string password;
public string[] bans;
public string[] moderators;
public static CLreConfig Default()
{
return new CLreConfig
{
clre_clients_only = false,
web_server = false,
terrain_exclusion_zone = false,
chat_commands = false,
email_address = "email@address.com",
password = "s3cur3-password",
bans = new string[0],
moderators = new []{ "NGuiness", "NGnius", "Zhang"},
};
}
public static CLreConfig FromString(string s)
{
return UnityEngine.JsonUtility.FromJson<CLreConfig>(s);
}
public static CLreConfig FromFile(string path)
{
string s = File.ReadAllText(path);
return FromString(s);
}
public static CLreConfig FromFileSafely(string path)
{
// try to load config file
CLreConfig conf = Default();
try
{
string s = File.ReadAllText(path);
conf = FromString(s);
}
catch (Exception e)
{
Utility.Logging.LogWarning($"Failed to load {path}: {e}\n{e.StackTrace}");
try
{
File.WriteAllText(path, conf.ToString());
}
catch (Exception e2)
{
Utility.Logging.LogWarning($"Failed to write {path}: {e2}\n{e2.StackTrace}");
}
}
return conf;
}
public override string ToString()
{
return UnityEngine.JsonUtility.ToJson(this, true);
}
public void ToFile(string path)
{
File.WriteAllText(path, this.ToString());
}
}
}