Clean up service intercepts

This commit is contained in:
wheremyfoodat 2025-07-04 00:25:02 +03:00
parent 9932e58bf0
commit 162e73bfd2
6 changed files with 85 additions and 46 deletions

View file

@ -216,25 +216,9 @@ void ServiceManager::publishToSubscriber(u32 messagePointer) {
}
void ServiceManager::sendCommandToService(u32 messagePointer, Handle handle) {
if (interceptedServices.size() != 0) [[unlikely]] {
// Check if there's a Lua handler for this function and call it
u32 function = mem.read32(messagePointer);
for (auto [serviceName, serviceHandle] : serviceMap) {
if (serviceHandle == handle) {
auto intercept = InterceptedService(std::string(serviceName), function);
if (interceptedServices.contains(intercept)) {
printf("Call to intercepted service\n");
// If the Lua handler returns true, it means the service is handled entirely
// From Lua, and we shouldn't do anything else here.
if (lua.signalInterceptedService(intercept.serviceName, function, messagePointer)) {
return;
}
}
break;
}
if (haveServiceIntercepts) [[unlikely]] {
if (checkForIntercept(messagePointer, handle)) [[unlikely]] {
return;
}
}
@ -281,4 +265,25 @@ void ServiceManager::sendCommandToService(u32 messagePointer, Handle handle) {
case KernelHandles::Y2R: y2r.handleSyncRequest(messagePointer); break;
default: Helpers::panic("Sent IPC message to unknown service %08X\n Command: %08X", handle, mem.read32(messagePointer));
}
}
bool ServiceManager::checkForIntercept(u32 messagePointer, Handle handle) {
// Check if there's a Lua handler for this function and call it
const u32 function = mem.read32(messagePointer);
for (auto [serviceName, serviceHandle] : serviceMap) {
if (serviceHandle == handle) {
auto intercept = InterceptedService(std::string(serviceName), function);
if (interceptedServices.contains(intercept)) {
// If the Lua handler returns true, it means the service is handled entirely
// From Lua, and we shouldn't do anything else here.
return lua.signalInterceptedService(intercept.serviceName, function, messagePointer);
}
break;
}
}
// Lua did not intercept the service, so emulate it normally
return false;
}

View file

@ -1,5 +1,4 @@
#ifdef PANDA3DS_ENABLE_LUA
#include <fmt/format.h>
#include <teakra/disassembler.h>
#include <array>
@ -119,8 +118,8 @@ bool LuaManager::signalInterceptedService(const std::string& service, u32 functi
if (status != LUA_OK) {
const char* err = lua_tostring(L, -1);
fprintf(stderr, "Lua error in interceptService: %s\n", err ? err : "(unknown error)");
lua_pop(L, 1); // Remove error message from stack
fprintf(stderr, "Lua: Error in interceptService: %s\n", err);
lua_pop(L, 1); // Pop error message from stack
return false; // Have the C++ handle the service call
}
@ -236,7 +235,7 @@ static int loadROMThunk(lua_State* L) {
return 1;
}
static int interceptServiceThunk(lua_State* L) {
static int addServiceInterceptThunk(lua_State* L) {
// Service name argument is invalid, report that loading failed and exit
if (lua_type(L, 1) != LUA_TSTRING) {
lua_pushboolean(L, 0);
@ -256,11 +255,14 @@ static int interceptServiceThunk(lua_State* L) {
const u32 function = (u32)lua_tointeger(L, 2);
const auto serviceName = std::string(str, nameLength);
LuaManager::g_emulator->getServiceManager().addServiceIntercept(serviceName, function);
fmt::print("Intercepting call to {} (Function id: {:08X})\n", serviceName, function);
return 2;
}
static int clearServiceInterceptsThunk(lua_State* L) {
LuaManager::g_emulator->getServiceManager().clearServiceIntercepts();
return 0;
}
static int getButtonsThunk(lua_State* L) {
auto buttons = LuaManager::g_emulator->getServiceManager().getHID().getOldButtons();
lua_pushinteger(L, static_cast<lua_Integer>(buttons));
@ -347,7 +349,8 @@ static constexpr luaL_Reg functions[] = {
{ "__getButton", getButtonThunk },
{ "__disassembleARM", disassembleARMThunk },
{ "__disassembleTeak", disassembleTeakThunk },
{"__interceptService", interceptServiceThunk},
{"__addServiceIntercept", addServiceInterceptThunk },
{"__clearServiceIntercepts", clearServiceInterceptsThunk },
{ nullptr, nullptr },
};
// clang-format on
@ -388,7 +391,8 @@ void LuaManager::initializeThunks() {
disassembleARM = function(pc, instruction) return GLOBALS.__disassembleARM(pc, instruction) end,
disassembleTeak = function(opcode, exp) return GLOBALS.__disassembleTeak(opcode, exp or 0) end,
interceptService = function(service, func) return GLOBALS.__interceptService(service, func) end,
addServiceIntercept = function(service, func) return GLOBALS.__addServiceIntercept(service, func) end,
clearServiceIntercepts = function() return GLOBALS.__clearServiceIntercepts() end,
Frame = __Frame,
ButtonA = __ButtonA,
@ -397,6 +401,8 @@ void LuaManager::initializeThunks() {
ButtonY = __ButtonY,
ButtonL = __ButtonL,
ButtonR = __ButtonR,
ButtonZL = __ButtonZL,
ButtonZR = __ButtonZR,
ButtonUp = __ButtonUp,
ButtonDown = __ButtonDown,
ButtonLeft = __ButtonLeft,