// ----
//
//   By this stage, the preferences have been loaded.
//    The following function combinations may be called...
//    initClient();  then  initServer();  if we start up without -dedicated
//    initServer();  then  initDedicated();  if we start up with -dedicated
//
// ----

$Gui::fontCacheDirectory = expandFilename("./fonts");
$Gui::clipboardFile      = expandFilename("./cache/clipboard.gui");

$game::settings::cleanup[0]  = expandFilename( "*.ofn" );
$game::settings::cleanup[1]  = expandFilename( "*.dso" );
//$game::settings::cleanup[2]  = expandFilename( "*.ml" );
//$game::settings::cleanup[3]  = expandFilename( "*.mm" );

// ----

function initCommon()
{
    warn("\n--------- Initializing MOD: Common ---------");

    exec("./client/audio.cs");
    exec("./client/canvas.cs");

    setRandomSeed();
}

function initBaseClient()
{
    warn("\nInitializing Common: BaseClient");

    // -- initialise the base aspects
    exec("./client/message.cs");
    exec("./client/mission.cs");
//    exec("./client/messageBox.cs");
    exec("./client/cursor.cs");
    exec("./client/metrics.cs");
    exec("./client/actionMap.cs");
    exec("./client/missionDownload.cs");
    exec("./client/recordings.cs");
    exec("./client/controls.cs");
    exec("./client/screenshot.cs");
    exec("./client/CDNCurler.cs");
}

function initBaseServer()
{
    warn("\nInitializing Common: BaseServer");

    // -- Load up game server support scripts
    exec("./server/audio.cs");
    exec("./server/server.cs");
    exec("./server/message.cs");
    exec("./server/missionLoad.cs");
    exec("./server/missionList.cs");
    exec("./server/missionInfo.cs");
    exec("./server/missionDownload.cs");
    exec("./server/clientConnection.cs");
	//exec("./server/TCPObject.cs");
}

function initBaseShell()
{
    warn("\nInitializing Common: BaseShell");

    exec("./client/guiProfiles.cs");
    
    Canvas.setCursor("DefaultCursor");

    exec("./client/shell.cs");

    exec("./edit/mapEditor/editor.cs");
    exec("./edit/guiEditor/creatorProfiles.cs");
    exec("./edit/guiEditor/guiEditorGui.gui");
    exec("./edit/guiEditor/guiEditorGui.cs");
    exec("./edit/particleEditor/ParticleEditor.cs");
    exec("./edit/mapEditor/utilMenuDlg.gui");
    exec("./edit/mapEditor/utilMenuDlg.cs");
}

package common
{
function displayHelp()
{
    Parent::displayHelp();
    error(
             "Torque command line options:"
    @ "\n" @ "  -fullscreen            Starts game in full screen mode"
    @ "\n" @ "  -windowed              Starts game in windowed mode"
    @ "\n" @ "  -resolution            Starts game with set res (width height bitdepth)"
    @ "\n" @ "  -autoVideo             Auto detect video, but prefers OpenGL"
    @ "\n" @ "  -openGL                Force OpenGL acceleration"
    @ "\n" @ "  -directX               Force DirectX acceleration"
    @ "\n" @ "  -voodoo2               Force Voodoo2 acceleration"
    @ "\n"
    );
}

function parseArgs()
{
    // -- Call the parent
    Parent::parseArgs();

    // -- Arguments, which override everything else.
    for( %i = 1; %i < $Game::argc; %i++ )
    {
        %arg = $Game::argv[%i];
        %nextArg = $Game::argv[%i+1];
        %nextArg2 = $Game::argv[%i+2];
        %nextArg3 = $Game::argv[%i+3];
        %hasNextArg = $Game::argc - %i > 1;
        %has2NextArgs = $Game::argc - %i > 2;
        %has3NextArgs = $Game::argc - %i > 3;

        switch$( %arg )
        {
            // ---- ---- ---- ----
            case "-fullscreen":
                $pref::Video::fullScreen = 1;
                $argUsed[%i]++;

            // ---- ---- ---- ----
            case "-windowed":
                $pref::Video::fullScreen = 0;
                $argUsed[%i]++;

            // ---- ---- ---- ----
            case "-resolution":
                $argUsed[%i]++;
                if( %has3NextArgs )
                {
                    %width = %nextArg;
                    $argUsed[%i+1]++;
                    %i++;

                    %height = %nextArg2;
                    $argUsed[%i+1]++;
                    %i++;

                    %bitdepth = %nextArg3;
                    $argUsed[%i+1]++;
                    %i++;

                    if(%bitdepth != 16 && %bitdepth != 32)
                    {
                        error("Error: Invalid bitdepth ("@%bitdepth@") given, defaulting to 16 bit.");
                        %bitdepth = 16;
                    }
                    if($pref::Video::fullScreen)
                        $pref::Video::resolution = %width SPC %height SPC %bitdepth;
                    else
                        $pref::Video::windowres = %width SPC %height SPC %bitdepth;
                }
                else if( %has2NextArgs )
                {
                    $argUsed[%i+1]++;
                    %i++;
                    %width = %nextArg;

                    $argUsed[%i+1]++;
                    %i++;
                    %height = %nextArg2;

                    %bitdepth = 16;

                    error("Error: Missing Command Line arguments. Usage: -resolution width height bitdepth");
                    error("Error: Defaulting to 16 bit.");

                    $pref::Video::resolution = %width SPC %height SPC %bitdepth;
                }
                else if(%hasNextArg )
                {
                    $argUsed[%i+1]++;
                    %i++;
                    error("Error: Missing Command Line arguments. Usage: -resolution width height bitdepth");
                }
                else
                    error("Error: Missing Command Line arguments. Usage: -resolution width height bitdepth");

            // ---- ---- ---- ----
            case "-openGL":
                $pref::Video::displayDevice = "OpenGL";
                $argUsed[%i]++;

            // ---- ---- ---- ----
            case "-directX":
                $pref::Video::displayDevice = "D3D";
                $argUsed[%i]++;

            // ---- ---- ---- ----
            case "-voodoo2":
                $pref::Video::displayDevice = "Voodoo2";
                $argUsed[%i]++;

            // ---- ---- ---- ----
            case "-autoVideo":
                $pref::Video::displayDevice = "";
                $argUsed[%i]++;

        }
    }
}

function onStart()
{
    Parent::onStart();
    initCommon();
}

function quit()
{
    // Lets disconnect from the server if you're hosting (fixes the crash on exit bug)
    $client::shuttingDown = true;
    if(isFunction(disconnect))
        disconnect();
    Parent::quit();
}

function GuiCanvas::setContent(%this, %ctrl)
{
    if(clientShuttingDown())
        return;
    Parent::setContent(%this, %ctrl);
}

function onExit()
{
    OpenALShutdown();
    Parent::onExit();
    // Refresh the mod paths just in case the files to be deleted no longer exist.
    setModPaths($modPath);
    if( $game::settings::cleanup[0] !$= "" )
    {
        // -- if cleanup is specified, let's do the dirty.
        //    targets are buffered, as realtime loops with delete actions have
        //    bad mojo.
        %i = -1;
        for( %h = 0; $game::settings::cleanup[%h] !$= ""; %h++)
        {
            for( %file = findFirstFile($game::settings::cleanup[%h]); %file !$= ""; %file = findNextFile($game::settings::cleanup[%h]) )
                %delete_file[%i++] = %file;
        }

        for( %i = %i; %i >= 0; %i-- )
            deleteFile(%delete_file[%i]);
    }
}
    
// ----
};
activatePackage(common);
// -----------------------------------------------------------------------------

function onNeedRelight()
{
//
}
