taml.cpp 64 KB

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