#include "MW4DUIHeaders.hpp"
#include <windows.h>
#include "DedicatedUI.hpp"
#include "resource.h"
#include <buildnum\buildnum.h>
#include "..\ScriptStrings\resource.h" // scriptstrings.h - jcem

extern void (*RunFromOtherAppEnterStoppingGameState)(void);
extern void (*RunFromOtherAppEnterRecyclingGameState)(void);
extern void (*RunFromOtherAppEnterLoadingGameState)(void);
extern void (*RunFromOtherAppEnterRunningGameState)(void);
extern bool gNoDialogs;
extern bool gRunEula;
extern bool gNoAutoConfig;
extern bool RunAutoConfigIfNeeded();
extern bool FirstRunEula(const char *eula_filename,const char *warranty_filename);
extern bool EulaDeclineMsg(const char *str,const char *caption);

long _stdcall ProcessException( EXCEPTION_POINTERS* ep );
void _stdcall InitExceptionHandler( char* CommandLine );
DWORD _stdcall RunGameOSLogic();
void _stdcall ExitGameOS();
LRESULT CALLBACK GameOSWinProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
extern HINSTANCE g_hInst;
extern HANDLE g_hThreadEditor;
HWND g_hWndMain = NULL;
HWND g_hWndSetup = NULL;
HWND g_hWndLobby = NULL;
HWND g_hWndServer = NULL;
DWORD g_langResources = 0;

int MechMessageBoxWin32(char *szTitle, char *szMessage, UINT uType=MB_OK)
{
	return ::MessageBox(NULL, szMessage, szTitle, uType);
}

