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.

64 lines
1.7KB

  1. using System;
  2. using System.IO;
  3. namespace CLre_server.API.Config
  4. {
  5. [Serializable]
  6. public struct CLreConfig
  7. {
  8. public bool clre_clients_only;
  9. public bool web_server;
  10. public bool terrain_exclusion_zone;
  11. public static CLreConfig Default()
  12. {
  13. return new CLreConfig
  14. {
  15. clre_clients_only = false,
  16. web_server = false,
  17. terrain_exclusion_zone = false,
  18. };
  19. }
  20. public static CLreConfig FromString(string s)
  21. {
  22. return UnityEngine.JsonUtility.FromJson<CLreConfig>(s);
  23. }
  24. public static CLreConfig FromFile(string path)
  25. {
  26. string s = File.ReadAllText(path);
  27. return FromString(s);
  28. }
  29. public static CLreConfig FromFileSafely(string path)
  30. {
  31. // try to load config file
  32. CLreConfig conf = Default();
  33. try
  34. {
  35. string s = File.ReadAllText(path);
  36. conf = FromString(s);
  37. }
  38. catch (Exception e)
  39. {
  40. Utility.Logging.LogWarning($"Failed to load {path}: {e}\n{e.StackTrace}");
  41. try
  42. {
  43. File.WriteAllText(path, conf.ToString());
  44. }
  45. catch (Exception e2)
  46. {
  47. Utility.Logging.LogWarning($"Failed to write {path}: {e2}\n{e2.StackTrace}");
  48. }
  49. }
  50. return conf;
  51. }
  52. public override string ToString()
  53. {
  54. return UnityEngine.JsonUtility.ToJson(this, true);
  55. }
  56. }
  57. }