Skip to content

Commit

Permalink
Merge pull request #90 from DreamEnderKing/dev
Browse files Browse the repository at this point in the history
fix: 🐛 Fixed some unexpected bugs on installer page.
  • Loading branch information
ONLOX authored Jan 29, 2024
2 parents 24e95ba + 6c534f3 commit 5d6cbdd
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 69 deletions.
64 changes: 51 additions & 13 deletions installer/Model/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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()
Expand All @@ -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;
Expand All @@ -188,8 +197,21 @@ public void Install()
UpdateMD5();
if (Status == UpdateStatus.error) return;

if (Directory.Exists(Data.InstallPath))
Directory.Delete(Data.InstallPath, true);
Action<DirectoryInfo> 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");
Expand Down Expand Up @@ -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();
}
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions installer/Model/EEsast.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Extensions.Logging;
using System;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
Expand Down
1 change: 0 additions & 1 deletion installer/Model/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ".");
Expand Down
107 changes: 59 additions & 48 deletions installer/Model/Local_Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DirectoryInfo> action = (dir) => { };
var moveTask = (DirectoryInfo dir) =>
{
// 跨盘符移动
Action<DirectoryInfo> 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)
Expand All @@ -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;
}

Expand Down Expand Up @@ -258,21 +265,23 @@ public void ReadMD5Data()
{
Exceptions.Push(e);
newMD5Data = new Dictionary<string, string>();
r.Close(); r.Dispose();
}
foreach (var item in newMD5Data)

Check warning on line 270 in installer/Model/Local_Data.cs

View workflow job for this annotation

GitHub Actions / dotnet-build-install

Dereference of a possibly null reference.
{
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));
}
}
}
Expand All @@ -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<string, string>(i.Key.Replace(Path.DirectorySeparatorChar, '/'), i.Value);
sw.Write(JsonSerializer.Serialize(exp1.ToDictionary<string, string>()));
sw.Flush();
}
}
Expand Down
4 changes: 0 additions & 4 deletions installer/Model/Tencent_Cos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,6 @@ public async Task<int> DownloadFileAsync(string savePath, string? remotePath = n
GetObjectRequest request = new GetObjectRequest(bucket, remotePath ?? localFileName, localDir, localFileName);

Dictionary<string, string> 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);
// 请求成功
Expand Down
2 changes: 2 additions & 0 deletions installer/Page/InstallPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
<Button
WidthRequest="400"
Text="下载"
IsEnabled="{Binding InstallEnabled}"
Command="{Binding DownloadBtnClickedCommand}"
BackgroundColor="{Binding ConstBackgroundColor}"
FontSize="{Binding ConstFontSize}"
TextColor="{Binding ConstTextColor}"/>
<Button
WidthRequest="200"
Text="检查更新"
IsEnabled="{Binding CheckEnabled}"
Command="{Binding CheckUpdBtnClickedCommand}"
BackgroundColor="{Binding ConstBackgroundColor}"
FontSize="{Binding ConstFontSize}"
Expand Down
21 changes: 20 additions & 1 deletion installer/ViewModel/InstallViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ internal class InstallViewModel : BaseViewModel
{
public InstallViewModel()
{
downloadPath = "D:\\THUAI7";
downloadPath = Downloader.Data.InstallPath;
installEnabled = false;
BrowseBtnClickedCommand = new RelayCommand(BrowseBtnClicked);
CheckUpdBtnClickedCommand = new RelayCommand(CheckUpdBtnClicked);
DownloadBtnClickedCommand = new RelayCommand(DownloadBtnClicked);
Expand Down Expand Up @@ -40,6 +41,7 @@ public string DownloadPath
set
{
downloadPath = value;
InstallEnabled = (value != Downloader.Data.InstallPath);
OnPropertyChanged();
}
}
Expand Down Expand Up @@ -69,6 +71,23 @@ private async void DownloadBtnClicked()
DebugAlert = "Download Button Clicked";
await Task.Run(() => Downloader.ResetInstallPath(downloadPath));
await Task.Run(() => Downloader.Install());
DownloadPath = Downloader.Data.InstallPath;
}

private bool installEnabled;
public bool InstallEnabled
{
get => installEnabled;
set
{
installEnabled = value;
OnPropertyChanged("CheckEnabled");
OnPropertyChanged();
}
}
public bool CheckEnabled
{
get => !installEnabled;
}
}
}

0 comments on commit 5d6cbdd

Please sign in to comment.