2022-10-03 02:04:54 +02:00
|
|
|
using System.Collections;
|
|
|
|
using System;
|
|
|
|
using UnityEngine;
|
|
|
|
using WindowsInput.Native;
|
|
|
|
using TMPro;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
using System.IO;
|
|
|
|
using Newtonsoft.Json;
|
2022-11-05 01:32:17 +01:00
|
|
|
using System.Collections.Generic;
|
2022-10-03 02:04:54 +02:00
|
|
|
|
|
|
|
public class ConfigManager : MonoBehaviour
|
|
|
|
{
|
|
|
|
public static Config config;
|
|
|
|
private static bool hasInitialized = false;
|
|
|
|
public static event Action onConfigChanged;
|
2022-10-05 22:47:49 +02:00
|
|
|
private static float saverTimer = 0;
|
|
|
|
private static bool isSavingConfig = false;
|
|
|
|
private float saverDelay = 1.5f;
|
2022-11-05 01:32:17 +01:00
|
|
|
|
|
|
|
public List<GameObject> Tabs;
|
|
|
|
[SerializeField]
|
|
|
|
public static List<ConfigPanelComponent> ConfigPanelComponents;
|
2022-10-03 02:04:54 +02:00
|
|
|
void Awake()
|
|
|
|
{
|
|
|
|
onConfigChanged += EnsureInitialization;
|
2022-12-05 23:15:03 +01:00
|
|
|
onConfigChanged += SaveFileWait;
|
2022-11-05 20:23:31 +01:00
|
|
|
|
2022-10-03 02:04:54 +02:00
|
|
|
}
|
|
|
|
void Start()
|
|
|
|
{
|
2022-11-05 20:23:31 +01:00
|
|
|
EnsureInitialization();
|
2022-11-05 01:32:17 +01:00
|
|
|
AddListenerToWidget(ConfigPanelComponents);
|
2022-10-03 02:04:54 +02:00
|
|
|
onConfigChanged?.Invoke();
|
|
|
|
}
|
|
|
|
public static void EnsureInitialization()
|
|
|
|
{
|
|
|
|
if (hasInitialized)
|
|
|
|
return;
|
|
|
|
LoadFile();
|
2022-11-05 20:23:31 +01:00
|
|
|
ConfigPanelComponents = GetConfigPanelComponentsStatic();
|
2022-11-05 01:32:17 +01:00
|
|
|
UpdateConfigPanelFromConfig(ref ConfigPanelComponents);
|
2022-10-03 02:04:54 +02:00
|
|
|
hasInitialized = true;
|
|
|
|
}
|
|
|
|
private static void LoadFile()
|
|
|
|
{
|
|
|
|
Debug.Log("Loading config file");
|
2023-03-04 03:25:38 +01:00
|
|
|
if (!File.Exists(GetFileName()) || JsonConvert.DeserializeObject<Config>(File.ReadAllText(GetFileName())) == null)
|
2022-10-03 02:04:54 +02:00
|
|
|
{
|
2023-03-04 03:25:38 +01:00
|
|
|
Debug.Log("Config file does not exist or is corrupted");
|
2022-10-03 02:04:54 +02:00
|
|
|
config = new Config();
|
2023-03-04 03:25:38 +01:00
|
|
|
saveFile();
|
2022-10-03 02:04:54 +02:00
|
|
|
Debug.Log("Config file created");
|
|
|
|
}
|
2023-03-04 03:25:38 +01:00
|
|
|
Debug.Log("Found config file");
|
|
|
|
config = JsonConvert.DeserializeObject<Config>(File.ReadAllText(GetFileName()));
|
|
|
|
Debug.Log("Config file loaded");
|
2022-10-03 02:04:54 +02:00
|
|
|
}
|
|
|
|
public static string GetFileName()
|
|
|
|
{
|
|
|
|
return Application.dataPath + "/../config.json";
|
|
|
|
}
|
2022-12-05 23:15:03 +01:00
|
|
|
public static void SaveFileWait()
|
2022-10-03 02:04:54 +02:00
|
|
|
{
|
2022-10-05 22:47:49 +02:00
|
|
|
isSavingConfig = true;
|
|
|
|
saverTimer = 0;
|
2022-10-07 17:12:42 +02:00
|
|
|
//Debug.Log("Saving config file");
|
2022-10-03 02:04:54 +02:00
|
|
|
}
|
2023-03-04 03:25:38 +01:00
|
|
|
public static void saveFile()
|
2022-10-03 02:04:54 +02:00
|
|
|
{
|
|
|
|
File.WriteAllText(GetFileName(), JsonConvert.SerializeObject(config, Formatting.Indented));
|
|
|
|
Debug.Log("Config file saved");
|
|
|
|
}
|
2022-10-05 22:47:49 +02:00
|
|
|
|
|
|
|
void Update()
|
|
|
|
{
|
|
|
|
if (isSavingConfig)
|
|
|
|
{
|
|
|
|
saverTimer += Time.deltaTime;
|
|
|
|
if (saverTimer >= saverDelay)
|
|
|
|
{
|
|
|
|
isSavingConfig = false;
|
|
|
|
saverTimer = 0;
|
2022-12-05 23:15:03 +01:00
|
|
|
saveFile();
|
2022-10-05 22:47:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 02:04:54 +02:00
|
|
|
|
2022-12-05 23:15:03 +01:00
|
|
|
void OnDestroy()
|
|
|
|
{
|
|
|
|
saveFile();
|
|
|
|
}
|
|
|
|
|
2022-11-05 01:32:17 +01:00
|
|
|
public static GameObject GetConfigPanelWidget(string configKeyName)
|
2022-10-03 02:04:54 +02:00
|
|
|
{
|
2022-11-05 20:23:31 +01:00
|
|
|
EnsureInitialization();
|
2022-11-05 01:32:17 +01:00
|
|
|
foreach (var item in ConfigPanelComponents)
|
|
|
|
{
|
|
|
|
if (item.ConfigKeyName == configKeyName)
|
|
|
|
{
|
|
|
|
return item.Widget;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
2022-10-03 02:04:54 +02:00
|
|
|
}
|
|
|
|
|
2022-11-05 01:32:17 +01:00
|
|
|
private List<ConfigPanelComponent> GetConfigPanelComponents()
|
2022-10-03 02:04:54 +02:00
|
|
|
{
|
2022-11-05 01:32:17 +01:00
|
|
|
var _configPanelComponents = new List<ConfigPanelComponent>();
|
|
|
|
foreach (var tab in Tabs)
|
|
|
|
{
|
|
|
|
var _configPanelComponentsInTab = tab.GetComponentsInChildren<ConfigPanelComponent>();
|
|
|
|
_configPanelComponents.AddRange(_configPanelComponentsInTab);
|
|
|
|
}
|
|
|
|
return _configPanelComponents;
|
2022-10-03 02:04:54 +02:00
|
|
|
}
|
2022-11-05 01:32:17 +01:00
|
|
|
|
2022-11-05 20:23:31 +01:00
|
|
|
private static List<ConfigPanelComponent> GetConfigPanelComponentsStatic()
|
|
|
|
{
|
|
|
|
var _configPanelComponents = new List<ConfigPanelComponent>();
|
|
|
|
_configPanelComponents = GameObject.FindObjectOfType<ConfigManager>().GetConfigPanelComponents();
|
|
|
|
return _configPanelComponents;
|
|
|
|
}
|
|
|
|
|
2022-11-05 01:32:17 +01:00
|
|
|
private void AddListenerToWidget(List<ConfigPanelComponent> _configPanelComponents)
|
2022-10-03 02:04:54 +02:00
|
|
|
{
|
2022-11-05 01:32:17 +01:00
|
|
|
foreach (var configPanelComponent in _configPanelComponents)
|
|
|
|
{
|
|
|
|
var widget = configPanelComponent.Widget;
|
2022-12-05 23:15:03 +01:00
|
|
|
var dropdown = widget.GetComponent<TMP_Dropdown>();
|
|
|
|
var toggle = widget.GetComponent<Toggle>();
|
|
|
|
var slider = widget.GetComponent<Slider>();
|
|
|
|
var valueManager = widget.GetComponent<ValueManager>();
|
|
|
|
if (dropdown != null) // add listener to dropdown to update config by key name
|
2022-11-05 01:32:17 +01:00
|
|
|
{
|
2022-11-07 22:31:18 +01:00
|
|
|
dropdown.onValueChanged.AddListener((int value) =>
|
2022-11-05 01:32:17 +01:00
|
|
|
{
|
|
|
|
var field = config.GetType().GetField(configPanelComponent.ConfigKeyName);
|
2022-11-07 22:31:18 +01:00
|
|
|
field.SetValue(config, value);
|
2022-11-05 01:32:17 +01:00
|
|
|
onConfigChanged?.Invoke();
|
|
|
|
});
|
|
|
|
}
|
2022-12-05 23:15:03 +01:00
|
|
|
else if (toggle != null)
|
2022-11-05 01:32:17 +01:00
|
|
|
{
|
2022-11-07 22:31:18 +01:00
|
|
|
toggle.onValueChanged.AddListener((bool value) =>
|
2022-11-05 01:32:17 +01:00
|
|
|
{
|
|
|
|
var field = config.GetType().GetField(configPanelComponent.ConfigKeyName);
|
2022-11-07 22:31:18 +01:00
|
|
|
field.SetValue(config, value);
|
2022-11-05 01:32:17 +01:00
|
|
|
onConfigChanged?.Invoke();
|
|
|
|
});
|
|
|
|
}
|
2022-12-05 23:15:03 +01:00
|
|
|
else if (slider != null)
|
2022-11-05 01:32:17 +01:00
|
|
|
{
|
2022-11-07 22:31:18 +01:00
|
|
|
slider.onValueChanged.AddListener((float value) =>
|
2022-11-05 01:32:17 +01:00
|
|
|
{
|
|
|
|
var field = config.GetType().GetField(configPanelComponent.ConfigKeyName);
|
2022-11-07 22:31:18 +01:00
|
|
|
field.SetValue(config, value);
|
2022-11-05 01:32:17 +01:00
|
|
|
onConfigChanged?.Invoke();
|
|
|
|
});
|
|
|
|
}
|
2022-12-05 23:15:03 +01:00
|
|
|
else if (valueManager != null)
|
2022-11-05 01:32:17 +01:00
|
|
|
{
|
|
|
|
valueManager.onValueChanged.AddListener(delegate
|
|
|
|
{
|
|
|
|
var field = config.GetType().GetField(configPanelComponent.ConfigKeyName);
|
2022-11-05 20:23:31 +01:00
|
|
|
field.SetValue(config, valueManager.value);
|
2022-11-05 01:32:17 +01:00
|
|
|
onConfigChanged?.Invoke();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 02:04:54 +02:00
|
|
|
}
|
2022-11-05 01:32:17 +01:00
|
|
|
|
|
|
|
private static void UpdateConfigPanelFromConfig(ref List<ConfigPanelComponent> _configPanelComponents)
|
2022-10-03 02:04:54 +02:00
|
|
|
{
|
2022-11-05 01:32:17 +01:00
|
|
|
foreach (var configPanelComponent in _configPanelComponents)
|
|
|
|
{
|
2022-11-08 21:23:25 +01:00
|
|
|
Debug.Log(configPanelComponent.ConfigKeyName);
|
2022-11-05 01:32:17 +01:00
|
|
|
var componentObject = configPanelComponent.Widget;
|
|
|
|
if (componentObject.GetComponent<TMP_Dropdown>() != null)
|
|
|
|
{
|
|
|
|
var dropdown = componentObject.GetComponent<TMP_Dropdown>();
|
|
|
|
var field = config.GetType().GetField(configPanelComponent.ConfigKeyName);
|
|
|
|
dropdown.value = (int)field.GetValue(config);
|
|
|
|
}
|
|
|
|
else if (componentObject.GetComponent<Toggle>() != null)
|
|
|
|
{
|
|
|
|
var toggle = componentObject.GetComponent<Toggle>();
|
|
|
|
var field = config.GetType().GetField(configPanelComponent.ConfigKeyName);
|
|
|
|
toggle.isOn = (bool)field.GetValue(config);
|
|
|
|
}
|
|
|
|
else if (componentObject.GetComponent<Slider>() != null)
|
|
|
|
{
|
|
|
|
var slider = componentObject.GetComponent<Slider>();
|
|
|
|
var field = config.GetType().GetField(configPanelComponent.ConfigKeyName);
|
|
|
|
slider.value = (float)field.GetValue(config);
|
|
|
|
}
|
|
|
|
else if (componentObject.GetComponent<ValueManager>() != null)
|
|
|
|
{
|
|
|
|
var valueManager = componentObject.GetComponent<ValueManager>();
|
|
|
|
var field = config.GetType().GetField(configPanelComponent.ConfigKeyName);
|
2022-11-05 20:23:31 +01:00
|
|
|
valueManager.value = (float)field.GetValue(config);
|
2022-11-05 01:32:17 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-03 02:04:54 +02:00
|
|
|
}
|
|
|
|
}
|