PlayerStateSaveData.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using SOTF_Struct.Data;
  2. using SOTF_Struct.Generic;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace SOTF_Savegame_Editor.SOTFSave {
  9. public class PlayerStateSaveData {
  10. public class DataObject {
  11. public string PlayerState { get; set; }
  12. }
  13. public string Version { get; set; }
  14. public DataObject Data { get; set; }
  15. protected PlayerState? _data = null;
  16. public PlayerState? PlayerState {
  17. get {
  18. if (this._data == null) {
  19. System.Text.Json.JsonSerializerOptions options = new() {
  20. NumberHandling = System.Text.Json.Serialization.JsonNumberHandling.AllowReadingFromString | System.Text.Json.Serialization.JsonNumberHandling.AllowNamedFloatingPointLiterals,
  21. AllowTrailingCommas = true,
  22. IgnoreReadOnlyFields = true,
  23. IgnoreReadOnlyProperties = true
  24. };
  25. this._data = System.Text.Json.JsonSerializer.Deserialize<PlayerState>(this.Data.PlayerState.Replace("\"NaN\"", "0"), options);
  26. }
  27. return this._data;
  28. }
  29. }
  30. public void Set(string key, bool value) {
  31. this.PlayerState.Set(key, value);
  32. }
  33. public void Set(string key, int value) {
  34. this.PlayerState.Set(key, value);
  35. }
  36. public void Set(string key, float value) {
  37. this.PlayerState.Set(key, value);
  38. }
  39. public void Set(string key, double value) {
  40. this.PlayerState.Set(key, value);
  41. }
  42. public void Set(string key, string value) {
  43. this.PlayerState.Set(key, value);
  44. }
  45. public void Set(string key, float[] value) {
  46. this.PlayerState.Set(key, value);
  47. }
  48. public void Set(string key, double[] value) {
  49. this.PlayerState.Set(key, value);
  50. }
  51. public dynamic GetSetting(string key) {
  52. if (this._data == null) {
  53. this._data = System.Text.Json.JsonSerializer.Deserialize<PlayerState>(this.Data.PlayerState.Replace("\"NaN\"", "0"));
  54. }
  55. if (this._data == null) {
  56. System.Diagnostics.Debug.WriteLine("PlayerState._data is null");
  57. return "";
  58. }
  59. foreach (Settings setting in this._data._entries)
  60. if (setting.Name == key)
  61. if (setting.SettingType == 3)
  62. return setting.StringValue;
  63. else if (setting.SettingType == 0)
  64. return setting.BoolValue;
  65. return "";
  66. }
  67. public string GetString(string key) {
  68. if (this._data == null) {
  69. this._data = System.Text.Json.JsonSerializer.Deserialize<PlayerState>(this.Data.PlayerState.Replace("\"NaN\"", "0"));
  70. }
  71. if (this._data == null) {
  72. System.Diagnostics.Debug.WriteLine("PlayerState._data is null");
  73. return "";
  74. }
  75. foreach (Settings setting in _data._entries)
  76. if (setting.Name == key)
  77. return setting.StringValue;
  78. return "";
  79. }
  80. public int GetInt(string key) {
  81. if (this._data == null) {
  82. this._data = System.Text.Json.JsonSerializer.Deserialize<PlayerState>(this.Data.PlayerState.Replace("\"NaN\"", "0"));
  83. }
  84. if (this._data == null) {
  85. System.Diagnostics.Debug.WriteLine("PlayerState._data is null");
  86. return 0;
  87. }
  88. foreach (Settings setting in _data._entries)
  89. if (setting.Name == key)
  90. return setting.IntValue;
  91. return 0;
  92. }
  93. public double GetFloat(string key) {
  94. if (this._data == null) {
  95. this._data = System.Text.Json.JsonSerializer.Deserialize<PlayerState>(this.Data.PlayerState.Replace("\"NaN\"", "0"));
  96. }
  97. if (this._data == null) {
  98. System.Diagnostics.Debug.WriteLine("PlayerState._data is null");
  99. return 0.0f;
  100. }
  101. foreach (Settings setting in _data._entries)
  102. if (setting.Name == key)
  103. return setting.FloatValue;
  104. return 0.0f;
  105. }
  106. public bool GetBool(string key) {
  107. if (this._data == null) {
  108. this._data = System.Text.Json.JsonSerializer.Deserialize<PlayerState>(this.Data.PlayerState.Replace("\"NaN\"", "0"));
  109. }
  110. if (this._data == null) {
  111. System.Diagnostics.Debug.WriteLine("PlayerState._data is null");
  112. return false;
  113. }
  114. foreach (Settings setting in _data._entries)
  115. if (setting.Name == key)
  116. return setting.BoolValue;
  117. return false;
  118. }
  119. public double[] GetFloatArray(string key) {
  120. if (this._data == null) {
  121. this._data = System.Text.Json.JsonSerializer.Deserialize<PlayerState>(this.Data.PlayerState.Replace("\"NaN\"", "0"));
  122. }
  123. if (this._data == null) {
  124. System.Diagnostics.Debug.WriteLine("PlayerState._data is null");
  125. return new double[] { };
  126. }
  127. foreach (Settings setting in _data._entries)
  128. if (setting.Name == key)
  129. return setting.FloatArrayValue;
  130. return new double[] { };
  131. }
  132. }
  133. }