8
8
#include " TextView_Render.hpp"
9
9
#include " xxHash/xxhash.h"
10
10
#include " textureHashes.hpp"
11
- #include " nn/fs.hpp"
12
11
#include " nn/os.hpp"
13
- #include < cstdio>
14
12
#include < cmath>
13
+ #include " nn/util/util_gzip.hpp"
14
+ #include " nn/util/util_snprintf.hpp"
15
15
16
16
int countLSBZeros (int value) {
17
17
unsigned int c = 0 ;
@@ -435,8 +435,6 @@ HOOK_DEFINE_TRAMPOLINE(PutCodeTo) {
435
435
436
436
};
437
437
438
- void * in_buffer = 0 ;
439
-
440
438
HOOK_DEFINE_INLINE (LoadTex_start) {
441
439
442
440
static void Callback (exl::hook::nx64::InlineCtx* ctx) {
@@ -446,19 +444,37 @@ HOOK_DEFINE_INLINE(LoadTex_start) {
446
444
nn::fs::FileHandle filehandle;
447
445
448
446
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 ());
450
448
}
451
449
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 ());
453
451
}
454
452
if (strlen (filepath) != 0 ) {
455
453
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 );
460
458
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
+ }
462
478
}
463
479
}
464
480
ctx->X [22 ] = ctx->X [1 ];
@@ -470,37 +486,80 @@ bool ReplaceTexture(void** TexturePointer, uint16_t index, size_t texture_size,
470
486
471
487
char filepath[128 ] = " " ;
472
488
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 );
474
490
}
475
491
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 );
477
493
}
478
494
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 );
480
496
}
497
+
481
498
FILE* tex = FDKfopen (filepath, " rb" );
482
499
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);
494
505
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);
495
528
return true ;
496
529
}
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
+ }
497
549
return false ;
498
550
}
551
+
552
+ // #define mutex_on
553
+
554
+ #ifdef mutex_on
499
555
nn::os::MutexType _mutex;
556
+ #endif
500
557
501
558
HOOK_DEFINE_TRAMPOLINE (Decompress) {
502
559
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
504
563
static bool init = false ;
505
564
if (!init) {
506
565
nn::fs::MountSdCardForDebug (" sdmc" );
@@ -542,11 +601,16 @@ HOOK_DEFINE_TRAMPOLINE(Decompress) {
542
601
}
543
602
544
603
_ZN4Nmpl15MemoryInterface10freeMemoryEPv (temp_buffer);
604
+ #ifdef mutex_on
605
+ nn::os::UnlockMutex (&_mutex);
606
+ #endif
545
607
return ;
546
608
}
547
609
548
610
}
549
- // nn::os::UnlockMutex(&_mutex);
611
+ #ifdef mutex_on
612
+ nn::os::UnlockMutex (&_mutex);
613
+ #endif
550
614
return Orig (in_buffer, unc_size, out_buffer, unk, com_size);
551
615
}
552
616
};
@@ -669,8 +733,10 @@ extern "C" void exl_main(void* x0, void* x1) {
669
733
670
734
ReplaceFont::InstallAtOffset (0x6FA7C );
671
735
736
+ #ifdef mutex_on
672
737
memset (&_mutex, 0 , sizeof (_mutex));
673
738
nn::os::InitializeMutex (&_mutex, false , 1 );
739
+ #endif
674
740
675
741
/* Alternative install funcs: */
676
742
/* InstallAtPtr takes an absolute address as a uintptr_t. */
0 commit comments