int applyAllCheats() { int i = 0, j = 0; for(i = 0; i < _cheats.num_cheats; i++) { CheatEntry *cheat = &_cheats.cheat[i]; cheat->enabled = 1; if(getEnabledCheatsSize() > kenobi_get_maxsize()) cheat->enabled = 0; j++; } return j; }
// Installs the GeckoOS (kenobiGC) cheats engine and sets up variables/copies cheats void kenobi_install_engine() { int isDebug = swissSettings.wiirdDebug; // If high memory is in use, we'll use low, otherwise high. u8 *ptr = isDebug ? kenobigc_dbg_bin : kenobigc_bin; u32 size = isDebug ? kenobigc_dbg_bin_size : kenobigc_bin_size; print_gecko("Copying kenobi%s to %08X\r\n", (isDebug?"_dbg":""),(u32)CHEATS_ENGINE); memcpy(CHEATS_ENGINE, ptr, size); memcpy(CHEATS_GAMEID, (void*)0x80000000, CHEATS_GAMEID_LEN); if(!isDebug) { CHEATS_ENABLE_CHEATS = CHEATS_TRUE; } CHEATS_START_PAUSED = isDebug ? CHEATS_TRUE : CHEATS_FALSE; memset(CHEATS_LOCATION(size), 0, kenobi_get_maxsize()); print_gecko("Copying %i bytes of cheats to %08X\r\n", getEnabledCheatsSize(),(u32)CHEATS_LOCATION(size)); u32 *cheatsLocation = (u32*)CHEATS_LOCATION(size); cheatsLocation[0] = 0x00D0C0DE; cheatsLocation[1] = 0x00D0C0DE; cheatsLocation+=2; int i = 0, j = 0; for(i = 0; i < _cheats.num_cheats; i++) { CheatEntry *cheat = &_cheats.cheat[i]; if(cheat->enabled) { for(j = 0; j < cheat->num_codes; j++) { // Copy & fix cheats that want to jump to the old cheat engine location 0x800018A8 -> CHEATS_ENGINE+0xA8 cheatsLocation[0] = cheat->codes[j][0]; cheatsLocation[1] = cheat->codes[j][1] == 0x800018A8 ? (u32)(CHEATS_ENGINE+0xA8) : cheat->codes[j][1]; cheatsLocation+=2; } } } cheatsLocation[0] = 0xFF000000; DCFlushRange((void*)CHEATS_ENGINE, WIIRD_ENGINE_SPACE); ICInvalidateRange((void*)CHEATS_ENGINE, WIIRD_ENGINE_SPACE); }
void DrawCheatsSelector(char *fileName) { CheatEntries* cheats = getCheats(); int cheat_selection = 0; int cheats_per_page = 6; while ((PAD_ButtonsHeld(0) & PAD_BUTTON_A)){ VIDEO_WaitVSync (); } while(1) { doBackdrop(); DrawEmptyBox(20,60, vmode->fbWidth-20, 460, COLOR_BLACK); sprintf(txtbuffer, "%s Cheats:", fileName); WriteFontStyled(25, 62, txtbuffer, GetTextScaleToFitInWidth(txtbuffer, vmode->fbWidth-50), false, defaultColor); int j = 0; int current_view_start = MIN(MAX(0,cheat_selection-cheats_per_page/2),MAX(0,cheats->num_cheats-cheats_per_page)); int current_view_end = MIN(cheats->num_cheats, MAX(cheat_selection+cheats_per_page/2,cheats_per_page)); int scrollBarHeight = 90+(cheats_per_page*20); int scrollBarTabHeight = (int)((float)scrollBarHeight/(float)cheats->num_cheats); DrawVertScrollBar(vmode->fbWidth-45, 120, 25, scrollBarHeight, (float)((float)cheat_selection/(float)(cheats->num_cheats-1)),scrollBarTabHeight); for(j = 0; current_view_start<current_view_end; ++current_view_start,++j) { drawCheatForCheatsSelector(&cheats->cheat[current_view_start], 25, 120+j*35, current_view_start==cheat_selection); } // Write about how many cheats are enabled DrawTransparentBox( 35, 350, vmode->fbWidth-35, 410); WriteFontStyled(33, 345, "Space taken by cheats:", 0.8f, false, defaultColor); GXColor noColor = (GXColor) {0,0,0,0}; //blank GXColor borderColor = (GXColor) {200,200,200,GUI_MSGBOX_ALPHA}; //silver GXColor progressBarColor = (GXColor) {255,128,0,GUI_MSGBOX_ALPHA}; //orange float multiplier = (float)getEnabledCheatsSize() / (float)kenobi_get_maxsize(); DrawSimpleBox( 33, 370, vmode->fbWidth-66, 20, 0, noColor, borderColor); DrawSimpleBox( 33, 370, (int)((vmode->fbWidth-66)*multiplier), 20, 0, progressBarColor, noColor); sprintf(txtbuffer, "WiiRD Debug %s", swissSettings.wiirdDebug ? "Enabled":"Disabled"); WriteFontStyled(33, 395, txtbuffer, 0.8f, false, defaultColor); WriteFontStyled(640/2, 440, "(A) Toggle Cheat - (X) WiiRD Debug - (B) Return", 0.9f, true, defaultColor); DrawFrameFinish(); while (!(PAD_ButtonsHeld(0) & (PAD_BUTTON_UP|PAD_BUTTON_DOWN|PAD_BUTTON_B|PAD_BUTTON_A|PAD_BUTTON_X))) { VIDEO_WaitVSync (); } u16 btns = PAD_ButtonsHeld(0); if(btns & (PAD_BUTTON_UP|PAD_BUTTON_DOWN)) { cheat_selection = btns & PAD_BUTTON_UP ? ((--cheat_selection < 0) ? cheats->num_cheats-1 : cheat_selection) :((cheat_selection + 1) % cheats->num_cheats); } if(btns & PAD_BUTTON_A) { cheats->cheat[cheat_selection].enabled ^= 1; if(getEnabledCheatsSize() > kenobi_get_maxsize()) // No room cheats->cheat[cheat_selection].enabled = 0; } if(btns & PAD_BUTTON_X) { swissSettings.wiirdDebug ^=1; } if(btns & PAD_BUTTON_B) { break; } while (PAD_ButtonsHeld(0) & (PAD_BUTTON_UP|PAD_BUTTON_DOWN|PAD_BUTTON_B|PAD_BUTTON_A|PAD_BUTTON_X)) { VIDEO_WaitVSync (); } } }
int patch_gcm(file_handle *file, ExecutableFile *filesToPatch, int numToPatch, int multiDol) { int i, num_patched = 0; // If the current device isn't SD Gecko, init SD Gecko Slot A or B to write patches. if(deviceHandler_initial != &initial_SD0 && deviceHandler_initial != &initial_SD1) { deviceHandler_setStatEnabled(0); if(deviceHandler_FAT_init(&initial_SD0)) { savePatchDevice = 0; } else if(deviceHandler_FAT_init(&initial_SD1)) { savePatchDevice = 1; } deviceHandler_setStatEnabled(1); } // Already using SD Gecko if(deviceHandler_initial == &initial_SD0) savePatchDevice = 0; else if(deviceHandler_initial == &initial_SD1) savePatchDevice = 1; if(savePatchDevice == -1) { DrawFrameStart(); DrawMessageBox(D_FAIL, "No writable device present\nA SD Gecko must be inserted in\n order to utilise patches for this game."); DrawFrameFinish(); sleep(5); return 0; } char patchFileName[256]; char patchDirName[256]; char patchBaseDirName[256]; char gameID[8]; memset(&gameID, 0, 8); memset(&patchDirName, 0, 256); memset(&patchBaseDirName, 0, 256); strncpy((char*)&gameID, (char*)&GCMDisk, 4); sprintf(&patchDirName[0],"%s:/swiss_patches/%s",(savePatchDevice ? "sdb":"sda"), &gameID[0]); sprintf(&patchBaseDirName[0],"%s:/swiss_patches",(savePatchDevice ? "sdb":"sda")); print_gecko("Patch dir will be: %s if required\r\n", patchDirName); *(u32*)VAR_EXECD_OFFSET = 0xFFFFFFFF; // Go through all the possible files we think need patching.. for(i = 0; i < numToPatch; i++) { u32 patched = 0, crc32 = 0; sprintf(txtbuffer, "Patching File %i/%i",i+1,numToPatch); DrawFrameStart(); DrawProgressBar((int)(((float)(i+1)/(float)numToPatch)*100), txtbuffer); DrawFrameFinish(); // Round up to 32 bytes if(filesToPatch[i].size % 0x20) { filesToPatch[i].size += (0x20-(filesToPatch[i].size%0x20)); } if(filesToPatch[i].size > 8*1024*1024) { print_gecko("Skipping %s %iKB too large\r\n", filesToPatch[i].name, filesToPatch[i].size/1024); continue; } print_gecko("Checking %s %iKb\r\n", filesToPatch[i].name, filesToPatch[i].size/1024); if(strstr(filesToPatch[i].name, "execD.")) { *(u32*)VAR_EXECD_OFFSET = filesToPatch[i].offset; } if(strstr(filesToPatch[i].name, "iwanagaD.dol") || strstr(filesToPatch[i].name, "switcherD.dol")) { continue; // skip unused PSO files } int sizeToRead = filesToPatch[i].size; u8 *buffer = (u8*)memalign(32, sizeToRead); deviceHandler_seekFile(file,filesToPatch[i].offset, DEVICE_HANDLER_SEEK_SET); if(deviceHandler_readFile(file,buffer,sizeToRead)!= sizeToRead) { DrawFrameStart(); DrawMessageBox(D_FAIL, "Failed to read!"); DrawFrameFinish(); sleep(5); return 0; } if(curDevice != DVD_DISC) { u32 ret = Patch_DVDLowLevelRead(buffer, sizeToRead, filesToPatch[i].type); if(READ_PATCHED_ALL != ret) { DrawFrameStart(); DrawMessageBox(D_FAIL, "Failed to find necessary functions for patching!"); DrawFrameFinish(); sleep(5); } else patched += 1; } if(swissSettings.debugUSB && usb_isgeckoalive(1) && !swissSettings.wiirdDebug) { patched += Patch_Fwrite(buffer, sizeToRead); } if(swissSettings.wiirdDebug || getEnabledCheatsSize() > 0) { Patch_CheatsHook(buffer, sizeToRead, filesToPatch[i].type); } if(curDevice == DVD_DISC && is_gamecube()) { patched += Patch_DVDLowLevelReadForDVD(buffer, sizeToRead, filesToPatch[i].type); patched += Patch_DVDReset(buffer, sizeToRead); } patched += Patch_VidMode(buffer, sizeToRead, filesToPatch[i].type); patched += Patch_FontEnc(buffer, sizeToRead); if(swissSettings.forceWidescreen) Patch_WideAspect(buffer, sizeToRead, filesToPatch[i].type); if(swissSettings.forceAnisotropy) Patch_TexFilt(buffer, sizeToRead, filesToPatch[i].type); if(patched) { // File handle for a patch we might need to write FILE *patchFile = 0; memset(patchFileName, 0, 256); // Make a base patches dir if we don't have one already if(mkdir(&patchBaseDirName[0], 0777) != 0) { if(errno != EEXIST) { return -2; } } if(mkdir(&patchDirName[0], 0777) != 0) { if(errno != EEXIST) { return -2; } } sprintf(patchFileName, "%s/%i",patchDirName, num_patched); // Work out the crc32 crc32 = Crc32_ComputeBuf( 0, buffer, (u32) sizeToRead); // See if this file already exists, if it does, match crc patchFile = fopen( patchFileName, "rb" ); if(patchFile) { //print_gecko("Old Patch exists\r\n"); u32 oldCrc32 = 0; fseek(patchFile, 0L, SEEK_END); u32 file_size = ftell(patchFile); fseek(patchFile, file_size-4, SEEK_SET); fread(&oldCrc32, 1, 4, patchFile); if(oldCrc32 == crc32) { num_patched++; fclose(patchFile); free(buffer); print_gecko("CRC matched, no need to patch again\r\n"); continue; } else { remove(patchFileName); fclose(patchFile); print_gecko("CRC mismatch, writing patch again\r\n"); } } // Otherwise, write a file out for this game with the patched buffer inside. print_gecko("Writing patch file: %s %i bytes (disc offset %08X)\r\n", patchFileName, sizeToRead, filesToPatch[i].offset); patchFile = fopen(patchFileName, "wb"); fwrite(buffer, 1, sizeToRead, patchFile); u32 magic = SWISS_MAGIC; fwrite(&filesToPatch[i].offset, 1, 4, patchFile); fwrite(&filesToPatch[i].size, 1, 4, patchFile); fwrite(&magic, 1, 4, patchFile); fwrite(&crc32, 1, 4, patchFile); fclose(patchFile); num_patched++; } free(buffer); } return num_patched; }