Skip to content

Commit

Permalink
自动更新增加一个简单的文件效验
Browse files Browse the repository at this point in the history
保存备份机制修改
  • Loading branch information
rmbadmin committed Jan 19, 2021
1 parent c1b19b4 commit 79871c3
Show file tree
Hide file tree
Showing 7 changed files with 270 additions and 50 deletions.
2 changes: 1 addition & 1 deletion source/SteamTool.Model/ToolModel/GithubReleaseModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class Assets
public string url { get; set; }
public string name { get; set; }
public string content_type { get; set; }
public string size { get; set; }
public long size { get; set; }
public string download_count { get; set; }
public string updated_at { get; set; }
public string browser_download_url { get; set; }
Expand Down
19 changes: 6 additions & 13 deletions source/SteamTool.Proxy/HttpProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public async Task OnRequest(object sender, SessionEventArgs e)
}

//没有匹配到的结果直接返回不支持,避免出现Loopback死循环内存溢出
e.Ok($"URL : {e.HttpClient.Request.RequestUri.AbsoluteUri} {Environment.NewLine} not support proxy");
e.Ok($"URL : {e.HttpClient.Request.RequestUri.AbsoluteUri} {Environment.NewLine}not support proxy");
return;
}
public async Task OnResponse(object sender, SessionEventArgs e)
Expand Down Expand Up @@ -360,7 +360,7 @@ public bool StartProxy(bool IsProxyGOG = false)
//proxyServer.ServerCertificateValidationCallback += OnCertificateValidation;
//proxyServer.ClientCertificateSelectionCallback += OnCertificateSelection;

//var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 443, true)
//var explicitEndPoint = new ExplicitProxyEndPoint(IPAddress.Any, 8888, true)
//{
// // 在所有https请求上使用自颁发的通用证书
// // 通过不为每个启用http的域创建证书来优化性能
Expand All @@ -372,19 +372,11 @@ public bool StartProxy(bool IsProxyGOG = false)
//explicit endpoint 是客户端知道代理存在的地方
//因此,客户端以代理友好的方式发送请求
//proxyServer.AddEndPoint(explicitEndPoint);
// transparentEndPoint 对于反向代理很有用(客户端不知道代理的存在)
// transparentEndPoint 通常需要一个网络路由器端口来转发HTTP(S)包或DNS
// 发送数据到此endpoint
var transparentEndPoint = new TransparentProxyEndPoint(IPAddress.Any, 443, true)
proxyServer.AddEndPoint(new TransparentProxyEndPoint(IPAddress.Any, 443, true)
{
//GenericCertificate = proxyServer.CertificateManager.RootCertificate
};
proxyServer.AddEndPoint(transparentEndPoint);
var transparentEndPoint80 = new TransparentProxyEndPoint(IPAddress.Any, 80, true)
{
//GenericCertificate = proxyServer.CertificateManager.RootCertificate
};
proxyServer.AddEndPoint(transparentEndPoint80);
});
proxyServer.AddEndPoint(new TransparentProxyEndPoint(IPAddress.Any, 80, true));
proxyServer.ExceptionFunc = ((Exception exception) =>
{
Logger.Error(exception);
Expand All @@ -399,6 +391,7 @@ public bool StartProxy(bool IsProxyGOG = false)
Logger.Error(ex);
return false;
}

//proxyServer.UpStreamHttpProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
//proxyServer.UpStreamHttpsProxy = new ExternalProxy() { HostName = "localhost", Port = 8888 };
#endregion
Expand Down
2 changes: 1 addition & 1 deletion source/SteamTools/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,8 @@ private void RaisePropertyChanged([CallerMemberName] string propertyName = null)

void IDisposable.Dispose()
{
GC.SuppressFinalize(this);
this.compositeDisposable.Dispose();
GC.SuppressFinalize(this);
}

#endregion
Expand Down
8 changes: 6 additions & 2 deletions source/SteamTools/Models/Settings/SettingsHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,29 @@ public static void Load()
try
{
Providers.Local.Load();
//每次成功读取完成时保存一份bak
File.Copy(Providers.LocalFilePath, Providers.LocalFilePath + ".bak", true);
}
catch (Exception ex)
{
Logger.Error(ex);
MessageBox.Show("Local Settings Load Error :" + ex.ToString(), $"{ProductInfo.Title} {ProductInfo.VersionString} Error");
File.Copy(Providers.LocalFilePath, Providers.LocalFilePath + ".bak", true);
File.Copy(Providers.LocalFilePath, Providers.LocalFilePath + ".error", true);
File.Delete(Providers.LocalFilePath);
Providers.Local.Load();
}

try
{
Providers.Roaming.Load();
//每次成功读取完成时保存一份bak
File.Copy(Providers.RoamingFilePath, Providers.RoamingFilePath + ".bak", true);
}
catch (Exception ex)
{
Logger.Error(ex);
MessageBox.Show("Roaming Settings Load Error :" + ex.ToString(), $"{ProductInfo.Title} {ProductInfo.VersionString} Error");
File.Copy(Providers.RoamingFilePath, Providers.RoamingFilePath + ".bak", true);
File.Copy(Providers.RoamingFilePath, Providers.RoamingFilePath + ".error", true);
File.Delete(Providers.RoamingFilePath);
Providers.Roaming.Load();
}
Expand Down
54 changes: 30 additions & 24 deletions source/SteamTools/Services/AutoUpdateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,27 @@ public bool IsExistUpdate
}
#endregion

