From c3eff423c6bc101e9beff1b024180e89da096e27 Mon Sep 17 00:00:00 2001 From: DreamEnderKing Date: Sat, 16 Mar 2024 21:33:10 +0800 Subject: [PATCH] fix: :bug: Fixed some bug and add version id to local_data. --- installer/Model/Downloader.cs | 3 +- installer/Model/Local_Data.cs | 63 ++++++++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/installer/Model/Downloader.cs b/installer/Model/Downloader.cs index f7fc12e5..db9f9f9e 100755 --- a/installer/Model/Downloader.cs +++ b/installer/Model/Downloader.cs @@ -194,9 +194,10 @@ public void UpdateMD5() /// /// 全新安装 /// - public void Install() + public void Install(string? path = null) { Data.Installed = false; + Data.InstallPath = path ?? Data.InstallPath; UpdateMD5(); if (Status == UpdateStatus.error) return; diff --git a/installer/Model/Local_Data.cs b/installer/Model/Local_Data.cs index 031b45a1..76063de8 100755 --- a/installer/Model/Local_Data.cs +++ b/installer/Model/Local_Data.cs @@ -10,11 +10,46 @@ namespace installer.Model { + public record VersionID + { + public VersionID(int major, int minor, int build, int revision) + { + (Major, Minor, Build, Revision) = (major, minor, build, revision); + } + public int Major, Minor, Build, Revision; + public static bool operator >(VersionID left, VersionID right) + { + return (left.Major > right.Major) | + (left.Major == right.Major && left.Minor > right.Minor) | + (left.Major == right.Major && left.Minor == right.Minor && left.Build > right.Build) | + (left.Major == right.Major && left.Minor == right.Minor && left.Build == right.Build && left.Revision > right.Revision); + } + public static bool operator <(VersionID left, VersionID right) + { + return (left.Major < right.Major) | + (left.Major == right.Major && left.Minor < right.Minor) | + (left.Major == right.Major && left.Minor == right.Minor && left.Build < right.Build) | + (left.Major == right.Major && left.Minor == right.Minor && left.Build == right.Build && left.Revision < right.Revision); + } + } + public class MD5DataFile + { + public Dictionary Data { get; set; } = new Dictionary(); + public (int, int, int, int) Version = (1, 0, 0, 0); + public string Description { get; set; } + = "The Description of the current version."; + public string BugFixed { get; set; } + = "Bugs had been fixed."; + public string BugGenerated { get; set; } + = "New bugs found in the new version."; + } + public class Local_Data { public string ConfigPath; // 标记路径记录文件THUAI7.json的路径 public string MD5DataPath; // 标记MD5本地缓存文件的路径 - public string UserCodePostfix; // 用户文件后缀(.cpp/.py) + public string UserCodePostfix = "cpp"; // 用户文件后缀(.cpp/.py) + public MD5DataFile FileData = new MD5DataFile(); public string UserCodePath { get => Path.Combine(InstallPath, @@ -221,25 +256,20 @@ public void SaveConfig() public void ReadMD5Data() { - Dictionary newMD5Data; + FileData = new MD5DataFile(); StreamReader r = new StreamReader(MD5DataPath); try { string json = r.ReadToEnd(); - if (json is null || json == "") - { - newMD5Data = new Dictionary(); - } - else + if (!string.IsNullOrEmpty(json)) { - newMD5Data = JsonSerializer.Deserialize>(json) ?? new Dictionary(); + FileData = JsonSerializer.Deserialize(json) ?? new MD5DataFile(); } r.Close(); r.Dispose(); } catch (JsonException e) { // Json反序列化失败,考虑重新创建MD5数据库 - newMD5Data = new Dictionary(); r.Close(); r.Dispose(); File.Delete(MD5DataPath); File.Create(MD5DataPath); @@ -247,10 +277,9 @@ public void ReadMD5Data() catch (Exception e) { Exceptions.Push(e); - newMD5Data = new Dictionary(); r.Close(); r.Dispose(); } - foreach (var item in newMD5Data) + foreach (var item in FileData.Data) { var key = item.Key.Replace('/', Path.DirectorySeparatorChar); MD5Data.AddOrUpdate(key, (k) => @@ -276,7 +305,8 @@ public void SaveMD5Data() fs.SetLength(0); var exp1 = from i in MD5Data select new KeyValuePair(i.Key.Replace(Path.DirectorySeparatorChar, '/'), i.Value); - sw.Write(JsonSerializer.Serialize(exp1.ToDictionary())); + FileData.Data = exp1.ToDictionary(); + sw.Write(JsonSerializer.Serialize(FileData)); sw.Flush(); } } @@ -362,14 +392,15 @@ public static bool IsUserFile(string filename) return false; } - public static int CountFile(string folder) + public static int CountFile(string folder, string? root = null) { - int result = (from f in Directory.EnumerateDirectories(folder) - where !IsUserFile(f) + int result = (from f in Directory.EnumerateFiles(folder) + let t = Helper.ConvertAbsToRel(root ?? folder, f) + where !IsUserFile(t) select f).Count(); foreach (var d in Directory.EnumerateDirectories(folder)) { - result += CountFile(d); + result += CountFile(d, root ?? folder); } return result; }