Upgrade Lua service intercepts (#775)

Co-authored-by: Théo B. <16072534+LiquidFenrir@users.noreply.github.com>
This commit is contained in:
wheremyfoodat 2025-07-07 11:52:57 +03:00 committed by GitHub
parent ce4750e375
commit c0948f4235
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 96 additions and 35 deletions

View file

@ -1,9 +1,10 @@
#include "services/service_manager.hpp"
#include <map>
#include <set>
#include "ipc.hpp"
#include "kernel.hpp"
#include "services/service_map.hpp"
ServiceManager::ServiceManager(
std::span<u32, 16> regs, Memory& mem, GPU& gpu, u32& currentPID, Kernel& kernel, const EmulatorConfig& config, LuaManager& lua
@ -98,7 +99,7 @@ void ServiceManager::registerClient(u32 messagePointer) {
}
// clang-format off
static std::map<std::string, HorizonHandle> serviceMap = {
static const ServiceMapEntry serviceMapArray[] = {
{ "ac:u", KernelHandles::AC },
{ "ac:i", KernelHandles::AC },
{ "act:a", KernelHandles::ACT },
@ -148,6 +149,9 @@ static std::map<std::string, HorizonHandle> serviceMap = {
};
// clang-format on
static std::set<ServiceMapEntry, ServiceMapByNameComparator> serviceMapByName{std::begin(serviceMapArray), std::end(serviceMapArray)};
static std::set<ServiceMapEntry, ServiceMapByHandleComparator> serviceMapByHandle{std::begin(serviceMapArray), std::end(serviceMapArray)};
// https://www.3dbrew.org/wiki/SRV:GetServiceHandle
void ServiceManager::getServiceHandle(u32 messagePointer) {
u32 nameLength = mem.read32(messagePointer + 12);
@ -158,7 +162,7 @@ void ServiceManager::getServiceHandle(u32 messagePointer) {
log("srv::getServiceHandle (Service: %s, nameLength: %d, flags: %d)\n", service.c_str(), nameLength, flags);
// Look up service handle in map, panic if it does not exist
if (auto search = serviceMap.find(service); search != serviceMap.end())
if (auto search = serviceMapByName.find(service); search != serviceMapByName.end())
handle = search->second;
else
Helpers::panic("srv: GetServiceHandle with unknown service %s", service.c_str());
@ -271,16 +275,13 @@ 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);
}
if (auto service_it = serviceMapByHandle.find(handle); service_it != serviceMapByHandle.end()) {
auto intercept = InterceptedService(service_it->first, function);
break;
if (auto intercept_it = interceptedServices.find(intercept); intercept_it != interceptedServices.end()) {
// 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, intercept_it->second);
}
}