diff --git a/src/integrations/ServerAPIEvents.hpp b/src/integrations/ServerAPIEvents.hpp new file mode 100644 index 0000000..2425f0b --- /dev/null +++ b/src/integrations/ServerAPIEvents.hpp @@ -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> 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> getRegisteredServers() { + auto event = GetRegisteredServersEvent(); + event.post(); + return event.m_servers; + } +} diff --git a/src/utils/ServerUtils.cpp b/src/utils/ServerUtils.cpp index d651ccc..bcb3825 100644 --- a/src/utils/ServerUtils.cpp +++ b/src/utils/ServerUtils.cpp @@ -1,5 +1,6 @@ #include "ServerUtils.h" #include "../utils.hpp" +#include "../integrations/ServerAPIEvents.hpp" #include @@ -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("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;