AdhityaRimba

main.cpp

Mar 12th, 2020
587
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.60 KB | None | 0 0
  1. #include <jni.h>
  2. #include <android/log.h>
  3. #include <ucontext.h>
  4. #include <pthread.h>
  5.  
  6. #include "main.h"
  7. #include "game/game.h"
  8. #include "game/RW/RenderWare.h"
  9. #include "net/netgame.h"
  10. #include "gui/gui.h"
  11. #include "chatwindow.h"
  12. #include "spawnscreen.h"
  13. #include "playertags.h"
  14. #include "dialog.h"
  15. #include "keyboard.h"
  16. #include "scoreboard.h"
  17. #include "settings.h"
  18. #include "button.h"
  19. #include "debug.h"
  20.  
  21. #include "util/armhook.h"
  22. #include "checkfilehash.h"
  23. #include "str_obfuscator_no_template.hpp"
  24.  
  25. uintptr_t g_libGTASA = 0;
  26. const char* g_pszStorage = nullptr;
  27.  
  28. char SERVER_IP[30] = "139.99.89.111";
  29. int SERVER_PORT = 7777;
  30.  
  31. CGame *pGame = nullptr;
  32. CNetGame *pNetGame = nullptr;
  33. CChatWindow *pChatWindow = nullptr;
  34. CSpawnScreen *pSpawnScreen = nullptr;
  35. CPlayerTags *pPlayerTags = nullptr;
  36. CDialogWindow *pDialogWindow = nullptr;
  37.  
  38. CGUI *pGUI = nullptr;
  39. CKeyBoard *pKeyBoard = nullptr;
  40. CButton *pButton = nullptr;
  41. CDebug *pDebug = nullptr;
  42. CSettings *pSettings = nullptr;
  43.  
  44. CScoreBoard *pScoreBoard = nullptr;
  45.  
  46. void InitHookStuff();
  47. void InstallSpecialHooks();
  48. void InitRenderWareFunctions();
  49. void ApplyInGamePatches();
  50. void ApplyPatches_level0();
  51. void MainLoop();
  52.  
  53. void InitSAMP()
  54. {
  55.     Log("Initializing SAMP..");
  56.     g_pszStorage = (const char*)(g_libGTASA+0x63C4B8);
  57.  
  58.     if(!g_pszStorage)
  59.     {
  60.         Log("Error: storage path not found!");
  61.         std::terminate();
  62.         return;
  63.     }
  64.  
  65.     Log("Storage: %s", g_pszStorage);
  66.  
  67.     pSettings = new CSettings();
  68.  
  69.     Log("Checking samp files..");
  70.     if(!FileCheckSum())
  71.     {
  72.         Log("SOME FILES HAVE BEEN MODIFIED. YOU NEED REINSTALL SAMP!");
  73.         std::terminate();
  74.         return;
  75.     }
  76. }
  77.  
  78. void InitInMenu()
  79. {
  80.     pGame = new CGame();
  81.     pGame->InitInMenu();
  82.    
  83.     if(pSettings->Get().bDebug)
  84.         pDebug = new CDebug();
  85.  
  86.     pGUI = new CGUI();
  87.     pKeyBoard = new CKeyBoard();
  88.     pChatWindow = new CChatWindow();
  89.     pSpawnScreen = new CSpawnScreen();
  90.     pPlayerTags = new CPlayerTags();
  91.     pDialogWindow = new CDialogWindow();
  92.     pButton = new CButton();
  93.     pScoreBoard = new CScoreBoard();
  94. }
  95.  
  96. void InitInGame()
  97. {
  98.     static bool bGameInited = false;
  99.     static bool bNetworkInited = false;
  100.    
  101.     // Password:
  102.     /*char PW1[32] = "J";
  103.     char PW2[32] = "2";
  104.     char PW3[32] = "T";
  105.     char PW4[32] = "D";
  106.     char PW5[32] = "0";
  107.     char szPassword[32] = "";
  108.    
  109.     sprintf(szPassword, "%s%s%s%s%s", PW1, PW2, PW3, PW4, PW5);
  110.     */
  111.     if(!bGameInited)
  112.     {
  113.         pGame->InitInGame();
  114.         pGame->SetMaxStats();
  115.  
  116.         if(pDebug && !pSettings->Get().bOnline)
  117.         {
  118.             pDebug->SpawnLocalPlayer();
  119.         }
  120.  
  121.         bGameInited = true;
  122.         return;
  123.     }
  124.  
  125.     if(!bNetworkInited && pSettings->Get().bOnline)
  126.     {
  127.         pNetGame = new CNetGame(encryptedAddress.decrypt(), port, pSettings->Get().szNickName,pSettings->Get().szPassword);
  128.         bNetworkInited = true;
  129.         return;
  130.     }
  131. }
  132.  
  133. void MainLoop()
  134. {
  135.     InitInGame();
  136.  
  137.     if(pDebug) pDebug->Process();
  138.     if(pNetGame) pNetGame->Process();
  139. }
  140.  
  141. void handler(int signum, siginfo_t *info, void* contextPtr)
  142. {
  143.     ucontext* context = (ucontext_t*)contextPtr;
  144.  
  145.     if(info->si_signo == SIGSEGV)
  146.     {
  147.         Log("SIGSEGV | Fault address: 0x%X", info->si_addr);
  148.         Log("libGTASA base address: 0x%X", g_libGTASA);
  149.         Log("register states:");
  150.  
  151.         Log("r0: 0x%X, r1: 0x%X, r2: 0x%X, r3: 0x%X",
  152.             context->uc_mcontext.arm_r0,
  153.             context->uc_mcontext.arm_r1,
  154.             context->uc_mcontext.arm_r2,
  155.             context->uc_mcontext.arm_r3);
  156.         Log("r4: 0x%x, r5: 0x%x, r6: 0x%x, r7: 0x%x",
  157.             context->uc_mcontext.arm_r4,
  158.             context->uc_mcontext.arm_r5,
  159.             context->uc_mcontext.arm_r6,
  160.             context->uc_mcontext.arm_r7);
  161.         Log("r8: 0x%x, r9: 0x%x, sl: 0x%x, fp: 0x%x",
  162.             context->uc_mcontext.arm_r8,
  163.             context->uc_mcontext.arm_r9,
  164.             context->uc_mcontext.arm_r10,
  165.             context->uc_mcontext.arm_fp);
  166.         Log("ip: 0x%x, sp: 0x%x, lr: 0x%x, pc: 0x%x",
  167.             context->uc_mcontext.arm_ip,
  168.             context->uc_mcontext.arm_sp,
  169.             context->uc_mcontext.arm_lr,
  170.             context->uc_mcontext.arm_pc);
  171.  
  172.         Log("backtrace:");
  173.         Log("1: libGTASA.so + 0x%X", context->uc_mcontext.arm_pc - g_libGTASA);
  174.         Log("2: libGTASA.so + 0x%X", context->uc_mcontext.arm_lr - g_libGTASA);
  175.  
  176.         exit(0);
  177.     }
  178.  
  179.     return;
  180. }
  181.  
  182. void *Init(void *p)
  183. {
  184.     ApplyPatches_level0();
  185.  
  186.     pthread_exit(0);
  187. }
  188.  
  189. jint JNI_OnLoad(JavaVM *vm, void *reserved)
  190. {
  191.     Log("SAMP library loaded! Build time: " __DATE__ " " __TIME__);
  192.  
  193.     g_libGTASA = FindLibrary("libGTASA.so");
  194.     if(g_libGTASA == 0)
  195.     {
  196.         Log("ERROR: libGTASA.so address not found!");
  197.         return 0;
  198.     }
  199.  
  200.     Log("libGTASA.so image base address: 0x%X", g_libGTASA);
  201.  
  202.     srand(time(0));
  203.  
  204.     InitHookStuff();
  205.     InitRenderWareFunctions();
  206.     InstallSpecialHooks();
  207.  
  208.     pthread_t thread;
  209.     pthread_create(&thread, 0, Init, 0);
  210.  
  211.     struct sigaction act;
  212.     act.sa_sigaction = handler;
  213.     sigemptyset(&act.sa_mask);
  214.     act.sa_flags = SA_SIGINFO;
  215.     sigaction(SIGSEGV, &act, 0);
  216.  
  217.     return JNI_VERSION_1_4;
  218. }
  219.  
  220. void Log(const char *fmt, ...)
  221. {  
  222.     char buffer[0xFF];
  223.     static FILE* flLog = nullptr;
  224.  
  225.     if(flLog == nullptr && g_pszStorage != nullptr)
  226.     {
  227.         sprintf(buffer, "%sSAMP/samp_log.txt", g_pszStorage);
  228.         flLog = fopen(buffer, "a");
  229.     }
  230.  
  231.     memset(buffer, 0, sizeof(buffer));
  232.  
  233.     va_list arg;
  234.     va_start(arg, fmt);
  235.     vsnprintf(buffer, sizeof(buffer), fmt, arg);
  236.     va_end(arg);
  237.  
  238.     __android_log_write(ANDROID_LOG_INFO, "AXL", buffer);
  239.  
  240.     if(pDebug) pDebug->AddMessage(buffer);
  241.  
  242.     char buff[80];
  243.     time_t seconds = time(NULL);
  244.     tm* timeinfo = localtime(&seconds);
  245.     char* format = "[%I:%M:%S]";
  246.     strftime(buff, 80, format, timeinfo);
  247.  
  248.     if(flLog == nullptr) return;
  249.     fprintf(flLog, "%s %s\n", buff, buffer);
  250.     fflush(flLog);
  251.  
  252.     return;
  253. }
  254.  
  255. uint32_t GetTickCount()
  256. {
  257.     struct timeval tv;
  258.     gettimeofday(&tv, nullptr);
  259.     return (tv.tv_sec*1000+tv.tv_usec/1000);
  260. }
Advertisement
Add Comment
Please, Sign In to add comment