From 9e9189c1927b3de77bcebbf7c6e5995fd44dc24c Mon Sep 17 00:00:00 2001 From: zilmar Date: Sun, 15 Nov 2015 07:56:40 +1100 Subject: [PATCH] [Project64] move program inir/cleanup code in to a separate file --- Source/Project64/AppInit.cpp | 139 +++++++++++ Source/Project64/AppInit.h | 6 + Source/Project64/Project64.vcxproj | 2 + Source/Project64/Project64.vcxproj.filters | 6 + Source/Project64/main.cpp | 254 +-------------------- 5 files changed, 160 insertions(+), 247 deletions(-) create mode 100644 Source/Project64/AppInit.cpp create mode 100644 Source/Project64/AppInit.h diff --git a/Source/Project64/AppInit.cpp b/Source/Project64/AppInit.cpp new file mode 100644 index 000000000..9937de1c9 --- /dev/null +++ b/Source/Project64/AppInit.cpp @@ -0,0 +1,139 @@ +#include "stdafx.h" +#include + +void FixDirectories(void); +void FixLocale(void); + +static void IncreaseThreadPriority(void); + +static CTraceFileLog * g_LogFile = NULL; + +void LogLevelChanged(CTraceFileLog * LogFile) +{ + LogFile->SetTraceLevel((TraceLevel)g_Settings->LoadDword(Debugger_AppLogLevel)); +} + +void LogFlushChanged(CTraceFileLog * LogFile) +{ + LogFile->SetFlushFile(g_Settings->LoadDword(Debugger_AppLogFlush) != 0); +} + +void InitializeLog(void) +{ + CPath LogFilePath(CPath::MODULE_DIRECTORY); + LogFilePath.AppendDirectory("Logs"); + if (!LogFilePath.DirectoryExists()) + { + LogFilePath.DirectoryCreate(); + } + LogFilePath.SetNameExtension("Project64.log"); + + g_LogFile = new CTraceFileLog(LogFilePath, g_Settings->LoadDword(Debugger_AppLogFlush) != 0, Log_New, 500); +#ifdef VALIDATE_DEBUG + g_LogFile->SetTraceLevel((TraceLevel)(g_Settings->LoadDword(Debugger_AppLogLevel) | TraceValidate | TraceDebug)); +#else + g_LogFile->SetTraceLevel((TraceLevel)g_Settings->LoadDword(Debugger_AppLogLevel)); +#endif + AddTraceModule(g_LogFile); + + g_Settings->RegisterChangeCB(Debugger_AppLogLevel, g_LogFile, (CSettings::SettingChangedFunc)LogLevelChanged); + g_Settings->RegisterChangeCB(Debugger_AppLogFlush, g_LogFile, (CSettings::SettingChangedFunc)LogFlushChanged); +} + +void AppInit(CNotification * Notify) +{ + try + { + g_Notify = Notify; + + FixDirectories(); + FixLocale(); + + stdstr_f AppName("Project64 %s", VER_FILE_VERSION_STR); + IncreaseThreadPriority(); + + g_Settings = new CSettings; + g_Settings->Initialize(AppName.c_str()); + + if (g_Settings->LoadBool(Setting_CheckEmuRunning) && + pjutil::TerminatedExistingExe()) + { + delete g_Settings; + g_Settings = new CSettings; + g_Settings->Initialize(AppName.c_str()); + } + + InitializeLog(); + + WriteTrace(TraceDebug, __FUNCTION__ ": Application Starting"); + CMipsMemoryVM::ReserveMemory(); + + //Create the plugin container + WriteTrace(TraceDebug, __FUNCTION__ ": Create Plugins"); + g_Plugins = new CPlugins(g_Settings->LoadStringVal(Directory_Plugin)); + + g_Lang = new CLanguage(); + g_Lang->LoadCurrentStrings(); + g_Notify->AppInitDone(); + } + catch (...) + { + g_Notify->DisplayError(stdstr_f("Exception caught\nFile: %s\nLine: %d", __FILE__, __LINE__).ToUTF16().c_str()); + } +} + +void AppCleanup(void) +{ + g_Settings->UnregisterChangeCB(Debugger_AppLogLevel, g_LogFile, (CSettings::SettingChangedFunc)LogLevelChanged); + g_Settings->UnregisterChangeCB(Debugger_AppLogFlush, g_LogFile, (CSettings::SettingChangedFunc)LogFlushChanged); + WriteTrace(TraceDebug, __FUNCTION__ ": cleaning up global objects"); + + if (g_Rom) { delete g_Rom; g_Rom = NULL; } + if (g_Plugins) { delete g_Plugins; g_Plugins = NULL; } + if (g_Settings) { delete g_Settings; g_Settings = NULL; } + if (g_Lang) { delete g_Lang; g_Lang = NULL; } + + CMipsMemoryVM::FreeReservedMemory(); + + WriteTrace(TraceDebug, __FUNCTION__ ": Done"); + CloseTrace(); +} + +void FixDirectories(void) +{ + CPath Directory(CPath::MODULE_DIRECTORY); + Directory.AppendDirectory("Config"); + if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); + + Directory.UpDirectory(); + Directory.AppendDirectory("Logs"); + if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); + + Directory.UpDirectory(); + Directory.AppendDirectory("Save"); + if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); + + Directory.UpDirectory(); + Directory.AppendDirectory("Screenshots"); + if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); + + Directory.UpDirectory(); + Directory.AppendDirectory("textures"); + if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); +} + +#include +void FixLocale(void) +{ + char *lbuffer = new char[10]; + if (GetLocaleInfoA(LOCALE_SYSTEM_DEFAULT, LOCALE_SABBREVLANGNAME, lbuffer, 10)) + { + setlocale(LC_ALL, lbuffer); + } + delete[] lbuffer; +} + +void IncreaseThreadPriority(void) +{ + SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL); +} \ No newline at end of file diff --git a/Source/Project64/AppInit.h b/Source/Project64/AppInit.h new file mode 100644 index 000000000..6c62a2395 --- /dev/null +++ b/Source/Project64/AppInit.h @@ -0,0 +1,6 @@ +#pragma once + +#include + +void AppInit(CNotification * Notify); +void AppCleanup(void); diff --git a/Source/Project64/Project64.vcxproj b/Source/Project64/Project64.vcxproj index 66fd0c3ae..3675d5abc 100644 --- a/Source/Project64/Project64.vcxproj +++ b/Source/Project64/Project64.vcxproj @@ -44,6 +44,7 @@ + @@ -178,6 +179,7 @@ + diff --git a/Source/Project64/Project64.vcxproj.filters b/Source/Project64/Project64.vcxproj.filters index dd0483794..0127d1bc3 100644 --- a/Source/Project64/Project64.vcxproj.filters +++ b/Source/Project64/Project64.vcxproj.filters @@ -426,6 +426,9 @@ Source Files\N64 System Source + + Source Files + @@ -845,5 +848,8 @@ Header Files\Settings Headers + + Header Files + \ No newline at end of file diff --git a/Source/Project64/main.cpp b/Source/Project64/main.cpp index 0c84246e3..c58fe7238 100644 --- a/Source/Project64/main.cpp +++ b/Source/Project64/main.cpp @@ -1,247 +1,21 @@ #include "stdafx.h" +#include #include "Multilanguage\LanguageSelector.h" -#include -CTraceFileLog * LogFile = NULL; - -void LogLevelChanged(CTraceFileLog * LogFile) -{ - LogFile->SetTraceLevel((TraceLevel)g_Settings->LoadDword(Debugger_AppLogLevel)); -} - -void LogFlushChanged(CTraceFileLog * LogFile) -{ - LogFile->SetFlushFile(g_Settings->LoadDword(Debugger_AppLogFlush) != 0); -} - -void InitializeLog(void) -{ - CPath LogFilePath(CPath::MODULE_DIRECTORY); - LogFilePath.AppendDirectory("Logs"); - if (!LogFilePath.DirectoryExists()) - { - LogFilePath.DirectoryCreate(); - } - LogFilePath.SetNameExtension("Project64.log"); - - LogFile = new CTraceFileLog(LogFilePath, g_Settings->LoadDword(Debugger_AppLogFlush) != 0, Log_New, 500); -#ifdef VALIDATE_DEBUG - LogFile->SetTraceLevel((TraceLevel)(g_Settings->LoadDword(Debugger_AppLogLevel) | TraceValidate | TraceDebug)); -#else - LogFile->SetTraceLevel((TraceLevel)g_Settings->LoadDword(Debugger_AppLogLevel)); -#endif - AddTraceModule(LogFile); - - g_Settings->RegisterChangeCB(Debugger_AppLogLevel, LogFile, (CSettings::SettingChangedFunc)LogLevelChanged); - g_Settings->RegisterChangeCB(Debugger_AppLogFlush, LogFile, (CSettings::SettingChangedFunc)LogFlushChanged); -} - -/*bool ChangeDirPermission ( const CPath & Dir) -{ -if (Dir.DirectoryExists()) -{ -HANDLE hDir = CreateFile(Dir,READ_CONTROL|WRITE_DAC,0,NULL,OPEN_EXISTING,FILE_FLAG_BACKUP_SEMANTICS,NULL); -if (hDir != INVALID_HANDLE_VALUE) -{ -ACL * pOldDACL = NULL; -PSECURITY_DESCRIPTOR pSD = NULL; - -if (GetSecurityInfo(hDir,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,NULL,NULL,&pOldDACL,NULL,&pSD) == ERROR_SUCCESS) -{ -bool bAdd = true; - -PEXPLICIT_ACCESS_W pListOfExplictEntries; -ULONG cCountOfExplicitEntries; -if (GetExplicitEntriesFromAclW(pOldDACL,&cCountOfExplicitEntries,&pListOfExplictEntries) == ERROR_SUCCESS) -{ -for (int i = 0; i < cCountOfExplicitEntries; i ++) -{ -EXPLICIT_ACCESS_W &ea = pListOfExplictEntries[i]; -if (ea.grfAccessMode != GRANT_ACCESS) { continue; } -if (ea.grfAccessPermissions != GENERIC_ALL) { continue; } -if ((ea.grfInheritance & (CONTAINER_INHERIT_ACE|OBJECT_INHERIT_ACE)) != (CONTAINER_INHERIT_ACE|OBJECT_INHERIT_ACE)) { continue; } - -if (ea.Trustee.TrusteeType == TRUSTEE_IS_SID) -{ -} -bAdd = false; -} -} - -if (bAdd) -{ -EXPLICIT_ACCESS ea = {0}; -ea.grfAccessMode = GRANT_ACCESS; -ea.grfAccessPermissions = GENERIC_ALL; -ea.grfInheritance = CONTAINER_INHERIT_ACE|OBJECT_INHERIT_ACE; -ea.Trustee.TrusteeType = TRUSTEE_IS_GROUP; -ea.Trustee.TrusteeForm = TRUSTEE_IS_NAME; -ea.Trustee.ptstrName = TEXT("Users"); - -ACL * pNewDACL = NULL; -SetEntriesInAcl(1,&ea,pOldDACL,&pNewDACL); - -SetSecurityInfo(hDir,SE_FILE_OBJECT,DACL_SECURITY_INFORMATION,NULL,NULL,pNewDACL,NULL); -LocalFree(pNewDACL); -} -LocalFree(pSD); -} -CloseHandle(hDir); -} -} -return true; -}*/ - -void FixDirectories(void) -{ - CPath Directory(CPath::MODULE_DIRECTORY); - Directory.AppendDirectory("Config"); - if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); - - Directory.UpDirectory(); - Directory.AppendDirectory("Logs"); - if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); - - Directory.UpDirectory(); - Directory.AppendDirectory("Save"); - if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); - - Directory.UpDirectory(); - Directory.AppendDirectory("Screenshots"); - if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); - - Directory.UpDirectory(); - Directory.AppendDirectory("textures"); - if (!Directory.DirectoryExists()) Directory.DirectoryCreate(); -} - -bool TerminatedExistingEmu() -{ - bool bTerminated = false; - bool AskedUser = false; - DWORD pid = GetCurrentProcessId(); - - HANDLE nSearch = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0); - if (nSearch != INVALID_HANDLE_VALUE) - { - PROCESSENTRY32 lppe; - - memset(&lppe, 0, sizeof(PROCESSENTRY32)); - lppe.dwSize = sizeof(PROCESSENTRY32); - stdstr ModuleName = CPath(CPath::MODULE_FILE).GetNameExtension(); - - if (Process32First(nSearch, &lppe)) - { - do - { - if (_stricmp(lppe.szExeFile, ModuleName.c_str()) != 0 || - lppe.th32ProcessID == pid) - { - continue; - } - if (!AskedUser) - { - 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); - if (res != IDYES) - { - break; - } - } - HANDLE hHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lppe.th32ProcessID); - if (hHandle != NULL) - { - if (TerminateProcess(hHandle, 0)) - { - bTerminated = true; - } - else { - MessageBox(NULL, stdstr_f("Failed to terminate pid %d", lppe.th32ProcessID).c_str(), "Terminate project64 failed!", MB_YESNO | MB_ICONEXCLAMATION); - } - CloseHandle(hHandle); - } - } while (Process32Next(nSearch, &lppe)); - } - CloseHandle(nSearch); - } - return bTerminated; -} - -const char * AppName(void) -{ - static stdstr Name; - if (Name.empty()) - { - Name = stdstr_f("Project64 %s", VER_FILE_VERSION_STR); - } - return Name.c_str(); -} - -#ifndef WINDOWS_UI -int main(int argc, char* argv[]) -{ - while (argc > 0) - { - puts(argv[--argc]); - } - putchar('\n'); - - fprintf( - stderr, - "Cross-platform (graphical/terminal?) UI not yet implemented.\n" - ); - return 0; -} -#else int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpszArgs*/, int /*nWinMode*/) { - FixDirectories(); - - char *lbuffer = new char[10]; - if (GetLocaleInfoA(LOCALE_SYSTEM_DEFAULT, LOCALE_SABBREVLANGNAME, lbuffer, 10)) - setlocale(LC_ALL, lbuffer); - delete[] lbuffer; - - CoInitialize(NULL); try { - SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL); - g_Settings = new CSettings; - g_Settings->Initialize(AppName()); - - if (g_Settings->LoadBool(Setting_CheckEmuRunning) && - TerminatedExistingEmu()) - { - delete g_Settings; - g_Settings = new CSettings; - g_Settings->Initialize(AppName()); - } - - InitializeLog(); - - WriteTrace(TraceDebug, __FUNCTION__ ": Application Starting"); - CMipsMemoryVM::ReserveMemory(); - - g_Notify = &Notify(); - - //Create the plugin container - WriteTrace(TraceDebug, __FUNCTION__ ": Create Plugins"); - g_Plugins = new CPlugins(g_Settings->LoadStringVal(Directory_Plugin)); - - //Select the language - g_Lang = new CLanguage(); - if (!g_Lang->LoadCurrentStrings()) + CoInitialize(NULL); + AppInit(&Notify()); + if (!g_Lang->IsLanguageLoaded()) { CLanguageSelector().Select(); } //Create the main window with Menu WriteTrace(TraceDebug, __FUNCTION__ ": Create Main Window"); - stdstr WinTitle(AppName()); - - WinTitle.Format("Project64 %s", VER_FILE_VERSION_STR); - - CMainGui MainWindow(true, WinTitle.c_str()), HiddenWindow(false); + CMainGui MainWindow(true, stdstr_f("Project64 %s", VER_FILE_VERSION_STR).c_str()), HiddenWindow(false); CMainMenu MainMenu(&MainWindow); g_Plugins->SetRenderWindows(&MainWindow, &HiddenWindow); Notify().SetMainWindow(&MainWindow); @@ -280,27 +54,13 @@ int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR / g_BaseSystem = NULL; } WriteTrace(TraceDebug, __FUNCTION__ ": System Closed"); - - g_Settings->UnregisterChangeCB(Debugger_AppLogLevel, LogFile, (CSettings::SettingChangedFunc)LogLevelChanged); - g_Settings->UnregisterChangeCB(Debugger_AppLogFlush, LogFile, (CSettings::SettingChangedFunc)LogFlushChanged); } catch (...) { 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); } - WriteTrace(TraceDebug, __FUNCTION__ ": cleaning up global objects"); - - if (g_Rom) { delete g_Rom; g_Rom = NULL; } - if (g_Plugins) { delete g_Plugins; g_Plugins = NULL; } - if (g_Settings) { delete g_Settings; g_Settings = NULL; } - if (g_Lang) { delete g_Lang; g_Lang = NULL; } - - CMipsMemoryVM::FreeReservedMemory(); - + AppCleanup(); CoUninitialize(); - WriteTrace(TraceDebug, __FUNCTION__ ": Done"); - CloseTrace(); return true; -} -#endif \ No newline at end of file +} \ No newline at end of file