consoleXMLExport.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #include "console/consoleInternal.h"
  23. #include "console/consoleObject.h"
  24. #include "console/SimXMLDocument.h"
  25. #include "console/consoleXMLExport.h"
  26. #if 0
  27. namespace Con {
  28. XMLExport::XMLExport()
  29. {
  30. mXML = NULL;
  31. }
  32. XMLExport::~XMLExport()
  33. {
  34. }
  35. void XMLExport::exportBaseTypes()
  36. {
  37. mXML->pushNewElement("BaseTypes");
  38. ConsoleBaseType *walk = ConsoleBaseType::getListHead();
  39. while( walk != NULL )
  40. {
  41. mXML->pushNewElement("BaseType");
  42. mXML->setAttribute("name", walk->getTypeName());
  43. mXML->setAttribute("id", avar("%i",walk->getTypeID()));
  44. mXML->setAttribute("size", avar("%i",walk->getTypeSize()));
  45. mXML->setAttribute("doc", walk->getDocString() ? walk->getDocString() : "" );
  46. mXML->popElement(); // Basetype
  47. walk = walk->getListNext();
  48. }
  49. mXML->popElement(); // Basetypes
  50. }
  51. void XMLExport::exportEntryTypes()
  52. {
  53. const char *typeNames [] = {
  54. "ScriptCallbackType", "GroupMarker", "OverloadMarker", "InvalidFunctionType",
  55. "ConsoleFunctionType", "StringCallbackType", "IntCallbackType", "FloatCallbackType",
  56. "VoidCallbackType", "BoolCallbackType"
  57. };
  58. S32 typeIds [] = {
  59. Namespace::Entry::ScriptCallbackType, Namespace::Entry::GroupMarker, Namespace::Entry::InvalidFunctionType,
  60. Namespace::Entry::ConsoleFunctionType, Namespace::Entry::StringCallbackType, Namespace::Entry::IntCallbackType, Namespace::Entry::FloatCallbackType,
  61. Namespace::Entry::VoidCallbackType, Namespace::Entry::BoolCallbackType
  62. };
  63. mXML->pushNewElement("EntryTypes");
  64. S32 numElements = sizeof(typeIds) / sizeof ( S32);
  65. for (S32 i = 0; i < numElements; i++)
  66. {
  67. mXML->pushNewElement("EntryType");
  68. mXML->setAttribute("name", typeNames[i]);
  69. mXML->setAttribute("id", avar("%i", typeIds[i]));
  70. mXML->popElement();
  71. }
  72. mXML->popElement(); // EntryTypes
  73. }
  74. void XMLExport::exportNamespaces()
  75. {
  76. // keep track of which enumTables are in use
  77. Vector < const EnumTable*> enumTables;
  78. mXML->pushNewElement("Namespaces");
  79. for (Namespace *walk = Namespace::mNamespaceList; walk; walk = walk->mNext)
  80. {
  81. if ( walk->mName && !walk->isClass() )
  82. continue;
  83. const char *name = walk->mName ? walk->mName : "";
  84. mXML->pushNewElement("Namespace");
  85. mXML->setAttribute("name", name);
  86. Namespace *p = walk->mParent;
  87. mXML->pushNewElement("Parents");
  88. while (p)
  89. {
  90. if (p->mName == walk->mName)
  91. {
  92. p = p->mParent;
  93. continue;
  94. }
  95. const char* pname = p->mName ? p->mName : "";
  96. mXML->pushNewElement("Parent");
  97. mXML->setAttribute("name", pname);
  98. mXML->popElement(); // Parent
  99. p = p->mParent;
  100. }
  101. mXML->popElement(); // Parents
  102. // Entries (Engine/Script Methods/Functions)
  103. mXML->pushNewElement("Entries");
  104. Namespace::Entry *entry;
  105. VectorPtr<Namespace::Entry *> vec;
  106. walk->getEntryList(&vec);
  107. for( NamespaceEntryListIterator compItr = vec.begin(); compItr != vec.end(); compItr++ )
  108. {
  109. entry = *compItr;
  110. if (entry->mNamespace != walk)
  111. continue;
  112. if (entry->mNamespace->mName != walk->mName)
  113. continue;
  114. mXML->pushNewElement("Entry");
  115. //consistently name functions
  116. char functionName[512];
  117. dSprintf(functionName, 512, entry->mFunctionName);
  118. functionName[0] = dTolower(functionName[0]);
  119. S32 minArgs = entry->mMinArgs;
  120. S32 maxArgs = entry->mMaxArgs;
  121. if (maxArgs < minArgs)
  122. maxArgs = minArgs;
  123. mXML->setAttribute("name", functionName);
  124. mXML->setAttribute("minArgs", avar("%i", minArgs));
  125. mXML->setAttribute("maxArgs", avar("%i", maxArgs));
  126. const char* usage = "";
  127. if (entry->mUsage && entry->mUsage[0])
  128. usage = entry->mUsage;
  129. mXML->setAttribute("usage", usage);
  130. mXML->setAttribute("package", entry->mPackage ? entry->mPackage : "");
  131. mXML->setAttribute("entryType", avar("%i", entry->mType));
  132. mXML->popElement(); // Entry
  133. }
  134. mXML->popElement(); // Entries
  135. // Fields
  136. mXML->pushNewElement("Fields");
  137. AbstractClassRep *rep = walk->mClassRep;
  138. Vector<U32> classFields;
  139. if (rep)
  140. {
  141. AbstractClassRep *parentRep = rep->getParentClass();
  142. const AbstractClassRep::FieldList& flist = rep->mFieldList;
  143. for(U32 i = 0; i < flist.size(); i++)
  144. {
  145. if (parentRep)
  146. {
  147. if (parentRep->findField(flist[i].pFieldname))
  148. continue;
  149. }
  150. classFields.push_back(i);
  151. }
  152. for(U32 i = 0; i < classFields.size(); i++)
  153. {
  154. U32 index = classFields[i];
  155. char fieldName[256];
  156. dSprintf(fieldName, 256, flist[index].pFieldname);
  157. //consistently name fields
  158. fieldName[0] = dToupper(fieldName[0]);
  159. mXML->pushNewElement("Field");
  160. mXML->setAttribute("name", fieldName);
  161. mXML->setAttribute("type", avar("%i", flist[index].type));
  162. // RD: temporarily deactivated; TypeEnum is no more; need to sync this up
  163. // if (flist[index].type == TypeEnum && flist[index].table && dStrlen(flist[index].table->name))
  164. // {
  165. // if (!enumTables.contains(flist[index].table))
  166. // enumTables.push_back(flist[index].table);
  167. //
  168. // mXML->setAttribute("enumTable", flist[index].table->name);
  169. //
  170. // }
  171. const char* pFieldDocs = "";
  172. if (flist[index].pFieldDocs && flist[index].pFieldDocs[0])
  173. pFieldDocs = flist[index].pFieldDocs;
  174. mXML->setAttribute("docs", pFieldDocs);
  175. mXML->setAttribute("elementCount", avar("%i", flist[index].elementCount));
  176. mXML->popElement(); // Field
  177. }
  178. }
  179. mXML->popElement(); // Fields
  180. mXML->popElement(); // Namespace
  181. }
  182. mXML->popElement(); // Namespaces
  183. mXML->pushNewElement("EnumTables");
  184. // write out the used EnumTables
  185. for (S32 i = 0; i < enumTables.size(); i++)
  186. {
  187. mXML->pushNewElement("EnumTable");
  188. const EnumTable* table = enumTables[i];
  189. mXML->setAttribute("name", table->name);
  190. mXML->setAttribute("firstFlag", avar("%i", table->firstFlag));
  191. mXML->setAttribute("mask", avar("%i", table->mask));
  192. mXML->pushNewElement("Enums");
  193. for (S32 j = 0; j < table->size; j++)
  194. {
  195. mXML->pushNewElement("Enum");
  196. mXML->setAttribute("name", table->table[j].label);
  197. mXML->setAttribute("index", avar("%i", table->table[j].index));
  198. mXML->popElement(); // Enum
  199. }
  200. mXML->popElement(); //Enums
  201. mXML->popElement(); // EnumTable
  202. }
  203. mXML->popElement(); // EnumTables
  204. }
  205. void XMLExport::exportXML(String& str)
  206. {
  207. mXML = new SimXMLDocument();
  208. mXML->registerObject();
  209. mXML->addHeader();
  210. mXML->pushNewElement("ConsoleXML");
  211. exportBaseTypes();
  212. exportEntryTypes();
  213. exportNamespaces();
  214. mXML->popElement(); // TorqueConsole
  215. mXML->saveToString(str);
  216. // If you're having trouble with the generated XML, you can dump to a file and inspect
  217. // mXML->saveFile("ConsoleExport.xml");
  218. mXML->deleteObject();
  219. }
  220. }; // namespace Con
  221. ConsoleFunction(consoleExportXML, const char*, 1, 1, "Exports console definition XML representation")
  222. {
  223. Con::XMLExport xmlExport;
  224. String xml;
  225. xmlExport.exportXML(xml);
  226. char* ret = Con::getReturnBuffer(xml.length() + 1);
  227. dStrcpy(ret, xml.c_str());
  228. return ret;
  229. }
  230. #endif