130 lines
5.2 KiB
C#
130 lines
5.2 KiB
C#
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine; // 假设在Unity环境中使用Debug.LogError
|
|
|
|
namespace HitFeedback
|
|
{
|
|
public class Config
|
|
{
|
|
public KeyCode hotKey = KeyCode.F8;
|
|
public Dictionary<string, float> probability = new Dictionary<string, float>();
|
|
|
|
public void LoadConfig(string filename)
|
|
{
|
|
if (!File.Exists(filename))
|
|
{
|
|
Debug.LogError($"Config file not found: {filename}");
|
|
return; // 如果文件不存在,就没必要继续了
|
|
}
|
|
|
|
// 清空旧的概率数据,确保每次加载都是从新开始
|
|
probability.Clear();
|
|
|
|
try
|
|
{
|
|
using (var sr = new StreamReader(filename))
|
|
{
|
|
string line;
|
|
var lineNumber = 0; // 用于错误报告
|
|
while ((line = sr.ReadLine()) != null)
|
|
{
|
|
lineNumber++;
|
|
line = line.Trim(); // 移除行首尾的空白字符
|
|
|
|
// 忽略空行和注释行
|
|
if (string.IsNullOrEmpty(line) || line.StartsWith(";") || line.StartsWith("#"))
|
|
{
|
|
continue;
|
|
}
|
|
|
|
// 查找等号
|
|
var separatorIndex = line.IndexOf('=');
|
|
if (separatorIndex == -1)
|
|
{
|
|
Debug.LogWarning($"Skipping malformed line in config file '{filename}' at line {lineNumber}: No '=' found. Line: '{line}'");
|
|
continue;
|
|
}
|
|
|
|
var key = line.Substring(0, separatorIndex).Trim();
|
|
var valueStr = line.Substring(separatorIndex + 1).Trim();
|
|
|
|
// 解析 hotKey
|
|
if (key.Equals("hotKey", System.StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
try
|
|
{
|
|
hotKey = (KeyCode)System.Enum.Parse(typeof(KeyCode), valueStr, true);
|
|
}
|
|
catch (System.ArgumentException)
|
|
{
|
|
Debug.LogError($"Invalid KeyCode '{valueStr}' in config file '{filename}' at line {lineNumber}. Using default F8.");
|
|
hotKey = KeyCode.F8; // 设置为默认值或保持不变
|
|
}
|
|
}
|
|
// 解析 probability 字典项
|
|
else
|
|
{
|
|
if (float.TryParse(valueStr, out var probValue))
|
|
{
|
|
probability[key] = probValue;
|
|
}
|
|
else
|
|
{
|
|
Debug.LogWarning($"Invalid float value '{valueStr}' for key '{key}' in config file '{filename}' at line {lineNumber}. Skipping entry.");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Debug.LogError($"Error reading config file '{filename}': {ex.Message}");
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 将当前配置存储到指定INI文件。
|
|
/// </summary>
|
|
/// <param name="filename">要保存的INI文件的路径。</param>
|
|
public void SaveConfig(string filename)
|
|
{
|
|
try
|
|
{
|
|
// 确保目录存在
|
|
var directory = Path.GetDirectoryName(filename);
|
|
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
using (var sw = new StreamWriter(filename))
|
|
{
|
|
sw.WriteLine("; HitFeedback Configuration File");
|
|
sw.WriteLine("; Generated by HitFeedback.Config class");
|
|
sw.WriteLine(); // 空行
|
|
sw.WriteLine("[General]");
|
|
sw.WriteLine($"hotKey = {hotKey.ToString()}");
|
|
sw.WriteLine();
|
|
if (probability.Count > 0)
|
|
{
|
|
sw.WriteLine("[Probabilities]");
|
|
foreach (var kvp in probability)
|
|
{
|
|
// 使用 InvariantCulture 确保浮点数的格式在不同区域设置下都一致
|
|
sw.WriteLine($"{kvp.Key} = {kvp.Value.ToString(System.Globalization.CultureInfo.InvariantCulture)}");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
sw.WriteLine("; No probabilities currently configured.");
|
|
}
|
|
sw.WriteLine();
|
|
}
|
|
Debug.Log($"Config saved to: {filename}");
|
|
}
|
|
catch (System.Exception ex)
|
|
{
|
|
Debug.LogError($"Error saving config file '{filename}': {ex.Message}");
|
|
}
|
|
}
|
|
}
|
|
}
|