[Project64] Cleanup main.cpp

This commit is contained in:
zilmar 2015-11-14 08:18:36 +11:00
parent 003bcc9096
commit eab2acdecf
1 changed files with 91 additions and 90 deletions

View File

@ -4,27 +4,27 @@
CTraceFileLog * LogFile = NULL; CTraceFileLog * LogFile = NULL;
void LogLevelChanged (CTraceFileLog * LogFile) void LogLevelChanged(CTraceFileLog * LogFile)
{ {
LogFile->SetTraceLevel((TraceLevel)g_Settings->LoadDword(Debugger_AppLogLevel)); LogFile->SetTraceLevel((TraceLevel)g_Settings->LoadDword(Debugger_AppLogLevel));
} }
void LogFlushChanged (CTraceFileLog * LogFile) void LogFlushChanged(CTraceFileLog * LogFile)
{ {
LogFile->SetFlushFile(g_Settings->LoadDword(Debugger_AppLogFlush) != 0); LogFile->SetFlushFile(g_Settings->LoadDword(Debugger_AppLogFlush) != 0);
} }
void InitializeLog ( void) void InitializeLog(void)
{ {
CPath LogFilePath(CPath::MODULE_DIRECTORY); CPath LogFilePath(CPath::MODULE_DIRECTORY);
LogFilePath.AppendDirectory("Logs"); LogFilePath.AppendDirectory("Logs");
if (!LogFilePath.DirectoryExists()) if (!LogFilePath.DirectoryExists())
{ {
LogFilePath.DirectoryCreate(); LogFilePath.DirectoryCreate();
} }
LogFilePath.SetNameExtension("Project64.log"); LogFilePath.SetNameExtension("Project64.log");
LogFile = new CTraceFileLog(LogFilePath, g_Settings->LoadDword(Debugger_AppLogFlush) != 0, Log_New,500); LogFile = new CTraceFileLog(LogFilePath, g_Settings->LoadDword(Debugger_AppLogFlush) != 0, Log_New, 500);
#ifdef VALIDATE_DEBUG #ifdef VALIDATE_DEBUG
LogFile->SetTraceLevel((TraceLevel)(g_Settings->LoadDword(Debugger_AppLogLevel) | TraceValidate | TraceDebug)); LogFile->SetTraceLevel((TraceLevel)(g_Settings->LoadDword(Debugger_AppLogLevel) | TraceValidate | TraceDebug));
#else #else
@ -32,8 +32,8 @@ void InitializeLog ( void)
#endif #endif
AddTraceModule(LogFile); AddTraceModule(LogFile);
g_Settings->RegisterChangeCB(Debugger_AppLogLevel,LogFile,(CSettings::SettingChangedFunc)LogLevelChanged); g_Settings->RegisterChangeCB(Debugger_AppLogLevel, LogFile, (CSettings::SettingChangedFunc)LogLevelChanged);
g_Settings->RegisterChangeCB(Debugger_AppLogFlush,LogFile,(CSettings::SettingChangedFunc)LogFlushChanged); g_Settings->RegisterChangeCB(Debugger_AppLogFlush, LogFile, (CSettings::SettingChangedFunc)LogFlushChanged);
} }
/*bool ChangeDirPermission ( const CPath & Dir) /*bool ChangeDirPermission ( const CPath & Dir)
@ -92,27 +92,27 @@ CloseHandle(hDir);
return true; return true;
}*/ }*/
void FixDirectories ( void ) void FixDirectories(void)
{ {
CPath Directory(CPath::MODULE_DIRECTORY); CPath Directory(CPath::MODULE_DIRECTORY);
Directory.AppendDirectory("Config"); Directory.AppendDirectory("Config");
if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); if (!Directory.DirectoryExists()) Directory.DirectoryCreate();
Directory.UpDirectory(); Directory.UpDirectory();
Directory.AppendDirectory("Logs"); Directory.AppendDirectory("Logs");
if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); if (!Directory.DirectoryExists()) Directory.DirectoryCreate();
Directory.UpDirectory(); Directory.UpDirectory();
Directory.AppendDirectory("Save"); Directory.AppendDirectory("Save");
if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); if (!Directory.DirectoryExists()) Directory.DirectoryCreate();
Directory.UpDirectory(); Directory.UpDirectory();
Directory.AppendDirectory("Screenshots"); Directory.AppendDirectory("Screenshots");
if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); if (!Directory.DirectoryExists()) Directory.DirectoryCreate();
Directory.UpDirectory(); Directory.UpDirectory();
Directory.AppendDirectory("textures"); Directory.AppendDirectory("textures");
if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); if (!Directory.DirectoryExists()) Directory.DirectoryCreate();
} }
bool TerminatedExistingEmu() bool TerminatedExistingEmu()
@ -122,7 +122,7 @@ bool TerminatedExistingEmu()
DWORD pid = GetCurrentProcessId(); DWORD pid = GetCurrentProcessId();
HANDLE nSearch = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); HANDLE nSearch = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(nSearch != INVALID_HANDLE_VALUE) if (nSearch != INVALID_HANDLE_VALUE)
{ {
PROCESSENTRY32 lppe; PROCESSENTRY32 lppe;
@ -134,7 +134,7 @@ bool TerminatedExistingEmu()
{ {
do do
{ {
if(_stricmp(lppe.szExeFile, ModuleName.c_str()) != 0 || if (_stricmp(lppe.szExeFile, ModuleName.c_str()) != 0 ||
lppe.th32ProcessID == pid) lppe.th32ProcessID == pid)
{ {
continue; continue;
@ -142,20 +142,21 @@ bool TerminatedExistingEmu()
if (!AskedUser) if (!AskedUser)
{ {
AskedUser = true; AskedUser = true;
int res = MessageBox(NULL,stdstr_f("Project64.exe currently running\n\nTerminate pid %d now?",lppe.th32ProcessID).c_str(),"Terminate project64",MB_YESNO|MB_ICONEXCLAMATION); int res = MessageBox(NULL, stdstr_f("Project64.exe currently running\n\nTerminate pid %d now?", lppe.th32ProcessID).c_str(), "Terminate project64", MB_YESNO | MB_ICONEXCLAMATION);
if (res != IDYES) if (res != IDYES)
{ {
break; break;
} }
} }
HANDLE hHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lppe.th32ProcessID); HANDLE hHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lppe.th32ProcessID);
if(hHandle != NULL) if (hHandle != NULL)
{ {
if (TerminateProcess(hHandle, 0)) if (TerminateProcess(hHandle, 0))
{ {
bTerminated = true; bTerminated = true;
} else { }
MessageBox(NULL,stdstr_f("Failed to terminate pid %d",lppe.th32ProcessID).c_str(),"Terminate project64 failed!",MB_YESNO|MB_ICONEXCLAMATION); else {
MessageBox(NULL, stdstr_f("Failed to terminate pid %d", lppe.th32ProcessID).c_str(), "Terminate project64 failed!", MB_YESNO | MB_ICONEXCLAMATION);
} }
CloseHandle(hHandle); CloseHandle(hHandle);
} }
@ -166,7 +167,7 @@ bool TerminatedExistingEmu()
return bTerminated; return bTerminated;
} }
const char * AppName ( void ) const char * AppName(void)
{ {
static stdstr Name; static stdstr Name;
if (Name.empty()) if (Name.empty())
@ -188,67 +189,66 @@ int main(int argc, char* argv[])
fprintf( fprintf(
stderr, stderr,
"Cross-platform (graphical/terminal?) UI not yet implemented.\n" "Cross-platform (graphical/terminal?) UI not yet implemented.\n"
); );
return 0; return 0;
} }
#else #else
int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpszArgs*/, int /*nWinMode*/) int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpszArgs*/, int /*nWinMode*/)
{ {
FixDirectories(); FixDirectories();
char *lbuffer = new char[10]; char *lbuffer = new char[10];
if (GetLocaleInfoA(LOCALE_SYSTEM_DEFAULT, LOCALE_SABBREVLANGNAME, lbuffer, 10)) if (GetLocaleInfoA(LOCALE_SYSTEM_DEFAULT, LOCALE_SABBREVLANGNAME, lbuffer, 10))
setlocale(LC_ALL, lbuffer); setlocale(LC_ALL, lbuffer);
delete[] lbuffer; delete[] lbuffer;
CoInitialize(NULL); CoInitialize(NULL);
try try
{ {
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL ); SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
g_Settings = new CSettings; g_Settings = new CSettings;
g_Settings->Initialize(AppName()); g_Settings->Initialize(AppName());
if (g_Settings->LoadBool(Setting_CheckEmuRunning) && if (g_Settings->LoadBool(Setting_CheckEmuRunning) &&
TerminatedExistingEmu()) TerminatedExistingEmu())
{ {
delete g_Settings; delete g_Settings;
g_Settings = new CSettings; g_Settings = new CSettings;
g_Settings->Initialize(AppName()); g_Settings->Initialize(AppName());
} }
InitializeLog(); InitializeLog();
WriteTrace(TraceDebug,__FUNCTION__ ": Application Starting"); WriteTrace(TraceDebug, __FUNCTION__ ": Application Starting");
CMipsMemoryVM::ReserveMemory(); CMipsMemoryVM::ReserveMemory();
g_Notify = &Notify(); g_Notify = &Notify();
//Create the plugin container //Create the plugin container
WriteTrace(TraceDebug,__FUNCTION__ ": Create Plugins"); WriteTrace(TraceDebug, __FUNCTION__ ": Create Plugins");
g_Plugins = new CPlugins(g_Settings->LoadStringVal(Directory_Plugin)); g_Plugins = new CPlugins(g_Settings->LoadStringVal(Directory_Plugin));
//Select the language
g_Lang = new CLanguage();
if (!g_Lang->LoadCurrentStrings())
{
CLanguageSelector().Select();
}
//Select the language
g_Lang = new CLanguage();
if (!g_Lang->LoadCurrentStrings())
{
CLanguageSelector().Select();
}
//Create the main window with Menu //Create the main window with Menu
WriteTrace(TraceDebug,__FUNCTION__ ": Create Main Window"); WriteTrace(TraceDebug, __FUNCTION__ ": Create Main Window");
stdstr WinTitle(AppName()); stdstr WinTitle(AppName());
WinTitle.Format("Project64 %s", VER_FILE_VERSION_STR); WinTitle.Format("Project64 %s", VER_FILE_VERSION_STR);
CMainGui MainWindow(true,WinTitle.c_str()), HiddenWindow(false); CMainGui MainWindow(true, WinTitle.c_str()), HiddenWindow(false);
CMainMenu MainMenu(&MainWindow); CMainMenu MainMenu(&MainWindow);
g_Plugins->SetRenderWindows(&MainWindow,&HiddenWindow); g_Plugins->SetRenderWindows(&MainWindow, &HiddenWindow);
Notify().SetMainWindow(&MainWindow); Notify().SetMainWindow(&MainWindow);
if (__argc > 1) if (__argc > 1)
{ {
WriteTraceF(TraceDebug,__FUNCTION__ ": Cmd line found \"%s\"",__argv[1]); WriteTraceF(TraceDebug, __FUNCTION__ ": Cmd line found \"%s\"", __argv[1]);
MainWindow.Show(true); //Show the main window MainWindow.Show(true); //Show the main window
CN64System::RunFileImage(__argv[1]); CN64System::RunFileImage(__argv[1]);
} }
@ -256,21 +256,22 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /
{ {
if (g_Settings->LoadDword(RomBrowser_Enabled)) if (g_Settings->LoadDword(RomBrowser_Enabled))
{ {
WriteTrace(TraceDebug,__FUNCTION__ ": Show Rom Browser"); WriteTrace(TraceDebug, __FUNCTION__ ": Show Rom Browser");
//Display the rom browser //Display the rom browser
MainWindow.ShowRomList(); MainWindow.ShowRomList();
MainWindow.Show(true); //Show the main window MainWindow.Show(true); //Show the main window
MainWindow.HighLightLastRom(); MainWindow.HighLightLastRom();
} else { }
WriteTrace(TraceDebug,__FUNCTION__ ": Show Main Window"); else {
WriteTrace(TraceDebug, __FUNCTION__ ": Show Main Window");
MainWindow.Show(true); //Show the main window MainWindow.Show(true); //Show the main window
} }
} }
//Process Messages till program is closed //Process Messages till program is closed
WriteTrace(TraceDebug,__FUNCTION__ ": Entering Message Loop"); WriteTrace(TraceDebug, __FUNCTION__ ": Entering Message Loop");
MainWindow.ProcessAllMessages(); MainWindow.ProcessAllMessages();
WriteTrace(TraceDebug,__FUNCTION__ ": Message Loop Finished"); WriteTrace(TraceDebug, __FUNCTION__ ": Message Loop Finished");
if (g_BaseSystem) if (g_BaseSystem)
{ {
@ -278,28 +279,28 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /
delete g_BaseSystem; delete g_BaseSystem;
g_BaseSystem = NULL; g_BaseSystem = NULL;
} }
WriteTrace(TraceDebug,__FUNCTION__ ": System Closed"); WriteTrace(TraceDebug, __FUNCTION__ ": System Closed");
g_Settings->UnregisterChangeCB(Debugger_AppLogLevel,LogFile,(CSettings::SettingChangedFunc)LogLevelChanged); g_Settings->UnregisterChangeCB(Debugger_AppLogLevel, LogFile, (CSettings::SettingChangedFunc)LogLevelChanged);
g_Settings->UnregisterChangeCB(Debugger_AppLogFlush,LogFile,(CSettings::SettingChangedFunc)LogFlushChanged); g_Settings->UnregisterChangeCB(Debugger_AppLogFlush, LogFile, (CSettings::SettingChangedFunc)LogFlushChanged);
} }
catch(...) catch (...)
{ {
WriteTraceF(TraceError,__FUNCTION__ ": Exception caught (File: \"%s\" Line: %d)",__FILE__,__LINE__); WriteTraceF(TraceError, __FUNCTION__ ": Exception caught (File: \"%s\" Line: %d)", __FILE__, __LINE__);
MessageBox(NULL,stdstr_f("Exception caught\nFile: %s\nLine: %d",__FILE__,__LINE__).c_str(),"Exception",MB_OK); MessageBox(NULL, stdstr_f("Exception caught\nFile: %s\nLine: %d", __FILE__, __LINE__).c_str(), "Exception", MB_OK);
} }
WriteTrace(TraceDebug,__FUNCTION__ ": cleaning up global objects"); WriteTrace(TraceDebug, __FUNCTION__ ": cleaning up global objects");
if (g_Rom) { delete g_Rom; g_Rom = NULL; } if (g_Rom) { delete g_Rom; g_Rom = NULL; }
if (g_Plugins) { delete g_Plugins; g_Plugins = NULL; } if (g_Plugins) { delete g_Plugins; g_Plugins = NULL; }
if (g_Settings) { delete g_Settings; g_Settings = NULL; } if (g_Settings) { delete g_Settings; g_Settings = NULL; }
if (g_Lang) { delete g_Lang; g_Lang = NULL; } if (g_Lang) { delete g_Lang; g_Lang = NULL; }
CMipsMemoryVM::FreeReservedMemory(); CMipsMemoryVM::FreeReservedMemory();
CoUninitialize(); CoUninitialize();
WriteTrace(TraceDebug,__FUNCTION__ ": Done"); WriteTrace(TraceDebug, __FUNCTION__ ": Done");
CloseTrace(); CloseTrace();
return true; return true;
} }
#endif #endif