Advertisement
Guest User

gameplg.c

a guest
Jul 15th, 2016
546
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. #include "global.h"
  2.  
  3. #define WRITEU8(addr, data) *(vu8*)(addr) = data
  4. #define WRITEU16(addr, data) *(vu16*)(addr) = data
  5. #define WRITEU32(addr, data) *(vu32*)(addr) = data
  6.  
  7. u32 threadStack[0x1000];
  8. Handle fsUserHandle;
  9. FS_archive sdmcArchive;
  10.  
  11.  
  12. #define IO_BASE_PAD     1
  13. #define IO_BASE_LCD     2
  14. #define IO_BASE_PDC     3
  15. #define IO_BASE_GSPHEAP     4
  16.  
  17. u32 IoBasePad = 0xFFFD4000;
  18.  
  19. u32 getKey() {
  20.     return (*(vu32*)(IoBasePad) ^ 0xFFF) & 0xFFF;
  21. }
  22.  
  23. void waitKeyUp() {
  24.     while (getKey() != 0) {
  25.         svc_sleepThread(100000000);
  26.     }
  27. }
  28.  
  29. u8 cheatEnabled[64];
  30. int isNewNtr = 0;
  31.  
  32.  
  33. u32 plgGetIoBase(u32 IoType);
  34. GAME_PLUGIN_MENU gamePluginMenu;
  35.  
  36. void initMenu() {
  37.     memset(&gamePluginMenu, 0, sizeof(GAME_PLUGIN_MENU));
  38. }
  39.  
  40. void addMenuEntry(u8* str) {
  41.     if (gamePluginMenu.count > 64) {
  42.         return;
  43.     }
  44.     u32 pos = gamePluginMenu.count;
  45.     u32 len = strlen(str) + 1;
  46.     gamePluginMenu.offsetInBuffer[pos] = gamePluginMenu.bufOffset;
  47.     strcpy(&(gamePluginMenu.buf[gamePluginMenu.bufOffset]), str);
  48.    
  49.     gamePluginMenu.count += 1;
  50.     gamePluginMenu.bufOffset += len;
  51. }
  52.  
  53. u32 updateMenu() {
  54.     PLGLOADER_INFO *plgLoaderInfo = (void*)0x07000000;
  55.     plgLoaderInfo->gamePluginPid = getCurrentProcessId();
  56.     plgLoaderInfo->gamePluginMenuAddr = (u32)&gamePluginMenu;
  57.    
  58.     u32 ret = 0;
  59.     u32 hProcess;
  60.     u32 homeMenuPid = plgGetIoBase(5);
  61.     if (homeMenuPid == 0) {
  62.         return 1;
  63.     }
  64.     ret = svc_openProcess(&hProcess, homeMenuPid);
  65.     if (ret != 0) {
  66.         return ret;
  67.     }
  68.     copyRemoteMemory( hProcess, &(plgLoaderInfo->gamePluginPid), CURRENT_PROCESS_HANDLE,  &(plgLoaderInfo->gamePluginPid), 8);
  69.     final:
  70.     svc_closeHandle(hProcess);
  71.     return ret;
  72. }
  73.  
  74. int scanMenu() {
  75.     u32 i;
  76.     for (i = 0; i < gamePluginMenu.count; i++) {
  77.         if (gamePluginMenu.state[i]) {
  78.             gamePluginMenu.state[i] = 0;
  79.             return i;
  80.         }
  81.     }
  82.     return -1;
  83. }
  84.  
  85. // detect language (0: english)
  86. int detectLanguage() {
  87.     // unimplemented
  88.     return 0;
  89. }
  90.  
  91. // add one cheat menu entry
  92. void addCheatMenuEntry(u8* str) {
  93.     u8 buf[100];
  94.     xsprintf(buf, "[ ] %s", str);
  95.     addMenuEntry(buf);
  96. }
  97.  
  98. // this function will be called when the state of cheat item changed
  99. void onCheatItemChanged(int id, int enable) {
  100.     // TODO: handle on cheat item is select or unselected
  101.    
  102.  
  103. }
  104.  
  105. // freeze the value
  106. void freezeCheatValue() {
  107.     if (cheatEnabled[0]) {
  108.         WRITEU16(0x0016AADC, 0x00000012);
  109.     }
  110.     if (cheatEnabled[1]) {
  111.         WRITEU16(0x0016AADC, 0x000000F2);
  112.     }
  113.     // TODO: handle your own cheat items
  114. }
  115.  
  116. // update the menu status
  117. void updateCheatEnableDisplay(id) {
  118.     gamePluginMenu.buf[gamePluginMenu.offsetInBuffer[id] + 1] = cheatEnabled[id] ? 'X' : ' ';
  119. }
  120.  
  121. // scan and handle events
  122. void scanCheatMenu() {
  123.     int ret = scanMenu();
  124.     if (ret != -1) {
  125.         cheatEnabled[ret] = !cheatEnabled[ret];
  126.         updateCheatEnableDisplay(ret);
  127.         onCheatItemChanged(ret, cheatEnabled[ret]);
  128.     }
  129. }
  130.  
  131. // init
  132. void initCheatMenu() {
  133.     initMenu();
  134.     addCheatMenuEntry("Cheat1");
  135.     addCheatMenuEntry("Cheat2");
  136.     // TODO: Add your own menu entries
  137.    
  138.     updateMenu();
  139. }
  140.  
  141. void gamePluginEntry() {
  142.     u32 ret, key;
  143.     INIT_SHARED_FUNC(plgGetIoBase, 8);
  144.     INIT_SHARED_FUNC(copyRemoteMemory, 9);
  145.     // wait for game starts up (5 seconds)
  146.     svc_sleepThread(5000000000);
  147.  
  148.     if (((NS_CONFIG*)(NS_CONFIGURE_ADDR))->sharedFunc[8]) {
  149.         isNewNtr = 1;
  150.     } else {
  151.         isNewNtr = 0;
  152.     }
  153.    
  154.     if (isNewNtr) {
  155.         IoBasePad = plgGetIoBase(IO_BASE_PAD);
  156.     }
  157.     initCheatMenu();
  158.     while (1) {
  159.         svc_sleepThread(100000000);
  160.         scanCheatMenu();
  161.         freezeCheatValue();
  162.     }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement