main.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. // Set the name of our application
  23. $appName = "Empty";
  24. // The directory it is run from
  25. $defaultGame = "scripts";
  26. // Set profile directory
  27. $Pref::Video::ProfilePath = "core/profile";
  28. $Core::windowIcon = "core/torque.png";
  29. $Core::splashWindowImage = "art/gui/splash.png";
  30. function createCanvas(%windowTitle)
  31. {
  32. if ($isDedicated)
  33. {
  34. GFXInit::createNullDevice();
  35. return true;
  36. }
  37. // Create the Canvas
  38. %foo = new GuiCanvas(Canvas)
  39. {
  40. displayWindow = $platform !$= "windows";
  41. };
  42. $GameCanvas = %foo;
  43. // Set the window title
  44. if (isObject(Canvas))
  45. Canvas.setWindowTitle(getEngineName() @ " - " @ $appName);
  46. return true;
  47. }
  48. // Display the optional commandline arguements
  49. $displayHelp = false;
  50. // Use these to record and play back crashes
  51. //saveJournal("editorOnFileQuitCrash.jrn");
  52. //playJournal("editorOnFileQuitCrash.jrn");
  53. //------------------------------------------------------------------------------
  54. // Check if a script file exists, compiled or not.
  55. function isScriptFile(%path)
  56. {
  57. if( isFile(%path @ ".dso") || isFile(%path) )
  58. return true;
  59. return false;
  60. }
  61. //------------------------------------------------------------------------------
  62. // Process command line arguments
  63. exec("core/parseArgs.cs");
  64. $isDedicated = false;
  65. $dirCount = 2;
  66. $userDirs = $defaultGame @ ";art;levels";
  67. // load tools scripts if we're a tool build
  68. if (isToolBuild())
  69. $userDirs = "tools;" @ $userDirs;
  70. // Parse the executable arguments with the standard
  71. // function from core/main.cs
  72. defaultParseArgs();
  73. if($dirCount == 0) {
  74. $userDirs = $defaultGame;
  75. $dirCount = 1;
  76. }
  77. //-----------------------------------------------------------------------------
  78. // Display a splash window immediately to improve app responsiveness before
  79. // engine is initialized and main window created
  80. if (!$isDedicated)
  81. displaySplashWindow();
  82. //-----------------------------------------------------------------------------
  83. // The displayHelp, onStart, onExit and parseArgs function are overriden
  84. // by mod packages to get hooked into initialization and cleanup.
  85. function onStart()
  86. {
  87. // Default startup function
  88. }
  89. function onExit()
  90. {
  91. // OnExit is called directly from C++ code, whereas onStart is
  92. // invoked at the end of this file.
  93. }
  94. function parseArgs()
  95. {
  96. // Here for mod override, the arguments have already
  97. // been parsed.
  98. }
  99. function compileFiles(%pattern)
  100. {
  101. %path = filePath(%pattern);
  102. %saveDSO = $Scripts::OverrideDSOPath;
  103. %saveIgnore = $Scripts::ignoreDSOs;
  104. $Scripts::OverrideDSOPath = %path;
  105. $Scripts::ignoreDSOs = false;
  106. %mainCsFile = makeFullPath("main.cs");
  107. for (%file = findFirstFileMultiExpr(%pattern); %file !$= ""; %file = findNextFileMultiExpr(%pattern))
  108. {
  109. // we don't want to try and compile the primary main.cs
  110. if(%mainCsFile !$= %file)
  111. compile(%file, true);
  112. }
  113. $Scripts::OverrideDSOPath = %saveDSO;
  114. $Scripts::ignoreDSOs = %saveIgnore;
  115. }
  116. if($compileAll)
  117. {
  118. echo(" --- Compiling all files ---");
  119. compileFiles("*.cs");
  120. compileFiles("*.gui");
  121. compileFiles("*.ts");
  122. echo(" --- Exiting after compile ---");
  123. quit();
  124. }
  125. if($compileTools)
  126. {
  127. echo(" --- Compiling tools scritps ---");
  128. compileFiles("tools/*.cs");
  129. compileFiles("tools/*.gui");
  130. compileFiles("tools/*.ts");
  131. echo(" --- Exiting after compile ---");
  132. quit();
  133. }
  134. package Help {
  135. function onExit() {
  136. // Override onExit when displaying help
  137. }
  138. };
  139. function displayHelp() {
  140. activatePackage(Help);
  141. // Notes on logmode: console logging is written to console.log.
  142. // -log 0 disables console logging.
  143. // -log 1 appends to existing logfile; it also closes the file
  144. // (flushing the write buffer) after every write.
  145. // -log 2 overwrites any existing logfile; it also only closes
  146. // the logfile when the application shuts down. (default)
  147. error(
  148. "Torque Demo command line options:\n"@
  149. " -log <logmode> Logging behavior; see main.cs comments for details\n"@
  150. " -game <game_name> Reset list of mods to only contain <game_name>\n"@
  151. " <game_name> Works like the -game argument\n"@
  152. " -dir <dir_name> Add <dir_name> to list of directories\n"@
  153. " -console Open a separate console\n"@
  154. " -jSave <file_name> Record a journal\n"@
  155. " -jPlay <file_name> Play back a journal\n"@
  156. " -help Display this help message\n"
  157. );
  158. }
  159. //--------------------------------------------------------------------------
  160. // Default to a new logfile each session.
  161. if( !$logModeSpecified )
  162. {
  163. if( $platform !$= "xbox" && $platform !$= "xenon" )
  164. setLogMode(6);
  165. }
  166. // Get the first dir on the list, which will be the last to be applied... this
  167. // does not modify the list.
  168. nextToken($userDirs, currentMod, ";");
  169. // Execute startup scripts for each mod, starting at base and working up
  170. function loadDir(%dir)
  171. {
  172. pushback($userDirs, %dir, ";");
  173. if (isScriptFile(%dir @ "/main.cs"))
  174. exec(%dir @ "/main.cs");
  175. }
  176. echo("--------- Loading DIRS ---------");
  177. function loadDirs(%dirPath)
  178. {
  179. %dirPath = nextToken(%dirPath, token, ";");
  180. if (%dirPath !$= "")
  181. loadDirs(%dirPath);
  182. if(exec(%token @ "/main.cs") != true)
  183. {
  184. error("Error: Unable to find specified directory: " @ %token );
  185. $dirCount--;
  186. }
  187. }
  188. loadDirs($userDirs);
  189. echo("");
  190. if($dirCount == 0) {
  191. enableWinConsole(true);
  192. error("Error: Unable to load any specified directories");
  193. quit();
  194. }
  195. // Parse the command line arguments
  196. echo("--------- Parsing Arguments ---------");
  197. parseArgs();
  198. // Either display the help message or startup the app.
  199. if ($displayHelp) {
  200. enableWinConsole(true);
  201. displayHelp();
  202. quit();
  203. }
  204. else {
  205. onStart();
  206. echo("Engine initialized...");
  207. ModuleDatabase.scanModules( "" );
  208. //You can also explicitly decalre some modules here to be loaded by default if they are part of your game
  209. //Ex: ModuleDatabase.LoadExplicit( "AppCore" );
  210. if( !$isDedicated )
  211. {
  212. // As we know at this point that the initial load is complete,
  213. // we can hide any splash screen we have, and show the canvas.
  214. // This keeps things looking nice, instead of having a blank window
  215. closeSplashWindow();
  216. Canvas.showWindow();
  217. }
  218. // Auto-load on the 360
  219. if( $platform $= "xenon" )
  220. {
  221. %mission = "levels/Empty Terrain.mis";
  222. echo("Xbox360 Autoloading level: '" @ %mission @ "'");
  223. if ($pref::HostMultiPlayer)
  224. %serverType = "MultiPlayer";
  225. else
  226. %serverType = "SinglePlayer";
  227. createAndConnectToLocalServer( %serverType, %mission );
  228. }
  229. }
  230. // Display an error message for unused arguments
  231. for ($i = 1; $i < $Game::argc; $i++) {
  232. if (!$argUsed[$i])
  233. error("Error: Unknown command line argument: " @ $Game::argv[$i]);
  234. }
  235. // Automatically start up the appropriate eidtor, if any
  236. if ($startWorldEditor) {
  237. Canvas.setCursor("DefaultCursor");
  238. Canvas.setContent(EditorChooseLevelGui);
  239. } else if ($startGUIEditor) {
  240. Canvas.setCursor("DefaultCursor");
  241. Canvas.setContent(EditorChooseGUI);
  242. }