// MW4GameEd2.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "MW4GameEd2.h"

#include "MainFrm.h"
#include "GameFrame.h"
#include "GameView.h"
#include "GameInterface.h"
#include "NameManager.h"


void GameInit(HINSTANCE inst,HWND win,char *Comline);

CCriticalSection MsgHan;
bool gHideWindows;
extern bool gNoDialogs;
extern bool AllowFail;

// jcem - start
#include "ctcls.h"
#include "ctcl.h" // jcem

extern Time g_fLastGameStart;

#pragma data_seg(".SHARED_DATA")
// all data in this section must be INITIALIZED!!!!!!!!!!!!!
BOOL g_bLaunched = FALSE;
#pragma data_seg()
#pragma comment(linker, "/section:.SHARED_DATA,RWS")

extern int g_bUseOrgJoy;
extern int g_bNoPlasma;

extern void (__stdcall *g_pfnCTCL_Proc)();

extern int g_nCTCL;
BOOL __stdcall CTCL_Before();
void __stdcall CTCL_After();
void __stdcall CTCL_Proc();
// jcem - end

extern void _stdcall ExitGameOS();

/////////////////////////////////////////////////////////////////////////////
// CMW4GameEd2App

BEGIN_MESSAGE_MAP(CMW4GameEd2App, CWinApp)
	//{{AFX_MSG_MAP(CMW4GameEd2App)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	ON_COMMAND(ID_FILE_NEW, OnFileNew)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMW4GameEd2App construction

CMW4GameEd2App::CMW4GameEd2App()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CMW4GameEd2App object

CMW4GameEd2App theApp;

/////////////////////////////////////////////////////////////////////////////
// CMW4GameEd2App initialization

