| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace SOTF_Savegame_Editor.SOTFSave {
- public class SOTF {
- public const bool LOADIN_VALUES_ON_CONSTRUCTION = true;
- public static T? LoadJson<T>(string path) {
- string file = "";
- if (typeof(T) == typeof(SOTFSave.ConstructionsSaveData))
- file = "ConstructionsSaveData.json";
- else if (typeof(T) == typeof(SOTFSave.ZipLineManagerSaveData))
- file = "ZipLineManagerSaveData.json";
- else if (typeof(T) == typeof(SOTFSave.GameSetupSaveData))
- file = "GameSetupSaveData.json";
- else if (typeof(T) == typeof(SOTFSave.GameStateSaveData))
- file = "GameStateSaveData.json";
- else if (typeof(T) == typeof(SOTFSave.PlayerStateSaveData))
- file = "PlayerStateSaveData.json";
- else if (typeof(T) == typeof(SOTFSave.PlayerInventorySaveData))
- file = "PlayerInventorySaveData.json";
- else if (typeof(T) == typeof(SOTFSave.GardenPlotManagerSaveData))
- file = "GardenPlotManagerSaveData.json";
- else if (typeof(T) == typeof(SOTFSave.SaveData))
- file = "SaveData.json";
- else if (typeof(T) == typeof(SOTFSave.ScrewStructureInstancesSaveData))
- file = "ScrewStructureInstancesSaveData.json";
- else if (typeof(T) == typeof(SOTFSave.WorldObjectLocatorManagerSaveData))
- file = "WorldObjectLocatorManagerSaveData.json";
- else if (typeof(T) == typeof(SOTFSave.PlayerArmourSystemSaveData))
- file = "PlayerArmourSystemSaveData.json";
- else if (typeof(T) == typeof(SOTFSave.WorldItemManagerSaveData))
- file = "WorldItemManagerSaveData.json";
- if (file != "") {
- T res;
- System.Text.Json.JsonSerializerOptions settings = new System.Text.Json.JsonSerializerOptions();
- settings.PropertyNameCaseInsensitive = false;
- settings.DefaultBufferSize = int.MaxValue;
- /*try {
- using (var reader = new StreamReader(path + file)) {
- return System.Text.Json.JsonSerializer.Deserialize<T>(reader.ReadToEnd(), settings);
- }
- }catch(Exception) {
-
- }*/
- if (File.Exists(path + file)) {
- StreamReader? reader = null;
- try {
- using (reader = new StreamReader(path + file)) {
- res = System.Text.Json.JsonSerializer.Deserialize<T>(reader.ReadToEnd(), settings);
- reader.Close();
- }
- } finally {
- reader?.Dispose();
- }
- reader = null;
- return res;
- }
- /*string fileContent = File.ReadAllText(path + file);
- fileContent = fileContent.Replace("\"NaN\"", "0");
- return System.Text.Json.JsonSerializer.Deserialize<T>(fileContent, settings);*/
- }
- return default(T);
- }
- public static bool SaveJson(object? T, string path) {
- if (T == null) return false;
- string file = "";
- if (T.GetType() == typeof(SOTFSave.ConstructionsSaveData))
- file = "ConstructionsSaveData.json";
- else if (T.GetType() == typeof(SOTFSave.ZipLineManagerSaveData))
- file = "ZipLineManagerSaveData.json";
- else if (T.GetType() == typeof(SOTFSave.GameSetupSaveData))
- file = "GameSetupSaveData.json";
- else if (T.GetType() == typeof(SOTFSave.GameStateSaveData))
- file = "GameStateSaveData.json";
- else if (T.GetType() == typeof(SOTFSave.PlayerStateSaveData))
- file = "PlayerStateSaveData.json";
- else if (T.GetType() == typeof(SOTFSave.PlayerInventorySaveData))
- file = "PlayerInventorySaveData.json";
- else if (T.GetType() == typeof(SOTFSave.GardenPlotManagerSaveData))
- file = "GardenPlotManagerSaveData.json";
- else if (T.GetType() == typeof(SOTFSave.SaveData))
- file = "SaveData.json";
- else if (T.GetType() == typeof(SOTFSave.ScrewStructureInstancesSaveData))
- file = "ScrewStructureInstancesSaveData.json";
- else if (T.GetType() == typeof(SOTFSave.WorldObjectLocatorManagerSaveData))
- file = "WorldObjectLocatorManagerSaveData.json";
- else if (T.GetType() == typeof(SOTFSave.PlayerArmourSystemSaveData))
- file = "PlayerArmourSystemSaveData.json";
- else if (T.GetType() == typeof(SOTFSave.WorldItemManagerSaveData))
- file = "WorldItemManagerSaveData.json";
- if (file != "") {
- System.Text.Json.JsonSerializerOptions settings = new System.Text.Json.JsonSerializerOptions();
- //settings.PropertyNameCaseInsensitive = false;
- //settings.DefaultBufferSize = int.MaxValue;
- settings.IgnoreReadOnlyFields = true;
- settings.IgnoreReadOnlyProperties = true;
- settings.WriteIndented = true;
- settings.IncludeFields = false;
- settings.Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping;
- string s = System.Text.Json.JsonSerializer.Serialize(T, settings);
- //Stream? stream = null;
- StreamWriter? writer = null;
- try {
- using (writer = new StreamWriter(path+file,false)) {
- writer.Write(s);
- writer.Close();
- writer.Dispose();
- }
- }
- finally {
- if(writer!=null)
- writer?.Dispose();
- }
- writer = null;
- return true;
- }
- return false;
- }
- public static int MapGraphMaskToAreaMask(int GrapthMask) {
- int res = 0;
- if ((GrapthMask | 1) == GrapthMask) res += 1;
- if ((GrapthMask | 2) == GrapthMask) res += 2;
- if ((GrapthMask | 4) == GrapthMask) res += 4;
- if ((GrapthMask | 8) == GrapthMask) res += 8;
- if ((GrapthMask | 16) == GrapthMask) res += 1048576;
- if ((GrapthMask | 32) == GrapthMask) res += 2097152;
- if ((GrapthMask | 64) == GrapthMask) res += 131072;
- if ((GrapthMask | 128) == GrapthMask) res += 262144;
- if ((GrapthMask | 256) == GrapthMask) res += 524288;
- if ((GrapthMask | 512) == GrapthMask) res += 32;
- if ((GrapthMask | 1024) == GrapthMask) res += 4194304;
- if ((GrapthMask | 2048) == GrapthMask) res += 16;
- if ((GrapthMask | 4096) == GrapthMask) res += 4096;
- return res;
- }
- public static int MapAreaMaskToGraphMask(int AreaMask) {
- int res = 0;
- if ((AreaMask | 1) == AreaMask) res += 1;
- if ((AreaMask | 2) == AreaMask) res += 2;
- if ((AreaMask | 4) == AreaMask) res += 4;
- if ((AreaMask | 8) == AreaMask) res += 8;
- if ((AreaMask | 1048576) == AreaMask) res += 16;
- if ((AreaMask | 2097152) == AreaMask) res += 32;
- if ((AreaMask | 131072) == AreaMask) res += 64;
- if ((AreaMask | 262144) == AreaMask) res += 128;
- if ((AreaMask | 524288) == AreaMask) res += 256;
- if ((AreaMask | 32) == AreaMask) res += 512;
- if ((AreaMask | 4194304) == AreaMask) res += 1024;
- if ((AreaMask | 16) == AreaMask) res += 2048;
- if ((AreaMask | 4096) == AreaMask) res += 4096;
- return res;
- }
- }
- }
|