mirror of
https://github.com/TwilitRealm/dusklight
synced 2026-05-24 06:50:43 -04:00
handle hook conflicts
This commit is contained in:
@@ -7,8 +7,8 @@ namespace dusk {
|
||||
void hookInstallByAddr(void* fn_addr, void* tramp_fn, void** orig_store);
|
||||
|
||||
void hookRegisterPre (void* fn_addr, void* mod, int32_t (*fn)(void* args));
|
||||
void hookRegisterPost(void* fn_addr, void* mod, void (*fn)(void* args));
|
||||
void hookSetReplace (void* fn_addr, void* mod, void (*fn)(void* args));
|
||||
void hookRegisterPost(void* fn_addr, void* mod, const char* mod_name, void (*fn)(void* args));
|
||||
bool hookSetReplace (void* fn_addr, void* mod, const char* mod_name, void (*fn)(void* args));
|
||||
|
||||
bool hookDispatchPre (void* fn_addr, void* args);
|
||||
void hookDispatchPost(void* fn_addr, void* args);
|
||||
|
||||
@@ -21,8 +21,9 @@ struct LoadedMod {
|
||||
std::string mod_path;
|
||||
std::string dir;
|
||||
|
||||
void* handle = nullptr;
|
||||
bool active = false;
|
||||
void* handle = nullptr;
|
||||
bool active = false;
|
||||
bool load_failed = false;
|
||||
|
||||
using FnInit = void (*)(DuskModAPI*);
|
||||
using FnTick = void (*)(DuskModAPI*);
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <funchook.h>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
@@ -17,6 +18,7 @@ struct PreHookFn {
|
||||
};
|
||||
struct VoidHookFn {
|
||||
void* mod;
|
||||
const char* mod_name;
|
||||
void (*fn)(void* args);
|
||||
};
|
||||
|
||||
@@ -121,15 +123,19 @@ void hookRegisterPre(void* fn_addr, void* mod, int32_t (*fn)(void* args)) {
|
||||
registry()[reinterpret_cast<uintptr_t>(fn_addr)].pre.push_back({mod, fn});
|
||||
}
|
||||
|
||||
void hookRegisterPost(void* fn_addr, void* mod, void (*fn)(void* args)) {
|
||||
registry()[reinterpret_cast<uintptr_t>(fn_addr)].post.push_back({mod, fn});
|
||||
void hookRegisterPost(void* fn_addr, void* mod, const char* mod_name, void (*fn)(void* args)) {
|
||||
registry()[reinterpret_cast<uintptr_t>(fn_addr)].post.push_back({mod, mod_name, fn});
|
||||
}
|
||||
|
||||
void hookSetReplace(void* fn_addr, void* mod, void (*fn)(void* args)) {
|
||||
bool hookSetReplace(void* fn_addr, void* mod, const char* mod_name, void (*fn)(void* args)) {
|
||||
auto& slot = registry()[reinterpret_cast<uintptr_t>(fn_addr)];
|
||||
if (slot.replace.fn)
|
||||
DuskLog.warn("HookSystem: replace hook for {} already set — overwriting", fn_addr);
|
||||
slot.replace = {mod, fn};
|
||||
if (slot.replace.fn) {
|
||||
DuskLog.error("HookSystem: '{}' conflicts with '{}', both replace the same function",
|
||||
mod_name, slot.replace.mod_name);
|
||||
return false;
|
||||
}
|
||||
slot.replace = {mod, mod_name, fn};
|
||||
return true;
|
||||
}
|
||||
|
||||
void hookClearMod(void* mod) {
|
||||
|
||||
@@ -48,12 +48,18 @@ void ImGuiMenuMods::showModsWindow() {
|
||||
|
||||
if (ImGui::BeginTabBar("##ModsOuter")) {
|
||||
for (const auto& mod : mods) {
|
||||
const std::string tabLabel = mod.name + (mod.active ? "" : " [disabled]");
|
||||
const std::string tabLabel = mod.name + (mod.load_failed ? " [failed]" : mod.active ? "" : " [disabled]");
|
||||
|
||||
if (ImGui::BeginTabItem(tabLabel.c_str())) {
|
||||
ImGui::Text("Version: %s", mod.version.c_str());
|
||||
ImGui::Text("Author: %s", mod.author.c_str());
|
||||
ImGui::Text("Status: %s", mod.active ? "Active" : "Disabled");
|
||||
|
||||
if (mod.load_failed) {
|
||||
ImGui::TextColored(ImVec4(1.f, 0.3f, 0.3f, 1.f), "Status: Failed to load");
|
||||
} else {
|
||||
ImGui::Text("Status: %s", mod.active ? "Active" : "Disabled");
|
||||
}
|
||||
|
||||
ImGui::Text("Path: %s", mod.mod_path.c_str());
|
||||
|
||||
if (!mod.description.empty()) {
|
||||
@@ -61,9 +67,11 @@ void ImGuiMenuMods::showModsWindow() {
|
||||
ImGui::TextWrapped("%s", mod.description.c_str());
|
||||
}
|
||||
|
||||
for (const auto& cb : mod.tab_content) {
|
||||
ImGui::Separator();
|
||||
ModLoader::callDrawCallback(mod, cb);
|
||||
if (!mod.load_failed) {
|
||||
for (const auto& cb : mod.tab_content) {
|
||||
ImGui::Separator();
|
||||
ModLoader::callDrawCallback(mod, cb);
|
||||
}
|
||||
}
|
||||
|
||||
ImGui::EndTabItem();
|
||||
|
||||
+12
-4
@@ -160,11 +160,15 @@ static void api_hook_pre(void* addr, int32_t (*fn)(void* args)) {
|
||||
}
|
||||
|
||||
static void api_hook_post(void* addr, void (*fn)(void* args)) {
|
||||
dusk::hookRegisterPost(addr, g_currentMod, fn);
|
||||
dusk::hookRegisterPost(addr, g_currentMod, modName(), fn);
|
||||
}
|
||||
|
||||
static void api_hook_replace(void* addr, void (*fn)(void* args)) {
|
||||
dusk::hookSetReplace(addr, g_currentMod, fn);
|
||||
if (!dusk::hookSetReplace(addr, g_currentMod, modName(), fn)) {
|
||||
if (g_currentMod) {
|
||||
g_currentMod->load_failed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
namespace dusk {
|
||||
@@ -343,8 +347,12 @@ void ModLoader::init() {
|
||||
ModGuard guard(&mod);
|
||||
try {
|
||||
mod.fn_init(&mod.api);
|
||||
mod.active = true;
|
||||
DuskLog.info("ModLoader: '{}' initialized", mod.name);
|
||||
if (!mod.load_failed) {
|
||||
mod.active = true;
|
||||
DuskLog.info("ModLoader: '{}' initialized", mod.name);
|
||||
} else {
|
||||
DuskLog.error("ModLoader: '{}' failed to load due to hook conflicts", mod.name);
|
||||
}
|
||||
} catch (const std::exception& e) {
|
||||
DuskLog.error("ModLoader: exception in {}.mod_init(): {}", mod.name, e.what());
|
||||
} catch (...) {
|
||||
|
||||
Reference in New Issue
Block a user