main.cs 7.8 KB

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