Rev 158 | Rev 183 | 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::CStringList dirList;
Utils::CStringList 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++ )
{
CyString arg = CyStringFromSystemString(args[i]);
if ( gamerun ) {
if ( gameArgs && gameArgs->Length ) gameArgs += " ";
gameArgs += args[i];
}
// its a switch
else if ( arg.Left(2) == "--" )
{
CyString argSwitch = arg.GetToken(":", 1, 1).ToLower();
CyString argSwitchFirst = arg.GetToken(" ", 1, 1).ToLower();
CyString rest = arg.GetToken(":", 2);
if ( rest[0] == '"' && rest[rest.Length() - 1] != '"' )
{
rest.Erase(0, 1);
while ( i < args->Length )
{
i++;
CyString argCheck = CyStringFromSystemString(args[i]);
rest += " ";
rest += argCheck;
if ( argCheck.Right(1) == "\"" )
break;
}
}
if ( rest.Right(1) == "\"" )
rest.Truncate(rest.Length() - 1);
if ( argSwitch == "--dir" && !rest.Empty() )
{
Utils::String gameName = packages.getGameName(rest.ToString());
if ( !gameName.empty() )
dirList.pushBack(rest.ToString(), gameName);
}
if ( argSwitch == "--advanced" )
Advanced = true;
else if ( argSwitch == "--fromlauncher" )
fromLauncher = true;
else if ( argSwitch == "--silent" )
silent = true;
else if ( argSwitch == "--forceemp" )
packages.SetForceEMP(true);
else if ( argSwitch == "--gamerun" && !rest.Empty() )
{
gameArgs = SystemStringFromCyString(rest);
break;
}
else if ( argSwitch == "--gamerun" ) {
gamerun = true;
}
else if ( argSwitchFirst == "--gamerun" ) {
gamerun = true;
gameArgs = SystemStringFromCyString(arg.GetToken(" ", 2));
}
else if ( argSwitch == "--noresume" )
{
RegistryKey ^searchKey = Registry::CurrentUser->CreateSubKey("Software\\Egosoft\\PluginManagerSettings");
if ( searchKey )
searchKey->SetValue("DontAutoResume", 1);
}
else if ( argSwitch == "--resume" )
{
int zero = 0;
RegistryKey ^searchKey = Registry::CurrentUser->CreateSubKey("Software\\Egosoft\\PluginManagerSettings");
if ( searchKey )
searchKey->SetValue("DontAutoResume", zero);
}
else if ( argSwitch == "--debug" ) {
CFileLog *log = dynamic_cast<CFileLog *>(CFileLog::create());
log->setFile(_S(mydoc) + "/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 = SystemStringFromCyString(arg);
}
#if _DEBUG
CFileLog *log = dynamic_cast<CFileLog *>(CFileLog::create());
log->setFile(_S(mydoc) + "/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(".", _S(tempDir), _S(mydoc), "");
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(_S(mydoc) + "/Egosoft/pluginmanager.dat");
bool anyDirs = false;
if ( configFile.exists() )
{
std::vector<Utils::String> *readFile = configFile.readLines();
if ( readFile )
{
for ( int i = 0; i < (int)readFile->size(); i++ )
{
Utils::String line(readFile->at(i));
Utils::String cmd = line.token(":", 1).lower();
Utils::String rest = line.tokens(":", 2).removeFirstSpace();
if ( cmd == "dir" )
{
// first check its on the list
if ( !dirList.findString(rest) )
{
Utils::String gameName = packages.GetGameName(rest).ToString();
if ( !gameName.empty() )
{
int lang = packages.GetGameLanguage(rest);
if ( lang )
dirList.pushBack(rest, Utils::String::Number(lang) + "|" + gameName);
else
dirList.pushBack(rest, gameName);
anyDirs = true;
}
}
}
else if ( cmd == "remdir" )
{
// first check its on the list
if ( !remDirList.findString(rest) )
{
remDirList.pushBack(rest);
anyDirs = true;
}
}
else if ( cmd == "experimental" )
gui->SetExperimental(true);
else if ( cmd == "filterships" )
gui->SetShips(true);
else if ( cmd == "filtersigned" )
gui->SetOnlySigned(true);
else if ( cmd == "modselectordetails" )
gui->SetModSelectorDetails(true);
else if ( cmd == "cheat" )
gui->SetCheat(true);
else if ( cmd == "downloadable" )
gui->SetDownloadable(true);
else if ( cmd == "noautoupdate" )
gui->SetAutoUpdate(false);
else if ( cmd == "managersize" )
gui->SetSize(rest.token(" ", 2).toLong(), rest.token(" ", 1).toLong());
else if ( cmd == "managerpos" )
gui->Location = System::Drawing::Point(rest.token(" ", 1).toLong(), rest.token(" ", 2).toLong());
else if ( cmd == "managermax" )
gui->WindowState = FormWindowState::Maximized;
else if ( cmd == "tips" )
gui->SetTips(rest.token(" ", 1).toInt(), rest.token(" ", 2).toInt());
else if ( cmd == "ignorejoy" )
gui->SetIgnoreJoy(rest.toInt());
else if ( cmd == "savegamemanager" )
gui->SetSaveGameManager(rest.toInt());
else if ( cmd == "restoremodified" ) {
CPackages p;
p.startup(".", _S(tempDir), _S(mydoc), "");
if ( p.Read(rest) ) {
p.SetVanilla(false);
p.PrepareEnableFromVanilla();
p.EnablePreparedPackages(0, 0, 0);
p.CloseDir();
}
}
}
delete readFile;
}
}
// 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::String dir = dirList.empty() ? "" : 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("");
gui->UpdateControls();
gui->UpdatePackages();
gui->StartCheckTimer();
gui->FindPackages();
System::String ^mod = PluginManager::ReadRegistryValue(packages.GetModKey());
packages.setMod(_S(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::CStringList 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(".", _S(tempDir), _S(mydoc), "");
Utils::String baseDir = CFileIO(_S(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::String> writeLines;
for(auto itr = dirList.begin(); itr != dirList.end(); itr++)
{
writeLines.push_back("Dir:" + (*itr)->str);
Utils::String data;
if ((*itr)->data.isin("|") )
{
data = CPackages::ConvertLanguage((*itr)->data.token("|", 1).toInt()) + "|";
data += (*itr)->data.tokens("|", 2);
}
else
data = (*itr)->data;
Utils::String exe = CFileIO(packages.GetGameExe()->GetGameRunExe((*itr)->str)).fullFilename();
writeLines.push_back("DirExe:" + data + "|" + exe);
// do the log files
writeLines.push_back("GameLog:" + packages.logDirectory(exe) + "|" + exe);
}
for (auto itr = remDirList.begin(); itr != remDirList.end(); itr++)
writeLines.push_back("RemDir:" + (*itr)->str);
if ( gui->IsExperimental() )
writeLines.push_back("Experimental:");
if ( gui->IsShips() )
writeLines.push_back("FilterShips:");
if ( gui->IsOnlySigned() )
writeLines.push_back("FilterSigned:");
if ( gui->IsCheat() )
writeLines.push_back("Cheat:");
writeLines.push_back(Utils::String("SaveGameManager:") + (long)gui->GetSaveGameManager());
if ( gui->IsDownloadable() )
writeLines.push_back("Downloadable:");
if ( gui->IsModSelectorDetailsShowing() )
writeLines.push_back("ModSelectorDetails:");
if ( !gui->IsAutoUpdate() )
writeLines.push_back("NoAutoUpdate:");
if ( gui->WindowState == FormWindowState::Normal )
{
writeLines.push_back(Utils::String("ManagerSize:") + (long)gui->Size.Height + " " + (long)gui->Size.Width);
writeLines.push_back(Utils::String("ManagerPos:") + (long)gui->Location.X + " " + (long)gui->Location.Y);
}
else
{
writeLines.push_back(Utils::String("ManagerPos:") + (long)gui->RestoreBounds.Location.X + " " + (long)gui->RestoreBounds.Location.Y);
writeLines.push_back(Utils::String("ManagerSize:") + (long)gui->RestoreBounds.Size.Height + " " + (long)gui->RestoreBounds.Size.Width);
}
if ( gui->WindowState == FormWindowState::Maximized )
writeLines.push_back("ManagerMax:");
if ( gui->GetIgnoreJoy() )
writeLines.push_back(Utils::String("IgnoreJoy:") + gui->GetIgnoreJoy());
for ( int i = 0; i < gui->GetMaxTips(); i++ )
{
int tips = gui->GetTips(i);
if ( tips == -1 )
continue;
writeLines.push_back(Utils::String("Tips: ") + (long)i + " " + (long)tips);
}
for (auto rm = restoreModified.begin(); rm != restoreModified.end(); rm++) {
writeLines.push_back("RestoreModified: " + (*rm)->str);
}
// write settings
configFile.writeFile(&writeLines);
return 0;
}