consoleInternal.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. #ifndef _CONSOLEINTERNAL_H_
  23. #define _CONSOLEINTERNAL_H_
  24. #ifndef _STRINGFUNCTIONS_H_
  25. #include "core/strings/stringFunctions.h"
  26. #endif
  27. #ifndef _STRINGTABLE_H_
  28. #include "core/stringTable.h"
  29. #endif
  30. #ifndef _CONSOLETYPES_H
  31. #include "console/consoleTypes.h"
  32. #endif
  33. #ifndef _CONSOLEOBJECT_H_
  34. #include "console/simObject.h"
  35. #endif
  36. #ifndef _DATACHUNKER_H_
  37. #include "core/dataChunker.h"
  38. #endif
  39. #include "module.h"
  40. /// @ingroup console_system Console System
  41. /// @{
  42. struct FunctionDecl;
  43. class CodeBlock;
  44. class AbstractClassRep;
  45. /// A dictionary of function entries.
  46. ///
  47. /// Namespaces are used for dispatching calls in the console system.
  48. class Namespace
  49. {
  50. enum {
  51. MaxActivePackages = 512,
  52. };
  53. static U32 mNumActivePackages;
  54. static U32 mOldNumActivePackages;
  55. static StringTableEntry mActivePackages[MaxActivePackages];
  56. public:
  57. StringTableEntry mName;
  58. StringTableEntry mPackage;
  59. Namespace *mParent;
  60. Namespace *mNext;
  61. AbstractClassRep *mClassRep;
  62. U32 mRefCountToParent;
  63. const char* mUsage;
  64. /// Script defined usage strings need to be cleaned up. This
  65. /// field indicates whether or not the usage was set from script.
  66. bool mCleanUpUsage;
  67. /// A function entry in the namespace.
  68. struct Entry
  69. {
  70. enum
  71. {
  72. ScriptCallbackType = -3,
  73. GroupMarker = -2,
  74. InvalidFunctionType = -1,
  75. ConsoleFunctionType,
  76. StringCallbackType,
  77. IntCallbackType,
  78. FloatCallbackType,
  79. VoidCallbackType,
  80. BoolCallbackType
  81. };
  82. /// Link back to the namespace to which the entry belongs.
  83. Namespace* mNamespace;
  84. /// Next function entry in the hashtable link chain of the namespace.
  85. Entry* mNext;
  86. /// Name of this function.
  87. StringTableEntry mFunctionName;
  88. ///
  89. S32 mType;
  90. /// Min number of arguments expected by this function.
  91. S32 mMinArgs;
  92. /// Max number of arguments expected by this function. If zero,
  93. /// function takes an arbitrary number of arguments.
  94. S32 mMaxArgs;
  95. /// Name of the package to which this function belongs.
  96. StringTableEntry mPackage;
  97. /// Whether this function is included only in TORQUE_TOOLS builds.
  98. bool mToolOnly;
  99. /// Usage string for documentation.
  100. const char* mUsage;
  101. /// Extended console function information.
  102. ConsoleFunctionHeader* mHeader;
  103. /// The compiled script code if this is a script function.
  104. Con::Module* mModule;
  105. /// The offset in the compiled script code at which this function begins.
  106. U32 mFunctionOffset;
  107. /// If it's a script function, this is the line of the declaration in code.
  108. /// @note 0 for functions read from legacy DSOs that have no line number information.
  109. U32 mFunctionLineNumber;
  110. union CallbackUnion {
  111. StringCallback mStringCallbackFunc;
  112. IntCallback mIntCallbackFunc;
  113. VoidCallback mVoidCallbackFunc;
  114. FloatCallback mFloatCallbackFunc;
  115. BoolCallback mBoolCallbackFunc;
  116. const char *mGroupName;
  117. const char *mCallbackName;
  118. } cb;
  119. Entry();
  120. ///
  121. void clear();
  122. ///
  123. ConsoleValue execute(S32 argc, ConsoleValue* argv, SimObject* thisObj);
  124. /// Return a one-line documentation text string for the function.
  125. String getBriefDescription(String* outRemainingDocText = NULL) const;
  126. /// Get the auto-doc string for this function. This string does not included prototype information.
  127. String getDocString() const;
  128. /// Return a string describing the arguments the function takes including default argument values.
  129. String getArgumentsString() const;
  130. /// Return a full prototype string for the function including return type, function name,
  131. /// and arguments.
  132. String getPrototypeString() const;
  133. /// Return a minimalized prototype string for the function including return type, function name,
  134. /// and arguments.
  135. String getPrototypeSig() const;
  136. };
  137. Entry* mEntryList;
  138. Entry** mHashTable;
  139. U32 mHashSize;
  140. U32 mHashSequence; ///< @note The hash sequence is used by the autodoc console facility
  141. /// as a means of testing reference state.
  142. Namespace();
  143. ~Namespace();
  144. void addFunction(StringTableEntry name, Con::Module* cb, U32 functionOffset, const char* usage = NULL, U32 lineNumber = 0);
  145. void addCommand(StringTableEntry name, StringCallback, const char *usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL);
  146. void addCommand(StringTableEntry name, IntCallback, const char *usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL);
  147. void addCommand(StringTableEntry name, FloatCallback, const char *usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL);
  148. void addCommand(StringTableEntry name, VoidCallback, const char *usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL);
  149. void addCommand(StringTableEntry name, BoolCallback, const char *usage, S32 minArgs, S32 maxArgs, bool toolOnly = false, ConsoleFunctionHeader* header = NULL);
  150. void addScriptCallback(const char *funcName, const char *usage, ConsoleFunctionHeader* header = NULL);
  151. void markGroup(const char* name, const char* usage);
  152. char * lastUsage;
  153. /// Returns true if this namespace represents an engine defined
  154. /// class and is not just a script defined class namespace.
  155. bool isClass() const { return mClassRep && mClassRep->getNameSpace() == this; }
  156. void getEntryList(VectorPtr<Entry *> *);
  157. /// Return the name of this namespace.
  158. StringTableEntry getName() const { return mName; }
  159. /// Return the superordinate namespace to this namespace. Symbols are inherited from
  160. /// this namespace.
  161. Namespace* getParent() const { return mParent; }
  162. /// Return the topmost package in the parent hierarchy of this namespace
  163. /// that still refers to the same namespace. If packages are active and
  164. /// adding to this namespace, then they will be linked in-between the namespace
  165. /// they are adding to and its real parent namespace.
  166. Namespace* getPackageRoot()
  167. {
  168. Namespace* walk = this;
  169. while (walk->mParent && walk->mParent->mName == mName)
  170. walk = walk->mParent;
  171. return walk;
  172. }
  173. /// Return the package in which this namespace is defined.
  174. StringTableEntry getPackage() const { return mPackage; }
  175. /// Increase the count on the reference that this namespace
  176. /// holds to its parent.
  177. /// @note Must not be called on namespaces coming from packages.
  178. void incRefCountToParent()
  179. {
  180. AssertFatal(mPackage == NULL, "Namespace::incRefCountToParent - Must not be called on a namespace coming from a package!");
  181. mRefCountToParent++;
  182. }
  183. /// Decrease the count on the reference that this namespace
  184. /// holds to its parent.
  185. /// @note Must not be called on namespaces coming from packages.
  186. void decRefCountToParent()
  187. {
  188. unlinkClass(NULL);
  189. }
  190. Entry *lookup(StringTableEntry name);
  191. Entry *lookupRecursive(StringTableEntry name);
  192. Entry *createLocalEntry(StringTableEntry name);
  193. void buildHashTable();
  194. void clearEntries();
  195. bool classLinkTo(Namespace *parent);
  196. bool unlinkClass(Namespace *parent);
  197. void getUniqueEntryLists(Namespace *other, VectorPtr<Entry *> *outThisList, VectorPtr<Entry *> *outOtherList);
  198. const char *tabComplete(const char *prevText, S32 baseLen, bool fForward);
  199. static U32 mCacheSequence;
  200. static DataChunker mCacheAllocator;
  201. static DataChunker mAllocator;
  202. static void trashCache();
  203. static Namespace *mNamespaceList;
  204. static Namespace *mGlobalNamespace;
  205. static void init();
  206. static void shutdown();
  207. static Namespace *global();
  208. static Namespace *find(StringTableEntry name, StringTableEntry package = NULL);
  209. static void activatePackage(StringTableEntry name);
  210. static void deactivatePackage(StringTableEntry name);
  211. static void deactivatePackageStack(StringTableEntry name);
  212. static void dumpClasses(bool dumpScript = true, bool dumpEngine = true);
  213. static void dumpFunctions(bool dumpScript = true, bool dumpEngine = true);
  214. static void printNamespaceEntries(Namespace * g, bool dumpScript = true, bool dumpEngine = true);
  215. static void unlinkPackages();
  216. static void relinkPackages();
  217. static bool isPackage(StringTableEntry name);
  218. static U32 getActivePackagesCount();
  219. static StringTableEntry getActivePackage(U32 index);
  220. };
  221. typedef VectorPtr<Namespace::Entry *>::iterator NamespaceEntryListIterator;
  222. class Dictionary
  223. {
  224. public:
  225. struct Entry
  226. {
  227. friend class Dictionary;
  228. StringTableEntry name;
  229. Entry *nextEntry;
  230. typedef Signal<void()> NotifySignal;
  231. /// The optional notification signal called when
  232. /// a value is assigned to this variable.
  233. NotifySignal *notify;
  234. /// Usage doc string.
  235. const char* mUsage;
  236. /// Whether this is a constant that cannot be assigned to.
  237. bool mIsConstant;
  238. ConsoleValue value;
  239. public:
  240. Entry() {
  241. name = NULL;
  242. notify = NULL;
  243. nextEntry = NULL;
  244. mUsage = NULL;
  245. mIsConstant = false;
  246. mNext = NULL;
  247. }
  248. Entry(StringTableEntry name);
  249. ~Entry();
  250. Entry *mNext;
  251. void reset();
  252. inline ConsoleValue getValue() { return (value); }
  253. inline U32 getIntValue()
  254. {
  255. return value.getInt();
  256. }
  257. inline F32 getFloatValue()
  258. {
  259. return value.getFloat();
  260. }
  261. inline const char *getStringValue()
  262. {
  263. return value.getString();
  264. }
  265. void setIntValue(U32 val)
  266. {
  267. if (mIsConstant)
  268. {
  269. Con::errorf("Cannot assign value to constant '%s'.", name);
  270. return;
  271. }
  272. if (value.isConsoleType())
  273. {
  274. const char* dptr = Con::getData(TypeS32, &val, 0);
  275. Con::setData(value.type, value.dataPtr, 0, 1, &dptr, value.enumTable);
  276. }
  277. else
  278. {
  279. value.setInt(val);
  280. }
  281. // Fire off the notification if we have one.
  282. if (notify)
  283. notify->trigger();
  284. }
  285. void setFloatValue(F32 val)
  286. {
  287. if (mIsConstant)
  288. {
  289. Con::errorf("Cannot assign value to constant '%s'.", name);
  290. return;
  291. }
  292. if (value.isConsoleType())
  293. {
  294. const char* dptr = Con::getData(TypeF32, &val, 0);
  295. Con::setData(value.type, value.dataPtr, 0, 1, &dptr, value.enumTable);
  296. }
  297. else
  298. {
  299. value.setFloat(val);
  300. }
  301. // Fire off the notification if we have one.
  302. if (notify)
  303. notify->trigger();
  304. }
  305. void setStringValue(const char* val)
  306. {
  307. if (mIsConstant)
  308. {
  309. Con::errorf("Cannot assign value to constant '%s'.", name);
  310. return;
  311. }
  312. if (value.isConsoleType())
  313. {
  314. Con::setData(value.type, value.dataPtr, 0, 1, &val, value.enumTable);
  315. }
  316. else
  317. {
  318. value.setString(val);
  319. }
  320. // Fire off the notification if we have one.
  321. if (notify)
  322. notify->trigger();
  323. }
  324. };
  325. struct HashTableData
  326. {
  327. Dictionary* owner;
  328. S32 size;
  329. S32 count;
  330. Entry **data;
  331. FreeListChunker< Entry > mChunker;
  332. HashTableData(Dictionary* owner)
  333. : owner(owner), size(0), count(0), data(NULL) {}
  334. };
  335. HashTableData* hashTable;
  336. HashTableData ownHashTable;
  337. StringTableEntry scopeName;
  338. Namespace *scopeNamespace;
  339. Con::Module *module;
  340. U32 ip;
  341. Dictionary();
  342. ~Dictionary();
  343. Entry *lookup(StringTableEntry name);
  344. Entry *add(StringTableEntry name);
  345. void setState(Dictionary* ref = NULL);
  346. void remove(Entry *);
  347. void reset();
  348. void exportVariables(const char *varString, const char *fileName, bool append);
  349. void exportVariables(const char *varString, Vector<String> *names, Vector<String> *values);
  350. void deleteVariables(const char *varString);
  351. void setVariable(StringTableEntry name, const char *value);
  352. const char *getVariable(StringTableEntry name, bool *valid = NULL);
  353. S32 getIntVariable(StringTableEntry name, bool *valid = NULL);
  354. F32 getFloatVariable(StringTableEntry name, bool *entValid = NULL);
  355. U32 getCount() const
  356. {
  357. return hashTable->count;
  358. }
  359. bool isOwner() const
  360. {
  361. return hashTable->owner;
  362. }
  363. /// @see Con::addVariable
  364. Entry* addVariable(const char *name,
  365. S32 type,
  366. void *dataPtr,
  367. const char* usage);
  368. /// @see Con::removeVariable
  369. bool removeVariable(StringTableEntry name);
  370. /// @see Con::addVariableNotify
  371. void addVariableNotify(const char *name, const Con::NotifyDelegate &callback);
  372. /// @see Con::removeVariableNotify
  373. void removeVariableNotify(const char *name, const Con::NotifyDelegate &callback);
  374. /// Return the best tab completion for prevText, with the length
  375. /// of the pre-tab string in baseLen.
  376. const char *tabComplete(const char *prevText, S32 baseLen, bool);
  377. /// Run integrity checks for debugging.
  378. void validate();
  379. };
  380. struct ConsoleValueFrame
  381. {
  382. ConsoleValue* values;
  383. bool isReference;
  384. ConsoleValueFrame() : values(NULL), isReference(false)
  385. {}
  386. ConsoleValueFrame(ConsoleValue* vals, bool isRef)
  387. {
  388. values = vals;
  389. isReference = isRef;
  390. }
  391. };
  392. namespace Con
  393. {
  394. /// The current $instantGroup setting.
  395. extern String gInstantGroup;
  396. /// Global variable storage
  397. inline Dictionary gGlobalVars;
  398. typedef Dictionary ConsoleFrame;
  399. typedef Vector<ConsoleFrame*> ConsoleStack;
  400. inline ConsoleStack gFrameStack;
  401. inline ConsoleStack getFrameStack() { return gFrameStack; }
  402. inline void pushStackFrame(ConsoleFrame* frame) { gFrameStack.push_back(frame); }
  403. inline ConsoleFrame* popStackFrame() { ConsoleFrame* last = gFrameStack.last(); gFrameStack.pop_back(); return last; }
  404. inline ConsoleFrame* getCurrentStackFrame() { return getFrameStack().empty() ? NULL : gFrameStack.last(); }
  405. inline ConsoleFrame* getStackFrame(S32 idx) { return gFrameStack[idx]; }
  406. inline Vector<Con::Module*> gScriptModules;
  407. inline Vector<Con::Module*> getAllScriptModules() { return gScriptModules; }
  408. Con::Module* findScriptModuleForFile(const char* fileName);
  409. // Convenience functions for getting the execution context
  410. inline const char* getCurrentScriptModulePath() { return getCurrentStackFrame() && getCurrentStackFrame()->module ? getCurrentStackFrame()->module->getPath() : NULL; }
  411. inline const char* getCurrentScriptModuleName() { return getCurrentStackFrame() && getCurrentStackFrame()->module ? getCurrentStackFrame()->module->getName() : NULL; }
  412. inline Con::Module* getCurrentScriptModule() { return getCurrentStackFrame() ? getCurrentStackFrame()->module : NULL; }
  413. inline bool gTraceOn;
  414. }
  415. /// @}
  416. #endif