34 lines
1.3 KiB
C#
34 lines
1.3 KiB
C#
namespace UCVL.VPNPlus;
|
|
|
|
internal static class InstallationVersionPolicy
|
|
{
|
|
private static bool TryParse(string? value, out Version normalized)
|
|
{
|
|
normalized = new Version(0, 0, 0, 0);
|
|
if (string.IsNullOrWhiteSpace(value)) return false;
|
|
var text = value.Trim();
|
|
var suffix = text.IndexOfAny(['-', '+']);
|
|
if (suffix >= 0) text = text[..suffix];
|
|
if (!Version.TryParse(text, out var parsed) || parsed is null) return false;
|
|
normalized = new Version(parsed.Major, parsed.Minor,
|
|
parsed.Build < 0 ? 0 : parsed.Build,
|
|
parsed.Revision < 0 ? 0 : parsed.Revision);
|
|
return true;
|
|
}
|
|
|
|
internal static bool CanInstall(string candidate, string? installed)
|
|
{
|
|
if (!TryParse(candidate, out var next)) return false;
|
|
if (string.IsNullOrWhiteSpace(installed)) return true;
|
|
return TryParse(installed, out var current) && next >= current;
|
|
}
|
|
|
|
internal static void RequireNotDowngrade(string candidate, string? installed)
|
|
{
|
|
if (CanInstall(candidate, installed)) return;
|
|
throw new IOException(string.IsNullOrWhiteSpace(installed)
|
|
? "安装包版本无效,未修改现有程序。"
|
|
: $"检测到旧版安装包({candidate}),当前版本为 {installed};已拒绝降级且未修改现有程序。");
|
|
}
|
|
}
|