//
// Process any messages before GameOS sees them
//
INT CALLBACK WinProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
	switch (uMsg) {
	case WM_CLOSE:
		CDedicatedServerUI::Quit();
		EndDialog(hWnd, TRUE);
		return 0;
	}

	return FALSE;
	//return GameOSWinProc( hWnd, uMsg, wParam, lParam );
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall CDedicatedServerUI::InitializeGameEngine()
{
	// Set_Heap_Check(1, 100);

	//
	//---------------------
	// Initialize libraries
	//---------------------
	//
	
	Stuff::InitializeClasses(4800, 40000);	
	if(!gNoAutoConfig)
		RunAutoConfigIfNeeded();
	
	NotationFile startup_ini("options.ini", NotationFile::Standard, true);
	
	MidLevelRenderer::InitializeClasses(&startup_ini, 2048, 2048);
	gosFX::InitializeClasses(&startup_ini);
	ElementRenderer::InitializeClasses(&startup_ini);
	Compost::InitializeClasses(&startup_ini);
	Adept::InitializeClasses(&startup_ini);
	MechWarrior4::InitializeClasses(&startup_ini);

	//
	//--------------------------------------
	// Create and initialize the application
	//--------------------------------------
	//
	gos_PushCurrentHeap(MechWarrior4::Heap);
	MWApplication *MW_Application =
		new MWApplication(MWApplication::DefaultData,true);
	Check_Object(MW_Application);
	//Adept::Application::GetInstance() = MW_Application;
	GlobalPointers::AddGlobalPointer(MW_Application, ApplicationGlobalPointerIndex);
	MW_Application->Initialize();
	
	//--------------------------------------
	// Run the Eula
	//--------------------------------------	
	if(gRunEula && Application::RunOK && !gNoDialogs)
	{
		bool gotlicense=false;
		while(!gotlicense)
			
		{
			MString ename,wname;
			ename=MWApplication::GetInstance()->GetLocString(IDS_EULA_FILENAME);
			wname=MWApplication::GetInstance()->GetLocString(IDS_WARRANTY_FILENAME);
			if(!FirstRunEula(ename,wname))
			{
				MString msg,cap;
				msg=MWApplication::GetInstance()->GetLocString(IDS_EULA_FAIL_MESSAGE);
				cap=MWApplication::GetInstance()->GetLocString(IDS_EULA_FAIL_CAPTION);
				if(EulaDeclineMsg(msg,cap))
				{
					Quit();
				}
			}
			else
			{
				gotlicense=true;
			}
		}
	}
	
	//
	//------------------------------------------------------------------
	// Now that we are out of building mode, reset the file hooks to use
	// resources
	//------------------------------------------------------------------
	//
	Environment.HookGetFile = Application::GetFileForGOS;
	Environment.HookDoesFileExist = Application::FindFileForGOS;
	Environment.defaultPlayerName = NULL;

	//
	// Now that resources are built. Add another layer in the getfile and find file change that can
	// watch for .mw4 file requests and potentially remap them for 8.3 compatibility.
	//
//	Environment.HookGetFile = MWApplication::MW4GetFileForGOS;
//	Environment.HookDoesFileExist = MWApplication::MW4FindFileForGOS;

	//
	//-----------------------------------------------
	// Terminate the game if running is not permitted
	//-----------------------------------------------
	//
	if (!Adept::Application::RunOK)
	{
		gos_TerminateApplication();
	}
	else
	{
		// generate the keys...
		MWApplication::GetInstance()->localKey = MWApplication::GetInstance()->GetUniqueKey();
		

		//
		//--------------------------------------------------------
		// We have disabled building, so load up the required data
		//--------------------------------------------------------
		//
		ResourceManager::Instance->OpenResourceFile(
			"Resource\\textures.mw4",
			NULL,
			TexturesResourceFileID,
			VER_CONTENTVERSION,
			true,
			false
		);
		ResourceManager::Instance->OpenResourceFile(
			"Resource\\core.mw4",
			NULL,
			CoreResourceFileID,
			VER_CONTENTVERSION,
			true,
			false
		);
		ResourceManager::Instance->OpenResourceFile(
			"Resource\\props.mw4",
			NULL,
			PropsResourceFileID,
			VER_CONTENTVERSION,
			true,
			false
		);

		MWApplication::GetInstance()->LoadLookupTables();
	}



//#ifdef LAB_ONLY
//	g_BergerName = MidLevelRenderer::MLRTexturePool::Instance->Add ("bf");
//#endif

	gos_PopCurrentHeap();
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void _stdcall CDedicatedServerUI::TerminateGameEngine()
{


	//
	//-------------------
	// Shut down the game
	//-------------------
	//
	gos_PushCurrentHeap(MechWarrior4::Heap);
	Adept::Application::GetInstance()->StopGame();
	Adept::Application::GetInstance()->Terminate();
	Unregister_Object(Adept::Application::GetInstance());
	delete Adept::Application::GetInstance();
	GlobalPointers::ClearPointer(ApplicationGlobalPointerIndex);
	gos_PopCurrentHeap();

	//
	//-----------------------
	// Shutdown the libraries
	//-----------------------
	//
	{
		NotationFile startup_ini("options.ini", NotationFile::Standard, true);
		MechWarrior4::TerminateClasses(&startup_ini);
		Adept::TerminateClasses(&startup_ini);
		Compost::TerminateClasses(&startup_ini);
		ElementRenderer::TerminateClasses(&startup_ini);
		gosFX::TerminateClasses(&startup_ini);
		MidLevelRenderer::TerminateClasses(&startup_ini);
		startup_ini.Save();
	}
	Stuff::TerminateClasses();
}

char g_connection_type[5][STRING_SIZE];
char g_bandwidth[10][STRING_SIZE];
INT CALLBACK CDedicatedServerUI::DedicatedServerProc(
  HWND hwndDlg,  // handle to dialog box
  UINT uMsg,     // message
  WPARAM wParam, // first message parameter
  LPARAM lParam  // second message parameter
)
{
    switch( uMsg )
    {
	case WM_INITDIALOG: 
	{

		MString new_pilot_directory = "Resource\\Pilots\\";
		if(!gos_DoesFileExist(new_pilot_directory))
		{
			char temp[STRING_SIZE];
			LoadString(g_hInst, IDS_STRING_NOPILOT, temp, STRING_SIZE);
			MechMessageBoxWin32(Application::GetInstance()->GetLocString(IDS_NETUI_ERROR), temp);
			Quit();
			return TRUE;
		}
		MW4Shell::Instance->SelectLastUsedPilot();
		MWApplication::GetInstance()->LoadServerOptions();
		gos_NetStartGame(0, MWApplication::SecureNetGame);	
		
		CancelDialup(NULL, 0, NULL);
		LoadMPConnectionSettings(NULL, NULL, NULL);
		if(NumConnections <= 0)
		{
			char temp[STRING_SIZE];
			LoadString(g_hInst, IDS_STRING_NOCONNECTION, temp, STRING_SIZE);
			MechMessageBoxWin32(Application::GetInstance()->GetLocString(IDS_NETUI_ERROR), temp);
			Quit();
			return TRUE;
		}
		
		InitConnectionWizard(NULL, 0, NULL);
		HWND comboConnection = ::GetDlgItem(hwndDlg, IDC_COMBO_CONNECTION);
		SendMessage(comboConnection,CB_RESETCONTENT, 0,0);
		for(int iloop = 0; iloop < NumConnections; iloop++)
		{
			SendMessage(comboConnection,CB_INSERTSTRING,iloop,(LPARAM) ListOfConnections[iloop]);
		}
		SendMessage(comboConnection,CB_SETCURSEL,ConnectionIndex,0);

		Str_Copy(g_connection_type[0], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_TYPE_MODEM), STRING_SIZE);
		Str_Copy(g_connection_type[1], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_TYPE_ISDN), STRING_SIZE);
		Str_Copy(g_connection_type[2], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_TYPE_XDSL), STRING_SIZE);
		Str_Copy(g_connection_type[3], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_TYPE_CABLE), STRING_SIZE);
		Str_Copy(g_connection_type[4], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_TYPE_LAN), STRING_SIZE);
		Str_Copy(g_bandwidth[0], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_14), STRING_SIZE);
		Str_Copy(g_bandwidth[1], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_28), STRING_SIZE);
		Str_Copy(g_bandwidth[2], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_56), STRING_SIZE);
		Str_Copy(g_bandwidth[3], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_64), STRING_SIZE);
		Str_Copy(g_bandwidth[4], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_128), STRING_SIZE);
		Str_Copy(g_bandwidth[5], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_384), STRING_SIZE);
		Str_Copy(g_bandwidth[6], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_768), STRING_SIZE);
		Str_Copy(g_bandwidth[7], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_1500), STRING_SIZE);
		Str_Copy(g_bandwidth[8], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_10K), STRING_SIZE);
		Str_Copy(g_bandwidth[9], gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_100K), STRING_SIZE);

		HWND descConnection = ::GetDlgItem(hwndDlg, IDC_CONNECTION_DESC);
		RECT rc;
		::GetWindowRect(descConnection, &rc);
		SendMessage(descConnection, LB_SETCOLUMNWIDTH, (rc.right-rc.left)/2, 0);
		SendMessage(descConnection, LB_SETCOUNT, 10*2, 0);

		OnConnectionSelectionChange(hwndDlg);
		return TRUE;
	}

	case WM_COMMAND:

		switch( HIWORD(wParam) )
		{
		case CBN_SELCHANGE:
			if (LOWORD(wParam) == IDC_COMBO_CONNECTION)
				OnConnectionSelectionChange(hwndDlg);
			return TRUE;
		}

		switch( LOWORD(wParam) )
		{
		case IDHOST:
		{
			EndDialog( hwndDlg, TRUE );
			HWND comboConnection = ::GetDlgItem(hwndDlg, IDC_COMBO_CONNECTION);
			int curSel = SendMessage( 
							(HWND) comboConnection,
							CB_GETCURSEL,
							(WPARAM) 0,
							(LPARAM) 0);

			if (curSel != CB_ERR) {
				if (GetDialupConnectionStatus(NULL, 0, NULL)==0)
				{
				}
				else {
					if (!g_hWndSetup) {
						g_hWndSetup = ::CreateDialog(g_hInst, 
							MAKEINTRESOURCE(IDD_HOSTSETUP),
							g_hWndMain,
							HostSetupProc);
					}
					else {
						SendMessage(g_hWndSetup, WM_INITDIALOG, 0, 0);
					}
					ShowWindow(g_hWndSetup, SW_SHOW);
				}
			}
			return TRUE;
		}
		case IDCANCEL:
			Quit();
			EndDialog( hwndDlg, TRUE );
			return TRUE;
		}
		return TRUE;

	case WM_CLOSE:
		Quit();
		EndDialog( hwndDlg, TRUE );
		break;
    }

    return FALSE;
}

