consoleXMLExport.cpp 9.4 KB

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