
///////////////////////////////////////////////////////////////////////////////
//
// File   : AVP_Chunk_WeaponData.cpp
//
// Copyright (C) 2009 Rebellion, All Rights Reserved.
//
///////////////////////////////////////////////////////////////////////////////

#include "Core/Asura.h"

#include "ChunkSystem/AVP_Chunk_WeaponData.h"

#include "ChunkSystem/Asura_SaveBlock.h"
#include "ChunkSystem/Asura_Chunk_Stream.h"

#include "Blueprint/AVP_Blueprint_HashIDs.h"
#include "ChunkSystem/AVP_Chunk_Loaders.h"
#include "LoadAndSave/AVP_WeaponData_System.h"

#if defined (ASURA_TOOLS)

static const u_int g_uChunkVersion = 1;

bool AVP_Chunk_WeaponData::WriteToFile( Asura_String& xFileName )
{
	Asura_SaveBlock					xSaveBlock;
	Asura_Chunk_SaveBlock_Stream	xStream(&xSaveBlock);

	// Fill-in the save-block...
	WriteToChunkStream(xStream);

	// Terminate the Asura-file...
	xStream.WriteAsuraTerminator();

	// ...before saving the file to disc...
	return(xSaveBlock.SaveFile(xFileName.GetString(), false));
}

void AVP_Chunk_WeaponData::WriteToChunkStream( Asura_Chunk_Stream& xStream )
{
	// Begin writing...
	xStream.WriteChunk_Start(AVP_CHUNKID_WEAPONDATA, g_uChunkVersion, 0);

	AVP_WeaponData_System::InitialiseFromBlueprints();

	xStream << AVP_Game_Core::NUM_CHARACTERTYPES; // we read into arrays of this size so if this changes then we need to barf.
	xStream << uAVP_MAX_WEAPONS_PER_CHARACTERTYPE; // we read into arrays of this size so if this changes then we need to barf.

	for ( u_int uCharacterTypeItr = 0; uCharacterTypeItr < AVP_Game_Core::NUM_CHARACTERTYPES; ++uCharacterTypeItr )
	{
		xStream << AVP_WeaponData_System::s_auNumWeapons[ uCharacterTypeItr ];

		for ( u_int uWeaponItr = 0; uWeaponItr < AVP_WeaponData_System::GetNumWeaponsForCharacterType( static_cast< AVP_Game_Core::CharacterType >( uCharacterTypeItr ) ); ++uWeaponItr )
		{
			xStream << Asura_String( AVP_WeaponData_System::GetWeaponNameFromIndex( uWeaponItr, static_cast< AVP_Game_Core::CharacterType >( uCharacterTypeItr ) ) );
		}
	}

	xStream.WriteChunk_End();
}

#endif

void AVP_Chunk_WeaponData::ReadFromChunkStream( Asura_Chunk_Stream& xStream )
{
	if ( Version == 1 )
	{
		u_int uNumCharacterType;
		xStream >> uNumCharacterType; 
		Asura_Assert( uNumCharacterType == AVP_Game_Core::NUM_CHARACTERTYPES, "Number of character types has changed, need to re-export common file" );

		u_int uNumWeaponsPerCharacterType;
		xStream >> uNumWeaponsPerCharacterType;
		Asura_Assert( uNumWeaponsPerCharacterType == uAVP_MAX_WEAPONS_PER_CHARACTERTYPE, "Number of weapons per character type has changed, need to re-export common file" );

		for ( u_int uCharacterTypeItr = 0; uCharacterTypeItr < uNumCharacterType; ++uCharacterTypeItr )
		{
			u_int uNumWeapons;
			xStream >> uNumWeapons;
			if ( uCharacterTypeItr < AVP_Game_Core::NUM_CHARACTERTYPES )
			{
				AVP_WeaponData_System::s_auNumWeapons[ uCharacterTypeItr ] = uNumWeapons;
			}
			
			for ( u_int uWeaponItr = 0; uWeaponItr < AVP_WeaponData_System::s_auNumWeapons[ uCharacterTypeItr ]; ++uWeaponItr )
			{
				Asura_String xWeaponName;
				xStream >> xWeaponName;
				if ( uWeaponItr < uAVP_MAX_WEAPONS_PER_CHARACTERTYPE )
				{
					Asura_PrintfToBfr( AVP_WeaponData_System::s_aaszWeaponNames[ uCharacterTypeItr ][ uWeaponItr ], "%s", uAVP_MAX_WEAPON_FILENAME_LENGTH, true, xWeaponName.GetString() );
					AVP_WeaponData_System::s_aabWeaponShouldBeLoaded[ uCharacterTypeItr ][ uWeaponItr ] = false; // reset the should be loaded flag
				}
			}
		}

		AVP_WeaponData_System::s_bLoaded = true;
	}
}
