#include "AdeptHeaders.hpp"

#include "Replicator.hpp"
#include "Tool.hpp"

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	Replicator__CreateMessage::ConstructCreateMessage(Script *script)
{
	Check_Object(script);

	//
	//--------------
	// Set up stream
	//--------------
	//
	MemoryStream *message_stream = script->messageStream;
	Check_Object(message_stream);
	message_stream->AllocateBytes(sizeof(Replicator__CreateMessage));
	Replicator__CreateMessage *message =
		Cast_Pointer(
			Replicator__CreateMessage*,
			message_stream->GetPointer()
		);
	message->messageLength = sizeof(*message);

	//
	//---------------------------------
	// Set up the basics of the message
	//---------------------------------
	//
	message->messageID = Connection::ReplicateMessageID;
	message->messagePriority = DefaultEventPriority;
	message->messageFlags = DefaultFlags;

	//
	//---------------------------------------------------------------
	// Point at the notation file and make other files relative to it
	//---------------------------------------------------------------
	//
	Check_Object(Tool::Instance);
	Page *page = script->instancePage;
	Check_Object(page);
	Check_Object(script->classData);
	message->classID = script->classData->GetClassID();

	//
	//--------------------------------------------
	// Set up the replicator ID and read the flags
	//--------------------------------------------
	//
	message->replicatorID.connectionID = 0;
	message->replicatorID.localID = 1;
	Check_Object(script->baseID);
	++*script->baseID;
	message->replicatorFlags = 0;
	const char* mode ;
	if (page->GetEntry("ReplicatorMode", &mode))
	{
		if (!_stricmp(mode, "Independent"))
			message->replicatorFlags = Replicator::IndependentMode;
		else if (!_stricmp(mode, "Hermit"))
			message->replicatorFlags = Replicator::HermitMode;
	}
}
