forked from blastrock/pkgj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.cpp
More file actions
125 lines (105 loc) · 3.34 KB
/
Copy pathupdate.cpp
File metadata and controls
125 lines (105 loc) · 3.34 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
#include "dialog.hpp"
#include "file.hpp"
#include "pkgi.hpp"
#include "vitahttp.hpp"
#include <boost/scope_exit.hpp>
#include <vector>
#define PKGJ_UPDATE_URL "https://raw.githubusercontent.com/blastrock/pkgj/last"
#define PKGJ_UPDATE_URL_VERSION PKGJ_UPDATE_URL "/version"
namespace
{
std::string version;
void start_download()
{
try
{
LOGF("Downloading PKGj update v{}", version);
const auto filename = fmt::format(
"{}/pkgj-v{}.vpk", pkgi_get_config_folder(), version);
const auto url =
fmt::format("{}/pkgj-v{}.vpk", PKGJ_UPDATE_URL, version);
pkgi_dialog_message("Downloading update", 0);
try
{
const auto file = pkgi_create(filename.c_str());
BOOST_SCOPE_EXIT_ALL(&)
{
pkgi_close(file);
};
VitaHttp http;
http.start(url, 0);
std::vector<uint8_t> data(64 * 1024);
while (true)
{
const auto read = http.read(data.data(), data.size());
if (read == 0)
break;
pkgi_write(file, data.data(), read);
}
LOGF("PKGj update downloaded successfully");
}
catch (...)
{
LOGF("PKGj update download failed, removing partial file");
pkgi_rm(filename.c_str());
throw;
}
pkgi_dialog_message(
fmt::format(
"The update has been downloaded to {}, install "
"it through VitaShell.",
filename)
.c_str());
}
catch (const std::exception& e)
{
pkgi_dialog_error(fmt::format("Download failed: {}", e.what()).c_str());
}
}
void update_thread()
{
try
{
if (!pkgi_is_module_present("NoNpDrm"))
pkgi_dialog_error(
"NoNpDrm not found. Games cannot be installed or played.");
while (pkgi_dialog_is_open())
{
pkgi_sleep(20);
}
LOGF("Checking for updates at: {}", PKGJ_UPDATE_URL_VERSION);
VitaHttp http;
http.start(PKGJ_UPDATE_URL_VERSION, 0);
std::vector<uint8_t> last_versionb(10);
last_versionb.resize(
http.read(last_versionb.data(), last_versionb.size()));
std::string last_version(last_versionb.begin(), last_versionb.end());
LOGF("Latest available version: {}", last_version);
if (last_version != PKGI_VERSION)
{
LOG("New PKGj version available: %s", last_version.c_str());
version = last_version;
pkgi_dialog_question(
fmt::format(
"New PKGj version {} is available!\nDo you want to "
"download it?",
last_version)
.c_str(),
{{"Yes",
[] {
pkgi_start_thread(
"pkgj_update_download", &start_download);
}},
{"No", [] {}}});
}
}
catch (const std::exception& e)
{
LOGF("Update check failed: {}", e.what());
}
}
}
void start_update_thread()
{
pkgi_start_thread("pkgj_update", &update_thread);
}