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

192 lines
4.7 KiB
C++
Raw Normal View History

2003-07-22 02:40:23 +00:00
#include "stdafx.h"
#include "SMPackageUtil.h"
#include "Registry.h"
2005-11-28 05:24:09 +00:00
#include "../ProductInfo.h"
2003-07-22 02:40:23 +00:00
void WriteStepManiaInstallDirs( const CStringArray& asInstallDirsToWrite )
{
CRegistry Reg;
Reg.SetRootKey(HKEY_LOCAL_MACHINE);
Reg.SetKey("Software\\StepMania\\smpackage\\Installations", TRUE); // create if not already present
unsigned i;
2003-07-22 02:40:23 +00:00
for( i=0; i<100; i++ )
{
CString sName = ssprintf("%d",i);
// Reg.DeleteKey( sName ); // delete key is broken in this library, so just write over it with ""
Reg.WriteString( sName, "" );
}
for( i=0; i<asInstallDirsToWrite.size(); i++ )
2003-07-22 02:40:23 +00:00
{
CString sName = ssprintf("%d",i);
Reg.WriteString( sName, asInstallDirsToWrite[i] );
}
}
void GetStepManiaInstallDirs( CStringArray& asInstallDirsOut )
{
asInstallDirsOut.clear();
2003-07-22 02:40:23 +00:00
CRegistry Reg;
Reg.SetRootKey(HKEY_LOCAL_MACHINE);
Reg.SetKey("Software\\StepMania\\smpackage\\Installations", TRUE); // create if not already present
for( int i=0; i<100; i++ )
{
CString sName = ssprintf("%d",i);
CString sPath = Reg.ReadString( sName, "" );
if( sPath == "" ) // read failed
continue; // skip
CString 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 );
}
void AddStepManiaInstallDir( CString sNewInstallDir )
{
CStringArray asInstallDirs;
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 );
}
void SetDefaultInstallDir( int iInstallDirIndex )
{
// move the specified index to the top of the list
CStringArray asInstallDirs;
GetStepManiaInstallDirs( asInstallDirs );
2005-11-11 22:00:26 +00:00
ASSERT( iInstallDirIndex >= 0 && iInstallDirIndex < asInstallDirs.size() );
CString sDefaultInstallDir = asInstallDirs[iInstallDirIndex];
asInstallDirs.erase( asInstallDirs.begin()+iInstallDirIndex );
asInstallDirs.insert( asInstallDirs.begin(), sDefaultInstallDir );
WriteStepManiaInstallDirs( asInstallDirs );
}
2003-07-22 02:40:23 +00:00
void SetDefaultInstallDir( CString sInstallDir )
{
CStringArray asInstallDirs;
GetStepManiaInstallDirs( asInstallDirs );
bool bAlreadyInList = false;
for( unsigned i=0; i<asInstallDirs.size(); i++ )
{
if( asInstallDirs[i].CompareNoCase(sInstallDir) == 0 )
{
SetDefaultInstallDir( i );
break;
}
}
}
2003-07-22 03:07:51 +00:00
bool GetPref( CString name, bool &val )
{
CRegistry Reg;
Reg.SetRootKey(HKEY_LOCAL_MACHINE);
Reg.SetKey("Software\\StepMania\\smpackage", FALSE); // don't create if not already present
return Reg.Read( name, val );
}
bool SetPref( CString name, bool val )
{
CRegistry Reg;
Reg.SetRootKey(HKEY_LOCAL_MACHINE);
Reg.SetKey("Software\\StepMania\\smpackage", TRUE); // don't create if not already present
Reg.WriteBool( name, val );
return false;
}
/* 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. */
CString GetPackageDirectory(CString path)
{
if( path.Find("CVS") != -1 )
return ""; // skip
CStringArray 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());
CString ret = join( "\\", Parts );
if( !IsADirectory(ret) )
return "";
return ret;
}
bool IsValidPackageDirectory(CString path)
{
/* Make sure the path contains only second-level directories, and doesn't
* contain any ".", "..", "...", etc. dirs. */
CStringArray 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-11-28 05:24:09 +00:00
void LaunchGame()
{
PROCESS_INFORMATION pi;
STARTUPINFO si;
ZeroMemory( &si, sizeof(si) );
CreateProcess(
NULL, // pointer to name of executable module
PRODUCT_NAME ".exe", // pointer to command line string
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
);
}