void CDedicatedServerUI::OnConnectionSelectionChange(HWND hWnd)
{
	HWND comboConnection = ::GetDlgItem(hWnd, IDC_COMBO_CONNECTION);
	ConnectionIndex = SendMessage(comboConnection,CB_GETCURSEL,0,0);
	PreConnect(NULL, 0, NULL);

	SendMessage(GetDlgItem(hWnd, IDC_CONNECTION_DESC), LB_RESETCONTENT,0,0);
	SendMessage(GetDlgItem(hWnd, IDC_CONNECTION_DESC), LB_INSERTSTRING, 0, (LPARAM)(char*)gos_GetResourceString(g_langResources,IDS_ML_CH_TYPE));
	SendMessage(GetDlgItem(hWnd, IDC_CONNECTION_DESC), LB_INSERTSTRING, 1, (LPARAM)(char*)gos_GetResourceString(g_langResources,IDS_ML_MAIN_SPEED));
	SendMessage(GetDlgItem(hWnd, IDC_CONNECTION_DESC), LB_INSERTSTRING, 2, (LPARAM)(char*)gos_GetResourceString(g_langResources,IDS_MP_MC_BROWSE));
	
	for (int i=3; i<10; i++)
		SendMessage(GetDlgItem(hWnd, IDC_CONNECTION_DESC), LB_INSERTSTRING, i, (LPARAM)" ");

	if (MWApplication::MyConnectionType > -1 && MWApplication::MyConnectionType < 5) {
		MString type = g_connection_type[MWApplication::MyConnectionType];
		SendMessage(GetDlgItem(hWnd, IDC_CONNECTION_DESC), LB_INSERTSTRING, 10+0, (LPARAM)(char*)type);
	}
	
	if (MWApplication::MyConnectionSpeed > -1 && MWApplication::MyConnectionSpeed < 10) {
		MString speed = g_bandwidth[MWApplication::MyConnectionSpeed];
		SendMessage(GetDlgItem(hWnd, IDC_CONNECTION_DESC), LB_INSERTSTRING, 10+1, (LPARAM)(char*)speed);
	}

	int index = 2;
	if (BrowseGameSpy == 1) {
		MString browse = gos_GetResourceString(g_langResources,IDS_NC_HOW_GAMESPY);
		SendMessage(GetDlgItem(hWnd, IDC_CONNECTION_DESC), LB_INSERTSTRING, 10+index++, (LPARAM)(char*)browse);
	}

	if (BrowseGUN == 1) {
		MString browse = gos_GetResourceString(g_langResources,IDS_NC_HOW_GAMEZONE);
		SendMessage(GetDlgItem(hWnd, IDC_CONNECTION_DESC), LB_INSERTSTRING, 10+index++, (LPARAM)(char*)browse);
	}

	if (BrowseLAN == 1) {
		MString browse = gos_GetResourceString(g_langResources,IDS_MP_NC_SPEED_TYPE_LAN);
		SendMessage(GetDlgItem(hWnd, IDC_CONNECTION_DESC), LB_INSERTSTRING, 10+index++, (LPARAM)(char*)browse);
	}

	MString ip_address = "";
	for (int k = 0; k < BrowseIPAddresses; k++)
	{
		ip_address = ListOfTCPIPAddresses[k];
		if (!strcmp(ip_address, "")) {
			ip_address = gos_GetResourceString(g_langResources,IDS_MP_MC_IPADDRESS) + ip_address;
			SendMessage(GetDlgItem(hWnd, IDC_CONNECTION_DESC), LB_INSERTSTRING, 10+index++, (LPARAM)(char*)ip_address);
		}
	}	
}

