DebuggerScript.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. ===========================================================================
  3. Doom 3 GPL Source Code
  4. Copyright (C) 1999-2011 id Software LLC, a ZeniMax Media company.
  5. This file is part of the Doom 3 GPL Source Code (?Doom 3 Source Code?).
  6. Doom 3 Source Code is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10. Doom 3 Source Code is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with Doom 3 Source Code. If not, see <http://www.gnu.org/licenses/>.
  16. In addition, the Doom 3 Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 Source Code. If not, please request a copy in writing from id Software at the address below.
  17. If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
  18. ===========================================================================
  19. */
  20. #include "../../idlib/precompiled.h"
  21. #pragma hdrstop
  22. #include "DebuggerApp.h"
  23. #include "DebuggerScript.h"
  24. #include "../../game/script/Script_Program.h"
  25. #include "../../ui/Window.h"
  26. #include "../../ui/UserInterfaceLocal.h"
  27. /*
  28. ================
  29. rvDebuggerScript::rvDebuggerScript
  30. ================
  31. */
  32. rvDebuggerScript::rvDebuggerScript ( void )
  33. {
  34. mContents = NULL;
  35. mProgram = NULL;
  36. mInterface = NULL;
  37. }
  38. /*
  39. ================
  40. rvDebuggerScript::~rvDebuggerScript
  41. ================
  42. */
  43. rvDebuggerScript::~rvDebuggerScript ( void )
  44. {
  45. Unload ( );
  46. }
  47. /*
  48. ================
  49. rvDebuggerScript::Unload
  50. Unload the script from memory
  51. ================
  52. */
  53. void rvDebuggerScript::Unload ( void )
  54. {
  55. delete[] mContents;
  56. if ( mInterface )
  57. {
  58. delete mInterface;
  59. }
  60. else
  61. {
  62. delete mProgram;
  63. }
  64. mContents = NULL;
  65. mProgram = NULL;
  66. mInterface = NULL;
  67. }
  68. /*
  69. ================
  70. rvDebuggerScript::Load
  71. Loads the debugger script and attempts to compile it using the method
  72. appropriate for the file being loaded. If the script cant be compiled
  73. the loading of the script fails
  74. ================
  75. */
  76. bool rvDebuggerScript::Load ( const char* filename )
  77. {
  78. void* buffer;
  79. int size;
  80. // Unload the script before reloading it
  81. Unload ( );
  82. // Cache the filename used to load the script
  83. mFilename = filename;
  84. // Read in the file
  85. size = fileSystem->ReadFile ( filename, &buffer, &mModifiedTime );
  86. if ( buffer == NULL )
  87. {
  88. return false;
  89. }
  90. // Copy the buffer over
  91. mContents = new char [ size + 1 ];
  92. memcpy ( mContents, buffer, size );
  93. mContents[size] = 0;
  94. // Cleanup
  95. fileSystem->FreeFile ( buffer );
  96. // Now compile the script so we can tell what a valid line is, etc.. If its
  97. // a gui file then we need to parse it using the userinterface system rather
  98. // than the normal script compiler.
  99. try
  100. {
  101. // Parse the script using the script compiler
  102. mProgram = new idProgram;
  103. mProgram->BeginCompilation ( );
  104. mProgram->CompileFile ( SCRIPT_DEFAULT );
  105. //BSM Nerve: Loads a game specific main script file
  106. idStr gamedir = cvarSystem->GetCVarString( "fs_game" );
  107. if(gamedir.Length() > 0) {
  108. idStr scriptFile = va("script/%s_main.script", gamedir.c_str());
  109. if(fileSystem->ReadFile(scriptFile.c_str(), NULL) > 0) {
  110. mProgram.CompileFile(scriptFile.c_str());
  111. }
  112. }
  113. // Make sure the file isnt already compiled before trying to compile it again
  114. for ( int f = mProgram->NumFilenames() - 1; f >= 0; f -- )
  115. {
  116. idStr qpath;
  117. qpath = fileSystem->OSPathToRelativePath ( mProgram->GetFilename ( f ) );
  118. qpath.BackSlashesToSlashes ( );
  119. if ( !qpath.Cmp ( filename ) )
  120. {
  121. break;
  122. }
  123. }
  124. if ( f < 0 )
  125. {
  126. mProgram->CompileText ( filename, mContents, false );
  127. }
  128. mProgram->FinishCompilation ( );
  129. }
  130. catch ( idException& )
  131. {
  132. // Failed to parse the script so fail to load the file
  133. delete mProgram;
  134. mProgram = NULL;
  135. delete[] mContents;
  136. mContents = NULL;
  137. // TODO: Should cache the error for the dialog box
  138. return false;
  139. }
  140. return true;
  141. }
  142. /*
  143. ================
  144. rvDebuggerScript::Reload
  145. Reload the contents of the script
  146. ================
  147. */
  148. bool rvDebuggerScript::Reload ( void )
  149. {
  150. return Load ( mFilename );
  151. }
  152. /*
  153. ================
  154. rvDebuggerScript::IsValidLine
  155. Determines whether or not the given line number within the script is a valid line of code
  156. ================
  157. */
  158. bool rvDebuggerScript::IsLineCode ( int linenumber )
  159. {
  160. int i;
  161. assert ( mProgram );
  162. // Run through all the statements in the program and see if any match the
  163. // linenumber that we are checking.
  164. for ( i = 0; i < mProgram->NumStatements ( ); i ++ )
  165. {
  166. if ( mProgram->GetStatement ( i ).linenumber == linenumber )
  167. {
  168. return true;
  169. }
  170. }
  171. return false;
  172. }
  173. /*
  174. ================
  175. rvDebuggerScript::IsFileModified
  176. Determines whether or not the file loaded for this script has been modified since
  177. it was loaded.
  178. ================
  179. */
  180. bool rvDebuggerScript::IsFileModified ( bool updateTime )
  181. {
  182. ID_TIME_T t;
  183. bool result = false;
  184. // Grab the filetime and shut the file down
  185. fileSystem->ReadFile ( mFilename, NULL, &t );
  186. // Has the file been modified?
  187. if ( t > mModifiedTime )
  188. {
  189. result = true;
  190. }
  191. // If updateTime is true then we will update the modified time
  192. // stored in the script with the files current modified time
  193. if ( updateTime )
  194. {
  195. mModifiedTime = t;
  196. }
  197. return result;
  198. }