-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUpdaterForm.cs
161 lines (143 loc) · 5.3 KB
/
UpdaterForm.cs
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using Octokit;
namespace LunaUpdater
{
public partial class UpdaterForm : Form
{
// Sets the window to be foreground
[DllImport("User32")]
private static extern int SetForegroundWindow(IntPtr hwnd);
// Activate or minimize a window
[DllImport("User32.DLL")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int SW_RESTORE = 9;
readonly Release release_;
readonly Updater updater_;
string tempFilePath_;
public UpdaterForm(Updater updater, Release release, string curVersion)
{
InitializeComponent();
release_ = release;
updater_ = updater;
if (curVersion == null)
{
labelUpdate.Text = "New version available: " + release_.TagName + "\nDo you want to update?";
}
else
{
labelUpdate.Text = "New version available: " + release_.TagName + ". Current version is " + curVersion + "\nDo you want to update?";
}
changelogLabel.Text = release_.Body;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
if (tempFilePath_ != null)
{
File.Delete(tempFilePath_);
}
}
private void buttonIgnore_Click(object sender, EventArgs e)
{
Close();
}
static void BringSelfToForeGround()
{
Process[] procList = Process.GetProcessesByName("LunaU");
if (procList.Length == 0)
{
return;
}
ShowWindow(procList[0].MainWindowHandle, SW_RESTORE);
SetForegroundWindow(procList[0].MainWindowHandle);
}
private void onProgress(object sender, float progress)
{
labelUpdate.Text = $"Downloading '{release_.TagName}'... {progress / 100:P0}";
}
private async void buttonUpdate_Click(object sender, EventArgs e)
{
if (tempFilePath_ == null)
{
buttonUpdate.Enabled = false;
buttonIgnore.Enabled = false;
labelUpdate.Text = $"Downloading an update '{release_.TagName}'...";
tempFilePath_ = await updater_.DownloadLatestRelease(release_, onProgress);
labelUpdate.Text = $"Downloaded '{release_.TagName}' successfully!\nDo you want to install?";
buttonUpdate.Text = "Install";
BringSelfToForeGround();
labelUpdate.Text = $"Installing '{release_.TagName}'...";
await Task.Run(() => {
KillAllProject64s();
ExtractFiles();
});
MessageBox.Show("The update has been installed successfully!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
Close();
Process.Start("Project64.exe");
}
}
private void KillAllProject64s()
{
Process[] emuProcesses = Process.GetProcessesByName("Project64");
if(emuProcesses.Length > 0)
{
foreach(Process emuProcess in emuProcesses)
{
try
{
emuProcess.Kill();
}
catch (Exception)
{
}
}
}
}
/*
* TODO:
* figure out a way to dynamicly get the name of the root folder
* Exeception handling
*/
private void ExtractFiles()
{
using (var strm = File.OpenRead(tempFilePath_))
using (ZipArchive archive = new ZipArchive(strm))
{
foreach (ZipArchiveEntry file in archive.Entries)
{
string completeFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, file.FullName);
string directory = Path.GetDirectoryName(completeFileName);
if (!Directory.Exists(directory))
Directory.CreateDirectory(directory);
if (file.Name != "")
{
try
{
if (file.Name == "LunaU.exe")
{
File.Delete(completeFileName + ".tmp");
File.Move(completeFileName, completeFileName + ".tmp");
file.ExtractToFile(completeFileName, true);
}
else
{
file.ExtractToFile(completeFileName, true);
}
}
catch (Exception)
{
}
}
}
}
File.Delete(tempFilePath_);
}
}
}