Skip to content

Commit

Permalink
use ServerAPI for GDPS integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Cvolton committed Nov 27, 2024
1 parent 8747a49 commit 88df44e
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 5 deletions.
130 changes: 130 additions & 0 deletions src/integrations/ServerAPIEvents.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#pragma once

using namespace geode::prelude;

#ifdef GEODE_IS_WINDOWS
#ifdef KM7DEV_SERVER_API_EXPORTING
#define SERVER_API_DLL __declspec(dllexport)
#else
#define SERVER_API_DLL __declspec(dllimport)
#endif
#else
#define SERVER_API_DLL __attribute__((visibility("default")))
#endif

// thx prevter for the help

struct GetCurrentServerEvent : geode::Event {
GetCurrentServerEvent() {}

int m_id = 0;
std::string m_url = "";
int m_prio = 0;
};

struct GetServerByIdEvent : geode::Event {
GetServerByIdEvent(int id) : m_id(std::move(id)) {}

int m_id = 0;
std::string m_url = "";
int m_prio = 0;
};

struct RegisterServerEvent : geode::Event {
RegisterServerEvent(std::string url, int priority) : m_url(std::move(url)), m_priority(std::move(priority)) {}

int m_id;
std::string m_url = "";
int m_priority = 0;
};

struct UpdateServerEvent : geode::Event {
UpdateServerEvent(int id, std::string url , int priority) : m_id(std::move(id)), m_url(std::move(url)), m_priority(std::move(priority)) {}
UpdateServerEvent(int id, std::string url) : m_id(std::move(id)), m_url(std::move(url)) {}
UpdateServerEvent(int id, int priority) : m_id(std::move(id)), m_priority(std::move(priority)) {}

int m_id = 0;
std::string m_url = "";
int m_priority = 0;
};

struct RemoveServerEvent : geode::Event {
RemoveServerEvent(int id) : m_id(std::move(id)) {}

int m_id = 0;
};

struct GetBaseUrlEvent : geode::Event {
GetBaseUrlEvent() {}

std::string m_url = "";
};

struct GetRegisteredServersEvent : geode::Event {
GetRegisteredServersEvent() {}

std::map<int, std::pair<std::string, int>> m_servers = {};
};

namespace ServerAPIEvents {
struct Server {
int id;
std::string url;
int priority;
};

inline Server getCurrentServer() {
auto event = GetCurrentServerEvent();
event.post();
return {event.m_id, event.m_url, event.m_prio};
}

inline Server getServerById(int id) {
auto event = GetServerByIdEvent(id);
event.post();
return {event.m_id, event.m_url, event.m_prio};
}

inline Server registerServer(std::string url, int priority = 0) {
auto event = RegisterServerEvent(url, priority);
event.post();
return {event.m_id, event.m_url, event.m_priority};
}

inline void updateServer(Server server) {
auto event = UpdateServerEvent(server.id, server.url, server.priority);
event.post();
}

inline void updateServer(int id, std::string url, int priority) {
auto event = UpdateServerEvent(id, url, priority);
event.post();
}

inline void updateServer(int id, std::string url) {
auto event = UpdateServerEvent(id, url);
event.post();
}

inline void updateServer(int id, int priority) {
auto event = UpdateServerEvent(id, priority);
event.post();
}

inline void removeServer(int id) {
auto event = RemoveServerEvent(id);
event.post();
}

inline std::string getBaseUrl() {
auto event = GetBaseUrlEvent();
event.post();
return event.m_url;
}

inline std::map<int, std::pair<std::string, int>> getRegisteredServers() {
auto event = GetRegisteredServersEvent();
event.post();
return event.m_servers;
}
}
8 changes: 3 additions & 5 deletions src/utils/ServerUtils.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "ServerUtils.h"
#include "../utils.hpp"
#include "../integrations/ServerAPIEvents.hpp"

#include <shared_mutex>

Expand All @@ -20,11 +21,8 @@ web::WebRequest ServerUtils::getBaseRequest(bool setUserAgent) {
}

std::string ServerUtils::getBaseURL() {
// A cleaner solution for this would be a Server API dependency
// however I'd rather avoid depending on a mod that 90% of users
// don't actually use
if(Loader::get()->isModLoaded("km7dev.gdps-switcher")) {
auto url = Loader::get()->getLoadedMod("km7dev.gdps-switcher")->getSavedValue<std::string>("server");
if(Loader::get()->isModLoaded("km7dev.server_api")) {
auto url = ServerAPIEvents::getCurrentServer().url;
if(!url.empty()) {
while(url.ends_with("/")) url.pop_back();
return url;
Expand Down

0 comments on commit 88df44e

Please sign in to comment.