diff --git a/installer/Model/Downloader.cs b/installer/Model/Downloader.cs index 61ccc1f5..3bc0dc1b 100755 --- a/installer/Model/Downloader.cs +++ b/installer/Model/Downloader.cs @@ -115,7 +115,23 @@ public Downloader() Web = new EEsast(LoggerProvider.FromFile(Path.Combine(Data.LogPath, "EESAST.log")), LoggerProvider.FromFile(Path.Combine(Data.LogPath, "EESAST.error.log"))); Web.Token_Changed += SaveToken; + LoggerBinding(); + string? temp; + if (Data.Config.TryGetValue("Remembered", out temp)) + { + if (Convert.ToBoolean(temp)) + { + if (Data.Config.TryGetValue("Username", out temp)) + Username = temp; + if (Data.Config.TryGetValue("Password", out temp)) + Password = temp; + } + } + } + + public void LoggerBinding() + { // Debug模式下将Exceptions直接抛出触发断点 if (Debugger.IsAttached && MauiProgram.ErrorTrigger_WhileDebug) { @@ -153,17 +169,10 @@ public Downloader() LogError.LogError($"从Downloader.Web处提取的错误。"); Exceptions.Push(e); }; - string? temp; - if (Data.Config.TryGetValue("Remembered", out temp)) + Exceptions.OnFailClear += (_, _) => { - if (Convert.ToBoolean(temp)) - { - if (Data.Config.TryGetValue("Username", out temp)) - Username = temp; - if (Data.Config.TryGetValue("Password", out temp)) - Password = temp; - } - } + Status = UpdateStatus.success; + }; } public void UpdateMD5() @@ -172,7 +181,7 @@ public void UpdateMD5() File.Delete(Data.MD5DataPath); Status = UpdateStatus.downloading; Cloud.DownloadFileAsync(Data.MD5DataPath, "hash.json").Wait(); - if (Cloud.Exceptions.Count > 0) + if (Exceptions.Count > 0) { Status = UpdateStatus.error; return; @@ -188,8 +197,21 @@ public void Install() UpdateMD5(); if (Status == UpdateStatus.error) return; - if (Directory.Exists(Data.InstallPath)) - Directory.Delete(Data.InstallPath, true); + Action action = (dir) => { }; + var deleteTask = (DirectoryInfo dir) => + { + foreach (var file in dir.EnumerateFiles()) + { + if (!Local_Data.IsUserFile(file.FullName)) + file.Delete(); + } + foreach (var sub in dir.EnumerateDirectories()) + { + action(sub); + } + }; + action = deleteTask; + deleteTask(new DirectoryInfo(Data.InstallPath)); Data.Installed = false; string zp = Path.Combine(Data.InstallPath, "THUAI7.tar.gz"); @@ -223,7 +245,23 @@ public void ResetInstallPath(string newPath) newPath = newPath.EndsWith(Path.DirectorySeparatorChar) ? newPath[0..-1] : newPath; var installPath = Data.InstallPath.EndsWith(Path.DirectorySeparatorChar) ? Data.InstallPath[0..-1] : Data.InstallPath; if (newPath != Data.InstallPath) + { + Log.Dispose(); LogError.Dispose(); Exceptions.logger.Dispose(); + Cloud.Log.Dispose(); Cloud.LogError.Dispose(); Cloud.Exceptions.logger.Dispose(); + Web.Log.Dispose(); Web.LogError.Dispose(); Web.Exceptions.logger.Dispose(); Data.ResetInstallPath(newPath); + + Cloud.Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "TencentCos.log")); + Cloud.LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "TencentCos.error.log")); + Cloud.Exceptions = new ExceptionStack(Cloud.LogError, Cloud); + Web.Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "EESAST.log")); + Web.LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "EESAST.error.log")); + Web.Exceptions = new ExceptionStack(Web.LogError, Web); + Log = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "Main.log")); + LogError = LoggerProvider.FromFile(Path.Combine(Data.LogPath, "Main.error.log")); + Exceptions = new ExceptionStack(LogError, this); + LoggerBinding(); + } } /// diff --git a/installer/Model/EEsast.cs b/installer/Model/EEsast.cs index c64dae6c..0f9dc575 100755 --- a/installer/Model/EEsast.cs +++ b/installer/Model/EEsast.cs @@ -1,5 +1,4 @@ -using Microsoft.Extensions.Logging; -using System; +using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; diff --git a/installer/Model/Helper.cs b/installer/Model/Helper.cs index fa494993..84447485 100755 --- a/installer/Model/Helper.cs +++ b/installer/Model/Helper.cs @@ -42,7 +42,6 @@ public static string GetFileMd5Hash(string strFileFullPath) public static string ConvertAbsToRel(string basePath, string fullPath) { - fullPath = fullPath.Replace(Path.DirectorySeparatorChar, '/'); if (fullPath.StartsWith(basePath)) { fullPath = fullPath.Replace(basePath, "."); diff --git a/installer/Model/Local_Data.cs b/installer/Model/Local_Data.cs index 1e87ced4..34d076ea 100755 --- a/installer/Model/Local_Data.cs +++ b/installer/Model/Local_Data.cs @@ -121,61 +121,66 @@ public Local_Data() public void ResetInstallPath(string newPath) { - if (!Directory.Exists(newPath)) - { - Directory.CreateDirectory(newPath); - } if (Installed) { // 移动已有文件夹至新位置 try { - Directory.Move(InstallPath, newPath); - } - catch (IOException e) - { - if (e.Message.ToLower().Contains("move will not work across volumes")) + if (!Directory.Exists(newPath)) + { + Directory.CreateDirectory(newPath); + } + Log.LogInfo($"Move work started: {InstallPath} -> {newPath}"); + Log.Dispose(); LogError.Dispose(); Exceptions.logger.Dispose(); + Action action = (dir) => { }; + var moveTask = (DirectoryInfo dir) => { - // 跨盘符移动 - Action action = (dir) => { }; - var moveTask = (DirectoryInfo dir) => + foreach (var file in dir.EnumerateFiles()) { - foreach (var file in dir.EnumerateFiles()) - { - var newName = Path.Combine(newPath, Helper.ConvertAbsToRel(InstallPath, file.FullName)); - file.MoveTo(newName); - } - foreach (var sub in dir.EnumerateDirectories()) + var newName = Path.Combine(newPath, Helper.ConvertAbsToRel(InstallPath, file.FullName)); + file.MoveTo(newName); + } + foreach (var sub in dir.EnumerateDirectories()) + { + var newName = Path.Combine(newPath, Helper.ConvertAbsToRel(InstallPath, sub.FullName)); + if (!Directory.Exists(newName)) { - var newName = Path.Combine(newPath, Helper.ConvertAbsToRel(InstallPath, sub.FullName)); - if (!Directory.Exists(newName)) - { - Directory.CreateDirectory(newName); - } - action(sub); + Directory.CreateDirectory(newName); } - }; - action = moveTask; - moveTask(new DirectoryInfo(InstallPath)); - Directory.Delete(InstallPath, true); - } + action(sub); + } + }; + action = moveTask; + moveTask(new DirectoryInfo(InstallPath)); + Directory.Delete(InstallPath, true); + InstallPath = newPath; + if (Config.ContainsKey("InstallPath")) + Config["InstallPath"] = InstallPath; else + Config.Add("InstallPath", InstallPath); + MD5DataPath = Config["MD5DataPath"].StartsWith('.') ? + Path.Combine(InstallPath, Config["MD5DataPath"]) : + Config["MD5DataPath"]; + SaveConfig(); + SaveMD5Data(); + Installed = true; + } + catch (Exception e) + { + Exceptions.Push(e); + } + finally + { + if (!Directory.Exists(LogPath)) { - Exceptions.Push(e); + Directory.CreateDirectory(LogPath); } + Log = LoggerProvider.FromFile(Path.Combine(LogPath, "LocalData.log")); + LogError = LoggerProvider.FromFile(Path.Combine(LogPath, "LocalData.error.log")); + Exceptions = new ExceptionStack(LogError, this); + Log.LogInfo($"Move work finished: {InstallPath} -> {newPath}"); } } - InstallPath = newPath; - if (Config.ContainsKey("InstallPath")) - Config["InstallPath"] = InstallPath; - else - Config.Add("InstallPath", InstallPath); - MD5DataPath = Config["MD5DataPath"].StartsWith('.') ? - Path.Combine(InstallPath, Config["MD5DataPath"]) : - Config["MD5DataPath"]; - SaveConfig(); - SaveMD5Data(); - Installed = true; } public static bool IsUserFile(string filename) @@ -190,6 +195,8 @@ public static bool IsUserFile(string filename) return true; if (filename.Contains("hash.json")) return true; + if (filename.EndsWith("log")) + return true; return false; } @@ -258,21 +265,23 @@ public void ReadMD5Data() { Exceptions.Push(e); newMD5Data = new Dictionary(); + r.Close(); r.Dispose(); } foreach (var item in newMD5Data) { - if (MD5Data.ContainsKey(item.Key)) + var key = item.Key.Replace('/', Path.DirectorySeparatorChar); + if (MD5Data.ContainsKey(key)) { - if (MD5Data[item.Key] != item.Value) + if (MD5Data[key] != item.Value) { - MD5Data[item.Key] = item.Value; - MD5Update.Add((DataRowState.Modified, item.Key)); + MD5Data[key] = item.Value; + MD5Update.Add((DataRowState.Modified, key)); } } else { - MD5Data.Add(item.Key, item.Value); - MD5Update.Add((DataRowState.Added, item.Key)); + MD5Data.Add(key, item.Value); + MD5Update.Add((DataRowState.Added, key)); } } } @@ -285,7 +294,9 @@ public void SaveMD5Data() using (StreamWriter sw = new StreamWriter(fs)) { fs.SetLength(0); - sw.Write(JsonSerializer.Serialize(MD5Data)); + var exp1 = from i in MD5Data + select new KeyValuePair(i.Key.Replace(Path.DirectorySeparatorChar, '/'), i.Value); + sw.Write(JsonSerializer.Serialize(exp1.ToDictionary())); sw.Flush(); } } diff --git a/installer/Model/Tencent_Cos.cs b/installer/Model/Tencent_Cos.cs index 18084238..ee210b50 100755 --- a/installer/Model/Tencent_Cos.cs +++ b/installer/Model/Tencent_Cos.cs @@ -75,10 +75,6 @@ public async Task DownloadFileAsync(string savePath, string? remotePath = n GetObjectRequest request = new GetObjectRequest(bucket, remotePath ?? localFileName, localDir, localFileName); Dictionary test = request.GetRequestHeaders(); - request.SetCosProgressCallback(delegate (long completed, long total) - { - Log.LogInfo(thID, $"[Download: {remotePath}] progress = {completed * 100.0 / total:##.##}%"); - }); // 执行请求 GetObjectResult result = cosXml.GetObject(request); // 请求成功 diff --git a/installer/Page/InstallPage.xaml b/installer/Page/InstallPage.xaml index 446c610e..984f9896 100644 --- a/installer/Page/InstallPage.xaml +++ b/installer/Page/InstallPage.xaml @@ -15,6 +15,7 @@