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

278 lines
7.9 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"
2005-12-29 02:40:09 +00:00
#include "RageFileManager.h"
2006-01-05 07:38:31 +00:00
#include "ThemeManager.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)
{
2005-12-21 09:00:54 +00:00
if( path.find("CVS") != string::npos )
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
}
2005-12-23 00:59:00 +00:00
2005-12-28 19:21:08 +00:00
RString SMPackageUtil::GetLanguageDisplayString( const RString &sIsoCode )
{
const LanguageInfo *li = GetLanguageInfo( sIsoCode );
return ssprintf( "%s (%s)", li ? li->szIsoCode:sIsoCode.c_str(), li ? li->szNativeName:"???" );
}
RString SMPackageUtil::GetLanguageCodeFromDisplayString( const RString &sDisplayString )
{
RString s = sDisplayString;
// strip the space and everything after
size_t iSpace = s.find(' ');
ASSERT( iSpace != s.npos );
s.erase( s.begin()+iSpace, s.end() );
return s;
}
2006-01-05 07:38:31 +00:00
void SMPackageUtil::LocalizeDialogAndContents( CDialog &dlg )
{
CString s1;
dlg.GetWindowText( s1 );
RString sGroup = "Tools-"+s1;
dlg.SetWindowText( THEME->GetString(sGroup,"DialogCaption") );
for( CWnd *pChild = dlg.GetTopWindow(); pChild != NULL; pChild = pChild->GetNextWindow() )
{
pChild->GetWindowText( s1 );
if( s1.IsEmpty() )
continue;
pChild->SetWindowText( THEME->GetString(sGroup,RString(s1)) );
}
}
2005-12-29 02:40:09 +00:00
static const RString TEMP_MOUNT_POINT = "/@package/";
RageFileOsAbsolute::~RageFileOsAbsolute()
{
if( !m_sOsDir.empty() )
FILEMAN->Unmount( "dir", m_sOsDir, TEMP_MOUNT_POINT );
}
bool RageFileOsAbsolute::Open( const RString& path, int mode )
{
if( !m_sOsDir.empty() )
FILEMAN->Unmount( "dir", m_sOsDir, TEMP_MOUNT_POINT );
m_sOsDir = path;
size_t iStart = m_sOsDir.find_last_of( "/\\" );
ASSERT( iStart != m_sOsDir.npos );
m_sOsDir.erase( m_sOsDir.begin()+iStart, m_sOsDir.end() );
FILEMAN->Mount( "dir", m_sOsDir, TEMP_MOUNT_POINT );
RString sFileName = path.Right( path.size()-m_sOsDir.size() );
return RageFile::Open( TEMP_MOUNT_POINT+sFileName, mode );
}
2005-12-23 00:59:00 +00:00
/*
* (c) 2002-2005 Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/