BOOL CMW4GameEd2App::InitInstance()
{
	// Initialize OLE libraries
	if (!AfxOleInit())
	{
		AfxMessageBox(IDP_OLE_INIT_FAILED);
//		return FALSE;
	// This is not essential don't kill execution here.
	}

	CString cmdline;
	cmdline=m_lpCmdLine;
	cmdline.MakeLower();
	if(cmdline.Find("-nobuild")>=0) 
		AllowBuild=false;
	if(cmdline.Find("-bldskies")>=0)  // jcem
		AllowBldSkies=true;
	if(cmdline.Find("-nobldmissions")>=0)  // jcem
		NoBldMissions=true;

	if(cmdline.Find("-report")>=0) 
		gHideWindows=true;

//	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	CString mutexstr;
		mutexstr.LoadString(AFX_IDS_APP_TITLE);
		HANDLE rst=CreateMutex(NULL,TRUE,mutexstr);
		if(ERROR_ALREADY_EXISTS==GetLastError())
		{	
			CString errmsg,title;
			errmsg.LoadString(IDS_APPRUNNING);
			title.LoadString(IDS_ERROR);
			if (MessageBox(NULL,errmsg,title,MB_ICONQUESTION | MB_YESNO) == IDNO)
			{
			exit(1);
			}
			ReleaseMutex(rst);
			CreateMutex(NULL,TRUE,mutexstr);
		}	

	// Change the registry key under which our settings are stored.
	// TODO: You should modify this string to be something appropriate
	// such as the name of your company or organization.
	SetRegistryKey(_T("Microsoft\\MW4\\MissionEditor"));


	// To create the main window, this code creates a new frame window
	// object and then sets it as the application's main window object.
	NameManager::Instance=new NameManager;
	pFrame = new CMainFrame;
	m_pMainWnd = pFrame;
	
	// create main MDI frame window
	if (!pFrame->LoadFrame(IDR_MAINFRAME))
		return FALSE;
	

	


	// try to load shared MDI menus and accelerator table
	//TODO: add additional member variables and load calls for
	//	additional menu types your application may need. 

	HINSTANCE hInst = AfxGetResourceHandle();
	m_hMDIAccel = ::LoadAccelerators(hInst, MAKEINTRESOURCE(IDR_MAINFRAME));



	 
	// The main window has been initialized, so show and update it.
//	pFrame->ShowWindow(m_nCmdShow);
	pFrame->ShowWindow(gHideWindows?SW_HIDE:SW_SHOWMAXIMIZED);
	pFrame->InitSecondaryWindows();
	// jcem - RunEula();
	GameInit(hInst,pFrame->GameWnd->m_wndView.m_hWnd,m_lpCmdLine);
	pFrame->InitPostGame();
	pFrame->UpdateWindow();

	int alev;
	CString numstr;
	if((alev=cmdline.Find("-armorlevel"))>=0) 
	{
	alev+=strlen("-armorlevel ");
	numstr=cmdline.Mid(alev,1);
	Stuff::ArmorLevel=atoi(numstr);

	}

	if((alev=cmdline.Find("-report"))>=0) 
	{
	alev+=strlen("-report ");
	CString misname;
	misname=cmdline.Mid(alev);
	int pos;
	pos=misname.Find(' ');
	Verify(pos!=0);
	gNoDialogs=true;
	AllowFail=true;
	Stuff::ArmorLevel=0;
	if(pos>0)
	{
	misname=misname.Left(pos);
	}

	pFrame->MakeReport(misname);
	exit(1);
	}

	

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMW4GameEd2App message handlers

int CMW4GameEd2App::ExitInstance() 
{
	//TODO: handle additional resources you may have added
/*
	if (m_hMDIMenu != NULL)
		FreeResource(m_hMDIMenu);
	if (m_hMDIAccel != NULL)
		FreeResource(m_hMDIAccel);
*/	
	TerminateGameEngine();
	
	return CWinApp::ExitInstance();
}

void CMW4GameEd2App::OnFileNew() 
{
}




CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

// App command to run the dialog
void CMW4GameEd2App::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CMW4GameEd2App message handlers

extern bool gActive;
extern bool gGotFocus;

BOOL CMW4GameEd2App::OnIdle(LONG lCount) 
{
	gActive = true;
	gGotFocus= true;
	BOOL res=pFrame->OnIdle();
	BOOL res2=CWinApp::OnIdle(lCount);
	return (res || res2); 
}

// jcem - start
int __stdcall CTCL_GetType(void* instance, int args, void* data[])
{
    ASSERT(FALSE);
    return 0;
}

int __stdcall CTCL_IsConsole(void* instance, int args, void* data[])
{
    ASSERT(FALSE);
    return 0;
}

int __stdcall CTCL_IsConsoleX(void* instance, int args, void* data[])
{
    ASSERT(FALSE);
    return 0;
}

int __stdcall CTCL_Get(void* instance, int args, void* data[])
{
    ASSERT(FALSE);
    return 0;
}

int __stdcall CTCL_Set(void* instance, int args, void* data[])
{
    ASSERT(FALSE);
    return 0;
}

BOOL __stdcall CTCL_Before()
{
    ASSERT(FALSE);
    return FALSE;
}

void __stdcall CTCL_After()
{
    ASSERT(FALSE);
}

void __stdcall CTCL_Proc()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_DoTerminateAppl()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_DoMainShell()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_DoCreateGame()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_DoJoinGame()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_CheckJoinGame()
{
    ASSERT(FALSE);
}

void CTCL_API CTCL_StartGame(BOOL bRealLaunch)
{
    ASSERT(FALSE);
}

SCRIPTVAR_INT(AdvertiseThisGame)		= 1;
SCRIPTCALLBACK(SetGameNameDefault)
{
	DWORD dwSize = 256;
	FREE_PTR(GameName);
	GameName = (char *) gos_Malloc(dwSize);

	strcpy(GameName, "Server");

	return 0;
}

//========================================================================
// Sets the global variable 'PlayerName' to the default player name.
//========================================================================
SCRIPTCALLBACK(SetPlayerNameDefault)
{
	DWORD dwSize = 256;
	FREE_PTR(PlayerName);
	PlayerName = (char *) gos_Malloc(dwSize);

	strcpy(PlayerName, "Player");

	return 0;
}

// jcem - end
