Files
AohDrllTools/ScalingProfileStore.cs
Mr.Xia 29c7be3fac feat: 二钻涨缩系数记忆,载入上次系数
涨缩完成后将模式与系数写入 %LocalAppData%\DrillTools\scaling.ini,涨缩窗口新增"载入上次系数"按钮实时读取回填。每次完成涨缩覆盖更新,只保留最新一条记录(含源文件名追溯)。ini 使用 UTF-8 无 BOM,损坏时容错返回空。
2026-06-14 12:16:17 +08:00

126 lines
4.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace DrillTools
{
/// <summary>
/// 二钻涨缩系数的 ini 持久化存储。
/// 存放于 %LocalAppData%\DrillTools\scaling.iniUTF-8 无 BOM
/// 只保留最近一次涨缩记录(模式 + 输入值 + 源文件名)。
/// </summary>
internal static class ScalingProfileStore
{
private const string SectionName = "LastScaling";
private const string KeyMode = "Mode";
private const string KeyInputX = "InputX";
private const string KeyInputY = "InputY";
private const string KeyFileName = "FileName";
/// <summary>
/// ini 配置文件完整路径:%LocalAppData%\DrillTools\scaling.ini
/// </summary>
private static string GetIniPath()
{
string dir = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"DrillTools");
return Path.Combine(dir, "scaling.ini");
}
/// <summary>
/// 读取上次涨缩记录。
/// 文件不存在或缺少必需键(含模式无法解析)时返回 null其它 IO 异常向上抛出,由调用边界处理。
/// </summary>
public static ScalingRecord? Load()
{
string path = GetIniPath();
if (!File.Exists(path))
return null;
// 简易 ini 解析:按 [section] 切节,仅在目标节内收集 key=value
var values = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
bool inSection = false;
foreach (var raw in File.ReadAllLines(path))
{
string line = raw.Trim();
if (line.Length == 0 || line.StartsWith(";"))
continue;
// 节标题
if (line.StartsWith("[") && line.EndsWith("]"))
{
inSection = line.Equals($"[{SectionName}]", StringComparison.OrdinalIgnoreCase);
continue;
}
if (!inSection)
continue;
int eq = line.IndexOf('=');
if (eq <= 0)
continue;
string key = line.Substring(0, eq).Trim();
string val = line.Substring(eq + 1).Trim();
values[key] = val;
}
// 必需键校验:缺任一或模式无法解析视为无有效记录(容错损坏)
if (!values.TryGetValue(KeyMode, out string? modeStr) ||
!values.TryGetValue(KeyInputX, out string? inputX) ||
!values.TryGetValue(KeyInputY, out string? inputY))
return null;
if (!Enum.TryParse(modeStr, out ScalingMode mode))
return null;
// 文件名为可选追溯字段
values.TryGetValue(KeyFileName, out string? fileName);
return new ScalingRecord(mode, inputX, inputY, fileName);
}
/// <summary>
/// 写入涨缩记录,整体覆盖。自动创建目录。失败时抛出异常,由调用边界处理。
/// </summary>
public static void Save(ScalingMode mode, string inputX, string inputY, string fileName)
{
string path = GetIniPath();
string? dir = Path.GetDirectoryName(path);
if (!string.IsNullOrEmpty(dir))
Directory.CreateDirectory(dir);
var content =
$"[{SectionName}]\r\n" +
$"{KeyMode}={mode}\r\n" +
$"{KeyInputX}={inputX}\r\n" +
$"{KeyInputY}={inputY}\r\n" +
$"{KeyFileName}={fileName}\r\n";
// UTF-8 无 BOM项目文件编码规范
File.WriteAllText(path, content, new UTF8Encoding(false));
}
}
/// <summary>
/// 涨缩系数持久化记录(只读)。
/// </summary>
internal sealed class ScalingRecord
{
public ScalingMode Mode { get; }
public string InputX { get; }
public string InputY { get; }
public string? FileName { get; }
public ScalingRecord(ScalingMode mode, string inputX, string inputY, string? fileName)
{
Mode = mode;
InputX = inputX;
InputY = inputY;
FileName = fileName;
}
}
}