2022-08-17 18:37:55 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2022-08-25 23:31:20 +02:00
|
|
|
using System;
|
2022-08-17 18:37:55 +02:00
|
|
|
|
|
|
|
public static class JsonConfig {
|
|
|
|
public static bool hasInitialized = false;
|
2022-08-25 23:31:20 +02:00
|
|
|
public static JObject config;
|
2022-08-17 18:37:55 +02:00
|
|
|
|
|
|
|
private static void ensureInitialization() {
|
|
|
|
if (hasInitialized)
|
|
|
|
return;
|
|
|
|
loadFile();
|
|
|
|
hasInitialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static string getFileName() {
|
|
|
|
return Application.dataPath + "/../config.json";
|
|
|
|
}
|
|
|
|
private static void saveFile() {
|
2022-08-25 23:31:20 +02:00
|
|
|
if (JsonConfigBehavior.instance != null)
|
|
|
|
JsonConfigBehavior.saveFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static IEnumerator saveFileWait() {
|
|
|
|
yield return new WaitForSeconds(1.5f); //
|
|
|
|
sort(config);
|
2022-08-17 18:37:55 +02:00
|
|
|
File.WriteAllText(getFileName(), config.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void loadFile() {
|
|
|
|
if (File.Exists(getFileName()))
|
|
|
|
config = JObject.Parse(File.ReadAllText(getFileName()));
|
|
|
|
else {
|
|
|
|
config = new JObject();
|
|
|
|
saveFile();
|
|
|
|
}
|
|
|
|
}
|
2022-08-25 23:31:20 +02:00
|
|
|
private static void sort(JObject jObj)
|
|
|
|
{
|
|
|
|
var props = jObj.Properties().ToList();
|
|
|
|
foreach (var prop in props)
|
|
|
|
{
|
|
|
|
prop.Remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var prop in props.OrderBy(p=>p.Name))
|
|
|
|
{
|
|
|
|
jObj.Add(prop);
|
|
|
|
if(prop.Value is JObject)
|
|
|
|
sort((JObject)prop.Value);
|
|
|
|
}
|
|
|
|
}
|
2022-08-17 18:37:55 +02:00
|
|
|
|
|
|
|
public static void DeleteAll() {
|
|
|
|
ensureInitialization();
|
|
|
|
config.RemoveAll();
|
|
|
|
saveFile();
|
|
|
|
}
|
|
|
|
public static void DeleteKey(string key) {
|
|
|
|
ensureInitialization();
|
|
|
|
config.Remove(key);
|
|
|
|
saveFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static bool HasKey(string key) {
|
|
|
|
ensureInitialization();
|
|
|
|
return config.ContainsKey(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void SetBoolean(string key, bool boolean) {
|
|
|
|
ensureInitialization();
|
|
|
|
config[key] = boolean;
|
|
|
|
saveFile();
|
|
|
|
}
|
|
|
|
public static void SetString(string key, string text) {
|
|
|
|
ensureInitialization();
|
|
|
|
config[key] = text;
|
|
|
|
saveFile();
|
|
|
|
}
|
|
|
|
public static void SetInt(string key, int number) {
|
|
|
|
ensureInitialization();
|
|
|
|
config[key] = number;
|
|
|
|
saveFile();
|
|
|
|
}
|
|
|
|
public static void SetDouble(string key, double number) {
|
|
|
|
ensureInitialization();
|
|
|
|
config[key] = number;
|
|
|
|
saveFile();
|
|
|
|
}
|
|
|
|
public static void SetFloatArray(string key, float[] numbers) {
|
|
|
|
ensureInitialization();
|
|
|
|
config[key] = JArray.FromObject(numbers);
|
|
|
|
saveFile();
|
|
|
|
}
|
2022-08-17 23:15:59 +02:00
|
|
|
public static void SetVector3(string key, Vector3 vector) {
|
|
|
|
SetFloatArray(key, new float[] { vector.x, vector.y, vector.z });
|
|
|
|
}
|
|
|
|
public static void SetQuaternion(string key, Quaternion quaternion) {
|
|
|
|
SetFloatArray(key, new float[] { quaternion.x, quaternion.y, quaternion.z, quaternion.w });
|
|
|
|
}
|
2022-08-25 23:31:20 +02:00
|
|
|
public static void SetJObject(string key, JObject value) {
|
|
|
|
ensureInitialization();
|
|
|
|
config[key] = JObject.FromObject(value);
|
|
|
|
saveFile();
|
|
|
|
}
|
2022-08-17 18:37:55 +02:00
|
|
|
|
|
|
|
public static bool GetBoolean(string key) {
|
|
|
|
ensureInitialization();
|
|
|
|
return config.Value<bool>(key);
|
|
|
|
}
|
|
|
|
public static string GetString(string key) {
|
|
|
|
ensureInitialization();
|
|
|
|
return config.Value<string>(key);
|
|
|
|
}
|
|
|
|
public static int GetInt(string key) {
|
|
|
|
ensureInitialization();
|
|
|
|
return config.Value<int>(key);
|
|
|
|
}
|
|
|
|
public static double GetDouble(string key) {
|
|
|
|
ensureInitialization();
|
|
|
|
return config.Value<double>(key);
|
|
|
|
}
|
|
|
|
public static float[] GetFloatArray(string key) {
|
|
|
|
ensureInitialization();
|
|
|
|
return config.Value<JArray>(key).ToObject<float[]>();
|
|
|
|
}
|
2022-08-17 23:15:59 +02:00
|
|
|
public static Vector3 GetVector3(string key) {
|
|
|
|
return new Vector3(GetFloatArray(key)[0], GetFloatArray(key)[1], GetFloatArray(key)[2]);
|
|
|
|
}
|
|
|
|
public static Quaternion GetQuaternion(string key) {
|
|
|
|
return new Quaternion(GetFloatArray(key)[0], GetFloatArray(key)[1], GetFloatArray(key)[2], GetFloatArray(key)[3]);
|
|
|
|
}
|
2022-08-25 23:31:20 +02:00
|
|
|
public static JObject GetJObject(string key) {
|
|
|
|
ensureInitialization();
|
|
|
|
return config.Value<JObject>(key);
|
|
|
|
}
|
|
|
|
}
|