Skip to content

Commit 70ae26f

Browse files
authored
Add files via upload
1 parent cfa8efc commit 70ae26f

File tree

1 file changed

+93
-27
lines changed

1 file changed

+93
-27
lines changed

source/program/main.cpp

+93-27
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
#include "TextView_Render.hpp"
99
#include "xxHash/xxhash.h"
1010
#include "textureHashes.hpp"
11-
#include "nn/fs.hpp"
1211
#include "nn/os.hpp"
13-
#include <cstdio>
1412
#include <cmath>
13+
#include "nn/util/util_gzip.hpp"
14+
#include "nn/util/util_snprintf.hpp"
1515

1616
int countLSBZeros(int value) {
1717
unsigned int c = 0;
@@ -435,8 +435,6 @@ HOOK_DEFINE_TRAMPOLINE(PutCodeTo) {
435435

436436
};
437437

438-
void* in_buffer = 0;
439-
440438
HOOK_DEFINE_INLINE(LoadTex_start) {
441439

442440
static void Callback(exl::hook::nx64::InlineCtx* ctx) {
@@ -446,19 +444,37 @@ HOOK_DEFINE_INLINE(LoadTex_start) {
446444
nn::fs::FileHandle filehandle;
447445

448446
if (ShinHaya_set == 1) {
449-
snprintf(filepath, sizeof(filepath), "rom0:/Tfoaf1/Textures/%s.tga", name.c_str());
447+
nn::util::SNPrintf(filepath, sizeof(filepath), "rom0:/Tfoaf1/Textures/%s.tga.gz", name.c_str());
450448
}
451449
else if (ShinHaya_set == 2) {
452-
snprintf(filepath, sizeof(filepath), "rom0:/Tfoaf2/Textures/%s.tga", name.c_str());
450+
nn::util::SNPrintf(filepath, sizeof(filepath), "rom0:/Tfoaf2/Textures/%s.tga.gz", name.c_str());
453451
}
454452
if (strlen(filepath) != 0) {
455453
if (R_SUCCEEDED(nn::fs::OpenFile(&filehandle, filepath, nn::fs::OpenMode_Read))) {
456-
long in_size = 0;
457-
nn::fs::GetFileSize(&in_size, filehandle);
458-
in_buffer = _ZN4Nmpl15MemoryInterface14allocateMemoryEm(in_size);
459-
nn::fs::ReadFile(filehandle, 0, in_buffer, in_size);
454+
long com_size = 0;
455+
nn::fs::GetFileSize(&com_size, filehandle);
456+
void* com_buffer = _ZN4Nmpl15MemoryInterface14allocateMemoryEm(com_size);
457+
nn::fs::ReadFile(filehandle, 0, com_buffer, com_size);
460458
nn::fs::CloseFile(filehandle);
461-
ctx -> X[1] = (u64)in_buffer;
459+
size_t in_size = nn::util::GetGzipDecompressedSize(com_buffer, com_size);
460+
void* in_buffer = _ZN4Nmpl15MemoryInterface14allocateMemoryEm(in_size);
461+
char workBuffer[nn::util::DecompressGzipWorkBufferSize] = "";
462+
if (nn::util::DecompressGzip(in_buffer, in_size, com_buffer, com_size, (void*)workBuffer, sizeof(workBuffer))) {
463+
ctx -> X[1] = (u64)in_buffer;
464+
}
465+
else _ZN4Nmpl15MemoryInterface10freeMemoryEPv(in_buffer);
466+
_ZN4Nmpl15MemoryInterface10freeMemoryEPv(com_buffer);
467+
}
468+
else {
469+
filepath[strlen(filepath)-3] = 0;
470+
if (R_SUCCEEDED(nn::fs::OpenFile(&filehandle, filepath, nn::fs::OpenMode_Read))) {
471+
long in_size = 0;
472+
nn::fs::GetFileSize(&in_size, filehandle);
473+
void* in_buffer = _ZN4Nmpl15MemoryInterface14allocateMemoryEm(in_size);
474+
nn::fs::ReadFile(filehandle, 0, in_buffer, in_size);
475+
nn::fs::CloseFile(filehandle);
476+
ctx -> X[1] = (u64)in_buffer;
477+
}
462478
}
463479
}
464480
ctx->X[22] = ctx->X[1];
@@ -470,37 +486,80 @@ bool ReplaceTexture(void** TexturePointer, uint16_t index, size_t texture_size,
470486

471487
char filepath[128] = "";
472488
if (!ShinHaya_set) {
473-
snprintf(&filepath[0], 128, "rom0:/Textures/%04d.dds", index);
489+
nn::util::SNPrintf(&filepath[0], 128, "rom0:/Textures/%04d.dds.gz", index);
474490
}
475491
else if (ShinHaya_set == 1) {
476-
snprintf(&filepath[0], 128, "rom0:/Tfoaf1/Textures/anm0/%04d.dds", index);
492+
nn::util::SNPrintf(&filepath[0], 128, "rom0:/Tfoaf1/Textures/anm0/%04d.dds.gz", index);
477493
}
478494
else if (ShinHaya_set == 2) {
479-
snprintf(&filepath[0], 128, "rom0:/Tfoaf2/Textures/anm0/%04d.dds", index);
495+
nn::util::SNPrintf(&filepath[0], 128, "rom0:/Tfoaf2/Textures/anm0/%04d.dds.gz", index);
480496
}
497+
481498
FILE* tex = FDKfopen(filepath, "rb");
482499
if (tex) {
483-
*TexturePointer = _ZN4Nmpl15MemoryInterface14allocateMemoryEm(texture_size);
484-
FDKfseek(tex, 0xC, 0);
485-
FDKfread(width, 4, 1, tex);
486-
FDKfread(height, 4, 1, tex);
487-
FDKfseek(tex, 0x40, 0);
488-
FDKfread(isSwizzled, 1, 1, tex);
489-
FDKfseek(tex, 0x54, 0);
490-
uint32_t type = 0;
491-
FDKfread(&type, 4, 1, tex);
492-
FDKfseek(tex, ((type == 0x30315844) ? 0x94 : 0x80), 0);
493-
FDKfread(*TexturePointer, texture_size, 1, tex);
500+
FDKfseek(tex, 0, 2);
501+
long com_size = FDKftell(tex);
502+
FDKfseek(tex, 0, 0);
503+
void* com_buffer = _ZN4Nmpl15MemoryInterface14allocateMemoryEm(com_size);
504+
FDKfread(com_buffer, com_size, 1, tex);
494505
FDKfclose(tex);
506+
507+
size_t in_size = nn::util::GetGzipDecompressedSize(com_buffer, com_size);
508+
*TexturePointer = _ZN4Nmpl15MemoryInterface14allocateMemoryEm(in_size);
509+
if (!*TexturePointer) {
510+
_ZN4Nmpl15MemoryInterface10freeMemoryEPv(com_buffer);
511+
return false;
512+
}
513+
char workBuffer[nn::util::DecompressGzipWorkBufferSize] = "";
514+
if (!nn::util::DecompressGzip(*TexturePointer, in_size, com_buffer, com_size, (void*)workBuffer, sizeof(workBuffer))) {
515+
_ZN4Nmpl15MemoryInterface10freeMemoryEPv(com_buffer);
516+
_ZN4Nmpl15MemoryInterface10freeMemoryEPv(*TexturePointer);
517+
return false;
518+
}
519+
_ZN4Nmpl15MemoryInterface10freeMemoryEPv(com_buffer);
520+
521+
uint8_t* buffer = (uint8_t*)*TexturePointer;
522+
memcpy((void*)width, (void*)&buffer[0xC], 4);
523+
memcpy((void*)height, (void*)&buffer[0x10], 4);
524+
memcpy((void*)isSwizzled, (void*)&buffer[0x40], 1);
525+
uint32_t type = 0;
526+
memcpy((void*)&type, (void*)&buffer[0x54], 4);
527+
memcpy((void*)buffer, (void*)&buffer[((type == 0x30315844) ? 0x94 : 0x80)], *width * *height);
495528
return true;
496529
}
530+
else {
531+
filepath[strlen(filepath)-3] = 0;
532+
tex = FDKfopen(filepath, "rb");
533+
if (tex) {
534+
*TexturePointer = _ZN4Nmpl15MemoryInterface14allocateMemoryEm(texture_size);
535+
FDKfseek(tex, 0xC, 0);
536+
FDKfread(width, 4, 1, tex);
537+
FDKfread(height, 4, 1, tex);
538+
FDKfseek(tex, 0x40, 0);
539+
FDKfread(isSwizzled, 1, 1, tex);
540+
FDKfseek(tex, 0x54, 0);
541+
uint32_t type = 0;
542+
FDKfread(&type, 4, 1, tex);
543+
FDKfseek(tex, ((type == 0x30315844) ? 0x94 : 0x80), 0);
544+
FDKfread(*TexturePointer, texture_size, 1, tex);
545+
FDKfclose(tex);
546+
return true;
547+
}
548+
}
497549
return false;
498550
}
551+
552+
//#define mutex_on
553+
554+
#ifdef mutex_on
499555
nn::os::MutexType _mutex;
556+
#endif
500557

501558
HOOK_DEFINE_TRAMPOLINE(Decompress) {
502559
static void Callback(uint8_t* in_buffer, size_t com_size, void** out_buffer, uint32_t* unk, size_t unc_size) {
503-
//nn::os::LockMutex(&_mutex);
560+
#ifdef mutex_on
561+
nn::os::LockMutex(&_mutex);
562+
#endif
504563
static bool init = false;
505564
if (!init) {
506565
nn::fs::MountSdCardForDebug("sdmc");
@@ -542,11 +601,16 @@ HOOK_DEFINE_TRAMPOLINE(Decompress) {
542601
}
543602

544603
_ZN4Nmpl15MemoryInterface10freeMemoryEPv(temp_buffer);
604+
#ifdef mutex_on
605+
nn::os::UnlockMutex(&_mutex);
606+
#endif
545607
return;
546608
}
547609

548610
}
549-
//nn::os::UnlockMutex(&_mutex);
611+
#ifdef mutex_on
612+
nn::os::UnlockMutex(&_mutex);
613+
#endif
550614
return Orig(in_buffer, unc_size, out_buffer, unk, com_size);
551615
}
552616
};
@@ -669,8 +733,10 @@ extern "C" void exl_main(void* x0, void* x1) {
669733

670734
ReplaceFont::InstallAtOffset(0x6FA7C);
671735

736+
#ifdef mutex_on
672737
memset(&_mutex, 0, sizeof(_mutex));
673738
nn::os::InitializeMutex(&_mutex, false, 1);
739+
#endif
674740

675741
/* Alternative install funcs: */
676742
/* InstallAtPtr takes an absolute address as a uintptr_t. */

0 commit comments

Comments
 (0)