mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-06-20 15:44:25 +08:00
65 lines
2.1 KiB
C#
65 lines
2.1 KiB
C#
using System.Text.Json;
|
|
|
|
namespace LanMountainDesktop.Shared.IPC;
|
|
|
|
public static class AirAppRuntimeDataRootResolver
|
|
{
|
|
private const string LauncherDataFolderName = ".Launcher";
|
|
private const string ConfigFileName = "data-location.config.json";
|
|
private const string DesktopFolderName = "Desktop";
|
|
|
|
public static string ResolveDataRoot(string? appRoot)
|
|
{
|
|
var defaultSystemDataPath = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
|
"LanMountainDesktop");
|
|
|
|
if (string.IsNullOrWhiteSpace(appRoot))
|
|
{
|
|
return defaultSystemDataPath;
|
|
}
|
|
|
|
var normalizedAppRoot = Path.GetFullPath(appRoot);
|
|
var configPath = Path.Combine(normalizedAppRoot, LauncherDataFolderName, ConfigFileName);
|
|
if (!File.Exists(configPath))
|
|
{
|
|
return defaultSystemDataPath;
|
|
}
|
|
|
|
try
|
|
{
|
|
using var document = JsonDocument.Parse(File.ReadAllText(configPath));
|
|
var root = document.RootElement;
|
|
var mode = GetString(root, "dataLocationMode");
|
|
|
|
if (string.Equals(mode, "Portable", StringComparison.OrdinalIgnoreCase))
|
|
{
|
|
return Path.GetFullPath(
|
|
GetString(root, "portableDataPath")
|
|
?? Path.Combine(normalizedAppRoot, DesktopFolderName));
|
|
}
|
|
|
|
return Path.GetFullPath(GetString(root, "systemDataPath") ?? defaultSystemDataPath);
|
|
}
|
|
catch
|
|
{
|
|
return defaultSystemDataPath;
|
|
}
|
|
}
|
|
|
|
private static string? GetString(JsonElement element, string propertyName)
|
|
{
|
|
foreach (var property in element.EnumerateObject())
|
|
{
|
|
if (string.Equals(property.Name, propertyName, StringComparison.OrdinalIgnoreCase) &&
|
|
property.Value.ValueKind is JsonValueKind.String)
|
|
{
|
|
var value = property.Value.GetString();
|
|
return string.IsNullOrWhiteSpace(value) ? null : value;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|