修复旧桌面会话无法读取djev安装目录的问题

This commit is contained in:
tfarcraw 2026-11-21 10:50:22 +08:00
parent 5b46c55208
commit 95c7e45a84
2 changed files with 35 additions and 2 deletions

View File

@ -12,9 +12,23 @@ public sealed record D3StartupOptions(Uri Endpoint, string Root, string Python,
public string MarkerPath => Path.Combine(Root, $"sophelper-d3-{Endpoint.Port}.json");
public string LockPath => Path.Combine(Root, $"sophelper-d3-{Endpoint.Port}.lock");
public static D3StartupOptions FromEnvironment(string applicationDirectory, string workspace)
/// <summary>读取启动配置;Windows 桌面会话尚未继承新环境变量时,直接读取用户和机器持久配置。</summary>
/// <param name="applicationDirectory">包含服务脚本的程序目录。</param>
/// <param name="workspace">保存服务日志的工作区。</param>
/// <param name="readEnvironment">环境读取器;默认读取系统环境,可注入以验证旧会话的配置回退。</param>
public static D3StartupOptions FromEnvironment(string applicationDirectory, string workspace,
Func<string, EnvironmentVariableTarget, string?>? readEnvironment = null)
{
var root = Path.GetFullPath(Environment.GetEnvironmentVariable("SOPHELPER_D3_ROOT")
readEnvironment ??= Environment.GetEnvironmentVariable;
var configuredRoot = readEnvironment("SOPHELPER_D3_ROOT", EnvironmentVariableTarget.Process);
// setx /M 不会更新已运行的 Explorer。保留进程显式覆盖,仅在缺省时读取注册表,安装后无需注销桌面。
if (OperatingSystem.IsWindows() && string.IsNullOrWhiteSpace(configuredRoot))
{
configuredRoot = readEnvironment("SOPHELPER_D3_ROOT", EnvironmentVariableTarget.User);
if (string.IsNullOrWhiteSpace(configuredRoot))
configuredRoot = readEnvironment("SOPHELPER_D3_ROOT", EnvironmentVariableTarget.Machine);
}
var root = Path.GetFullPath((string.IsNullOrWhiteSpace(configuredRoot) ? null : configuredRoot)
?? (OperatingSystem.IsWindows() ? @"E:\D3Lite" : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "D3Lite")));
return new(new Uri(Environment.GetEnvironmentVariable("SOPHELPER_VISION_ENDPOINT") ?? "http://127.0.0.1:8766/v1/systemone"),
root, Path.Combine(root, ".venv", OperatingSystem.IsWindows() ? @"Scripts\python.exe" : "bin/python"),

View File

@ -18,6 +18,25 @@ if(args.Length==3&&args[0]=="--djev-live")
}
static void Check(bool value,string message){if(!value)throw new Exception(message);Console.WriteLine("PASS: "+message);}
// 模拟安装后未注销的桌面:进程环境缺少变量,但机器配置已经写入 D 盘安装目录。
if (OperatingSystem.IsWindows())
{
var machineRoot = Path.GetFullPath(@"D:\D3Nano");
var userRoot = Path.GetFullPath(@"D:\UserDjev");
var processRoot = Path.GetFullPath(@"D:\ProcessDjev");
var resolved = D3StartupOptions.FromEnvironment("app", "workspace",
(_, target) => target == EnvironmentVariableTarget.Machine ? machineRoot : null);
Check(resolved.Root == machineRoot && resolved.Python == Path.Combine(machineRoot, @".venv\Scripts\python.exe"),
"旧桌面会话未继承变量时读取机器安装目录及其Python,不回退E盘");
resolved = D3StartupOptions.FromEnvironment("app", "workspace",
(_, target) => target == EnvironmentVariableTarget.User ? userRoot : target == EnvironmentVariableTarget.Machine ? machineRoot : " ");
Check(resolved.Root == userRoot, "空进程变量按缺省处理,用户安装目录优先于机器配置");
resolved = D3StartupOptions.FromEnvironment("app", "workspace",
(_, target) => target == EnvironmentVariableTarget.Process ? processRoot : machineRoot);
Check(resolved.Root == processRoot, "显式进程安装目录仍优先于持久配置");
resolved = D3StartupOptions.FromEnvironment("app", "workspace", (_, _) => null);
Check(resolved.Root == Path.GetFullPath(@"E:\D3Lite"), "未配置安装目录时保留默认Lite目录");
}
var root=Path.Combine(Path.GetTempPath(),"d3-startup-"+Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(Path.Combine(root,"model"));
var python=Path.Combine(root,"python.exe");var script=Path.Combine(root,"service.py");