//===========================================================================//
// File:	D3DRMVideoRenderables.cpp                                        //
// Project: MUNGA           Brick: Win95 Video Renderer 	                 // 
// Contents: GOS95 Layer Video Renderer									 //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 03/13/97  JTR  Initial coding.                                            //
//---------------------------------------------------------------------------//
// Copyright (C) 1997, Virtual World Entertainment, Inc.                     //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "AdeptHeaders.hpp"
#include "VideoHeaders.hpp"
#include "Tool.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
ShapeComponent::ClassData*
	ShapeComponent::CreateFactoryRequest(FactoryRequestParameters *parameters)
{
	Check_Object(parameters);

	//
	//-------------------------------------------------------------------------
	// Allocate enough room for what we need to write out, then call our parent
	//-------------------------------------------------------------------------
	//
	MemoryStream *component_stream = parameters->m_stream;
	Check_Object(component_stream);
	component_stream->AllocateBytes(sizeof(ShapeComponent));
	Check_Object(Resource::ParentFileDependencies);

	//
	//------------------------------------
	// Find the parent object for the mesh
	//------------------------------------
	//
	Page *page = parameters->m_page;
	Check_Object(page);

	//
	//-------------------------
	// Deal with the instancing
	//-------------------------
	//
	bool instance = false;
	page->GetEntry("Unique", &instance);
	*component_stream << instance;

	//
	//--------------------------
	// Read in the geometry name
	//--------------------------
	//
	const char* geometry_name;
	page->GetEntry("Geometry", &geometry_name, true);
	FileStream file_stream(geometry_name);
	const char* file_name = file_stream.GetFileName();

	//
	//---------------------------------------------------------------
	// See if the resource already exists, and if so, just use its ID
	//---------------------------------------------------------------
	//
	Resource erf_res(file_name);
	if (!erf_res.DoesResourceExist() || !erf_res.IsResourceUpToDate())
	{
		FileDependencies erf_dependencies;
		erf_dependencies.AddDependency(&file_stream);
		erf_res.Save(&file_stream, &erf_dependencies);
		if (!erf_res.IsRegistered())
			Resource::ParentFileDependencies->AddDependencies(&erf_dependencies);
	}
	else if (!erf_res.IsRegistered())
		erf_res.AddFileDependenciesTo(Resource::ParentFileDependencies);
	*component_stream << erf_res.GetResourceID();

	//
	//----------------------------
	// Let our parent do its thing
	//----------------------------
	//
	bool result = BaseClass::CreateFactoryRequest(parameters) != NULL;

	Check_Object(DefaultData);
	return (result) ? DefaultData : NULL;
}

