//===========================================================================//
// File:	MWTool.cpp														 //
//---------------------------------------------------------------------------//
//   Date    Who  Modification                                               //
// --------  ---  ---------------------------------------------------------- //
// 09/09/98  JSE  Inital base class based off of Shadowrun/Adept             //
//---------------------------------------------------------------------------//
// Copyright (C) 1998, Fasa Interactive										 //
// All Rights reserved worldwide                                             //
// This unpublished sourcecode is PROPRIETARY and CONFIDENTIAL               //
//===========================================================================//

#include "MW4Headers.hpp"

#include "MWTool.hpp"
#include "lancemate.hpp"
#include "Salvage.hpp"
#include "MWCampaign.hpp"
#include "MWTable.hpp"
#include "MWOptions.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
int
	MWTool::FindControlMode(const char* mode_name)
{
	STOP(("Need something here..."));
	return 0;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
bool
	MWTool::IsRegisterable(const char* entry_type)
{
	Check_Object(this);
	Check_Pointer(entry_type);

	if (
		!_stricmp(entry_type, "lancemate") ||
		!_stricmp(entry_type, "campaign") ||
		!_stricmp(entry_type, "salvage") ||
		!_stricmp(entry_type, "armature") ||
		!_stricmp(entry_type, "subsystems") ||
		!_stricmp(entry_type, "damage") ||
		!_stricmp(entry_type, "mwtable") ||
		!_stricmp(entry_type, "options")
	)
		return true;

	return Tool::IsRegisterable(entry_type);
}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MWTool::BuildResource(
		const char* entry_type,
		const char* entry_filename
	)
{
	Check_Object(this);
	Check_Pointer(entry_type);
	Check_Pointer(entry_filename);

	#if defined(_ARMOR)
		int stack_index = stackIndex;
	#endif

	//
	//--------------------
	// Deal with campaigns
	//--------------------
	//
	if (!_stricmp(entry_type, "campaign"))
	{
		NotationFile campaign_file(entry_filename, NotationFile::NonEmpty);
		Resource campaign_resource(campaign_file.GetFileName());
		if (!campaign_resource.DoesResourceExist() || !campaign_resource.IsResourceUpToDate())
		{
			DynamicMemoryStream campaign_stream;
			MWCampaign::ConstructCampaignStream(&campaign_stream, &campaign_file);
			FileDependencies dependencies(*campaign_file.GetFileDependencies());
			campaign_resource.Save(&campaign_stream, &dependencies);
		}
	}

	//
	//---------------------
	// Deal with lancemates
	//---------------------
	//
	else if (!_stricmp(entry_type, "lancemate"))
	{
		NotationFile lancemate_file(entry_filename, NotationFile::NonEmpty, true);
		Resource lancemate_resource(lancemate_file.GetFileName());
		if (!lancemate_resource.DoesResourceExist() || !lancemate_resource.IsResourceUpToDate())
		{
			DynamicMemoryStream lancemate_stream;
			LancemateManager::ConstructLancemateStream(&lancemate_stream, &lancemate_file);
			FileDependencies dependencies(*lancemate_file.GetFileDependencies());
			lancemate_resource.Save(&lancemate_stream, &dependencies);
		}
	}

	//
	//------------------
	// Deal with salvage
	//------------------
	//
	else if (!_stricmp(entry_type, "salvage"))
	{
		NotationFile salvage_file(entry_filename, NotationFile::NonEmpty);
		Resource salvage_resource(salvage_file.GetFileName());
		if (!salvage_resource.DoesResourceExist() || !salvage_resource.IsResourceUpToDate())
		{
			DynamicMemoryStream salvage_stream;
			SalvageManager::ConstructSalvageStream(&salvage_stream, &salvage_file);
			FileDependencies dependencies(*salvage_file.GetFileDependencies());
			salvage_resource.Save(&salvage_stream, &dependencies);
		}
	}

	else if (!_stricmp(entry_type, "armature"))
	{
		NotationFile armature_file(entry_filename, NotationFile::NonEmpty);
		Resource armature_resource(armature_file.GetFileName());
		if (!armature_resource.DoesResourceExist() || !armature_resource.IsResourceUpToDate())
		{
			DynamicMemoryStream armature_stream;
			MWObject::CreateArmatureStream(&armature_stream, &armature_file);
			FileDependencies dependencies(*armature_file.GetFileDependencies());
			armature_resource.Save(&armature_stream, &dependencies);
		}
	}

	else if (!_stricmp(entry_type, "subsystems"))
	{
		NotationFile subsystems_file(entry_filename, NotationFile::NonEmpty);
		Resource subsystems_resource(subsystems_file.GetFileName());
		if (!subsystems_resource.DoesResourceExist() || !subsystems_resource.IsResourceUpToDate())
		{
			DynamicMemoryStream subsystems_stream;
			MWObject::CreateSubsystemStream(&subsystems_stream, &subsystems_file);
			FileDependencies dependencies(*subsystems_file.GetFileDependencies());
			subsystems_resource.Save(&subsystems_stream, &dependencies);
		}
	}

	else if (!_stricmp(entry_type, "damage"))
	{
		NotationFile damage_file(entry_filename, NotationFile::NonEmpty);
		Resource damage_resource(damage_file.GetFileName());
		if (!damage_resource.DoesResourceExist() || !damage_resource.IsResourceUpToDate())
		{
			DynamicMemoryStream damage_stream;
			MWObject::CreateDamageStream(&damage_stream, &damage_file);
			FileDependencies dependencies(*damage_file.GetFileDependencies());
			damage_resource.Save(&damage_stream, &dependencies);
		}
	}

	else if (!_stricmp(entry_type, "mwtable"))
	{
		NotationFile table_file(entry_filename, NotationFile::NonEmpty, true);
		Resource table_resource(table_file.GetFileName());
		if (!table_resource.DoesResourceExist() || !table_resource.IsResourceUpToDate())
		{
			DynamicMemoryStream table_stream;
			MWTable::ConstructTableStream(&table_file, &table_stream);
			FileDependencies dependencies(*table_file.GetFileDependencies());
			table_resource.Save(&table_stream, &dependencies);
		}
	}

	else if (!_stricmp(entry_type, "options"))
	{
		NotationFile options_file(entry_filename, NotationFile::NonEmpty);
		Resource options_resource(options_file.GetFileName());
		if (!options_resource.DoesResourceExist() || !options_resource.IsResourceUpToDate())
		{
			DynamicMemoryStream options_stream;
			MWOptions::ConstructOptionsStream(&options_file, &options_stream);
			FileDependencies dependencies(*options_file.GetFileDependencies());
			options_resource.Save(&options_stream, &dependencies);
		}
	}

	else
		Tool::BuildResource(entry_type, entry_filename);

	Verify(stack_index == stackIndex);

}

