1010#include " xhook/xhook.h"
1111#include " com_unity3d_hookplayer_Boostrap.h"
1212#include " mymap32.h"
13-
14- struct GlobalData
15- {
16- /* std::map<int, FILE*>*/ MyMap32 g_fd_to_file;
17- /* std::map<FILE*, ShadowZip*>*/ MyMap32 g_file_to_shadowzip;
18- PthreadRwMutex g_file_to_shadowzip_mutex;
19- };
20- #define g_global_data (LeakSingleton<GlobalData, 0 >::instance())
13+ #include " file_mapping.h"
2114
2215
2316static inline char * dupstr (const char * const str)
@@ -40,9 +33,6 @@ std::string get_apk_path(const std::string& bundle_id);
4033__attribute__ ((visibility (" default" )))
4134JNIEXPORT int JNICALL Java_com_unity3d_hookplayer_Boostrap_init(JNIEnv * jenv, jclass cls)
4235{
43- LeakSingleton<GlobalData, 0 >::init ();
44- g_global_data->g_fd_to_file .init (4096 , 4096 , " fd_to_file" );
45- g_global_data->g_file_to_shadowzip .init (8192 , 8192 , " file_to_shadowzip" );
4636 return bootstrap ();
4737}
4838
@@ -81,7 +71,7 @@ JNIEXPORT char* JNICALL Java_com_unity3d_hookplayer_Boostrap_getAbi(JNIEnv * jen
8171 return dupstr(TARGET_ARCH_ABI);
8272}*/
8373
84- static char * g_use_data_path = NULL ;
74+ char * g_use_data_path = NULL ;
8575__attribute__ ((visibility (" default" )))
8676JNIEXPORT int JNICALL Java_com_unity3d_hookplayer_Boostrap_useDataDir(JNIEnv * jenv, jclass cls, jstring data_path_str, jstring apk_path_str)
8777{
@@ -444,22 +434,14 @@ static int my_stat(const char *path, struct stat *file_stat)
444434
445435static ShadowZip* get_cached_shadowzip (FILE *stream)
446436{
447- PthreadReadGuard (g_global_data->g_file_to_shadowzip_mutex );
448- ShadowZip* shadow_zip = NULL ;
449- g_global_data->g_file_to_shadowzip .find ((intptr_t )stream, (intptr_t &)shadow_zip);
450- return shadow_zip;
437+ FileExtraData* file_extra_data = get_file_mapping (stream);
438+ return file_extra_data == NULL ? NULL : file_extra_data->shadow_zip ;
451439}
452440
453441static ShadowZip* get_cached_shadowzip (int fd)
454442{
455- PthreadReadGuard (g_global_data->g_file_to_shadowzip_mutex );
456- FILE * stream = NULL ;
457- g_global_data->g_fd_to_file .find ((intptr_t )fd, (intptr_t &)stream);
458- if (stream == NULL ){return NULL ;}
459-
460- ShadowZip* shadow_zip = NULL ;
461- g_global_data->g_file_to_shadowzip .find ((intptr_t )stream, (intptr_t &)shadow_zip);
462- return shadow_zip;
443+ FileExtraData* file_extra_data = get_file_mapping (fd);
444+ return file_extra_data == NULL ? NULL : file_extra_data->shadow_zip ;
463445}
464446
465447typedef FILE *(* FOpenType)(const char *path, const char *mode);
@@ -485,10 +467,9 @@ static FILE *my_fopen(const char *path, const char *mode)
485467 return fopen (path, mode);
486468 }
487469
488- MY_LOG (" shadow apk in fopen: %s, fd:0x%08x, file*: 0x%08llx" , path, fileno (fp), (unsigned long long )fp);
489- PthreadWriteGuard (g_global_data->g_file_to_shadowzip_mutex );
490- g_global_data->g_file_to_shadowzip .set ((intptr_t )fp, (intptr_t )shadow_zip);
491- return fp;
470+ FileExtraData* file_extra_data = save_file_mapping (shadow_zip);
471+ MY_LOG (" shadow apk in fopen: %s, fd:0x%08x, file*: 0x%08llx" , path, file_extra_data->fd , (unsigned long long )file_extra_data->file );
472+ return file_extra_data->file ;
492473 }
493474
494475 MY_METHOD (" not apk([%s],[%s])" , path, mode);
@@ -561,26 +542,15 @@ static int my_fclose(FILE* stream)
561542{
562543 MY_METHOD (" my_fclose: file*: 0x%08llx" , (unsigned long long )stream);
563544
564- ShadowZip* shadow_zip = NULL ;
565- {
566- PthreadWriteGuard (g_global_data->g_file_to_shadowzip_mutex );
567- if (g_global_data->g_file_to_shadowzip .find ((intptr_t )stream, (intptr_t &)shadow_zip))
568- {
569- g_global_data->g_file_to_shadowzip .del ((intptr_t )stream);
570-
571- int fd = fileno (stream);
572- MY_METHOD (" my_fclose: fd: 0x%08x, file*: 0x%08llx" , fd, (unsigned long long )stream);
573- g_global_data->g_fd_to_file .del ((intptr_t )fd);
574- }
575-
545+ FileExtraData* file_extra_data = get_file_mapping (stream);
546+ if (file_extra_data != NULL )
547+ {
548+ clean_mapping_data (file_extra_data);
549+ return 0 ;
576550 }
577-
578- if (shadow_zip == NULL ){
579- return fclose (stream);
580- }else {
581- int ret = shadow_zip->fclose (stream);
582- delete shadow_zip;
583- return ret;
551+ else
552+ {
553+ return fclose (stream);
584554 }
585555}
586556
@@ -633,13 +603,10 @@ static int my_open(const char *path, int flags, ...)
633603 MY_METHOD (" open(rollback): %s -> fd:0x%08x" , path, ret);
634604 return ret;
635605 }
636- int fd = fileno (fp);
637606
638- MY_LOG (" shadow apk: %s, fd:0x%08x, file*: 0x%08llx" , path, fd, (unsigned long long )fp);
639- PthreadWriteGuard (g_global_data->g_file_to_shadowzip_mutex );
640- g_global_data->g_fd_to_file .set ((intptr_t )fd, (intptr_t )fp);
641- g_global_data->g_file_to_shadowzip .set ((intptr_t )fp, (intptr_t )shadow_zip);
642- return fd;
607+ FileExtraData* file_extra_data = save_file_mapping (shadow_zip);
608+ MY_LOG (" shadow apk in open: %s, fd:0x%08x, file*: 0x%08llx" , path, file_extra_data->fd , (unsigned long long )file_extra_data->file );
609+ return file_extra_data->fd ;
643610 }
644611
645612 int ret = has_mode ? open (path, flags, mode) : open (path, flags);
@@ -668,8 +635,7 @@ typedef off_t (*LseekType)(int fd, off_t offset, int whence);
668635off_t my_lseek (int fd, off_t offset, int whence)
669636{
670637 MY_METHOD (" lseek: 0x%08x, offset: 0x%08lx, whence: %d" , fd, offset, whence);
671-
672-
638+
673639 off_t ret = 0 ;
674640 ShadowZip* shadow_zip = get_cached_shadowzip (fd);
675641 if (shadow_zip == NULL ){
@@ -704,29 +670,15 @@ static int my_close(int fd)
704670{
705671 MY_METHOD (" my_close: 0x%08x" , fd);
706672
707-
708- ShadowZip* shadow_zip = NULL ;
673+ FileExtraData* file_extra_data = get_file_mapping (fd);
674+ if (file_extra_data ! = NULL )
709675 {
710- PthreadWriteGuard (g_global_data->g_file_to_shadowzip_mutex );
711- FILE* stream = NULL ;
712- if (g_global_data->g_fd_to_file .find ((intptr_t )fd, (intptr_t &)stream))
713- {
714- g_global_data->g_fd_to_file .del ((intptr_t )fd);
715-
716- MY_METHOD (" my_close: fd: 0x%08x, file*: 0x%08llx" , fd, (unsigned long long )stream);
717- if (g_global_data->g_file_to_shadowzip .find ((intptr_t )stream, (intptr_t &)shadow_zip))
718- {
719- g_global_data->g_file_to_shadowzip .del ((intptr_t )stream);
720- }
721- }
676+ clean_mapping_data (file_extra_data);
677+ return 0 ;
722678 }
723-
724- if (shadow_zip == NULL ){
725- return close (fd);
726- }else {
727- int ret = shadow_zip->fclose ((FILE*)(size_t )fd);
728- delete shadow_zip;
729- return ret;
679+ else
680+ {
681+ return close (fd);
730682 }
731683}
732684
@@ -870,7 +822,7 @@ static int bootstrap()
870822
871823 if (use_patch){
872824 MY_INFO (" bootstrap running %s with apk_path:%s" , TARGET_ARCH_ABI, g_apk_file_path);
873- bool success = (0 == ShadowZip::init (g_use_data_path, g_apk_file_path)) && (0 == init_hook (bundle_id)) && (0 == init_art_hook ());
825+ bool success = (0 == ShadowZip::init (g_use_data_path, g_apk_file_path)) && (0 == init_hook (bundle_id)) && (0 == init_file_mapping_data ());
874826 if (success)
875827 {
876828 static void *handle = dlopen (patch_il2cpp_path.c_str (), RTLD_NOW);
0 commit comments