Files
2022-10-26 12:25:11 +08:00

189 lines
6.2 KiB
C++

#include ".\updatemanager.h"
#include "LauncherUpdate.h"
#include "ApplicationUpdate.h"
#include "UpdateScriptInfo.h"
HWND CUpdateManager::m_hWnd = NULL;
CUpdateManager::CUpdateManager(void)
{
m_pApplicationUpdate = NULL;
m_pLauncherUpdate = NULL;
m_hThread = NULL;
}
CUpdateManager::~CUpdateManager(void)
{
}
VOID CUpdateManager::Release()
{
if( m_pLauncherUpdate ) m_pLauncherUpdate->StopUpdate();
if( m_pApplicationUpdate ) m_pApplicationUpdate->StopUpdate();
WaitForSingleObject( m_hThread, INFINITE );
if( m_hThread )
{
CloseHandle( m_hThread );
m_hThread = NULL;
}
if( m_pLauncherUpdate )
{
delete m_pLauncherUpdate;
m_pLauncherUpdate = NULL;
}
if( m_pApplicationUpdate )
{
delete m_pApplicationUpdate;
m_pApplicationUpdate = NULL;
}
}
// 여기서는 각각의 Update모듈을 생성하고, 스크립트를 파싱한다.
// 파싱한 스크립의 내용을 각각의 Update모듈에 입력한다.
// bApplyScript : 스크립트의 UpdateNumber를 적용할지, 아니면 뒤에 입력한 값을 적용할지 선택.
BOOL CUpdateManager::Init( eLAUNCHER_START_TYPE StartType, BOOL bApplyScript, BYTE byAppUpdateNumber, TCHAR* pszSubParam )
{
//1. INI파일 로드
if( !CUpdateScriptParser::GetInstance()->IsLoad() )
return FALSE;
if( m_pLauncherUpdate ) delete m_pLauncherUpdate;
m_pLauncherUpdate = new CLauncherUpdate;
if( m_pApplicationUpdate ) delete m_pApplicationUpdate;
m_pApplicationUpdate = new CApplicationUpdate;
//Init
/************************************************************************/
/* Launcher */
/************************************************************************/
//1. Launcher
if( !m_pLauncherUpdate->Init() ) return FALSE;
// 다운로드 디렉토리 설정
m_pLauncherUpdate->SetStartType( StartType );
std::string strLauncherBasicDownDir, strLauncherBasicExecuteDir;
strLauncherBasicDownDir = CUpdateScriptParser::GetInstance()->m_LauncherInfo.m_szDownPath;
strLauncherBasicDownDir += "\\Down";
m_pLauncherUpdate->SetLocalBasicDownDir( (TCHAR*)strLauncherBasicDownDir.c_str() );
m_pLauncherUpdate->SetLocalBasicExecuteDir( CUpdateScriptParser::GetInstance()->m_LauncherInfo.m_szExecutePath );
m_pLauncherUpdate->SetUpdateNumber( CUpdateScriptParser::GetInstance()->m_LauncherInfo.m_nUpdateNumber );
m_pLauncherUpdate->SetExecuteParameter( pszSubParam );
//2. 프로세스 정보 설정
int nLauncherCount = CUpdateScriptParser::GetInstance()->m_LauncherInfo.m_nTotalProcessCount;
for( int i = 0; i < nLauncherCount; i++ )
{
PROCESS_INFO* pProcessInfo = CUpdateScriptParser::GetInstance()->m_LauncherInfo.GetProcessInfo( i + 1 );
if( !pProcessInfo ) return FALSE;
m_pLauncherUpdate->InsertProcessInfo( pProcessInfo );
}
/************************************************************************/
/* Application */
/************************************************************************/
if( !m_pApplicationUpdate->Init() ) return FALSE;
// 다운로드 디렉토리 설정
m_pApplicationUpdate->SetStartType( StartType );
m_pApplicationUpdate->SetLocalBasicDownDir( CUpdateScriptParser::GetInstance()->m_ApplicationInfo.m_szDownPath );
m_pApplicationUpdate->SetLocalBasicExecuteDir( CUpdateScriptParser::GetInstance()->m_ApplicationInfo.m_szExecutePath );
BYTE byUpdateNumber = 0;
if( bApplyScript ) byUpdateNumber = CUpdateScriptParser::GetInstance()->m_ApplicationInfo.m_nUpdateNumber;
else byUpdateNumber = byAppUpdateNumber;
m_pApplicationUpdate->SetUpdateNumber( byUpdateNumber );
//2. 프로세스 정보 설정
int nApplicationCount = CUpdateScriptParser::GetInstance()->m_ApplicationInfo.m_nTotalProcessCount;
for( int i = 0; i < nApplicationCount; i++ )
{
PROCESS_INFO* pProcessInfo = CUpdateScriptParser::GetInstance()->m_ApplicationInfo.GetProcessInfo( i + 1 );
if( !pProcessInfo ) return FALSE;
m_pApplicationUpdate->InsertProcessInfo( pProcessInfo );
}
return TRUE;
}
//이함수에서 쓰레드가 생성되며, 이 쓰레드 안에서 패치가 이루어 진다.
VOID CUpdateManager::StartUpdate()
{
//Update Thread 가동
m_unThreadID = 2;
m_hThread = (HANDLE)_beginthreadex (NULL, NULL, UpdateThread, (void*)this, 0, &m_unThreadID);
if( m_hThread == INVALID_HANDLE_VALUE )
{
MessageBox( NULL, "업데이트 쓰레드 생성 실패", NULL, MB_OK );
return;
}
return;
}
BOOL CUpdateManager::ExecuteUpdate()
{
//0. CallBack Msg 정의
m_pLauncherUpdate->SetMsgCallBack( SendWndMessage );
m_pApplicationUpdate->SetMsgCallBack( SendWndMessage );
//1. 런처 FTP 연결
CUpdateProcessInfo& rLauncherInfo = CUpdateScriptParser::GetInstance()->m_LauncherInfo;
if( !m_pLauncherUpdate->ConnectFTP( rLauncherInfo.m_szFtpIP, rLauncherInfo.m_wFtpPort, rLauncherInfo.m_szID, rLauncherInfo.m_szPass ) )
return FALSE;
//2. 런처 업데이트는 실패하든 성공하든, 다음으로 넘어간다.
m_pLauncherUpdate->StartUpdate();
//3. 런처 업데이트 완료 되었으므로, Disconnect한다.
m_pLauncherUpdate->DisconnectFTP();
//4. 어플리케이션 FTP 연결
CUpdateProcessInfo& rAppInfo = CUpdateScriptParser::GetInstance()->m_ApplicationInfo;
if( !m_pApplicationUpdate->ConnectFTP( rAppInfo.m_szFtpIP, rAppInfo.m_wFtpPort, rAppInfo.m_szID, rAppInfo.m_szPass ) )
return FALSE;
//5. 어플리 케이션은 업데이트가 하나만 실패해도, 실패하도록 한다.
if( !m_pApplicationUpdate->StartUpdate() )
return FALSE;
return TRUE;
}
BOOL CUpdateManager::SaveVersion( BYTE byUpdateType, BYTE byUpdateNumber, CVersionInfo& rInfo )
{
return CUpdateScriptParser::GetInstance()->SaveVersion( byUpdateType, byUpdateNumber, rInfo );
}
VOID SendWndMessage( UINT unMsgType, VOID* pData )
{
if( !CUpdateManager::m_hWnd ) return;
if( unMsgType == WM_UPDATE_VERSION_ALARM )
{
UPDATE_INFO* pUpdateInfo = (UPDATE_INFO*)pData;
if( !pUpdateInfo ) return;
CVersionInfo verInfo;
verInfo.SetVersionFromString( pUpdateInfo->m_szProcessVer );
eUPDATE_TYPE type = CUpdateScriptParser::GetInstance()->GetUpdateTypeFromProcName( pUpdateInfo->m_szProcessName );
CUpdateManager::SaveVersion( type, pUpdateInfo->m_byUpdateNumber, verInfo );
}
PostMessage( CUpdateManager::m_hWnd, unMsgType, (WPARAM)unMsgType, (LPARAM)pData );
}
unsigned __stdcall CUpdateManager::UpdateThread( void* pParam )
{
CUpdateManager* pManager = (CUpdateManager*)pParam;
pManager->ExecuteUpdate();
SendWndMessage( WM_TERMINATE_PROCESS, "" );
return 1;
}