206 lines
6.7 KiB
C#
206 lines
6.7 KiB
C#
// using System;
|
|
// using System.IO;
|
|
// using UnityEngine;
|
|
|
|
// /// <summary>
|
|
// /// 安卓平台配置初始化器 - 负责将StreamingAssets中的配置复制到可写目录
|
|
// /// </summary>
|
|
// public class AndroidConfigInitializer : MonoBehaviour
|
|
// {
|
|
// private static bool _isInitialized = false;
|
|
|
|
// /// <summary>
|
|
// /// 初始化安卓平台配置
|
|
// /// </summary>
|
|
// public static void Initialize()
|
|
// {
|
|
// if (_isInitialized) return;
|
|
|
|
// try
|
|
// {
|
|
// #if UNITY_ANDROID && !UNITY_EDITOR
|
|
// InitializeAndroidConfig();
|
|
// #endif
|
|
|
|
// _isInitialized = true;
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// Debug.LogError($"安卓配置初始化失败: {ex.Message}");
|
|
// }
|
|
// }
|
|
|
|
// #if UNITY_ANDROID && !UNITY_EDITOR
|
|
// private static void InitializeAndroidConfig()
|
|
// {
|
|
// Debug.Log("开始初始化安卓配置...");
|
|
|
|
// // 确保目标目录存在
|
|
// var persistentPath = Application.persistentDataPath;
|
|
// if (!Directory.Exists(persistentPath))
|
|
// {
|
|
// Directory.CreateDirectory(persistentPath);
|
|
// }
|
|
|
|
// // 复制安卓配置文件
|
|
// var sourceConfigPath = Path.Combine(Application.streamingAssetsPath, "config_android.ini");
|
|
// var targetConfigPath = Path.Combine(persistentPath, "config_android.ini");
|
|
|
|
// try
|
|
// {
|
|
// // 检查源文件是否存在
|
|
// if (File.Exists(sourceConfigPath))
|
|
// {
|
|
// // 如果目标文件不存在或源文件更新,则复制
|
|
// if (!File.Exists(targetConfigPath) || IsSourceFileNewer(sourceConfigPath, targetConfigPath))
|
|
// {
|
|
// File.Copy(sourceConfigPath, targetConfigPath, true);
|
|
// Debug.Log($"安卓配置文件已复制到: {targetConfigPath}");
|
|
// }
|
|
// else
|
|
// {
|
|
// Debug.Log("安卓配置文件已是最新版本");
|
|
// }
|
|
// }
|
|
// else
|
|
// {
|
|
// Debug.LogWarning($"安卓配置文件不存在: {sourceConfigPath}");
|
|
// CreateDefaultAndroidConfig(targetConfigPath);
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// Debug.LogError($"复制安卓配置文件失败: {ex.Message}");
|
|
// CreateDefaultAndroidConfig(targetConfigPath);
|
|
// }
|
|
|
|
// // 验证配置文件
|
|
// ValidateAndroidConfig(targetConfigPath);
|
|
// }
|
|
|
|
// private static bool IsSourceFileNewer(string sourcePath, string targetPath)
|
|
// {
|
|
// try
|
|
// {
|
|
// var sourceTime = File.GetLastWriteTime(sourcePath);
|
|
// var targetTime = File.GetLastWriteTime(targetPath);
|
|
// return sourceTime > targetTime;
|
|
// }
|
|
// catch
|
|
// {
|
|
// return true; // 如果无法比较,则强制复制
|
|
// }
|
|
// }
|
|
|
|
// private static void CreateDefaultAndroidConfig(string configPath)
|
|
// {
|
|
// try
|
|
// {
|
|
// var defaultConfig = @"[SerialPort]
|
|
// serialPortName=/dev/ttyS4
|
|
// serialBaudRate=115200
|
|
// serialDataBits=8
|
|
// serialStopBits=1
|
|
// serialParity=None
|
|
// serialFlowControl=false
|
|
// heartbeatInterval=1000
|
|
// communicationTimeout=5000
|
|
// autoReconnect=true
|
|
// maxReconnectAttempts=5
|
|
|
|
// [System]
|
|
// debugMode=true
|
|
// logLevel=0
|
|
|
|
// [Android]
|
|
// usbHostEnabled=true
|
|
// requestUsbPermission=true
|
|
// jniDebug=false";
|
|
|
|
// File.WriteAllText(configPath, defaultConfig);
|
|
// Debug.Log($"创建默认安卓配置文件: {configPath}");
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// Debug.LogError($"创建默认安卓配置文件失败: {ex.Message}");
|
|
// }
|
|
// }
|
|
|
|
// private static void ValidateAndroidConfig(string configPath)
|
|
// {
|
|
// try
|
|
// {
|
|
// if (!File.Exists(configPath))
|
|
// {
|
|
// Debug.LogError("安卓配置文件验证失败:文件不存在");
|
|
// return;
|
|
// }
|
|
|
|
// var lines = File.ReadAllLines(configPath);
|
|
// bool hasSerialPortName = false;
|
|
// bool hasBaudRate = false;
|
|
|
|
// foreach (var line in lines)
|
|
// {
|
|
// if (line.StartsWith("serialPortName="))
|
|
// hasSerialPortName = true;
|
|
// else if (line.StartsWith("serialBaudRate="))
|
|
// hasBaudRate = true;
|
|
// }
|
|
|
|
// if (hasSerialPortName && hasBaudRate)
|
|
// {
|
|
// Debug.Log("安卓配置文件验证成功");
|
|
// }
|
|
// else
|
|
// {
|
|
// Debug.LogWarning("安卓配置文件缺少必要配置项");
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// Debug.LogError($"验证安卓配置文件失败: {ex.Message}");
|
|
// }
|
|
// }
|
|
// #endif
|
|
|
|
// /// <summary>
|
|
// /// 读取安卓平台串口配置
|
|
// /// </summary>
|
|
// /// <returns>串口配置信息</returns>
|
|
// public static (string portName, int baudRate, bool enabled) GetAndroidSerialConfig()
|
|
// {
|
|
// #if UNITY_ANDROID && !UNITY_EDITOR
|
|
// try
|
|
// {
|
|
// var configPath = Path.Combine(Application.persistentDataPath, "config_android.ini");
|
|
// if (File.Exists(configPath))
|
|
// {
|
|
// var lines = File.ReadAllLines(configPath);
|
|
// string portName = "/dev/ttyS4";
|
|
// int baudRate = 115200;
|
|
// bool enabled = true;
|
|
|
|
// foreach (var line in lines)
|
|
// {
|
|
// if (line.StartsWith("serialPortName="))
|
|
// portName = line.Substring("serialPortName=".Length).Trim();
|
|
// else if (line.StartsWith("serialBaudRate="))
|
|
// int.TryParse(line.Substring("serialBaudRate=".Length).Trim(), out baudRate);
|
|
// else if (line.StartsWith("enabled="))
|
|
// bool.TryParse(line.Substring("enabled=".Length).Trim(), out enabled);
|
|
// }
|
|
|
|
// return (portName, baudRate, enabled);
|
|
// }
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// Debug.LogError($"读取安卓串口配置失败: {ex.Message}");
|
|
// }
|
|
// #endif
|
|
|
|
// // 默认配置
|
|
// return ("/dev/ttyS4", 115200, true);
|
|
// }
|
|
// } |