Rev 226 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
// PluginManager.cpp : main project file.
#include "stdafx.h"
#include "Forms\MainGui.h"
#include "Forms\StartUp.h"
#include "Languages.h"
using namespace PluginManager;
using namespace Microsoft::Win32;
using namespace System::IO;
#include <spk.h>
#undef GetTempPath
#undef GetCurrentDirectory
CPackages packages;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
System::String ^tempDir = System::IO::Path::GetTempPath();
System::String ^mydoc = Environment::GetFolderPath(Environment::SpecialFolder::Personal );
System::String ^progfile = Environment::GetFolderPath(Environment::SpecialFolder::ProgramFiles );
Utils::WStringList dirList;
Utils::WStringList remDirList;
System::String ^gameArgs;
System::String ^openPackage;
CLanguages::Instance()->setLanguage(44);
bool Advanced = false, fromLauncher = false, silent = false, gamerun = false;
for ( int i = 0; i < args->Length; i++ )
{
Utils::WString arg = _WS(args[i]);
if ( gamerun ) {
if ( gameArgs && gameArgs->Length ) gameArgs += " ";
gameArgs += args[i];
}
// its a switch
else if ( arg.left(2) == L"--" )
{
Utils::WString argSwitch = arg.token(L":", 1).lower();
Utils::WString argSwitchFirst = arg.token(L" ", 1).lower();
Utils::WString rest = arg.tokens(L":", 2);
if (!rest.empty())
{
if (rest[0] == '"' && rest[rest.length() - 1] != '"')
{
rest.erase(0, 1);
while (i < args->Length)
{
i++;
Utils::WString argCheck = _WS(args[i]);
rest += L" ";
rest += argCheck;
if (argCheck.right(1) == L"\"")
break;
}
}
if (rest.right(1) == L"\"")
rest.truncate(rest.length() - 1);
}
if ( argSwitch == L"--dir" && !rest.empty() )
{
Utils::WString gameName = packages.getGameName(rest);
if ( !gameName.empty() )
dirList.pushBack(rest, gameName);
}
if ( argSwitch == L"--advanced" )
Advanced = true;
else if ( argSwitch == L"--fromlauncher" )
fromLauncher = true;
else if ( argSwitch == L"--silent" )
silent = true;
else if ( argSwitch == L"--forceemp" )
packages.SetForceEMP(true);
else if (argSwitch == L"--gamerun" && !rest.empty())
{
gameArgs = _US(rest);
break;
}
else if ( argSwitch == L"--gamerun" ) {
gamerun = true;
}
else if ( argSwitchFirst == L"--gamerun" ) {
gamerun = true;
gameArgs = _US(arg.tokens(L" ", 2));
}
else if ( argSwitch == L"--noresume" )
{
RegistryKey ^searchKey = Registry::CurrentUser->CreateSubKey("Software\\Egosoft\\PluginManagerSettings");
if ( searchKey )
searchKey->SetValue("DontAutoResume", 1);
}
else if ( argSwitch == L"--resume" )
{
int zero = 0;
RegistryKey ^searchKey = Registry::CurrentUser->CreateSubKey("Software\\Egosoft\\PluginManagerSettings");
if ( searchKey )
searchKey->SetValue("DontAutoResume", zero);
}
else if ( argSwitch == L"--debug" ) {
CFileLog *log = dynamic_cast<CFileLog *>(CFileLog::create());
log->setFile(_WS(mydoc) + L"/Egosoft/pluginmanager.log");
log->setFilter(CLog::Log_Install | CLog::Log_Uninstall | CLog::Log_IO | CLog::Log_Directory | CLog::Log_File);
log->setLevel(rest.toInt());
}
}
// otherwise it must be a file to open
else
openPackage = _US(arg);
}
#if _DEBUG
CFileLog *log = dynamic_cast<CFileLog *>(CFileLog::create());
log->setFile(_WS(mydoc) + L"/Egosoft/pluginmanager.log");
log->setFilter(CLog::Log_Install | CLog::Log_Uninstall | CLog::Log_IO | CLog::Log_Directory | CLog::Log_File);
log->setLevel(5);
Advanced = true;
#endif
//TODO: Load languge xml
//Language.LoadLanguages();
// get the last language used
RegistryKey ^searchKeyLang = Registry::CurrentUser->OpenSubKey("Software\\Egosoft\\PluginManagerSettings");
if ( searchKeyLang )
{
int lang = System::Convert::ToInt32(searchKeyLang->GetValue("Language"));
if ( lang )
CLanguages::Instance()->setLanguage(lang);
}
bool firstInstance = false;
System::Threading::Mutex ^mut = gcnew System::Threading::Mutex(false, "Local\\XPluginManagerLite", firstInstance);
if ( !firstInstance )
{
if ( openPackage )
{
if ( File::Exists(mydoc + "\\Egosoft\\pluginmanager_load.dat") )
{
StreamWriter ^sw = File::AppendText(mydoc + "\\Egosoft\\pluginmanager_load.dat");
try
{
sw->WriteLine("File: " + openPackage);
}
finally
{
if ( sw )
delete (IDisposable ^)sw;
}
}
else
{
StreamWriter ^sw = File::CreateText(mydoc + "\\Egosoft\\pluginmanager_load.dat");
try
{
sw->WriteLine("File: " + openPackage);
}
finally
{
if ( sw )
delete (IDisposable ^)sw;
}
}
}
else if ( !silent )
MessageBox::Show(_US(CLanguages::Instance()->findText(LS_STARTUP, LANGSTARTUP_ANOTHERINSTANCE)), _US(CLanguages::Instance()->findText(LS_STARTUP, LANGSTARTUP_ANOTHERINSTANCE_TITLE)), MessageBoxButtons::OK, MessageBoxIcon::Stop);
return 0;
}
RegistryKey ^searchKey = Registry::CurrentUser->CreateSubKey("Software\\Egosoft\\PluginManagerSettings");
if ( searchKey )
{
searchKey->SetValue("Run", System::Windows::Forms::Application::ExecutablePath);
searchKey->SetValue("Version", (float)PMLVERSION);
searchKey->SetValue("Beta", (int)PMLBETA);
}
if ( File::Exists(mydoc + "\\Egosoft\\pluginmanager_load.dat") )
File::Delete(mydoc + "\\Egosoft\\pluginmanager_load.dat");
if ( File::Exists(tempDir + "\\AutoUpdater_old.exe") )
File::Delete(tempDir + "\\AutoUpdater_old.exe");
packages.startup(L".", _WS(tempDir), _WS(mydoc), L"");
packages.SetRenameText(true);
// Create the main window and run it
MainGui ^gui = gcnew MainGui(&packages, &dirList, &remDirList, Advanced);
gui->AddGameArgs(gameArgs);
// load in the config file
CFileIO configFile(_WS(mydoc) + L"/Egosoft/pluginmanager.dat");
bool anyDirs = false;
if ( configFile.exists() )
{
std::vector<Utils::WString> readFile;
if(configFile.readLinesUTF(readFile))
{
for (size_t i = 0; i < readFile.size(); i++ )
{
Utils::WString line(readFile.at(i));
Utils::WString cmd = line.token(L":", 1).lower();
Utils::WString rest = line.tokens(L":", 2).removeFirstSpace();
if ( cmd == L"dir" )
{
// first check its on the list
if ( !dirList.contains(rest) )
{
Utils::WString gameName = packages.getGameName(rest);
if ( !gameName.empty() )
{
int lang = packages.getGameLanguage(rest);
if ( lang )
dirList.pushBack(rest, Utils::WString::Number(lang) + L"|" + gameName);
else
dirList.pushBack(rest, gameName);
anyDirs = true;
}
}
}
else if ( cmd == L"remdir" )
{
// first check its on the list
if ( !remDirList.contains(rest) )
{
remDirList.pushBack(rest);
anyDirs = true;
}
}
else if ( cmd == L"experimental" )
gui->SetExperimental(true);
else if ( cmd == L"filterships" )
gui->SetShips(true);
else if ( cmd == L"filtersigned" )
gui->SetOnlySigned(true);
else if ( cmd == L"modselectordetails" )
gui->SetModSelectorDetails(true);
else if ( cmd == L"cheat" )
gui->SetCheat(true);
else if ( cmd == L"downloadable" )
gui->SetDownloadable(true);
else if ( cmd == L"noautoupdate" )
gui->SetAutoUpdate(false);
else if ( cmd == L"managersize" )
gui->SetSize(rest.token(L" ", 2).toLong(), rest.token(L" ", 1).toLong());
else if ( cmd == L"managerpos" )
gui->Location = System::Drawing::Point(rest.token(L" ", 1).toLong(), rest.token(L" ", 2).toLong());
else if ( cmd == L"managermax" )
gui->WindowState = FormWindowState::Maximized;
else if ( cmd == L"tips" )
gui->SetTips(rest.token(L" ", 1).toInt(), rest.token(L" ", 2).toInt());
else if ( cmd == L"ignorejoy" )
gui->SetIgnoreJoy(rest.toInt());
else if ( cmd == L"savegamemanager" )
gui->SetSaveGameManager(rest.toInt());
else if ( cmd == L"restoremodified" ) {
CPackages p;
p.startup(L".", _WS(tempDir), _WS(mydoc), L"");
if ( p.read(rest) ) {
p.SetVanilla(false);
p.PrepareEnableFromVanilla();
p.enablePreparedPackages(0, 0, 0);
p.closeDir();
}
}
}
}
}
// still no directories, quit
if (dirList.empty() && openPackage)
{
MessageBox::Show("You dont currently have any controlled directories", "Open Package", MessageBoxButtons::OK, MessageBoxIcon::Error);
return 1;
}
Utils::WString dir = dirList.empty() ? L"" : dirList.first()->str;
StartUp ^startup = gcnew StartUp(&packages, &dir, (openPackage) ? true : false, Advanced);
Application::Run(startup);
// opening a package from command line
if ( openPackage )
{
if ( gui->InstallPackage(openPackage, true, false, false) )
{
if ( !packages.getModKey().empty() )
PluginManager::WriteRegistryValue(packages.getModKey(), packages.selectedModName());
packages.closeDir(0, 0, true);
return 0;
}
return 1;
}
gui->UpdateDirList(L"");
gui->UpdateControls();
gui->UpdatePackages();
gui->StartCheckTimer();
gui->FindPackages();
System::String ^mod = PluginManager::ReadRegistryValue(packages.getModKey());
packages.setMod(_WS(mod));
gui->CheckProtectedDir();
Application::Run(gui);
// close directory
if ( packages.IsLoaded() )
{
if ( !packages.getModKey().empty() )
PluginManager::WriteRegistryValue(packages.getModKey(), packages.selectedModName());
if ( packages.AnyUnusedShared() )
{
if ( MessageBox::Show("You have some unused shared files, would you like to remove these?", "Remove Shared Files", MessageBoxButtons::YesNo, MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::Yes)
packages.removeUnusedSharedFiles();
}
packages.closeDir(0, 0, true);
}
Utils::WStringList restoreModified;
if ( gui->GetDownloadFile() )
{
if ( System::IO::File::Exists(".\\AutoUpdater.exe") )
{
// rename the autoupdater
System::String ^dir = System::IO::FileInfo(System::Windows::Forms::Application::ExecutablePath).DirectoryName;
if ( System::IO::File::Exists(tempDir + "\\AutoUpdater_old.exe") )
System::IO::File::Delete(tempDir + "\\AutoUpdater_old.exe");
System::IO::File::Copy(dir + "\\AutoUpdater.exe", tempDir + "\\AutoUpdater_old.exe");
System::Diagnostics::ProcessStartInfo ^info = nullptr;
if ( System::IO::File::Exists(tempDir + "\\AutoUpdater_old.exe") )
info = gcnew System::Diagnostics::ProcessStartInfo(tempDir + "\\AutoUpdater_old.exe");
else
{
MessageBox::Show("Error trying to update the Plugin Manager\nUnable to load AutoUpdater", "AutoUpdate", MessageBoxButtons::OK, MessageBoxIcon::Warning);
System::Windows::Forms::Application::Restart();
return 0;
}
if ( info )
{
info->WorkingDirectory = dir;
info->Arguments = "\"" + gui->GetDownloadFile() + "\" \"" + System::Windows::Forms::Application::ExecutablePath + "\"";
info->UseShellExecute = false;
info->WindowStyle = System::Diagnostics::ProcessWindowStyle::Normal;
System::Diagnostics::Process ^process = System::Diagnostics::Process::Start(info);
}
}
else
{
MessageBox::Show("Error trying to update the Plugin Manager\nUnable to load AutoUpdater", "AutoUpdate", MessageBoxButtons::OK, MessageBoxIcon::Warning);
System::Windows::Forms::Application::Restart();
}
}
else if ( gui->GetRunFile() )
{
System::String ^dir = System::IO::FileInfo(System::Windows::Forms::Application::ExecutablePath).DirectoryName;
// manager was run from the launcher, ie the launcher is still running, send a message to run the game instead of loading another one
if ( fromLauncher )
{
StreamWriter ^sw = File::CreateText(mydoc + "\\Egosoft\\launchgame.dat");
try
{
sw->WriteLine("File:" + gui->GetRunFile());
if ( gui->GetGameArgs() && gui->GetGameArgs()->Length )
sw->WriteLine("Args:" + gui->GetGameArgs());
}
finally
{
if ( sw )
delete (IDisposable ^)sw;
}
}
else
{
if ( System::IO::File::Exists(dir + "\\GameLauncher.exe") )
{
System::Diagnostics::ProcessStartInfo ^info = gcnew System::Diagnostics::ProcessStartInfo(dir + "\\GameLauncher.exe");
if ( info )
{
if ( packages.GetCurrentGameFlags() & EXEFLAG_ADDON ) {
CPackages p;
p.startup(L".", _WS(tempDir), _WS(mydoc), L"");
Utils::WString baseDir = CFileIO(_WS(gui->GetRunFile())).dir();
if ( p.read(baseDir) ) {
if ( !p.IsVanilla() ) {
p.SetVanilla(true);
p.PrepareDisableForVanilla();
p.enablePreparedPackages(0, 0, 0);
restoreModified.pushBack(baseDir);
}
p.closeDir();
}
}
info->WorkingDirectory = dir;
info->Arguments = "";
if ( Advanced ) info->Arguments = "--advanced ";
if ( gui->GetGameArgs() && gui->GetGameArgs()->Length )
info->Arguments += "\"" + gui->GetRunFile() + "\" \"" + System::Windows::Forms::Application::ExecutablePath + "\" \"" + gui->GetGameArgs() + "\"";
else
info->Arguments += "\"" + gui->GetRunFile() + "\" \"" + System::Windows::Forms::Application::ExecutablePath + "\"";
info->UseShellExecute = false;
info->WindowStyle = System::Diagnostics::ProcessWindowStyle::Normal;
System::Diagnostics::Process ^process = System::Diagnostics::Process::Start(info);
}
}
}
}
// write config
std::vector<Utils::WString> writeLines;
for(auto itr = dirList.begin(); itr != dirList.end(); itr++)
{
writeLines.push_back(L"Dir:" + (*itr)->str);
Utils::WString data;
if ((*itr)->data.isin(L"|") )
{
data = CPackages::ConvertLanguage((*itr)->data.token(L"|", 1).toInt()) + L"|";
data += (*itr)->data.tokens(L"|", 2);
}
else
data = (*itr)->data;
Utils::WString exe = CFileIO(packages.GetGameExe()->gameRunExe((*itr)->str)).fullFilename();
writeLines.push_back(L"DirExe:" + data + L"|" + exe);
// do the log files
writeLines.push_back(L"GameLog:" + packages.logDirectory(exe) + L"|" + exe);
}
for (auto itr = remDirList.begin(); itr != remDirList.end(); itr++)
writeLines.push_back(L"RemDir:" + (*itr)->str);
if ( gui->IsExperimental() )
writeLines.push_back(L"Experimental:");
if ( gui->IsShips() )
writeLines.push_back(L"FilterShips:");
if ( gui->IsOnlySigned() )
writeLines.push_back(L"FilterSigned:");
if ( gui->IsCheat() )
writeLines.push_back(L"Cheat:");
writeLines.push_back(L"SaveGameManager:" + Utils::WString::Number(gui->GetSaveGameManager()));
if ( gui->IsDownloadable() )
writeLines.push_back(L"Downloadable:");
if ( gui->IsModSelectorDetailsShowing() )
writeLines.push_back(L"ModSelectorDetails:");
if ( !gui->IsAutoUpdate() )
writeLines.push_back(L"NoAutoUpdate:");
if ( gui->WindowState == FormWindowState::Normal )
{
writeLines.push_back(L"ManagerSize:" + Utils::WString::Number(gui->Size.Height) + L" " + Utils::WString::Number(gui->Size.Width));
writeLines.push_back(L"ManagerPos:" + Utils::WString::Number(gui->Location.X) + L" " + Utils::WString::Number(gui->Location.Y));
}
else
{
writeLines.push_back(L"ManagerPos:" + Utils::WString::Number(gui->RestoreBounds.Location.X) + L" " + Utils::WString::Number(gui->RestoreBounds.Location.Y));
writeLines.push_back(L"ManagerSize:" + Utils::WString::Number(gui->RestoreBounds.Size.Height) + L" " + Utils::WString::Number(gui->RestoreBounds.Size.Width));
}
if ( gui->WindowState == FormWindowState::Maximized )
writeLines.push_back(L"ManagerMax:");
if ( gui->GetIgnoreJoy() )
writeLines.push_back(L"IgnoreJoy:" + Utils::WString::Number(gui->GetIgnoreJoy()));
for ( int i = 0; i < gui->GetMaxTips(); i++ )
{
int tips = gui->GetTips(i);
if ( tips == -1 )
continue;
writeLines.push_back(L"Tips: " + Utils::WString::Number(i) + L" " + Utils::WString::Number(tips));
}
for (auto rm = restoreModified.begin(); rm != restoreModified.end(); rm++) {
writeLines.push_back(L"RestoreModified: " + (*rm)->str);
}
// write settings
configFile.writeFileUTF(&writeLines);
return 0;
}