project64/Source/Project64/main.cpp

306 lines
9.8 KiB
C++
Raw Normal View History

#include "stdafx.h"
#include "Multilanguage\LanguageSelector.h"
#include <Tlhelp32.h>
CTraceFileLog * LogFile = NULL;
2015-11-13 21:18:36 +00:00
void LogLevelChanged(CTraceFileLog * LogFile)
{
2015-10-25 11:10:54 +00:00
LogFile->SetTraceLevel((TraceLevel)g_Settings->LoadDword(Debugger_AppLogLevel));
}
2015-11-13 21:18:36 +00:00
void LogFlushChanged(CTraceFileLog * LogFile)
{
2015-10-25 11:10:54 +00:00
LogFile->SetFlushFile(g_Settings->LoadDword(Debugger_AppLogFlush) != 0);
}
2015-11-13 21:18:36 +00:00
void InitializeLog(void)
{
2015-10-25 11:10:54 +00:00
CPath LogFilePath(CPath::MODULE_DIRECTORY);
LogFilePath.AppendDirectory("Logs");
if (!LogFilePath.DirectoryExists())
{
2015-11-13 21:18:36 +00:00
LogFilePath.DirectoryCreate();
2015-10-25 11:10:54 +00:00
}
LogFilePath.SetNameExtension("Project64.log");
2015-11-13 21:18:36 +00:00
LogFile = new CTraceFileLog(LogFilePath, g_Settings->LoadDword(Debugger_AppLogFlush) != 0, Log_New, 500);
#ifdef VALIDATE_DEBUG
2015-10-25 11:10:54 +00:00
LogFile->SetTraceLevel((TraceLevel)(g_Settings->LoadDword(Debugger_AppLogLevel) | TraceValidate | TraceDebug));
#else
2015-10-25 11:10:54 +00:00
LogFile->SetTraceLevel((TraceLevel)g_Settings->LoadDword(Debugger_AppLogLevel));
#endif
2015-10-25 11:10:54 +00:00
AddTraceModule(LogFile);
2015-11-13 21:18:36 +00:00
g_Settings->RegisterChangeCB(Debugger_AppLogLevel, LogFile, (CSettings::SettingChangedFunc)LogLevelChanged);
g_Settings->RegisterChangeCB(Debugger_AppLogFlush, LogFile, (CSettings::SettingChangedFunc)LogFlushChanged);
2015-10-25 11:10:54 +00:00
}
/*bool ChangeDirPermission ( const CPath & Dir)
{
2015-10-25 11:10:54 +00:00
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;
}*/
2015-11-13 21:18:36 +00:00
void FixDirectories(void)
{
2015-11-13 21:18:36 +00:00
CPath Directory(CPath::MODULE_DIRECTORY);
Directory.AppendDirectory("Config");
if (!Directory.DirectoryExists()) Directory.DirectoryCreate();
2015-11-13 21:18:36 +00:00
Directory.UpDirectory();
Directory.AppendDirectory("Logs");
if (!Directory.DirectoryExists()) Directory.DirectoryCreate();
2015-11-13 21:18:36 +00:00
Directory.UpDirectory();
Directory.AppendDirectory("Save");
if (!Directory.DirectoryExists()) Directory.DirectoryCreate();
2015-11-13 21:18:36 +00:00
Directory.UpDirectory();
Directory.AppendDirectory("Screenshots");
if (!Directory.DirectoryExists()) Directory.DirectoryCreate();
2015-11-13 21:18:36 +00:00
Directory.UpDirectory();
Directory.AppendDirectory("textures");
if (!Directory.DirectoryExists()) Directory.DirectoryCreate();
}
bool TerminatedExistingEmu()
{
2015-10-25 11:10:54 +00:00
bool bTerminated = false;
bool AskedUser = false;
DWORD pid = GetCurrentProcessId();
2015-10-25 11:10:54 +00:00
HANDLE nSearch = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
2015-11-13 21:18:36 +00:00
if (nSearch != INVALID_HANDLE_VALUE)
2015-10-25 11:10:54 +00:00
{
PROCESSENTRY32 lppe;
memset(&lppe, 0, sizeof(PROCESSENTRY32));
lppe.dwSize = sizeof(PROCESSENTRY32);
stdstr ModuleName = CPath(CPath::MODULE_FILE).GetNameExtension();
if (Process32First(nSearch, &lppe))
{
do
{
2015-11-13 21:18:36 +00:00
if (_stricmp(lppe.szExeFile, ModuleName.c_str()) != 0 ||
2015-10-25 11:10:54 +00:00
lppe.th32ProcessID == pid)
{
continue;
}
if (!AskedUser)
{
AskedUser = true;
2015-11-13 21:18:36 +00:00
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);
2015-10-25 11:10:54 +00:00
if (res != IDYES)
{
break;
}
}
HANDLE hHandle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lppe.th32ProcessID);
2015-11-13 21:18:36 +00:00
if (hHandle != NULL)
2015-10-25 11:10:54 +00:00
{
if (TerminateProcess(hHandle, 0))
{
bTerminated = true;
2015-11-13 21:18:36 +00:00
}
else {
MessageBox(NULL, stdstr_f("Failed to terminate pid %d", lppe.th32ProcessID).c_str(), "Terminate project64 failed!", MB_YESNO | MB_ICONEXCLAMATION);
2015-10-25 11:10:54 +00:00
}
CloseHandle(hHandle);
}
} while (Process32Next(nSearch, &lppe));
}
CloseHandle(nSearch);
}
return bTerminated;
}
2015-11-13 21:18:36 +00:00
const char * AppName(void)
2015-02-02 19:25:10 +00:00
{
2015-10-25 11:10:54 +00:00
static stdstr Name;
if (Name.empty())
{
Name = stdstr_f("Project64 %s", VER_FILE_VERSION_STR);
}
return Name.c_str();
2015-02-02 19:25:10 +00:00
}
#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"
2015-11-13 21:18:36 +00:00
);
return 0;
}
#else
int WINAPI WinMain(HINSTANCE /*hInstance*/, HINSTANCE /*hPrevInstance*/, LPSTR /*lpszArgs*/, int /*nWinMode*/)
{
2015-11-13 21:18:36 +00:00
FixDirectories();
2015-11-13 21:18:36 +00:00
char *lbuffer = new char[10];
if (GetLocaleInfoA(LOCALE_SYSTEM_DEFAULT, LOCALE_SABBREVLANGNAME, lbuffer, 10))
setlocale(LC_ALL, lbuffer);
delete[] lbuffer;
2015-10-03 04:35:27 +00:00
2015-11-13 21:18:36 +00:00
CoInitialize(NULL);
2015-10-25 11:10:54 +00:00
try
{
2015-11-13 21:18:36 +00:00
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL);
g_Settings = new CSettings;
g_Settings->Initialize(AppName());
2015-11-13 21:18:36 +00:00
if (g_Settings->LoadBool(Setting_CheckEmuRunning) &&
TerminatedExistingEmu())
{
delete g_Settings;
g_Settings = new CSettings;
g_Settings->Initialize(AppName());
}
2015-11-13 21:18:36 +00:00
InitializeLog();
2012-12-18 10:43:29 +00:00
2015-11-13 21:18:36 +00:00
WriteTrace(TraceDebug, __FUNCTION__ ": Application Starting");
CMipsMemoryVM::ReserveMemory();
2015-11-13 21:18:36 +00:00
g_Notify = &Notify();
2015-11-13 21:18:36 +00:00
//Create the plugin container
WriteTrace(TraceDebug, __FUNCTION__ ": Create Plugins");
g_Plugins = new CPlugins(g_Settings->LoadStringVal(Directory_Plugin));
2015-11-13 21:18:36 +00:00
//Select the language
g_Lang = new CLanguage();
if (!g_Lang->LoadCurrentStrings())
{
CLanguageSelector().Select();
}
2015-10-25 11:10:54 +00:00
//Create the main window with Menu
2015-11-13 21:18:36 +00:00
WriteTrace(TraceDebug, __FUNCTION__ ": Create Main Window");
stdstr WinTitle(AppName());
2015-11-13 21:18:36 +00:00
WinTitle.Format("Project64 %s", VER_FILE_VERSION_STR);
2015-11-13 21:18:36 +00:00
CMainGui MainWindow(true, WinTitle.c_str()), HiddenWindow(false);
2015-10-25 11:10:54 +00:00
CMainMenu MainMenu(&MainWindow);
2015-11-13 21:18:36 +00:00
g_Plugins->SetRenderWindows(&MainWindow, &HiddenWindow);
Notify().SetMainWindow(&MainWindow);
2015-10-25 11:10:54 +00:00
if (__argc > 1)
{
2015-11-13 21:18:36 +00:00
WriteTraceF(TraceDebug, __FUNCTION__ ": Cmd line found \"%s\"", __argv[1]);
2015-10-25 11:10:54 +00:00
MainWindow.Show(true); //Show the main window
CN64System::RunFileImage(__argv[1]);
}
else
{
if (g_Settings->LoadDword(RomBrowser_Enabled))
{
2015-11-13 21:18:36 +00:00
WriteTrace(TraceDebug, __FUNCTION__ ": Show Rom Browser");
2015-10-25 11:10:54 +00:00
//Display the rom browser
MainWindow.ShowRomList();
MainWindow.Show(true); //Show the main window
MainWindow.HighLightLastRom();
2015-11-13 21:18:36 +00:00
}
else {
WriteTrace(TraceDebug, __FUNCTION__ ": Show Main Window");
2015-10-25 11:10:54 +00:00
MainWindow.Show(true); //Show the main window
}
}
//Process Messages till program is closed
2015-11-13 21:18:36 +00:00
WriteTrace(TraceDebug, __FUNCTION__ ": Entering Message Loop");
2015-10-25 11:10:54 +00:00
MainWindow.ProcessAllMessages();
2015-11-13 21:18:36 +00:00
WriteTrace(TraceDebug, __FUNCTION__ ": Message Loop Finished");
2015-10-25 11:10:54 +00:00
if (g_BaseSystem)
{
g_BaseSystem->CloseCpu();
delete g_BaseSystem;
g_BaseSystem = NULL;
}
2015-11-13 21:18:36 +00:00
WriteTrace(TraceDebug, __FUNCTION__ ": System Closed");
2015-10-25 11:10:54 +00:00
2015-11-13 21:18:36 +00:00
g_Settings->UnregisterChangeCB(Debugger_AppLogLevel, LogFile, (CSettings::SettingChangedFunc)LogLevelChanged);
g_Settings->UnregisterChangeCB(Debugger_AppLogFlush, LogFile, (CSettings::SettingChangedFunc)LogFlushChanged);
2015-10-25 11:10:54 +00:00
}
2015-11-13 21:18:36 +00:00
catch (...)
2015-10-25 11:10:54 +00:00
{
2015-11-13 21:18:36 +00:00
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);
2015-10-25 11:10:54 +00:00
}
2015-11-13 21:18:36 +00:00
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();
CoUninitialize();
WriteTrace(TraceDebug, __FUNCTION__ ": Done");
CloseTrace();
2015-10-25 11:10:54 +00:00
return true;
}
2015-11-13 21:18:36 +00:00
#endif