2022-02-04 16:25:44 +01:00
|
|
|
#include "PluginManagement.h"
|
|
|
|
#include "globals.h"
|
|
|
|
#include "hooks.h"
|
|
|
|
#include "patcher/hooks_patcher_static.h"
|
|
|
|
#include "plugin/PluginDataFactory.h"
|
2022-02-14 20:26:32 +01:00
|
|
|
#include "utils/utils.h"
|
2022-02-04 16:25:44 +01:00
|
|
|
#include <coreinit/debug.h>
|
|
|
|
#include <wums.h>
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2020-05-17 21:08:13 +02:00
|
|
|
WUMS_MODULE_EXPORT_NAME("homebrew_wupsbackend");
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2021-04-07 00:23:23 +02:00
|
|
|
WUMS_USE_WUT_DEVOPTAB();
|
2021-09-17 16:45:11 +02:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
WUMS_INITIALIZE() {
|
2022-01-23 21:43:54 +01:00
|
|
|
initLogging();
|
2022-05-14 14:00:20 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Patching functions");
|
|
|
|
for (uint32_t i = 0; i < method_hooks_static_size; i++) {
|
|
|
|
if (!FunctionPatcherPatchFunction(&method_hooks_static[i], nullptr)) {
|
|
|
|
OSFatal("homebrew_wupsbackend: Failed to patch function");
|
|
|
|
}
|
2020-06-07 14:10:31 +02:00
|
|
|
}
|
2022-01-23 21:43:54 +01:00
|
|
|
deinitLogging();
|
2020-12-28 14:38:08 +01:00
|
|
|
}
|
|
|
|
|
2021-03-16 17:55:32 +01:00
|
|
|
WUMS_APPLICATION_REQUESTS_EXIT() {
|
2022-05-14 14:00:20 +02:00
|
|
|
uint32_t upid = OSGetUPID();
|
|
|
|
if (upid != 2 && upid != 15) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_APPLICATION_REQUESTS_EXIT);
|
2021-03-16 17:55:32 +01:00
|
|
|
}
|
|
|
|
|
2020-12-28 14:38:08 +01:00
|
|
|
WUMS_APPLICATION_ENDS() {
|
2022-05-14 14:00:20 +02:00
|
|
|
uint32_t upid = OSGetUPID();
|
|
|
|
if (upid != 2 && upid != 15) {
|
|
|
|
return;
|
2022-02-11 22:18:56 +01:00
|
|
|
}
|
2022-05-14 14:00:20 +02:00
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_APPLICATION_ENDS);
|
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_FINI_WUT_SOCKETS);
|
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_FINI_WUT_DEVOPTAB);
|
2022-02-11 22:19:27 +01:00
|
|
|
|
2022-10-05 20:08:49 +02:00
|
|
|
for (auto &pair : gUsedRPLs) {
|
|
|
|
OSDynLoad_Release(pair.second);
|
|
|
|
}
|
|
|
|
gUsedRPLs.clear();
|
|
|
|
|
2022-02-14 20:24:41 +01:00
|
|
|
deinitLogging();
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
|
2021-04-01 00:37:22 +02:00
|
|
|
|
2020-06-03 18:21:43 +02:00
|
|
|
WUMS_APPLICATION_STARTS() {
|
2020-05-28 20:49:52 +02:00
|
|
|
uint32_t upid = OSGetUPID();
|
|
|
|
if (upid != 2 && upid != 15) {
|
2020-06-03 18:21:43 +02:00
|
|
|
return;
|
2020-05-28 20:49:52 +02:00
|
|
|
}
|
2022-10-03 21:58:11 +02:00
|
|
|
|
2022-09-19 16:06:55 +02:00
|
|
|
OSReport("Running WiiUPluginLoaderBackend " VERSION_FULL "\n");
|
2022-10-05 20:08:49 +02:00
|
|
|
|
|
|
|
gUsedRPLs.clear();
|
|
|
|
|
|
|
|
// If an allocated rpl was not released properly (e.g. if something else calls OSDynload_Acquire without releasing it) memory get leaked.
|
|
|
|
// Let's clean this up!
|
|
|
|
for (auto &addr : gAllocatedAddresses) {
|
|
|
|
DEBUG_FUNCTION_LINE_WARN("Memory allocated by OSDynload was not freed properly, let's clean it up! (%08X)", addr);
|
|
|
|
free((void *) addr);
|
|
|
|
}
|
|
|
|
gAllocatedAddresses.clear();
|
|
|
|
|
2022-01-23 21:43:54 +01:00
|
|
|
initLogging();
|
2020-05-03 10:21:05 +02:00
|
|
|
bool initNeeded = false;
|
2020-05-17 20:49:31 +02:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
std::lock_guard<std::mutex> lock(gLoadedDataMutex);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
if (gTrampData == nullptr) {
|
|
|
|
gTrampData = (relocation_trampoline_entry_t *) memalign(0x4, sizeof(relocation_trampoline_entry_t) * TRAMP_DATA_SIZE);
|
|
|
|
if (gTrampData == nullptr) {
|
|
|
|
DEBUG_FUNCTION_LINE_ERR("Failed to allocated the memory for the trampoline data");
|
|
|
|
OSFatal("Failed to allocated the memory for the trampoline data");
|
|
|
|
}
|
2022-10-03 21:58:11 +02:00
|
|
|
memset(gTrampData, 0, sizeof(relocation_trampoline_entry_t) * TRAMP_DATA_SIZE);
|
2022-05-14 14:00:20 +02:00
|
|
|
}
|
2021-04-01 00:37:22 +02:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
if (gLoadedPlugins.empty()) {
|
|
|
|
auto pluginPath = getPluginPath();
|
2021-01-10 13:17:47 +01:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Load plugins from %s", pluginPath.c_str());
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
auto pluginData = PluginDataFactory::loadDir(pluginPath);
|
2022-10-03 21:58:11 +02:00
|
|
|
gLoadedPlugins = PluginManagement::loadPlugins(pluginData, gTrampData, TRAMP_DATA_SIZE);
|
2022-05-14 14:00:20 +02:00
|
|
|
|
|
|
|
initNeeded = true;
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
2020-05-17 20:49:31 +02:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
if (!gLoadOnNextLaunch.empty()) {
|
2022-12-30 21:08:22 +01:00
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_DEINIT_PLUGIN);
|
2022-05-14 14:00:20 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Restore function patches of currently loaded plugins.");
|
|
|
|
PluginManagement::RestoreFunctionPatches(gLoadedPlugins);
|
|
|
|
DEBUG_FUNCTION_LINE("Unload existing plugins.");
|
|
|
|
gLoadedPlugins.clear();
|
2022-10-03 21:58:11 +02:00
|
|
|
memset(gTrampData, 0, sizeof(relocation_trampoline_entry_t) * TRAMP_DATA_SIZE);
|
2020-05-17 20:49:31 +02:00
|
|
|
|
2022-10-03 21:58:11 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Load new plugins");
|
2022-05-14 14:00:20 +02:00
|
|
|
gLoadedPlugins = PluginManagement::loadPlugins(gLoadOnNextLaunch, gTrampData, TRAMP_DATA_SIZE);
|
|
|
|
initNeeded = true;
|
|
|
|
}
|
2020-05-17 20:49:31 +02:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
DEBUG_FUNCTION_LINE("Clear plugin data lists.");
|
|
|
|
gLoadOnNextLaunch.clear();
|
|
|
|
gLoadedData.clear();
|
2020-05-17 20:49:31 +02:00
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
if (!gLoadedPlugins.empty()) {
|
2022-10-05 20:08:49 +02:00
|
|
|
if (!PluginManagement::doRelocations(gLoadedPlugins, gTrampData, TRAMP_DATA_SIZE, gUsedRPLs)) {
|
2022-05-14 14:00:20 +02:00
|
|
|
DEBUG_FUNCTION_LINE_ERR("Relocations failed");
|
|
|
|
OSFatal("Relocations failed");
|
2020-05-17 20:49:31 +02:00
|
|
|
}
|
2020-06-03 19:33:09 +02:00
|
|
|
// PluginManagement::memsetBSS(plugins);
|
2020-04-29 18:02:36 +02:00
|
|
|
|
2022-02-11 22:19:27 +01:00
|
|
|
if (initNeeded) {
|
2022-05-14 14:00:20 +02:00
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_INIT_WUT_MALLOC);
|
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_INIT_WUT_NEWLIB);
|
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_INIT_WUT_STDCPP);
|
2022-02-11 22:19:27 +01:00
|
|
|
}
|
2022-05-14 14:00:20 +02:00
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_INIT_WUT_DEVOPTAB);
|
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_INIT_WUT_SOCKETS);
|
2021-03-16 17:55:32 +01:00
|
|
|
|
2022-02-11 22:19:27 +01:00
|
|
|
if (initNeeded) {
|
2022-05-14 14:00:20 +02:00
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_INIT_WRAPPER);
|
2022-02-11 22:19:27 +01:00
|
|
|
}
|
2022-01-23 21:16:38 +01:00
|
|
|
|
2020-05-03 10:21:05 +02:00
|
|
|
if (initNeeded) {
|
2022-05-14 14:00:20 +02:00
|
|
|
PluginManagement::callInitHooks(gLoadedPlugins);
|
2020-04-29 18:02:36 +02:00
|
|
|
}
|
|
|
|
|
2022-05-14 14:00:20 +02:00
|
|
|
CallHook(gLoadedPlugins, WUPS_LOADER_HOOK_APPLICATION_STARTS);
|
2021-04-01 00:37:22 +02:00
|
|
|
}
|
2022-05-14 14:00:20 +02:00
|
|
|
}
|