Guest User

Untitled

a guest
Dec 5th, 2016
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 11.83 KB | None | 0 0
  1. using System.Reflection;
  2. using Torque3D;
  3. using Torque3D.Engine;
  4. using Path = System.IO.Path;
  5.  
  6. namespace Game
  7. {
  8.    class TestScript
  9.    {
  10.       // [ScriptEntryPoint] Specifies which method should handle initialization of the engine. Think of it as the main.cs file.
  11.       [ScriptEntryPoint]
  12.       public static void entry()
  13.       {
  14.          // --- Boilerplate C#-specific setup. Normally Torque uses the main.cs file to set these variables, here we have to do it ourselves.
  15.          string CSDir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location).Replace('\\', '/');
  16.          Global.setMainDotCsDir(CSDir);
  17.          Global.setCurrentDirectory(CSDir);
  18.          // ---
  19.  
  20.          Global.setLogMode(6);
  21.  
  22.          // Set the name of our application
  23.          Globals.SetString("appName", "Empty");
  24.  
  25.          // The directory it is run from
  26.          Globals.SetString("defaultGame", "scripts");
  27.  
  28.          // Set profile directory
  29.          Globals.SetString("Pref::Video::ProfilePath", "core/profile");
  30.  
  31.          // Display the optional commandline arguements
  32.          Globals.SetBool("displayHelp", false);
  33.  
  34.          // Use these to record and play back crashes
  35.          //Global.saveJournal("editorOnFileQuitCrash.jrn");
  36.          //Global.playJournal("editorOnFileQuitCrash.jrn");
  37.  
  38.          // Process command line arguments
  39.          Global.exec("core/parseArgs.cs");
  40.  
  41.          Globals.SetBool("isDedicated", false);
  42.          Globals.SetInt("dirCount", 2);
  43.          Globals.SetString("userDirs", Globals.GetString("defaultGame") + ";art;levels");
  44.  
  45.          // load tools scripts if we're a tool build
  46.          if (Global.isToolBuild())
  47.             Globals.SetString("userDirs", "tools;" + Globals.GetString("userDirs"));
  48.  
  49.          // Parse the executable arguments with the standard
  50.          // function from core/main.cs
  51.          Global.call("defaultParseArgs");
  52.  
  53.          if (Globals.GetInt("dirCount") == 0)
  54.          {
  55.             Globals.SetString("userDirs", Globals.GetString("defaultGame"));
  56.             Globals.SetInt("dirCount", 1);
  57.          }
  58.  
  59.          //-----------------------------------------------------------------------------
  60.          // Display a splash window immediately to improve app responsiveness before
  61.          // engine is initialized and main window created
  62.          if (!Globals.GetBool("isDedicated"))
  63.             Global.displaySplashWindow();
  64.  
  65.          if (Globals.GetBool("compileAll"))
  66.          {
  67.             Global.echo(" --- Compiling all files ---");
  68.             compileFiles("*.cs");
  69.             compileFiles("*.gui");
  70.             compileFiles("*.ts");
  71.             Global.echo(" --- Compiling all files ---");
  72.             Global.echo(" --- Exiting after compile ---");
  73.             Global.quit();
  74.          }
  75.  
  76.          if (Globals.GetBool("compileTools"))
  77.          {
  78.             Global.echo(" --- Compiling tools scritps ---");
  79.             compileFiles("tools/*.cs");
  80.             compileFiles("tools/*.gui");
  81.             compileFiles("tools/*.ts");
  82.             Global.echo(" --- Exiting after compile ---");
  83.             Global.quit();
  84.          }
  85.  
  86.          // Default to a new logfile each session.
  87.          if (!Globals.GetBool("logModeSpecified"))
  88.          {
  89.             string platform = Globals.GetString("platform");
  90.             if (!platform.Equals("xbox") && !platform.Equals("xenon"))
  91.                Global.setLogMode(6);
  92.          }
  93.  
  94.          // Get the first dir on the list, which will be the last to be applied... this
  95.          // does not modify the list.
  96.          Global.nextToken(Globals.GetString("userDirs"), "currentMod", ";");
  97.  
  98.          Global.echo("--------- Loading DIRS ---------");
  99.  
  100.          loadDirs(Globals.GetString("userDirs"));
  101.          Global.echo("");
  102.  
  103.          if (Globals.GetInt("dirCount") == 0)
  104.          {
  105.             Global.enableWinConsole(true);
  106.             Global.error("Error: Unable to load any specified directories");
  107.             Global.quit();
  108.          }
  109.  
  110.          // Parse the command line arguments
  111.          Global.echo("--------- Parsing Arguments ---------");
  112.          // We want to call the TS-define parseArgs, not the C#-defined atm.
  113.          Global.call("parseArgs");
  114.          GuiCanvas Canvas = null;
  115.          // Either display the help message or startup the app.
  116.          if (Globals.GetBool("displayHelp"))
  117.          {
  118.             Global.enableWinConsole(true);
  119.             // We want to call the TS-define displayHelp, not the C#-defined atm.
  120.             Global.call("displayHelp");
  121.             Global.quit();
  122.          }
  123.          else
  124.          {
  125.             // We want to call the TS-define onStart, not the C#-defined atm.
  126.             Global.call("onStart");
  127.             Global.echo("Engine initialized...");
  128.  
  129.             ModuleManager ModuleDatabase = Sim.FindObjectByName<ModuleManager>("ModuleDatabase");
  130.             ModuleDatabase.scanModules("");
  131.  
  132.             //You can also explicitly decalre some modules here to be loaded by default if they are part of your game
  133.             //Ex: ModuleDatabase.LoadExplicit( "AppCore" );
  134.  
  135.             Canvas = Sim.FindObjectByName<GuiCanvas>("Canvas");
  136.             if (!Globals.GetBool("isDedicated"))
  137.             {
  138.                // As we know at this point that the initial load is complete,
  139.                // we can hide any splash screen we have, and show the canvas.
  140.                // This keeps things looking nice, instead of having a blank window
  141.                Global.closeSplashWindow();
  142.                Canvas.showWindow();
  143.             }
  144.  
  145.             // Auto-load on the 360
  146.             if (Globals.GetString("platform").Equals("xenon"))
  147.             {
  148.                string mission = "levels/Empty Terrain.mis";
  149.  
  150.                Global.echo($"Xbox360 Autoloading level: \'{mission}\'");
  151.  
  152.                string serverType;
  153.                if (Globals.GetBool("pref::HostMultiPlayer"))
  154.                   serverType = "MultiPlayer";
  155.                else
  156.                   serverType = "SinglePlayer";
  157.  
  158.                Global.call("createAndConnectToLocalServer", serverType, mission);
  159.             }
  160.          }
  161.  
  162.          // Display an error message for unused arguments
  163.          for (int i = 1; i < Globals.GetInt("Game::argc"); i++)
  164.          {
  165.             if (!Globals.GetBool($"argUsed[{i}"))
  166.                Global.error("Error: Unknown command line argument: " + Globals.GetString($"Game::argv[{i}]"));
  167.          }
  168.  
  169.          GuiCursor DefaultCursor = Sim.FindObjectByName<GuiCursor>("DefaultCursor");
  170.          GuiControl EditorChooseLevelGui = Sim.FindObjectByName<GuiControl>("EditorChooseLevelGui");
  171.          GuiControl EditorChooseGui = Sim.FindObjectByName<GuiControl>("EditorChooseGui");
  172.  
  173.          // Automatically start up the appropriate eidtor, if any
  174.          if (Globals.GetBool("startWorldEditor"))
  175.          {
  176.             Canvas.setCursor(DefaultCursor);
  177.             Canvas.setContent(EditorChooseLevelGui);
  178.          }
  179.          else if (Globals.GetBool("startGUIEditor"))
  180.          {
  181.             Canvas.setCursor(DefaultCursor);
  182.             Canvas.setContent(EditorChooseGui);
  183.          }
  184.       }
  185.  
  186.       [ConsoleFunction]
  187.       public static bool createCanvas()
  188.       {
  189.          if (Globals.GetBool("isDedicated"))
  190.          {
  191.             // This doesn't work in C# atm
  192.             Global.call("GFXInit::createNullDevice");
  193.             return true;
  194.          }
  195.  
  196.          // Create the Canvas
  197.          GuiCanvas Canvas = new GuiCanvas();
  198.          Canvas.setName("Canvas");
  199.          Canvas.registerObject();
  200.          // foo.displayWindow = $platform !$= "windows";
  201.  
  202.          Globals.SetInt("GameCanvas", Canvas.getId());
  203.  
  204.          // Set the window title
  205.          if (Global.isObject("Canvas"))
  206.             Canvas.setWindowTitle(Global.getEngineName() + " - " + Globals.GetString("appName"));
  207.  
  208.          return true;
  209.       }
  210.  
  211.       // Check if a script file exists, compiled or not.
  212.       // Marking a static function with [ConsoleFunction] exposes it to the engine, so it can be called from TorqueScript or via eval etc.
  213.       [ConsoleFunction]
  214.       public static bool isScriptFile(string path)
  215.       {
  216.          return Global.isFile(path + ".dso") || Global.isFile(path);
  217.       }
  218.  
  219.       // The displayHelp, onStart, onExit and parseArgs function are overriden
  220.       // by mod packages to get hooked into initialization and cleanup.
  221.       // lukaspj: Currently these are not marked with ConsoleFunction, because I have not figured out how to represent packages in C# yet.
  222.       //  - And whether they should be represented.
  223.       public static void onStart()
  224.       {
  225.          // Default startup function
  226.       }
  227.  
  228.       public static void onExit()
  229.       {
  230.          // OnExit is called directly from C++ code, whereas onStart is
  231.          // invoked at the end of this file.
  232.       }
  233.  
  234.       public static void parseArgs()
  235.       {
  236.          // Here for mod override, the arguments have already
  237.          // been parsed.
  238.       }
  239.  
  240.       [ConsoleFunction]
  241.       public static void compileFiles(string pattern)
  242.       {
  243.          string path = Global.filePath(pattern);
  244.  
  245.          string saveDSO = Globals.GetString("Scripts::OverrideDSOPath");
  246.          bool saveIgnore = Globals.GetBool("Scripts::ignoreDSOs");
  247.  
  248.          Globals.SetString("Scripts::OverrideDSOPath", path);
  249.          Globals.SetBool("Scripts::ignoreDSOs", false);
  250.          string mainCsFile = Global.makeFullPath("main.cs");
  251.  
  252.          for (string file = Global.findFirstFileMultiExpr(pattern);
  253.             !file.Equals("");
  254.             file = Global.findNextFileMultiExpr(pattern))
  255.          {
  256.             // we don't want to try and compile the primary main.cs
  257.             if (!mainCsFile.Equals(file))
  258.                Global.compile(file, true);
  259.          }
  260.  
  261.          Globals.SetString("Scripts::OverrideDSOPath", saveDSO);
  262.          Globals.SetBool("Scripts::ignoreDSOs", saveIgnore);
  263.       }
  264.  
  265.       [ConsoleFunction]
  266.       public static void displayHelp()
  267.       {
  268.          Global.activatePackage("Help");
  269.  
  270.          // Notes on logmode: console logging is written to console.log.
  271.          // -log 0 disables console logging.
  272.          // -log 1 appends to existing logfile; it also closes the file
  273.          // (flushing the write buffer) after every write.
  274.          // -log 2 overwrites any existing logfile; it also only closes
  275.          // the logfile when the application shuts down.  (default)
  276.  
  277.          Global.error(
  278.             "Torque Demo command line options:\n" +
  279.             "  -log <logmode>         Logging behavior; see main.cs comments for details\n" +
  280.             "  -game <game_name>      Reset list of mods to only contain <game_name>\n" +
  281.             "  <game_name>            Works like the -game argument\n" +
  282.             "  -dir <dir_name>        Add <dir_name> to list of directories\n" +
  283.             "  -console               Open a separate console\n" +
  284.             "  -jSave  <file_name>    Record a journal\n" +
  285.             "  -jPlay  <file_name>    Play back a journal\n" +
  286.             "  -help                  Display this help message\n"
  287.             );
  288.       }
  289.  
  290.       // Execute startup scripts for each mod, starting at base and working up
  291.       [ConsoleFunction]
  292.       public static void loadDir(string dir)
  293.       {
  294.          //TODO pushback($userDirs, % dir, ";");
  295.  
  296.          if (isScriptFile(dir + "/main.cs"))
  297.             Global.exec(dir + "/main.cs");
  298.       }
  299.  
  300.       [ConsoleFunction]
  301.       public static void loadDirs(string dirPath)
  302.       {
  303.          dirPath = Global.nextToken(dirPath, "token", ";");
  304.          string token = Globals.GetString("token");
  305.  
  306.          if (!dirPath.Equals(""))
  307.             loadDirs(dirPath);
  308.  
  309.          if (Global.exec(token + "/main.cs") != true)
  310.          {
  311.             Global.error("Error: Unable to find specified directory: " + "token");
  312.             Globals.SetInt("dirCount", Globals.GetInt("dirCount") - 1);
  313.          }
  314.       }
  315.    }
  316. }
Advertisement
Add Comment
Please, Sign In to add comment