//global functions we need in order to start up:
///here as a stub until common loads
function updateLoadStatus()
{
}

function updateLoadLevelStatus()
{
}

function getModCount(%modlist)
{
	//mods are seperated by ";" character
	//we count en by replacing " " with #
	//then replace ; with  " " and then use the wordcount function
	//silly - but saves us looking for a C++ function that does this.
	%list = strreplace(%modlist, " ", "#");
	%list = strreplace(%list, ";", " ");
	return getWordCount(%list);

}

// --------------------------------------------------------------------
// Bit-Shifter Helper.
// --------------------------------------------------------------------
function BIT(%bitIndex)
{
	return 1<<%bitIndex;
}


//-----------------------------------------------------------------------------
// Add Mod-Token to front of mods list.
//-----------------------------------------------------------------------------
function pushFront(%list, %token, %delim)
{
	// Is the list empty?
	if (%list !$= "")
		// No, so add new token.
		return %token @ %delim @ %list;
		
	// Yes, so just use new token.
	return %token;
}

//-----------------------------------------------------------------------------
// Add Mod-Token to back of mods list.
//-----------------------------------------------------------------------------
function pushBack(%list, %token, %delim)
{
	// Is the list empty?
	if (%list !$= "")
		// No, so add new token.
		return %list @ %delim @ %token;
		
	// Yes, so just use new token.
	return %token;
}


//-----------------------------------------------------------------------------
// Remove Mod-Token from the mods list.
//-----------------------------------------------------------------------------
function popFront(%list, %delim)
{
	// Next Token.
	return nextToken(%list, unused, %delim);
}


//-----------------------------------------------------------------------------
// The displayHelp, onStart, onExit and parseArgs function are overriden
// by mod packages to get hooked into initialization and cleanup. 
//-----------------------------------------------------------------------------
function onStart()
{
   // Default startup function
}


//-----------------------------------------------------------------------------
// Exit Engine.
//-----------------------------------------------------------------------------
function onExit()
{
   // OnExit is called directly from C++ code, whereas onStart is
   // invoked at the end of this file.
}


//-----------------------------------------------------------------------------
// Parse Arguments.
//-----------------------------------------------------------------------------
function parseArgs()
{
   // Here for mod override, the arguments have already
   // been parsed.
}   

//--------------------------------------------------------------------------
// Execute MOD Directory.
//--------------------------------------------------------------------------
function loadDir( %dir )
{
	// Set Mod Paths.
	setModPaths( pushback($userMods, %dir, ";") );
	
	// Execute Boot-strap file.
	exec( %dir @ "/main.cs" );
}


//--------------------------------------------------------------------------
// Execute startup scripts for each mod, starting at base and working up.
//--------------------------------------------------------------------------
function loadMods( %modPath )
{
	// Get MOD Path.
	%modPath = nextToken(%modPath, token, ";");
	echo("mod to load: " @ %modPath);
	
	// Did we get a MOD token?
	if (%modPath !$= "")
		// Yes, so recursively load it.
		loadMods(%modPath);


	echo("executing mod main: " @ %token);
	
	// Execute MOD Boot-strap file.
	if( exec(%token @ "/main.cs") != true )
	{
		// Problem.
		error("Error: Unable to find specified mod: " @ %token );
		// Decrease MOD Count.
	}
}






if(isPublicRelease())
{
	$runWithEditors = false;
}
else
{
	$runWithEditors = true;
}
//set this to your game's name 
$defaultGame = "P52"; 

//set this to be the boot strap script file, that will get executed ASAP when canvas is ready
$BootStrapFile = "P52/guicode/cs/P52Loader.cs";