void _stdcall CDedicatedServerUI::Run(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
	RunFromOtherAppEnterRecyclingGameState = &EnterRecyclingGameState;
	RunFromOtherAppEnterStoppingGameState = &EnterStoppingGameState;
	RunFromOtherAppEnterLoadingGameState = &EnterLoadingGameState;
	RunFromOtherAppEnterRunningGameState = &EnterRunningGameState;

	g_langResources = gos_OpenResourceDLL("ScriptStrings.dll");

	g_hWndMain = ::CreateDialog(g_hInst, 
		MAKEINTRESOURCE(IDD_MAINFRAME),
		::GetDesktopWindow(),
		WinProc);

	RECT rcDesk, rc;
	GetWindowRect(GetDesktopWindow(), &rcDesk);
	GetWindowRect(g_hWndMain, &rc);
	int w = rc.right-rc.left;
	int h = rc.bottom-rc.top;
	rc.left = rcDesk.left + ((rcDesk.right-rcDesk.left)-w)/2;
	rc.top = rcDesk.top + ((rcDesk.bottom-rcDesk.top)-h)/2;
	::MoveWindow(g_hWndMain, rc.left, rc.top, w, h, FALSE);
	::ShowWindow(g_hWndMain, SW_SHOW);

	if (MWApplication::GetInstance()->AutoLaunchServerGame)
	{
		if (AutoLaunchInitialize()) {
			g_hWndLobby = ::CreateDialog(g_hInst, 
				MAKEINTRESOURCE(IDD_GAMELOBBY),
				g_hWndMain,
				GameLobbyProc);
			::ShowWindow(g_hWndLobby, SW_SHOW);
		}
		else {
			gos_CloseResourceDLL(g_langResources);
			CDedicatedServerUI::TerminateGameEngine();
			ExitGameOS();			
		}
	}
	
	else {
		g_hWndServer = ::CreateDialog(g_hInst, 
			MAKEINTRESOURCE(IDD_DEDICATEDSERVER),
			g_hWndMain,
			DedicatedServerProc);
		::ShowWindow(g_hWndServer, SW_SHOW);
	}	

	MSG	msg;
	DWORD Terminate = 0;
	DWORD LastRun = 0;

	__try
	{
		while( !Terminate )
		{
			while( PeekMessage(&msg,NULL,0,0,PM_REMOVE) )
			{
				if( msg.message==WM_QUIT ) 
					break;

				TranslateMessage( &msg );
				DispatchMessage( &msg );
			}
		
			DWORD ElapsedTime = timeGetTime() - LastRun;
			if (ElapsedTime >= 60)
			{
				LastRun = timeGetTime();
				Terminate = RunGameOSLogic();
			}
			else
				Sleep(60-ElapsedTime);
		}
	}
	__except( ProcessException(GetExceptionInformation()) )
	{
	}
	
	if (g_hWndLobby) {
		::CloseWindow(g_hWndLobby);
		::DestroyWindow(g_hWndLobby);
		g_hWndLobby = NULL;
	}
	if (g_hWndSetup) {
		::CloseWindow(g_hWndSetup);
		::DestroyWindow(g_hWndSetup);
		g_hWndSetup = NULL;
	}
	if (g_hWndServer) {
		::CloseWindow(g_hWndServer);
		::DestroyWindow(g_hWndServer);
		g_hWndServer = NULL;
	}
	if (g_hWndMain) {
		::CloseWindow(g_hWndMain);
		::DestroyWindow(g_hWndMain);
		g_hWndMain = NULL;
	}

	gos_CloseResourceDLL(g_langResources);
	CDedicatedServerUI::TerminateGameEngine();
	ExitGameOS();
}

