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"
|
2021-01-17 13:57:57 +01:00
|
|
|
#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
|
|
|
|
|
|
|
|
2021-01-17 13:57:57 +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-04-05 19:54:50 +02:00
|
|
|
if (gReplacementInfo.rpxReplacementInfo.isRPXReplaced) {
|
2021-01-17 13:57:57 +01:00
|
|
|
memset(&metaxml->longname_ja, 0, 0x338C - 0x38C); // clear all names
|
2021-04-05 19:54:50 +02:00
|
|
|
|
|
|
|
snprintf(metaxml->longname_en, sizeof(metaxml->longname_en), "%s", gReplacementInfo.rpxReplacementInfo.metaInformation.longname);
|
|
|
|
snprintf(metaxml->shortname_en, sizeof(metaxml->shortname_en), "%s", gReplacementInfo.rpxReplacementInfo.metaInformation.longname);
|
|
|
|
snprintf(metaxml->publisher_en, sizeof(metaxml->publisher_en), "%s", gReplacementInfo.rpxReplacementInfo.metaInformation.longname);
|
2021-01-24 15:45:30 +01:00
|
|
|
|
|
|
|
// Disbale the emanual
|
2021-01-17 13:57:57 +01:00
|
|
|
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-04-05 19:54:50 +02:00
|
|
|
if (gReplacementInfo.rpxReplacementInfo.isRPXReplaced) {
|
2021-01-17 13:57:57 +01:00
|
|
|
if (StringTools::EndsWith(path, iconTex)) {
|
2021-04-05 19:54:50 +02:00
|
|
|
auto *reader = new FileReader(reinterpret_cast<uint8_t *>(gReplacementInfo.rpxReplacementInfo.iconCache), sizeof(gReplacementInfo.rpxReplacementInfo.iconCache));
|
2021-01-17 13:57:57 +01:00
|
|
|
*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);
|
|
|
|
}
|
|
|
|
|
2021-03-13 13:58:34 +01:00
|
|
|
DECL_FUNCTION(void, Loader_ReportWarn) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-17 13:57:57 +01:00
|
|
|
function_replacement_data_t rpx_utils_function_replacements[] = {
|
2021-03-13 13:58:34 +01:00
|
|
|
REPLACE_FUNCTION_VIA_ADDRESS(Loader_ReportWarn, 0x32002f74, 0x01002f74),
|
2021-01-17 13:57:57 +01:00
|
|
|
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;
|
2021-01-17 13:57:57 +01:00
|
|
|
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
|
|
|
|
if (MATCH("menu", "longname")) {
|
2021-04-05 19:54:50 +02:00
|
|
|
fInfo->longname[0] = '\0';
|
|
|
|
strncat(fInfo->longname, value, sizeof(fInfo->longname) - 1);
|
2021-01-17 13:57:57 +01:00
|
|
|
} else if (MATCH("menu", "shortname")) {
|
2021-04-05 19:54:50 +02:00
|
|
|
fInfo->shortname[0] = '\0';
|
|
|
|
strncat(fInfo->shortname, value, sizeof(fInfo->shortname) - 1);
|
2021-01-17 13:57:57 +01:00
|
|
|
} else if (MATCH("menu", "author")) {
|
2021-04-05 19:54:50 +02:00
|
|
|
fInfo->author[0] = '\0';
|
|
|
|
strncat(fInfo->author, value, sizeof(fInfo->author) - 1);
|
2021-01-17 13:57:57 +01:00
|
|
|
} 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
|
2021-04-05 19:54:50 +02:00
|
|
|
request.filesize = 0; // unknown filesize
|
2021-01-01 01:39:28 +01:00
|
|
|
request.fileoffset = 0; //
|
|
|
|
|
|
|
|
romfs_fileInfo info;
|
|
|
|
|
2021-01-17 13:57:57 +01:00
|
|
|
bool metaLoaded = false;
|
|
|
|
|
|
|
|
std::string completePath = std::string("/vol/external01/") + bundle_path;
|
2021-01-01 01:39:28 +01:00
|
|
|
int res = getRPXInfoForPath(completePath, &info);
|
2021-01-09 21:23:48 +01:00
|
|
|
bool isBundle = false;
|
2021-01-01 01:39:28 +01:00
|
|
|
if (res >= 0) {
|
2021-01-09 21:23:48 +01:00
|
|
|
isBundle = true;
|
2021-01-17 13:57:57 +01:00
|
|
|
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-04-05 19:54:50 +02:00
|
|
|
if (ini_parse("rcc:/meta/meta.ini", parseINIhandler, &gReplacementInfo.rpxReplacementInfo.metaInformation) < 0) {
|
2021-01-17 13:57:57 +01:00
|
|
|
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-04-05 19:54:50 +02:00
|
|
|
uint32_t toRead = sizeof(gReplacementInfo.rpxReplacementInfo.iconCache);
|
2021-01-17 13:57:57 +01:00
|
|
|
do {
|
2021-04-05 19:54:50 +02:00
|
|
|
int read = reader->read(reinterpret_cast<uint8_t *>(&gReplacementInfo.rpxReplacementInfo.iconCache[alreadyRead]), toRead);
|
2021-01-17 13:57:57 +01:00
|
|
|
if (read <= 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
alreadyRead += read;
|
|
|
|
toRead -= read;
|
2021-04-05 19:54:50 +02:00
|
|
|
} while (alreadyRead < sizeof(gReplacementInfo.rpxReplacementInfo.iconCache));
|
2021-01-17 13:57:57 +01:00
|
|
|
delete reader;
|
|
|
|
} else {
|
2021-04-05 19:54:50 +02:00
|
|
|
memset(gReplacementInfo.rpxReplacementInfo.iconCache, 0, sizeof(gReplacementInfo.rpxReplacementInfo.iconCache));
|
2021-01-17 13:57:57 +01:00
|
|
|
}
|
|
|
|
romfsUnmount("rcc");
|
|
|
|
}
|
2021-04-05 19:54:50 +02:00
|
|
|
} else {
|
|
|
|
if (!(gReplacementInfo.contentReplacementInfo.mode == CONTENTREDIRECT_FROM_WUHB_BUNDLE &&
|
|
|
|
gReplacementInfo.contentReplacementInfo.bundleMountInformation.isMounted)) {
|
|
|
|
memset(gReplacementInfo.rpxReplacementInfo.iconCache, 0, sizeof(gReplacementInfo.rpxReplacementInfo.iconCache));
|
|
|
|
}
|
2021-01-01 01:39:28 +01:00
|
|
|
}
|
|
|
|
|
2021-01-17 13:57:57 +01:00
|
|
|
if (!metaLoaded) {
|
2021-04-05 19:54:50 +02:00
|
|
|
gReplacementInfo.rpxReplacementInfo.metaInformation.author[0] = '\0';
|
|
|
|
gReplacementInfo.rpxReplacementInfo.metaInformation.shortname[0] = '\0';
|
|
|
|
gReplacementInfo.rpxReplacementInfo.metaInformation.longname[0] = '\0';
|
|
|
|
|
|
|
|
strncat(gReplacementInfo.rpxReplacementInfo.metaInformation.author, bundle_path, sizeof(gReplacementInfo.rpxReplacementInfo.metaInformation.author) - 1);
|
|
|
|
strncat(gReplacementInfo.rpxReplacementInfo.metaInformation.shortname, bundle_path, sizeof(gReplacementInfo.rpxReplacementInfo.metaInformation.shortname) - 1);
|
|
|
|
strncat(gReplacementInfo.rpxReplacementInfo.metaInformation.longname, bundle_path, sizeof(gReplacementInfo.rpxReplacementInfo.metaInformation.longname) - 1);
|
2021-01-17 13:57:57 +01:00
|
|
|
}
|
|
|
|
|
2021-04-05 19:54:50 +02:00
|
|
|
request.path[0] = '\0';
|
|
|
|
strncat(request.path, bundle_path, sizeof(request.path) - 1);
|
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) {
|
2021-04-13 20:14:50 +02:00
|
|
|
gReplacementInfo.rpxReplacementInfo.willRPXBeReplaced = false;
|
2021-02-19 20:32:44 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Failed to load %s on next restart", request.path);
|
|
|
|
return false;
|
2021-04-05 19:54:50 +02:00
|
|
|
} else {
|
2021-04-13 20:14:50 +02:00
|
|
|
gReplacementInfo.rpxReplacementInfo.willRPXBeReplaced = true;
|
2021-02-19 20:32:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
DEBUG_FUNCTION_LINE("Launch %s on next restart [size: %08X offset: %08X]", request.path, request.filesize, request.fileoffset);
|
|
|
|
|
2021-01-17 13:57:57 +01:00
|
|
|
if (isBundle) {
|
2021-01-24 15:45:30 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Loaded file is a .wuhb bundle");
|
2021-04-05 19:54:50 +02:00
|
|
|
gReplacementInfo.contentReplacementInfo.bundleMountInformation.toMountPath[0] = '\0';
|
|
|
|
strncat(gReplacementInfo.contentReplacementInfo.bundleMountInformation.toMountPath,
|
|
|
|
completePath.c_str(),
|
|
|
|
sizeof(gReplacementInfo.contentReplacementInfo.bundleMountInformation.toMountPath) - 1);
|
|
|
|
gReplacementInfo.contentReplacementInfo.mode = CONTENTREDIRECT_FROM_WUHB_BUNDLE;
|
2021-01-17 13:57:57 +01:00
|
|
|
} else {
|
2021-01-24 15:45:30 +01:00
|
|
|
DEBUG_FUNCTION_LINE("Loaded file is no bundle");
|
2021-04-13 20:14:50 +02:00
|
|
|
gReplacementInfo.rpxReplacementInfo.willRPXBeReplaced = true;
|
2021-04-05 19:54:50 +02:00
|
|
|
|
|
|
|
if (gReplacementInfo.contentReplacementInfo.mode == CONTENTREDIRECT_FROM_WUHB_BUNDLE &&
|
|
|
|
gReplacementInfo.contentReplacementInfo.bundleMountInformation.isMounted) {
|
2021-01-09 21:23:48 +01:00
|
|
|
// keep the old /vol/content mounted, this way you can reload just the rpx via wiiload
|
2021-04-05 19:54:50 +02:00
|
|
|
gReplacementInfo.contentReplacementInfo.bundleMountInformation.toMountPath[0] = '\0';
|
|
|
|
strncat(gReplacementInfo.contentReplacementInfo.bundleMountInformation.toMountPath,
|
|
|
|
gReplacementInfo.contentReplacementInfo.bundleMountInformation.mountedPath,
|
|
|
|
sizeof(gReplacementInfo.contentReplacementInfo.bundleMountInformation.toMountPath) - 1);
|
2021-01-09 21:23:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2021-01-17 13:57:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2021-01-17 13:57:57 +01:00
|
|
|
std::string checkgz = std::string(name) + ".gz";
|
|
|
|
return CheckFile(name) || CheckFile(checkgz.c_str());
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:32:40 +02:00
|
|
|
bool RL_RedirectContentWithFallback(const char * newContentPath) {
|
|
|
|
auto dirHandle = opendir(newContentPath);
|
|
|
|
if (dirHandle == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
closedir(dirHandle);
|
|
|
|
|
|
|
|
gReplacementInfo.contentReplacementInfo.replacementPath[0] = '\0';
|
|
|
|
strncat(gReplacementInfo.contentReplacementInfo.replacementPath, newContentPath, sizeof(gReplacementInfo.contentReplacementInfo.replacementPath) - 1);
|
|
|
|
gReplacementInfo.contentReplacementInfo.mode = CONTENTREDIRECT_FROM_PATH;
|
|
|
|
gReplacementInfo.contentReplacementInfo.fallbackOnError = true;
|
|
|
|
gReplacementInfo.contentReplacementInfo.replaceSave = false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-04-05 21:39:01 +02:00
|
|
|
bool RL_DisableContentRedirection() {
|
|
|
|
if (gReplacementInfo.contentReplacementInfo.mode == CONTENTREDIRECT_FROM_PATH) {
|
|
|
|
gReplacementInfo.contentReplacementInfo.mode = CONTENTREDIRECT_NONE;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-17 13:57:57 +01:00
|
|
|
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);
|
2021-04-05 21:32:40 +02:00
|
|
|
WUMS_EXPORT_FUNCTION(RL_FileExists);
|
2021-04-05 21:39:01 +02:00
|
|
|
WUMS_EXPORT_FUNCTION(RL_RedirectContentWithFallback);
|
|
|
|
WUMS_EXPORT_FUNCTION(RL_DisableContentRedirection);
|