Files
Sun1602/Tools/ServerStarter/ScriptParser.cpp
T
2022-10-26 12:25:11 +08:00

325 lines
9.0 KiB
C++

#include ".\scriptparser.h"
#include "io.h"
#include "Psapi.h"
ScriptParser::ScriptParser(void)
{
Release();
}
ScriptParser::~ScriptParser(void)
{
Release();
}
VOID ScriptParser::Release()
{
m_nDriveType = 0;
m_nShotDownTime = 0;
m_nFileCount = 0;
m_szSourcePath[0] = '\0';
m_szDestPath[0] = '\0';
m_vecSourcePath.clear();
m_vecDestPath.clear();
m_strServerFile.clear();
}
BOOL ScriptParser::ParsingCurProcess( OUT TCHAR* pszCategory, IN int nCategorySize, OUT TCHAR* pszServerFile, int nServerSize )
{
ZeroMemory( pszCategory, nCategorySize );
ZeroMemory( pszServerFile, nServerSize );
TCHAR szFullFileName[MAX_PATH];
HANDLE hHandle = GetCurrentProcess();
GetProcessImageFileName( hHandle, szFullFileName, MAX_PATH );
TCHAR szCurFileName[MAX_PATH];
TCHAR szFileName[MAX_PATH];
TCHAR szFileExt[MAX_PATH];
//분리..
_splitpath( szFullFileName, NULL, NULL, szFileName, szFileExt );
//파일이름
_snprintf( szCurFileName, MAX_PATH, "%s%s", szFileName, szFileExt);
szCurFileName[MAX_PATH-1] = '\0';
#ifdef _DEBUG
if( strcmp( szCurFileName, "_DEBUGAgentServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "AGENT_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "AgentServer_d.exe" );
}
else if( strcmp( szCurFileName, "_DEBUGFieldServer.exe" ) == 0)
{
_snprintf( pszCategory, MAX_PATH, "FIELD_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "FieldServer_d.exe" );
}
else if( strcmp( szCurFileName, "_DEBUGBattleServer.exe" ) == 0)
{
_snprintf( pszCategory, MAX_PATH, "BATTLE_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "BattleServer_d.exe" );
}
else if( strcmp( szCurFileName, "_DEBUGDBPServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "DBPROXY_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "databaseproxy_d.exe" );
}
else if( strcmp( szCurFileName, "_DEBUGWorldServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "WORLD_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "WorldServer_d.exe" );
}
else if( strcmp( szCurFileName, "_DEBUGGuildServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "GUILD_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "GuildServer_d.exe" );
}
else if( strcmp( szCurFileName, "_DEBUGPortalServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "PORTAL_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "PortalServer_d.exe" );
}
else if( strcmp( szCurFileName, "_DEBUGMasterServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "MASTER_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "MasterServer_d.exe" );
}
else if( strcmp( szCurFileName, "_DEBUGADBPServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "ACCOUNTDBP_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "accountdatabaseproxy_d.exe" );
}
else
{
printf( "Category Not Found, Current FileName is %s\n", szCurFileName );
return FALSE;
}
#else
if( strcmp( szCurFileName, "_RELEASEAgentServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "AGENT_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "AgentServer.exe" );
}
else if( strcmp( szCurFileName, "_RELEASEFieldServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "FIELD_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "FieldServer.exe" );
}
else if( strcmp( szCurFileName, "_RELEASEBattleServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "BATTLE_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "BattleServer.exe" );
}
else if( strcmp( szCurFileName, "_RELEASEDBPServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "DBPROXY_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "databaseproxy.exe" );
}
else if( strcmp( szCurFileName, "_RELEASEWorldServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "WORLD_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "WorldServer.exe" );
}
else if( strcmp( szCurFileName, "_RELEASEGuildServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "GUILD_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "GuildServer.exe" );
}
else if( strcmp( szCurFileName, "_RELEASEPortalServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "PORTAL_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "PortalServer.exe" );
}
else if( strcmp( szCurFileName, "_RELEASEMasterServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "MASTER_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "MasterServer.exe" );
}
else if( strcmp( szCurFileName, "_RELEASEADBPServer.exe" ) == 0 )
{
_snprintf( pszCategory, MAX_PATH, "ACCOUNTDBP_SERVER" );
_snprintf( pszServerFile, MAX_PATH, "accountdatabaseproxy.exe" );
}
else
{
printf( "Category Not Found, Current FileName is %s\n", szCurFileName );
return FALSE;
}
#endif
return TRUE;
}
BOOL ScriptParser::Load( const char* pszFilePath )
{
TCHAR szDir[MAX_PATH], szFilePath[MAX_PATH];
GetCurrentDirectory( MAX_PATH, szDir );
printf( "Current Directory is %s\n", szDir );
_snprintf( szFilePath, MAX_PATH, "%s\\%s", szDir, pszFilePath );
if( !pszFilePath ) return FALSE;
if( _access( pszFilePath, 0 ) == - 1 )
{
printf( "%s File Not Exist\n", pszFilePath );
return FALSE;
}
Release();
//Common
m_nDriveType = GetPrivateProfileInt( "COMMON", "DRIVE_TYPE", 0, szFilePath );
m_nShotDownTime = GetPrivateProfileInt( "COMMON", "SHUTDOWN_TIME", 0, szFilePath );
if( !GetPrivateProfileString( "COMMON", "SERVER_ARGUMENT", "", m_szServerArgument, MAX_PATH, szFilePath ) )
{
printf( "Script Read Error, Category = COMMON, Key = SERVER_ARGUMENT\n" );
return FALSE;
}
//각각의 Server 정보
TCHAR szCategory[MAX_PATH], szServerFile[MAX_PATH];
if( !ParsingCurProcess( szCategory, MAX_PATH, szServerFile, MAX_PATH ) )
return FALSE;
if( m_nDriveType == NONE_DRIVE )
{
//서버파일 세팅
TCHAR szCurDir[MAX_PATH];
GetCurrentDirectory( MAX_PATH, szCurDir );
m_strServerFile = szCurDir;
m_strServerFile += "\\";
m_strServerFile += szServerFile;
return TRUE;
}
if( !GetPrivateProfileString( szCategory, "SOURCE", "", m_szSourcePath, MAX_PATH, szFilePath ) )
{
printf( "Script Read Error, Category = %s, Key = SOURCE \n", szCategory );
return FALSE;
}
if( !GetPrivateProfileString( szCategory, "DEST", "", m_szDestPath, MAX_PATH, szFilePath ) )
{
printf( "Script Read Error, Category = %s, Key = DEST \n", szCategory );
return FALSE;
}
m_nFileCount = GetPrivateProfileInt( szCategory, "FILE_COUNT", 0, szFilePath );
TCHAR szFileName[MAX_PATH], szKey[MAX_PATH];
std::string strSourceFile, strDestFile;
for( int i = 1; i <= m_nFileCount; i++ )
{
_snprintf( szKey, MAX_PATH, "FILE%d", i );
if( !GetPrivateProfileString( szCategory, szKey, "", szFileName, MAX_PATH, szFilePath ) )
{
printf( "Script Read Error, Category = %s, Key = %s \n", szCategory, szKey );
return FALSE;
}
strSourceFile = m_szSourcePath;
strSourceFile += "\\";
strSourceFile += szFileName;
m_vecSourcePath.push_back( strSourceFile );
strDestFile = m_szDestPath;
strDestFile += "\\";
strDestFile += szFileName;
m_vecDestPath.push_back( strDestFile );
}
//서버파일 세팅
m_strServerFile = m_szDestPath;
m_strServerFile += "\\";
m_strServerFile += szServerFile;
return TRUE;
}
BOOL ScriptParser::CopyFiles()
{
if( GetDriveType() != NETWORK_DRIVE )
return TRUE;
if( m_vecSourcePath.size() != m_vecDestPath.size() )
return FALSE;
int nSize = (int) m_vecDestPath.size();
if( nSize == 0 )
return FALSE;
for( int i = 0 ; i < nSize; i++ )
{
std::string strSource = m_vecSourcePath[i];
std::string strDest = m_vecDestPath[i];
if( !CopyFile( strSource.c_str(), strDest.c_str(), FALSE ) )
{
DWORD dwError = GetLastError();
printf( "File Copy Fail[Error Code = %u], %s To %s \n", dwError, strSource.c_str(), strDest.c_str() );
return FALSE;
}
else
{
DWORD dwAttrs = GetFileAttributes( strDest.c_str() );
if ( (dwAttrs & FILE_ATTRIBUTE_READONLY) )
SetFileAttributes( strDest.c_str(), FILE_ATTRIBUTE_NORMAL );
}
printf( "File Copy Success, %s To %s \n", strSource.c_str(), strDest.c_str() );
}
return TRUE;
}
BOOL ScriptParser::StartServer()
{
if( m_strServerFile.empty() )
{
printf( "ServerFile is EMPTY.\n");
return FALSE;
}
if( _access( m_strServerFile.c_str(), 0 ) == -1 )
{
printf( "ServerFile Not Exist. Path = %s\n", m_strServerFile.c_str() );
return FALSE;
}
if( GetDriveType() == NETWORK_DRIVE )
{
m_strServerFile += " ";
m_strServerFile += m_szServerArgument;
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);
ZeroMemory( &pi, sizeof(pi) );
// Start the child process.
if( !CreateProcess( NULL, // No module name (use command line).
(LPSTR)m_strServerFile.c_str(), // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
printf( "Server Execute Success. Path = %s\n", m_strServerFile.c_str() );
}
return TRUE;
}