////////////////////////////////////////////////////////////////////////////////////////////////////
/////	The one and only start function - called at the bottom of this file
/////   it should be the *only* function called from the global space in this file!
///////////////////////////////////////////////////////////////////////////////////////////////////////
function mainStart()
{	//set up the mods we want to run - based on the few globals specified at the top 
	//of this file. Note, we *always* want to load and activate the common mod *first*
	//so need to make sure it is the last mod in our list:
	%modsToLoad = $defaultGame;
	
	// Log-Mode Off by default.
	//may be set to true in mainParseArgs
	$logModeSpecified = false;
		
	if($runWithEditors)
	{
		%modsToLoad	=	"common;" @ %modsToLoad @";tools;";
		echo("Running with tools");
	}
	else
	{
		%modsToLoad	=	"common;" @ %modsToLoad;
	}	
	
	//parse the command line, give it the modlist to modify if need be:
	mainParseArgs(%modsToLoad);
	
	//set mod paths
	//there may be additional ones set that are actually packages under 
	//the creators mod: %modsToLoad
	setModPaths(%modsToLoad);
	
	//set the log mode if no log specified:
	if ( !$logModeSpecified )
	{
		setLogMode(6);
	}
	
	//load the mods
	loadMods(%modsToLoad);
	echo("Mods loaded, calling onStart");

	// Let the mods do their arg-parsing.
	parseArgs(%modsToLoad);

	//call the onStart function (will get called in common, first!)
	//note that common will call the other onStarts after it is done.
	onStart();
	// User Info.
	echo("T2D Engine (" @ getT2DVersion() @ ") initialized...");
}


////////////////////////////////////////////////////////////////////////////////////////////////////
/////	called from main start to parseCommand line args
/////   we pass in the current %modlist so it can be modified here
/////   all the other args can be applied right here:
///////////////////////////////////////////////////////////////////////////////////////////////////////
function mainParseArgs(%modlist)
{
	for (%i = 1; %i < $Game::argc ; %i++)
	{
		// Set Current Argument.
		%arg = $Game::argv[%i];
		// Set Next Argument.
		%nextArg = $Game::argv[%i+1];
		// Check if we've got another argument.
		%hasNextArg = $Game::argc - %i > 1;
		
		
		// Handle Argument Appropriately.
		switch$ (%arg)
		{		
			// ****************************************************
			// Console Logging.
			// ****************************************************
			case "-log":
			if (%hasNextArg)
			{
				// Turn on console logging
				if (%nextArg != 0)
				{
				// Dump existing console to logfile first.
				%nextArg += 4;
				}
				setLogMode(%nextArg);
				$logModeSpecified = true;
				%i++;
			}
			else
				error("Error: Missing Command Line argument. Usage: -log <Mode: 0,1,2>");
			
			
			// ****************************************************
			// MODs.
			// ****************************************************
			case "-mod":
			
			if (%hasNextArg)
			{
				// Append the mod to the end of the current list
				%modlist = strreplace(%modlists, %nextArg, "");
				%modlist = pushFront(%modlist, %nextArg, ";");
				%i++;
			}
			else
				error("Error: Missing Command Line argument. Usage: -mod <mod_name>");

				
			
			// ****************************************************
			// Journal Save.
			// ****************************************************
			case "-jSave":
			if (%hasNextArg)
			{
				echo("Saving event log to journal: " @ %nextArg);
				saveJournal(%nextArg);
				%i++;
			}
			else
				error("Error: Missing Command Line argument. Usage: -jSave <journal_name>");

				
			// ****************************************************
			// Journal Play.
			// ****************************************************
			case "-jPlay":
			if (%hasNextArg)
			{
				playJournal(%nextArg,false);
				%i++;
			}
			else
				error("Error: Missing Command Line argument. Usage: -jPlay <journal_name>");

				
			// ****************************************************
			// Debugging.
			// ****************************************************
			case "-jDebug":
			if (%hasNextArg)
			{
				playJournal(%nextArg,true);
				%i++;
			}
			else
				error("Error: Missing Command Line argument. Usage: -jDebug <journal_name>");
			
			////////////////////////////////////////////////////////////////////////////////////////
			// Build dsos for export. (Meant to be called from update_distributions_src.bat)
			////////////////////////////////////////////////////////////////////////////////////////
			case "-buildDsoFilesForExport":

				// We'll do this a bit later, after the ResourceManager's been set up.
				$g_bBuildDsoFilesForExport	= true;
		}
	}

}


//--------------------------------------------------------------------------
// Default to a new logfile each session.
//--------------------------------------------------------------------------


//--------------------------------------------------------------------------
// Set the mod path which dictates which directories will be visible
// to the scripts and the resource engine.
//--------------------------------------------------------------------------




//start the game going:
mainStart();



