Update meta
This commit is contained in:
parent
99450ca12a
commit
ebf11a94ab
|
@ -3,6 +3,7 @@ IndentWidth: 4
|
||||||
TabWidth: 4
|
TabWidth: 4
|
||||||
UseTab: Always
|
UseTab: Always
|
||||||
ContinuationIndentWidth: 4
|
ContinuationIndentWidth: 4
|
||||||
|
ColumnLimit: 120
|
||||||
---
|
---
|
||||||
Language: Cpp
|
Language: Cpp
|
||||||
AccessModifierOffset: -4
|
AccessModifierOffset: -4
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
cmake_minimum_required(VERSION 3.25)
|
cmake_minimum_required(VERSION 3.25)
|
||||||
project(leapfrog)
|
project(leapfrog)
|
||||||
|
|
||||||
|
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||||
|
set(CMAKE_CXX_STANDARD 17)
|
||||||
|
|
||||||
add_subdirectory(lib)
|
add_subdirectory(lib)
|
||||||
add_subdirectory(installer)
|
add_subdirectory(installer)
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
|
||||||
|
|
||||||
configure_file(info.hpp.in info.hpp)
|
configure_file(info.hpp.in info.hpp)
|
||||||
add_executable(installer main.cpp ui.cpp ui.hpp meta.cpp meta.hpp info.hpp)
|
add_executable(installer main.cpp ui.cpp ui.hpp meta.cpp meta.hpp info.hpp)
|
||||||
target_link_libraries(installer PRIVATE wx::base wx::core wx::net)
|
target_link_libraries(installer PRIVATE wx::base wx::core wx::net)
|
||||||
|
|
|
@ -11,7 +11,25 @@ public:
|
||||||
|
|
||||||
wxLog::SetActiveTarget(new wxLogStream(&std::cout));
|
wxLog::SetActiveTarget(new wxLogStream(&std::cout));
|
||||||
|
|
||||||
get_loader_versions(this, [](auto) {});
|
get_loader_version(this, "placeholder", [](std::optional<LoaderVersion> value) {
|
||||||
|
if (!value.has_value())
|
||||||
|
return;
|
||||||
|
|
||||||
|
wxString buf('\n');
|
||||||
|
|
||||||
|
buf += "Version: " + value->version + '\n';
|
||||||
|
buf += "Release Date: " + value->release_date.Format() + '\n';
|
||||||
|
buf += "Libraries:\n";
|
||||||
|
|
||||||
|
for (const auto &library : value->libraries) {
|
||||||
|
buf += "- Name: " + library.name + '\n';
|
||||||
|
buf += " URL: " + library.url + '\n';
|
||||||
|
buf += " SHA-1: " + library.sha1 + '\n';
|
||||||
|
buf += " Size: " + wxString::Format("%ld", library.size) + '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
wxLogInfo(buf);
|
||||||
|
});
|
||||||
|
|
||||||
auto *window = new InstallerWindow;
|
auto *window = new InstallerWindow;
|
||||||
window->Show();
|
window->Show();
|
||||||
|
|
|
@ -2,28 +2,61 @@
|
||||||
#include "info.hpp"
|
#include "info.hpp"
|
||||||
#include <nlohmann/json.hpp>
|
#include <nlohmann/json.hpp>
|
||||||
#include <wx/log.h>
|
#include <wx/log.h>
|
||||||
|
#include <wx/uri.h>
|
||||||
#include <wx/webrequest.h>
|
#include <wx/webrequest.h>
|
||||||
|
|
||||||
static wxString meta_url(const wxString &url) {
|
static wxString meta_url(const wxString &url) { return "https://meta.frogmc.dev/v1/" + url; }
|
||||||
return "https://meta.frogmc.dev/v1/" + url;
|
|
||||||
|
static LoaderLibrary make_loader_library(const nlohmann::json &json) {
|
||||||
|
if (!json.is_object())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
wxString name = wxString::FromUTF8(json.value("name", std::string()));
|
||||||
|
wxString url = wxString::FromUTF8(json.value("url", std::string()));
|
||||||
|
wxString sha1 = wxString::FromUTF8(json.value("sha1", std::string()));
|
||||||
|
const int64_t size = json.value("size", int64_t(-1));
|
||||||
|
|
||||||
|
if (name.IsEmpty() || url.IsEmpty() || sha1.IsEmpty() || size < 0)
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return {.valid = true, .name = std::move(name), .url = std::move(url), .sha1 = std::move(sha1), .size = size};
|
||||||
}
|
}
|
||||||
|
|
||||||
static LoaderVersion make_loader_version(const nlohmann::json &value) {
|
static LoaderVersion make_loader_version(const nlohmann::json &json) {
|
||||||
wxDateTime release_date(static_cast<time_t>(0));
|
if (!json.is_object())
|
||||||
release_date.ParseISOCombined(
|
return {};
|
||||||
wxString::FromUTF8(value.value("dateTime", std::string())));
|
|
||||||
|
|
||||||
return {wxString::FromUTF8(value.value("version", std::string())),
|
wxString version = wxString::FromUTF8(json.value("version", std::string()));
|
||||||
release_date,
|
|
||||||
wxString::FromUTF8(value.value("downloadUrl", std::string()))};
|
wxDateTime release_date;
|
||||||
|
release_date.ParseISOCombined(wxString::FromUTF8(json.value("releaseDate", std::string())));
|
||||||
|
|
||||||
|
std::vector<LoaderLibrary> libraries;
|
||||||
|
|
||||||
|
if (json.contains("libraries")) {
|
||||||
|
const nlohmann::json &libraries_json = json.at("libraries");
|
||||||
|
|
||||||
|
if (!libraries_json.is_array())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
for (const nlohmann::json &library_json : libraries_json) {
|
||||||
|
libraries.push_back(make_loader_library(library_json));
|
||||||
|
|
||||||
|
if (!libraries.back().valid)
|
||||||
|
return {};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void get_loader_versions(
|
if (version.IsEmpty() || !release_date.IsValid())
|
||||||
wxEvtHandler *handler,
|
return {};
|
||||||
const std::function<void(std::optional<std::vector<LoaderVersion>>)>
|
|
||||||
&callback) {
|
return {
|
||||||
wxWebRequest request = wxWebSession::GetDefault().CreateRequest(
|
.valid = true, .version = std::move(version), .release_date = release_date, .libraries = std::move(libraries)};
|
||||||
handler, meta_url("loader/versions"));
|
}
|
||||||
|
|
||||||
|
void get_loader_version(wxEvtHandler *handler, const wxString &version,
|
||||||
|
std::function<void(std::optional<LoaderVersion>)> callback) {
|
||||||
|
wxWebRequest request = wxWebSession::GetDefault().CreateRequest(handler, meta_url("loader/versions/" + version));
|
||||||
|
|
||||||
if (!request.IsOk()) {
|
if (!request.IsOk()) {
|
||||||
wxLogError("Invalid request object");
|
wxLogError("Invalid request object");
|
||||||
|
@ -32,34 +65,65 @@ void get_loader_versions(
|
||||||
}
|
}
|
||||||
|
|
||||||
request.SetHeader("User-Agent", info::USER_AGENT);
|
request.SetHeader("User-Agent", info::USER_AGENT);
|
||||||
handler->Bind(wxEVT_WEBREQUEST_STATE, [callback](wxWebRequestEvent &event) {
|
|
||||||
|
handler->Bind(wxEVT_WEBREQUEST_STATE, [callback = std::move(callback)](wxWebRequestEvent &event) mutable {
|
||||||
if (event.GetState() == wxWebRequest::State_Completed) {
|
if (event.GetState() == wxWebRequest::State_Completed) {
|
||||||
const nlohmann::json response = nlohmann::json::parse(
|
const nlohmann::json json = nlohmann::json::parse(event.GetResponse().AsString().utf8_string());
|
||||||
event.GetResponse().AsString().utf8_string());
|
|
||||||
|
|
||||||
if (!response.is_array()) {
|
LoaderVersion result = make_loader_version(json);
|
||||||
wxLogError("Response is not an array");
|
|
||||||
|
if (!result.valid) {
|
||||||
|
wxLogError("Invalid loader version object: `%s`", json.dump(4));
|
||||||
callback({});
|
callback({});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<LoaderVersion> result;
|
callback({std::move(result)});
|
||||||
|
return;
|
||||||
size_t i = 0;
|
} else if (event.GetState() == wxWebRequest::State_Failed) {
|
||||||
|
wxLogError("Request failed: %s", event.GetErrorDescription());
|
||||||
for (const auto &value : response) {
|
callback({});
|
||||||
result.push_back(make_loader_version(value));
|
return;
|
||||||
|
}
|
||||||
if (!result.back().is_valid()) {
|
});
|
||||||
wxLogError("Invalid loader version object: %s",
|
|
||||||
value.dump(4));
|
request.Start();
|
||||||
callback({});
|
}
|
||||||
return;
|
|
||||||
}
|
void get_loader_versions(wxEvtHandler *handler,
|
||||||
|
std::function<void(std::optional<std::vector<LoaderVersion>>)> callback) {
|
||||||
++i;
|
wxWebRequest request = wxWebSession::GetDefault().CreateRequest(handler, meta_url("loader/versions"));
|
||||||
}
|
|
||||||
|
if (!request.IsOk()) {
|
||||||
|
wxLogError("Invalid request object");
|
||||||
|
callback({});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
request.SetHeader("User-Agent", info::USER_AGENT);
|
||||||
|
|
||||||
|
handler->Bind(wxEVT_WEBREQUEST_STATE, [callback = std::move(callback)](wxWebRequestEvent &event) mutable {
|
||||||
|
if (event.GetState() == wxWebRequest::State_Completed) {
|
||||||
|
const nlohmann::json json = nlohmann::json::parse(event.GetResponse().AsString().utf8_string());
|
||||||
|
|
||||||
|
if (!json.is_array()) {
|
||||||
|
wxLogError("Response is not an array");
|
||||||
|
callback({});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector<LoaderVersion> result;
|
||||||
|
|
||||||
|
for (const nlohmann::json &version_json : json) {
|
||||||
|
result.push_back(make_loader_version(version_json));
|
||||||
|
|
||||||
|
if (!result.back().valid) {
|
||||||
|
wxLogError("Invalid loader version object: `%s`", version_json.dump(4));
|
||||||
|
callback({});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
callback({std::move(result)});
|
callback({std::move(result)});
|
||||||
return;
|
return;
|
||||||
} else if (event.GetState() == wxWebRequest::State_Failed) {
|
} else if (event.GetState() == wxWebRequest::State_Failed) {
|
||||||
|
|
|
@ -4,17 +4,24 @@
|
||||||
#include <wx/datetime.h>
|
#include <wx/datetime.h>
|
||||||
#include <wx/event.h>
|
#include <wx/event.h>
|
||||||
|
|
||||||
|
struct LoaderLibrary {
|
||||||
|
bool valid;
|
||||||
|
wxString name;
|
||||||
|
wxString url;
|
||||||
|
wxString sha1;
|
||||||
|
int64_t size;
|
||||||
|
};
|
||||||
|
|
||||||
struct LoaderVersion {
|
struct LoaderVersion {
|
||||||
|
bool valid;
|
||||||
wxString version;
|
wxString version;
|
||||||
wxDateTime release_date;
|
wxDateTime release_date;
|
||||||
wxString download_url;
|
std::vector<LoaderLibrary> libraries;
|
||||||
|
|
||||||
bool is_valid() const {
|
|
||||||
return !version.IsEmpty() && release_date.IsValid() &&
|
|
||||||
!download_url.IsEmpty();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void get_loader_version(wxEvtHandler *handler, const wxString &version,
|
||||||
|
std::function<void(std::optional<LoaderVersion>)> callback);
|
||||||
|
|
||||||
void get_loader_versions(
|
void get_loader_versions(
|
||||||
wxEvtHandler *handler,
|
wxEvtHandler *handler,
|
||||||
const std::function<void(std::optional<std::vector<LoaderVersion>>)> &callback);
|
std::function<void(std::optional<std::vector<LoaderVersion>>)> callback);
|
||||||
|
|
Loading…
Reference in a new issue