taml.cpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 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 "taml.h"
  23. #ifndef _TAML_XMLWRITER_H_
  24. #include "persistence/taml/xml/tamlXmlWriter.h"
  25. #endif
  26. #ifndef _TAML_XMLREADER_H_
  27. #include "persistence/taml/xml/tamlXmlReader.h"
  28. #endif
  29. #ifndef _TAML_XMLPARSER_H_
  30. #include "persistence/taml/xml/tamlXmlParser.h"
  31. #endif
  32. #ifndef _TAML_BINARYWRITER_H_
  33. #include "persistence/taml/binary/tamlBinaryWriter.h"
  34. #endif
  35. #ifndef _TAML_BINARYREADER_H_
  36. #include "persistence/taml/binary/tamlBinaryReader.h"
  37. #endif
  38. /*#ifndef _TAML_JSONWRITER_H_
  39. #include "taml/json/tamlJSONWriter.h"
  40. #endif
  41. #ifndef _TAML_JSONREADER_H_
  42. #include "taml/json/tamlJSONReader.h"
  43. #endif
  44. #ifndef _TAML_JSONPARSER_H_
  45. #include "taml/json/tamlJSONParser.h"
  46. #endif*/
  47. #ifndef _FRAMEALLOCATOR_H_
  48. #include "core/frameAllocator.h"
  49. #endif
  50. #ifndef _SIMBASE_H_
  51. #include "console/simBase.h"
  52. #endif
  53. #ifndef _MATHTYPES_H_
  54. #include "math/mathTypes.h"
  55. #endif
  56. #ifndef _MPOINT2_H_
  57. #include "math/mPoint2.h"
  58. #endif
  59. #ifndef _ASSET_BASE_H_
  60. #include "assets/assetBase.h"
  61. #endif
  62. // Script bindings.
  63. #include "taml_ScriptBinding.h"
  64. // Debug Profiling.
  65. #include "platform/profiler.h"
  66. //-----------------------------------------------------------------------------
  67. IMPLEMENT_CONOBJECT(Taml);
  68. //-----------------------------------------------------------------------------
  69. StringTableEntry tamlRefIdName = StringTable->insert("TamlId");
  70. StringTableEntry tamlRefToIdName = StringTable->insert("TamlRefId");
  71. StringTableEntry tamlNamedObjectName = StringTable->insert("Name");
  72. //-----------------------------------------------------------------------------
  73. typedef Taml::TamlFormatMode _TamlFormatMode;
  74. ImplementEnumType(_TamlFormatMode,
  75. "")
  76. {
  77. Taml::XmlFormat, "xml"
  78. },
  79. { Taml::BinaryFormat, "binary" }//,
  80. //{ Taml::JSONFormat, "json" }
  81. EndImplementEnumType;
  82. //-----------------------------------------------------------------------------
  83. Taml::TamlFormatMode Taml::getFormatModeEnum(const char* label)
  84. {
  85. // Search for Mnemonic.
  86. for (U32 i = 0; i < (sizeof(__TamlFormatMode::_sEnums) / sizeof(EnumTable::Value)); i++)
  87. {
  88. if (dStricmp(__TamlFormatMode::_sEnumTable[i].getName(), label) == 0)
  89. return (TamlFormatMode)__TamlFormatMode::_sEnumTable[i].getInt();
  90. }
  91. // Warn.
  92. Con::warnf("Taml::getFormatModeEnum() - Invalid format of '%s'.", label);
  93. return Taml::InvalidFormat;
  94. }
  95. //-----------------------------------------------------------------------------
  96. const char* Taml::getFormatModeDescription(const Taml::TamlFormatMode formatMode)
  97. {
  98. // Search for Mnemonic.
  99. for (U32 i = 0; i < (sizeof(__TamlFormatMode::_sEnums) / sizeof(EnumTable::Value)); i++)
  100. {
  101. if (__TamlFormatMode::_sEnumTable[i].getInt() == (S32)formatMode)
  102. return __TamlFormatMode::_sEnumTable[i].getName();
  103. }
  104. // Warn.
  105. Con::warnf("Taml::getFormatModeDescription() - Invalid format mode.");
  106. return StringTable->EmptyString();
  107. }
  108. //-----------------------------------------------------------------------------
  109. // The string-table-entries are set to string literals below because Taml is used in a static scope and the string-table cannot currently be used like that.
  110. Taml::Taml() :
  111. mMasterNodeId(0),
  112. mFormatMode(XmlFormat),
  113. mJSONStrict(true),
  114. mBinaryCompression(true),
  115. mWriteDefaults(false),
  116. mAutoFormatXmlExtension("taml"),
  117. mAutoFormat(true),
  118. mProgenitorUpdate(true),
  119. mAutoFormatBinaryExtension("baml"),
  120. mAutoFormatJSONExtension("json")
  121. {
  122. // Reset the file-path buffer.
  123. mFilePathBuffer[0] = 0;
  124. }
  125. //-----------------------------------------------------------------------------
  126. void Taml::initPersistFields()
  127. {
  128. docsURL;
  129. // Call parent.
  130. Parent::initPersistFields();
  131. addField("Format", TYPEID<_TamlFormatMode>(), Offset(mFormatMode, Taml), "The read/write format that should be used.");
  132. addField("JSONStrict", TypeBool, Offset(mBinaryCompression, Taml), "Whether to write JSON that is strictly compatible with RFC4627 or not.\n");
  133. addField("BinaryCompression", TypeBool, Offset(mBinaryCompression, Taml), "Whether ZIP compression is used on binary formatting or not.\n");
  134. addField("WriteDefaults", TypeBool, Offset(mWriteDefaults, Taml), "Whether to write static fields that are at their default or not.\n");
  135. addField("ProgenitorUpdate", TypeBool, Offset(mProgenitorUpdate, Taml), "Whether to update each type instances file-progenitor or not.\n");
  136. addField("AutoFormat", TypeBool, Offset(mAutoFormat, Taml), "Whether the format type is automatically determined by the filename extension or not.\n");
  137. addField("AutoFormatXmlExtension", TypeString, Offset(mAutoFormatXmlExtension, Taml), "When using auto-format, this is the extension (end of filename) used to detect the XML format.\n");
  138. addField("AutoFormatBinaryExtension", TypeString, Offset(mAutoFormatBinaryExtension, Taml), "When using auto-format, this is the extension (end of filename) used to detect the BINARY format.\n");
  139. addField("AutoFormatJSONExtension", TypeString, Offset(mAutoFormatJSONExtension, Taml), "When using auto-format, this is the extension (end of filename) used to detect the JSON format.\n");
  140. }
  141. //-----------------------------------------------------------------------------
  142. bool Taml::onAdd()
  143. {
  144. // Call parent.
  145. if (!Parent::onAdd())
  146. return false;
  147. // Set JSON strict mode.
  148. mJSONStrict = Con::getBoolVariable(TAML_JSON_STRICT_VARIBLE, true);
  149. // Reset the compilation.
  150. resetCompilation();
  151. return true;
  152. }
  153. //-----------------------------------------------------------------------------
  154. void Taml::onRemove()
  155. {
  156. // Reset the compilation.
  157. resetCompilation();
  158. // Call parent.
  159. Parent::onRemove();
  160. }
  161. //-----------------------------------------------------------------------------
  162. bool Taml::write(SimObject* pSimObject, const char* pFilename)
  163. {
  164. // Debug Profiling.
  165. PROFILE_SCOPE(Taml_Write);
  166. // Sanity!
  167. AssertFatal(pSimObject != NULL, "Cannot write a NULL object.");
  168. AssertFatal(pFilename != NULL, "Cannot write to a NULL filename.");
  169. // Expand the file-name into the file-path buffer unless we're a secure VFS
  170. #ifndef TORQUE_SECURE_VFS
  171. Con::expandToolScriptFilename(mFilePathBuffer, sizeof(mFilePathBuffer), pFilename);
  172. #else
  173. dMemset(mFilePathBuffer, 0x00, sizeof(mFilePathBuffer));
  174. dMemcpy(mFilePathBuffer, pFilename, dStrlen(pFilename));
  175. #endif
  176. FileStream stream;
  177. // File opened?
  178. if (!stream.open(mFilePathBuffer, Torque::FS::File::Write))
  179. {
  180. // No, so warn.
  181. Con::warnf("Taml::writeFile() - Could not open filename '%s' for write.", mFilePathBuffer);
  182. return false;
  183. }
  184. // Get the file auto-format mode.
  185. const TamlFormatMode formatMode = getFileAutoFormatMode(mFilePathBuffer);
  186. // Reset the compilation.
  187. resetCompilation();
  188. // Write object.
  189. const bool status = write(stream, pSimObject, formatMode);
  190. // Close file.
  191. stream.close();
  192. // Reset the compilation.
  193. resetCompilation();
  194. return status;
  195. }
  196. //-----------------------------------------------------------------------------
  197. SimObject* Taml::read(const char* pFilename)
  198. {
  199. // Debug Profiling.
  200. PROFILE_SCOPE(Taml_Read);
  201. // Sanity!
  202. AssertFatal(pFilename != NULL, "Cannot read from a NULL filename.");
  203. // Expand the file-name into the file-path buffer.
  204. Con::expandScriptFilename(mFilePathBuffer, sizeof(mFilePathBuffer), pFilename);
  205. FileStream stream;
  206. // File opened?
  207. if (!stream.open(mFilePathBuffer, Torque::FS::File::Read))
  208. {
  209. // No, so warn.
  210. Con::warnf("Taml::read() - Could not open filename '%s' for read.", mFilePathBuffer);
  211. return NULL;
  212. }
  213. // Get the file auto-format mode.
  214. const TamlFormatMode formatMode = getFileAutoFormatMode(mFilePathBuffer);
  215. // Reset the compilation.
  216. resetCompilation();
  217. // Write object.
  218. SimObject* pSimObject = read(stream, formatMode);
  219. // Close file.
  220. stream.close();
  221. // Reset the compilation.
  222. resetCompilation();
  223. // Did we generate an object?
  224. if (pSimObject == NULL)
  225. {
  226. // No, so warn.
  227. Con::warnf("Taml::read() - Failed to load an object from the file '%s'.", mFilePathBuffer);
  228. }
  229. else
  230. {
  231. pSimObject->onPostAdd();
  232. }
  233. return pSimObject;
  234. }
  235. //-----------------------------------------------------------------------------
  236. bool Taml::write(FileStream& stream, SimObject* pSimObject, const TamlFormatMode formatMode)
  237. {
  238. // Sanity!
  239. AssertFatal(pSimObject != NULL, "Cannot write a NULL object.");
  240. // Compile nodes.
  241. TamlWriteNode* pRootNode = compileObject(pSimObject);
  242. // Format appropriately.
  243. switch (formatMode)
  244. {
  245. /// Xml.
  246. case XmlFormat:
  247. {
  248. // Create writer.
  249. TamlXmlWriter writer(this);
  250. // Write.
  251. return writer.write(stream, pRootNode);
  252. }
  253. /// Binary.
  254. case BinaryFormat:
  255. {
  256. // Create writer.
  257. TamlBinaryWriter writer(this);
  258. // Write.
  259. return writer.write(stream, pRootNode, mBinaryCompression);
  260. }
  261. /// JSON.
  262. case JSONFormat:
  263. {
  264. // Create writer.
  265. //TamlJSONWriter writer( this );
  266. // Write.
  267. //return writer.write( stream, pRootNode );
  268. return false;
  269. }
  270. /// Invalid.
  271. case InvalidFormat:
  272. {
  273. // Warn.
  274. Con::warnf("Taml::write() - Cannot write, invalid format.");
  275. return false;
  276. }
  277. }
  278. // Warn.
  279. Con::warnf("Taml::write() - Unknown format.");
  280. return false;
  281. }
  282. //-----------------------------------------------------------------------------
  283. SimObject* Taml::read(FileStream& stream, const TamlFormatMode formatMode)
  284. {
  285. // Format appropriately.
  286. switch (formatMode)
  287. {
  288. /// Xml.
  289. case XmlFormat:
  290. {
  291. // Create reader.
  292. TamlXmlReader reader(this);
  293. // Read.
  294. return reader.read(stream);
  295. }
  296. /// Binary.
  297. case BinaryFormat:
  298. {
  299. // Create reader.
  300. TamlBinaryReader reader(this);
  301. // Read.
  302. return reader.read(stream);
  303. }
  304. /// JSON.
  305. case JSONFormat:
  306. {
  307. // Create reader.
  308. //TamlJSONReader reader( this );
  309. // Read.
  310. //return reader.read( stream );
  311. return NULL;
  312. }
  313. /// Invalid.
  314. case InvalidFormat:
  315. {
  316. // Warn.
  317. Con::warnf("Taml::read() - Cannot read, invalid format.");
  318. return NULL;
  319. }
  320. }
  321. // Warn.
  322. Con::warnf("Taml::read() - Unknown format.");
  323. return NULL;
  324. }
  325. //-----------------------------------------------------------------------------
  326. bool Taml::parse(const char* pFilename, TamlVisitor& visitor)
  327. {
  328. // Debug Profiling.
  329. PROFILE_SCOPE(Taml_Parse);
  330. // Sanity!
  331. AssertFatal(pFilename != NULL, "Taml::parse() - Cannot parse a NULL filename.");
  332. // Fetch format mode.
  333. const TamlFormatMode formatMode = getFileAutoFormatMode(pFilename);
  334. // Handle format mode appropriately.
  335. switch (formatMode)
  336. {
  337. case XmlFormat:
  338. {
  339. // Parse with the visitor.
  340. TamlXmlParser parser;
  341. // Are property changes needed but not supported?
  342. if (visitor.wantsPropertyChanges() && !parser.canChangeProperty())
  343. {
  344. // Yes, so warn.
  345. Con::warnf("Taml::parse() - Cannot parse '%s' file-type for filename '%s' as a specified visitor requires property changes which are not supported by the parser.", getFormatModeDescription(formatMode), pFilename);
  346. return false;
  347. }
  348. return parser.accept(pFilename, visitor);
  349. }
  350. case JSONFormat:
  351. {
  352. // Parse with the visitor.
  353. /*TamlJSONParser parser;
  354. // Are property changes needed but not supported?
  355. if ( visitor.wantsPropertyChanges() && !parser.canChangeProperty() )
  356. {
  357. // Yes, so warn.
  358. Con::warnf( "Taml::parse() - Cannot parse '%s' file-type for filename '%s' as a specified visitor requires property changes which are not supported by the parser.", getFormatModeDescription(formatMode), pFilename );
  359. return false;
  360. }
  361. return parser.accept( pFilename, visitor ); */
  362. return false;
  363. }
  364. case BinaryFormat:
  365. default:
  366. break;
  367. }
  368. // Warn.
  369. Con::warnf("Taml::parse() - Cannot parse '%s' file-type for filename '%s' as a required parser is not available.", getFormatModeDescription(formatMode), pFilename);
  370. return false;
  371. }
  372. //-----------------------------------------------------------------------------
  373. void Taml::resetCompilation(void)
  374. {
  375. // Debug Profiling.
  376. PROFILE_SCOPE(Taml_ResetCompilation);
  377. // Clear compiled nodes.
  378. for (typeNodeVector::iterator itr = mCompiledNodes.begin(); itr != mCompiledNodes.end(); ++itr)
  379. {
  380. // Fetch node.
  381. TamlWriteNode* pNode = (*itr);
  382. // Reset node.
  383. pNode->resetNode();
  384. // Delete node.
  385. delete pNode;
  386. }
  387. mCompiledNodes.clear();
  388. // Clear compiled objects.
  389. mCompiledObjects.clear();
  390. // Reset master node Id.
  391. mMasterNodeId = 0;
  392. }
  393. //-----------------------------------------------------------------------------
  394. Taml::TamlFormatMode Taml::getFileAutoFormatMode(const char* pFilename)
  395. {
  396. // Sanity!
  397. AssertFatal(pFilename != NULL, "Taml::getFileAutoFormatMode() - Cannot auto-format using a NULL filename.");
  398. // Is auto-format active?
  399. if (mAutoFormat)
  400. {
  401. // Yes, so fetch the extension lengths.
  402. const U32 xmlExtensionLength = dStrlen(mAutoFormatXmlExtension);
  403. const U32 binaryExtensionLength = dStrlen(mAutoFormatBinaryExtension);
  404. const U32 jsonExtensionLength = dStrlen(mAutoFormatJSONExtension);
  405. // Fetch filename length.
  406. const U32 filenameLength = dStrlen(pFilename);
  407. // Fetch end of filename,
  408. const char* pEndOfFilename = pFilename + filenameLength;
  409. // Check for the XML format.
  410. if (xmlExtensionLength <= filenameLength && dStricmp(pEndOfFilename - xmlExtensionLength, mAutoFormatXmlExtension) == 0)
  411. return Taml::XmlFormat;
  412. // Check for the Binary format.
  413. if (binaryExtensionLength <= filenameLength && dStricmp(pEndOfFilename - xmlExtensionLength, mAutoFormatBinaryExtension) == 0)
  414. return Taml::BinaryFormat;
  415. // Check for the XML format.
  416. if (jsonExtensionLength <= filenameLength && dStricmp(pEndOfFilename - jsonExtensionLength, mAutoFormatJSONExtension) == 0)
  417. return Taml::JSONFormat;
  418. }
  419. // Use the explicitly specified format mode.
  420. return mFormatMode;
  421. }
  422. //-----------------------------------------------------------------------------
  423. TamlWriteNode* Taml::compileObject(SimObject* pSimObject, const bool forceId)
  424. {
  425. // Debug Profiling.
  426. PROFILE_SCOPE(Taml_CompileObject);
  427. // Sanity!
  428. AssertFatal(pSimObject != NULL, "Taml::compileObject() - Cannot compile a NULL object.");
  429. // Fetch object Id.
  430. const SimObjectId objectId = pSimObject->getId();
  431. // Find a previously compiled node.
  432. typeCompiledHash::Iterator compiledItr = mCompiledObjects.find(objectId);
  433. // Have we already compiled this?
  434. if (compiledItr != mCompiledObjects.end())
  435. {
  436. // Yes, so sanity!
  437. AssertFatal(mCompiledNodes.size() != 0, "Taml::compileObject() - Found a compiled node at the root.");
  438. // Yes, so fetch node.
  439. TamlWriteNode* compiledNode = compiledItr->value;
  440. // Is a reference Id already present?
  441. if (compiledNode->mRefId == 0)
  442. {
  443. // No, so allocate one.
  444. compiledNode->mRefId = ++mMasterNodeId;
  445. }
  446. // Create write node.
  447. TamlWriteNode* pNewNode = new TamlWriteNode();
  448. pNewNode->set(pSimObject);
  449. // Set reference node.
  450. pNewNode->mRefToNode = compiledNode;
  451. // Push new node.
  452. mCompiledNodes.push_back(pNewNode);
  453. return pNewNode;
  454. }
  455. // No, so create write node.
  456. TamlWriteNode* pNewNode = new TamlWriteNode();
  457. pNewNode->set(pSimObject);
  458. // Is an Id being forced for this object?
  459. if (forceId)
  460. {
  461. // Yes, so allocate one.
  462. pNewNode->mRefId = ++mMasterNodeId;
  463. }
  464. // Push new node.
  465. mCompiledNodes.push_back(pNewNode);
  466. // Insert compiled object.
  467. mCompiledObjects.insertUnique(objectId, pNewNode);
  468. // Are there any Taml callbacks?
  469. if (pNewNode->mpTamlCallbacks != NULL)
  470. {
  471. // Yes, so call it.
  472. tamlPreWrite(pNewNode->mpTamlCallbacks);
  473. }
  474. // Compile static and dynamic fields.
  475. compileStaticFields(pNewNode);
  476. compileDynamicFields(pNewNode);
  477. // Compile children.
  478. compileChildren(pNewNode);
  479. // Compile custom state.
  480. compileCustomState(pNewNode);
  481. // Are there any Taml callbacks?
  482. if (pNewNode->mpTamlCallbacks != NULL)
  483. {
  484. // Yes, so call it.
  485. tamlPostWrite(pNewNode->mpTamlCallbacks);
  486. }
  487. return pNewNode;
  488. }
  489. //-----------------------------------------------------------------------------
  490. void Taml::compileStaticFields(TamlWriteNode* pTamlWriteNode)
  491. {
  492. // Debug Profiling.
  493. PROFILE_SCOPE(Taml_CompileStaticFields);
  494. // Sanity!
  495. AssertFatal(pTamlWriteNode != NULL, "Cannot compile static fields on a NULL node.");
  496. AssertFatal(pTamlWriteNode->mpSimObject != NULL, "Cannot compile static fields on a node with no object.");
  497. // Fetch object.
  498. SimObject* pSimObject = pTamlWriteNode->mpSimObject;
  499. // Fetch field list.
  500. const AbstractClassRep::FieldList& fieldList = pSimObject->getFieldList();
  501. // Fetch field count.
  502. const U32 fieldCount = fieldList.size();
  503. ConsoleObject* defaultConObject = NULL;
  504. SimObject* defaultObject = NULL;
  505. if (!getWriteDefaults())
  506. {
  507. // Create a default object of the same type
  508. defaultConObject = ConsoleObject::create(pSimObject->getClassName());
  509. if (!defaultConObject)
  510. return;
  511. defaultObject = dynamic_cast<SimObject*>(defaultConObject);
  512. }
  513. // ***Really*** shouldn't happen
  514. if (!defaultConObject || !defaultObject)
  515. return;
  516. // Iterate fields.
  517. U8 arrayDepth = 0;
  518. TamlCustomNode* currentArrayNode = NULL;
  519. for (U32 index = 0; index < fieldCount; ++index)
  520. {
  521. // Fetch field.
  522. const AbstractClassRep::Field* pField = &fieldList[index];
  523. // Ignore if field not appropriate.
  524. if (pField->type == AbstractClassRep::DeprecatedFieldType ||
  525. pField->type == AbstractClassRep::StartGroupFieldType ||
  526. pField->type == AbstractClassRep::EndGroupFieldType)
  527. continue;
  528. if (pField->type == AbstractClassRep::StartArrayFieldType)
  529. {
  530. TamlCustomNodes& pCustomNodes = pTamlWriteNode->mCustomNodes;
  531. currentArrayNode = pCustomNodes.addNode(pField->pGroupname);
  532. for (U16 idx = 0; idx < pField->elementCount; idx++)
  533. currentArrayNode->addNode(pField->pFieldname);
  534. arrayDepth++;
  535. continue;
  536. }
  537. if (pField->type == AbstractClassRep::EndArrayFieldType)
  538. {
  539. arrayDepth--;
  540. continue;
  541. }
  542. if (arrayDepth == 0 && pField->elementCount > 1)
  543. {
  544. TamlCustomNodes& pCustomNodes = pTamlWriteNode->mCustomNodes;
  545. char* niceFieldName = const_cast<char *>(pField->pFieldname);
  546. niceFieldName[0] = dToupper(niceFieldName[0]);
  547. String str_niceFieldName = String(niceFieldName);
  548. currentArrayNode = pCustomNodes.addNode(str_niceFieldName + "s");
  549. for (U16 idx = 0; idx < pField->elementCount; idx++)
  550. currentArrayNode->addNode(str_niceFieldName);
  551. }
  552. // Fetch fieldname.
  553. StringTableEntry fieldName = StringTable->insert(pField->pFieldname);
  554. // Fetch element count.
  555. const U32 elementCount = pField->elementCount;
  556. // Skip if the field should not be written.
  557. // For now, we only deal with non-array fields.
  558. if (elementCount == 1 &&
  559. pField->setDataFn != NULL &&
  560. (!getWriteDefaults() && pField->writeDataFn(pSimObject, fieldName) == false))
  561. continue;
  562. // Iterate elements.
  563. for (U32 elementIndex = 0; elementIndex < elementCount; ++elementIndex)
  564. {
  565. char indexBuffer[8];
  566. dSprintf(indexBuffer, 8, "%d", elementIndex);
  567. // Fetch object field value.
  568. const char* pFieldValue = pSimObject->getPrefixedDataField(fieldName, indexBuffer);
  569. if (!pFieldValue)
  570. pFieldValue = StringTable->EmptyString();
  571. U32 nBufferSize = dStrlen(pFieldValue) + 1;
  572. FrameTemp<char> valueCopy(nBufferSize);
  573. dStrcpy((char *)valueCopy, pFieldValue, nBufferSize);
  574. // Skip if field should not be written.
  575. if (!pSimObject->writeField(fieldName, valueCopy))
  576. continue;
  577. if (!getWriteDefaults())
  578. {
  579. //If the field hasn't been changed from the default value, then don't bother writing it out
  580. const char* fieldData = defaultObject->getDataField(fieldName, indexBuffer);
  581. if (fieldData && fieldData[0] != '\0' && dStricmp(fieldData, pFieldValue) == 0)
  582. continue;
  583. }
  584. // Reassign field value.
  585. pFieldValue = valueCopy;
  586. if (pField->type == TypeBool)
  587. pFieldValue = dAtob(pFieldValue) ? "true" : "false";
  588. // Detect and collapse relative path information
  589. char fnBuf[1024];
  590. if ((S32)pField->type == TypeFilename)
  591. {
  592. Con::collapseScriptFilename(fnBuf, 1024, pFieldValue);
  593. pFieldValue = fnBuf;
  594. }
  595. // Save field/value.
  596. if (currentArrayNode && (arrayDepth > 0 || pField->elementCount > 1))
  597. currentArrayNode->getChildren()[elementIndex]->addField(fieldName, pFieldValue);
  598. else
  599. {
  600. TamlWriteNode::FieldValuePair* pFieldValuePair = new TamlWriteNode::FieldValuePair(fieldName, pFieldValue);
  601. pTamlWriteNode->mFields.push_back(pFieldValuePair);
  602. }
  603. }
  604. }
  605. if (!getWriteDefaults())
  606. {
  607. // Cleanup our created default object
  608. delete defaultConObject;
  609. }
  610. }
  611. //-----------------------------------------------------------------------------
  612. static S32 QSORT_CALLBACK compareFieldEntries(const void* a, const void* b)
  613. {
  614. // Debug Profiling.
  615. PROFILE_SCOPE(Taml_CompareFieldEntries);
  616. SimFieldDictionary::Entry *fa = *((SimFieldDictionary::Entry **)a);
  617. SimFieldDictionary::Entry *fb = *((SimFieldDictionary::Entry **)b);
  618. return dStricmp(fa->slotName, fb->slotName);
  619. }
  620. //-----------------------------------------------------------------------------
  621. void Taml::compileDynamicFields(TamlWriteNode* pTamlWriteNode)
  622. {
  623. // Debug Profiling.
  624. PROFILE_SCOPE(Taml_CompileDynamicFields);
  625. // Sanity!
  626. AssertFatal(pTamlWriteNode != NULL, "Cannot compile dynamic fields on a NULL node.");
  627. AssertFatal(pTamlWriteNode->mpSimObject != NULL, "Cannot compile dynamic fields on a node with no object.");
  628. // Fetch object.
  629. SimObject* pSimObject = pTamlWriteNode->mpSimObject;
  630. // Fetch field dictionary.
  631. SimFieldDictionary* pFieldDictionary = pSimObject->getFieldDictionary();
  632. // Ignore if not writing dynamic fields.
  633. if (!pFieldDictionary || !pSimObject->getCanSaveDynamicFields())
  634. return;
  635. // Fetch field list.
  636. const AbstractClassRep::FieldList& fieldList = pSimObject->getFieldList();
  637. // Fetch field count.
  638. const U32 fieldCount = fieldList.size();
  639. Vector<SimFieldDictionary::Entry*> dynamicFieldList(__FILE__, __LINE__);
  640. // Ensure the dynamic field doesn't conflict with static field.
  641. for (U32 hashIndex = 0; hashIndex < SimFieldDictionary::HashTableSize; ++hashIndex)
  642. {
  643. for (SimFieldDictionary::Entry* pEntry = pFieldDictionary->mHashTable[hashIndex]; pEntry; pEntry = pEntry->next)
  644. {
  645. // Iterate static fields.
  646. U32 fieldIndex;
  647. for (fieldIndex = 0; fieldIndex < fieldCount; ++fieldIndex)
  648. {
  649. if (fieldList[fieldIndex].pFieldname == pEntry->slotName)
  650. break;
  651. }
  652. // Skip if found.
  653. if (fieldIndex != (U32)fieldList.size())
  654. continue;
  655. // Skip if not writing field.
  656. if (!pSimObject->writeField(pEntry->slotName, pEntry->value))
  657. continue;
  658. dynamicFieldList.push_back(pEntry);
  659. }
  660. }
  661. // Sort Entries to prevent version control conflicts
  662. if (dynamicFieldList.size() > 1)
  663. dQsort(dynamicFieldList.address(), dynamicFieldList.size(), sizeof(SimFieldDictionary::Entry*), compareFieldEntries);
  664. // Save the fields.
  665. for (Vector<SimFieldDictionary::Entry*>::iterator entryItr = dynamicFieldList.begin(); entryItr != dynamicFieldList.end(); ++entryItr)
  666. {
  667. // Fetch entry.
  668. SimFieldDictionary::Entry* pEntry = *entryItr;
  669. // Save field/value.
  670. TamlWriteNode::FieldValuePair* pFieldValuePair = new TamlWriteNode::FieldValuePair(pEntry->slotName, pEntry->value);
  671. pTamlWriteNode->mFields.push_back(pFieldValuePair);
  672. }
  673. }
  674. //-----------------------------------------------------------------------------
  675. void Taml::compileChildren(TamlWriteNode* pTamlWriteNode)
  676. {
  677. // Debug Profiling.
  678. PROFILE_SCOPE(Taml_CompileChildren);
  679. // Sanity!
  680. AssertFatal(pTamlWriteNode != NULL, "Cannot compile children on a NULL node.");
  681. AssertFatal(pTamlWriteNode->mpSimObject != NULL, "Cannot compile children on a node with no object.");
  682. // Fetch object.
  683. SimObject* pSimObject = pTamlWriteNode->mpSimObject;
  684. // Fetch the Taml children.
  685. TamlChildren* pChildren = dynamic_cast<TamlChildren*>(pSimObject);
  686. // Finish if object does not contain Taml children.
  687. if (pChildren == NULL || pChildren->getTamlChildCount() == 0)
  688. return;
  689. // Create children vector.
  690. pTamlWriteNode->mChildren = new typeNodeVector();
  691. // Fetch the child count.
  692. const U32 childCount = pChildren->getTamlChildCount();
  693. // Iterate children.
  694. for (U32 childIndex = 0; childIndex < childCount; childIndex++)
  695. {
  696. // Compile object.
  697. TamlWriteNode* pChildTamlWriteNode = compileObject(pChildren->getTamlChild(childIndex));
  698. // Save node.
  699. pTamlWriteNode->mChildren->push_back(pChildTamlWriteNode);
  700. }
  701. }
  702. //-----------------------------------------------------------------------------
  703. void Taml::compileCustomState(TamlWriteNode* pTamlWriteNode)
  704. {
  705. // Debug Profiling.
  706. PROFILE_SCOPE(Taml_CompileCustomProperties);
  707. // Sanity!
  708. AssertFatal(pTamlWriteNode != NULL, "Cannot compile custom state on a NULL node.");
  709. AssertFatal(pTamlWriteNode->mpSimObject != NULL, "Cannot compile custom state on a node with no object.");
  710. // Fetch the custom node on the write node.
  711. TamlCustomNodes& customNodes = pTamlWriteNode->mCustomNodes;
  712. // Are there any Taml callbacks?
  713. if (pTamlWriteNode->mpTamlCallbacks != NULL)
  714. {
  715. // Yes, so call it.
  716. tamlCustomWrite(pTamlWriteNode->mpTamlCallbacks, customNodes);
  717. }
  718. // Fetch custom nodes.
  719. const TamlCustomNodeVector& nodes = customNodes.getNodes();
  720. // Finish if no custom nodes to process.
  721. if (nodes.size() == 0)
  722. return;
  723. // Iterate custom properties.
  724. for (TamlCustomNodeVector::const_iterator customNodesItr = nodes.begin(); customNodesItr != nodes.end(); ++customNodesItr)
  725. {
  726. // Fetch the custom node.
  727. TamlCustomNode* pCustomNode = *customNodesItr;
  728. // Compile custom node state.
  729. compileCustomNodeState(pCustomNode);
  730. }
  731. }
  732. //-----------------------------------------------------------------------------
  733. void Taml::compileCustomNodeState(TamlCustomNode* pCustomNode)
  734. {
  735. // Sanity!
  736. AssertFatal(pCustomNode != NULL, "Taml: Cannot compile NULL custom node state.");
  737. // Fetch children.
  738. const TamlCustomNodeVector& children = pCustomNode->getChildren();
  739. // Fetch proxy object.
  740. SimObject* pProxyObject = pCustomNode->getProxyObject<SimObject>(false);
  741. // Do we have a proxy object?
  742. if (pProxyObject != NULL)
  743. {
  744. // Yes, so sanity!
  745. AssertFatal(children.size() == 0, "Taml: Cannot compile a proxy object on a custom node that has children.");
  746. // Yes, so compile it.
  747. // NOTE: We force an Id for custom compiled objects so we guarantee an Id. The reason for this is fairly
  748. // weak in that the XML parser currently has no way of distinguishing between a compiled object node
  749. // and a custom node. If the node has an Id or an Id-Ref then it's obviously an object and should be parsed as such.
  750. pCustomNode->setWriteNode(compileObject(pProxyObject, true));
  751. return;
  752. }
  753. // Finish if no children.
  754. if (children.size() == 0)
  755. return;
  756. // Iterate children.
  757. for (TamlCustomNodeVector::const_iterator childItr = children.begin(); childItr != children.end(); ++childItr)
  758. {
  759. // Fetch shape node.
  760. TamlCustomNode* pChildNode = *childItr;
  761. // Compile the child.
  762. compileCustomNodeState(pChildNode);
  763. }
  764. }
  765. //-----------------------------------------------------------------------------
  766. SimObject* Taml::createType(StringTableEntry typeName, const Taml* pTaml, const char* pProgenitorSuffix)
  767. {
  768. // Debug Profiling.
  769. PROFILE_SCOPE(Taml_CreateType);
  770. typedef HashTable<StringTableEntry, AbstractClassRep*> typeClassHash;
  771. static typeClassHash mClassMap;
  772. // Sanity!
  773. AssertFatal(typeName != NULL, "Taml: Type cannot be NULL");
  774. // Find type.
  775. typeClassHash::Iterator typeItr = mClassMap.find(typeName);
  776. // Found type?
  777. if (typeItr == mClassMap.end())
  778. {
  779. // No, so find type.
  780. AbstractClassRep* pClassRep = AbstractClassRep::getClassList();
  781. while (pClassRep)
  782. {
  783. // Is this the type?
  784. if (dStricmp(pClassRep->getClassName(), typeName) == 0)
  785. {
  786. // Yes, so insert it.
  787. typeItr = mClassMap.insertUnique(typeName, pClassRep);
  788. break;
  789. }
  790. // Next type.
  791. pClassRep = pClassRep->getNextClass();
  792. }
  793. // Did we find the type?
  794. if (typeItr == mClassMap.end())
  795. {
  796. // No, so warn and fail.
  797. Con::warnf("Taml: Failed to create type '%s' as such a registered type could not be found.", typeName);
  798. return NULL;
  799. }
  800. }
  801. // Create the object.
  802. ConsoleObject* pConsoleObject = typeItr->value->create();
  803. // NOTE: It is important that we don't register the object here as many objects rely on the fact that
  804. // fields are set prior to the object being registered. Registering here will invalid those assumptions.
  805. // Fetch the SimObject.
  806. SimObject* pSimObject = dynamic_cast<SimObject*>(pConsoleObject);
  807. // Was it a SimObject?
  808. if (pSimObject == NULL)
  809. {
  810. // No, so warn.
  811. Con::warnf("Taml: Failed to create type '%s' as it is not a SimObject.", typeName);
  812. // Destroy object and fail.
  813. delete pConsoleObject;
  814. return NULL;
  815. }
  816. // Are we updating the file-progenitor?
  817. if (pTaml->getProgenitorUpdate())
  818. {
  819. // Yes, so do we have a progenitor suffix?
  820. if (pProgenitorSuffix == NULL)
  821. {
  822. // No, so just set it to the progenitor file.
  823. pSimObject->setProgenitorFile(pTaml->getFilePathBuffer());
  824. }
  825. else
  826. {
  827. // Yes, so format the progenitor buffer.
  828. char progenitorBuffer[2048];
  829. dSprintf(progenitorBuffer, sizeof(progenitorBuffer), "%s,%s", pTaml->getFilePathBuffer(), pProgenitorSuffix);
  830. // Set the progenitor file.
  831. pSimObject->setProgenitorFile(progenitorBuffer);
  832. }
  833. }
  834. return pSimObject;
  835. }
  836. //-----------------------------------------------------------------------------
  837. tinyxml2::XMLElement* g__write_schema_attribute_element(const AbstractClassRep::Field& field, AbstractClassRep* pType,
  838. tinyxml2::XMLDocument& schemaDocument)
  839. {
  840. // Skip if not a data field.
  841. if (field.type == AbstractClassRep::DeprecatedFieldType ||
  842. field.type == AbstractClassRep::StartGroupFieldType ||
  843. field.type == AbstractClassRep::EndGroupFieldType)
  844. return NULL;
  845. // Skip if the field root is not this type.
  846. if (pType->findFieldRoot(field.pFieldname) != pType)
  847. return NULL;
  848. // Add attribute element.
  849. tinyxml2::XMLElement* pAttributeElement = schemaDocument.NewElement("xs:attribute");
  850. pAttributeElement->SetAttribute("name", field.pFieldname);
  851. // Handle the console type appropriately.
  852. const S32 fieldType = (S32)field.type;
  853. /*
  854. // Is the field an enumeration?
  855. if ( fieldType == TypeEnum )
  856. {
  857. // Yes, so add attribute type.
  858. tinyxml2::XMLElement* pAttributeSimpleTypeElement = schemaDocument.NewElement( "xs:simpleType" );
  859. pAttributeElement->LinkEndChild( pAttributeSimpleTypeElement );
  860. // Add restriction element.
  861. tinyxml2::XMLElement* pAttributeRestrictionElement = schemaDocument.NewElement( "xs:restriction" );
  862. pAttributeRestrictionElement->SetAttribute( "base", "xs:string" );
  863. pAttributeSimpleTypeElement->LinkEndChild( pAttributeRestrictionElement );
  864. // Yes, so fetch enumeration count.
  865. const S32 enumCount = field.table->size;
  866. // Iterate enumeration.
  867. for( S32 index = 0; index < enumCount; ++index )
  868. {
  869. // Add enumeration element.
  870. tinyxml2::XMLElement* pAttributeEnumerationElement = schemaDocument.NewElement( "xs:enumeration" );
  871. pAttributeEnumerationElement->SetAttribute( "value", field.table->table[index].label );
  872. pAttributeRestrictionElement->LinkEndChild( pAttributeEnumerationElement );
  873. }
  874. }
  875. else
  876. {*/
  877. // No, so assume it's a string type initially.
  878. const char* pFieldTypeDescription = "xs:string";
  879. // Handle known types.
  880. if (fieldType == TypeF32)
  881. {
  882. pFieldTypeDescription = "xs:float";
  883. }
  884. else if (fieldType == TypeS8 || fieldType == TypeS16 || fieldType == TypeS32)
  885. {
  886. pFieldTypeDescription = "xs:int";
  887. }
  888. else if (fieldType == TypeBool || fieldType == TypeFlag)
  889. {
  890. pFieldTypeDescription = "xs:boolean";
  891. }
  892. else if (fieldType == TypePoint2F)
  893. {
  894. pFieldTypeDescription = "Point2F_ConsoleType";
  895. }
  896. else if (fieldType == TypePoint2I)
  897. {
  898. pFieldTypeDescription = "Point2I_ConsoleType";
  899. }
  900. else if (fieldType == TypeRectI)
  901. {
  902. pFieldTypeDescription = "RectI_ConsoleType";
  903. }
  904. else if (fieldType == TypeRectF)
  905. {
  906. pFieldTypeDescription = "RectF_ConsoleType";
  907. }
  908. else if (fieldType == TypeColorF)
  909. {
  910. pFieldTypeDescription = "ColorF_ConsoleType";
  911. }
  912. else if (fieldType == TypeColorI)
  913. {
  914. pFieldTypeDescription = "ColorI_ConsoleType";
  915. }
  916. else if (fieldType == TypeAssetId/* ||
  917. fieldType == TypeImageAssetPtr ||
  918. fieldType == TypeAnimationAssetPtr ||
  919. fieldType == TypeAudioAssetPtr*/)
  920. {
  921. pFieldTypeDescription = "AssetId_ConsoleType";
  922. }
  923. // Set attribute type.
  924. pAttributeElement->SetAttribute("type", pFieldTypeDescription);
  925. //}
  926. pAttributeElement->SetAttribute("use", "optional");
  927. return pAttributeElement;
  928. }
  929. String g_sanitize_schema_element_name(String buffer)
  930. {
  931. return buffer.replace("(", "")
  932. .replace(")", "");
  933. }
  934. bool Taml::generateTamlSchema()
  935. {
  936. // Fetch any TAML Schema file reference.
  937. const char* pTamlSchemaFile = Con::getVariable(TAML_SCHEMA_VARIABLE);
  938. // Do we have a schema file reference?
  939. if (pTamlSchemaFile == NULL || *pTamlSchemaFile == 0)
  940. {
  941. // No, so warn.
  942. Con::warnf("Taml::generateTamlSchema() - Cannot write a TAML schema as no schema variable is set ('%s').", TAML_SCHEMA_VARIABLE);
  943. return false;
  944. }
  945. // Expand the file-name into the file-path buffer.
  946. char filePathBuffer[1024];
  947. Con::expandToolScriptFilename(filePathBuffer, sizeof(filePathBuffer), pTamlSchemaFile);
  948. FileStream stream;
  949. // File opened?
  950. /*if ( !stream.open( filePathBuffer, Torque::FS::File::Write ) )
  951. {
  952. // No, so warn.
  953. Con::warnf("Taml::GenerateTamlSchema() - Could not open filename '%s' for write.", filePathBuffer );
  954. return false;
  955. }*/
  956. // Create document.
  957. tinyxml2::XMLDocument schemaDocument;
  958. // Add declaration.
  959. tinyxml2::XMLDeclaration* schemaDeclaration = schemaDocument.NewDeclaration("xml version=\"1.0\" encoding=\"iso-8859-1\" standalone =\"no\"");
  960. schemaDocument.InsertEndChild(schemaDeclaration);
  961. // Add schema element.
  962. tinyxml2::XMLElement* pSchemaElement = schemaDocument.NewElement("xs:schema");
  963. pSchemaElement->SetAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema");
  964. schemaDocument.LinkEndChild(pSchemaElement);
  965. // Fetch class-rep root.
  966. AbstractClassRep* pRootType = AbstractClassRep::getClassList();
  967. // Fetch SimObject class rep.
  968. AbstractClassRep* pSimObjectType = AbstractClassRep::findClassRep("SimObject");
  969. // Sanity!
  970. AssertFatal(pSimObjectType != NULL, "Taml::GenerateTamlSchema() - Could not find SimObject class rep.");
  971. // Reset scratch state.
  972. char buffer[1024];
  973. HashTable<AbstractClassRep*, StringTableEntry> childGroups;
  974. // *************************************************************
  975. // Generate console type elements.
  976. // *************************************************************
  977. // Vector2.
  978. tinyxml2::XMLComment* pVector2Comment = schemaDocument.NewComment("Vector2 Console Type");
  979. pSchemaElement->LinkEndChild(pVector2Comment);
  980. tinyxml2::XMLElement* pVector2TypeElement = schemaDocument.NewElement("xs:simpleType");
  981. pVector2TypeElement->SetAttribute("name", "Vector2_ConsoleType");
  982. pSchemaElement->LinkEndChild(pVector2TypeElement);
  983. tinyxml2::XMLElement* pVector2ElementA = schemaDocument.NewElement("xs:restriction");
  984. pVector2ElementA->SetAttribute("base", "xs:string");
  985. pVector2TypeElement->LinkEndChild(pVector2ElementA);
  986. tinyxml2::XMLElement* pVector2ElementB = schemaDocument.NewElement("xs:pattern");
  987. pVector2ElementB->SetAttribute("value", "([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b");
  988. pVector2ElementA->LinkEndChild(pVector2ElementB);
  989. // Point2F.
  990. tinyxml2::XMLComment* pPoint2FComment = schemaDocument.NewComment("Point2F Console Type");
  991. pSchemaElement->LinkEndChild(pPoint2FComment);
  992. tinyxml2::XMLElement* pPoint2FTypeElement = schemaDocument.NewElement("xs:simpleType");
  993. pPoint2FTypeElement->SetAttribute("name", "Point2F_ConsoleType");
  994. pSchemaElement->LinkEndChild(pPoint2FTypeElement);
  995. tinyxml2::XMLElement* pPoint2FElementA = schemaDocument.NewElement("xs:restriction");
  996. pPoint2FElementA->SetAttribute("base", "xs:string");
  997. pPoint2FTypeElement->LinkEndChild(pPoint2FElementA);
  998. tinyxml2::XMLElement* pPoint2FElementB = schemaDocument.NewElement("xs:pattern");
  999. pPoint2FElementB->SetAttribute("value", "([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b");
  1000. pPoint2FElementA->LinkEndChild(pPoint2FElementB);
  1001. // Point2I.
  1002. tinyxml2::XMLComment* pPoint2IComment = schemaDocument.NewComment("Point2I Console Type");
  1003. pSchemaElement->LinkEndChild(pPoint2IComment);
  1004. tinyxml2::XMLElement* pPoint2ITypeElement = schemaDocument.NewElement("xs:simpleType");
  1005. pPoint2ITypeElement->SetAttribute("name", "Point2I_ConsoleType");
  1006. pSchemaElement->LinkEndChild(pPoint2ITypeElement);
  1007. tinyxml2::XMLElement* pPoint2IElementA = schemaDocument.NewElement("xs:restriction");
  1008. pPoint2IElementA->SetAttribute("base", "xs:string");
  1009. pPoint2ITypeElement->LinkEndChild(pPoint2IElementA);
  1010. tinyxml2::XMLElement* pPoint2IElementB = schemaDocument.NewElement("xs:pattern");
  1011. pPoint2IElementB->SetAttribute("value", "[-]?[0-9]* [-]?[0-9]*");
  1012. pPoint2IElementA->LinkEndChild(pPoint2IElementB);
  1013. // b2AABB.
  1014. tinyxml2::XMLComment* pb2AABBComment = schemaDocument.NewComment("b2AABB Console Type");
  1015. pSchemaElement->LinkEndChild(pb2AABBComment);
  1016. tinyxml2::XMLElement* pb2AABBTypeElement = schemaDocument.NewElement("xs:simpleType");
  1017. pb2AABBTypeElement->SetAttribute("name", "b2AABB_ConsoleType");
  1018. pSchemaElement->LinkEndChild(pb2AABBTypeElement);
  1019. tinyxml2::XMLElement* pb2AABBElementA = schemaDocument.NewElement("xs:restriction");
  1020. pb2AABBElementA->SetAttribute("base", "xs:string");
  1021. pb2AABBTypeElement->LinkEndChild(pb2AABBElementA);
  1022. tinyxml2::XMLElement* pb2AABBElementB = schemaDocument.NewElement("xs:pattern");
  1023. pb2AABBElementB->SetAttribute("value", "([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b");
  1024. pb2AABBElementA->LinkEndChild(pb2AABBElementB);
  1025. // RectI.
  1026. tinyxml2::XMLComment* pRectIComment = schemaDocument.NewComment("RectI Console Type");
  1027. pSchemaElement->LinkEndChild(pRectIComment);
  1028. tinyxml2::XMLElement* pRectITypeElement = schemaDocument.NewElement("xs:simpleType");
  1029. pRectITypeElement->SetAttribute("name", "RectI_ConsoleType");
  1030. pSchemaElement->LinkEndChild(pRectITypeElement);
  1031. tinyxml2::XMLElement* pRectIElementA = schemaDocument.NewElement("xs:restriction");
  1032. pRectIElementA->SetAttribute("base", "xs:string");
  1033. pRectITypeElement->LinkEndChild(pRectIElementA);
  1034. tinyxml2::XMLElement* pRectIElementB = schemaDocument.NewElement("xs:pattern");
  1035. pRectIElementB->SetAttribute("value", "[-]?[0-9]* [-]?[0-9]* [-]?[0-9]* [-]?[0-9]*");
  1036. pRectIElementA->LinkEndChild(pRectIElementB);
  1037. // RectF.
  1038. tinyxml2::XMLComment* pRectFComment = schemaDocument.NewComment("RectF Console Type");
  1039. pSchemaElement->LinkEndChild(pRectFComment);
  1040. tinyxml2::XMLElement* pRectFTypeElement = schemaDocument.NewElement("xs:simpleType");
  1041. pRectFTypeElement->SetAttribute("name", "RectF_ConsoleType");
  1042. pSchemaElement->LinkEndChild(pRectFTypeElement);
  1043. tinyxml2::XMLElement* pRectFElementA = schemaDocument.NewElement("xs:restriction");
  1044. pRectFElementA->SetAttribute("base", "xs:string");
  1045. pRectFTypeElement->LinkEndChild(pRectFElementA);
  1046. tinyxml2::XMLElement* pRectFElementB = schemaDocument.NewElement("xs:pattern");
  1047. pRectFElementB->SetAttribute("value", "(\\b[-]?(b[0-9]+)?\\.)?[0-9]+\\b");
  1048. pRectFElementA->LinkEndChild(pRectFElementB);
  1049. // AssetId.
  1050. tinyxml2::XMLComment* pAssetIdComment = schemaDocument.NewComment("AssetId Console Type");
  1051. pSchemaElement->LinkEndChild(pAssetIdComment);
  1052. tinyxml2::XMLElement* pAssetIdTypeElement = schemaDocument.NewElement("xs:simpleType");
  1053. pAssetIdTypeElement->SetAttribute("name", "AssetId_ConsoleType");
  1054. pSchemaElement->LinkEndChild(pAssetIdTypeElement);
  1055. tinyxml2::XMLElement* pAssetIdElementA = schemaDocument.NewElement("xs:restriction");
  1056. pAssetIdElementA->SetAttribute("base", "xs:string");
  1057. pAssetIdTypeElement->LinkEndChild(pAssetIdElementA);
  1058. tinyxml2::XMLElement* pAssetIdElementB = schemaDocument.NewElement("xs:pattern");
  1059. dSprintf(buffer, sizeof(buffer), "(%s)?\\b[a-zA-Z0-9]+\\b%s\\b[a-zA-Z0-9]+\\b", ASSET_ID_FIELD_PREFIX, ASSET_SCOPE_TOKEN);
  1060. pAssetIdElementB->SetAttribute("value", buffer);
  1061. pAssetIdElementA->LinkEndChild(pAssetIdElementB);
  1062. // Color Enums.
  1063. tinyxml2::XMLComment* pColorEnumsComment = schemaDocument.NewComment("Color Enums");
  1064. pSchemaElement->LinkEndChild(pColorEnumsComment);
  1065. tinyxml2::XMLElement* pColorEnumsTypeElement = schemaDocument.NewElement("xs:simpleType");
  1066. pColorEnumsTypeElement->SetAttribute("name", "Color_Enums");
  1067. pSchemaElement->LinkEndChild(pColorEnumsTypeElement);
  1068. tinyxml2::XMLElement* pColorEnumsRestrictionElement = schemaDocument.NewElement("xs:restriction");
  1069. pColorEnumsRestrictionElement->SetAttribute("base", "xs:string");
  1070. pColorEnumsTypeElement->LinkEndChild(pColorEnumsRestrictionElement);
  1071. const S32 ColorEnumsCount = StockColor::getCount();
  1072. for (S32 index = 0; index < ColorEnumsCount; ++index)
  1073. {
  1074. // Add enumeration element.
  1075. tinyxml2::XMLElement* pColorEnumsAttributeEnumerationElement = schemaDocument.NewElement("xs:enumeration");
  1076. pColorEnumsAttributeEnumerationElement->SetAttribute("value", StockColor::getColorItem(index)->getColorName());
  1077. pColorEnumsRestrictionElement->LinkEndChild(pColorEnumsAttributeEnumerationElement);
  1078. }
  1079. // LinearColorF.
  1080. tinyxml2::XMLComment* pColorFValuesComment = schemaDocument.NewComment("LinearColorF Values");
  1081. pSchemaElement->LinkEndChild(pColorFValuesComment);
  1082. tinyxml2::XMLElement* pColorFValuesTypeElement = schemaDocument.NewElement("xs:simpleType");
  1083. pColorFValuesTypeElement->SetAttribute("name", "ColorF_Values");
  1084. pSchemaElement->LinkEndChild(pColorFValuesTypeElement);
  1085. tinyxml2::XMLElement* pColorFValuesElementA = schemaDocument.NewElement("xs:restriction");
  1086. pColorFValuesElementA->SetAttribute("base", "xs:string");
  1087. pColorFValuesTypeElement->LinkEndChild(pColorFValuesElementA);
  1088. tinyxml2::XMLElement* pColorFValuesElementB = schemaDocument.NewElement("xs:pattern");
  1089. pColorFValuesElementB->SetAttribute("value", "([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b");
  1090. pColorFValuesElementA->LinkEndChild(pColorFValuesElementB);
  1091. tinyxml2::XMLComment* pColorFComment = schemaDocument.NewComment("LinearColorF Console Type");
  1092. pSchemaElement->LinkEndChild(pColorFComment);
  1093. tinyxml2::XMLElement* pColorFTypeElement = schemaDocument.NewElement("xs:simpleType");
  1094. pColorFTypeElement->SetAttribute("name", "ColorF_ConsoleType");
  1095. pSchemaElement->LinkEndChild(pColorFTypeElement);
  1096. tinyxml2::XMLElement* pColorFUnionElement = schemaDocument.NewElement("xs:union");
  1097. pColorFUnionElement->SetAttribute("memberTypes", "ColorF_Values Color_Enums");
  1098. pColorFTypeElement->LinkEndChild(pColorFUnionElement);
  1099. // ColorI.
  1100. tinyxml2::XMLComment* pColorIValuesComment = schemaDocument.NewComment("ColorI Values");
  1101. pSchemaElement->LinkEndChild(pColorIValuesComment);
  1102. tinyxml2::XMLElement* pColorIValuesTypeElement = schemaDocument.NewElement("xs:simpleType");
  1103. pColorIValuesTypeElement->SetAttribute("name", "ColorI_Values");
  1104. pSchemaElement->LinkEndChild(pColorIValuesTypeElement);
  1105. tinyxml2::XMLElement* pColorIValuesElementA = schemaDocument.NewElement("xs:restriction");
  1106. pColorIValuesElementA->SetAttribute("base", "xs:string");
  1107. pColorIValuesTypeElement->LinkEndChild(pColorIValuesElementA);
  1108. tinyxml2::XMLElement* pColorIValuesElementB = schemaDocument.NewElement("xs:pattern");
  1109. pColorIValuesElementB->SetAttribute("value", "[-]?[0-9]* [-]?[0-9]* [-]?[0-9]* [-]?[0-9]*");
  1110. pColorIValuesElementA->LinkEndChild(pColorIValuesElementB);
  1111. tinyxml2::XMLComment* pColorIComment = schemaDocument.NewComment("ColorI Console Type");
  1112. pSchemaElement->LinkEndChild(pColorIComment);
  1113. tinyxml2::XMLElement* pColorITypeElement = schemaDocument.NewElement("xs:simpleType");
  1114. pColorITypeElement->SetAttribute("name", "ColorI_ConsoleType");
  1115. pSchemaElement->LinkEndChild(pColorITypeElement);
  1116. tinyxml2::XMLElement* pColorIUnionElement = schemaDocument.NewElement("xs:union");
  1117. pColorIUnionElement->SetAttribute("memberTypes", "ColorI_Values Color_Enums");
  1118. pColorITypeElement->LinkEndChild(pColorIUnionElement);
  1119. // *************************************************************
  1120. // Generate engine type elements.
  1121. // *************************************************************
  1122. // Generate the engine type elements.
  1123. tinyxml2::XMLComment* tComment = schemaDocument.NewComment("Type Elements");
  1124. pSchemaElement->LinkEndChild(tComment);
  1125. for (AbstractClassRep* pType = pRootType; pType != NULL; pType = pType->getNextClass())
  1126. {
  1127. // Add type.
  1128. tinyxml2::XMLElement* pTypeElement = schemaDocument.NewElement("xs:element");
  1129. pTypeElement->SetAttribute("name", pType->getClassName());
  1130. dSprintf(buffer, sizeof(buffer), "%s_Type", pType->getClassName());
  1131. pTypeElement->SetAttribute("type", buffer);
  1132. pSchemaElement->LinkEndChild(pTypeElement);
  1133. }
  1134. // *************************************************************
  1135. // Generate the engine complex types.
  1136. // *************************************************************
  1137. for (AbstractClassRep* pType = pRootType; pType != NULL; pType = pType->getNextClass())
  1138. {
  1139. // Add complex type comment.
  1140. dSprintf(buffer, sizeof(buffer), " %s Type ", pType->getClassName());
  1141. tinyxml2::XMLComment* ctComment = schemaDocument.NewComment(buffer);
  1142. pSchemaElement->LinkEndChild(ctComment);
  1143. // Add complex type.
  1144. tinyxml2::XMLElement* pComplexTypeElement = schemaDocument.NewElement("xs:complexType");
  1145. dSprintf(buffer, sizeof(buffer), "%s_Type", pType->getClassName());
  1146. pComplexTypeElement->SetAttribute("name", buffer);
  1147. pSchemaElement->LinkEndChild(pComplexTypeElement);
  1148. // Add sequence.
  1149. tinyxml2::XMLElement* pAllElement = schemaDocument.NewElement("xs:all");
  1150. pComplexTypeElement->LinkEndChild(pAllElement);
  1151. // Fetch container child class.
  1152. AbstractClassRep* pContainerChildClass = pType->getContainerChildClass(true);
  1153. // Is the type allowed children?
  1154. if (pContainerChildClass != NULL)
  1155. {
  1156. // Yes, so add choice element.
  1157. tinyxml2::XMLElement* pChoiceElement = schemaDocument.NewElement("xs:choice");
  1158. pChoiceElement->SetAttribute("minOccurs", 0);
  1159. pChoiceElement->SetAttribute("maxOccurs", "unbounded");
  1160. pAllElement->LinkEndChild(pChoiceElement);
  1161. // Find child group.
  1162. HashTable<AbstractClassRep*, StringTableEntry>::Iterator childGroupItr = childGroups.find(pContainerChildClass);
  1163. // Does the group exist?
  1164. if (childGroupItr == childGroups.end())
  1165. {
  1166. // No, so format group name.
  1167. dSprintf(buffer, sizeof(buffer), "%s_ChildrenTypes", pContainerChildClass->getClassName());
  1168. // Insert into child group hash.
  1169. childGroupItr = childGroups.insertUnique(pContainerChildClass, StringTable->insert(buffer));
  1170. // Add the group.
  1171. tinyxml2::XMLElement* pChildrenGroupElement = schemaDocument.NewElement("xs:group");
  1172. pChildrenGroupElement->SetAttribute("name", buffer);
  1173. pSchemaElement->LinkEndChild(pChildrenGroupElement);
  1174. // Add choice element.
  1175. tinyxml2::XMLElement* pChildreGroupChoiceElement = schemaDocument.NewElement("xs:choice");
  1176. pChildrenGroupElement->LinkEndChild(pChildreGroupChoiceElement);
  1177. // Add choice members.
  1178. for (AbstractClassRep* pChoiceType = pRootType; pChoiceType != NULL; pChoiceType = pChoiceType->getNextClass())
  1179. {
  1180. // Skip if not derived from the container child class.
  1181. if (!pChoiceType->isClass(pContainerChildClass))
  1182. continue;
  1183. // Add choice member.
  1184. tinyxml2::XMLElement* pChildrenMemberElement = schemaDocument.NewElement("xs:element");
  1185. pChildrenMemberElement->SetAttribute("name", pChoiceType->getClassName());
  1186. dSprintf(buffer, sizeof(buffer), "%s_Type", pChoiceType->getClassName());
  1187. pChildrenMemberElement->SetAttribute("type", buffer);
  1188. pChildreGroupChoiceElement->LinkEndChild(pChildrenMemberElement);
  1189. }
  1190. }
  1191. // Reference the child group.
  1192. tinyxml2::XMLElement* pChoiceGroupReferenceElement = schemaDocument.NewElement("xs:group");
  1193. pChoiceGroupReferenceElement->SetAttribute("ref", childGroupItr->value);
  1194. pChoiceGroupReferenceElement->SetAttribute("minOccurs", 0);
  1195. pChoiceElement->LinkEndChild(pChoiceGroupReferenceElement);
  1196. }
  1197. // Generate the custom Taml schema.
  1198. for (AbstractClassRep* pCustomSchemaType = pType; pCustomSchemaType != NULL; pCustomSchemaType = pCustomSchemaType->getParentClass())
  1199. {
  1200. // Fetch the types custom TAML schema function.
  1201. AbstractClassRep::WriteCustomTamlSchema customSchemaFn = pCustomSchemaType->getCustomTamlSchema();
  1202. // Skip if no function avilable.
  1203. if (customSchemaFn == NULL)
  1204. continue;
  1205. // Call schema generation function.
  1206. customSchemaFn(pType, pAllElement);
  1207. }
  1208. // Fetch field list.
  1209. const AbstractClassRep::FieldList& fields = pType->mFieldList;
  1210. // Fetch field count.
  1211. const S32 fieldCount = fields.size();
  1212. // Generate array attribute groups
  1213. for (S32 index = 0; index < fieldCount; ++index)
  1214. {
  1215. // Fetch field.
  1216. const AbstractClassRep::Field& field = fields[index];
  1217. if (field.type == AbstractClassRep::StartArrayFieldType)
  1218. {
  1219. // Add the top-level array identifier
  1220. tinyxml2::XMLElement* pArrayElement = schemaDocument.NewElement("xs:element");
  1221. dSprintf(buffer, sizeof(buffer), "%s.%s", pType->getClassName(), g_sanitize_schema_element_name(field.pGroupname).c_str());
  1222. pArrayElement->SetAttribute("name", buffer);
  1223. pArrayElement->SetAttribute("minOccurs", 0);
  1224. pAllElement->LinkEndChild(pArrayElement);
  1225. // Inline type specification
  1226. tinyxml2::XMLElement* pArrayComplexTypeElement = schemaDocument.NewElement("xs:complexType");
  1227. pArrayElement->LinkEndChild(pArrayComplexTypeElement);
  1228. // Add the actual (repeating) array elements
  1229. tinyxml2::XMLElement* pInnerArrayElement = schemaDocument.NewElement("xs:element");
  1230. pInnerArrayElement->SetAttribute("name", g_sanitize_schema_element_name(field.pFieldname).c_str());
  1231. pInnerArrayElement->SetAttribute("minOccurs", 0);
  1232. pInnerArrayElement->SetAttribute("maxOccurs", field.elementCount);
  1233. pArrayComplexTypeElement->LinkEndChild(pInnerArrayElement);
  1234. // Inline type specification
  1235. tinyxml2::XMLElement* pInnerComplexTypeElement = schemaDocument.NewElement("xs:complexType");
  1236. pInnerArrayElement->LinkEndChild(pInnerComplexTypeElement);
  1237. // Add a reference to the attribute group for the array
  1238. tinyxml2::XMLElement* pInnerAttributeGroupElement = schemaDocument.NewElement("xs:attributeGroup");
  1239. dSprintf(buffer, sizeof(buffer), "%s_%s_Array_Fields", pType->getClassName(), g_sanitize_schema_element_name(field.pFieldname).c_str());
  1240. pInnerAttributeGroupElement->SetAttribute("ref", buffer);
  1241. pInnerComplexTypeElement->LinkEndChild(pInnerAttributeGroupElement);
  1242. // Add the attribute group itself
  1243. tinyxml2::XMLElement* pFieldAttributeGroupElement = schemaDocument.NewElement("xs:attributeGroup");
  1244. pFieldAttributeGroupElement->SetAttribute("name", buffer);
  1245. pSchemaElement->LinkEndChild(pFieldAttributeGroupElement);
  1246. // Keep adding fields to attribute group until we hit the end of the array
  1247. for (; index < fieldCount; ++index)
  1248. {
  1249. const AbstractClassRep::Field& array_field = fields[index];
  1250. if (array_field.type == AbstractClassRep::EndArrayFieldType)
  1251. {
  1252. break;
  1253. }
  1254. tinyxml2::XMLElement* pAttributeElement = g__write_schema_attribute_element(array_field, pType, schemaDocument);
  1255. if (pAttributeElement == NULL)
  1256. {
  1257. continue;
  1258. }
  1259. pFieldAttributeGroupElement->LinkEndChild(pAttributeElement);
  1260. }
  1261. }
  1262. }
  1263. // Generate field attribute group.
  1264. tinyxml2::XMLElement* pFieldAttributeGroupElement = schemaDocument.NewElement("xs:attributeGroup");
  1265. dSprintf(buffer, sizeof(buffer), "%s_Fields", pType->getClassName());
  1266. pFieldAttributeGroupElement->SetAttribute("name", buffer);
  1267. pSchemaElement->LinkEndChild(pFieldAttributeGroupElement);
  1268. // Iterate static fields (in reverse as most types are organized from the root-fields up).
  1269. for (S32 index = fieldCount - 1; index > 0; --index)
  1270. {
  1271. // Fetch field.
  1272. const AbstractClassRep::Field& field = fields[index];
  1273. // Skip fields inside arrays
  1274. if (field.type == AbstractClassRep::EndArrayFieldType)
  1275. {
  1276. for (; index > 0; --index)
  1277. {
  1278. if (field.type == AbstractClassRep::StartArrayFieldType)
  1279. {
  1280. break;
  1281. }
  1282. }
  1283. continue;
  1284. }
  1285. tinyxml2::XMLElement* pAttributeElement = g__write_schema_attribute_element(field, pType, schemaDocument);
  1286. if (pAttributeElement == NULL)
  1287. {
  1288. continue;
  1289. }
  1290. pFieldAttributeGroupElement->LinkEndChild(pAttributeElement);
  1291. }
  1292. // Is this the SimObject Type?
  1293. if (pType == pSimObjectType)
  1294. {
  1295. // Yes, so add reserved Taml field attributes here...
  1296. // Add Taml "Name" attribute element.
  1297. tinyxml2::XMLElement* pNameAttributeElement = schemaDocument.NewElement("xs:attribute");
  1298. pNameAttributeElement->SetAttribute("name", tamlNamedObjectName);
  1299. pNameAttributeElement->SetAttribute("type", "xs:normalizedString");
  1300. pFieldAttributeGroupElement->LinkEndChild(pNameAttributeElement);
  1301. // Add Taml "TamlId" attribute element.
  1302. tinyxml2::XMLElement* pTamlIdAttributeElement = schemaDocument.NewElement("xs:attribute");
  1303. pTamlIdAttributeElement->SetAttribute("name", tamlRefIdName);
  1304. pTamlIdAttributeElement->SetAttribute("type", "xs:nonNegativeInteger");
  1305. pFieldAttributeGroupElement->LinkEndChild(pTamlIdAttributeElement);
  1306. // Add Taml "TamlRefId" attribute element.
  1307. tinyxml2::XMLElement* pTamlRefIdAttributeElement = schemaDocument.NewElement("xs:attribute");
  1308. pTamlRefIdAttributeElement->SetAttribute("name", tamlRefToIdName);
  1309. pTamlRefIdAttributeElement->SetAttribute("type", "xs:nonNegativeInteger");
  1310. pFieldAttributeGroupElement->LinkEndChild(pTamlRefIdAttributeElement);
  1311. }
  1312. // Add attribute group types.
  1313. for (AbstractClassRep* pAttributeGroupsType = pType; pAttributeGroupsType != NULL; pAttributeGroupsType = pAttributeGroupsType->getParentClass())
  1314. {
  1315. tinyxml2::XMLElement* pFieldAttributeGroupRefElement = schemaDocument.NewElement("xs:attributeGroup");
  1316. dSprintf(buffer, sizeof(buffer), "%s_Fields", pAttributeGroupsType->getClassName());
  1317. pFieldAttributeGroupRefElement->SetAttribute("ref", buffer);
  1318. pComplexTypeElement->LinkEndChild(pFieldAttributeGroupRefElement);
  1319. }
  1320. // Add "any" attribute element (dynamic fields).
  1321. tinyxml2::XMLElement* pAnyAttributeElement = schemaDocument.NewElement("xs:anyAttribute");
  1322. pAnyAttributeElement->SetAttribute("processContents", "skip");
  1323. pComplexTypeElement->LinkEndChild(pAnyAttributeElement);
  1324. }
  1325. // Write the schema document.
  1326. schemaDocument.SaveFile(filePathBuffer);
  1327. // Close file.
  1328. stream.close();
  1329. return true;
  1330. }
  1331. //-----------------------------------------------------------------------------
  1332. void Taml::WriteUnrestrictedCustomTamlSchema(const char* pCustomNodeName, const AbstractClassRep* pClassRep, tinyxml2::XMLElement* pParentElement)
  1333. {
  1334. // Sanity!
  1335. AssertFatal(pCustomNodeName != NULL, "Taml::WriteDefaultCustomTamlSchema() - Node name cannot be NULL.");
  1336. AssertFatal(pClassRep != NULL, "Taml::WriteDefaultCustomTamlSchema() - ClassRep cannot be NULL.");
  1337. AssertFatal(pParentElement != NULL, "Taml::WriteDefaultCustomTamlSchema() - Parent Element cannot be NULL.");
  1338. tinyxml2::XMLDocument* doc = pParentElement->GetDocument();
  1339. char buffer[1024];
  1340. // Add custom type element.
  1341. tinyxml2::XMLElement* pCustomElement = doc->NewElement("xs:element");
  1342. dSprintf(buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), pCustomNodeName);
  1343. pCustomElement->SetAttribute("name", buffer);
  1344. pCustomElement->SetAttribute("minOccurs", 0);
  1345. pCustomElement->SetAttribute("maxOccurs", 1);
  1346. pParentElement->LinkEndChild(pCustomElement);
  1347. // Add complex type element.
  1348. tinyxml2::XMLElement* pComplexTypeElement = doc->NewElement("xs:complexType");
  1349. pCustomElement->LinkEndChild(pComplexTypeElement);
  1350. // Add choice element.
  1351. tinyxml2::XMLElement* pChoiceElement = doc->NewElement("xs:choice");
  1352. pChoiceElement->SetAttribute("minOccurs", 0);
  1353. pChoiceElement->SetAttribute("maxOccurs", "unbounded");
  1354. pComplexTypeElement->LinkEndChild(pChoiceElement);
  1355. // Add sequence element.
  1356. tinyxml2::XMLElement* pSequenceElement = doc->NewElement("xs:sequence");
  1357. pChoiceElement->LinkEndChild(pSequenceElement);
  1358. // Add "any" element.
  1359. tinyxml2::XMLElement* pAnyElement = doc->NewElement("xs:any");
  1360. pAnyElement->SetAttribute("processContents", "skip");
  1361. pSequenceElement->LinkEndChild(pAnyElement);
  1362. }