void _stdcall CDedicatedServerUI::Quit()
{
	gos_NetEndGame();
	gos_TerminateApplication();

	::PostQuitMessage(0);
}

HWND g_hWndWait = NULL;
void CDedicatedServerUI::EnterRunningGameState()
{
	CDedicatedServerUI::game_state = 2;
	if (g_hWndWait) {
		EndDialog(g_hWndWait, TRUE);
		g_hWndWait = NULL;
	}
	EnableWindow(GetDlgItem(g_hWndLobby, IDDISCONNECT), TRUE);
	EnableWindow(GetDlgItem(g_hWndLobby, IDEXIT), TRUE);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_BUTTON_KICK), TRUE);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_BUTTON_BAN), TRUE);
}

void CDedicatedServerUI::EnterStoppingGameState()
{
	CDedicatedServerUI::game_state = 0;

	if (IsWindowVisible(g_hWndLobby)) {
		SetNextGame(g_hWndLobby);		
		EnableMissionEdit(g_hThreadEditor==NULL);
		EnableWindow(GetDlgItem(g_hWndLobby, IDLAUNCH), TRUE);
		EnableWindow(GetDlgItem(g_hWndLobby, IDDISCONNECT), TRUE);
		EnableWindow(GetDlgItem(g_hWndLobby, IDC_BUTTON_KICK), TRUE);
		EnableWindow(GetDlgItem(g_hWndLobby, IDC_BUTTON_BAN), TRUE);
	}
}

void CDedicatedServerUI::EnterRecyclingGameState()
{
	EnterStoppingGameState();
}

void CDedicatedServerUI::EnterLoadingGameState()
{
	CDedicatedServerUI::game_state = 1;

	EnableWindow(GetDlgItem(g_hWndLobby, IDLAUNCH), FALSE);
	EnableWindow(GetDlgItem(g_hWndLobby, IDDISCONNECT), FALSE);
	EnableWindow(GetDlgItem(g_hWndLobby, IDEXIT), FALSE);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_BUTTON_KICK), FALSE);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_BUTTON_BAN), FALSE);
	EnableMissionEdit(FALSE);

	g_hWndWait = CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_WAIT), g_hWndMain, NULL);
	RECT rcDesk, rc;
	GetWindowRect(g_hWndMain, &rcDesk);
	GetWindowRect(g_hWndWait, &rc);
	int w = rc.right-rc.left;
	int h = rc.bottom-rc.top;
	rc.left = rcDesk.left + ((rcDesk.right-rcDesk.left)-w)/2;
	rc.top = rcDesk.top + ((rcDesk.bottom-rcDesk.top)-h)/2;
	MoveWindow(g_hWndWait, rc.left, rc.top, w, h, FALSE);
	ShowWindow(g_hWndWait, SW_SHOW);

	MSG	msg;
	while( PeekMessage(&msg,NULL,0,0,PM_REMOVE) )
	{
		if( msg.message==WM_QUIT ) 
			break;
		
		TranslateMessage( &msg );
		DispatchMessage( &msg );
	}
}

