| 1 | using System.Globalization; |
| 2 | using System.Text.RegularExpressions; |
| 3 | |
| 4 | namespace Cage.Windows; |
| 5 | |
| 6 | internal static partial class UpdatePolicy |
| 7 | { |
| 8 | public static bool IsWorkbench(string? raw, int port) => |
| 9 | Uri.TryCreate(raw, UriKind.Absolute, out var url) && port > 0 && |
| 10 | url.Scheme == "http" && url.Host == "localhost" && url.Port == port && |
| 11 | url.UserInfo.Length == 0; |
| 12 | |
| 13 | public static Uri Entry(string raw) |
| 14 | { |
| 15 | if (!Uri.TryCreate(raw, UriKind.Absolute, out var url) || |
| 16 | !IsWorkbench(raw, url.Port) || url.Port <= 0 || url.Fragment.Length < 2) |
| 17 | throw new InvalidDataException("后台返回了无效的工作台地址"); |
| 18 | return url; |
| 19 | } |
| 20 | |
| 21 | [GeneratedRegex(@"^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?$")] |
| 22 | private static partial Regex VersionPattern(); |
| 23 | |
| 24 | private static string[]? VersionParts(string value) |
| 25 | { |
| 26 | var match = VersionPattern().Match(value); |
| 27 | if (!match.Success) return null; |
| 28 | var pre = match.Groups[4].Value; |
| 29 | if (pre.Split('.').Any(x => x.Length > 1 && x[0] == '0' && x.All(char.IsAsciiDigit))) return null; |
| 30 | return [match.Groups[1].Value, match.Groups[2].Value, match.Groups[3].Value, pre]; |
| 31 | } |
| 32 | |
| 33 | private static int Number(string a, string b) => a.Length != b.Length |
| 34 | ? a.Length.CompareTo(b.Length) : string.CompareOrdinal(a, b); |
| 35 | |
| 36 | public static bool IsNewer(string candidate, string current) |
| 37 | { |
| 38 | var a = VersionParts(candidate); var b = VersionParts(current); |
| 39 | if (a == null || b == null) return false; |
| 40 | for (var i = 0; i < 3; i++) |
| 41 | if (a[i] != b[i]) return Number(a[i], b[i]) > 0; |
| 42 | if (a[3].Length == 0 || b[3].Length == 0) return a[3].Length == 0 && b[3].Length != 0; |
| 43 | var x = a[3].Split('.'); var y = b[3].Split('.'); |
| 44 | for (var i = 0; i < Math.Min(x.Length, y.Length); i++) |
| 45 | { |
| 46 | if (x[i] == y[i]) continue; |
| 47 | var xn = x[i].All(char.IsAsciiDigit); var yn = y[i].All(char.IsAsciiDigit); |
| 48 | if (xn != yn) return !xn; |
| 49 | return (xn ? Number(x[i], y[i]) : string.CompareOrdinal(x[i], y[i])) > 0; |
| 50 | } |
| 51 | return x.Length > y.Length; |
| 52 | } |
| 53 | |
| 54 | public static string DownloadPath(string raw, string updateRoot) |
| 55 | { |
| 56 | var path = Path.GetFullPath(raw); |
| 57 | var directory = Path.GetDirectoryName(path) ?? ""; |
| 58 | if (!string.Equals(Path.GetDirectoryName(directory), Path.GetFullPath(updateRoot).TrimEnd(Path.DirectorySeparatorChar), StringComparison.OrdinalIgnoreCase) || |
| 59 | !Path.GetFileName(directory).StartsWith("download-", StringComparison.Ordinal) || |
| 60 | !string.Equals(Path.GetFileName(path), "Cage-setup.exe", StringComparison.Ordinal)) |
| 61 | throw new InvalidDataException("更新安装包不属于 Cage 的私有下载目录"); |
| 62 | return path; |
| 63 | } |
| 64 | |
| 65 | public static void PlainPath(string path) |
| 66 | { |
| 67 | var entry = Path.GetFullPath(path); |
| 68 | while (!string.IsNullOrEmpty(entry)) |
| 69 | { |
| 70 | if ((File.Exists(entry) || Directory.Exists(entry)) && (File.GetAttributes(entry) & FileAttributes.ReparsePoint) != 0) |
| 71 | throw new InvalidDataException("更新路径包含符号链接或目录联接"); |
| 72 | entry = Path.GetDirectoryName(entry); |
| 73 | } |
| 74 | } |
| 75 | } |