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

#include "stdafx.h"
#include "ResourceBrowser.h"

#include "MainFrm.h"
#include "ResourceBrowserDoc.h"
#include "ResourceBrowserView.h"
#include <Stuff\Stuff.hpp>
#include <DLLPlatform\DLLPlatform.hpp>
#include <Adept\Adept.hpp>
#include <Compost\Compost.hpp>
#include <gosfx\gosfx.hpp>
#include <elementrenderer\elementrenderer.hpp>
#include <GameOS\ToolOS.hpp>
#include <Adept\Resource.hpp>
#include <buildnum\buildnum.h>
#include <MW4\MW4.hpp>


HGOSHEAP Heap;

BOOL CTCL_IsConsole()
{
    return FALSE;
}

using namespace Adept;
using namespace MechWarrior4;

void __stdcall InitializeGameEngine()
{
	Stuff::InitializeClasses();
	MidLevelRenderer::InitializeClasses(NULL, 2048*8);
	Compost::InitializeClasses();
	gosFX::InitializeClasses();
	ElementRenderer::InitializeClasses();
	Adept::InitializeClasses();

	Adept::ResourceManager::Instance = new Adept::ResourceManager;
	Check_Object(Adept::ResourceManager::Instance);
	
	ResourceManager::Instance->OpenResourceFile(
		"Resource\\textures.mw4",
		"Resource\\textures.dep",
		TexturesResourceFileID,
		VER_CONTENTVERSION,
		true,
		false
		);
	ResourceManager::Instance->OpenResourceFile(
		"Resource\\core.mw4",
		"Resource\\core.dep",
		CoreResourceFileID,
		VER_CONTENTVERSION,
		true,
		false
		);
	ResourceManager::Instance->OpenResourceFile(
		"Resource\\props.mw4",
		"Resource\\props.dep",
		PropsResourceFileID,
		VER_CONTENTVERSION,
		true,
		false
		);

ResFileNames[TexturesResourceFileID]="Resource\\textures.mw4";
ResFileNames[CoreResourceFileID]="Resource\\core.mw4";
ResFileNames[PropsResourceFileID]="Resource\\props.mw4";
	::Heap = gos_CreateMemoryHeap("Default");
	Check_Pointer(::Heap);
	gos_PushCurrentHeap(::Heap);

}

int FreeId=FirstNonCoreResourceFileID;
CString ResFileNames[1024];

void CResourceBrowserApp::OpenFilesInPath(CString &str)
{	
	WIN32_FIND_DATA file_data;
	HANDLE file_find;
	file_find=FindFirstFile(str+"\\*.mw4",&file_data);
	
	CString base_file_name;
	if(file_find != INVALID_HANDLE_VALUE)
	{
		do
		{
		base_file_name=str+"\\"+file_data.cFileName;
		base_file_name=base_file_name.Left(base_file_name.ReverseFind('.'));

		ResFileNames[FreeId]=base_file_name+".mw4";

		ResourceManager::Instance->OpenResourceFile(
		base_file_name+".mw4",
		base_file_name+".dep",
		FreeId++,
		VER_CONTENTVERSION,
		false,
		false
		);


		}

		while (FindNextFile(file_find,&file_data));
	}

}
 


/////////////////////////////////////////////////////////////////////////////
// CResourceBrowserApp

BEGIN_MESSAGE_MAP(CResourceBrowserApp, CWinApp)
	//{{AFX_MSG_MAP(CResourceBrowserApp)
	ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
	//}}AFX_MSG_MAP
	// Standard file based document commands
	ON_COMMAND(ID_FILE_NEW, CWinApp::OnFileNew)
	ON_COMMAND(ID_FILE_OPEN, CWinApp::OnFileOpen)
	// Standard print setup command
	ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CResourceBrowserApp construction

CResourceBrowserApp::CResourceBrowserApp()
{
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CResourceBrowserApp object

CResourceBrowserApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CResourceBrowserApp initialization

BOOL CResourceBrowserApp::InitInstance()
{
	InitGameOS(NULL, NULL, "");
	InitializeGameEngine();
	AfxEnableControlContainer();

	// Standard initialization

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

	// Change the registry key under which our settings are stored.
	SetRegistryKey(_T("Local AppWizard-Generated Applications"));

	LoadStdProfileSettings();  // Load standard INI file options (including MRU)

	// Register document templates

	CSingleDocTemplate* pDocTemplate;
	pDocTemplate = new CSingleDocTemplate(
		IDR_MAINFRAME,
		RUNTIME_CLASS(CResourceBrowserDoc),
		RUNTIME_CLASS(CMainFrame),       // main SDI frame window
		RUNTIME_CLASS(CResourceBrowserView));
	AddDocTemplate(pDocTemplate);

OpenFilesInPath(CString("Resource\\Missions"));
OpenFilesInPath(CString("Resource\\Maps"));

	// Parse command line for standard shell commands, DDE, file open
	CCommandLineInfo cmdInfo;
	ParseCommandLine(cmdInfo);

	// Dispatch commands specified on the command line
	if (!ProcessShellCommand(cmdInfo))
		return FALSE;
	m_pMainWnd->ShowWindow(SW_SHOW);
	m_pMainWnd->UpdateWindow();

	return TRUE;
}


/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
		// No message handlers
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

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 CResourceBrowserApp::OnAppAbout()
{
	CAboutDlg aboutDlg;
	aboutDlg.DoModal();
}

/////////////////////////////////////////////////////////////////////////////
// CResourceBrowserApp message handlers

void __stdcall TerminateGameEngine()
{
	Stuff::TerminateClasses();
}

void __stdcall GetGameOSEnvironment(char* CommandLine)
{

//	Environment.UpdateRenderers			= UpdateDisplay;
//	Environment.DoGameLogic				= DoGameLogic;
	Environment.InitializeGameEngine	= InitializeGameEngine;
	Environment.TerminateGameEngine		= TerminateGameEngine;


}

int CResourceBrowserApp::ExitInstance() 
{
	
	Adept::ResourceManager::Instance->CloseAllButCore();
	delete Adept::ResourceManager::Instance;
	Adept::ResourceManager::Instance=NULL;
	ExitGameOS();	
	return CWinApp::ExitInstance();
}
