// panelafter.cpp : implementation file
//

#include "stdafx.h"
#include "binarytool.h"
#include "globaldata.h"
#include "process.h"
#include "panelafter.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPanelAfter dialog


CPanelAfter::CPanelAfter(CWnd* pParent /*=NULL*/)
	: CPanel(CPanelAfter::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPanelAfter)
	//}}AFX_DATA_INIT

		_tcscpy( m_szTitle, _T("Post-Binarization") );
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


void CPanelAfter::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPanelAfter)
	DDX_Control(pDX, IDC_EDIT_SizeOfBigFile, m_EditSizeOfBigFile);
	DDX_Control(pDX, IDC_CHECK_DeleteRelocTables, m_CheckDeleteRelocTables);
	//}}AFX_DATA_MAP
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


BEGIN_MESSAGE_MAP(CPanelAfter, CDialog)
	//{{AFX_MSG_MAP(CPanelAfter)
	ON_BN_CLICKED(IDC_BUTTON_GenerateBigFile, OnBUTTONGenerateBigFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


BOOL CPanelAfter::Create( CWnd* pParentWnd )
{
	BOOL bResult = CDialog::Create( IDD, pParentWnd );

	if( bResult )
	{
		// Read registry, update controls
		CWinApp *p_oApp = AfxGetApp();
		CString csSection = m_szTitle;

		UINT     uiSize = p_oApp -> GetProfileInt( csSection, _T("BigFileSize"), 1 );
		CString  csSize;

		csSize.Format( "%u", uiSize );

		m_EditSizeOfBigFile.SetWindowText( csSize );
		m_CheckDeleteRelocTables.SetCheck( p_oApp -> GetProfileInt( csSection, _T("DeleteRelocTables"), 0 ) );
	}

	return bResult;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 


void CPanelAfter::m_fn_vUpdateRegistry()
{
	CWinApp *p_oApp = AfxGetApp();
	CString csSection = m_szTitle;

	CString  csSize;
	m_EditSizeOfBigFile.GetWindowText( csSize );

	p_oApp -> WriteProfileInt( csSection, _T("BigFileSize"), atoi(csSize) );
	p_oApp -> WriteProfileInt( csSection, _T("DeleteRelocTables"), m_CheckDeleteRelocTables.GetCheck() );
}

/////////////////////////////////////////////////////////////////////////////
// CPanelAfter message handlers

void CPanelAfter::OnBUTTONGenerateBigFile() 
{
	char      szCommandLine[512];
	char      szExeName[512];
	CString   csGamedata;
	CString   csBigFileSize;

	m_EditSizeOfBigFile.GetWindowText( csBigFileSize );
	UINT uiBigFileSize = atoi( csBigFileSize );

	if( uiBigFileSize == 0 )
	{
		AfxMessageBox( "Please specify a size for the bigfile", MB_OK | MB_ICONSTOP );
		return;
	}

	g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyStartOfWork();

	fn_vOutputLine( "Generating BigFile for relocation tables...\r\n" );

	csGamedata = g_stTheGlobalData.csMainDirectory + "\\Gamedata";
	sprintf( szExeName, "%s\\BigFileTool.exe", (LPCTSTR)g_stTheGlobalData.csStartupDirectory );
	sprintf( 
	    szCommandLine, 
	    "%s -scriptpath:%s -binarypath:%s -bigfilesize:%s -autorun -generateinfo -startprg", 
			szExeName,
			(LPCTSTR)csGamedata,
			(LPCTSTR)g_stTheGlobalData.csBinDirectory,
			(LPCTSTR)csBigFileSize );

	if( fn_eSpawnAndWaitForProcessWithInputEvents( szExeName, szCommandLine ) == STATUS_C_Error )
		g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyEndOfWork( TRUE );
	else
		g_stTheGlobalData.p_oMainDlg -> m_fn_vNotifyEndOfWork( FALSE );
}
