forked from minecraft-linux/mcpelauncher-client
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1144 lines (1077 loc) · 38.9 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <dlfcn.h>
#include <wctype.h>
#include <string.h>
#include <signal.h>
#ifdef __linux__
#include <sys/eventfd.h>
#endif
#include <sys/epoll.h>
#include <jnivm.h>
#include <log.h>
#include "native_activity.h"
#include <game_window.h>
#include <game_window_manager.h>
#include <iostream>
#include <thread>
#include <future>
#include <atomic>
#include "../mcpelauncher-linker/bionic/linker/linker_soinfo.h"
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE
#endif
#include <setjmp.h>
#include <sys/stat.h>
#ifdef __linux__
#include <sys/vfs.h>
#endif
extern "C" void RunExe(const char * path, int argc, char** argv);
extern "C" int my_pthread_create(pthread_t *thread, const pthread_attr_t *__attr,
void *(*start_routine)(void*), void *arg);
soinfo* soinfo_from_handle(void* handle);
void* mb;
void* cb;
void* tb;
enum class AndroidLogPriority {
ANDROID_LOG_UNKNOWN = 0,
ANDROID_LOG_DEFAULT,
ANDROID_LOG_VERBOSE,
ANDROID_LOG_DEBUG,
ANDROID_LOG_INFO,
ANDROID_LOG_WARN,
ANDROID_LOG_ERROR,
ANDROID_LOG_FATAL,
ANDROID_LOG_SILENT
};
static LogLevel convertAndroidLogLevel(int level) {
if (level <= (int) AndroidLogPriority::ANDROID_LOG_VERBOSE)
return LogLevel::LOG_TRACE;
if (level == (int) AndroidLogPriority::ANDROID_LOG_DEBUG)
return LogLevel::LOG_DEBUG;
if (level == (int) AndroidLogPriority::ANDROID_LOG_INFO)
return LogLevel::LOG_INFO;
if (level == (int) AndroidLogPriority::ANDROID_LOG_WARN)
return LogLevel::LOG_WARN;
if (level >= (int) AndroidLogPriority::ANDROID_LOG_ERROR)
return LogLevel::LOG_ERROR;
return LogLevel::LOG_ERROR;
}
extern "C" void* __loader_dlopen(const char* filename, int flags, const void* caller_addr);
extern "C" void* __loader_dlsym(void* handle, const char* symbol, const void* caller_addr);
extern "C" int __loader_dl_iterate_phdr(int (*cb)(struct dl_phdr_info* info, size_t size, void* data),
void* data);
extern "C" int __loader_dlclose(void* handle);
extern "C" char* __loader_dlerror();
void solist_init();
extern "C" {
struct _hook {
const char *name;
void *func;
};
extern struct _hook main_hooks[];
extern struct _hook dirent_hooks[];
extern struct _hook io_hooks[];
extern struct _hook net_hooks[];
extern struct _hook pthread_hooks[];
#ifdef __APPLE__
extern struct _hook net_darwin_hooks[];
#endif
}
const char* libm_symbols[] = {
"__sF",
"acos",
"acosf",
"acosh",
"acoshf",
"acoshl",
"acosl",
"asin",
"asinf",
"asinh",
"asinhf",
"asinhl",
"asinl",
"atan",
"atan2",
"atan2f",
"atan2l",
"atanf",
"atanh",
"atanhf",
"atanhl",
"atanl",
"cabsl",
"cbrt",
"cbrtf",
"cbrtl",
"ceil",
"ceilf",
"ceill",
"copysign",
"copysignf",
"copysignl",
"cos",
"cosf",
"cosh",
"coshf",
"coshl",
"cosl",
"cprojl",
"csqrtl",
"drem",
"dremf",
"erf",
"erfc",
"erfcf",
"erfcl",
"erff",
"erfl",
"exp",
"exp2",
"exp2f",
"exp2l",
"expf",
"expl",
"expm1",
"expm1f",
"expm1l",
"fabs",
"fabsf",
"fabsl",
"fdim",
"fdimf",
"fdiml",
"feclearexcept",
"fedisableexcept",
"feenableexcept",
"fegetenv",
"fegetexcept",
"fegetexceptflag",
"fegetround",
"feholdexcept",
"feraiseexcept",
"fesetenv",
"fesetexceptflag",
"fesetround",
"fetestexcept",
"feupdateenv",
"finite",
"finitef",
"floor",
"floorf",
"floorl",
"fma",
"fmaf",
"fmal",
"fmax",
"fmaxf",
"fmaxl",
"fmin",
"fminf",
"fminl",
"fmod",
"fmodf",
"fmodl",
"frexp",
"frexpf",
"frexpl",
"gamma",
"gammaf",
"gammaf_r",
"gamma_r",
"hypot",
"hypotf",
"hypotl",
"ilogb",
"ilogbf",
"ilogbl",
"j0",
"j0f",
"j1",
"j1f",
"jn",
"jnf",
"ldexpf",
"ldexpl",
"lgamma",
"lgammaf",
"lgammaf_r",
"lgammal",
"lgammal_r",
"lgamma_r",
"llrint",
"llrintf",
"llrintl",
"llround",
"llroundf",
"llroundl",
"log",
"log10",
"log10f",
"log10l",
"log1p",
"log1pf",
"log1pl",
"log2",
"log2f",
"log2l",
"logb",
"logbf",
"logbl",
"logf",
"logl",
"lrint",
"lrintf",
"lrintl",
"lround",
"lroundf",
"lroundl",
"modf",
"modff",
"modfl",
"nan",
"nanf",
"nanl",
"nearbyint",
"nearbyintf",
"nearbyintl",
"nextafter",
"nextafterf",
"nextafterl",
"nexttoward",
"nexttowardf",
"nexttowardl",
"pow",
"powf",
"powl",
"remainder",
"remainderf",
"remainderl",
"remquo",
"remquof",
"remquol",
"rint",
"rintf",
"rintl",
"round",
"roundf",
"roundl",
"scalb",
"scalbf",
"scalbln",
"scalblnf",
"scalblnl",
"scalbn",
"scalbnf",
"scalbnl",
"__signbit",
"__signbitf",
"__signbitl",
"signgam",
"significand",
"significandf",
"significandl",
"sin",
"sincos",
"sincosf",
"sincosl",
"sinf",
"sinh",
"sinhf",
"sinhl",
"sinl",
"sqrt",
"sqrtf",
"sqrtl",
"tan",
"tanf",
"tanh",
"tanhf",
"tanhl",
"tanl",
"tgamma",
"tgammaf",
"tgammal",
"trunc",
"truncf",
"truncl",
"y0",
"y0f",
"y1",
"y1f",
"yn",
"ynf",
"isnan",
nullptr
};
const char* egl_symbols[] = {
"eglGetCurrentDisplay",
"eglChooseConfig",
"eglGetError",
"eglCreateWindowSurface",
"eglGetConfigAttrib",
"eglCreateContext",
"eglDestroySurface",
// "eglSwapBuffers",
"eglMakeCurrent",
"eglDestroyContext",
"eglTerminate",
"eglGetDisplay",
"eglInitialize",
"eglQuerySurface",
// "eglSwapInterval",
"eglQueryString",
"eglGetCurrentContext",
nullptr
};
const char* android_symbols[] = {
"ANativeWindow_setBuffersGeometry",
"AAssetManager_open",
"AAsset_getLength",
"AAsset_getBuffer",
"AAsset_close",
"AAsset_read",
"AAsset_seek64",
"AAsset_getLength64",
"AAsset_getRemainingLength64",
"ALooper_pollAll",
"ANativeActivity_finish",
"AInputQueue_getEvent",
"AKeyEvent_getKeyCode",
"AInputQueue_preDispatchEvent",
"AInputQueue_finishEvent",
"AKeyEvent_getAction",
"AMotionEvent_getAxisValue",
"AKeyEvent_getRepeatCount",
"AKeyEvent_getMetaState",
"AInputEvent_getDeviceId",
"AInputEvent_getType",
"AInputEvent_getSource",
"AMotionEvent_getAction",
"AMotionEvent_getPointerId",
"AMotionEvent_getX",
"AMotionEvent_getRawX",
"AMotionEvent_getY",
"AMotionEvent_getRawY",
"AMotionEvent_getPointerCount",
"AConfiguration_new",
"AConfiguration_fromAssetManager",
"AConfiguration_getLanguage",
"AConfiguration_getCountry",
"ALooper_prepare",
"ALooper_addFd",
"AInputQueue_detachLooper",
"AConfiguration_delete",
"AInputQueue_attachLooper",
"AAssetManager_openDir",
"AAssetDir_getNextFileName",
"AAssetDir_close",
"AAssetManager_fromJava",
nullptr
};
const char* fmod_symbols[] = {
"_ZN4FMOD6System12mixerSuspendEv",
"_ZN4FMOD6System11mixerResumeEv",
"_ZN4FMOD14ChannelControl7setMuteEb",
"_ZN4FMOD14ChannelControl9setVolumeEf",
"_ZN4FMOD14ChannelControl9isPlayingEPb",
"_ZN4FMOD14ChannelControl4stopEv",
"_ZN4FMOD6System9playSoundEPNS_5SoundEPNS_12ChannelGroupEbPPNS_7ChannelE",
"_ZN4FMOD5Sound15getNumSubSoundsEPi",
"_ZN4FMOD5Sound11getSubSoundEiPPS0_",
"_ZN4FMOD14ChannelControl15set3DAttributesEPK11FMOD_VECTORS3_S3_",
"_ZN4FMOD14ChannelControl15set3DAttributesEPK11FMOD_VECTORS3_",/*New x64*/
"_ZN4FMOD14ChannelControl8setPitchEf",
"_ZN4FMOD14ChannelControl9setPausedEb",
"_ZN4FMOD5Sound7releaseEv",
"_ZN4FMOD6System5closeEv",
"_ZN4FMOD6System7releaseEv",
"FMOD_System_Create",
"_ZN4FMOD6System10getVersionEPj",
"_ZN4FMOD6System9setOutputE15FMOD_OUTPUTTYPE",
"_ZN4FMOD6System4initEijPv",
"_ZN4FMOD6System13set3DSettingsEfff",
"_ZN4FMOD6System18createChannelGroupEPKcPPNS_12ChannelGroupE",
"_ZN4FMOD6System21getMasterChannelGroupEPPNS_12ChannelGroupE",
"_ZN4FMOD12ChannelGroup8addGroupEPS0_bPPNS_13DSPConnectionE",
"_ZN4FMOD6System23set3DListenerAttributesEiPK11FMOD_VECTORS3_S3_S3_",
"_ZN4FMOD6System6updateEv",
"_ZN4FMOD6System12createStreamEPKcjP22FMOD_CREATESOUNDEXINFOPPNS_5SoundE",
"_ZN4FMOD6System11createSoundEPKcjP22FMOD_CREATESOUNDEXINFOPPNS_5SoundE",
"_ZN4FMOD5Sound19set3DMinMaxDistanceEff",
"_ZN4FMOD6System13getNumDriversEPi",
"_ZN4FMOD6System13getDriverInfoEiPciP9FMOD_GUIDPiP16FMOD_SPEAKERMODES4_",
"_ZN4FMOD6System9setDriverEi",
"_ZN4FMOD5Sound7setModeEj",
"_ZN4FMOD5Sound9getFormatEP15FMOD_SOUND_TYPEP17FMOD_SOUND_FORMATPiS5_",
"_ZN4FMOD6System17getSoftwareFormatEPiP16FMOD_SPEAKERMODES1_",
"_ZN4FMOD14ChannelControl11getDSPClockEPyS1_",
"_ZN4FMOD14ChannelControl12addFadePointEyf",
"_ZN4FMOD14ChannelControl8setDelayEyyb",
"_ZN4FMOD6System17set3DNumListenersEi",
"_ZN4FMOD6System13setFileSystemEPF11FMOD_RESULTPKcPjPPvS5_EPFS1_S5_S5_EPFS1_S5_S5_jS4_S5_EPFS1_S5_jS5_EPFS1_P18FMOD_ASYNCREADINFOS5_ESI_i",
"FMOD_Memory_GetStats",
"_ZN4FMOD6System11getCPUUsageEPfS1_S1_S1_S1_",
"_ZN4FMOD6System18getChannelsPlayingEPiS1_",
"_ZN4FMOD6System12getFileUsageEPxS1_S1_",
nullptr
};
static void __android_log_vprint(int prio, const char *tag, const char *fmt, va_list args) {
Log::vlog(convertAndroidLogLevel(prio), tag, fmt, args);
}
static void __android_log_print(int prio, const char *tag, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
Log::vlog(convertAndroidLogLevel(prio), tag, fmt, args);
va_end(args);
}
static void __android_log_write(int prio, const char *tag, const char *text) {
Log::log(convertAndroidLogLevel(prio), tag, "%s", text);
}
#define hybris_hook(a, b) symbols[a] = b;
static std::shared_ptr<GameWindow> window;
void InstallALooper(std::unordered_map<std::string, void *>& symbols) {
struct Looper {
int fd;
int indent;
void * data;
int indent2;
void * data2;
};
static Looper looper;
symbols["ALooper_pollAll"] = (void *)+[]( int timeoutMillis,
int *outFd,
int *outEvents,
void **outData) {
fd_set rfds;
struct timeval tv;
int retval;
/* Watch stdin (fd 0) to see when it has input. */
FD_ZERO(&rfds);
FD_SET(looper.fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
retval = select(looper.fd + 1, &rfds, NULL, NULL, &tv);
/* Don't rely on the value of tv now! */
if (retval == -1)
perror("select()");
else if (retval) {
// printf("Data is available now.\n");
*outData = looper.data;
return looper.indent;
/* FD_ISSET(0, &rfds) will be true. */
}
if(window) {
window->pollEvents();
}
return -3;
};
hybris_hook("ALooper_addFd", (void *)+[]( void *loopere ,
int fd,
int ident,
int events,
int(* callback)(int fd, int events, void *data),
void *data) {
looper.fd = fd;
looper.indent = ident;
looper.data = data;
return 1;
});
hybris_hook("AInputQueue_attachLooper", (void *)+[]( void *queue,
void *looper2,
int ident,
void* callback,
void *data) {
looper.indent2 = ident;
looper.data2 = data;
});
}
#define EGL_NONE 0x3038
#define EGL_TRUE 1
#define EGL_FALSE 0
#define EGL_WIDTH 0x3057
#define EGL_HEIGHT 0x3056
using EGLint = int;
using EGLDisplay = void*;
using EGLSurface = void*;
using EGLContext = void*;
using EGLConfig = void*;
using NativeWindowType = void*;
using NativeDisplayType = void*;
void CreateIfNeededWindow() {
if(!window) {
window = GameWindowManager::getManager()->createWindow("mcpelauncher "
#ifdef _LP64
"64"
#else
"32"
#endif
"bit alpha", 1280, 720, GraphicsApi::OPENGL_ES2);
window->show();
}
}
// extern "C" void sigsetjmp();
void InstallEGL(std::unordered_map<std::string, void *>& symbols) {
hybris_hook("eglChooseConfig", (void *)+[](EGLDisplay dpy, const EGLint *attrib_list, EGLConfig *configs, EGLint config_size, EGLint *num_config) {
*num_config = 1;
return EGL_TRUE;
});
hybris_hook("eglGetError", (void *)(void (*)())[]() {
});
hybris_hook("eglGetCurrentDisplay", (void *)+[]() -> EGLDisplay {
return (EGLDisplay)1;
});
hybris_hook("eglCreateWindowSurface", (void *)+[](EGLDisplay display,
EGLConfig config,
NativeWindowType native_window,
EGLint const * attrib_list) {
return native_window;
});
hybris_hook("eglGetConfigAttrib", (void *)+[](EGLDisplay display,
EGLConfig config,
EGLint attribute,
EGLint * value) {
return EGL_TRUE;
});
hybris_hook("eglCreateContext", (void *)+[](EGLDisplay display,
EGLConfig config,
EGLContext share_context,
EGLint const * attrib_list) {
// CreateIfNeededWindow();
return 1;
});
hybris_hook("eglDestroySurface", (void *)(void (*)())[]() {
});
symbols["eglSwapBuffers"] = (void *)+[](EGLDisplay *display, EGLSurface surface) {
std::cout << "eglSwapBuffers\n";
window->swapBuffers();
};
// hybris_hook("eglSwapBuffers", (void *)+[](EGLDisplay *display,
// EGLSurface surface) {
// window->swapBuffers();
// });
hybris_hook("eglMakeCurrent", (void *)+[](EGLDisplay display,
EGLSurface draw,
EGLSurface read,
EGLContext context) {
Log::warn("Launcher", "EGL stub %s called", "eglMakeCurrent");
// CreateIfNeededWindow();
return EGL_TRUE;
});
hybris_hook("eglDestroyContext", (void *)(void (*)())[]() {
});
hybris_hook("eglTerminate", (void *)(void (*)())[]() {
});
hybris_hook("eglGetDisplay", (void *)+[](NativeDisplayType native_display) {
return 1;
});
hybris_hook("eglInitialize", (void *)+[](void* display, uint32_t * major, uint32_t * minor) {
return EGL_TRUE;
});
hybris_hook("eglQuerySurface", (void *) + [](void* dpy, EGLSurface surface, EGLint attribute, EGLint *value) {
int dummy;
// CreateIfNeededWindow();
switch (attribute)
{
case EGL_WIDTH:
window->getWindowSize(*value, dummy);
break;
case EGL_HEIGHT:
window->getWindowSize(dummy, *value);
break;
default:
return EGL_FALSE;
}
return EGL_TRUE;
});
hybris_hook("eglSwapInterval", (void *)+[](EGLDisplay display, EGLint interval) {
window->swapInterval(interval);
return EGL_TRUE;
});
hybris_hook("eglQueryString", (void *)+[](void* display, int32_t name) {
return 0;
});
hybris_hook("eglGetProcAddress", ((void*)+[](char* ch)->void*{
static std::unordered_map<std::string, void*> eglfuncs = {{ "glInvalidateFramebuffer", (void*)+[]() {}}};
auto hook = eglfuncs[ch];
if(!hook) {
hook = ((void* (*)(const char*))GameWindowManager::getManager()->getProcAddrFunc())(ch);
}
return hook;
}));
hybris_hook("eglGetCurrentContext", (void*) + []() -> int {
return 0;
});
}
#include "../mcpelauncher-linker/bionic/libc/platform/bionic/tls.h"
#if defined(__x86_64__) && defined(__APPLE__)
// Signal handler for when code tries to use %fs.
static void handle_sigsegv(int sig, siginfo_t *si, void *ucp) {
ucontext_t *uc = (ucontext_t*)ucp;
unsigned char *p = (unsigned char *)uc->uc_mcontext->__ss.__rip;
if (p && *p == 0x64) {
// Instruction starts with 0x64, meaning it tries to access %fs. By
// changing the first byte to 0x65, it uses %gs instead.
//std::cout << "Try to patch it\n";
*p = 0x65;
//std::cout << "Tried to patch it\n";
} else if (p && *p == 0x65) {
// Instruction has already been patched up, but it may well be the
// case that this was done by another CPU core. There is nothing
// else we can do than return and try again. This may cause us to
// get stuck indefinitely.
} else {
// Segmentation violation on an instruction that does not try to
// access %fs. Reset the handler to its default action, so that the
// segmentation violation is rethrown.
struct sigaction sa = {
.sa_handler = SIG_DFL,
};
sigemptyset(&sa.sa_mask);
sigaction(SIGSEGV, &sa, NULL);
}
}
static void *tls_get(void) {
void *tcb;
asm volatile("mov %%gs:0, %0" : "=r"(tcb));
return tcb;
}
static void tls_set(const void *tcb) {
asm volatile("mov %0, %%gs:0x28" : : "r"(tcb));
}
#endif
int main(int argc, char** argv) {
CreateIfNeededWindow();
#if defined(__x86_64__) && defined(__APPLE__)
// On OS X there doesn't seem to be any way to modify the %fs base.
// Let's use %gs instead. Install a signal handler for SIGSEGV to
// dynamically patch up instructions that access %fs.
static bool handler_set_up = false;
if (!handler_set_up) {
struct sigaction sa = {
.sa_sigaction = handle_sigsegv,
.sa_flags = SA_SIGINFO,
};
sigemptyset(&sa.sa_mask);
sigaction(SIGSEGV, &sa, NULL);
handler_set_up = true;
}
void * val2 = tls_get();
static uintptr_t guard = 0;
tls_set(&guard);
#endif
solist_init();
std::unordered_map<std::string, void *> symbols;
for (size_t i = 0; main_hooks[i].name; i++) {
symbols[main_hooks[i].name] = main_hooks[i].func;
}
for (size_t i = 0; dirent_hooks[i].name; i++) {
symbols[dirent_hooks[i].name] = dirent_hooks[i].func;
}
for (size_t i = 0; io_hooks[i].name; i++) {
symbols[io_hooks[i].name] = io_hooks[i].func;
}
for (size_t i = 0; net_hooks[i].name; i++) {
symbols[net_hooks[i].name] = net_hooks[i].func;
}
#if defined(__APPLE__)
for (size_t i = 0; net_darwin_hooks[i].name; i++) {
symbols[net_darwin_hooks[i].name] = net_darwin_hooks[i].func;
}
#endif
for (size_t i = 0; pthread_hooks[i].name; i++) {
symbols[pthread_hooks[i].name] = pthread_hooks[i].func;
}
auto h = dlopen("libm."
#ifdef __APPLE__
"dylib"
#else
"so.1"
#endif
, RTLD_LAZY);
for (size_t i = 0; libm_symbols[i]; i++) {
symbols[libm_symbols[i]] = dlsym(h, libm_symbols[i]);
}
symbols["newlocale"] = (void*)newlocale;
symbols["uselocale"] = (void*)uselocale;
symbols["mbsrtowcs"] = (void*)mbsrtowcs;
symbols["freelocale"] = (void*)freelocale;
symbols["iswlower"] = (void*)iswlower;
symbols["iswprint"] = (void*)iswprint;
symbols["iswblank"] = (void*)iswblank;
symbols["iswcntrl"] = (void*)iswcntrl;
symbols["iswupper"] = (void*)iswupper;
symbols["iswalpha"] = (void*)iswalpha;
symbols["iswdigit"] = (void*)iswdigit;
symbols["iswpunct"] = (void*)iswpunct;
symbols["iswxdigit"] = (void*)iswxdigit;
symbols["wcsnrtombs"] = (void*)wcsnrtombs;
symbols["mbsnrtowcs"] = (void*)mbsnrtowcs;
symbols["__ctype_get_mb_cur_max"] = (void*) + []() -> size_t {
return 4;
};
symbols["mbrlen"] = (void*)mbrlen;
symbols["vasprintf"] = (void*)+ []() {
};
symbols["wcstol"] = (void*)wcstol;
symbols["wcstoul"] = (void*)wcstoul;
symbols["wcstoll"] = (void*)wcstoll;
symbols["wcstoull"] = (void*)wcstoull;
symbols["wcstof"] = (void*)wcstof;
symbols["wcstod"] = (void*)wcstod;
symbols["wcstold"] = (void*)wcstold;
symbols["swprintf"] = (void*)swprintf;
symbols["android_set_abort_message"] = (void*)+[](const char msg) {
};
symbols["sigemptyset"] = (void*)sigemptyset;
symbols["sigaddset"] = (void*)sigaddset;
symbols["arc4random"] = (void*)+[]() -> uint32_t{
return 0;
};
symbols["strptime"] = (void*)+[]() {
};
symbols["strptime_l"] = (void*)+[]() {
};
symbols["__FD_SET_chk"] = (void*)+[]() {
};
symbols["__FD_ISSET_chk"] = (void*)+[]() {
};
#ifdef __linux__
symbols["epoll_create1"] = (void*)epoll_create1;
symbols["eventfd"] = (void*)+[](unsigned int __count, int __flags) {
return 2; // Bad stub
};
#else
symbols["epoll_create1"] = (void*)+[](int flags) {
return epoll_create(100);
};
symbols["eventfd"] = (void*)+[](unsigned int __count, int __flags) {
return 2;
};
#endif
symbols["__memcpy_chk"] = (void*) + [](void* dst, const void* src, size_t count, size_t dst_len) -> void*{
return memcpy(dst, src, count);
};
symbols["__vsnprintf_chk"] = (void*) + [](char* dst, size_t supplied_size, int /*flags*/,
size_t dst_len_from_compiler, const char* format, va_list va) -> int {
return vsnprintf(dst, supplied_size, format, va);
};
symbols["__fgets_chk"] = (void*) + [](char* dst, int supplied_size, FILE* stream, size_t dst_len_from_compiler) {
return fgets(dst, supplied_size, stream);
};
// symbols["__libc_init"] = (void*)+ []() {
// };
// symbols["isascii"] = (void*)isascii;
// symbols["sigsetjmp"] = (void*)__sigsetjmp;
// symbols["siglongjmp"] = (void*)siglongjmp;
// symbols["wprintf"] = (void*)wprintf;
// symbols["sigfillset"] = (void*)sigfillset;
// symbols["pthread_sigmask"] = (void*)pthread_sigmask;
// symbols["lstat"] = (void*)lstat;
// symbols["statfs"] = (void*)statfs;
// soinfo::load_library("libhybris.so", symbols);
soinfo::load_library("libdl.so", { { std::string("dl_iterate_phdr"), (void*)&__loader_dl_iterate_phdr },
{ std::string("dlopen"), (void*)+ [](const char * filename, int flags)-> void* {
return __loader_dlopen(filename, flags, nullptr);
}},
{ std::string("dlsym"), (void*)+ [](void* dl, const char * name)-> void* {
return __loader_dlsym(dl, name, nullptr);
}},
{ std::string("dlclose"), (void*)&__loader_dlclose },
{ std::string("dlerror"), (void*)&__loader_dlerror},
});
soinfo::load_library("libdl.so.2", { { std::string("dl_iterate_phdr"), (void*)&__loader_dl_iterate_phdr },
{ std::string("dlopen"), (void*)+ [](const char * filename, int flags)-> void* {
return __loader_dlopen(filename, flags, nullptr);
}},
{ std::string("dlsym"), (void*)+ [](void* dl, const char * name)-> void* {
return __loader_dlsym(dl, name, nullptr);
}},
{ std::string("dlclose"), (void*)&__loader_dlclose },
{ std::string("dlerror"), (void*)&__loader_dlerror},
});
symbols["_ZN6cohtml17VerifiyLicenseKeyEPKc"] = (void*) + []() {
return true;
};
symbols["_ZN3web4http6client7details35verify_cert_chain_platform_specificERN5boost4asio3ssl14verify_contextERKSs"] = (void*) + []() {
return true;
};
static std::promise<std::pair<void *(*)(void*), void *>> pthread_main;
auto fut = pthread_main.get_future();
static std::atomic_bool run_pthread_main(true);
static pthread_t pthread_main_v = pthread_self();
symbols["pthread_create"] = (void*) + [](pthread_t *thread, const pthread_attr_t *__attr, void *(*start_routine)(void*), void *arg) -> int {
if(run_pthread_main.load()) {
run_pthread_main.store(false);
*thread = pthread_main_v;
pthread_main.set_value({start_routine, arg});
return 0;
}
return my_pthread_create(thread, __attr, start_routine, arg);
};
// Hack pthread to run mainthread on the main function #macoscacoa support
// static std::atomic_bool uithread_started;
// uithread_started = false;
// static void *(*main_routine)(void*) = nullptr;
// static void *main_arg = nullptr;
// static pthread_t mainthread = pthread_self();
// static int (*my_pthread_create)(pthread_t *thread, const pthread_attr_t *__attr,
// void *(*start_routine)(void*), void *arg) = 0;
// // my_pthread_create = (int (*)(pthread_t *thread, const pthread_attr_t *__attr,
// // void *(*start_routine)(void*), void *arg))get_hooked_symbol("pthread_create");
// hybris_hook("pthread_create", (void*) + [](pthread_t *thread, const pthread_attr_t *__attr,
// void *(*start_routine)(void*), void *arg) {
// if(uithread_started.load()) {
// return my_pthread_create(thread, __attr, start_routine, arg);
// } else {
// uithread_started = true;
// *thread = mainthread;
// main_routine = start_routine;
// main_arg = arg;
// return 0;
// }
// }
// );
// symbols["pthread_create"] = (void*) my_pthread_create;
soinfo::load_library("libc.so", symbols);
soinfo::load_library("libc.so.6", symbols);
soinfo::load_empty_library("libpthread.so.0");
// symbols.clear();
// auto h = dlopen("libm.so.6", RTLD_LAZY);
// for (size_t i = 0; libm_symbols[i]; i++) {
// symbols[libm_symbols[i]] = dlsym(h, libm_symbols[i]);
// }
soinfo::load_library("libm.so", /* symbols */ {});
symbols.clear();
for (size_t i = 0; egl_symbols[i]; i++) {
symbols[egl_symbols[i]] = (void*)+[]() {
std::cout << "egl_symbols Stub called" << "\n";
};
}
symbols["eglGetProcAddress"] = (void*) + [](const char* name) -> void* {
return nullptr;
};
InstallEGL(symbols);
soinfo::load_library("libEGL.so", symbols);
symbols.clear();
symbols["__android_log_print"] = (void*) __android_log_print;
symbols["__android_log_vprint"] = (void*) __android_log_vprint;
symbols["__android_log_write"] = (void*) __android_log_write;
soinfo::load_library("liblog.so", symbols);
symbols.clear();
for (size_t i = 0; android_symbols[i]; i++) {
symbols[android_symbols[i]] = (void*)+[]() {
};
}
InstallALooper(symbols);
soinfo::load_library("libandroid.so", symbols);
soinfo::load_library("libOpenSLES.so", { });
// char s[] = "/home/christopher/cpprestsdk/Build_android/build/build.x86_64.debug/Release/Binaries";
// auto es = chdir(s);
auto libcpp = __loader_dlopen("../libs/libc++_shared.so", 0, 0);
if(!libcpp) {
libcpp = __loader_dlopen("../libs/libgnustl_shared.so", 0, 0);
}
symbols.clear();
for (size_t i = 0; fmod_symbols[i]; i++) {
symbols[fmod_symbols[i]] = (void*)+[]() {
};
}
soinfo::load_library("libfmod.so", symbols);
// auto libcrypro = __loader_dlopen("./libcrypto.so", 0, 0);
// auto libssl = __loader_dlopen("./libssl.so", 0, 0);
void * libmcpe = __loader_dlopen("../libs/libminecraftpe.so", 0, 0);
if(!libmcpe) {
std::cout << "Please change the current working directory to the assets folder.\nOn linux e.g \"cd ~/.local/share/mcpelauncher/versions/1.16.0.55/assets\"\n";
return -1;
}
auto vm = std::make_shared<jnivm::VM>();
///Fake act
auto mainActivity = std::make_shared<jnivm::Object>();
auto MainActivity_ = vm->GetEnv()->GetClass("com/mojang/minecraftpe/MainActivity");
mainActivity->clazz = MainActivity_;
MainActivity_->HookInstanceFunction(vm->GetEnv().get(), "createUUID", [](jnivm::ENV*env, jnivm::Object*obj) -> std::shared_ptr<jnivm::String> {
return std::make_shared<jnivm::String>("daa78df1-373a-444d-9b1d-4c71a14bb559");
});
struct ClassLoader : jnivm::Object { };
auto ClassLoader_ = vm->GetEnv()->GetClass<ClassLoader>("java/lang/ClassLoader");
MainActivity_->HookInstanceFunction(vm->GetEnv().get(), "getClassLoader", [](jnivm::ENV*env, jnivm::Object*obj) -> std::shared_ptr<ClassLoader> {
return std::make_shared<ClassLoader>();
});
// OLD BEGIN
auto env = vm->GetEnv();
MainActivity_->Hook(env.get(), "hasWriteExternalStoragePermission", [](jnivm::ENV*env, jnivm::Object*obj) -> jboolean {
return 1;
});
MainActivity_->Hook(env.get(), "isNetworkEnabled", [](jnivm::ENV*env, jnivm::Object*obj, jboolean b) -> jboolean {
printf("isNetworkEnabled %d\n", (int)b);
return 1;
});
MainActivity_->HookInstanceFunction(env.get(), "launchUri", [](jnivm::ENV*env, jnivm::Object*obj, std::shared_ptr<jnivm::String> uri) {
Log::trace("Launch URI", "%s", uri->data());
});
// MainActivity_->HookInstanceFunction(env.get(), "tick", [](jnivm::ENV*env, jnivm::Object*obj) {
// if(window)
// window->swapBuffers();
// });
struct StoreListener : jnivm::Object {
jlong nstorelisterner;
};
struct NativeStoreListener : StoreListener {
};
auto NativeStoreListener_ = env->GetClass<NativeStoreListener>("com/mojang/minecraftpe/store/NativeStoreListener");
NativeStoreListener_->Hook(env.get(), "<init>", [](jnivm::ENV*env, jnivm::Class*cl, jlong arg0) -> std::shared_ptr<NativeStoreListener> {
auto storel = std::make_shared<NativeStoreListener>();
storel->nstorelisterner = arg0;
return storel;
});
// Show Gamepad Options
auto Build = env->GetClass("android/os/Build$VERSION");
Build->HookGetterFunction(env.get(), "SDK_INT", [](jnivm::ENV*env, jnivm::Class*cl) -> jint {
return 28;
});
// Make pictures loading, advance apilevel
MainActivity_->HookInstanceFunction(env.get(), "getAndroidVersion", [](jnivm::ENV*env, jnivm::Object*obj) -> jint {
return 28;
});
auto StoreListener_ = env->GetClass<StoreListener>("com/mojang/minecraftpe/store/StoreListener");
struct Store : jnivm::Object {
};
auto Store_ = env->GetClass<Store>("com/mojang/minecraftpe/store/Store");
Store_->HookInstanceFunction(env.get(), "receivedLicenseResponse", [](jnivm::ENV* env, jnivm::Object* store) -> jboolean {
return true;
});
Store_->HookInstanceFunction(env.get(), "hasVerifiedLicense", [](jnivm::ENV* env, jnivm::Object* store) -> jboolean {
return true;