SaveTool: remove the dependency on json.hpp.

This commit is contained in:
Guillaume Jacquemin 2022-03-30 15:15:54 +02:00
parent ef05c075ba
commit 2c2e5ad936
5 changed files with 37 additions and 68 deletions

View file

@ -92,7 +92,4 @@ 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. 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) add_subdirectory(third-party/cpr EXCLUDE_FROM_ALL)
set(JSON_BuildTests OFF CACHE BOOL "" FORCE)
add_subdirectory(third-party/json)
add_subdirectory(src) add_subdirectory(src)

View file

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

View file

@ -35,8 +35,6 @@
#include <cpr/cpr.h> #include <cpr/cpr.h>
#include <nlohmann/json.hpp>
#include <windef.h> #include <windef.h>
#include <winuser.h> #include <winuser.h>
#include <processthreadsapi.h> #include <processthreadsapi.h>
@ -331,10 +329,6 @@ void SaveTool::updateCheckEvent(SDL_Event& event) {
return; return;
} }
using json = nlohmann::json;
json response = json::parse(r.text);
struct Version { struct Version {
explicit Version(Containers::StringView str) { explicit Version(Containers::StringView str) {
std::size_t start_point = 0; std::size_t start_point = 0;
@ -348,17 +342,31 @@ void SaveTool::updateCheckEvent(SDL_Event& event) {
major = std::strtol(components[0].data(), nullptr, 10); major = std::strtol(components[0].data(), nullptr, 10);
minor = std::strtol(components[1].data(), nullptr, 10); minor = std::strtol(components[1].data(), nullptr, 10);
patch = std::strtol(components[2].data(), nullptr, 10); patch = std::strtol(components[2].data(), nullptr, 10);
fullVersion = major * 10000 + minor * 100 + patch;
if(str.hasSuffix("-pre")) {
prerelease = true;
}
} }
Int major; Int fullVersion;
Int minor; Int major = 0;
Int patch; Int minor = 0;
Int patch = 0;
bool prerelease = false;
bool operator==(const Version& other) const { bool operator==(const Version& other) const {
return (major == other.major) && (minor == other.minor) && (patch == other.patch); return fullVersion == other.fullVersion;
} }
bool operator>(const Version& other) const { bool operator>(const Version& other) const {
return ( major * 10000 + minor * 100 + patch) > if((fullVersion > other.fullVersion) ||
(other.major * 10000 + other.minor * 100 + other.patch); (fullVersion == other.fullVersion && prerelease == false && other.prerelease == true))
{
return true;
}
else {
return false;
}
} }
operator Containers::String() const { operator Containers::String() const {
return Utility::format("{}.{}.{}", major, minor, patch); return Utility::format("{}.{}.{}", major, minor, patch);
@ -367,29 +375,24 @@ void SaveTool::updateCheckEvent(SDL_Event& event) {
static const Version current_ver{SAVETOOL_VERSION}; static const Version current_ver{SAVETOOL_VERSION};
for(auto& release : response) { Containers::String response = r.text.c_str();
if(release["prerelease"] == true) { auto components = response.split('\n');
continue;
}
Version latest_ver{release["tag_name"].get<std::string>().c_str()}; Version latest_ver{components.front()};
if(latest_ver > current_ver || (latest_ver == current_ver && Utility::String::endsWith(SAVETOOL_VERSION, "-pre"))) { if(latest_ver > current_ver) {
_queue.addToast(Toast::Type::Warning, "Your version is out of date.\nCheck the settings for more information."_s, _queue.addToast(Toast::Type::Warning, "Your version is out of date.\nCheck the settings for more information."_s,
std::chrono::milliseconds{5000}); std::chrono::milliseconds{5000});
_updateAvailable = true; _updateAvailable = true;
_latestVersion = latest_ver; _latestVersion = latest_ver;
_releaseLink = release["html_url"].get<std::string>().c_str(); _releaseLink = Utility::format("https://williamjcm.ovh/git/williamjcm/MassBuilderSaveTool/releases/tag/v{}", components.front());
_downloadLink = release["assets"][0]["browser_download_url"].get<std::string>().c_str(); _downloadLink = components.back();
} }
else if(latest_ver == current_ver || (current_ver > latest_ver && Utility::String::endsWith(SAVETOOL_VERSION, "-pre"))) { else if(latest_ver == current_ver || (current_ver > latest_ver && current_ver.prerelease == true)) {
_queue.addToast(Toast::Type::Success, "The application is already up to date."_s); _queue.addToast(Toast::Type::Success, "The application is already up to date."_s);
} }
else if(current_ver > latest_ver) { else if(current_ver > latest_ver && current_ver.prerelease == false) {
_queue.addToast(Toast::Type::Warning, "Your version is more recent than the latest one in the repo. How???"_s); _queue.addToast(Toast::Type::Warning, "Your version is more recent than the latest one in the repo. How???"_s);
}
break;
} }
} }
@ -884,12 +887,11 @@ void SaveTool::checkGameState() {
} }
void SaveTool::checkForUpdates() { void SaveTool::checkForUpdates() {
cpr::Response r = cpr::Get(cpr::Url{"https://williamjcm.ovh/git/api/v1/repos/williamjcm/MassBuilderSaveTool/releases"}, cpr::Timeout{10000}); cpr::Response r = cpr::Get(cpr::Url{"https://williamjcm.ovh/mbst/version"}, cpr::Timeout{10000});
SDL_Event event; SDL_Event event;
SDL_zero(event); SDL_zero(event);
event.type = _updateEventId; event.type = _updateEventId;
event.user.code = r.status_code;
event.user.data1 = new cpr::Response{std::move(r)}; event.user.data1 = new cpr::Response{std::move(r)};
SDL_PushEvent(&event); SDL_PushEvent(&event);
} }

View file

@ -270,31 +270,6 @@ void SaveTool::drawAbout() {
ImGui::TreePop(); ImGui::TreePop();
} }
if(ImGui::TreeNodeEx("JSON for Modern C++ (aka json.hpp)", ImGuiTreeNodeFlags_SpanAvailWidth)) {
auto json_website = "https://json.nlohmann.me/";
drawAlignedText(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 auto json_licence = _rs.getRaw("LICENSE.json");
if(ImGui::BeginChild("##jsonLicence", {0.0f, windowSize().y() * 0.3f}, true)) {
ImGui::PushFont(ImGui::GetIO().Fonts->Fonts[1]);
ImGui::TextUnformatted(json_licence);
ImGui::PopFont();
}
ImGui::EndChild();
ImGui::TreePop();
}
if(ImGui::TreeNodeEx("Font Awesome", ImGuiTreeNodeFlags_SpanAvailWidth)) { if(ImGui::TreeNodeEx("Font Awesome", ImGuiTreeNodeFlags_SpanAvailWidth)) {
ImGui::TextUnformatted("Version used: 5.15.3"); ImGui::TextUnformatted("Version used: 5.15.3");
auto fa_website = "https://fontawesome.com/"; auto fa_website = "https://fontawesome.com/";

View file

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