private GithubReleaseModel _UpdateInfo;
public GithubReleaseModel UpdateInfo
{
get => this._UpdateInfo;
set
{
if (this._UpdateInfo != value)
{
this._UpdateInfo = value;
this.RaisePropertyChanged();
}
}
}

public async void CheckUpdate()
{
try
{
StatusService.Current.Notify("正在从Github检查更新...");
var result = await httpServices.Get(Const.GITHUB_LATEST_RELEASEAPI_URL);
UpdateInfo = JsonConvert.DeserializeObject<GithubReleaseModel>(result);

// if (!(ProductInfo.Version > UpdateInfo.version)) Debug时反向检查更新来测试
if (!(ProductInfo.Version < UpdateInfo.version))
{
Expand All @@ -82,32 +95,18 @@ public async void CheckUpdate()

}

private GithubReleaseModel _UpdateInfo;
public GithubReleaseModel UpdateInfo
{
get => this._UpdateInfo;
set
{
if (this._UpdateInfo != value)
{
this._UpdateInfo = value;
this.RaisePropertyChanged();
}
}
}

public async void DownloadUpdate()
{
if (WindowService.Current.MainWindow.Dialog($"检测到新版本更新内容:{UpdateInfo.body}{Environment.NewLine}是否立即更新?", $"{ProductInfo.Title} | 更新提示") == true)
if (WindowService.Current.MainWindow.Dialog($"检测到新版本更新内容:{UpdateInfo.body}{Environment.NewLine}是否立即下载更新?", $"{ProductInfo.Title} | 更新提示") == true)
{
var name = Path.Combine(AppContext.BaseDirectory, UpdateInfo.assets.FirstOrDefault()?.name);
var upFile = new FileInfo(Path.Combine(AppContext.BaseDirectory, UpdateInfo.assets.FirstOrDefault()?.name));
//var name = Path.Combine(AppContext.BaseDirectory, @$"{ProductInfo.Title} {UpdateInfo.version}.zip");
if (File.Exists(name))
if (upFile.Exists)
{
StatusService.Current.Notify("更新文件已经存在,不需要下载");
if (Path.GetExtension(name) == ".zip")
if (upFile.Extension == ".zip")
{
OverwriteUpgrade(name);
OverwriteUpgrade(upFile.FullName);
}
return;
}
Expand All @@ -116,7 +115,7 @@ public async void DownloadUpdate()
{
long totalBytes = s.Result.ContentLength;
using Stream responseStream = s.Result.GetResponseStream();
using FileStream fileStream = new FileStream(name, FileMode.Create, FileAccess.Write);
using FileStream fileStream = new FileStream(upFile.FullName, FileMode.Create, FileAccess.Write);
long totalDownloadBytes = 0;
byte[] bs = new byte[1024];
int size = responseStream.Read(bs, 0, bs.Length);
Expand All @@ -131,9 +130,9 @@ public async void DownloadUpdate()
fileStream.Flush();
fileStream.Close();
StatusService.Current.Set(Resources.Ready);
if (Path.GetExtension(name) == ".zip")
if (upFile.Extension == ".zip")
{
OverwriteUpgrade(name);
OverwriteUpgrade(upFile.FullName);
}
StatusService.Current.Notify($"{ProductInfo.Title} {UpdateInfo.version}版本已经下载到程序根目录下,请手动替换更新");
});
Expand All @@ -142,6 +141,13 @@ public async void DownloadUpdate()

private void OverwriteUpgrade(string zipFile)
{
if (new FileInfo(zipFile).Length != UpdateInfo.assets.First().size)
{
File.Delete(zipFile);
StatusService.Current.Notify("效验更新文件失败,重新下载更新...");
DownloadUpdate();
return;
}
App.Current.Dispatcher.Invoke(() =>
{
if (WindowService.Current.MainWindow.Dialog($"新版本下载完成,是否自动重启替换到新版本?", $"{ProductInfo.Title} | 更新提示") == true)
Expand All @@ -155,7 +161,7 @@ private void OverwriteUpgrade(string zipFile)
string.Format(SteamTool.Core.Properties.Resources.ProgramUpdateCmd,
ProductInfo.Title + ".exe", out_dir, AppContext.BaseDirectory, App.Instance.ProgramName), Encoding.Default);
Process p = new Process();
p.StartInfo.FileName = batpath;
p.StartInfo.FileName = batpath;
p.StartInfo.UseShellExecute = false;
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
//管理员权限运行
Expand Down
14 changes: 7 additions & 7 deletions source/SteamTools/ViewModels/Base/MainWindowViewModelBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ public bool Dialog(string content, string title)

protected override void CloseCanceledCallbackCore()
{
//var dialog = new DialogViewModel { Title = "确认关闭", };
var dialog = new DialogWindowViewModel { Title = "确认关闭", };

//this.Dialog(dialog, typeof(ExitDialog));
this.Dialog(dialog, typeof(MessageDialog));

//if (dialog.DialogResult)
//{
// this.CanClose = true;
// this.InvokeOnUIDispatcher(this.Close);
//}
if (dialog.DialogResult)
{
this.CanClose = true;
this.InvokeOnUIDispatcher(this.Close);
}
}

protected void RaiseCanCloseChanged()
Expand Down
Loading

0 comments on commit 79871c3

Please sign in to comment.