Files
itgmania212121/stepmania/src/smpackage/SMPackageUtil.cpp
T

195 lines
5.0 KiB
C++
Raw Normal View History

2005-12-10 08:23:10 +00:00
#define CO_EXIST_WITH_MFC
#include "global.h"
2003-07-22 02:40:23 +00:00
#include "stdafx.h"
#include "SMPackageUtil.h"
2005-12-10 08:23:10 +00:00
#include "archutils/Win32/RegistryAccess.h"
#include "ProductInfo.h"
#include "RageUtil.h"
2003-07-22 02:40:23 +00:00
2005-12-10 08:23:10 +00:00
void SMPackageUtil::WriteStepManiaInstallDirs( const vector<RString>& asInstallDirsToWrite )
2003-07-22 02:40:23 +00:00
{
2005-12-10 08:23:10 +00:00
RString sKey = "HKEY_LOCAL_MACHINE\\Software\\StepMania\\smpackage\\Installations";
2003-07-22 02:40:23 +00:00
unsigned i;
2003-07-22 02:40:23 +00:00
for( i=0; i<100; i++ )
{
2005-12-10 08:23:10 +00:00
RString sName = ssprintf("%d",i);
2003-07-22 02:40:23 +00:00
// Reg.DeleteKey( sName ); // delete key is broken in this library, so just write over it with ""
2005-12-10 08:23:10 +00:00
RegistryAccess::SetRegValue( sKey, sName, RString() );
2003-07-22 02:40:23 +00:00
}
for( i=0; i<asInstallDirsToWrite.size(); i++ )
2003-07-22 02:40:23 +00:00
{
2005-12-10 08:23:10 +00:00
RString sName = ssprintf("%d",i);
RegistryAccess::SetRegValue( sKey, sName, asInstallDirsToWrite[i] );
2003-07-22 02:40:23 +00:00
}
}
2005-12-10 08:23:10 +00:00
void SMPackageUtil::GetStepManiaInstallDirs( vector<RString>& asInstallDirsOut )
2003-07-22 02:40:23 +00:00
{
asInstallDirsOut.clear();
2003-07-22 02:40:23 +00:00
2005-12-10 08:23:10 +00:00
RString sKey = "HKEY_LOCAL_MACHINE\\Software\\StepMania\\smpackage\\Installations";
2003-07-22 02:40:23 +00:00
for( int i=0; i<100; i++ )
{
2005-12-10 08:23:10 +00:00
RString sName = ssprintf("%d",i);
2003-07-22 02:40:23 +00:00
2005-12-10 08:23:10 +00:00
RString sPath;
if( !RegistryAccess::GetRegValue(sKey, sName, sPath) )
continue;
2003-07-22 02:40:23 +00:00
if( sPath == "" ) // read failed
continue; // skip
2005-12-10 08:23:10 +00:00
RString sProgramDir = sPath+"\\Program";
if( !DoesFileExist(sProgramDir) )
continue; // skip
asInstallDirsOut.push_back( sPath );
2003-07-22 02:40:23 +00:00
}
// while we're at it, write to clean up stale entries
WriteStepManiaInstallDirs( asInstallDirsOut );
}
2005-12-10 08:23:10 +00:00
void SMPackageUtil::AddStepManiaInstallDir( RString sNewInstallDir )
2003-07-22 02:40:23 +00:00
{
2005-12-10 08:23:10 +00:00
vector<RString> asInstallDirs;
2003-07-22 02:40:23 +00:00
GetStepManiaInstallDirs( asInstallDirs );
bool bAlreadyInList = false;
for( unsigned i=0; i<asInstallDirs.size(); i++ )
2003-07-22 02:40:23 +00:00
{
if( asInstallDirs[i].CompareNoCase(sNewInstallDir) == 0 )
{
bAlreadyInList = true;
break;
}
}
if( !bAlreadyInList )
asInstallDirs.push_back( sNewInstallDir );
2003-07-22 02:40:23 +00:00
WriteStepManiaInstallDirs( asInstallDirs );
}
2005-12-10 08:23:10 +00:00
void SMPackageUtil::SetDefaultInstallDir( int iInstallDirIndex )
{
// move the specified index to the top of the list
2005-12-10 08:23:10 +00:00
vector<RString> asInstallDirs;
GetStepManiaInstallDirs( asInstallDirs );
2005-12-10 08:23:10 +00:00
ASSERT( iInstallDirIndex >= 0 && iInstallDirIndex < (int)asInstallDirs.size() );
RString sDefaultInstallDir = asInstallDirs[iInstallDirIndex];
asInstallDirs.erase( asInstallDirs.begin()+iInstallDirIndex );
asInstallDirs.insert( asInstallDirs.begin(), sDefaultInstallDir );
WriteStepManiaInstallDirs( asInstallDirs );
}
2003-07-22 02:40:23 +00:00
2005-12-10 08:23:10 +00:00
void SMPackageUtil::SetDefaultInstallDir( RString sInstallDir )
{
2005-12-10 08:23:10 +00:00
vector<RString> asInstallDirs;
GetStepManiaInstallDirs( asInstallDirs );
for( unsigned i=0; i<asInstallDirs.size(); i++ )
{
if( asInstallDirs[i].CompareNoCase(sInstallDir) == 0 )
{
SetDefaultInstallDir( i );
break;
}
}
}
2005-12-10 08:23:10 +00:00
bool SMPackageUtil::GetPref( RString name, bool &val )
2003-07-22 03:07:51 +00:00
{
2005-12-10 08:23:10 +00:00
return RegistryAccess::GetRegValue( "HKEY_LOCAL_MACHINE\\Software\\StepMania\\smpackage", name, val );
2003-07-22 03:07:51 +00:00
}
2005-12-10 08:23:10 +00:00
bool SMPackageUtil::SetPref( RString name, bool val )
2003-07-22 03:07:51 +00:00
{
2005-12-10 08:23:10 +00:00
return RegistryAccess::SetRegValue( "HKEY_LOCAL_MACHINE\\Software\\StepMania\\smpackage", name, val );
2003-07-22 03:07:51 +00:00
}
/* Get a package directory. For most paths, this is the first two components. For
2004-03-26 01:48:47 +00:00
* songs and note skins, this is the first three. */
2005-12-10 08:23:10 +00:00
RString SMPackageUtil::GetPackageDirectory(RString path)
{
if( path.Find("CVS") != -1 )
return ""; // skip
2005-12-10 08:23:10 +00:00
vector<RString> Parts;
split( path, "\\", Parts );
unsigned NumParts = 2;
2004-03-26 01:48:47 +00:00
if( !Parts[0].CompareNoCase("Songs") || !Parts[0].CompareNoCase("NoteSkins") )
NumParts = 3;
if( Parts.size() < NumParts )
return "";
Parts.erase(Parts.begin() + NumParts, Parts.end());
2005-12-10 08:23:10 +00:00
RString ret = join( "\\", Parts );
if( !IsADirectory(ret) )
return "";
return ret;
}
2005-12-10 08:23:10 +00:00
bool SMPackageUtil::IsValidPackageDirectory( RString path )
{
/* Make sure the path contains only second-level directories, and doesn't
* contain any ".", "..", "...", etc. dirs. */
2005-12-10 08:23:10 +00:00
vector<RString> Parts;
split( path, "\\", Parts, true );
if( Parts.size() == 0 )
return false;
/* Make sure we're not going to "uninstall" an entire Songs subfolder. */
unsigned NumParts = 2;
if( !Parts[0].CompareNoCase("songs") )
NumParts = 3;
if( Parts.size() < NumParts )
return false;
/* Make sure the path doesn't contain any ".", "..", "...", etc. dirs. */
for( unsigned i = 0; i < Parts.size(); ++i )
if( Parts[i][0] == '.' )
return false;
return true;
}
2003-07-22 03:07:51 +00:00
2005-12-18 01:27:54 +00:00
bool SMPackageUtil::LaunchGame()
2005-11-28 05:24:09 +00:00
{
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory( &si, sizeof(si) );
2005-12-10 08:23:10 +00:00
RString sFile = PRODUCT_NAME ".exe";
if( !DoesFileExist(sFile) )
{
sFile = "Program\\" + sFile;
if( !DoesFileExist(sFile) )
{
2005-12-18 01:27:54 +00:00
MessageBox( NULL, "Could not find " PRODUCT_NAME ".exe", "Error", MB_ICONEXCLAMATION );
return false;
}
}
2005-11-28 05:24:09 +00:00
CreateProcess(
sFile, // pointer to name of executable module
NULL, // pointer to command line string
2005-11-28 05:24:09 +00:00
NULL, // process security attributes
NULL, // thread security attributes
false, // handle inheritance flag
0, // creation flags
NULL, // pointer to new environment block
NULL, // pointer to current directory name
&si, // pointer to STARTUPINFO
&pi // pointer to PROCESS_INFORMATION
);
2005-12-18 01:27:54 +00:00
return true;
2005-11-28 05:24:09 +00:00
}