Tidy up the code a bit, fix some memory leaks

This commit is contained in:
Maschell 2021-01-01 01:50:00 +01:00
parent 08a4176418
commit 0e8d1c4c76
5 changed files with 46 additions and 21 deletions

View File

@ -7,7 +7,7 @@
CFile::CFile() { CFile::CFile() {
iFd = -1; iFd = -1;
mem_file = NULL; mem_file = nullptr;
filesize = 0; filesize = 0;
pos = 0; pos = 0;
} }

View File

@ -63,9 +63,9 @@ public:
protected: protected:
int32_t iFd; int32_t iFd;
const uint8_t *mem_file; const uint8_t *mem_file{};
uint64_t filesize; uint64_t filesize{};
uint64_t pos; uint64_t pos{};
}; };
#endif #endif

View File

@ -44,7 +44,7 @@ int32_t FSUtils::LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_
if (done != filesize) { if (done != filesize) {
free(buffer); free(buffer);
buffer = NULL; buffer = nullptr;
return -3; return -3;
} }
@ -62,7 +62,7 @@ int32_t FSUtils::CheckFile(const char *filepath) {
if (!filepath) if (!filepath)
return 0; return 0;
struct stat filestat; struct stat filestat{};
char dirnoslash[strlen(filepath) + 2]; char dirnoslash[strlen(filepath) + 2];
snprintf(dirnoslash, sizeof(dirnoslash), "%s", filepath); snprintf(dirnoslash, sizeof(dirnoslash), "%s", filepath);

View File

@ -1,28 +1,31 @@
#include <wups.h> #include <wups.h>
#include <cstdio> #include <cstdio>
#include <cstring> #include <cstring>
#include <coreinit/debug.h>
#include <coreinit/title.h> #include <coreinit/title.h>
#include <coreinit/cache.h> #include <coreinit/cache.h>
#include <coreinit/systeminfo.h> #include <coreinit/systeminfo.h>
#include <coreinit/mcp.h> #include <coreinit/mcp.h>
#include <coreinit/filesystem.h> #include <coreinit/filesystem.h>
#include <sysapp/title.h> #include <sysapp/title.h>
#include <coreinit/dynload.h>
#include <nn/acp.h> #include <nn/acp.h>
#include <coreinit/ios.h> #include <coreinit/ios.h>
#include <utils/logger.h> #include <utils/logger.h>
#include "utils/StringTools.h" #include "utils/StringTools.h"
#include <fs/DirList.h> #include <fs/DirList.h>
#include <wut_romfs_dev.h> #include <romfs_dev.h>
#include "readFileWrapper.h" #include "readFileWrapper.h"
#include <whb/log_udp.h> #include <whb/log_udp.h>
#include "fs/FSUtils.h" #include "fs/FSUtils.h"
#include "romfs_helper.h" #include "romfs_helper.h"
#include "filelist.h" #include "filelist.h"
#include "utils/utils.h"
struct _ACPMetaData { typedef struct ACPMetaData {
char bootmovie[80696]; char bootmovie[80696];
char bootlogo[28604]; char bootlogo[28604];
} _ACPMetaData; } ACPMetaData;
WUPS_PLUGIN_NAME("Homebrew in Wii U menu"); WUPS_PLUGIN_NAME("Homebrew in Wii U menu");
WUPS_PLUGIN_DESCRIPTION("Allows the user to load homebrew from the Wii U menu"); WUPS_PLUGIN_DESCRIPTION("Allows the user to load homebrew from the Wii U menu");
@ -31,6 +34,7 @@ WUPS_PLUGIN_AUTHOR("Maschell");
WUPS_PLUGIN_LICENSE("GPL"); WUPS_PLUGIN_LICENSE("GPL");
#define UPPER_TITLE_ID_HOMEBREW 0x0005000F #define UPPER_TITLE_ID_HOMEBREW 0x0005000F
#define TITLE_ID_HOMEBREW_MASK (((uint64_t) UPPER_TITLE_ID_HOMEBREW) << 32) #define TITLE_ID_HOMEBREW_MASK (((uint64_t) UPPER_TITLE_ID_HOMEBREW) << 32)
char gIconCache[65580] __attribute__((section(".data"))); char gIconCache[65580] __attribute__((section(".data")));
@ -49,9 +53,9 @@ INITIALIZE_PLUGIN() {
gHomebrewLaunched = FALSE; gHomebrewLaunched = FALSE;
} }
ON_APPLICATION_START(args) { ON_APPLICATION_START(args) {
WHBLogUdpInit(); WHBLogUdpInit();
DEBUG_FUNCTION_LINE("IN PLUGIN");
if (_SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY) != OSGetTitleID()) { if (_SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY) != OSGetTitleID()) {
DEBUG_FUNCTION_LINE("gHomebrewLaunched to FALSE"); DEBUG_FUNCTION_LINE("gHomebrewLaunched to FALSE");
@ -158,7 +162,6 @@ DECL_FUNCTION(int32_t, MCP_TitleList, uint32_t handle, uint32_t *outTitleCount,
const char *indexedDevice = "mlc"; const char *indexedDevice = "mlc";
strcpy(template_title.indexedDevice, indexedDevice); strcpy(template_title.indexedDevice, indexedDevice);
// System apps don't have a splash screen. // System apps don't have a splash screen.
template_title.appType = MCP_APP_TYPE_SYSTEM_APPS; template_title.appType = MCP_APP_TYPE_SYSTEM_APPS;
@ -392,6 +395,9 @@ DECL_FUNCTION(int32_t, ACPGetTitleMetaDirByDevice, uint32_t titleid_upper, uint3
return result; return result;
} }
/*
* Load the H&S app instead
*/
DECL_FUNCTION(int32_t, _SYSLaunchTitleByPathFromLauncher, char *pathToLoad, uint32_t u2) { DECL_FUNCTION(int32_t, _SYSLaunchTitleByPathFromLauncher, char *pathToLoad, uint32_t u2) {
const char *start = "/custom/"; const char *start = "/custom/";
if (strncmp(pathToLoad, start, strlen(start)) == 0) { if (strncmp(pathToLoad, start, strlen(start)) == 0) {
@ -422,6 +428,9 @@ DECL_FUNCTION(uint32_t, ACPGetApplicationBox, uint32_t *u1, uint32_t *u2, uint32
return result; return result;
} }
/*
* Redirect the launchable check to H&S
*/
DECL_FUNCTION(uint32_t, PatchChkStart__3RplFRCQ3_2nn6drmapp8StartArg, uint32_t *param) { DECL_FUNCTION(uint32_t, PatchChkStart__3RplFRCQ3_2nn6drmapp8StartArg, uint32_t *param) {
if (param[2] == UPPER_TITLE_ID_HOMEBREW) { if (param[2] == UPPER_TITLE_ID_HOMEBREW) {
uint64_t titleID = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY); uint64_t titleID = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY);
@ -432,6 +441,9 @@ DECL_FUNCTION(uint32_t, PatchChkStart__3RplFRCQ3_2nn6drmapp8StartArg, uint32_t *
return result; return result;
} }
/*
* Redirect the launchable check to H&S
*/
DECL_FUNCTION(uint32_t, MCP_RightCheckLaunchable, uint32_t *u1, uint32_t *u2, uint32_t u3, uint32_t u4, uint32_t u5) { DECL_FUNCTION(uint32_t, MCP_RightCheckLaunchable, uint32_t *u1, uint32_t *u2, uint32_t u3, uint32_t u4, uint32_t u5) {
if (u3 == UPPER_TITLE_ID_HOMEBREW) { if (u3 == UPPER_TITLE_ID_HOMEBREW) {
uint64_t titleID = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY); uint64_t titleID = _SYSGetSystemApplicationTitleId(SYSTEM_APP_ID_HEALTH_AND_SAFETY);
@ -442,6 +454,9 @@ DECL_FUNCTION(uint32_t, MCP_RightCheckLaunchable, uint32_t *u1, uint32_t *u2, ui
return result; return result;
} }
/*
* 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) { DECL_FUNCTION(int32_t, HBM_NN_ACP_ACPGetTitleMetaXmlByDevice, uint32_t titleid_upper, uint32_t titleid_lower, ACPMetaXml *metaxml, uint32_t device) {
if (gHomebrewLaunched) { if (gHomebrewLaunched) {
memcpy(metaxml, &gLaunchXML, sizeof(gLaunchXML)); memcpy(metaxml, &gLaunchXML, sizeof(gLaunchXML));
@ -451,26 +466,29 @@ DECL_FUNCTION(int32_t, HBM_NN_ACP_ACPGetTitleMetaXmlByDevice, uint32_t titleid_u
return result; return result;
} }
/*
DECL_FUNCTION(uint32_t, ACPGetLaunchMetaData, struct _ACPMetaData *metadata) { * Patch the boot movie and and boot logo
*/
DECL_FUNCTION(uint32_t, ACPGetLaunchMetaData, struct ACPMetaData *metadata) {
uint32_t result = real_ACPGetLaunchMetaData(metadata); uint32_t result = real_ACPGetLaunchMetaData(metadata);
if (gHomebrewLaunched) { if (gHomebrewLaunched) {
memcpy(metadata->bootmovie, bootMovie_h264, bootMovie_h264_size); memcpy(metadata->bootmovie, bootMovie_h264, bootMovie_h264_size);
memcpy(metadata->bootlogo, bootLogoTex_tga, bootLogoTex_tga_size); memcpy(metadata->bootlogo, bootLogoTex_tga, bootLogoTex_tga_size);
DCFlushRange(metadata->bootmovie, bootMovie_h264_size);
DCFlushRange(metadata->bootlogo, bootMovie_h264_size);
} }
return result; return result;
} }
WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(HBM_NN_ACP_ACPGetTitleMetaXmlByDevice, 0x2E36CE44, 0x0E36CE44, WUPS_FP_TARGET_PROCESS_HOME_MENU); WUPS_MUST_REPLACE_PHYSICAL_FOR_PROCESS(HBM_NN_ACP_ACPGetTitleMetaXmlByDevice, 0x2E36CE44, 0x0E36CE44, WUPS_FP_TARGET_PROCESS_HOME_MENU);
WUPS_MUST_REPLACE(ACPGetApplicationBox, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetApplicationBox); WUPS_MUST_REPLACE(ACPGetApplicationBox, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetApplicationBox);
WUPS_MUST_REPLACE(PatchChkStart__3RplFRCQ3_2nn6drmapp8StartArg, WUPS_LOADER_LIBRARY_DRMAPP, PatchChkStart__3RplFRCQ3_2nn6drmapp8StartArg); WUPS_MUST_REPLACE(PatchChkStart__3RplFRCQ3_2nn6drmapp8StartArg, WUPS_LOADER_LIBRARY_DRMAPP, PatchChkStart__3RplFRCQ3_2nn6drmapp8StartArg);
WUPS_MUST_REPLACE(MCP_RightCheckLaunchable, WUPS_LOADER_LIBRARY_COREINIT, MCP_RightCheckLaunchable); WUPS_MUST_REPLACE(MCP_RightCheckLaunchable, WUPS_LOADER_LIBRARY_COREINIT, MCP_RightCheckLaunchable);
WUPS_MUST_REPLACE(FSReadFile, WUPS_LOADER_LIBRARY_COREINIT, FSReadFile);
WUPS_MUST_REPLACE(FSOpenFile, WUPS_LOADER_LIBRARY_COREINIT, FSOpenFile);
WUPS_MUST_REPLACE(FSCloseFile, WUPS_LOADER_LIBRARY_COREINIT, FSCloseFile);
WUPS_MUST_REPLACE(MCP_TitleList, WUPS_LOADER_LIBRARY_COREINIT, MCP_TitleList); WUPS_MUST_REPLACE(MCP_TitleList, WUPS_LOADER_LIBRARY_COREINIT, MCP_TitleList);
WUPS_MUST_REPLACE(MCP_GetTitleInfoByTitleAndDevice, WUPS_LOADER_LIBRARY_COREINIT, MCP_GetTitleInfoByTitleAndDevice); WUPS_MUST_REPLACE(MCP_GetTitleInfoByTitleAndDevice, WUPS_LOADER_LIBRARY_COREINIT, MCP_GetTitleInfoByTitleAndDevice);
@ -480,3 +498,7 @@ WUPS_MUST_REPLACE(ACPGetLaunchMetaXml, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetLaunchM
WUPS_MUST_REPLACE(ACPGetTitleMetaDirByDevice, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetTitleMetaDirByDevice); WUPS_MUST_REPLACE(ACPGetTitleMetaDirByDevice, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetTitleMetaDirByDevice);
WUPS_MUST_REPLACE(_SYSLaunchTitleByPathFromLauncher, WUPS_LOADER_LIBRARY_SYSAPP, _SYSLaunchTitleByPathFromLauncher); WUPS_MUST_REPLACE(_SYSLaunchTitleByPathFromLauncher, WUPS_LOADER_LIBRARY_SYSAPP, _SYSLaunchTitleByPathFromLauncher);
WUPS_MUST_REPLACE(ACPGetLaunchMetaData, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetLaunchMetaData); WUPS_MUST_REPLACE(ACPGetLaunchMetaData, WUPS_LOADER_LIBRARY_NN_ACP, ACPGetLaunchMetaData);
WUPS_MUST_REPLACE(FSReadFile, WUPS_LOADER_LIBRARY_COREINIT, FSReadFile);
WUPS_MUST_REPLACE(FSOpenFile, WUPS_LOADER_LIBRARY_COREINIT, FSOpenFile);
WUPS_MUST_REPLACE(FSCloseFile, WUPS_LOADER_LIBRARY_COREINIT, FSCloseFile);

View File

@ -1,11 +1,11 @@
#include "readFileWrapper.h" #include "readFileWrapper.h"
#include "fs/FSUtils.h" #include "fs/FSUtils.h"
#include "utils/logger.h" #include "utils/logger.h"
#include <unistd.h>
#include <malloc.h> #include <cstdio>
#include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include "romfs_helper.h" #include "romfs_helper.h"
#include <cstdlib>
fileReadInformation gFileReadInformation[FILE_READ_INFO_SIZE] __attribute__((section(".data"))); fileReadInformation gFileReadInformation[FILE_READ_INFO_SIZE] __attribute__((section(".data")));
@ -117,7 +117,7 @@ void DeInitAllFiles() {
int fileReadInformation_getSlot() { int fileReadInformation_getSlot() {
for (int i = 0; i < 32; i++) { for (int i = 0; i < 32; i++) {
if (gFileReadInformation[i].inUse == false) { if (!gFileReadInformation[i].inUse) {
gFileReadInformation[i].inUse = true; gFileReadInformation[i].inUse = true;
return i; return i;
} }
@ -127,7 +127,7 @@ int fileReadInformation_getSlot() {
bool initCompressedFileReadInformation(fileReadInformation *info) { bool initCompressedFileReadInformation(fileReadInformation *info) {
if (info == NULL || !info->compressed) { if (info == nullptr || !info->compressed) {
info->cInitDone = false; info->cInitDone = false;
return false; return false;
} }
@ -213,11 +213,13 @@ int32_t FSOpenFile_for_ID(uint32_t id, const char *filepath, int *handle) {
bool nonCompressed = false; bool nonCompressed = false;
if (!FSUtils::CheckFile(buffer)) { if (!FSUtils::CheckFile(buffer)) {
snprintf(buffer, 256, "%s:/%s", romName, test); snprintf(buffer, 256, "%s:/%s", romName, test);
free(test);
if (!FSUtils::CheckFile(buffer)) { if (!FSUtils::CheckFile(buffer)) {
return -3; return -3;
} }
nonCompressed = true; nonCompressed = true;
} }
free(test);
int fd = open(buffer, 0); int fd = open(buffer, 0);
if (fd >= 0) { if (fd >= 0) {
@ -225,6 +227,7 @@ int32_t FSOpenFile_for_ID(uint32_t id, const char *filepath, int *handle) {
int slot = fileReadInformation_getSlot(); int slot = fileReadInformation_getSlot();
if (slot < 0) { if (slot < 0) {
DEBUG_FUNCTION_LINE("Failed to get a slot"); DEBUG_FUNCTION_LINE("Failed to get a slot");
close(fd);
return -5; return -5;
} }
fileReadInformation *info = &gFileReadInformation[slot]; fileReadInformation *info = &gFileReadInformation[slot];