RPXLoadingModule/src/RPXLoading.cpp

267 lines
10 KiB
C++
Raw Normal View History

2021-01-01 01:39:28 +01:00
#include <string>
#include <coreinit/cache.h>
#include <coreinit/ios.h>
#include <romfs_dev.h>
#include "utils/logger.h"
#include "RPXLoading.h"
#include "globals.h"
#include "FileUtils.h"
#include <nn/acp/title.h>
#include <memory>
#include <algorithm>
#include "utils/FileReader.h"
#include "utils/FileReaderCompressed.h"
#include "utils/StringTools.h"
#include "utils/ini.h"
#include <cstring>
2021-01-24 14:06:25 +01:00
#include <rpxloader.h>
2021-01-01 01:39:28 +01:00
/*
* Patch the meta xml for the home menu
*/
DECL_FUNCTION(int32_t, HBM_NN_ACP_ACPGetTitleMetaXmlByDevice, uint32_t titleid_upper, uint32_t titleid_lower, ACPMetaXml *metaxml, uint32_t device) {
2021-02-19 20:32:44 +01:00
if (gReplacementInfo.isRPXReplaced) {
memset(&metaxml->longname_ja, 0, 0x338C - 0x38C); // clear all names
2021-02-19 20:32:44 +01:00
strncpy(metaxml->longname_en, gReplacementInfo.metaInformation.longname, sizeof(gReplacementInfo.metaInformation.longname));
strncpy(metaxml->shortname_en, gReplacementInfo.metaInformation.shortname, sizeof(gReplacementInfo.metaInformation.shortname));
strncpy(metaxml->publisher_en, gReplacementInfo.metaInformation.author, sizeof(gReplacementInfo.metaInformation.author));
2021-01-24 15:45:30 +01:00
// Disbale the emanual
metaxml->e_manual = 0;
return 0;
}
int result = real_HBM_NN_ACP_ACPGetTitleMetaXmlByDevice(titleid_upper, titleid_lower, metaxml, device);
return result;
}
DECL_FUNCTION(int, RPX_FSOpenFile, FSClient *client, FSCmdBlock *block, char *path, const char *mode, int *handle, int error) {
const char *iconTex = "iconTex.tga";
if (StringTools::EndsWith(path, iconTex)) {
2021-02-19 20:32:44 +01:00
if (gReplacementInfo.isRPXReplaced) {
if (StringTools::EndsWith(path, iconTex)) {
2021-02-19 20:32:44 +01:00
auto *reader = new FileReader(reinterpret_cast<uint8_t *>(gReplacementInfo.iconCache), sizeof(gReplacementInfo.iconCache));
*handle = (uint32_t) reader;
return FS_STATUS_OK;
}
}
}
int result = real_RPX_FSOpenFile(client, block, path, mode, handle, error);
return result;
}
DECL_FUNCTION(FSStatus, RPX_FSReadFile, FSClient *client, FSCmdBlock *block, uint8_t *buffer, uint32_t size, uint32_t count, FSFileHandle handle, uint32_t unk1, uint32_t flags) {
// We check if the handle is part of our heap (the MemoryMapping Module allocates to 0x80000000)
if ((handle & 0xF0000000) == 0x80000000) {
auto reader = (FileReader *) handle;
return (FSStatus) reader->read(buffer, size * count);
}
FSStatus result = real_RPX_FSReadFile(client, block, buffer, size, count, handle, unk1, flags);
return result;
}
DECL_FUNCTION(FSStatus, RPX_FSCloseFile, FSClient *client, FSCmdBlock *block, FSFileHandle handle, uint32_t flags) {
// We check if the handle is part of our heap (the MemoryMapping Module allocates to 0x80000000)
if ((handle & 0xF0000000) == 0x80000000) {
auto reader = (FileReader *) handle;
delete reader;
return FS_STATUS_OK;
}
return real_RPX_FSCloseFile(client, block, handle, flags);
}
function_replacement_data_t rpx_utils_function_replacements[] = {
REPLACE_FUNCTION_VIA_ADDRESS_FOR_PROCESS(HBM_NN_ACP_ACPGetTitleMetaXmlByDevice, 0x2E36CE44, 0x0E36CE44, FP_TARGET_PROCESS_HOME_MENU),
REPLACE_FUNCTION_FOR_PROCESS(RPX_FSOpenFile, LIBRARY_COREINIT, FSOpenFile, FP_TARGET_PROCESS_HOME_MENU),
REPLACE_FUNCTION_FOR_PROCESS(RPX_FSReadFile, LIBRARY_COREINIT, FSReadFile, FP_TARGET_PROCESS_HOME_MENU),
REPLACE_FUNCTION_FOR_PROCESS(RPX_FSCloseFile, LIBRARY_COREINIT, FSCloseFile, FP_TARGET_PROCESS_HOME_MENU),
};
uint32_t rpx_utils_function_replacements_size = sizeof(rpx_utils_function_replacements) / sizeof(function_replacement_data_t);
static int parseINIhandler(void *user, const char *section, const char *name,
const char *value) {
2021-02-19 20:32:44 +01:00
auto *fInfo = (MetaInformation *) user;
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
if (MATCH("menu", "longname")) {
strncpy(fInfo->longname, value, 64 - 1);
} else if (MATCH("menu", "shortname")) {
strncpy(fInfo->shortname, value, 64 - 1);
} else if (MATCH("menu", "author")) {
strncpy(fInfo->author, value, 64 - 1);
} else {
return 0; /* unknown section/name, error */
}
return 1;
}
bool RL_LoadFromSDOnNextLaunch(const char *bundle_path) {
2021-01-01 01:39:28 +01:00
LOAD_REQUEST request;
memset(&request, 0, sizeof(request));
request.command = 0xFC; // IPC_CUSTOM_LOAD_CUSTOM_RPX;
request.target = 0; // LOAD_FILE_TARGET_SD_CARD
request.filesize = 0; // unknown
request.fileoffset = 0; //
romfs_fileInfo info;
bool metaLoaded = false;
2021-02-19 20:32:44 +01:00
gReplacementInfo.replacementType = RPXLoader_RPX;
std::string completePath = std::string("/vol/external01/") + bundle_path;
2021-01-01 01:39:28 +01:00
int res = getRPXInfoForPath(completePath, &info);
bool isBundle = false;
2021-01-01 01:39:28 +01:00
if (res >= 0) {
2021-02-19 20:32:44 +01:00
gReplacementInfo.replacementType = RPXLoader_BUNDLE;
isBundle = true;
request.filesize = ((uint32_t *) &info.length)[1];
request.fileoffset = ((uint32_t *) &info.offset)[1];
if (romfsMount("rcc", completePath.c_str(), RomfsSource_FileDescriptor_CafeOS) == 0) {
2021-02-19 20:32:44 +01:00
if (ini_parse("rcc:/meta/meta.ini", parseINIhandler, &gReplacementInfo.metaInformation) < 0) {
DEBUG_FUNCTION_LINE("Failed to load and parse meta.ini");
} else {
metaLoaded = true;
}
FileReader *reader = nullptr;
if (CheckFile("rcc:/meta/iconTex.tga")) {
std::string path = "rcc:/meta/iconTex.tga";
reader = new FileReader(path);
} else if (CheckFile("rcc:/meta/iconTex.tga.gz")) {
std::string path = "rcc:/meta/iconTex.tga.gz";
reader = new FileReaderCompressed(path);
}
if (reader) {
uint32_t alreadyRead = 0;
2021-02-19 20:32:44 +01:00
uint32_t toRead = sizeof(gReplacementInfo.iconCache);
do {
2021-02-19 20:32:44 +01:00
int read = reader->read(reinterpret_cast<uint8_t *>(&gReplacementInfo.iconCache[alreadyRead]), toRead);
if (read <= 0) {
break;
}
alreadyRead += read;
toRead -= read;
2021-02-19 20:32:44 +01:00
} while (alreadyRead < sizeof(gReplacementInfo.iconCache));
delete reader;
} else {
2021-02-19 20:32:44 +01:00
memset(gReplacementInfo.iconCache, 0, sizeof(gReplacementInfo.iconCache));
}
romfsUnmount("rcc");
}
2021-01-01 01:39:28 +01:00
}
if (!metaLoaded) {
2021-02-19 20:32:44 +01:00
strncpy(gReplacementInfo.metaInformation.author, bundle_path, sizeof(gReplacementInfo.metaInformation.author));
strncpy(gReplacementInfo.metaInformation.shortname, bundle_path, sizeof(gReplacementInfo.metaInformation.shortname));
strncpy(gReplacementInfo.metaInformation.longname, bundle_path, sizeof(gReplacementInfo.metaInformation.longname));
}
strncpy(request.path, bundle_path, 255);
2021-01-01 01:39:28 +01:00
DCFlushRange(&request, sizeof(LOAD_REQUEST));
2021-02-19 20:32:44 +01:00
int success = false;
2021-01-01 01:39:28 +01:00
int mcpFd = IOS_Open("/dev/mcp", (IOSOpenMode) 0);
if (mcpFd >= 0) {
int out = 0;
IOS_Ioctl(mcpFd, 100, &request, sizeof(request), &out, sizeof(out));
2021-02-19 20:32:44 +01:00
if (out > 0) {
success = true;
}
2021-01-01 01:39:28 +01:00
IOS_Close(mcpFd);
}
2021-02-19 20:32:44 +01:00
DCFlushRange(&gReplacementInfo, sizeof(gReplacementInfo));
if (!success) {
gReplacementInfo.replacementType = RPXLoader_NONE;
DEBUG_FUNCTION_LINE("Failed to load %s on next restart", request.path);
return false;
}
DEBUG_FUNCTION_LINE("Launch %s on next restart [size: %08X offset: %08X]", request.path, request.filesize, request.fileoffset);
if (isBundle) {
2021-01-24 15:45:30 +01:00
DEBUG_FUNCTION_LINE("Loaded file is a .wuhb bundle");
2021-02-19 20:32:44 +01:00
memset(gReplacementInfo.bundleMountInformation.path, 0, sizeof(gReplacementInfo.bundleMountInformation.path));
strncpy(gReplacementInfo.bundleMountInformation.path, completePath.c_str(), completePath.length());
gReplacementInfo.replacementType = RPXLoader_BUNDLE;
gReplacementInfo.bundleMountInformation.redirectionRequested = true;
} else {
2021-01-24 15:45:30 +01:00
DEBUG_FUNCTION_LINE("Loaded file is no bundle");
2021-02-19 20:32:44 +01:00
if (!gReplacementInfo.bundleMountInformation.isMounted) {
memset(gReplacementInfo.bundleMountInformation.path, 0, sizeof(gReplacementInfo.bundleMountInformation.path));
} else {
2021-02-19 20:32:44 +01:00
gReplacementInfo.replacementType = RPXLoader_BUNDLE_OTHER_RPX;
gReplacementInfo.bundleMountInformation.redirectionRequested = true;
// keep the old /vol/content mounted, this way you can reload just the rpx via wiiload
}
}
2021-02-19 20:32:44 +01:00
DCFlushRange(&gReplacementInfo, sizeof(gReplacementInfo));
2021-01-01 01:39:28 +01:00
return true;
}
2021-01-24 14:06:25 +01:00
int32_t RL_MountBundle(const char *name, const char *path, BundleSource source) {
return romfsMount(name, path, (RomfsSource) source);
}
int32_t RL_UnmountBundle(const char *name) {
return romfsUnmount(name);
}
int32_t RL_FileOpen(const char *name, uint32_t *handle) {
if (handle == nullptr) {
return -1;
}
FileReader *reader = nullptr;
std::string path = std::string(name);
std::string pathGZ = path + ".gz";
if (CheckFile(path.c_str())) {
reader = new FileReader(path);
} else if (CheckFile(pathGZ.c_str())) {
reader = new FileReaderCompressed(pathGZ);
} else {
return -2;
}
if (reader == nullptr) {
return -3;
}
*handle = (uint32_t) reader;
return 0;
}
int32_t RL_FileRead(uint32_t handle, uint8_t *buffer, uint32_t size) {
auto reader = (FileReader *) handle;
return reader->read(buffer, size);
}
int32_t RL_FileClose(uint32_t handle) {
auto reader = (FileReader *) handle;
delete reader;
return 0;
}
2021-01-24 14:06:25 +01:00
bool RL_FileExists(const char *name) {
std::string checkgz = std::string(name) + ".gz";
return CheckFile(name) || CheckFile(checkgz.c_str());
}
WUMS_EXPORT_FUNCTION(RL_LoadFromSDOnNextLaunch);
WUMS_EXPORT_FUNCTION(RL_MountBundle);
WUMS_EXPORT_FUNCTION(RL_UnmountBundle);
WUMS_EXPORT_FUNCTION(RL_FileOpen);
WUMS_EXPORT_FUNCTION(RL_FileRead);
WUMS_EXPORT_FUNCTION(RL_FileClose);
WUMS_EXPORT_FUNCTION(RL_FileExists);