CLre/CLre_server/API/Config/CLreConfig.cs

64 lines
1.7 KiB
C#
Raw Normal View History

using System;
using System.IO;
namespace CLre_server.API.Config
{
[Serializable]
public struct CLreConfig
{
public bool clre_clients_only;
public bool web_server;
public bool terrain_exclusion_zone;
public static CLreConfig Default()
{
return new CLreConfig
{
clre_clients_only = false,
web_server = false,
terrain_exclusion_zone = false,
};
}
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);
}
}
}