143 lines
4.6 KiB
C#
143 lines
4.6 KiB
C#
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Reflection;
|
|||
|
|
using UnityEngine;
|
|||
|
|
|
|||
|
|
namespace GeneralTools
|
|||
|
|
{
|
|||
|
|
public class ConfigHelper
|
|||
|
|
{
|
|||
|
|
protected static string configPath;
|
|||
|
|
|
|||
|
|
protected static Dictionary<string, Dictionary<string, object>> 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<T>(string configName, string section = "config", T defultValue = default(T)) where T : IComparable
|
|||
|
|
{
|
|||
|
|
if (configDict == null)
|
|||
|
|
{
|
|||
|
|
configDict = new Dictionary<string, Dictionary<string, object>>();
|
|||
|
|
}
|
|||
|
|
if (!configDict.ContainsKey(section))
|
|||
|
|
{
|
|||
|
|
configDict[section] = new Dictionary<string, object>();
|
|||
|
|
}
|
|||
|
|
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<T>(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<int>(n);
|
|||
|
|
else if (t == typeof(float))
|
|||
|
|
obj = GetConfig<float>(n);
|
|||
|
|
else if (t == typeof(bool))
|
|||
|
|
obj = GetConfig<bool>(n);
|
|||
|
|
else if (t == typeof(string))
|
|||
|
|
obj = GetConfig<string>(n);
|
|||
|
|
else if (t == typeof(double))
|
|||
|
|
obj = GetConfig<double>(n);
|
|||
|
|
else if (t == typeof(short))
|
|||
|
|
obj = GetConfig<short>(n);
|
|||
|
|
else
|
|||
|
|
obj = null;
|
|||
|
|
return obj;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|