Unofficial CardLife revival project, pronounced like "celery"
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.2KB

  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. namespace CLre_server.API.Config
  5. {
  6. [Serializable]
  7. public struct CLreConfig
  8. {
  9. public bool clre_clients_only;
  10. public bool web_server;
  11. public bool terrain_exclusion_zone;
  12. public bool chat_commands;
  13. public string email_address;
  14. public string password;
  15. public string[] bans;
  16. public string[] moderators;
  17. public static CLreConfig Default()
  18. {
  19. return new CLreConfig
  20. {
  21. clre_clients_only = false,
  22. web_server = false,
  23. terrain_exclusion_zone = false,
  24. chat_commands = false,
  25. email_address = "email@address.com",
  26. password = "s3cur3-password",
  27. bans = new string[0],
  28. moderators = new []{ "NGuiness", "NGnius", "Zhang"},
  29. };
  30. }
  31. public static CLreConfig FromString(string s)
  32. {
  33. return UnityEngine.JsonUtility.FromJson<CLreConfig>(s);
  34. }
  35. public static CLreConfig FromFile(string path)
  36. {
  37. string s = File.ReadAllText(path);
  38. return FromString(s);
  39. }
  40. public static CLreConfig FromFileSafely(string path)
  41. {
  42. // try to load config file
  43. CLreConfig conf = Default();
  44. try
  45. {
  46. string s = File.ReadAllText(path);
  47. conf = FromString(s);
  48. }
  49. catch (Exception e)
  50. {
  51. Utility.Logging.LogWarning($"Failed to load {path}: {e}\n{e.StackTrace}");
  52. try
  53. {
  54. File.WriteAllText(path, conf.ToString());
  55. }
  56. catch (Exception e2)
  57. {
  58. Utility.Logging.LogWarning($"Failed to write {path}: {e2}\n{e2.StackTrace}");
  59. }
  60. }
  61. return conf;
  62. }
  63. public override string ToString()
  64. {
  65. return UnityEngine.JsonUtility.ToJson(this, true);
  66. }
  67. public void ToFile(string path)
  68. {
  69. File.WriteAllText(path, this.ToString());
  70. }
  71. }
  72. }