56 lines
1.7 KiB
C#
56 lines
1.7 KiB
C#
using System.IO;
|
|
using Newtonsoft.Json;
|
|
using UnityEngine;
|
|
|
|
public static class JsonDataHandler
|
|
{
|
|
private const string SaveCategory = "generic_json_data";
|
|
private static readonly string FileSuffix = ".json";
|
|
|
|
public static string File_Path_Folder => Path.Combine(Application.persistentDataPath, "FILE");
|
|
|
|
public static string File_Path(string file_Name)
|
|
{
|
|
return Path.Combine(File_Path_Folder, file_Name + FileSuffix);
|
|
}
|
|
|
|
public static void Save<T>(T data, string file_Name, string saver_Name)
|
|
{
|
|
string json = JsonConvert.SerializeObject(data, Formatting.Indented);
|
|
string legacyPath = File_Path(file_Name);
|
|
|
|
if (!SecureSaveVault.SaveRawJson(SaveCategory, file_Name, json, legacyPath))
|
|
{
|
|
Debug.LogWarning($"[JsonDataHandler] Save failed: {file_Name}");
|
|
return;
|
|
}
|
|
|
|
Debug.Log($"_Save_[{file_Name}{FileSuffix}]_FROM_[{saver_Name}]\nSECURE::{SaveCategory}/{file_Name}");
|
|
}
|
|
|
|
public static bool Try_Load<T>(ref T data, string file_Name)
|
|
{
|
|
string json;
|
|
if (!SecureSaveVault.TryLoadRawJson(SaveCategory, file_Name, out json, File_Path(file_Name)))
|
|
{
|
|
Debug.Log($"Load_[{file_Name}]_NULL");
|
|
return false;
|
|
}
|
|
|
|
data = JsonConvert.DeserializeObject<T>(json);
|
|
return true;
|
|
}
|
|
|
|
public static T Load<T>(string file_Name)
|
|
{
|
|
string json;
|
|
if (!SecureSaveVault.TryLoadRawJson(SaveCategory, file_Name, out json, File_Path(file_Name)))
|
|
{
|
|
Debug.Log($"Load_[{file_Name}]_NULL");
|
|
return default;
|
|
}
|
|
|
|
return JsonConvert.DeserializeObject<T>(json);
|
|
}
|
|
}
|