Basic installation -- hardcoded vers

This commit is contained in:
TheKodeToad 2024-06-21 20:41:05 +01:00
parent bd85e982e5
commit f90508cedd
No known key found for this signature in database
GPG key ID: 5E39D70B4C93C38E
11 changed files with 204 additions and 40 deletions

7
.gitignore vendored
View file

@ -1,7 +1,8 @@
cmake-build-debug/
cmake-build-release/
.idea/
cmake-build-*/
.vs/
out/
.project
.workspace

View file

@ -1,4 +1,10 @@
add_executable(installer
if (WIN32)
add_executable(installer WIN32)
else()
add_executable(installer)
endif()
target_sources(installer PRIVATE
main.cpp
ui.cpp
ui.hpp
@ -29,3 +35,4 @@ target_sources(installer PRIVATE resources/resources.cpp resources/resources.h)
target_link_libraries(installer PRIVATE wx::base wx::core wx::net wx::xrc)
include_directories(../lib/wx/include)
include_directories(../lib/json/include)

View file

@ -1 +1,89 @@
#include "installer.hpp"
#include <nlohmann/json.hpp>
#include <wx/log.h>
#include <wx/tokenzr.h>
InstallerResult install_loader(const InstallerConfig &config) {
if (!config.installation_dir.Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL))
return InstallerResult::CouldntCreateLauncherDataDir;
const wxString version_id = "frogloader-" + config.loader_version.version + '-' + config.minecraft_version;
const wxString version_dir = config.launcher_data_dir.GetFullPath() + "/versions/" + version_id;
const wxString version_json_path = version_dir + '/' + version_id + ".json";
if (!wxFileName::Mkdir(version_dir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL))
return InstallerResult::CouldntCreateVersionDir;
nlohmann::json version_libraries_json = nlohmann::json::array();
for (const LoaderLibrary &library : config.loader_version.libraries) {
wxArrayString name_tokens = wxStringTokenize(library.name, ":", wxTOKEN_RET_EMPTY_ALL);
if (name_tokens.size() < 3) {
wxLogWarning("Invalid library name: %s", library.name);
continue;
}
if (name_tokens.size() > 4) {
wxLogWarning("Don't know how to handle more than 4 components in name: %s", library.name);
continue;
}
wxString base_path = name_tokens.at(0);
base_path.Replace(".", "/");
wxString path = base_path + '/' + name_tokens.at(1) + '/' + name_tokens.at(2) + '/' + name_tokens.at(1) + '-' +
name_tokens.at(2);
if (name_tokens.size() > 3)
path += '-' + name_tokens[3];
path += ".jar";
version_libraries_json.push_back({
{"downloads",
{{"artifact",
{
{"path", path},
{"url", library.url},
{"size", library.size},
{"sha1", library.sha1},
}}}},
{"name", library.name},
});
}
nlohmann::json version_json{{"id", version_id},
{"mainClass", "dev.frogmc.frogloader.impl.launch.client.FrogClient"},
{"inheritsFrom", config.minecraft_version},
{"libraries", version_libraries_json}};
{
wxFile version_json_file(version_json_path, wxFile::write);
if (!version_json_file.IsOpened())
return InstallerResult::CouldntWriteVersion;
if (!version_json_file.Write(version_json.dump(4), wxConvUTF8))
return InstallerResult::CouldntWriteVersion;
}
//
// wxString launcher_profiles_path = config.launcher_data_dir.GetFullPath() + "/launcher-profiles.json";
// nlohmann::json launcher_profiles_json;
//
// if (wxFileExists(launcher_profiles_path)) {
// wxFile file(launcher_profiles_path, wxFile::read);
// if (!file.IsOpened())
// return InstallerResult::CouldntReadLauncherProfiles;
//
// wxString content;
// if (!file.ReadAll(&content, wxConvUTF8))
// return InstallerResult::CouldntReadLauncherProfiles;
//
// launcher_profiles_json = nlohmann::json::parse(content);
// } else
// launcher_profiles_json = nlohmann::json::object({"version", 3, "profiles", nlohmann::json::array()});
return InstallerResult::OK;
}

View file

@ -1,7 +1,30 @@
#pragma once
#include "meta.hpp"
#include <wx/filename.h>
wxFileName get_launcher_data_dir();
struct InstallerConfig {
wxFileName launcher_data_dir;
wxString minecraft_version;
LoaderVersion loader_version;
wxString installation_name;
wxFileName installation_dir;
};
enum class InstallerResult {
OK,
CouldntCreateLauncherDataDir,
CouldntCreateVersionDir,
CouldntWriteVersion,
CouldntReadLauncherProfiles,
CouldntWriteLauncherProfiles,
CouldntCreateInstallationDir,
};
wxFileName find_launcher_data_dir();
bool is_launcher_open();
InstallerResult install_loader(const InstallerConfig &config);

View file

@ -1,10 +1,12 @@
#include <wx/defs.h>
#ifdef __LINUX__
#include "installer.hpp"
#include <wx/dir.h>
#include <wx/utils.h>
#ifdef __LINUX__
wxFileName get_launcher_data_dir() {
wxFileName find_launcher_data_dir() {
const wxString home = wxGetHomeDir();
const wxFileName dir = wxFileName::DirName(home + "/.minecraft");
@ -52,7 +54,6 @@ bool is_launcher_open() {
ssize_t read;
wxString current_content;
std::vector<wxString> args;
while ((read = cmdline_file.Read(buffer, BUFSIZ)) != wxInvalidOffset && read != 0) {
for (size_t index = 0; index < read; ++index) {
@ -60,7 +61,9 @@ bool is_launcher_open() {
if (value == '\0') {
if (!current_content.empty()) {
args.push_back(current_content);
if (filter(current_content))
return true;
current_content.clear();
}
} else
@ -68,10 +71,7 @@ bool is_launcher_open() {
}
}
if (!current_content.empty())
args.push_back(current_content);
if (std::any_of(args.begin(), args.end(), filter))
if (!current_content.empty() && filter(current_content))
return true;
}

View file

@ -1,10 +1,12 @@
#include <wx/defs.h>
#ifdef __DARWIN__
#include "installer.hpp"
#include <wx/dir.h>
#include <wx/utils.h>
#ifdef __DARWIN__
wxFileName get_launcher_data_dir() {
wxFileName find_launcher_data_dir() {
return wxFileName::DirName(wxGetHomeDir() + "/Library/Application Support/minecraft");
}

View file

@ -1,10 +1,14 @@
#include "installer.hpp"
#include <wx/dir.h>
#include <wx/utils.h>
#include <wx/defs.h>
#ifdef __WINDOWS__
wxFileName get_launcher_data_dir() {
#include "installer.hpp"
#include <wx/dir.h>
#include <wx/stdpaths.h>
#include <wx/utils.h>
wxFileName find_launcher_data_dir() {
return wxFileName::DirName(wxStandardPaths::Get().GetUserConfigDir() + "\\.minecraft");
}

View file

@ -55,7 +55,7 @@ static LoaderVersion make_loader_version(const nlohmann::json &json) {
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));
wxWebRequest request = wxWebSession::GetDefault().CreateRequest(handler, meta_url("loader/version/" + version));
if (!request.IsOk()) {
wxLogError("Invalid request object");
@ -67,7 +67,15 @@ void get_loader_version(wxEvtHandler *handler, const wxString &version,
handler->Bind(wxEVT_WEBREQUEST_STATE, [callback = std::move(callback)](const wxWebRequestEvent &event) mutable {
if (event.GetState() == wxWebRequest::State_Completed) {
const nlohmann::json json = nlohmann::json::parse(event.GetResponse().AsString().utf8_string());
nlohmann::json json;
try {
json = nlohmann::json::parse(event.GetResponse().AsString().utf8_string());
} catch (const nlohmann::json::parse_error &error) {
wxLogError("Invalid response: %s", error.what());
callback({});
return;
}
LoaderVersion result = make_loader_version(json);

View file

@ -4,25 +4,51 @@
InstallerFrame::InstallerFrame() : InstallerFrame_UI(nullptr) {
heading->SetFont(heading->GetFont().Scaled(1.6F));
launcher_data_dir_input->ChangeValue(get_launcher_data_dir().GetFullPath());
launcher_data_dir_browse->Bind(wxEVT_BUTTON, [this](wxCommandEvent &) {
launcher_data_dir_input->ChangeValue(find_launcher_data_dir().GetFullPath());
launcher_data_dir_browse->Bind(wxEVT_BUTTON, &InstallerFrame::select_launcher_data_dir, this);
name_input->SetHint("FrogLoader");
game_data_dir_input->SetHint(_("Use Default"));
game_data_dir_browse->Bind(wxEVT_BUTTON, &InstallerFrame::select_game_data_dir, this);
cancel->Bind(wxEVT_BUTTON, [this](wxCommandEvent &) { Close(); });
install->Bind(wxEVT_BUTTON, &InstallerFrame::perform_install, this);
}
void InstallerFrame::select_launcher_data_dir(const wxCommandEvent &) {
wxDirDialog dialog(this, _("Select launcher data directory"));
if (dialog.ShowModal() != wxID_OK)
return;
game_data_dir_input->ChangeValue(dialog.GetPath());
});
launcher_data_dir_input->ChangeValue(dialog.GetPath());
}
game_data_dir_input->SetHint(_("Use Default"));
game_data_dir_browse->Bind(wxEVT_BUTTON, [this](wxCommandEvent &) {
void InstallerFrame::select_game_data_dir(const wxCommandEvent &) {
wxDirDialog dialog(this, _("Select game data directory"));
if (dialog.ShowModal() != wxID_OK)
return;
game_data_dir_input->ChangeValue(dialog.GetPath());
}
void InstallerFrame::perform_install(const wxCommandEvent &) {
get_loader_version(this, "0.0.1-alpha.10", [this](std::optional<LoaderVersion> version) {
if (!version.has_value())
return;
InstallerResult result = install_loader({
.launcher_data_dir = launcher_data_dir_input->GetValue(),
.minecraft_version = "1.21",
.loader_version = *version,
.installation_name = name_input->GetValue(),
.installation_dir = game_data_dir_input->GetValue(),
});
cancel->Bind(wxEVT_BUTTON, [this](wxCommandEvent &) { Close(); });
if (result == InstallerResult::OK) {
wxMessageBox(_("Reopen the launcher and FrogLoader should be available."), _("Installation Complete"));
}
});
}

View file

@ -8,4 +8,10 @@
class InstallerFrame final : public InstallerFrame_UI {
public:
explicit InstallerFrame();
private:
void select_launcher_data_dir(const wxCommandEvent &);
void select_game_data_dir(const wxCommandEvent &);
void perform_install(const wxCommandEvent &);
};

View file

@ -1,6 +1,5 @@
set(wxBUILD_SHARED OFF)
set(wxUSE_REGEX OFF)
set(wxUSE_ZLIB OFF)
set(wxUSE_LIBTIFF OFF)
set(wxUSE_LIBJPEG OFF)
set(wxUSE_LIBSDL OFF)