335 lines
13 KiB
C#
335 lines
13 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.IO;
|
||
using UnityEngine;
|
||
|
||
namespace GeneralTools
|
||
{
|
||
/// <summary>
|
||
/// 安卓平台兼容的配置助手
|
||
/// 支持打包后动态调整配置
|
||
/// </summary>
|
||
public class AndroidConfigHelper
|
||
{
|
||
private static string configFileName = "config.ini";
|
||
private static string persistentConfigPath;
|
||
private static string streamingConfigPath;
|
||
private static Dictionary<string, Dictionary<string, object>> configDict;
|
||
|
||
#region 串口通信配置
|
||
public static string serialPortName { get; set; } = "/dev/ttyS4"; // 安卓串口设备路径
|
||
public static int serialBaudRate { get; set; } = 115200;
|
||
public static int serialDataBits { get; set; } = 8;
|
||
public static int serialStopBits { get; set; } = 1;
|
||
public static string serialParity { get; set; } = "None";
|
||
public static bool serialFlowControl { get; set; } = false;
|
||
public static int heartbeatInterval { get; set; } = 1000; // 心跳间隔(ms)
|
||
public static int communicationTimeout { get; set; } = 5000; // 通信超时(ms)
|
||
public static bool autoReconnect { get; set; } = true;
|
||
public static int maxReconnectAttempts { get; set; } = 5;
|
||
#endregion
|
||
|
||
#region 显示设置
|
||
public static int screen_width { get; set; } = 1920;
|
||
public static int screen_height { get; set; } = 1080;
|
||
public static bool showcursor { get; set; } = false;
|
||
public static int fullscreen { get; set; } = 2;
|
||
public static float brightness { get; set; } = 47.0f;
|
||
#endregion
|
||
|
||
#region 音频设置
|
||
public static bool isAllowPlayBGM { get; set; } = true;
|
||
public static int audioShotLimit { get; set; } = 5;
|
||
public static int volume { get; set; } = 26;
|
||
public static int muteDurationMinutes { get; set; } = 4;
|
||
#endregion
|
||
|
||
#region 系统设置
|
||
public static int logType { get; set; } = 0;
|
||
public static bool saveLog { get; set; } = true;
|
||
public static bool broadcastDebug { get; set; } = false;
|
||
public static int targetFramerate { get; set; } = 60;
|
||
public static bool autoBackup { get; set; } = true;
|
||
public static int dataRetentionDays { get; set; } = 30;
|
||
#endregion
|
||
|
||
/// <summary>
|
||
/// 初始化配置系统(安卓兼容版本)
|
||
/// </summary>
|
||
public static void Init()
|
||
{
|
||
// 安卓平台路径设置
|
||
streamingConfigPath = Path.Combine(Application.streamingAssetsPath, configFileName);
|
||
persistentConfigPath = Path.Combine(Application.persistentDataPath, configFileName);
|
||
|
||
Debug.Log($"StreamingAssets配置路径: {streamingConfigPath}");
|
||
Debug.Log($"可写配置路径: {persistentConfigPath}");
|
||
|
||
LoadConfiguration();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 加载配置(优先从可写目录加载)
|
||
/// </summary>
|
||
private static void LoadConfiguration()
|
||
{
|
||
configDict = new Dictionary<string, Dictionary<string, object>>();
|
||
|
||
string configToLoad = persistentConfigPath;
|
||
|
||
// 如果可写目录没有配置文件,从StreamingAssets复制默认配置
|
||
if (!File.Exists(persistentConfigPath))
|
||
{
|
||
CopyDefaultConfigToPersistent();
|
||
}
|
||
|
||
if (File.Exists(configToLoad))
|
||
{
|
||
LoadFromIniFile(configToLoad);
|
||
ApplyConfigurationToProperties();
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("配置文件不存在,使用默认值");
|
||
SaveConfiguration(); // 创建默认配置文件
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从StreamingAssets复制默认配置到可写目录
|
||
/// </summary>
|
||
private static void CopyDefaultConfigToPersistent()
|
||
{
|
||
try
|
||
{
|
||
#if UNITY_ANDROID && !UNITY_EDITOR
|
||
// 安卓平台需要通过WWW或UnityWebRequest读取StreamingAssets
|
||
var www = new WWW(streamingConfigPath);
|
||
while (!www.isDone) { }
|
||
|
||
if (string.IsNullOrEmpty(www.error))
|
||
{
|
||
File.WriteAllText(persistentConfigPath, www.text);
|
||
Debug.Log("默认配置已复制到可写目录");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogError($"复制默认配置失败: {www.error}");
|
||
}
|
||
#else
|
||
// 编辑器或其他平台直接复制
|
||
if (File.Exists(streamingConfigPath))
|
||
{
|
||
File.Copy(streamingConfigPath, persistentConfigPath, true);
|
||
Debug.Log("默认配置已复制到可写目录");
|
||
}
|
||
#endif
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogError($"复制配置文件失败: {e.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 从INI文件加载配置
|
||
/// </summary>
|
||
private static void LoadFromIniFile(string filePath)
|
||
{
|
||
try
|
||
{
|
||
string[] lines = File.ReadAllLines(filePath);
|
||
string currentSection = "config";
|
||
|
||
foreach (string line in lines)
|
||
{
|
||
string trimmedLine = line.Trim();
|
||
|
||
// 跳过注释和空行
|
||
if (string.IsNullOrEmpty(trimmedLine) || trimmedLine.StartsWith(";"))
|
||
continue;
|
||
|
||
// 处理节
|
||
if (trimmedLine.StartsWith("[") && trimmedLine.EndsWith("]"))
|
||
{
|
||
currentSection = trimmedLine.Substring(1, trimmedLine.Length - 2);
|
||
if (!configDict.ContainsKey(currentSection))
|
||
{
|
||
configDict[currentSection] = new Dictionary<string, object>();
|
||
}
|
||
continue;
|
||
}
|
||
|
||
// 处理键值对
|
||
int equalIndex = trimmedLine.IndexOf('=');
|
||
if (equalIndex > 0)
|
||
{
|
||
string key = trimmedLine.Substring(0, equalIndex).Trim();
|
||
string value = trimmedLine.Substring(equalIndex + 1).Trim();
|
||
|
||
if (!configDict.ContainsKey(currentSection))
|
||
{
|
||
configDict[currentSection] = new Dictionary<string, object>();
|
||
}
|
||
|
||
configDict[currentSection][key] = value;
|
||
}
|
||
}
|
||
|
||
Debug.Log($"配置加载完成,共{configDict.Count}个节");
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogError($"加载配置文件失败: {e.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 将配置字典应用到属性
|
||
/// </summary>
|
||
private static void ApplyConfigurationToProperties()
|
||
{
|
||
// 串口配置
|
||
serialPortName = GetConfig("serialPortName", "SerialPort", serialPortName);
|
||
serialBaudRate = GetConfig("serialBaudRate", "SerialPort", serialBaudRate);
|
||
serialDataBits = GetConfig("serialDataBits", "SerialPort", serialDataBits);
|
||
serialStopBits = GetConfig("serialStopBits", "SerialPort", serialStopBits);
|
||
serialParity = GetConfig("serialParity", "SerialPort", serialParity);
|
||
serialFlowControl = GetConfig("serialFlowControl", "SerialPort", serialFlowControl);
|
||
heartbeatInterval = GetConfig("heartbeatInterval", "SerialPort", heartbeatInterval);
|
||
communicationTimeout = GetConfig("communicationTimeout", "SerialPort", communicationTimeout);
|
||
autoReconnect = GetConfig("autoReconnect", "SerialPort", autoReconnect);
|
||
maxReconnectAttempts = GetConfig("maxReconnectAttempts", "SerialPort", maxReconnectAttempts);
|
||
|
||
// 显示配置
|
||
screen_width = GetConfig("screen_width", "config", screen_width);
|
||
screen_height = GetConfig("screen_height", "config", screen_height);
|
||
showcursor = GetConfig("showcursor", "config", showcursor);
|
||
fullscreen = GetConfig("fullscreen", "config", fullscreen);
|
||
brightness = GetConfig("brightness", "Display", brightness);
|
||
|
||
// 音频配置
|
||
isAllowPlayBGM = GetConfig("isAllowPlayBGM", "config", isAllowPlayBGM);
|
||
audioShotLimit = GetConfig("audioShotLimit", "config", audioShotLimit);
|
||
volume = GetConfig("volume", "Audio", volume);
|
||
muteDurationMinutes = GetConfig("muteDurationMinutes", "Audio", muteDurationMinutes);
|
||
|
||
// 系统配置
|
||
logType = GetConfig("logType", "config", logType);
|
||
saveLog = GetConfig("saveLog", "config", saveLog);
|
||
broadcastDebug = GetConfig("broadcastDebug", "config", broadcastDebug);
|
||
targetFramerate = GetConfig("targetFramerate", "config", targetFramerate);
|
||
autoBackup = GetConfig("autoBackup", "System", autoBackup);
|
||
dataRetentionDays = GetConfig("dataRetentionDays", "System", dataRetentionDays);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取配置值
|
||
/// </summary>
|
||
public static T GetConfig<T>(string configName, string section = "config", T defaultValue = default(T)) where T : IComparable
|
||
{
|
||
try
|
||
{
|
||
if (configDict != null &&
|
||
configDict.ContainsKey(section) &&
|
||
configDict[section].ContainsKey(configName))
|
||
{
|
||
object value = configDict[section][configName];
|
||
return (T)Convert.ChangeType(value, typeof(T));
|
||
}
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogWarning($"获取配置 {section}.{configName} 失败: {e.Message}");
|
||
}
|
||
|
||
return defaultValue;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 设置配置值并保存(支持运行时修改)
|
||
/// </summary>
|
||
public static void SetConfig<T>(string configName, T value, string section = "config") where T : IComparable
|
||
{
|
||
if (configDict == null)
|
||
{
|
||
configDict = new Dictionary<string, Dictionary<string, object>>();
|
||
}
|
||
|
||
if (!configDict.ContainsKey(section))
|
||
{
|
||
configDict[section] = new Dictionary<string, object>();
|
||
}
|
||
|
||
configDict[section][configName] = value;
|
||
|
||
// 立即保存到文件
|
||
SaveConfiguration();
|
||
|
||
Debug.Log($"配置已更新: {section}.{configName} = {value}");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 保存配置到可写目录
|
||
/// </summary>
|
||
public static void SaveConfiguration()
|
||
{
|
||
try
|
||
{
|
||
List<string> lines = new List<string>();
|
||
|
||
foreach (var section in configDict)
|
||
{
|
||
lines.Add($"[{section.Key}]");
|
||
lines.Add("");
|
||
|
||
foreach (var kvp in section.Value)
|
||
{
|
||
lines.Add($"{kvp.Key}={kvp.Value}");
|
||
}
|
||
lines.Add("");
|
||
}
|
||
|
||
File.WriteAllLines(persistentConfigPath, lines.ToArray());
|
||
Debug.Log($"配置已保存到: {persistentConfigPath}");
|
||
}
|
||
catch (Exception e)
|
||
{
|
||
Debug.LogError($"保存配置失败: {e.Message}");
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重新加载配置
|
||
/// </summary>
|
||
public static void ReloadConfiguration()
|
||
{
|
||
LoadConfiguration();
|
||
Debug.Log("配置已重新加载");
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取配置文件路径(用于外部编辑)
|
||
/// </summary>
|
||
public static string GetConfigFilePath()
|
||
{
|
||
return persistentConfigPath;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重置为默认配置
|
||
/// </summary>
|
||
public static void ResetToDefault()
|
||
{
|
||
if (File.Exists(persistentConfigPath))
|
||
{
|
||
File.Delete(persistentConfigPath);
|
||
}
|
||
|
||
CopyDefaultConfigToPersistent();
|
||
LoadConfiguration();
|
||
|
||
Debug.Log("配置已重置为默认值");
|
||
}
|
||
}
|
||
} |