diff options
Diffstat (limited to 'src/updater.cpp')
-rw-r--r-- | src/updater.cpp | 309 |
1 files changed, 309 insertions, 0 deletions
diff --git a/src/updater.cpp b/src/updater.cpp new file mode 100644 index 0000000..2b05daf --- /dev/null +++ b/src/updater.cpp | |||
@@ -0,0 +1,309 @@ | |||
1 | #include "updater.h" | ||
2 | |||
3 | #include <fmt/core.h> | ||
4 | #include <openssl/evp.h> | ||
5 | #include <openssl/sha.h> | ||
6 | #include <wx/evtloop.h> | ||
7 | #include <wx/progdlg.h> | ||
8 | #include <wx/webrequest.h> | ||
9 | #include <wx/wfstream.h> | ||
10 | #include <wx/zipstrm.h> | ||
11 | #include <yaml-cpp/yaml.h> | ||
12 | |||
13 | #include <cstdio> | ||
14 | #include <deque> | ||
15 | #include <filesystem> | ||
16 | #include <fstream> | ||
17 | |||
18 | #include "global.h" | ||
19 | #include "logger.h" | ||
20 | #include "version.h" | ||
21 | |||
22 | constexpr const char* kVersionFileUrl = | ||
23 | "https://code.fourisland.com/lingo-ap-tracker/plain/VERSION.yaml"; | ||
24 | constexpr const char* kChangelogUrl = | ||
25 | "https://code.fourisland.com/lingo-ap-tracker/about/CHANGELOG.md"; | ||
26 | |||
27 | namespace { | ||
28 | |||
29 | std::string CalculateStringSha256(const wxString& data) { | ||
30 | unsigned char hash[SHA256_DIGEST_LENGTH]; | ||
31 | EVP_MD_CTX* sha256 = EVP_MD_CTX_new(); | ||
32 | EVP_DigestInit(sha256, EVP_sha256()); | ||
33 | EVP_DigestUpdate(sha256, data.c_str(), data.length()); | ||
34 | EVP_DigestFinal_ex(sha256, hash, nullptr); | ||
35 | EVP_MD_CTX_free(sha256); | ||
36 | |||
37 | char output[65] = {0}; | ||
38 | for (int i = 0; i < SHA256_DIGEST_LENGTH; i++) { | ||
39 | snprintf(output + (i * 2), 3, "%02x", hash[i]); | ||
40 | } | ||
41 | |||
42 | return std::string(output); | ||
43 | } | ||
44 | |||
45 | } // namespace | ||
46 | |||
47 | Updater::Updater(wxFrame* parent) : parent_(parent) { | ||
48 | Bind(wxEVT_WEBREQUEST_STATE, &Updater::OnWebRequestState, this); | ||
49 | } | ||
50 | |||
51 | void Updater::Cleanup() { | ||
52 | std::filesystem::path oldDir = GetExecutableDirectory() / "old"; | ||
53 | if (std::filesystem::is_directory(oldDir)) { | ||
54 | std::filesystem::remove_all(oldDir); | ||
55 | } | ||
56 | } | ||
57 | |||
58 | void Updater::CheckForUpdates(bool invisible) { | ||
59 | wxWebRequest versionRequest = | ||
60 | wxWebSession::GetDefault().CreateRequest(this, kVersionFileUrl); | ||
61 | |||
62 | if (invisible) { | ||
63 | update_state_ = UpdateState::GetVersionInvisible; | ||
64 | |||
65 | versionRequest.Start(); | ||
66 | } else { | ||
67 | update_state_ = UpdateState::GetVersionManual; | ||
68 | |||
69 | if (DownloadWithProgress(versionRequest)) { | ||
70 | if (versionRequest.GetState() == wxWebRequest::State_Failed) { | ||
71 | wxMessageBox("Could not check for updates.", "Error", | ||
72 | wxOK | wxICON_ERROR); | ||
73 | } else if (versionRequest.GetState() == wxWebRequest::State_Completed) { | ||
74 | ProcessVersionFile( | ||
75 | versionRequest.GetResponse().AsString().utf8_string()); | ||
76 | } | ||
77 | } | ||
78 | } | ||
79 | } | ||
80 | |||
81 | void Updater::OnWebRequestState(wxWebRequestEvent& evt) { | ||
82 | if (update_state_ == UpdateState::GetVersionInvisible) { | ||
83 | if (evt.GetState() == wxWebRequest::State_Completed) { | ||
84 | ProcessVersionFile(evt.GetResponse().AsString().utf8_string()); | ||
85 | } else if (evt.GetState() == wxWebRequest::State_Failed) { | ||
86 | parent_->SetStatusText("Could not check for updates."); | ||
87 | } | ||
88 | } | ||
89 | } | ||
90 | |||
91 | void Updater::ProcessVersionFile(std::string data) { | ||
92 | try { | ||
93 | YAML::Node versionInfo = YAML::Load(data); | ||
94 | Version latestVersion(versionInfo["version"].as<std::string>()); | ||
95 | |||
96 | if (kTrackerVersion < latestVersion) { | ||
97 | if (versionInfo["packages"]) { | ||
98 | std::string platformIdentifier; | ||
99 | |||
100 | if (wxPlatformInfo::Get().GetOperatingSystemId() == wxOS_WINDOWS_NT) { | ||
101 | platformIdentifier = "win64"; | ||
102 | } | ||
103 | |||
104 | if (!platformIdentifier.empty() && | ||
105 | versionInfo["packages"][platformIdentifier]) { | ||
106 | wxMessageDialog dialog( | ||
107 | nullptr, | ||
108 | fmt::format("There is a newer version of Lingo AP Tracker " | ||
109 | "available. You have {}, and the latest version is " | ||
110 | "{}. Would you like to update?", | ||
111 | kTrackerVersion.ToString(), latestVersion.ToString()), | ||
112 | "Update available", wxYES_NO | wxCANCEL); | ||
113 | dialog.SetYesNoLabels("Install update", "Open changelog"); | ||
114 | |||
115 | int dlgResult = dialog.ShowModal(); | ||
116 | if (dlgResult == wxID_YES) { | ||
117 | const YAML::Node& packageInfo = | ||
118 | versionInfo["packages"][platformIdentifier]; | ||
119 | std::string packageUrl = packageInfo["url"].as<std::string>(); | ||
120 | std::string packageChecksum = | ||
121 | packageInfo["checksum"].as<std::string>(); | ||
122 | |||
123 | std::vector<std::filesystem::path> packageFiles; | ||
124 | if (packageInfo["files"]) { | ||
125 | for (const YAML::Node& filename : packageInfo["files"]) { | ||
126 | packageFiles.push_back(filename.as<std::string>()); | ||
127 | } | ||
128 | } | ||
129 | |||
130 | std::vector<std::filesystem::path> deletedFiles; | ||
131 | if (packageInfo["deleted_files"]) { | ||
132 | for (const YAML::Node& filename : packageInfo["deleted_files"]) { | ||
133 | deletedFiles.push_back(filename.as<std::string>()); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | InstallUpdate(packageUrl, packageChecksum, packageFiles, | ||
138 | deletedFiles); | ||
139 | } else if (dlgResult == wxID_NO) { | ||
140 | wxLaunchDefaultBrowser(kChangelogUrl); | ||
141 | } | ||
142 | |||
143 | return; | ||
144 | } | ||
145 | } | ||
146 | |||
147 | if (wxMessageBox( | ||
148 | fmt::format("There is a newer version of Lingo AP Tracker " | ||
149 | "available. You have {}, and the latest version is " | ||
150 | "{}. Would you like to update?", | ||
151 | kTrackerVersion.ToString(), latestVersion.ToString()), | ||
152 | "Update available", wxYES_NO) == wxYES) { | ||
153 | wxLaunchDefaultBrowser(kChangelogUrl); | ||
154 | } | ||
155 | } else if (update_state_ == UpdateState::GetVersionManual) { | ||
156 | wxMessageBox("Lingo AP Tracker is up to date!", "Lingo AP Tracker", wxOK); | ||
157 | } | ||
158 | } catch (const std::exception& ex) { | ||
159 | wxMessageBox("Could not check for updates.", "Error", wxOK | wxICON_ERROR); | ||
160 | } | ||
161 | } | ||
162 | |||
163 | void Updater::InstallUpdate(std::string url, std::string checksum, | ||
164 | std::vector<std::filesystem::path> files, | ||
165 | std::vector<std::filesystem::path> deletedFiles) { | ||
166 | update_state_ = UpdateState::GetPackage; | ||
167 | |||
168 | wxWebRequest packageRequest = | ||
169 | wxWebSession::GetDefault().CreateRequest(this, url); | ||
170 | |||
171 | if (!DownloadWithProgress(packageRequest)) { | ||
172 | return; | ||
173 | } | ||
174 | |||
175 | bool download_issue = false; | ||
176 | |||
177 | wxFileName package_path; | ||
178 | package_path.AssignTempFileName(""); | ||
179 | |||
180 | if (!package_path.IsOk()) { | ||
181 | download_issue = true; | ||
182 | } else { | ||
183 | wxFileOutputStream writeOut(package_path.GetFullPath()); | ||
184 | wxString fileData = packageRequest.GetResponse().AsString(); | ||
185 | writeOut.WriteAll(fileData.c_str(), fileData.length()); | ||
186 | |||
187 | std::string downloadedChecksum = CalculateStringSha256(fileData); | ||
188 | if (downloadedChecksum != checksum) { | ||
189 | download_issue = true; | ||
190 | } | ||
191 | } | ||
192 | |||
193 | if (download_issue) { | ||
194 | if (wxMessageBox("There was an issue downloading the update. Would you " | ||
195 | "like to manually download it instead?", | ||
196 | "Error", wxYES_NO | wxICON_ERROR) == wxID_YES) { | ||
197 | wxLaunchDefaultBrowser(kChangelogUrl); | ||
198 | } | ||
199 | return; | ||
200 | } | ||
201 | |||
202 | std::filesystem::path newArea = GetExecutableDirectory(); | ||
203 | std::filesystem::path oldArea = newArea / "old"; | ||
204 | std::set<std::filesystem::path> folders; | ||
205 | std::set<std::filesystem::path> filesToMove; | ||
206 | for (const std::filesystem::path& existingFile : files) { | ||
207 | std::filesystem::path movedPath = oldArea / existingFile; | ||
208 | std::filesystem::path movedDir = movedPath; | ||
209 | movedDir.remove_filename(); | ||
210 | folders.insert(movedDir); | ||
211 | filesToMove.insert(existingFile); | ||
212 | } | ||
213 | for (const std::filesystem::path& existingFile : deletedFiles) { | ||
214 | std::filesystem::path movedPath = oldArea / existingFile; | ||
215 | std::filesystem::path movedDir = movedPath; | ||
216 | movedDir.remove_filename(); | ||
217 | folders.insert(movedDir); | ||
218 | } | ||
219 | |||
220 | for (const std::filesystem::path& newFolder : folders) { | ||
221 | TrackerLog(fmt::format("Creating directory {}", newFolder.string())); | ||
222 | |||
223 | std::filesystem::create_directories(newFolder); | ||
224 | } | ||
225 | |||
226 | for (const std::filesystem::path& existingFile : files) { | ||
227 | std::filesystem::path existingPath = newArea / existingFile; | ||
228 | |||
229 | if (std::filesystem::is_regular_file(existingPath)) { | ||
230 | std::filesystem::path movedPath = oldArea / existingFile; | ||
231 | |||
232 | TrackerLog(fmt::format("Moving {} -> {}", existingPath.string(), | ||
233 | movedPath.string())); | ||
234 | |||
235 | std::filesystem::rename(existingPath, movedPath); | ||
236 | } | ||
237 | } | ||
238 | for (const std::filesystem::path& existingFile : deletedFiles) { | ||
239 | std::filesystem::path existingPath = newArea / existingFile; | ||
240 | |||
241 | if (std::filesystem::is_regular_file(existingPath)) { | ||
242 | std::filesystem::path movedPath = oldArea / existingFile; | ||
243 | |||
244 | TrackerLog(fmt::format("Moving {} -> {}", existingPath.string(), | ||
245 | movedPath.string())); | ||
246 | |||
247 | std::filesystem::rename(existingPath, movedPath); | ||
248 | } | ||
249 | } | ||
250 | |||
251 | wxFileInputStream fileInputStream(package_path.GetFullPath()); | ||
252 | wxZipInputStream zipStream(fileInputStream); | ||
253 | std::unique_ptr<wxZipEntry> zipEntry; | ||
254 | while ((zipEntry = std::unique_ptr<wxZipEntry>(zipStream.GetNextEntry())) != | ||
255 | nullptr) { | ||
256 | if (zipEntry->IsDir()) { | ||
257 | continue; | ||
258 | } | ||
259 | |||
260 | std::filesystem::path archivePath = zipEntry->GetName().utf8_string(); | ||
261 | |||
262 | TrackerLog(fmt::format("Found {} in archive", archivePath.string())); | ||
263 | |||
264 | // Cut off the root folder name | ||
265 | std::filesystem::path subPath; | ||
266 | for (auto it = std::next(archivePath.begin()); it != archivePath.end(); | ||
267 | it++) { | ||
268 | subPath /= *it; | ||
269 | } | ||
270 | |||
271 | std::filesystem::path pastePath = newArea / subPath; | ||
272 | |||
273 | wxFileOutputStream fileOutput(pastePath.string()); | ||
274 | zipStream.Read(fileOutput); | ||
275 | } | ||
276 | |||
277 | if (wxMessageBox( | ||
278 | "Update installed! The tracker must be restarted for the changes to take " | ||
279 | "effect. Do you want to close the tracker?", | ||
280 | "Update installed", wxYES_NO) == wxYES) { | ||
281 | wxExit(); | ||
282 | } | ||
283 | } | ||
284 | |||
285 | bool Updater::DownloadWithProgress(wxWebRequest& request) { | ||
286 | request.Start(); | ||
287 | |||
288 | wxProgressDialog dialog("Checking for updates...", "Checking for updates...", | ||
289 | 100, nullptr, | ||
290 | wxPD_APP_MODAL | wxPD_AUTO_HIDE | wxPD_CAN_ABORT | | ||
291 | wxPD_ELAPSED_TIME | wxPD_REMAINING_TIME); | ||
292 | while (request.GetState() != wxWebRequest::State_Completed && | ||
293 | request.GetState() != wxWebRequest::State_Failed) { | ||
294 | if (request.GetBytesExpectedToReceive() == -1) { | ||
295 | if (!dialog.Pulse()) { | ||
296 | request.Cancel(); | ||
297 | return false; | ||
298 | } | ||
299 | } else { | ||
300 | dialog.SetRange(request.GetBytesExpectedToReceive()); | ||
301 | if (!dialog.Update(request.GetBytesReceived())) { | ||
302 | request.Cancel(); | ||
303 | return false; | ||
304 | } | ||
305 | } | ||
306 | } | ||
307 | |||
308 | return true; | ||
309 | } | ||