//---------------------------------------------------------------------------------------------
// Torque Game Builder
// Copyright (C) GarageGames.com, Inc.
//---------------------------------------------------------------------------------------------

//saveJournal("bug.jnl");

//playJournal("bug.jnl");

/// Player Initialization Procedure
/// 
function onStart()
{   
}

function onExit()
{
}

//---------------------------------------------------------------------------------------------
// Load the paths we need access to
//---------------------------------------------------------------------------------------------
function loadPath( %path )
{
   setModPaths( getModPaths() @ ";" @ %path );
   exec(%path @ "/main.cs");

}


function preBuild( )
{
   if (isShippingBuild())
   {
      error("Cannot perform Pre Build on shipping version");
      quit();
   }
   
   echo("Performing PRE Build Startup");
   
	loadPath("common");
	loadPath("game");
	
   // Make sure EVERYTHING is compiled before we continue
   %spec = "*.cs";
   // compile all matching cs files into dso's (if any)
   for (%file = findFirstFile(%spec); %file !$= ""; %file = findNextFile(%spec))
      compile(%file);   
      
   %spec = "*.gui";
   // compile all matching cs files into dso's (if any)
   for (%file = findFirstFile(%spec); %file !$= ""; %file = findNextFile(%spec))
      compile(%file);  
   
   %spec = "*.t2d";
   // compile all matching cs files into dso's (if any)
   for (%file = findFirstFile(%spec); %file !$= ""; %file = findNextFile(%spec))
      compile(%file); 
      
   onStart();
   
	_initializeProject();

	// Startup the project
	initializeProject();
	
   // dump font cache (we assume build scripts have cleared this
   populateAllFontCacheRange(1,128);
   writeFontCache();
   
   echo("Pre Build Complete, quitting");
   quit();
}

function standardStartup()
{
	//---------------------------------------------
	// Do some bootstrap voodoo to get the game to 
	// the initializeProject phase of loading and 
	// pass off to the user
	//---------------------------------------------

	// Output a console log
	setLogMode(6);

   setModPaths( "screenbranding" );
   
	loadPath( "common" );

	loadPath( "game" );

	onStart();
	
	// remainder of loading has been moved to standardStartupPhase2 and delayed
	// until common has finished its phased loading 
}

// Callback made by common once screen initialised and remainder of common loaded
function standardStartupPhase2()
{
	// Initialized
	echo("\nTorque Game Builder (" @ getT2DVersion() @ ") initialized...");

	if( !isFunction( "initializeProject" ) || !isFunction( "_initializeProject" ) )
	{
		messageBox( "Game Startup Error", "'initializeProject' function could not be found." @
						"\nThis could indicate a bad or corrupt common directory for your game." @
						"\n\nThe Game will now shutdown because it cannot properly function", "Ok", "MIStop" );
		quit();
	}

	_initializeProject();

	// Startup the project
	// NOTE: All project loading is done in multiple stages with very short scheduled delays between
	// each phase. This allows the loading gui a chance to update the canvas.
	schedule( 100, 0, "initializeProject" );
}


if ( $Game::argc == 2 && $Game::argv[1] $= "-PreBuild")
	prebuild();
else
	standardStartup();
