Compare commits

...

8 commits

Author SHA1 Message Date
d0716d6242 SaveTool: add credits for cpr and json.hpp. 2021-07-28 14:27:44 +02:00
512fa4088b SaveTool: remove testing code. 2021-07-28 14:27:19 +02:00
ab124174b0 SaveTool: add rest of response handling. 2021-07-28 14:18:39 +02:00
51127241ef Add json.hpp as the JSON lib. 2021-07-28 14:17:47 +02:00
ee384843e9 SaveTool: launch the thread, and add basic response handling. 2021-07-28 14:17:21 +02:00
7f32166ab0 SaveTool: implement the request thread. 2021-07-28 14:15:39 +02:00
017900afe2 SaveTool: add ToastQueue.
Might be useful for warnings and non-fatal errors, to replace standard
SDL message boxes.
2021-07-28 14:14:12 +02:00
52b60ff2a5 ToastQueue: fix formatting a bit. 2021-07-28 14:12:43 +02:00
10 changed files with 168 additions and 4 deletions

4
.gitmodules vendored
View file

@ -30,3 +30,7 @@
path = third-party/cpr
url = https://github.com/whoshuu/cpr
branch = master
[submodule "json.hpp"]
path = third-party/json
url = https://github.com/nlohmann/json
branch = master

View file

@ -92,4 +92,7 @@ set(CPR_BUILD_TESTS OFF CACHE BOOL "" FORCE)
set(CMAKE_USE_LIBSSH2 OFF CACHE BOOL "" FORCE) # For some reason, even when HTTP_ONLY is set to ON, libcurl will try to link to libssh2.
add_subdirectory(third-party/cpr EXCLUDE_FROM_ALL)
set(JSON_BuildTests OFF CACHE BOOL "" FORCE)
add_subdirectory(third-party/json)
add_subdirectory(src)

View file

@ -78,5 +78,6 @@ target_link_libraries(MassBuilderSaveTool PRIVATE
efsw
zip
cpr::cpr
nlohmann_json::nlohmann_json
imm32
wtsapi32)

View file