void CDedicatedServerUI::EnableMissionEdit(BOOL bEnable)
{

	EnableWindow(GetDlgItem(g_hWndLobby, IDC_BUTTON_ADDBOT), bEnable);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_COMBO_TIMELIMIT), bEnable);
	//EnableWindow(GetDlgItem(g_hWndLobby, IDC_COMBO_TEAMSEL), bEnable);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_COMBO_TEAMMINTON), bEnable);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_COMBO_TEAMMAXTON), bEnable);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_COMBO_TEAMTOTMAXTON), bEnable);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_EDIT_BOTSKILL), bEnable);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_EDIT_BOTNAME), bEnable);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_COMBO_MECHS), bEnable);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_COMBO_TEAM), bEnable);
	EnableWindow(GetDlgItem(g_hWndLobby, IDC_CHECK_FILLTEAM), bEnable);
	
	for (int i=IDC_COMBO_GAMETYPE; i<=IDC_CHECK_LOCKSERVER; i++)
		EnableWindow(GetDlgItem(g_hWndLobby, i), bEnable);

	for (i=0; i<16; i++)
	{
		EnableWindow(GetDlgItem(g_hWndLobby, IDC_BUTTON_CHANGE0+i), bEnable);
		EnableWindow(GetDlgItem(g_hWndLobby, IDC_BUTTON_CHANGETEAM0+i), bEnable);
	}

	if (bEnable) {
		OnCheckFragLimit(g_hWndLobby);
		OnCheckNumLives(g_hWndLobby);
	}

	return;
}

BOOL CDedicatedServerUI::AutoLaunchInitialize()
{		
	MString new_pilot_directory = "Resource\\Pilots\\";
	if(!gos_DoesFileExist(new_pilot_directory))
	{
		char temp[STRING_SIZE];
		LoadString(g_hInst, IDS_STRING_NOPILOT, temp, STRING_SIZE);
		MechMessageBoxWin32(Application::GetInstance()->GetLocString(IDS_NETUI_ERROR), temp);
		Quit();
		return FALSE;
	}
	MW4Shell::Instance->SelectLastUsedPilot();
	MWApplication::GetInstance()->LoadServerOptions();

	gos_InitializeNetworking();
	//gos_ConnectTCP(0,0);
	PrepareDefaultServerAdvertisers();
	
	Mech4CreateGame(MWApplication::NetGameName, MWApplication::NetPlayerName, MWApplication::NetPassword);
	DWORD networking = reinterpret_cast<DWORD>(gos_NetInformation(gos_Networking, 0));
	MWApplication::GetInstance()->networkingFlag = (networking)?true:false;
	
	NetMissionParameters::MWNetMissionParameters *params = MWApplication::GetInstance()->GetLocalNetParams();
	Str_Copy(params->m_serverName, MWApplication::NetGameName, STRING_SIZE);
	Str_Copy(params->m_serverIP, LocalIPAddress, STRING_SIZE);

	if (MWApplication::GetInstance()->networkingFlag)
	{
#if defined(_ARMOR)
		DWORD server =
			reinterpret_cast<DWORD>(
			gos_NetInformation(gos_AmITheServer, 0)
			);
		Verify(server);
#endif
		
		MWApplication::GetInstance()->serverFlag = true;
		
#if defined(LAB_ONLY)
		MWGameInfo::g_lastGameType = MWGameInfo::g_currentGameType;
		MWGameInfo::g_currentGameType = MWGameInfo::MultiServer;
#endif
		
		
		Verify(!Network::GetInstance());
		GlobalPointers::AddGlobalPointer(new Network, NetworkGlobalPointerIndex);
		Register_Object(Network::GetInstance());
		
		
		MWApplication::GetInstance()->SendRequestConnectionMessage();
		MW4Shell::Instance->StartNetworkServerExecute();
		Environment.DoGameLogic = &MWApplication::DoShellLogic;
		
	}
	else
	{
		STOP(("FAILED TO CREATE MULTIPLAYER GAME"));
		return FALSE;
	}
	return TRUE;
}
