update ipc

- add getRuntimeInfo funcs for getting json representation of Hook, Patch, Mod and others
 - add args to list-mods
This commit is contained in:
HJfod 2022-11-23 16:38:08 +02:00
parent b9287cebde
commit 1ac6ad4996
7 changed files with 92 additions and 14 deletions

View file

@ -5,6 +5,7 @@
#include <Geode/utils/types.hpp>
#include <inttypes.h>
#include <string_view>
#include "../utils/json.hpp"
namespace geode {
class Mod;
@ -77,6 +78,12 @@ namespace geode {
Mod* getOwner() const {
return m_owner;
}
/**
* Get info about the hook as JSON
* @note For IPC
*/
nlohmann::json getRuntimeInfo() const;
};
class GEODE_DLL Patch {
@ -128,5 +135,11 @@ namespace geode {
Mod* getOwner() const {
return m_owner;
}
/**
* Get info about the patch as JSON
* @note For IPC
*/
nlohmann::json getRuntimeInfo() const;
};
}

View file

@ -402,7 +402,7 @@ namespace geode {
/**
* Get the mod's config directory path
*/
ghc::filesystem::path getConfigDir() const;
ghc::filesystem::path getConfigDir(bool create = true) const;
bool hasSettings() const;
decltype(ModInfo::m_settings) getSettings() const;
@ -646,6 +646,12 @@ namespace geode {
std::vector<Dependency> getUnresolvedDependencies();
char const* expandSpriteName(char const* name);
/**
* Get info about the mod as JSON
* @note For IPC
*/
ModJson getRuntimeInfo() const;
};
template<class T>

View file

@ -96,3 +96,12 @@ bool InternalLoader::loadHooks() {
internalHooks().clear();
return !thereWereErrors;
}
nlohmann::json Hook::getRuntimeInfo() const {
auto json = nlohmann::json::object();
json["address"] = reinterpret_cast<uintptr_t>(m_address);
json["detour"] = reinterpret_cast<uintptr_t>(m_detour);
json["name"] = m_displayName;
json["enabled"] = m_enabled;
return json;
}

View file

@ -550,9 +550,9 @@ ghc::filesystem::path Mod::getPackagePath() const {
return m_info.m_path;
}
ghc::filesystem::path Mod::getConfigDir() const {
ghc::filesystem::path Mod::getConfigDir(bool create) const {
auto dir = Loader::get()->getGeodeDirectory() / GEODE_CONFIG_DIRECTORY / m_info.m_id;
if (!ghc::filesystem::exists(dir)) {
if (create && !ghc::filesystem::exists(dir)) {
ghc::filesystem::create_directories(dir);
}
return dir;
@ -633,3 +633,25 @@ bool Mod::hasSetting(std::string const& key) const {
std::string Mod::getLoadErrorInfo() const {
return m_loadErrorInfo;
}
ModJson Mod::getRuntimeInfo() const {
auto json = m_info.toJSON();
auto obj = ModJson::object();
obj["hooks"] = ModJson::array();
for (auto hook : m_hooks) {
obj["hooks"].push_back(ModJson(hook->getRuntimeInfo()));
}
obj["patches"] = ModJson::array();
for (auto patch : m_patches) {
obj["patches"].push_back(ModJson(patch->getRuntimeInfo()));
}
obj["enabled"] = m_enabled;
obj["loaded"] = m_loaded;
obj["temp-dir"] = this->getTempDir();
obj["save-dir"] = this->getSaveDir();
obj["config-dir"] = this->getConfigDir(false);
json["runtime"] = obj;
return json;
}

View file

@ -303,6 +303,6 @@ ModJson ModInfo::getRawJSON() const {
return m_rawJSON;
}
void to_json(nlohmann::json& json, ModInfo const& info) {
void geode::to_json(nlohmann::json& json, ModInfo const& info) {
json = info.toJSON();
}

View file

@ -48,3 +48,12 @@ bool Patch::apply() {
bool Patch::restore() {
return lilac::hook::write_memory(m_address, m_original.data(), m_original.size());
}
nlohmann::json Patch::getRuntimeInfo() const {
auto json = nlohmann::json::object();
json["address"] = reinterpret_cast<uintptr_t>(m_address);
json["original"] = m_original;
json["patch"] = m_patch;
json["applied"] = m_applied;
return json;
}

View file

@ -118,17 +118,36 @@ static auto $_ = listenForIPC("ipc-test", +[](IPCEvent* event) -> nlohmann::json
return "Hello from Geode!";
});
static auto $_ = listenForIPC("loader-info", +[](IPCEvent* event) -> nlohmann::json {
return Loader::get()->getInternalMod()->getModInfo();
});
static auto $_ = listenForIPC("list-mods", +[](IPCEvent* event) -> nlohmann::json {
log::debug("List mods for {}", event->getReplyID().value_or("<None>"));
return ranges::map<std::vector<nlohmann::json>>(
ranges::concat(
{ Loader::get()->getInternalMod() },
Loader::get()->getAllMods()
),
[](Mod* mod) {
return mod->getModInfo().toJSON();
}
);
std::vector<nlohmann::json> res;
auto args = event->getMessageData();
JsonChecker checker(args);
auto root = checker.root("").obj();
auto includeRunTimeInfo = root.has("include-runtime-info").template get<bool>();
auto dontIncludeLoader = root.has("dont-include-loader").template get<bool>();
if (!dontIncludeLoader) {
res.push_back(includeRunTimeInfo ?
Loader::get()->getInternalMod()->getRuntimeInfo() :
Loader::get()->getInternalMod()->getModInfo().toJSON()
);
}
for (auto& mod : Loader::get()->getAllMods()) {
res.push_back(
includeRunTimeInfo ?
mod->getRuntimeInfo() :
mod->getModInfo().toJSON()
);
}
return res;
});
int geodeEntry(void* platformData) {