main.cs.in 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 = "@TORQUE_APP_NAME@";
  24. // The directory it is run from
  25. $defaultGame = "scripts";
  26. // Set profile directory
  27. $Pref::Video::ProfilePath = "core/profile";
  28. function createCanvas(%windowTitle)
  29. {
  30. if ($isDedicated)
  31. {
  32. GFXInit::createNullDevice();
  33. return true;
  34. }
  35. // Create the Canvas
  36. %foo = new GuiCanvas(Canvas)
  37. {
  38. displayWindow = $platform !$= "windows";
  39. };
  40. $GameCanvas = %foo;
  41. // Set the window title
  42. if (isObject(Canvas))
  43. Canvas.setWindowTitle(getEngineName() @ " - " @ $appName);
  44. return true;
  45. }
  46. // Display the optional commandline arguements
  47. $displayHelp = false;
  48. // Use these to record and play back crashes
  49. //saveJournal("editorOnFileQuitCrash.jrn");
  50. //playJournal("editorOnFileQuitCrash.jrn");
  51. //------------------------------------------------------------------------------
  52. // Check if a script file exists, compiled or not.
  53. function isScriptFile(%path)
  54. {
  55. if( isFile(%path @ ".dso") || isFile(%path) )
  56. return true;
  57. return false;
  58. }
  59. //------------------------------------------------------------------------------
  60. // Process command line arguments
  61. exec("core/parseArgs.cs");
  62. $isDedicated = false;
  63. $dirCount = 2;
  64. $userDirs = $defaultGame @ ";art;levels";
  65. // load tools scripts if we're a tool build
  66. if (isToolBuild())
  67. $userDirs = "tools;" @ $userDirs;
  68. // Parse the executable arguments with the standard
  69. // function from core/main.cs
  70. defaultParseArgs();
  71. if($dirCount == 0) {
  72. $userDirs = $defaultGame;
  73. $dirCount = 1;
  74. }
  75. //-----------------------------------------------------------------------------
  76. // Display a splash window immediately to improve app responsiveness before
  77. // engine is initialized and main window created
  78. if (!$isDedicated)
  79. displaySplashWindow();
  80. //-----------------------------------------------------------------------------
  81. // The displayHelp, onStart, onExit and parseArgs function are overriden
  82. // by mod packages to get hooked into initialization and cleanup.
  83. function onStart()
  84. {
  85. // Default startup function
  86. }
  87. function onExit()
  88. {
  89. // OnExit is called directly from C++ code, whereas onStart is
  90. // invoked at the end of this file.
  91. }
  92. function parseArgs()
  93. {
  94. // Here for mod override, the arguments have already
  95. // been parsed.
  96. }
  97. function compileFiles(%pattern)
  98. {
  99. %path = filePath(%pattern);
  100. %saveDSO = $Scripts::OverrideDSOPath;
  101. %saveIgnore = $Scripts::ignoreDSOs;
  102. $Scripts::OverrideDSOPath = %path;
  103. $Scripts::ignoreDSOs = false;
  104. %mainCsFile = makeFullPath("main.cs");
  105. for (%file = findFirstFileMultiExpr(%pattern); %file !$= ""; %file = findNextFileMultiExpr(%pattern))
  106. {
  107. // we don't want to try and compile the primary main.cs
  108. if(%mainCsFile !$= %file)
  109. compile(%file, true);
  110. }
  111. $Scripts::OverrideDSOPath = %saveDSO;
  112. $Scripts::ignoreDSOs = %saveIgnore;
  113. }
  114. if($compileAll)
  115. {
  116. echo(" --- Compiling all files ---");
  117. compileFiles("*.cs");
  118. compileFiles("*.gui");
  119. compileFiles("*.ts");
  120. echo(" --- Exiting after compile ---");
  121. quit();
  122. }
  123. if($compileTools)
  124. {
  125. echo(" --- Compiling tools scritps ---");
  126. compileFiles("tools/*.cs");
  127. compileFiles("tools/*.gui");
  128. compileFiles("tools/*.ts");
  129. echo(" --- Exiting after compile ---");
  130. quit();
  131. }
  132. package Help {
  133. function onExit() {
  134. // Override onExit when displaying help
  135. }
  136. };
  137. function displayHelp() {
  138. activatePackage(Help);
  139. // Notes on logmode: console logging is written to console.log.
  140. // -log 0 disables console logging.
  141. // -log 1 appends to existing logfile; it also closes the file
  142. // (flushing the write buffer) after every write.
  143. // -log 2 overwrites any existing logfile; it also only closes
  144. // the logfile when the application shuts down. (default)
  145. error(
  146. "Torque Demo command line options:\n"@
  147. " -log <logmode> Logging behavior; see main.cs comments for details\n"@
  148. " -game <game_name> Reset list of mods to only contain <game_name>\n"@
  149. " <game_name> Works like the -game argument\n"@
  150. " -dir <dir_name> Add <dir_name> to list of directories\n"@
  151. " -console Open a separate console\n"@
  152. " -jSave <file_name> Record a journal\n"@
  153. " -jPlay <file_name> Play back a journal\n"@
  154. " -help Display this help message\n"
  155. );
  156. }
  157. //--------------------------------------------------------------------------
  158. // Default to a new logfile each session.
  159. if( !$logModeSpecified )
  160. {
  161. if( $platform !$= "xbox" && $platform !$= "xenon" )
  162. setLogMode(6);
  163. }
  164. // Get the first dir on the list, which will be the last to be applied... this
  165. // does not modify the list.
  166. nextToken($userDirs, currentMod, ";");
  167. // Execute startup scripts for each mod, starting at base and working up
  168. function loadDir(%dir)
  169. {
  170. pushback($userDirs, %dir, ";");
  171. if (isScriptFile(%dir @ "/main.cs"))
  172. exec(%dir @ "/main.cs");
  173. }
  174. echo("--------- Loading DIRS ---------");
  175. function loadDirs(%dirPath)
  176. {
  177. %dirPath = nextToken(%dirPath, token, ";");
  178. if (%dirPath !$= "")
  179. loadDirs(%dirPath);
  180. if(exec(%token @ "/main.cs") != true)
  181. {
  182. error("Error: Unable to find specified directory: " @ %token );
  183. $dirCount--;
  184. }
  185. }
  186. loadDirs($userDirs);
  187. echo("");
  188. if($dirCount == 0) {
  189. enableWinConsole(true);
  190. error("Error: Unable to load any specified directories");
  191. quit();
  192. }
  193. // Parse the command line arguments
  194. echo("--------- Parsing Arguments ---------");
  195. parseArgs();
  196. // Either display the help message or startup the app.
  197. if ($displayHelp) {
  198. enableWinConsole(true);
  199. displayHelp();
  200. quit();
  201. }
  202. else {
  203. onStart();
  204. echo("Engine initialized...");
  205. if( !$isDedicated )
  206. {
  207. // As we know at this point that the initial load is complete,
  208. // we can hide any splash screen we have, and show the canvas.
  209. // This keeps things looking nice, instead of having a blank window
  210. closeSplashWindow();
  211. Canvas.showWindow();
  212. }
  213. // Auto-load on the 360
  214. if( $platform $= "xenon" )
  215. {
  216. %mission = "levels/Empty Terrain.mis";
  217. echo("Xbox360 Autoloading level: '" @ %mission @ "'");
  218. if ($pref::HostMultiPlayer)
  219. %serverType = "MultiPlayer";
  220. else
  221. %serverType = "SinglePlayer";
  222. createAndConnectToLocalServer( %serverType, %mission );
  223. }
  224. }
  225. // Display an error message for unused arguments
  226. for ($i = 1; $i < $Game::argc; $i++) {
  227. if (!$argUsed[$i])
  228. error("Error: Unknown command line argument: " @ $Game::argv[$i]);
  229. }
  230. // Automatically start up the appropriate eidtor, if any
  231. if ($startWorldEditor) {
  232. Canvas.setCursor("DefaultCursor");
  233. Canvas.setContent(EditorChooseLevelGui);
  234. } else if ($startGUIEditor) {
  235. Canvas.setCursor("DefaultCursor");
  236. Canvas.setContent(EditorChooseGUI);
  237. }