2010-06-07 02:23:58 +00:00
|
|
|
#include "stdafx.h"
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
void FixUPXIssue ( BYTE * ProgramLocation );
|
|
|
|
|
2012-10-04 01:45:30 +00:00
|
|
|
CControl_Plugin::CControl_Plugin ( const char * FileName) :
|
|
|
|
m_hDll(NULL),
|
|
|
|
m_Initilized(false),
|
|
|
|
m_RomOpen(false),
|
|
|
|
m_AllocatedControllers(false),
|
|
|
|
Config(NULL),
|
|
|
|
WM_KeyDown(NULL),
|
|
|
|
WM_KeyUp(NULL),
|
|
|
|
RumbleCommand(NULL),
|
|
|
|
GetKeys(NULL),
|
|
|
|
ReadController(NULL),
|
|
|
|
ControllerCommand(NULL),
|
|
|
|
CloseDLL(NULL),
|
|
|
|
RomOpen(NULL),
|
|
|
|
RomClosed(NULL),
|
|
|
|
PluginOpened(NULL),
|
|
|
|
SetSettingInfo(NULL),
|
|
|
|
SetSettingInfo2(NULL)
|
|
|
|
{
|
|
|
|
memset(&m_PluginInfo,0,sizeof(m_PluginInfo));
|
|
|
|
memset(&m_PluginControllers,0,sizeof(m_PluginControllers));
|
|
|
|
memset(&m_Controllers,0,sizeof(m_Controllers));
|
|
|
|
|
|
|
|
Init(FileName);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CControl_Plugin::Init ( const char * FileName )
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
//Make sure all parts of the class are initialized
|
|
|
|
UnloadPlugin();
|
|
|
|
|
|
|
|
//Try to load the DLL library
|
|
|
|
UINT LastErrorMode = SetErrorMode( SEM_FAILCRITICALERRORS );
|
2012-10-04 01:45:30 +00:00
|
|
|
m_hDll = LoadLibrary(FileName);
|
2008-09-18 03:15:49 +00:00
|
|
|
SetErrorMode(LastErrorMode);
|
|
|
|
|
2012-10-04 01:45:30 +00:00
|
|
|
if (m_hDll == NULL) {
|
2008-09-18 03:15:49 +00:00
|
|
|
UnloadPlugin();
|
|
|
|
return;
|
|
|
|
}
|
2012-10-04 01:45:30 +00:00
|
|
|
FixUPXIssue((BYTE *)m_hDll);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//Get DLL information
|
|
|
|
void (__cdecl *GetDllInfo) ( PLUGIN_INFO * PluginInfo );
|
2012-10-04 01:45:30 +00:00
|
|
|
GetDllInfo = (void (__cdecl *)(PLUGIN_INFO *))GetProcAddress( (HMODULE)m_hDll, "GetDllInfo" );
|
2008-09-18 03:15:49 +00:00
|
|
|
if (GetDllInfo == NULL) { UnloadPlugin(); return; }
|
|
|
|
|
|
|
|
GetDllInfo(&m_PluginInfo);
|
2012-10-06 13:57:18 +00:00
|
|
|
if (!CPluginList::ValidPluginVersion(m_PluginInfo)) { UnloadPlugin(); return; }
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//Find entries for functions in DLL
|
|
|
|
void (__cdecl *InitFunc) ( void );
|
2012-10-04 01:45:30 +00:00
|
|
|
Config = (void (__cdecl *)(DWORD))GetProcAddress( (HMODULE)m_hDll, "DllConfig" );
|
|
|
|
ControllerCommand = (void (__cdecl *)(int,BYTE *))GetProcAddress( (HMODULE)m_hDll, "ControllerCommand" );
|
|
|
|
GetKeys = (void (__cdecl *)(int,BUTTONS*)) GetProcAddress( (HMODULE)m_hDll, "GetKeys" );
|
|
|
|
ReadController = (void (__cdecl *)(int,BYTE *))GetProcAddress( (HMODULE)m_hDll, "ReadController" );
|
|
|
|
WM_KeyDown = (void (__cdecl *)(DWORD,DWORD))GetProcAddress( (HMODULE)m_hDll, "WM_KeyDown" );
|
|
|
|
WM_KeyUp = (void (__cdecl *)(DWORD,DWORD))GetProcAddress( (HMODULE)m_hDll, "WM_KeyUp" );
|
|
|
|
InitFunc = (void (__cdecl *)(void)) GetProcAddress( (HMODULE)m_hDll, "InitiateControllers" );
|
|
|
|
RomOpen = (void (__cdecl *)(void)) GetProcAddress( (HMODULE)m_hDll, "RomOpen" );
|
|
|
|
RomClosed = (void (__cdecl *)(void)) GetProcAddress( (HMODULE)m_hDll, "RomClosed" );
|
|
|
|
CloseDLL = (void (__cdecl *)(void)) GetProcAddress( (HMODULE)m_hDll, "CloseDLL" );
|
|
|
|
RumbleCommand = (void (__cdecl *)(int, BOOL))GetProcAddress( (HMODULE)m_hDll, "RumbleCommand" );
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//version 101 functions
|
2012-10-04 01:45:30 +00:00
|
|
|
PluginOpened = (void (__cdecl *)(void))GetProcAddress( (HMODULE)m_hDll, "PluginLoaded" );
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//Make sure dll had all needed functions
|
|
|
|
if (InitFunc == NULL) { UnloadPlugin(); return; }
|
|
|
|
if (CloseDLL == NULL) { UnloadPlugin(); return; }
|
|
|
|
|
2012-10-04 01:45:30 +00:00
|
|
|
SetSettingInfo2 = (void (__cdecl *)(PLUGIN_SETTINGS2 *))GetProcAddress( (HMODULE)m_hDll, "SetSettingInfo2" );
|
2008-12-04 10:41:51 +00:00
|
|
|
if (SetSettingInfo2)
|
|
|
|
{
|
|
|
|
PLUGIN_SETTINGS2 info;
|
|
|
|
info.FindSystemSettingId = (unsigned int (*)( void * handle, const char * ))CSettings::FindGameSetting;
|
|
|
|
SetSettingInfo2(&info);
|
|
|
|
}
|
|
|
|
|
2012-10-04 01:45:30 +00:00
|
|
|
SetSettingInfo = (void (__cdecl *)(PLUGIN_SETTINGS *))GetProcAddress( (HMODULE)m_hDll, "SetSettingInfo" );
|
2008-12-01 18:19:49 +00:00
|
|
|
if (SetSettingInfo)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
PLUGIN_SETTINGS info;
|
|
|
|
info.dwSize = sizeof(PLUGIN_SETTINGS);
|
|
|
|
info.DefaultStartRange = FirstCtrlDefaultSet;
|
|
|
|
info.SettingStartRange = FirstCtrlSettings;
|
|
|
|
info.MaximumSettings = MaxPluginSetting;
|
2008-11-14 20:51:06 +00:00
|
|
|
info.NoDefault = Default_None;
|
2012-11-17 01:02:04 +00:00
|
|
|
info.DefaultLocation = g_Settings->LoadDword(Setting_UseFromRegistry) ? SettingType_Registry : SettingType_CfgFile;
|
|
|
|
info.handle = g_Settings;
|
2008-11-14 20:51:06 +00:00
|
|
|
info.RegisterSetting = (void (*)(void *,int,int,SettingDataType,SettingType,const char *,const char *, DWORD))CSettings::RegisterSetting;
|
2008-09-18 03:15:49 +00:00
|
|
|
info.GetSetting = (unsigned int (*)( void * handle, int ID ))CSettings::GetSetting;
|
|
|
|
info.GetSettingSz = (const char * (*)( void *, int, char *, int ))CSettings::GetSettingSz;
|
|
|
|
info.SetSetting = (void (*)(void *,int,unsigned int))CSettings::SetSetting;
|
|
|
|
info.SetSettingSz = (void (*)(void *,int,const char *))CSettings::SetSettingSz;
|
|
|
|
info.UseUnregisteredSetting = NULL;
|
|
|
|
|
|
|
|
SetSettingInfo(&info);
|
2012-11-17 01:02:04 +00:00
|
|
|
// g_Settings->UnknownSetting_CTRL = info.UseUnregisteredSetting;
|
2008-12-01 18:19:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_PluginInfo.Version >= 0x0102)
|
|
|
|
{
|
|
|
|
if (PluginOpened == NULL) { UnloadPlugin(); return; }
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
PluginOpened();
|
|
|
|
}
|
|
|
|
|
|
|
|
//Allocate our own controller
|
|
|
|
m_AllocatedControllers = true;
|
|
|
|
m_Controllers[0] = new CCONTROL(m_PluginControllers[0].Present,m_PluginControllers[0].RawData,m_PluginControllers[0].Plugin);
|
|
|
|
m_Controllers[1] = new CCONTROL(m_PluginControllers[1].Present,m_PluginControllers[1].RawData,m_PluginControllers[1].Plugin);
|
|
|
|
m_Controllers[2] = new CCONTROL(m_PluginControllers[2].Present,m_PluginControllers[2].RawData,m_PluginControllers[2].Plugin);
|
|
|
|
m_Controllers[3] = new CCONTROL(m_PluginControllers[3].Present,m_PluginControllers[3].RawData,m_PluginControllers[3].Plugin);
|
|
|
|
}
|
|
|
|
|
|
|
|
CControl_Plugin::~CControl_Plugin (void) {
|
|
|
|
Close();
|
|
|
|
UnloadPlugin();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CControl_Plugin::Initiate ( CN64System * System, CMainGui * RenderWindow ) {
|
|
|
|
m_PluginControllers[0].Present = FALSE;
|
|
|
|
m_PluginControllers[0].RawData = FALSE;
|
|
|
|
m_PluginControllers[0].Plugin = PLUGIN_NONE;
|
|
|
|
|
|
|
|
m_PluginControllers[1].Present = FALSE;
|
|
|
|
m_PluginControllers[1].RawData = FALSE;
|
|
|
|
m_PluginControllers[1].Plugin = PLUGIN_NONE;
|
|
|
|
|
|
|
|
m_PluginControllers[2].Present = FALSE;
|
|
|
|
m_PluginControllers[2].RawData = FALSE;
|
|
|
|
m_PluginControllers[2].Plugin = PLUGIN_NONE;
|
|
|
|
|
|
|
|
m_PluginControllers[3].Present = FALSE;
|
|
|
|
m_PluginControllers[3].RawData = FALSE;
|
|
|
|
m_PluginControllers[3].Plugin = PLUGIN_NONE;
|
|
|
|
|
|
|
|
//Get DLL information
|
|
|
|
void (__cdecl *GetDllInfo) ( PLUGIN_INFO * PluginInfo );
|
2012-10-04 01:45:30 +00:00
|
|
|
GetDllInfo = (void (__cdecl *)(PLUGIN_INFO *))GetProcAddress( (HMODULE)m_hDll, "GetDllInfo" );
|
2008-09-18 03:15:49 +00:00
|
|
|
if (GetDllInfo == NULL) { return false; }
|
|
|
|
|
|
|
|
PLUGIN_INFO PluginInfo;
|
|
|
|
GetDllInfo(&PluginInfo);
|
|
|
|
|
|
|
|
//Test Plugin version
|
|
|
|
if (PluginInfo.Version == 0x0100) {
|
|
|
|
//Get Function from DLL
|
|
|
|
void (__cdecl *InitiateControllers_1_0)( HWND hMainWindow, CONTROL Controls[4] );
|
2012-10-04 01:45:30 +00:00
|
|
|
InitiateControllers_1_0 = (void (__cdecl *)(HWND, CONTROL *))GetProcAddress( (HMODULE)m_hDll, "InitiateControllers" );
|
2008-09-18 03:15:49 +00:00
|
|
|
if (InitiateControllers_1_0 == NULL) { return false; }
|
|
|
|
InitiateControllers_1_0((HWND)RenderWindow->m_hMainWindow,m_PluginControllers);
|
|
|
|
m_Initilized = true;
|
|
|
|
}
|
|
|
|
if (PluginInfo.Version >= 0x0101) {
|
|
|
|
typedef struct {
|
|
|
|
HWND hMainWindow;
|
|
|
|
HINSTANCE hinst;
|
|
|
|
|
|
|
|
BOOL MemoryBswaped; // If this is set to TRUE, then the memory has been pre
|
|
|
|
// bswap on a dword (32 bits) boundry, only effects header.
|
|
|
|
// eg. the first 8 bytes are stored like this:
|
|
|
|
// 4 3 2 1 8 7 6 5
|
|
|
|
BYTE * HEADER; // This is the rom header (first 40h bytes of the rom)
|
|
|
|
CONTROL *Controls; // A pointer to an array of 4 controllers .. eg:
|
|
|
|
// CONTROL Controls[4];
|
|
|
|
} CONTROL_INFO;
|
|
|
|
|
|
|
|
//Get Function from DLL
|
|
|
|
void (__cdecl *InitiateControllers_1_1)( CONTROL_INFO * ControlInfo );
|
2012-10-04 01:45:30 +00:00
|
|
|
InitiateControllers_1_1 = (void (__cdecl *)(CONTROL_INFO *))GetProcAddress( (HMODULE)m_hDll, "InitiateControllers" );
|
2008-09-18 03:15:49 +00:00
|
|
|
if (InitiateControllers_1_1 == NULL) { return false; }
|
|
|
|
|
|
|
|
CONTROL_INFO ControlInfo;
|
|
|
|
if (System == NULL) {
|
|
|
|
BYTE Buffer[100];
|
|
|
|
|
|
|
|
ControlInfo.Controls = m_PluginControllers;
|
|
|
|
ControlInfo.HEADER = Buffer;
|
|
|
|
ControlInfo.hinst = GetModuleHandle(NULL);
|
|
|
|
ControlInfo.hMainWindow = (HWND)RenderWindow->m_hMainWindow;
|
|
|
|
ControlInfo.MemoryBswaped = TRUE;
|
|
|
|
InitiateControllers_1_1(&ControlInfo);
|
|
|
|
m_Initilized = true;
|
|
|
|
} else {
|
|
|
|
ControlInfo.Controls = m_PluginControllers;
|
2010-05-22 04:47:15 +00:00
|
|
|
ControlInfo.HEADER = _Rom->GetRomAddress();
|
2008-09-18 03:15:49 +00:00
|
|
|
ControlInfo.hinst = GetModuleHandle(NULL);
|
|
|
|
ControlInfo.hMainWindow = (HWND)RenderWindow->m_hMainWindow;
|
|
|
|
ControlInfo.MemoryBswaped = TRUE;
|
|
|
|
InitiateControllers_1_1(&ControlInfo);
|
|
|
|
m_Initilized = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//jabo had a bug so I call CreateThread so his dllmain gets called again
|
|
|
|
DWORD ThreadID;
|
|
|
|
HANDLE hthread = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)DummyFunction,NULL,0, &ThreadID);
|
|
|
|
CloseHandle(hthread);
|
|
|
|
|
|
|
|
|
|
|
|
return m_Initilized;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CControl_Plugin::RomOpened ( void )
|
|
|
|
{
|
|
|
|
//Real system ... then make the file as open
|
|
|
|
if (!m_RomOpen)
|
|
|
|
{
|
|
|
|
RomOpen();
|
|
|
|
m_RomOpen = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CControl_Plugin::Close(void) {
|
|
|
|
if (m_RomOpen) {
|
|
|
|
RomClosed();
|
|
|
|
m_RomOpen = false;
|
|
|
|
}
|
|
|
|
if (m_Initilized) {
|
|
|
|
CloseDLL();
|
|
|
|
m_Initilized = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CControl_Plugin::GameReset(void)
|
|
|
|
{
|
|
|
|
if (m_RomOpen)
|
|
|
|
{
|
|
|
|
RomClosed();
|
|
|
|
RomOpen();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CControl_Plugin::UnloadPlugin(void) {
|
|
|
|
if (m_AllocatedControllers) {
|
|
|
|
for (int count = 0; count < sizeof(m_Controllers) / sizeof(m_Controllers[0]); count++) {
|
|
|
|
delete m_Controllers[count];
|
|
|
|
m_Controllers[count] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(&m_PluginInfo,0,sizeof(m_PluginInfo));
|
2012-10-04 01:45:30 +00:00
|
|
|
if (m_hDll != NULL ) {
|
|
|
|
FreeLibrary((HMODULE)m_hDll);
|
|
|
|
m_hDll = NULL;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
m_AllocatedControllers = false;
|
|
|
|
m_Controllers[0] = NULL;
|
|
|
|
m_Controllers[1] = NULL;
|
|
|
|
m_Controllers[2] = NULL;
|
|
|
|
m_Controllers[3] = NULL;
|
|
|
|
Config = NULL;
|
|
|
|
ControllerCommand = NULL;
|
|
|
|
GetKeys = NULL;
|
|
|
|
ReadController = NULL;
|
|
|
|
WM_KeyDown = NULL;
|
|
|
|
WM_KeyUp = NULL;
|
|
|
|
CloseDLL = NULL;
|
|
|
|
RomOpen = NULL;
|
|
|
|
RomClosed = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void CControl_Plugin::UpdateKeys (void) {
|
|
|
|
if (!m_AllocatedControllers) { return; }
|
|
|
|
for (int cont = 0; cont < sizeof(m_Controllers) / sizeof(m_Controllers[0]); cont++) {
|
|
|
|
if (!m_Controllers[cont]->m_Present) { continue; }
|
|
|
|
if (!m_Controllers[cont]->m_RawData) {
|
|
|
|
GetKeys(cont,&m_Controllers[cont]->m_Buttons);
|
|
|
|
} else {
|
2012-11-17 00:58:31 +00:00
|
|
|
g_Notify->BreakPoint(__FILE__,__LINE__);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ReadController) { ReadController(-1,NULL); }
|
|
|
|
}
|
|
|
|
|
2010-05-31 00:21:08 +00:00
|
|
|
void CControl_Plugin::SetControl(CControl_Plugin const * const Plugin)
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
if (m_AllocatedControllers) {
|
|
|
|
for (int count = 0; count < sizeof(m_Controllers) / sizeof(m_Controllers[0]); count++) {
|
|
|
|
delete m_Controllers[count];
|
|
|
|
m_Controllers[count] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_AllocatedControllers = false;
|
|
|
|
for (int count = 0; count < sizeof(m_Controllers) / sizeof(m_Controllers[0]); count++) {
|
|
|
|
m_Controllers[count] = Plugin->m_Controllers[count];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CCONTROL::CCONTROL(DWORD &Present,DWORD &RawData, int &PlugType) :
|
|
|
|
m_Present(Present),m_RawData(RawData),m_PlugType(PlugType)
|
|
|
|
{
|
|
|
|
m_Buttons.Value = 0;
|
|
|
|
}
|