SOTF.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace SOTF_Savegame_Editor.SOTFSave {
  7. public class SOTF {
  8. public const bool LOADIN_VALUES_ON_CONSTRUCTION = true;
  9. public static T? LoadJson<T>(string path) {
  10. string file = "";
  11. if (typeof(T) == typeof(SOTFSave.ConstructionsSaveData))
  12. file = "ConstructionsSaveData.json";
  13. else if (typeof(T) == typeof(SOTFSave.ZipLineManagerSaveData))
  14. file = "ZipLineManagerSaveData.json";
  15. else if (typeof(T) == typeof(SOTFSave.GameSetupSaveData))
  16. file = "GameSetupSaveData.json";
  17. else if (typeof(T) == typeof(SOTFSave.GameStateSaveData))
  18. file = "GameStateSaveData.json";
  19. else if (typeof(T) == typeof(SOTFSave.PlayerStateSaveData))
  20. file = "PlayerStateSaveData.json";
  21. else if (typeof(T) == typeof(SOTFSave.PlayerInventorySaveData))
  22. file = "PlayerInventorySaveData.json";
  23. else if (typeof(T) == typeof(SOTFSave.GardenPlotManagerSaveData))
  24. file = "GardenPlotManagerSaveData.json";
  25. else if (typeof(T) == typeof(SOTFSave.SaveData))
  26. file = "SaveData.json";
  27. else if (typeof(T) == typeof(SOTFSave.ScrewStructureInstancesSaveData))
  28. file = "ScrewStructureInstancesSaveData.json";
  29. else if (typeof(T) == typeof(SOTFSave.WorldObjectLocatorManagerSaveData))
  30. file = "WorldObjectLocatorManagerSaveData.json";
  31. else if (typeof(T) == typeof(SOTFSave.PlayerArmourSystemSaveData))
  32. file = "PlayerArmourSystemSaveData.json";
  33. else if (typeof(T) == typeof(SOTFSave.WorldItemManagerSaveData))
  34. file = "WorldItemManagerSaveData.json";
  35. if (file != "") {
  36. T res;
  37. System.Text.Json.JsonSerializerOptions settings = new System.Text.Json.JsonSerializerOptions();
  38. settings.PropertyNameCaseInsensitive = false;
  39. settings.DefaultBufferSize = int.MaxValue;
  40. /*try {
  41. using (var reader = new StreamReader(path + file)) {
  42. return System.Text.Json.JsonSerializer.Deserialize<T>(reader.ReadToEnd(), settings);
  43. }
  44. }catch(Exception) {
  45. }*/
  46. if (File.Exists(path + file)) {
  47. StreamReader? reader = null;
  48. try {
  49. using (reader = new StreamReader(path + file)) {
  50. res = System.Text.Json.JsonSerializer.Deserialize<T>(reader.ReadToEnd(), settings);
  51. reader.Close();
  52. }
  53. } finally {
  54. reader?.Dispose();
  55. }
  56. reader = null;
  57. return res;
  58. }
  59. /*string fileContent = File.ReadAllText(path + file);
  60. fileContent = fileContent.Replace("\"NaN\"", "0");
  61. return System.Text.Json.JsonSerializer.Deserialize<T>(fileContent, settings);*/
  62. }
  63. return default(T);
  64. }
  65. public static bool SaveJson(object? T, string path) {
  66. if (T == null) return false;
  67. string file = "";
  68. if (T.GetType() == typeof(SOTFSave.ConstructionsSaveData))
  69. file = "ConstructionsSaveData.json";
  70. else if (T.GetType() == typeof(SOTFSave.ZipLineManagerSaveData))
  71. file = "ZipLineManagerSaveData.json";
  72. else if (T.GetType() == typeof(SOTFSave.GameSetupSaveData))
  73. file = "GameSetupSaveData.json";
  74. else if (T.GetType() == typeof(SOTFSave.GameStateSaveData))
  75. file = "GameStateSaveData.json";
  76. else if (T.GetType() == typeof(SOTFSave.PlayerStateSaveData))
  77. file = "PlayerStateSaveData.json";
  78. else if (T.GetType() == typeof(SOTFSave.PlayerInventorySaveData))
  79. file = "PlayerInventorySaveData.json";
  80. else if (T.GetType() == typeof(SOTFSave.GardenPlotManagerSaveData))
  81. file = "GardenPlotManagerSaveData.json";
  82. else if (T.GetType() == typeof(SOTFSave.SaveData))
  83. file = "SaveData.json";
  84. else if (T.GetType() == typeof(SOTFSave.ScrewStructureInstancesSaveData))
  85. file = "ScrewStructureInstancesSaveData.json";
  86. else if (T.GetType() == typeof(SOTFSave.WorldObjectLocatorManagerSaveData))
  87. file = "WorldObjectLocatorManagerSaveData.json";
  88. else if (T.GetType() == typeof(SOTFSave.PlayerArmourSystemSaveData))
  89. file = "PlayerArmourSystemSaveData.json";
  90. else if (T.GetType() == typeof(SOTFSave.WorldItemManagerSaveData))
  91. file = "WorldItemManagerSaveData.json";
  92. if (file != "") {
  93. System.Text.Json.JsonSerializerOptions settings = new System.Text.Json.JsonSerializerOptions();
  94. //settings.PropertyNameCaseInsensitive = false;
  95. //settings.DefaultBufferSize = int.MaxValue;
  96. settings.IgnoreReadOnlyFields = true;
  97. settings.IgnoreReadOnlyProperties = true;
  98. settings.WriteIndented = true;
  99. settings.IncludeFields = false;
  100. settings.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
  101. string s = System.Text.Json.JsonSerializer.Serialize(T, settings);
  102. //Stream? stream = null;
  103. StreamWriter? writer = null;
  104. try {
  105. using (writer = new StreamWriter(path+file,false)) {
  106. writer.Write(s);
  107. writer.Close();
  108. writer.Dispose();
  109. }
  110. }
  111. finally {
  112. if(writer!=null)
  113. writer?.Dispose();
  114. }
  115. writer = null;
  116. return true;
  117. }
  118. return false;
  119. }
  120. public static int MapGraphMaskToAreaMask(int GrapthMask) {
  121. int res = 0;
  122. if ((GrapthMask | 1) == GrapthMask) res += 1;
  123. if ((GrapthMask | 2) == GrapthMask) res += 2;
  124. if ((GrapthMask | 4) == GrapthMask) res += 4;
  125. if ((GrapthMask | 8) == GrapthMask) res += 8;
  126. if ((GrapthMask | 16) == GrapthMask) res += 1048576;
  127. if ((GrapthMask | 32) == GrapthMask) res += 2097152;
  128. if ((GrapthMask | 64) == GrapthMask) res += 131072;
  129. if ((GrapthMask | 128) == GrapthMask) res += 262144;
  130. if ((GrapthMask | 256) == GrapthMask) res += 524288;
  131. if ((GrapthMask | 512) == GrapthMask) res += 32;
  132. if ((GrapthMask | 1024) == GrapthMask) res += 4194304;
  133. if ((GrapthMask | 2048) == GrapthMask) res += 16;
  134. if ((GrapthMask | 4096) == GrapthMask) res += 4096;
  135. return res;
  136. }
  137. public static int MapAreaMaskToGraphMask(int AreaMask) {
  138. int res = 0;
  139. if ((AreaMask | 1) == AreaMask) res += 1;
  140. if ((AreaMask | 2) == AreaMask) res += 2;
  141. if ((AreaMask | 4) == AreaMask) res += 4;
  142. if ((AreaMask | 8) == AreaMask) res += 8;
  143. if ((AreaMask | 1048576) == AreaMask) res += 16;
  144. if ((AreaMask | 2097152) == AreaMask) res += 32;
  145. if ((AreaMask | 131072) == AreaMask) res += 64;
  146. if ((AreaMask | 262144) == AreaMask) res += 128;
  147. if ((AreaMask | 524288) == AreaMask) res += 256;
  148. if ((AreaMask | 32) == AreaMask) res += 512;
  149. if ((AreaMask | 4194304) == AreaMask) res += 1024;
  150. if ((AreaMask | 16) == AreaMask) res += 2048;
  151. if ((AreaMask | 4096) == AreaMask) res += 4096;
  152. return res;
  153. }
  154. }
  155. }