-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdateWindow.xaml.cs
More file actions
131 lines (113 loc) · 4.82 KB
/
UpdateWindow.xaml.cs
File metadata and controls
131 lines (113 loc) · 4.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright (C) 2026 Raytolfas
// This file is part of Raytolfas Launcher.
//
// Raytolfas Launcher is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Raytolfas Launcher is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Raytolfas Launcher. If not, see <https://www.gnu.org/licenses/>.
using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;
using System.Windows;
using System.Windows.Input;
namespace RaytolfasLauncher
{
public partial class UpdateWindow : Window
{
private readonly string downloadUrl;
private readonly string tempFile;
private readonly string language;
public UpdateWindow(string currentVer, string newVer, string changelog, string url, string language)
{
InitializeComponent();
this.language = LocalizationManager.NormalizeLanguage(language);
CurrentVerText.Text = currentVer;
NewVerText.Text = newVer;
ChangelogText.Text = changelog;
downloadUrl = url;
tempFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "update_temp.exe");
ApplyLocalization();
}
private string T(string key) => LocalizationManager.Get(key, language);
private string T(string key, params object[] args) => string.Format(T(key), args);
private void ApplyLocalization()
{
Title = T("update.window_title");
UpdateTitleText.Text = T("update.available");
CurrentVersionLabel.Text = T("update.current");
NewVersionLabel.Text = T("update.new");
DownloadBtn.Content = T("update.download");
CancelBtn.Content = T("update.cancel");
RestartBtn.Content = T("update.restart");
}
private void CloseUpdate_Click(object sender, RoutedEventArgs e) => Close();
private void CancelBtn_Click(object sender, RoutedEventArgs e) => Close();
private async void DownloadBtn_Click(object sender, RoutedEventArgs e)
{
DownloadBtn.Visibility = Visibility.Collapsed;
UpdateProgress.Visibility = Visibility.Visible;
CloseButton.IsEnabled = false;
try
{
using var client = new HttpClient();
var response = await client.GetAsync(downloadUrl);
response.EnsureSuccessStatusCode();
using (var fs = new FileStream(tempFile, FileMode.Create))
{
await response.Content.CopyToAsync(fs);
}
UpdateProgress.Value = 100;
RestartBtn.Visibility = Visibility.Visible;
System.Windows.MessageBox.Show(T("update.downloaded"), T("update.success"), MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
System.Windows.MessageBox.Show(T("update.error", ex.Message));
DownloadBtn.Visibility = Visibility.Visible;
UpdateProgress.Visibility = Visibility.Collapsed;
CloseButton.IsEnabled = true;
}
}
private void RestartBtn_Click(object sender, RoutedEventArgs e)
{
string? currentExe = Process.GetCurrentProcess().MainModule?.FileName;
if (string.IsNullOrEmpty(currentExe))
return;
string finalExeName = "RaytolfasLauncher.exe";
string finalExePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, finalExeName);
string batchScript = $@"
@echo off
timeout /t 2 /nobreak > nul
:loop
del /f /q ""{currentExe}""
if exist ""{currentExe}"" goto loop
move /y ""{tempFile}"" ""{finalExePath}""
start """" ""{finalExePath}""
del ""%~f0""";
string batchPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "update.bat");
File.WriteAllText(batchPath, batchScript, System.Text.Encoding.Default);
Process.Start(new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c \"{batchPath}\"",
CreateNoWindow = true,
UseShellExecute = false
});
System.Windows.Application.Current.Shutdown();
}
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
DragMove();
}
}
}