using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Reflection; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System.IO; namespace BITKit { public class PersistentDataManager { static IEnumerable fieldInfos=new List(); static JObject config; static string configPath = PathHelper.GetFilePath($"{nameof(config)}.json"); [ExcuteOnAwake] public static async void Awake() { var json = File.ReadAllText( configPath ); if(string.IsNullOrEmpty( json ) is false) { config = JObject.Parse(json); fieldInfos = await ReflectionHelper.GetFields(); foreach (var field in fieldInfos) { if (config.TryGetValue(field.Name, out var token)) { var value = token.ToObject(field.FieldType); BIT4Log.Log($"正在加载:{field.Name}->{value}"); field.SetValue(null, value); } } } } [ExcuteOnStart] public static void Start() { DataParser.Set(File.ReadAllText(configPath)); } [ExcuteOnStop] public static void Stop() { Dictionary dictionary = new Dictionary(); foreach (var field in fieldInfos) { var value = field.GetValue(null); dictionary.Add(field.Name,field.GetValue(null)); BIT4Log.Log($"正在保存:{field.Name}->{value}"); } var json = JsonConvert.SerializeObject(dictionary); File.WriteAllText(configPath,json); BIT4Log.Log("保存完成"); } } }