Meta parsing stuff

This commit is contained in:
TheKodeToad 2024-06-10 00:02:12 +01:00
parent b6b70fed17
commit 1c726f7d0f
No known key found for this signature in database
GPG key ID: 5E39D70B4C93C38E
13 changed files with 155 additions and 22 deletions

8
.clang-format Normal file
View file

@ -0,0 +1,8 @@
---
IndentWidth: 4
TabWidth: 4
UseTab: Always
ContinuationIndentWidth: 4
---
Language: Cpp
AccessModifierOffset: -4

2
.gitignore vendored
View file

@ -5,4 +5,4 @@ cmake-build-release/
.project
.workspace
.settings/
.settings/

3
.gitmodules vendored
View file

@ -1,3 +1,6 @@
[submodule "lib/wx"]
path = lib/wx
url = https://github.com/wxWidgets/wxWidgets.git
[submodule "lib/json"]
path = lib/json
url = https://github.com/nlohmann/json.git

View file

@ -2,7 +2,4 @@ cmake_minimum_required(VERSION 3.25)
project(leapfrog)
add_subdirectory(lib)
add_executable(leapfrog main.cpp)
target_link_libraries(leapfrog PRIVATE wx::base wx::core wx::net)
include_directories(lib/wx/include)
add_subdirectory(installer)

7
installer/CMakeLists.txt Normal file
View file

@ -0,0 +1,7 @@
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)
include_directories(lib/wx/include)
include_directories(lib/json/include)

7
installer/info.hpp.in Normal file
View file

@ -0,0 +1,7 @@
#pragma once
#include <wx/string.h>
namespace info {
const wxString USER_AGENT("Leapfrog Installer/@leapfrog_VERSION@");
}

23
installer/main.cpp Normal file
View file

@ -0,0 +1,23 @@
#include "meta.hpp"
#include "ui.hpp"
#include <iostream>
#include <wx/app.h>
class App final : public wxApp {
public:
bool OnInit() override {
if (!wxApp::OnInit())
return false;
wxLog::SetActiveTarget(new wxLogStream(&std::cout));
get_loader_versions(this, [](auto) {});
auto *window = new InstallerWindow;
window->Show();
return true;
}
};
wxIMPLEMENT_APP(App);

73
installer/meta.cpp Normal file
View file

@ -0,0 +1,73 @@
#include "meta.hpp"
#include "info.hpp"
#include <nlohmann/json.hpp>
#include <wx/log.h>
#include <wx/webrequest.h>
static wxString meta_url(const wxString &url) {
return "https://meta.frogmc.dev/v1/" + url;
}
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())));
return {wxString::FromUTF8(value.value("version", std::string())),
release_date,
wxString::FromUTF8(value.value("downloadUrl", std::string()))};
}
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 (!request.IsOk()) {
wxLogError("Invalid request object");
callback({});
return;
}
request.SetHeader("User-Agent", info::USER_AGENT);
handler->Bind(wxEVT_WEBREQUEST_STATE, [callback](wxWebRequestEvent &event) {
if (event.GetState() == wxWebRequest::State_Completed) {
const nlohmann::json response = nlohmann::json::parse(
event.GetResponse().AsString().utf8_string());
if (!response.is_array()) {
wxLogError("Response is not an array");
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();
}

20
installer/meta.hpp Normal file
View file

@ -0,0 +1,20 @@
#pragma once
#include <optional>
#include <wx/datetime.h>
#include <wx/event.h>
struct LoaderVersion {
wxString version;
wxDateTime release_date;
wxString download_url;
bool is_valid() const {
return !version.IsEmpty() && release_date.IsValid() &&
!download_url.IsEmpty();
}
};
void get_loader_versions(
wxEvtHandler *handler,
const std::function<void(std::optional<std::vector<LoaderVersion>>)> &callback);

3
installer/ui.cpp Normal file
View file

@ -0,0 +1,3 @@
#include "ui.hpp"
InstallerWindow::InstallerWindow() {}

8
installer/ui.hpp Normal file
View file

@ -0,0 +1,8 @@
#pragma once
#include "wx/window.h"
class InstallerWindow final : public wxWindow {
public:
explicit InstallerWindow();
};

1
lib/json Submodule

@ -0,0 +1 @@
Subproject commit 8c391e04fe4195d8be862c97f38cfe10e2a3472e

View file

@ -1,17 +0,0 @@
#include <iostream>
#include <wx/wx.h>
class App final : public wxApp {
public:
bool OnInit() override {
if (!wxApp::OnInit())
return false;
wxMessageBox("gaming");
return true;
}
};
wxIMPLEMENT_APP(App);