Skip to content

Commit

Permalink
Improved debug system to output redirected files.
Browse files Browse the repository at this point in the history
  • Loading branch information
mariohackandglitch committed Mar 31, 2017
1 parent 90c7a43 commit b3697b6
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 21 deletions.
1 change: 1 addition & 0 deletions include/ctr/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ Result FSFILE_Read(Handle handle, u32 *bytesRead, u64 offset, void *buffer, u32
Result FSFILE_Write(Handle handle, u32 *bytesWritten, u64 offset, u32 *buffer, u32 size, u32 flushFlags);
Result FSFILE_GetSize(Handle handle, u64 *size);
Result FSFILE_SetSize(Handle handle, u64 size);
Result FSFILE_Flush(Handle handle);

Result FSDIR_Read(Handle handle, u32 *entriesRead, u32 entrycount, u16 *buffer);
Result FSDIR_Close(Handle handle);
Expand Down
Binary file modified release/OnionFS.plg
Binary file not shown.
Binary file modified release/OnionFS_debug.plg
Binary file not shown.
12 changes: 12 additions & 0 deletions source/libctru/FS.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,18 @@ Result FSFILE_SetSize(Handle handle, u64 size)
return cmdbuf[1];
}

Result FSFILE_Flush(Handle handle)
{
u32 *cmdbuf = getThreadCommandBuffer();

cmdbuf[0] = 0x8090000;

Result ret = 0;
if((ret = svc_sendSyncRequest(handle))) return ret;

return cmdbuf[1];
}

Result FSDIR_Read(Handle handle, u32 *entriesRead, u32 entrycount, u16 *buffer)
{
u32 *cmdbuf=getThreadCommandBuffer();
Expand Down
6 changes: 4 additions & 2 deletions source/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ void WriteLog(const char *log)
u32 size = (u32)strnlen(log, 0x100);
u32 bytes;

if (!FSFILE_Write(g_logFile, &bytes, g_offset, (u32 *)log, size, 0x0))
g_offset += bytes;
if (!FSFILE_Flush(g_logFile)) {
if (!FSFILE_Write(g_logFile, &bytes, g_offset, (u32 *)log, size, 0x0))
g_offset += bytes;
}
}
#endif
70 changes: 51 additions & 19 deletions source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ u64 currposs = 0;
FS_archive debugsdmcArchive = {0x9, (FS_path){PATH_EMPTY, 1, (u8*)""}};
Handle debugfileout;
u64 lastcurrposs = 0;
u8 safewritedebug = 1;
#endif

int InitFS(void)
Expand Down Expand Up @@ -110,36 +111,57 @@ u8 checkarchive(u8* path, u8 size) {

void addarchive(u8* path) {
u8 len = 0;
#ifdef DEBUG_MODE
LOG("Registering new archive: %s... ", path);
#endif
while(*(path + len)) len++;
if (!(checkarchive(path, len)) && (archivecount < 20)) {
strCat(archivelist + (10*archivecount), path);
archivecount++;
#ifdef DEBUG_MODE
LOG("success\r\n");
#endif
}
#ifdef DEBUG_MODE
else {
LOG("skipped, already registered\r\n");
}
#endif
}

u32 fsRegArchiveCallback(u8* path, u32 ptr, u32 isAddOnContent, u32 isAlias) {
#ifdef DEBUG_MODE
while (!safewritedebug){
svc_sleepThread(1000000);
}
safewritedebug = 0;
#endif
u32 ret, ret2;
static u32 isFisrt = 1;
u32 sdmcArchive = 0;

nsDbgPrint("regArchive: %s, %08x, %08x, %08x\n", path, ptr, isAddOnContent, isAlias);
ret = ((fsRegArchiveTypeDef)regArchiveHook.callCode)(path, ptr, isAddOnContent, isAlias);
addarchive(path);
if (isFisrt) {
isFisrt = 0;

((fsMountArchiveTypeDef)fsMountArchive)(&sdmcArchive, 9);
#ifdef LOG_FILES
nsDbgPrint("sdmcArchive: %08x\n", sdmcArchive);
#ifdef DEBUG_MODE
LOG("Trying to open SD archive... ");
#endif
((fsMountArchiveTypeDef)fsMountArchive)(&sdmcArchive, 9);

if (sdmcArchive) {
ret2 = ((fsRegArchiveTypeDef)regArchiveHook.callCode)("ram:", sdmcArchive, 0, 0);
#ifdef LOG_FILES
nsDbgPrint("regArchive ret: %08x\n", ret2);
#ifdef DEBUG_MODE
if (ret2 != 0) {
LOG("failed: 0x%08X\r\n", ret2);
} else {
LOG("success.\r\n");
}
#endif
}
}
#ifdef DEBUG_MODE
safewritedebug = 1;
#endif
return ret;
}

Expand Down Expand Up @@ -180,29 +202,41 @@ u8 findu8character(u8* in, char character) {
}

u32 userFsTryOpenFileCallback(u32 a1, u16 * fileName, u32 mode) {
#ifdef DEBUG_MODE
while (!safewritedebug){
svc_sleepThread(1000000);
}
safewritedebug = 0;
#endif
u16 buf[300];
u32 ret;


convertUnicodeToAnsi(fileName, (u8*) buf);
u8 chara = findu8character((u8*) buf, '/');
#ifdef LOG_FILES
nsDbgPrint("path: %s\n", buf);
#endif

if ((chara) && checkarchive((u8*) buf, chara)) {
// accessing rom:/ file
#ifdef DEBUG_MODE
LOG("File %s ", (u8*)buf);
#endif
buf[0] = 0;
ustrCat(buf, ustrRootPath);
ustrCat(buf, &fileName[chara + 1]);
ret = ((userFsTryOpenFileTypeDef)userFsTryOpenFileHook.callCode)(a1, buf, 1);
#ifdef LOG_FILES
nsDbgPrint("ret: %08x\n", ret);
#endif
if (ret == 0) {
#ifdef DEBUG_MODE
LOG("found in SD, redirected.\r\n");
safewritedebug = 1;
#endif
return ret;
}
#ifdef DEBUG_MODE
else if (ret == 0xC8804478) {LOG("not found in SD, skipping.\r\n");}
else {LOG("returned unknown result: 0x%08X.\r\n", ret);}
#endif
}
#ifdef DEBUG_MODE
safewritedebug = 1;
#endif
return ((userFsTryOpenFileTypeDef)userFsTryOpenFileHook.callCode)(a1, fileName, mode);
}

Expand Down Expand Up @@ -651,10 +685,8 @@ int main() {
rtEnableHook(&cfgGetRegionHook);
#endif
#ifdef DEBUG_MODE
LOG("\r\nFinished patching, launching game... Bye!");
ExitLog();
LOG("\r\nFinished patching, launching game...\r\n\r\n");
#endif
InitFS();
return 0;
}

0 comments on commit b3697b6

Please sign in to comment.