WiiUPluginLoaderBackend/source/utils/utils.cpp

86 lines
2.6 KiB
C++
Raw Normal View History

#include "utils.h"
#include "globals.h"
#include "logger.h"
#include <algorithm>
#include <coreinit/ios.h>
#include <string>
std::string getPluginPath() {
char environmentPath[0x100];
memset(environmentPath, 0, sizeof(environmentPath));
auto handle = IOS_Open("/dev/mcp", IOS_OPEN_READ);
if (handle >= 0) {
int in = 0xF9; // IPC_CUSTOM_COPY_ENVIRONMENT_PATH
if (IOS_Ioctl(handle, 100, &in, sizeof(in), environmentPath, sizeof(environmentPath)) != IOS_ERROR_OK) {
return "fs:/vol/external01/wiiu/plugins";
}
IOS_Close(handle);
}
return std::string(environmentPath).append("/plugins");
}
2019-08-15 10:45:18 +02:00
// https://gist.github.com/ccbrown/9722406
2020-05-03 12:30:15 +02:00
void dumpHex(const void *data, size_t size) {
2019-08-15 10:45:18 +02:00
char ascii[17];
size_t i, j;
ascii[16] = '\0';
DEBUG_FUNCTION_LINE("0x%08X (0x0000): ", data);
for (i = 0; i < size; ++i) {
2020-05-08 11:17:24 +02:00
WHBLogWritef("%02X ", ((unsigned char *) data)[i]);
2020-05-03 12:30:15 +02:00
if (((unsigned char *) data)[i] >= ' ' && ((unsigned char *) data)[i] <= '~') {
ascii[i % 16] = ((unsigned char *) data)[i];
2019-08-15 10:45:18 +02:00
} else {
ascii[i % 16] = '.';
}
2020-05-03 12:30:15 +02:00
if ((i + 1) % 8 == 0 || i + 1 == size) {
2020-05-08 11:17:24 +02:00
WHBLogWritef(" ");
2020-05-03 12:30:15 +02:00
if ((i + 1) % 16 == 0) {
2020-05-08 11:17:24 +02:00
WHBLogPrintf("| %s ", ascii);
2020-05-03 12:30:15 +02:00
if (i + 1 < size) {
DEBUG_FUNCTION_LINE("0x%08X (0x%04X); ", ((uint32_t) data) + i + 1, i + 1);
2019-08-15 10:45:18 +02:00
}
2020-05-03 12:30:15 +02:00
} else if (i + 1 == size) {
ascii[(i + 1) % 16] = '\0';
if ((i + 1) % 16 <= 8) {
2020-05-08 11:17:24 +02:00
WHBLogWritef(" ");
2019-08-15 10:45:18 +02:00
}
2020-05-03 12:30:15 +02:00
for (j = (i + 1) % 16; j < 16; ++j) {
2020-05-08 11:17:24 +02:00
WHBLogWritef(" ");
2019-08-15 10:45:18 +02:00
}
2020-05-08 11:17:24 +02:00
WHBLogPrintf("| %s ", ascii);
2019-08-15 10:45:18 +02:00
}
}
}
}
OSDynLoad_Error CustomDynLoadAlloc(int32_t size, int32_t align, void **outAddr) {
if (!outAddr) {
return OS_DYNLOAD_INVALID_ALLOCATOR_PTR;
}
if (align >= 0 && align < 4) {
align = 4;
} else if (align < 0 && align > -4) {
align = -4;
}
if (!(*outAddr = memalign(align, size))) {
return OS_DYNLOAD_OUT_OF_MEMORY;
}
// keep track of allocated memory to clean it up if RPLs won't get unloaded properly
gAllocatedAddresses.push_back(*outAddr);
return OS_DYNLOAD_OK;
}
void CustomDynLoadFree(void *addr) {
free(addr);
// Remove from list
auto it = std::find(gAllocatedAddresses.begin(), gAllocatedAddresses.end(), addr);
if (it != gAllocatedAddresses.end()) {
gAllocatedAddresses.erase(it);
}
}