2017-05-07 14:56:35 +02:00
# include "main.h"
# include "version.h"
# include "controller_patcher/ControllerPatcher.hpp"
2016-08-03 22:47:09 +02:00
# include "dynamic_libs/os_functions.h"
# include "dynamic_libs/gx2_functions.h"
# include "dynamic_libs/syshid_functions.h"
# include "dynamic_libs/vpad_functions.h"
# include "dynamic_libs/socket_functions.h"
# include "dynamic_libs/sys_functions.h"
2017-07-20 16:08:00 +02:00
# include "dynamic_libs/proc_ui_functions.h"
2017-05-07 14:56:35 +02:00
2017-03-30 20:11:37 +02:00
# include "utils/logger.h"
# include "utils/utils.h"
2017-05-07 14:56:35 +02:00
# include "utils/function_patcher.h"
2017-03-30 20:11:37 +02:00
2017-05-07 14:56:35 +02:00
# include "patcher/hid_controller_function_patcher.hpp"
# include "fs/fs_utils.h"
# include "fs/sd_fat_devoptab.h"
# include "system/memory.h"
# include "kernel/kernel_functions.h"
2017-03-30 20:11:37 +02:00
# include "common/common.h"
2017-04-14 13:10:57 +02:00
# include "language/gettext.h"
2016-08-03 22:47:09 +02:00
# include "video/CursorDrawer.h"
2017-05-07 14:56:35 +02:00
# include "Application.h"
2017-05-07 14:57:33 +02:00
# include "settings/CSettings.h"
2017-05-07 14:56:35 +02:00
# include "utils/StringTools.h"
2016-08-03 22:47:09 +02:00
/* Entry point */
2017-04-10 11:07:50 +02:00
extern " C " s32 Menu_Main ( void ) {
2017-07-20 16:08:00 +02:00
if ( gAppStatus = = 2 ) {
//"No, we don't want to patch stuff again.");
return EXIT_RELAUNCH_ON_LOAD ;
}
2016-08-03 22:47:09 +02:00
//!*******************************************************************
//! Initialize function pointers *
//!*******************************************************************
//! do OS (for acquire) and sockets first so we got logging
InitOSFunctionPointers ( ) ;
2017-04-14 16:35:30 +02:00
InitVPadFunctionPointers ( ) ;
2017-07-20 16:08:00 +02:00
InitProcUIFunctionPointers ( ) ;
2017-04-14 16:10:34 +02:00
if ( OSGetTitleID ( ) = = 0x00050000101a5f00L | | // Shantae and the Pirate's Curse USA (reason: crashes when pressing HOME, Pro Controller not recognized)
2017-04-14 17:00:37 +02:00
OSGetTitleID ( ) = = 0x00050000101F7F00L | | // Shantae and the Pirate's Curse JAP (シャンティ -海賊の呪い-) (reason: crashes when pressing HOME, Pro Controller not recognized)
2017-04-14 16:10:34 +02:00
OSGetTitleID ( ) = = 0x00050000101a9500L ) { // Shantae and the Pirate's Curse EUR (reason: crashes when pressing HOME, Pro Controller not recognized)
2017-04-14 16:35:30 +02:00
bool res = SplashScreen ( 5 , std : : string ( " This game is not fully supported. Press X to keep patches applied. " ) . c_str ( ) , 0 , VPAD_BUTTON_X ) ;
if ( res ) {
SplashScreen ( 0 , std : : string ( " Keeping patches applied. Pressing Home results in a crash. " ) . c_str ( ) , 1 , 0 ) ;
SplashScreen ( 5 , std : : string ( " Pro Controller emulation is not supported. " ) . c_str ( ) , 2 , 0 ) ;
} else {
SplashScreen ( 3 , std : : string ( " Unloading patches. The game should work normally! " ) . c_str ( ) , 1 , 0 ) ;
RestorePatches ( ) ;
return EXIT_RELAUNCH_ON_LOAD ;
}
2017-04-14 16:10:34 +02:00
}
2017-03-30 20:11:37 +02:00
InitFSFunctionPointers ( ) ;
2016-08-03 22:47:09 +02:00
InitSocketFunctionPointers ( ) ;
InitGX2FunctionPointers ( ) ;
InitSysFunctionPointers ( ) ;
2017-04-14 16:35:30 +02:00
2017-03-30 20:11:37 +02:00
InitPadScoreFunctionPointers ( ) ;
InitAXFunctionPointers ( ) ;
2016-08-03 22:47:09 +02:00
SetupKernelCallback ( ) ;
log_init ( " 192.168.0.181 " ) ;
log_printf ( " HID to VPAD %s - %s %s - by Maschell \n \n " , APP_VERION , __DATE__ , __TIME__ ) ;
//!*******************************************************************
//! Initialize HID Config *
//!*******************************************************************
2017-04-10 11:07:50 +02:00
log_printf ( " Menu_Main (line %d): Initializing the controller data \n " , __LINE__ ) ;
2017-03-30 20:11:37 +02:00
bool res = ControllerPatcher : : Init ( ) ;
if ( ! res ) {
2017-04-14 16:35:30 +02:00
SplashScreen ( 5 , std : : string ( " Error. The app starts in 5 seconds without patches. " ) . c_str ( ) , 0 , 0 ) ;
2017-03-30 20:11:37 +02:00
RestorePatches ( ) ;
return EXIT_RELAUNCH_ON_LOAD ;
}
ControllerPatcher : : enableControllerMapping ( ) ;
ControllerPatcher : : startNetworkServer ( ) ;
2017-04-10 11:07:50 +02:00
s32 result = 0 ;
2017-03-30 20:11:37 +02:00
if ( isInMiiMakerHBL ( ) ) {
//!*******************************************************************
//! Initialize heap memory *
//!*******************************************************************
2017-04-10 11:07:50 +02:00
log_printf ( " Menu_Main (line %d): Initialize memory management \n " , __LINE__ ) ;
2017-03-30 20:11:37 +02:00
memoryInitialize ( ) ;
2017-05-07 14:57:33 +02:00
2017-04-10 11:07:50 +02:00
log_printf ( " Menu_Main (line %d): Mount SD partition \n " , __LINE__ ) ;
2017-03-30 20:11:37 +02:00
mount_sd_fat ( " sd " ) ;
2017-04-10 11:07:50 +02:00
log_printf ( " Menu_Main (line %d): Start main application \n " , __LINE__ ) ;
2017-03-30 20:11:37 +02:00
result = Application : : instance ( ) - > exec ( ) ;
2017-04-10 11:07:50 +02:00
log_printf ( " Menu_Main (line %d): Main application stopped result: %d \n " , __LINE__ , result ) ;
2017-03-30 20:11:37 +02:00
Application : : destroyInstance ( ) ;
2017-05-07 14:57:33 +02:00
bool rumble = CSettings : : instance ( ) - > getValueAsBool ( CSettings : : RumbleActivated ) ;
log_printf ( " Menu_Main (line %d): Setting rumble to: %d \n " , __LINE__ , rumble ) ;
ControllerPatcher : : setRumbleActivated ( rumble ) ;
2017-07-20 17:29:39 +02:00
bool networkController = CSettings : : instance ( ) - > getValueAsBool ( CSettings : : NetworkControllerActivated ) ;
ControllerPatcher : : setNetworkControllerActivated ( networkController ) ;
2017-04-10 11:07:50 +02:00
log_printf ( " Menu_Main (line %d): Unmount SD \n " , __LINE__ ) ;
2017-03-30 20:11:37 +02:00
unmount_sd_fat ( " sd " ) ;
2017-04-10 11:07:50 +02:00
log_printf ( " Menu_Main (line %d): Release memory \n " , __LINE__ ) ;
2017-03-30 20:11:37 +02:00
memoryRelease ( ) ;
ControllerPatcher : : destroyConfigHelper ( ) ;
2017-05-07 18:44:34 +02:00
CSettings : : destroyInstance ( ) ;
2016-08-03 22:47:09 +02:00
}
2017-03-30 20:11:37 +02:00
2016-08-03 22:47:09 +02:00
//!*******************************************************************
//! Patching functions *
//!*******************************************************************
2017-04-10 11:07:50 +02:00
log_printf ( " Menu_Main(line %d): Patching functions \n " , __LINE__ ) ;
2017-03-30 20:11:37 +02:00
ApplyPatches ( ) ;
2016-08-03 22:47:09 +02:00
2017-03-30 20:11:37 +02:00
if ( ! isInMiiMakerHBL ( ) ) {
ControllerPatcher : : disableWiiUEnergySetting ( ) ;
2016-08-03 22:47:09 +02:00
return EXIT_RELAUNCH_ON_LOAD ;
}
2017-03-30 20:11:37 +02:00
if ( result = = APPLICATION_CLOSE_APPLY ) {
2017-04-10 11:07:50 +02:00
log_printf ( " Menu_Main (line %d): Loading the system menu. \n " , __LINE__ ) ;
2016-08-03 22:47:09 +02:00
SYSLaunchMenu ( ) ;
return EXIT_RELAUNCH_ON_LOAD ;
}
2017-04-10 11:07:50 +02:00
log_printf ( " Menu_Main (line %d): Going back to the Homebrew Launcher \n " , __LINE__ ) ;
2017-03-30 20:11:37 +02:00
ControllerPatcher : : restoreWiiUEnergySetting ( ) ;
2016-08-03 22:47:09 +02:00
deInit ( ) ;
return EXIT_SUCCESS ;
}
2017-03-30 20:11:37 +02:00
void ApplyPatches ( ) {
PatchInvidualMethodHooks ( method_hooks_hid_controller , method_hooks_size_hid_controller , method_calls_hid_controller ) ;
}
void RestorePatches ( ) {
RestoreInvidualInstructions ( method_hooks_hid_controller , method_hooks_size_hid_controller ) ;
}
2016-08-03 22:47:09 +02:00
void deInit ( ) {
CursorDrawer : : destroyInstance ( ) ;
2017-03-30 20:11:37 +02:00
RestorePatches ( ) ;
ControllerPatcher : : DeInit ( ) ;
ControllerPatcher : : stopNetworkServer ( ) ;
2016-08-03 22:47:09 +02:00
log_deinit ( ) ;
}
2017-04-10 11:07:50 +02:00
s32 isInMiiMakerHBL ( ) {
2017-03-30 20:11:37 +02:00
if ( OSGetTitleID ! = 0 & & (
OSGetTitleID ( ) = = 0x000500101004A200 | | // mii maker eur
OSGetTitleID ( ) = = 0x000500101004A100 | | // mii maker usa
2017-03-31 16:54:16 +02:00
OSGetTitleID ( ) = = 0x000500101004A000 | | // mii maker jpn
2017-03-30 20:11:37 +02:00
OSGetTitleID ( ) = = 0x0005000013374842 ) )
{
return 1 ;
}
return 0 ;
}
2016-08-03 22:47:09 +02:00
2017-04-14 16:35:30 +02:00
bool SplashScreen ( s32 time , const char * message , u8 pos , u32 button ) {
bool result = false ;
2016-08-03 22:47:09 +02:00
// Prepare screen
2017-04-10 11:07:50 +02:00
s32 screen_buf0_size = 0 ;
2016-08-03 22:47:09 +02:00
// Init screen and screen buffers
OSScreenInit ( ) ;
screen_buf0_size = OSScreenGetBufferSizeEx ( 0 ) ;
OSScreenSetBufferEx ( 0 , ( void * ) 0xF4000000 ) ;
OSScreenSetBufferEx ( 1 , ( void * ) ( 0xF4000000 + screen_buf0_size ) ) ;
OSScreenEnableEx ( 0 , 1 ) ;
OSScreenEnableEx ( 1 , 1 ) ;
// Clear screens
OSScreenClearBufferEx ( 0 , 0 ) ;
OSScreenClearBufferEx ( 1 , 0 ) ;
// Flip buffers
OSScreenFlipBuffersEx ( 0 ) ;
OSScreenFlipBuffersEx ( 1 ) ;
2017-04-14 16:35:30 +02:00
OSScreenPutFontEx ( 0 , 0 , pos , message ) ;
OSScreenPutFontEx ( 1 , 0 , pos , message ) ;
2016-08-03 22:47:09 +02:00
OSScreenFlipBuffersEx ( 0 ) ;
OSScreenFlipBuffersEx ( 1 ) ;
2017-04-10 11:07:50 +02:00
s32 tickswait = time * 1000 * 1000 ;
s32 times = 1000 ;
s32 sleepingtime = tickswait / 1000 ;
s32 i = 0 ;
2017-04-14 16:35:30 +02:00
VPADData vpad_data ;
s32 error ;
2016-08-03 22:47:09 +02:00
while ( i < times ) {
2017-04-14 16:35:30 +02:00
VPADRead ( 0 , & vpad_data , 1 , & error ) ;
if ( button > 0 & & ( vpad_data . btns_h & button ) ) {
result = true ;
i = times ;
}
2016-08-03 22:47:09 +02:00
i + + ;
2017-05-07 14:44:47 +02:00
os_usleep ( sleepingtime ) ;
2016-08-03 22:47:09 +02:00
}
2017-04-14 16:35:30 +02:00
return result ;
2016-08-03 22:47:09 +02:00
}