//===========================================================================//
// File:	ErfImport.cpp                                                 
// Project: MechWarrior 4
// Contents: Plugin to export Collision Volume objects
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 07/29/99  jkyle
//---------------------------------------------------------------------------//
// Copyright (C) 1998, Fasa Interactive, Inc.
// All Rights reserved worldwide
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL
//===========================================================================//


#include "stdafx.h"

#ifdef STRICT							// Already defined by stdafx,
#undef STRICT							// so we avoid warning linking msg
#endif
#ifdef _MBCS							// The same as above
#undef _MBCS
#endif

#include "ErfImport.h"
#include <GameOS\ToolOS.hpp>
#include <Stuff\StuffHeaders.hpp>
#include <DLLPlatform\DLLPlatform.hpp>
#include <mlr\mlrheaders.hpp>

#include "MaxProxies\MaxProxyHeaders.hpp"
#include <ElementProxies\ElementProxies.hpp>
#include "proxies\burnlights.hpp"
#include "ExportUtil.h"

//#include "nodeobject.h"

#include "cvobject.h"
#include "mw4.hpp"


//undef Verify

#ifndef _DEBUG						//This is a hack To make it link in Release
#undef Spew
//void Spew() {}
#endif

//===========================================================================//
int ErfImp::DoImport(const TCHAR *name, ImpInterface *ii,Interface *i, BOOL suppressPrompts)
{
	int return_value;
	__try
	{
		return_value = DoImportWrap(name, ii, i, suppressPrompts);
		//return_value = true;
		GetSystemSetting(SYSSET_CLEAR_UNDO);
		Verify(!theHold.Holding());
	}
	__except(ProcessException(GetExceptionInformation()))
	{
	}

	return return_value;
}


extern int g_bConvertMeshes;

int	ErfImp::DoImportWrap(const TCHAR *name,ImpInterface *ei,Interface *ip, BOOL suppressPrompts)
{
	g_bConvertMeshes = 0;
	AFX_MANAGE_STATE(AfxGetStaticModuleState());

	CString gameDir = GetGamePath(ip);
	TurnOnExceptions();

	//Create the texture pool
	{
		gos_PushCurrentHeap(MidLevelRenderer::TexturePoolHeap);
		MidLevelRenderer::TGAFilePool *pool = new MidLevelRenderer::TGAFilePool(gameDir+"\\content\\textures\\");
		Check_Object(pool);
		MidLevelRenderer::MLRTexturePool::Instance = new MidLevelRenderer::MLRTexturePool(pool);
		Check_Object(MidLevelRenderer::MLRTexturePool::Instance);
		Check_Object(MidLevelRenderer::MLRTexturePool::Instance);
		gos_PopCurrentHeap();
	}

	// Load the .ERF
	Stuff::FileStream erf_file;
	Stuff::FileStream temp_erf_file;
	erf_file.Open(name);


	// Create the element_scene
	ElementRenderer::GroupElement *element_scene = new ElementRenderer::GroupElement;
	Check_Object(element_scene);
	element_scene->SetRootMode();
	element_scene->LockBounds();

	ElementRenderer::Element *erf_element=NULL;
	int version = ElementRenderer::ReadERFVersion(&erf_file);
	while(erf_file.GetBytesRemaining() > 0)
	{
		erf_element =
			ElementRenderer::Element::Create(
			&erf_file,
			version
			);
		erf_element->LockBounds();
		element_scene->AttachChild(erf_element);
		erf_element->ClearAlignX();
		erf_element->SetNeverCullMode();
		erf_element->SetLocalToParent(Stuff::LinearMatrix4D::Identity);
		element_scene->Sync();
	}
	
	ElementProxies::ElementSceneProxy *element_scene_proxy = ElementProxies::ElementSceneProxy::MakeProxy(element_scene);

	// Create the MAX interface
	MAXProxies::MAXScene *max_scene = MAXProxies::MAXScene::MakeProxy(ip,"");
	Check_Object(max_scene);

	// copy the mesh from the element to the mesh in max
	Proxies::CopyProcess copy_process;
	max_scene->Copy(&copy_process,element_scene_proxy);

	if (erf_element)
	{
		delete erf_element;
	}

	char dir[MAX_PATH],path[MAX_PATH],fname[MAX_PATH],ext[MAX_PATH];
	_splitpath(name,dir,path,fname,ext);

	int count = max_scene->rootList.Count();
	for (int x=0;x<count;x++)
	{
		INode *pNode = max_scene->rootList.GetNode(x);
		pNode->SetName((char *)fname);
	}

	if (ElementProxies::MLRTexturePoolProxy::Instance)
	{
	//	ElementProxies::MLRTexturePoolProxy::Instance->CloseLibrary(true);
	}

	TurnOffExceptions();
	g_bConvertMeshes = 1;
	return true;
}
