2008-09-18 03:15:49 +00:00
|
|
|
#include "Multilanguage.h"
|
|
|
|
#include "User Interface.h"
|
|
|
|
#include "N64 System.h"
|
|
|
|
#include "Plugin.h"
|
|
|
|
#include "Support.h"
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
//#pragma comment(linker,"/merge:.rdata=.text")
|
|
|
|
|
|
|
|
void FixUPXIssue ( BYTE * ProgramLocation )
|
|
|
|
{
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
DWORD VirtualAddress;
|
|
|
|
DWORD SizeOfRawData;
|
|
|
|
DWORD Characteristics;
|
|
|
|
} ORIGINAL_SECTION;
|
|
|
|
|
|
|
|
char FileName[MAX_PATH];
|
|
|
|
GetModuleFileName((HMODULE)ProgramLocation,FileName,sizeof(FileName));
|
|
|
|
|
|
|
|
|
|
|
|
HANDLE hFile = CreateFile(FileName,GENERIC_READ,FILE_SHARE_WRITE|FILE_SHARE_READ,NULL,OPEN_ALWAYS,0,NULL);
|
|
|
|
DWORD TestID, NoOfSections, dwRead;
|
|
|
|
SetFilePointer(hFile,-4,0,FILE_END);
|
|
|
|
ReadFile(hFile,&TestID,4,&dwRead,NULL);
|
|
|
|
|
|
|
|
if (TestID != 0x3345505a)
|
|
|
|
{
|
|
|
|
/* //Read Dos Header from file
|
|
|
|
IMAGE_DOS_HEADER * DosHeader = (IMAGE_DOS_HEADER *)ProgramLocation;
|
|
|
|
if (DosHeader->e_magic != IMAGE_DOS_SIGNATURE )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Read NT (PE) Header
|
|
|
|
IMAGE_NT_HEADERS * NTHeader = (IMAGE_NT_HEADERS *)(ProgramLocation + DosHeader->e_lfanew);
|
|
|
|
if (NTHeader->Signature != IMAGE_NT_SIGNATURE )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
IMAGE_SECTION_HEADER * Sections = (IMAGE_SECTION_HEADER *)(ProgramLocation + DosHeader->e_lfanew +
|
|
|
|
sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + NTHeader->FileHeader.SizeOfOptionalHeader);
|
|
|
|
|
|
|
|
for (int count = 0; count < NTHeader->FileHeader.NumberOfSections; count ++ )
|
|
|
|
{
|
|
|
|
IMAGE_SECTION_HEADER & Section = Sections[count];
|
|
|
|
LPVOID Address = ProgramLocation + Section.VirtualAddress;
|
|
|
|
MEMORY_BASIC_INFORMATION Buffer;
|
|
|
|
if (VirtualQuery(Address,&Buffer,sizeof(Buffer)))
|
|
|
|
{
|
|
|
|
DWORD OldProtect = Buffer.Protect;
|
|
|
|
//VirtualProtect(Address, Section.SizeOfRawData,PAGE_READONLY,&OldProtect);
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
CloseHandle(hFile);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SetFilePointer(hFile,-8,0,FILE_END);
|
|
|
|
ReadFile(hFile,&NoOfSections,4,&dwRead,NULL);
|
|
|
|
|
|
|
|
ORIGINAL_SECTION * Section = new ORIGINAL_SECTION[NoOfSections];
|
|
|
|
DWORD SizeOfSections = (NoOfSections * sizeof(ORIGINAL_SECTION));
|
|
|
|
|
|
|
|
SetFilePointer(hFile,(8 + SizeOfSections) * -1,0,FILE_END);
|
|
|
|
ReadFile(hFile,Section,SizeOfSections,&dwRead,NULL);
|
|
|
|
|
|
|
|
for (int count = 0; count < NoOfSections; count ++ )
|
|
|
|
{
|
|
|
|
LPVOID Address = ProgramLocation + Section[count].VirtualAddress;
|
|
|
|
MEMORY_BASIC_INFORMATION Buffer;
|
|
|
|
|
|
|
|
if (VirtualQuery(Address,&Buffer,sizeof(Buffer)))
|
|
|
|
{
|
|
|
|
DWORD MemoryProctect = PAGE_EXECUTE_READWRITE;
|
|
|
|
switch (Section[count].Characteristics & 0xF0000000)
|
|
|
|
{
|
|
|
|
case 0x20000000: MemoryProctect = PAGE_EXECUTE; break;
|
|
|
|
case 0x40000000: MemoryProctect = PAGE_READONLY; break;
|
|
|
|
case 0x60000000: MemoryProctect = PAGE_EXECUTE_READ; break;
|
|
|
|
case 0x80000000: MemoryProctect = PAGE_READWRITE; break;
|
|
|
|
case 0xA0000000: MemoryProctect = PAGE_EXECUTE_READWRITE; break;
|
|
|
|
case 0xC0000000: MemoryProctect = PAGE_READWRITE; break;
|
|
|
|
case 0xE0000000: MemoryProctect = PAGE_EXECUTE_READWRITE; break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Buffer.Protect != MemoryProctect)
|
|
|
|
{
|
|
|
|
DWORD OldProtect;
|
|
|
|
VirtualProtect(Address, Section[count].SizeOfRawData,MemoryProctect,&OldProtect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete [] Section;
|
|
|
|
CloseHandle(hFile);
|
|
|
|
/*
|
|
|
|
|
|
|
|
//Read Dos Header from file
|
|
|
|
IMAGE_DOS_HEADER * DosHeader = (IMAGE_DOS_HEADER *)ProgramLocation;
|
|
|
|
if (DosHeader->e_magic != IMAGE_DOS_SIGNATURE )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Read NT (PE) Header
|
|
|
|
IMAGE_NT_HEADERS * NTHeader = (IMAGE_NT_HEADERS *)(ProgramLocation + DosHeader->e_lfanew);
|
|
|
|
if (NTHeader->Signature != IMAGE_NT_SIGNATURE )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD a = sizeof(DWORD);
|
|
|
|
DWORD b = sizeof(IMAGE_FILE_HEADER);
|
|
|
|
IMAGE_SECTION_HEADER * Sections = (IMAGE_SECTION_HEADER *)(ProgramLocation + DosHeader->e_lfanew +
|
|
|
|
sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER) + NTHeader->FileHeader.SizeOfOptionalHeader);
|
|
|
|
|
|
|
|
for (int count = 0; count < NTHeader->FileHeader.NumberOfSections; count ++ )
|
|
|
|
{
|
|
|
|
IMAGE_SECTION_HEADER & Section = Sections[count];
|
|
|
|
if (_stricmp((char *)Section.Name,".rdata") == 0)
|
|
|
|
{
|
|
|
|
LPVOID Address = ProgramLocation + Section.VirtualAddress;
|
|
|
|
MEMORY_BASIC_INFORMATION Buffer;
|
|
|
|
if (VirtualQuery(Address,&Buffer,sizeof(Buffer)))
|
|
|
|
{
|
|
|
|
if (Buffer.Protect != 2)
|
|
|
|
{
|
|
|
|
DWORD OldProtect;
|
|
|
|
VirtualProtect(Address, Section.SizeOfRawData,PAGE_READONLY,&OldProtect);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//break;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogLevelChanged (CTraceFileLog * LogFile)
|
|
|
|
{
|
2008-11-14 20:51:06 +00:00
|
|
|
LogFile->SetTraceLevel((TraceLevel)_Settings->LoadDword(Debugger_AppLogLevel));
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void LogFlushChanged (CTraceFileLog * LogFile)
|
|
|
|
{
|
2008-11-14 20:51:06 +00:00
|
|
|
LogFile->SetFlushFile(_Settings->LoadDword(Debugger_AppLogFlush) != 0);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void InitializeLog ( void)
|
|
|
|
{
|
|
|
|
|
|
|
|
CPath LogFilePath(CPath::MODULE_DIRECTORY,_T("Project64.log"));
|
|
|
|
|
2008-11-14 20:51:06 +00:00
|
|
|
CTraceFileLog * LogFile = new CTraceFileLog(LogFilePath, _Settings->LoadDword(Debugger_AppLogFlush) != 0, Log_New);
|
|
|
|
LogFile->SetTraceLevel((TraceLevel)_Settings->LoadDword(Debugger_AppLogLevel));
|
2008-09-18 03:15:49 +00:00
|
|
|
AddTraceModule(LogFile);
|
|
|
|
|
2008-11-14 20:51:06 +00:00
|
|
|
_Settings->RegisterChangeCB(Debugger_AppLogLevel,LogFile,(CSettings::SettingChangedFunc)LogLevelChanged);
|
|
|
|
_Settings->RegisterChangeCB(Debugger_AppLogFlush,LogFile,(CSettings::SettingChangedFunc)LogFlushChanged);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszArgs, int nWinMode) {
|
|
|
|
CoInitialize(NULL);
|
|
|
|
try
|
|
|
|
{
|
|
|
|
|
|
|
|
LPCSTR AppName = "Project64 1.7";
|
|
|
|
_Lang = new CLanguage();
|
|
|
|
|
|
|
|
_Settings = new CSettings;
|
|
|
|
_Settings->Initilize(AppName);
|
|
|
|
|
|
|
|
InitializeLog();
|
|
|
|
|
2008-11-14 20:51:06 +00:00
|
|
|
WriteTrace(TraceDebug,"WinMain - Application Starting");
|
2008-09-18 03:15:49 +00:00
|
|
|
FixUPXIssue((BYTE *)hInstance);
|
|
|
|
|
|
|
|
//Create the plugin container
|
2008-11-14 20:51:06 +00:00
|
|
|
WriteTrace(TraceDebug,"WinMain - Create Plugins");
|
|
|
|
CPlugins Plugins ( _Settings->LoadString(Directory_Plugin) );
|
|
|
|
WriteTrace(TraceDebug,"WinMain - Create N64 system");
|
2008-09-18 03:15:49 +00:00
|
|
|
CN64System N64System ( &Notify(), &Plugins ); //Create the backend n64 system
|
|
|
|
|
|
|
|
//Select the language
|
|
|
|
_Lang->LoadCurrentStrings(true);
|
|
|
|
|
|
|
|
//Create the main window with Menu
|
2008-11-14 20:51:06 +00:00
|
|
|
WriteTrace(TraceDebug,"WinMain - Create Main Window");
|
2008-09-18 03:15:49 +00:00
|
|
|
stdstr WinTitle(AppName);
|
2008-11-14 20:51:06 +00:00
|
|
|
if (_Settings->LoadBool(Beta_IsBetaVersion))
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2008-11-14 20:51:06 +00:00
|
|
|
WinTitle.Format("Project64 %s (%s)",VersionInfo(VERSION_PRODUCT_VERSION).c_str(),_Settings->LoadString(Beta_UserName).c_str());
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
CMainGui MainWindow(WinTitle.c_str(),&Notify(),&N64System), HiddenWindow;
|
|
|
|
CMainMenu MainMenu(&MainWindow, &N64System);
|
2008-11-14 20:51:06 +00:00
|
|
|
Plugins.SetRenderWindows(&MainWindow,&HiddenWindow);
|
|
|
|
Notify().SetMainWindow(&MainWindow);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
{
|
2008-11-14 20:51:06 +00:00
|
|
|
stdstr_f User("%s",_Settings->LoadString(Beta_UserName).c_str());
|
|
|
|
stdstr_f Email("%s",_Settings->LoadString(Beta_EmailAddress).c_str());
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2008-11-14 20:51:06 +00:00
|
|
|
if (MD5(User).hex_digest() != _Settings->LoadString(Beta_UserNameMD5) ||
|
|
|
|
MD5(Email).hex_digest() != _Settings->LoadString(Beta_EmailAddressMD5))
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2008-11-14 20:51:06 +00:00
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
if (__argc > 1) {
|
2008-11-14 20:51:06 +00:00
|
|
|
WriteTraceF(TraceDebug,"WinMain - Cmd line found \"%s\"",__argv[1]);
|
2008-09-18 03:15:49 +00:00
|
|
|
MainWindow.Show(true); //Show the main window
|
|
|
|
N64System.RunFileImage(__argv[1]);
|
|
|
|
} else {
|
2008-11-14 20:51:06 +00:00
|
|
|
if (_Settings->LoadDword(RomBrowser_Enabled))
|
|
|
|
{
|
|
|
|
WriteTrace(TraceDebug,"WinMain - Show Rom Browser");
|
2008-09-18 03:15:49 +00:00
|
|
|
//Display the rom browser
|
|
|
|
MainWindow.SetPluginList(&Plugins);
|
|
|
|
MainWindow.ShowRomList();
|
|
|
|
MainWindow.Show(true); //Show the main window
|
|
|
|
MainWindow.HighLightLastRom();
|
|
|
|
} else {
|
2008-11-14 20:51:06 +00:00
|
|
|
WriteTrace(TraceDebug,"WinMain - Show Main Window");
|
2008-09-18 03:15:49 +00:00
|
|
|
MainWindow.Show(true); //Show the main window
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Process Messages till program is closed
|
2008-11-14 20:51:06 +00:00
|
|
|
WriteTrace(TraceDebug,"WinMain - Entering Message Loop");
|
2008-09-18 03:15:49 +00:00
|
|
|
MainWindow.ProcessAllMessages();
|
2008-11-14 20:51:06 +00:00
|
|
|
WriteTrace(TraceDebug,"WinMain - Message Loop Finished");
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
N64System.CloseCpu(); //terminate the cpu thread before quiting
|
2008-11-14 20:51:06 +00:00
|
|
|
|
|
|
|
WriteTrace(TraceDebug,"WinMain - System Closed");
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
2008-11-14 20:51:06 +00:00
|
|
|
WriteTraceF(TraceError,"WinMain - Exception caught (File: \"%s\" Line: %d)",__FILE__,__LINE__);
|
2008-09-18 03:15:49 +00:00
|
|
|
MessageBox(NULL,stdstr_f("Exception caught\nFile: %s\nLine: %d",__FILE__,__LINE__).c_str(),"Exception",MB_OK);
|
|
|
|
}
|
2008-11-14 20:51:06 +00:00
|
|
|
WriteTrace(TraceDebug,"WinMain - cleaning up global objects");
|
2008-09-18 03:15:49 +00:00
|
|
|
if (_Settings)
|
|
|
|
{
|
|
|
|
delete _Settings;
|
|
|
|
_Settings = NULL;
|
|
|
|
}
|
|
|
|
if (_Lang)
|
|
|
|
{
|
|
|
|
delete _Lang;
|
|
|
|
_Lang = NULL;
|
|
|
|
}
|
|
|
|
CoUninitialize();
|
|
|
|
WriteTrace(TraceDebug,"WinMain - Done");
|
|
|
|
CloseTrace();
|
|
|
|
return true;
|
|
|
|
}
|