81 lines
2.9 KiB
C#
81 lines
2.9 KiB
C#
using System;
|
|
using System.IO;
|
|
|
|
namespace Deploymentcenter.Client;
|
|
|
|
public static class LicenseConfig
|
|
{
|
|
private static string? _storageDirectoryOverride;
|
|
private static string? _hardwareIdOverride;
|
|
|
|
public static string? HardwareIdOverride
|
|
{
|
|
get
|
|
{
|
|
if (!string.IsNullOrWhiteSpace(_hardwareIdOverride))
|
|
return _hardwareIdOverride;
|
|
|
|
var envHwid = Environment.GetEnvironmentVariable("DEPLOYMENTCENTER_HWID")
|
|
?? Environment.GetEnvironmentVariable("LICENSELABRADOR_HWID");
|
|
if (!string.IsNullOrWhiteSpace(envHwid))
|
|
return envHwid;
|
|
|
|
return null;
|
|
}
|
|
set => _hardwareIdOverride = value;
|
|
}
|
|
|
|
public static string GetStorageDirectory(string productSlug)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(productSlug))
|
|
productSlug = "default_app";
|
|
|
|
// 1. Explicitly set property
|
|
if (!string.IsNullOrWhiteSpace(_storageDirectoryOverride))
|
|
return ValidateNonEmpty(_storageDirectoryOverride!);
|
|
|
|
// 2. Environment Variable
|
|
var envDir = Environment.GetEnvironmentVariable("DEPLOYMENTCENTER_STORAGE_DIR")
|
|
?? Environment.GetEnvironmentVariable("LICENSELABRADOR_STORAGE_DIR");
|
|
if (!string.IsNullOrWhiteSpace(envDir))
|
|
return ValidateNonEmpty(Path.Combine(envDir!, productSlug, "license"));
|
|
|
|
// 3. Platform specific resolution
|
|
if (OperatingSystemHelpers.IsLinux() || OperatingSystemHelpers.IsMacOS())
|
|
{
|
|
var xdg = Environment.GetEnvironmentVariable("XDG_CONFIG_HOME");
|
|
if (!string.IsNullOrWhiteSpace(xdg))
|
|
return ValidateNonEmpty(Path.Combine(xdg!, productSlug, "license"));
|
|
|
|
var home = Environment.GetEnvironmentVariable("HOME");
|
|
if (!string.IsNullOrWhiteSpace(home))
|
|
return ValidateNonEmpty(Path.Combine(home!, ".config", productSlug, "license"));
|
|
}
|
|
else if (OperatingSystemHelpers.IsWindows())
|
|
{
|
|
var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
|
|
if (!string.IsNullOrWhiteSpace(appData))
|
|
return ValidateNonEmpty(Path.Combine(appData, productSlug, "license"));
|
|
}
|
|
|
|
// 4. Fallback to AppContext.BaseDirectory
|
|
var baseDir = AppContext.BaseDirectory;
|
|
if (!string.IsNullOrWhiteSpace(baseDir))
|
|
return ValidateNonEmpty(Path.Combine(baseDir, "license"));
|
|
|
|
throw new InvalidOperationException("Could not resolve valid non-empty StorageDirectory for licensing.");
|
|
}
|
|
|
|
public static void SetStorageDirectory(string path)
|
|
{
|
|
_storageDirectoryOverride = path;
|
|
}
|
|
|
|
private static string ValidateNonEmpty(string path)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(path))
|
|
throw new InvalidOperationException("StorageDirectory resolved to an empty or invalid path.");
|
|
return path;
|
|
}
|
|
}
|