Skip to content

Commit

Permalink
Merge pull request #113 from DreamEnderKing/dev
Browse files Browse the repository at this point in the history
fix: 🐛 Fixed some unexpected exitment from loop and funtion in installer.
  • Loading branch information
shangfengh authored Mar 7, 2024
2 parents ecf4f46 + a42a8a2 commit 0ab488a
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 31 deletions.
27 changes: 24 additions & 3 deletions installer/Model/Downloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public enum UpdateStatus
{
success, unarchieving, downloading, hash_computing, error
}
public UpdateStatus Status; // 当前工作状态
public UpdateStatus Status = UpdateStatus.success; // 当前工作状态

public ConcurrentQueue<string> downloadFailed = new ConcurrentQueue<string>();
public string Route { get; set; }
Expand Down Expand Up @@ -188,6 +188,7 @@ public void UpdateMD5()
return;
}
Data.ReadMD5Data();
Status = UpdateStatus.success;
}

/// <summary>
Expand Down Expand Up @@ -223,6 +224,17 @@ public void Install()
File.Delete(zp);

Data.ResetInstallPath(Data.InstallPath);
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();

Status = UpdateStatus.hash_computing;
Data.ScanDir();
if (Data.MD5Update.Count != 0)
Expand All @@ -245,7 +257,7 @@ 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)
if (newPath != installPath)
{
Log.Dispose(); LogError.Dispose(); Exceptions.logger.Dispose();
Cloud.Log.Dispose(); Cloud.LogError.Dispose(); Cloud.Exceptions.logger.Dispose();
Expand All @@ -263,6 +275,7 @@ public void ResetInstallPath(string newPath)
Exceptions = new ExceptionStack(LogError, this);
LoggerBinding();
}
Update();
}

/// <summary>
Expand All @@ -276,6 +289,7 @@ public bool CheckUpdate()
Data.MD5Update.Clear();
Status = UpdateStatus.hash_computing;
Data.ScanDir();
Status = UpdateStatus.success;
return Data.MD5Update.Count != 0;
}

Expand All @@ -288,8 +302,15 @@ public void Update()
{
Status = UpdateStatus.downloading;
Cloud.DownloadQueueAsync(Data.InstallPath,
from item in Data.MD5Update select item.name,
from item in Data.MD5Update where item.state != System.Data.DataRowState.Added select item.name,
downloadFailed).Wait();
foreach (var item in Data.MD5Update.Where((s) => s.state == System.Data.DataRowState.Added))
{
var _file = item.name;
var file = _file.StartsWith('.') ?
Path.Combine(Data.InstallPath, _file) : _file;
File.Delete(file);
}
if (downloadFailed.Count == 0)
{
Data.MD5Update.Clear();
Expand Down
52 changes: 26 additions & 26 deletions installer/Model/Local_Data.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,10 +121,10 @@ public Local_Data()

public void ResetInstallPath(string newPath)
{
if (Installed)
// 移动已有文件夹至新位置
try
{
// 移动已有文件夹至新位置
try
if (InstallPath != newPath)
{
if (!Directory.Exists(newPath))
{
Expand Down Expand Up @@ -154,32 +154,32 @@ public void ResetInstallPath(string newPath)
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 (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))
{
if (!Directory.Exists(LogPath))
{
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}");
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}");
}
}

Expand Down
6 changes: 5 additions & 1 deletion installer/Model/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,13 @@ public FileLogger(string path)
var option = new FileStreamOptions();
option.Mode = FileMode.Append;
option.Access = FileAccess.Write;
option.Share = FileShare.Read;
option.Share = FileShare.ReadWrite;
writer = new StreamWriter(path, Encoding.UTF8, option);
}
~FileLogger()
{
writer.Dispose();
}
protected override bool IsEnabled(LogLevel logLevel)
{
if (Debugger.IsAttached)
Expand Down
5 changes: 4 additions & 1 deletion installer/Model/Tencent_Cos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public async Task<int> DownloadFileAsync(string savePath, string? remotePath = n
string localDir = Path.GetDirectoryName(savePath) // 本地文件夹
?? throw new Exception("本地文件夹路径获取失败");
string localFileName = Path.GetFileName(savePath); // 指定本地保存的文件名
remotePath = remotePath?.Replace('\\', '/')?.TrimStart('.', '/');
GetObjectRequest request = new GetObjectRequest(bucket, remotePath ?? localFileName, localDir, localFileName);

Dictionary<string, string> test = request.GetRequestHeaders();
Expand All @@ -99,12 +100,14 @@ public async Task<int> DownloadQueueAsync(string basePath, IEnumerable<string> q
var array = queue.ToArray();
int count = array.Count();
int finished = 0;
if (count == 0)
return 0;
var partitionar = Partitioner.Create(0, count);
Parallel.ForEach(partitionar, (range, loopState) =>
{
for (int i = range.Item1; i < range.Item2; i++)
{
if (!loopState.IsStopped)
if (loopState.IsStopped)
break;
string local = Path.Combine(basePath, array[i]);
int subID = -1;
Expand Down

0 comments on commit 0ab488a

Please sign in to comment.