using System; using System.Collections.Generic; using System.Reflection; using UnityEngine; namespace GeneralTools { public class ConfigHelper { protected static string configPath; protected static Dictionary> configDict; #region 默认设置 public static int screen_width { get; set; } public static int screen_height { get; set; } public static int window_Left { get; set; } public static int window_Top { get; set; } public static bool showcursor { get; set; } public static int fullscreen { get; set; } public static int screenDepth { get; set; } public static bool isAllowPlayBGM { get; set; } public static int audioShotLimit { get; set; } public static int logType { get; set; } public static bool saveLog { get; set; } public static bool broadcastDebug { get; set; } public static int debugSocketMsgMode { get; set; } public static int targetFramerate { get; set; } public static int keyDownActionLevel { get; set; } public static int releaseTime { get; set; } #region 串口通信设置 public static string serialPortName { get; set; } public static int baudRate { get; set; } public static bool enabled { get; set; } public static int connectionTimeoutMs { get; set; } public static int heartbeatIntervalSeconds { get; set; } #endregion #endregion public static void Init(string _configPath) { configPath = _configPath; ConfigHelper consts = new ConfigHelper(); Type type = consts.GetType(); PropertyInfo[] propertyInfoArr = type.GetProperties(); foreach (PropertyInfo property in propertyInfoArr) { string propertyName = property.Name; Type t = property.PropertyType; object obj = GetObj(t, propertyName); if (obj == null) { Debug.LogError("错误的类型" + propertyName); continue; } property.SetValue(consts, obj); } FieldInfo[] fieldInfoArr = type.GetFields(); foreach (FieldInfo fieldInfo in fieldInfoArr) { string propertyName = fieldInfo.Name; Type t = fieldInfo.FieldType; object obj = GetObj(t, propertyName); if (obj == null) { Debug.LogError("错误的类型" + propertyName); continue; } fieldInfo.SetValue(consts, obj); } } public static T GetConfig(string configName, string section = "config", T defultValue = default(T)) where T : IComparable { if (configDict == null) { configDict = new Dictionary>(); } if (!configDict.ContainsKey(section)) { configDict[section] = new Dictionary(); } if (configDict[section].ContainsKey(configName)) { return (T)configDict[section][configName]; } else { if (defultValue == null) defultValue = (T)Convert.ChangeType("", typeof(T)); object obj = ReadConfigFromFile(configName, section, defultValue); configDict[section][configName] = obj; return (T)obj; } } public static string GetConfigPath() { return configPath; } private static T ReadConfigFromFile(string configName, string section, T defultValue) where T : IComparable { return IniFile.GetTypeValue(section, configName, defultValue, configPath); } private static object GetObj(Type t, string n) { object obj; if (t == typeof(int)) obj = GetConfig(n); else if (t == typeof(float)) obj = GetConfig(n); else if (t == typeof(bool)) obj = GetConfig(n); else if (t == typeof(string)) obj = GetConfig(n); else if (t == typeof(double)) obj = GetConfig(n); else if (t == typeof(short)) obj = GetConfig(n); else obj = null; return obj; } } }