Update meta

This commit is contained in:
TheKodeToad 2024-06-13 23:49:57 +01:00
parent 99450ca12a
commit ebf11a94ab
No known key found for this signature in database
GPG key ID: 5E39D70B4C93C38E
6 changed files with 139 additions and 48 deletions

View file

@ -3,6 +3,7 @@ IndentWidth: 4
TabWidth: 4
UseTab: Always
ContinuationIndentWidth: 4
ColumnLimit: 120
---
Language: Cpp
AccessModifierOffset: -4

View file

@ -1,5 +1,8 @@
cmake_minimum_required(VERSION 3.25)
project(leapfrog)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_CXX_STANDARD 17)
add_subdirectory(lib)
add_subdirectory(installer)

View file

@ -1,5 +1,3 @@
set(CMAKE_INCLUDE_CURRENT_DIR ON)
configure_file(info.hpp.in 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)

View file

@ -11,7 +11,25 @@ public:
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;
window->Show();

View file

@ -2,28 +2,61 @@
#include "info.hpp"
#include <nlohmann/json.hpp>
#include <wx/log.h>
#include <wx/uri.h>
#include <wx/webrequest.h>
static wxString meta_url(const wxString &url) {
return "https://meta.frogmc.dev/v1/" + url;
static wxString meta_url(const wxString &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) {
wxDateTime release_date(static_cast<time_t>(0));
release_date.ParseISOCombined(
wxString::FromUTF8(value.value("dateTime", std::string())));
static LoaderVersion make_loader_version(const nlohmann::json &json) {
if (!json.is_object())
return {};
return {wxString::FromUTF8(value.value("version", std::string())),
release_date,
wxString::FromUTF8(value.value("downloadUrl", std::string()))};
wxString version = wxString::FromUTF8(json.value("version", 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(
wxEvtHandler *handler,
const std::function<void(std::optional<std::vector<LoaderVersion>>)>
&callback) {
wxWebRequest request = wxWebSession::GetDefault().CreateRequest(
handler, meta_url("loader/versions"));
if (version.IsEmpty() || !release_date.IsValid())
return {};
return {
.valid = true, .version = std::move(version), .release_date = release_date, .libraries = std::move(libraries)};
}
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()) {
wxLogError("Invalid request object");
@ -32,34 +65,65 @@ void get_loader_versions(
}
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) {
const nlohmann::json response = nlohmann::json::parse(
event.GetResponse().AsString().utf8_string());
const nlohmann::json json = nlohmann::json::parse(event.GetResponse().AsString().utf8_string());
if (!response.is_array()) {
wxLogError("Response is not an array");
LoaderVersion result = make_loader_version(json);
if (!result.valid) {
wxLogError("Invalid loader version object: `%s`", json.dump(4));
callback({});
return;
}
std::vector<LoaderVersion> result;
size_t i = 0;
for (const auto &value : response) {
result.push_back(make_loader_version(value));
if (!result.back().is_valid()) {
wxLogError("Invalid loader version object: %s",
value.dump(4));
callback({});
return;
}
++i;
}
callback({std::move(result)});
return;
} else if (event.GetState() == wxWebRequest::State_Failed) {
wxLogError("Request failed: %s", event.GetErrorDescription());
callback({});
return;
}
});
request.Start();
}
void get_loader_versions(wxEvtHandler *handler,
std::function<void(std::optional<std::vector<LoaderVersion>>)> callback) {
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)});
return;
} else if (event.GetState() == wxWebRequest::State_Failed) {

View file

@ -4,17 +4,24 @@
#include <wx/datetime.h>
#include <wx/event.h>
struct LoaderLibrary {
bool valid;
wxString name;
wxString url;
wxString sha1;
int64_t size;
};
struct LoaderVersion {
bool valid;
wxString version;
wxDateTime release_date;
wxString download_url;
bool is_valid() const {
return !version.IsEmpty() && release_date.IsValid() &&
!download_url.IsEmpty();
}
std::vector<LoaderLibrary> libraries;
};
void get_loader_version(wxEvtHandler *handler, const wxString &version,
std::function<void(std::optional<LoaderVersion>)> callback);
void get_loader_versions(
wxEvtHandler *handler,
const std::function<void(std::optional<std::vector<LoaderVersion>>)> &callback);
std::function<void(std::optional<std::vector<LoaderVersion>>)> callback);