@ -32,6 +32,8 @@
#include <cpr/cpr.h>
#include <nlohmann/json.hpp>
#include <windef.h>
#include <winuser.h>
#include <processthreadsapi.h>
@ -133,6 +135,11 @@ SaveTool::SaveTool(const Arguments& arguments):
}
initialiseConfiguration();
if(_checkUpdatesOnStartup) {
_thread = std::thread{[this]{ checkForUpdates(); }};
_queue.addToast(Toast::Type::Default, "Checking for updates...");
}
}
SaveTool::~SaveTool() {
@ -286,7 +293,67 @@ void SaveTool::initEvent(SDL_Event& event) {
void SaveTool::updateCheckEvent(SDL_Event& event) {
_thread.join();
// TODO: implement
cpr::Response r{std::move(*static_cast<cpr::Response*>(event.user.data1))};
delete static_cast<cpr::Response*>(event.user.data1);
if(r.elapsed > 10.0) {
_queue.addToast(Toast::Type::Error, "The request timed out.");
return;
}
if(r.status_code != 200) {
_queue.addToast(Toast::Type::Error, Utility::formatString("The request failed with error code {}: {}", r.status_code, r.reason));
return;
}
using json = nlohmann::json;
json response = json::parse(r.text);
struct Version {
explicit Version(const std::string& str) {
std::size_t start_point = 0;
if(str[0] == 'v') {
start_point++;
}
major = std::atoi(str.c_str() + start_point);
start_point = str.find('.', start_point) + 1;
minor = std::atoi(str.c_str() + start_point);
start_point = str.find('.', start_point) + 1;
patch = std::atoi(str.c_str() + start_point);
}
Int major;
Int minor;
Int patch;
bool operator==(const Version& other) const {
return (major == other.major) && (minor == other.minor) && (patch == other.patch);
}
bool operator>(const Version& other) const {
return ( major * 10000 + minor * 100 + patch) >
(other.major * 10000 + other.minor * 100 + other.patch);
}
};
static const Version current_ver{SAVETOOL_VERSION};
Version latest_ver{response[0]["tag_name"]};
if(latest_ver == current_ver) {
_queue.addToast(Toast::Type::Success, "The application is already up to date.");
}
else if(latest_ver > current_ver) {
_queue.addToast(Toast::Type::Warning, "Your version is out of date.\nCheck the settings for more information.",
std::chrono::milliseconds{5000});
_updateAvailable = true;
_latestVersion = Utility::formatString("{}.{}.{}", latest_ver.major, latest_ver.minor, latest_ver.patch);
_releaseLink = response[0]["html_url"];
_downloadLink = response[0]["assets"][0]["browser_download_url"];
}
else if(current_ver > latest_ver) {
_queue.addToast(Toast::Type::Warning, "Your version is more recent than the latest one in the repo. How???");
}
}
void SaveTool::initialiseConfiguration() {
@ -594,10 +661,13 @@ void SaveTool::checkGameState() {
}
void SaveTool::checkForUpdates() {
// TODO: implement
cpr::Response r = cpr::Get(cpr::Url{"https://williamjcm.ovh/git/api/v1/repos/williamjcm/MassBuilderSaveTool/releases"},
cpr::Parameters{{"limit", "1"}}, cpr::Timeout{10000});
SDL_Event event;
SDL_zero(event);
event.type = _updateEventId;
event.user.code = r.status_code;
event.user.data1 = new cpr::Response{std::move(r)};
SDL_PushEvent(&event);
}

View file

@ -34,6 +34,7 @@
#include "../ProfileManager/ProfileManager.h"
#include "../MassManager/MassManager.h"
#include "../ToastQueue/ToastQueue.h"
using namespace Corrade;
using namespace Magnum;
@ -161,6 +162,8 @@ class SaveTool: public Platform::Sdl2Application, public efsw::FileWatchListener
bool _metricsWindow{false};
#endif
ToastQueue _queue;
std::thread _thread; // used for threaded operations such as profile manager init or update checking.
UnsignedInt _initEventId;
@ -196,4 +199,9 @@ class SaveTool: public Platform::Sdl2Application, public efsw::FileWatchListener
bool _checkUpdatesOnStartup{true};
bool _unsafeMode{false};
bool _updateAvailable{false};
std::string _latestVersion;
std::string _releaseLink;
std::string _downloadLink;
};

View file

@ -251,6 +251,58 @@ void SaveTool::drawAbout() {
ImGui::TreePop();
}
if(ImGui::TreeNodeEx("C++ Requests (cpr)", ImGuiTreeNodeFlags_SpanAvailWidth)) {
ImGui::AlignTextToFramePadding();
const char* cpr_website = "https://whoshuu.github.io/cpr/";
ImGui::Text(ICON_FA_GLOBE " %s", cpr_website);
ImGui::SameLine();
if(ImGui::Button("Copy to clipboard")) {
ImGui::SetClipboardText(cpr_website);
}
ImGui::SameLine();
if(ImGui::Button("Open in browser")) {
openUri(cpr_website);
}
ImGui::TextUnformatted("Licence: MIT");
static const auto cpr_licence = _rs.get("LICENSE.cpr");
if(ImGui::BeginChild("##cprLicence", {0.0f, windowSize().y() * 0.3f}, true)) {
ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[1]);
ImGui::TextUnformatted(cpr_licence.c_str());
ImGui::PopFont();
}
ImGui::EndChild();
ImGui::TreePop();
}
if(ImGui::TreeNodeEx("JSON for Modern C++ (aka json.hpp)", ImGuiTreeNodeFlags_SpanAvailWidth)) {
ImGui::AlignTextToFramePadding();
const char* json_website = "https://json.nlohmann.me/";
ImGui::Text(ICON_FA_GLOBE " %s", json_website);
ImGui::SameLine();
if(ImGui::Button("Copy to clipboard")) {
ImGui::SetClipboardText(json_website);
}
ImGui::SameLine();
if(ImGui::Button("Open in browser")) {
openUri(json_website);
}
ImGui::TextUnformatted("Licence: MIT");
static const auto json_licence = _rs.get("LICENSE.json");
if(ImGui::BeginChild("##jsonLicence", {0.0f, windowSize().y() * 0.3f}, true)) {
ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[1]);
ImGui::TextUnformatted(json_licence.c_str());
ImGui::PopFont();
}
ImGui::EndChild();
ImGui::TreePop();
}
if(ImGui::TreeNodeEx("Font Awesome", ImGuiTreeNodeFlags_SpanAvailWidth)) {
ImGui::TextUnformatted("Version used: 5.15.3");
ImGui::AlignTextToFramePadding();

View file

@ -62,7 +62,22 @@ void SaveTool::drawMainMenu() {
ImGui::Checkbox("Check for updates on startup", &_checkUpdatesOnStartup);
ImGui::SameLine();
ImGui::Button(ICON_FA_SYNC_ALT " Check now");
if(ImGui::Button(ICON_FA_SYNC_ALT " Check now")) {
_queue.addToast(Toast::Type::Default, "Checking for updates...");
_thread = std::thread{[this]{ checkForUpdates(); }};
}
if(_updateAvailable) {
ImGui::AlignTextToFramePadding();
ImGui::Text("Version %s is available.", _latestVersion.c_str());
if(ImGui::Button(ICON_FA_FILE_SIGNATURE " Release notes")) {
openUri(_releaseLink);
}
ImGui::SameLine();
if(ImGui::Button(ICON_FA_DOWNLOAD " Download now")) {
openUri(_downloadLink);
}
}
ImGui::EndMenu();
}

View file

@ -138,7 +138,9 @@ void ToastQueue::draw(Vector2i viewport_size) {
break;
}
if(current->type() != Toast::Type::Default) {
ImGui::SameLine();
}
if(current->message().length() > 127) {
ImGui::TextColored(colour, "%.*s...", 127, current->message().c_str());

View file

@ -43,3 +43,11 @@ alias=LICENSE.libzip
[file]
filename=../third-party/efsw/LICENSE
alias=LICENSE.efsw
[file]
filename=../third-party/cpr/LICENSE
alias=LICENSE.cpr
[file]
filename=../third-party/json/LICENSE.MIT
alias=LICENSE.json

1
third-party/json vendored Submodule

@ -0,0 +1 @@
Subproject commit 350ff4f7ced7c4117eae2fb93df02823c8021fcb