taml.cpp 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678
  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;
  504. SimObject* defaultObject;
  505. if (!getWriteDefaults())
  506. {
  507. // Create a default object of the same type
  508. defaultConObject = ConsoleObject::create(pSimObject->getClassName());
  509. defaultObject = dynamic_cast<SimObject*>(defaultConObject);
  510. // ***Really*** shouldn't happen
  511. if (!defaultObject)
  512. return;
  513. }
  514. // Iterate fields.
  515. U8 arrayDepth = 0;
  516. TamlCustomNode* currentArrayNode = NULL;
  517. for (U32 index = 0; index < fieldCount; ++index)
  518. {
  519. // Fetch field.
  520. const AbstractClassRep::Field* pField = &fieldList[index];
  521. // Ignore if field not appropriate.
  522. if (pField->type == AbstractClassRep::DeprecatedFieldType ||
  523. pField->type == AbstractClassRep::StartGroupFieldType ||
  524. pField->type == AbstractClassRep::EndGroupFieldType)
  525. continue;
  526. if (pField->type == AbstractClassRep::StartArrayFieldType)
  527. {
  528. TamlCustomNodes& pCustomNodes = pTamlWriteNode->mCustomNodes;
  529. currentArrayNode = pCustomNodes.addNode(pField->pGroupname);
  530. for (U16 idx = 0; idx < pField->elementCount; idx++)
  531. currentArrayNode->addNode(pField->pFieldname);
  532. arrayDepth++;
  533. continue;
  534. }
  535. if (pField->type == AbstractClassRep::EndArrayFieldType)
  536. {
  537. arrayDepth--;
  538. continue;
  539. }
  540. if (arrayDepth == 0 && pField->elementCount > 1)
  541. {
  542. TamlCustomNodes& pCustomNodes = pTamlWriteNode->mCustomNodes;
  543. char* niceFieldName = const_cast<char *>(pField->pFieldname);
  544. niceFieldName[0] = dToupper(niceFieldName[0]);
  545. String str_niceFieldName = String(niceFieldName);
  546. currentArrayNode = pCustomNodes.addNode(str_niceFieldName + "s");
  547. for (U16 idx = 0; idx < pField->elementCount; idx++)
  548. currentArrayNode->addNode(str_niceFieldName);
  549. }
  550. // Fetch fieldname.
  551. StringTableEntry fieldName = StringTable->insert(pField->pFieldname);
  552. // Fetch element count.
  553. const U32 elementCount = pField->elementCount;
  554. // Skip if the field should not be written.
  555. // For now, we only deal with non-array fields.
  556. if (elementCount == 1 &&
  557. pField->setDataFn != NULL &&
  558. (!getWriteDefaults() && pField->writeDataFn(pSimObject, fieldName) == false))
  559. continue;
  560. // Iterate elements.
  561. for (U32 elementIndex = 0; elementIndex < elementCount; ++elementIndex)
  562. {
  563. char indexBuffer[8];
  564. dSprintf(indexBuffer, 8, "%d", elementIndex);
  565. // Fetch object field value.
  566. const char* pFieldValue = pSimObject->getPrefixedDataField(fieldName, indexBuffer);
  567. if (!pFieldValue)
  568. pFieldValue = StringTable->EmptyString();
  569. U32 nBufferSize = dStrlen(pFieldValue) + 1;
  570. FrameTemp<char> valueCopy(nBufferSize);
  571. dStrcpy((char *)valueCopy, pFieldValue, nBufferSize);
  572. // Skip if field should not be written.
  573. if (!pSimObject->writeField(fieldName, valueCopy))
  574. continue;
  575. if (!getWriteDefaults())
  576. {
  577. //If the field hasn't been changed from the default value, then don't bother writing it out
  578. const char* fieldData = defaultObject->getDataField(fieldName, indexBuffer);
  579. if (fieldData && fieldData[0] != '\0' && dStricmp(fieldData, pFieldValue) == 0)
  580. continue;
  581. }
  582. // Reassign field value.
  583. pFieldValue = valueCopy;
  584. if (pField->type == TypeBool)
  585. pFieldValue = dAtob(pFieldValue) ? "true" : "false";
  586. // Detect and collapse relative path information
  587. char fnBuf[1024];
  588. if ((S32)pField->type == TypeFilename)
  589. {
  590. Con::collapseScriptFilename(fnBuf, 1024, pFieldValue);
  591. pFieldValue = fnBuf;
  592. }
  593. // Save field/value.
  594. if (arrayDepth > 0 || pField->elementCount > 1)
  595. currentArrayNode->getChildren()[elementIndex]->addField(fieldName, pFieldValue);
  596. else
  597. {
  598. TamlWriteNode::FieldValuePair* pFieldValuePair = new TamlWriteNode::FieldValuePair(fieldName, pFieldValue);
  599. pTamlWriteNode->mFields.push_back(pFieldValuePair);
  600. }
  601. }
  602. }
  603. if (!getWriteDefaults())
  604. {
  605. // Cleanup our created default object
  606. delete defaultConObject;
  607. }
  608. }
  609. //-----------------------------------------------------------------------------
  610. static S32 QSORT_CALLBACK compareFieldEntries(const void* a, const void* b)
  611. {
  612. // Debug Profiling.
  613. PROFILE_SCOPE(Taml_CompareFieldEntries);
  614. SimFieldDictionary::Entry *fa = *((SimFieldDictionary::Entry **)a);
  615. SimFieldDictionary::Entry *fb = *((SimFieldDictionary::Entry **)b);
  616. return dStricmp(fa->slotName, fb->slotName);
  617. }
  618. //-----------------------------------------------------------------------------
  619. void Taml::compileDynamicFields(TamlWriteNode* pTamlWriteNode)
  620. {
  621. // Debug Profiling.
  622. PROFILE_SCOPE(Taml_CompileDynamicFields);
  623. // Sanity!
  624. AssertFatal(pTamlWriteNode != NULL, "Cannot compile dynamic fields on a NULL node.");
  625. AssertFatal(pTamlWriteNode->mpSimObject != NULL, "Cannot compile dynamic fields on a node with no object.");
  626. // Fetch object.
  627. SimObject* pSimObject = pTamlWriteNode->mpSimObject;
  628. // Fetch field dictionary.
  629. SimFieldDictionary* pFieldDictionary = pSimObject->getFieldDictionary();
  630. // Ignore if not writing dynamic fields.
  631. if (!pFieldDictionary || !pSimObject->getCanSaveDynamicFields())
  632. return;
  633. // Fetch field list.
  634. const AbstractClassRep::FieldList& fieldList = pSimObject->getFieldList();
  635. // Fetch field count.
  636. const U32 fieldCount = fieldList.size();
  637. Vector<SimFieldDictionary::Entry*> dynamicFieldList(__FILE__, __LINE__);
  638. // Ensure the dynamic field doesn't conflict with static field.
  639. for (U32 hashIndex = 0; hashIndex < SimFieldDictionary::HashTableSize; ++hashIndex)
  640. {
  641. for (SimFieldDictionary::Entry* pEntry = pFieldDictionary->mHashTable[hashIndex]; pEntry; pEntry = pEntry->next)
  642. {
  643. // Iterate static fields.
  644. U32 fieldIndex;
  645. for (fieldIndex = 0; fieldIndex < fieldCount; ++fieldIndex)
  646. {
  647. if (fieldList[fieldIndex].pFieldname == pEntry->slotName)
  648. break;
  649. }
  650. // Skip if found.
  651. if (fieldIndex != (U32)fieldList.size())
  652. continue;
  653. // Skip if not writing field.
  654. if (!pSimObject->writeField(pEntry->slotName, pEntry->value))
  655. continue;
  656. dynamicFieldList.push_back(pEntry);
  657. }
  658. }
  659. // Sort Entries to prevent version control conflicts
  660. if (dynamicFieldList.size() > 1)
  661. dQsort(dynamicFieldList.address(), dynamicFieldList.size(), sizeof(SimFieldDictionary::Entry*), compareFieldEntries);
  662. // Save the fields.
  663. for (Vector<SimFieldDictionary::Entry*>::iterator entryItr = dynamicFieldList.begin(); entryItr != dynamicFieldList.end(); ++entryItr)
  664. {
  665. // Fetch entry.
  666. SimFieldDictionary::Entry* pEntry = *entryItr;
  667. // Save field/value.
  668. TamlWriteNode::FieldValuePair* pFieldValuePair = new TamlWriteNode::FieldValuePair(pEntry->slotName, pEntry->value);
  669. pTamlWriteNode->mFields.push_back(pFieldValuePair);
  670. }
  671. }
  672. //-----------------------------------------------------------------------------
  673. void Taml::compileChildren(TamlWriteNode* pTamlWriteNode)
  674. {
  675. // Debug Profiling.
  676. PROFILE_SCOPE(Taml_CompileChildren);
  677. // Sanity!
  678. AssertFatal(pTamlWriteNode != NULL, "Cannot compile children on a NULL node.");
  679. AssertFatal(pTamlWriteNode->mpSimObject != NULL, "Cannot compile children on a node with no object.");
  680. // Fetch object.
  681. SimObject* pSimObject = pTamlWriteNode->mpSimObject;
  682. // Fetch the Taml children.
  683. TamlChildren* pChildren = dynamic_cast<TamlChildren*>(pSimObject);
  684. // Finish if object does not contain Taml children.
  685. if (pChildren == NULL || pChildren->getTamlChildCount() == 0)
  686. return;
  687. // Create children vector.
  688. pTamlWriteNode->mChildren = new typeNodeVector();
  689. // Fetch the child count.
  690. const U32 childCount = pChildren->getTamlChildCount();
  691. // Iterate children.
  692. for (U32 childIndex = 0; childIndex < childCount; childIndex++)
  693. {
  694. // Compile object.
  695. TamlWriteNode* pChildTamlWriteNode = compileObject(pChildren->getTamlChild(childIndex));
  696. // Save node.
  697. pTamlWriteNode->mChildren->push_back(pChildTamlWriteNode);
  698. }
  699. }
  700. //-----------------------------------------------------------------------------
  701. void Taml::compileCustomState(TamlWriteNode* pTamlWriteNode)
  702. {
  703. // Debug Profiling.
  704. PROFILE_SCOPE(Taml_CompileCustomProperties);
  705. // Sanity!
  706. AssertFatal(pTamlWriteNode != NULL, "Cannot compile custom state on a NULL node.");
  707. AssertFatal(pTamlWriteNode->mpSimObject != NULL, "Cannot compile custom state on a node with no object.");
  708. // Fetch the custom node on the write node.
  709. TamlCustomNodes& customNodes = pTamlWriteNode->mCustomNodes;
  710. // Are there any Taml callbacks?
  711. if (pTamlWriteNode->mpTamlCallbacks != NULL)
  712. {
  713. // Yes, so call it.
  714. tamlCustomWrite(pTamlWriteNode->mpTamlCallbacks, customNodes);
  715. }
  716. // Fetch custom nodes.
  717. const TamlCustomNodeVector& nodes = customNodes.getNodes();
  718. // Finish if no custom nodes to process.
  719. if (nodes.size() == 0)
  720. return;
  721. // Iterate custom properties.
  722. for (TamlCustomNodeVector::const_iterator customNodesItr = nodes.begin(); customNodesItr != nodes.end(); ++customNodesItr)
  723. {
  724. // Fetch the custom node.
  725. TamlCustomNode* pCustomNode = *customNodesItr;
  726. // Compile custom node state.
  727. compileCustomNodeState(pCustomNode);
  728. }
  729. }
  730. //-----------------------------------------------------------------------------
  731. void Taml::compileCustomNodeState(TamlCustomNode* pCustomNode)
  732. {
  733. // Sanity!
  734. AssertFatal(pCustomNode != NULL, "Taml: Cannot compile NULL custom node state.");
  735. // Fetch children.
  736. const TamlCustomNodeVector& children = pCustomNode->getChildren();
  737. // Fetch proxy object.
  738. SimObject* pProxyObject = pCustomNode->getProxyObject<SimObject>(false);
  739. // Do we have a proxy object?
  740. if (pProxyObject != NULL)
  741. {
  742. // Yes, so sanity!
  743. AssertFatal(children.size() == 0, "Taml: Cannot compile a proxy object on a custom node that has children.");
  744. // Yes, so compile it.
  745. // NOTE: We force an Id for custom compiled objects so we guarantee an Id. The reason for this is fairly
  746. // weak in that the XML parser currently has no way of distinguishing between a compiled object node
  747. // 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.
  748. pCustomNode->setWriteNode(compileObject(pProxyObject, true));
  749. return;
  750. }
  751. // Finish if no children.
  752. if (children.size() == 0)
  753. return;
  754. // Iterate children.
  755. for (TamlCustomNodeVector::const_iterator childItr = children.begin(); childItr != children.end(); ++childItr)
  756. {
  757. // Fetch shape node.
  758. TamlCustomNode* pChildNode = *childItr;
  759. // Compile the child.
  760. compileCustomNodeState(pChildNode);
  761. }
  762. }
  763. //-----------------------------------------------------------------------------
  764. SimObject* Taml::createType(StringTableEntry typeName, const Taml* pTaml, const char* pProgenitorSuffix)
  765. {
  766. // Debug Profiling.
  767. PROFILE_SCOPE(Taml_CreateType);
  768. typedef HashTable<StringTableEntry, AbstractClassRep*> typeClassHash;
  769. static typeClassHash mClassMap;
  770. // Sanity!
  771. AssertFatal(typeName != NULL, "Taml: Type cannot be NULL");
  772. // Find type.
  773. typeClassHash::Iterator typeItr = mClassMap.find(typeName);
  774. // Found type?
  775. if (typeItr == mClassMap.end())
  776. {
  777. // No, so find type.
  778. AbstractClassRep* pClassRep = AbstractClassRep::getClassList();
  779. while (pClassRep)
  780. {
  781. // Is this the type?
  782. if (dStricmp(pClassRep->getClassName(), typeName) == 0)
  783. {
  784. // Yes, so insert it.
  785. typeItr = mClassMap.insertUnique(typeName, pClassRep);
  786. break;
  787. }
  788. // Next type.
  789. pClassRep = pClassRep->getNextClass();
  790. }
  791. // Did we find the type?
  792. if (typeItr == mClassMap.end())
  793. {
  794. // No, so warn and fail.
  795. Con::warnf("Taml: Failed to create type '%s' as such a registered type could not be found.", typeName);
  796. return NULL;
  797. }
  798. }
  799. // Create the object.
  800. ConsoleObject* pConsoleObject = typeItr->value->create();
  801. // NOTE: It is important that we don't register the object here as many objects rely on the fact that
  802. // fields are set prior to the object being registered. Registering here will invalid those assumptions.
  803. // Fetch the SimObject.
  804. SimObject* pSimObject = dynamic_cast<SimObject*>(pConsoleObject);
  805. // Was it a SimObject?
  806. if (pSimObject == NULL)
  807. {
  808. // No, so warn.
  809. Con::warnf("Taml: Failed to create type '%s' as it is not a SimObject.", typeName);
  810. // Destroy object and fail.
  811. delete pConsoleObject;
  812. return NULL;
  813. }
  814. // Are we updating the file-progenitor?
  815. if (pTaml->getProgenitorUpdate())
  816. {
  817. // Yes, so do we have a progenitor suffix?
  818. if (pProgenitorSuffix == NULL)
  819. {
  820. // No, so just set it to the progenitor file.
  821. pSimObject->setProgenitorFile(pTaml->getFilePathBuffer());
  822. }
  823. else
  824. {
  825. // Yes, so format the progenitor buffer.
  826. char progenitorBuffer[2048];
  827. dSprintf(progenitorBuffer, sizeof(progenitorBuffer), "%s,%s", pTaml->getFilePathBuffer(), pProgenitorSuffix);
  828. // Set the progenitor file.
  829. pSimObject->setProgenitorFile(progenitorBuffer);
  830. }
  831. }
  832. return pSimObject;
  833. }
  834. //-----------------------------------------------------------------------------
  835. tinyxml2::XMLElement* g__write_schema_attribute_element(const AbstractClassRep::Field& field, AbstractClassRep* pType,
  836. tinyxml2::XMLDocument& schemaDocument)
  837. {
  838. // Skip if not a data field.
  839. if (field.type == AbstractClassRep::DeprecatedFieldType ||
  840. field.type == AbstractClassRep::StartGroupFieldType ||
  841. field.type == AbstractClassRep::EndGroupFieldType)
  842. return NULL;
  843. // Skip if the field root is not this type.
  844. if (pType->findFieldRoot(field.pFieldname) != pType)
  845. return NULL;
  846. // Add attribute element.
  847. tinyxml2::XMLElement* pAttributeElement = schemaDocument.NewElement("xs:attribute");
  848. pAttributeElement->SetAttribute("name", field.pFieldname);
  849. // Handle the console type appropriately.
  850. const S32 fieldType = (S32)field.type;
  851. /*
  852. // Is the field an enumeration?
  853. if ( fieldType == TypeEnum )
  854. {
  855. // Yes, so add attribute type.
  856. tinyxml2::XMLElement* pAttributeSimpleTypeElement = schemaDocument.NewElement( "xs:simpleType" );
  857. pAttributeElement->LinkEndChild( pAttributeSimpleTypeElement );
  858. // Add restriction element.
  859. tinyxml2::XMLElement* pAttributeRestrictionElement = schemaDocument.NewElement( "xs:restriction" );
  860. pAttributeRestrictionElement->SetAttribute( "base", "xs:string" );
  861. pAttributeSimpleTypeElement->LinkEndChild( pAttributeRestrictionElement );
  862. // Yes, so fetch enumeration count.
  863. const S32 enumCount = field.table->size;
  864. // Iterate enumeration.
  865. for( S32 index = 0; index < enumCount; ++index )
  866. {
  867. // Add enumeration element.
  868. tinyxml2::XMLElement* pAttributeEnumerationElement = schemaDocument.NewElement( "xs:enumeration" );
  869. pAttributeEnumerationElement->SetAttribute( "value", field.table->table[index].label );
  870. pAttributeRestrictionElement->LinkEndChild( pAttributeEnumerationElement );
  871. }
  872. }
  873. else
  874. {*/
  875. // No, so assume it's a string type initially.
  876. const char* pFieldTypeDescription = "xs:string";
  877. // Handle known types.
  878. if (fieldType == TypeF32)
  879. {
  880. pFieldTypeDescription = "xs:float";
  881. }
  882. else if (fieldType == TypeS8 || fieldType == TypeS32)
  883. {
  884. pFieldTypeDescription = "xs:int";
  885. }
  886. else if (fieldType == TypeBool || fieldType == TypeFlag)
  887. {
  888. pFieldTypeDescription = "xs:boolean";
  889. }
  890. else if (fieldType == TypePoint2F)
  891. {
  892. pFieldTypeDescription = "Point2F_ConsoleType";
  893. }
  894. else if (fieldType == TypePoint2I)
  895. {
  896. pFieldTypeDescription = "Point2I_ConsoleType";
  897. }
  898. else if (fieldType == TypeRectI)
  899. {
  900. pFieldTypeDescription = "RectI_ConsoleType";
  901. }
  902. else if (fieldType == TypeRectF)
  903. {
  904. pFieldTypeDescription = "RectF_ConsoleType";
  905. }
  906. else if (fieldType == TypeColorF)
  907. {
  908. pFieldTypeDescription = "ColorF_ConsoleType";
  909. }
  910. else if (fieldType == TypeColorI)
  911. {
  912. pFieldTypeDescription = "ColorI_ConsoleType";
  913. }
  914. else if (fieldType == TypeAssetId/* ||
  915. fieldType == TypeImageAssetPtr ||
  916. fieldType == TypeAnimationAssetPtr ||
  917. fieldType == TypeAudioAssetPtr*/)
  918. {
  919. pFieldTypeDescription = "AssetId_ConsoleType";
  920. }
  921. // Set attribute type.
  922. pAttributeElement->SetAttribute("type", pFieldTypeDescription);
  923. //}
  924. pAttributeElement->SetAttribute("use", "optional");
  925. return pAttributeElement;
  926. }
  927. String g_sanitize_schema_element_name(String buffer)
  928. {
  929. return buffer.replace("(", "")
  930. .replace(")", "");
  931. }
  932. bool Taml::generateTamlSchema()
  933. {
  934. // Fetch any TAML Schema file reference.
  935. const char* pTamlSchemaFile = Con::getVariable(TAML_SCHEMA_VARIABLE);
  936. // Do we have a schema file reference?
  937. if (pTamlSchemaFile == NULL || *pTamlSchemaFile == 0)
  938. {
  939. // No, so warn.
  940. Con::warnf("Taml::generateTamlSchema() - Cannot write a TAML schema as no schema variable is set ('%s').", TAML_SCHEMA_VARIABLE);
  941. return false;
  942. }
  943. // Expand the file-name into the file-path buffer.
  944. char filePathBuffer[1024];
  945. Con::expandToolScriptFilename(filePathBuffer, sizeof(filePathBuffer), pTamlSchemaFile);
  946. FileStream stream;
  947. // File opened?
  948. /*if ( !stream.open( filePathBuffer, Torque::FS::File::Write ) )
  949. {
  950. // No, so warn.
  951. Con::warnf("Taml::GenerateTamlSchema() - Could not open filename '%s' for write.", filePathBuffer );
  952. return false;
  953. }*/
  954. // Create document.
  955. tinyxml2::XMLDocument schemaDocument;
  956. // Add declaration.
  957. tinyxml2::XMLDeclaration* schemaDeclaration = schemaDocument.NewDeclaration("xml version=\"1.0\" encoding=\"iso-8859-1\" standalone =\"no\"");
  958. schemaDocument.InsertEndChild(schemaDeclaration);
  959. // Add schema element.
  960. tinyxml2::XMLElement* pSchemaElement = schemaDocument.NewElement("xs:schema");
  961. pSchemaElement->SetAttribute("xmlns:xs", "http://www.w3.org/2001/XMLSchema");
  962. schemaDocument.LinkEndChild(pSchemaElement);
  963. // Fetch class-rep root.
  964. AbstractClassRep* pRootType = AbstractClassRep::getClassList();
  965. // Fetch SimObject class rep.
  966. AbstractClassRep* pSimObjectType = AbstractClassRep::findClassRep("SimObject");
  967. // Sanity!
  968. AssertFatal(pSimObjectType != NULL, "Taml::GenerateTamlSchema() - Could not find SimObject class rep.");
  969. // Reset scratch state.
  970. char buffer[1024];
  971. HashTable<AbstractClassRep*, StringTableEntry> childGroups;
  972. // *************************************************************
  973. // Generate console type elements.
  974. // *************************************************************
  975. // Vector2.
  976. tinyxml2::XMLComment* pVector2Comment = schemaDocument.NewComment("Vector2 Console Type");
  977. pSchemaElement->LinkEndChild(pVector2Comment);
  978. tinyxml2::XMLElement* pVector2TypeElement = schemaDocument.NewElement("xs:simpleType");
  979. pVector2TypeElement->SetAttribute("name", "Vector2_ConsoleType");
  980. pSchemaElement->LinkEndChild(pVector2TypeElement);
  981. tinyxml2::XMLElement* pVector2ElementA = schemaDocument.NewElement("xs:restriction");
  982. pVector2ElementA->SetAttribute("base", "xs:string");
  983. pVector2TypeElement->LinkEndChild(pVector2ElementA);
  984. tinyxml2::XMLElement* pVector2ElementB = schemaDocument.NewElement("xs:pattern");
  985. pVector2ElementB->SetAttribute("value", "([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b");
  986. pVector2ElementA->LinkEndChild(pVector2ElementB);
  987. // Point2F.
  988. tinyxml2::XMLComment* pPoint2FComment = schemaDocument.NewComment("Point2F Console Type");
  989. pSchemaElement->LinkEndChild(pPoint2FComment);
  990. tinyxml2::XMLElement* pPoint2FTypeElement = schemaDocument.NewElement("xs:simpleType");
  991. pPoint2FTypeElement->SetAttribute("name", "Point2F_ConsoleType");
  992. pSchemaElement->LinkEndChild(pPoint2FTypeElement);
  993. tinyxml2::XMLElement* pPoint2FElementA = schemaDocument.NewElement("xs:restriction");
  994. pPoint2FElementA->SetAttribute("base", "xs:string");
  995. pPoint2FTypeElement->LinkEndChild(pPoint2FElementA);
  996. tinyxml2::XMLElement* pPoint2FElementB = schemaDocument.NewElement("xs:pattern");
  997. pPoint2FElementB->SetAttribute("value", "([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b ([-]?(\\b[0-9]+)?\\.)?[0-9]+\\b");
  998. pPoint2FElementA->LinkEndChild(pPoint2FElementB);
  999. // Point2I.
  1000. tinyxml2::XMLComment* pPoint2IComment = schemaDocument.NewComment("Point2I Console Type");
  1001. pSchemaElement->LinkEndChild(pPoint2IComment);
  1002. tinyxml2::XMLElement* pPoint2ITypeElement = schemaDocument.NewElement("xs:simpleType");
  1003. pPoint2ITypeElement->SetAttribute("name", "Point2I_ConsoleType");
  1004. pSchemaElement->LinkEndChild(pPoint2ITypeElement);
  1005. tinyxml2::XMLElement* pPoint2IElementA = schemaDocument.NewElement("xs:restriction");
  1006. pPoint2IElementA->SetAttribute("base", "xs:string");
  1007. pPoint2ITypeElement->LinkEndChild(pPoint2IElementA);
  1008. tinyxml2::XMLElement* pPoint2IElementB = schemaDocument.NewElement("xs:pattern");
  1009. pPoint2IElementB->SetAttribute("value", "[-]?[0-9]* [-]?[0-9]*");
  1010. pPoint2IElementA->LinkEndChild(pPoint2IElementB);
  1011. // b2AABB.
  1012. tinyxml2::XMLComment* pb2AABBComment = schemaDocument.NewComment("b2AABB Console Type");
  1013. pSchemaElement->LinkEndChild(pb2AABBComment);
  1014. tinyxml2::XMLElement* pb2AABBTypeElement = schemaDocument.NewElement("xs:simpleType");
  1015. pb2AABBTypeElement->SetAttribute("name", "b2AABB_ConsoleType");
  1016. pSchemaElement->LinkEndChild(pb2AABBTypeElement);
  1017. tinyxml2::XMLElement* pb2AABBElementA = schemaDocument.NewElement("xs:restriction");
  1018. pb2AABBElementA->SetAttribute("base", "xs:string");
  1019. pb2AABBTypeElement->LinkEndChild(pb2AABBElementA);
  1020. tinyxml2::XMLElement* pb2AABBElementB = schemaDocument.NewElement("xs:pattern");
  1021. 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");
  1022. pb2AABBElementA->LinkEndChild(pb2AABBElementB);
  1023. // RectI.
  1024. tinyxml2::XMLComment* pRectIComment = schemaDocument.NewComment("RectI Console Type");
  1025. pSchemaElement->LinkEndChild(pRectIComment);
  1026. tinyxml2::XMLElement* pRectITypeElement = schemaDocument.NewElement("xs:simpleType");
  1027. pRectITypeElement->SetAttribute("name", "RectI_ConsoleType");
  1028. pSchemaElement->LinkEndChild(pRectITypeElement);
  1029. tinyxml2::XMLElement* pRectIElementA = schemaDocument.NewElement("xs:restriction");
  1030. pRectIElementA->SetAttribute("base", "xs:string");
  1031. pRectITypeElement->LinkEndChild(pRectIElementA);
  1032. tinyxml2::XMLElement* pRectIElementB = schemaDocument.NewElement("xs:pattern");
  1033. pRectIElementB->SetAttribute("value", "[-]?[0-9]* [-]?[0-9]* [-]?[0-9]* [-]?[0-9]*");
  1034. pRectIElementA->LinkEndChild(pRectIElementB);
  1035. // RectF.
  1036. tinyxml2::XMLComment* pRectFComment = schemaDocument.NewComment("RectF Console Type");
  1037. pSchemaElement->LinkEndChild(pRectFComment);
  1038. tinyxml2::XMLElement* pRectFTypeElement = schemaDocument.NewElement("xs:simpleType");
  1039. pRectFTypeElement->SetAttribute("name", "RectF_ConsoleType");
  1040. pSchemaElement->LinkEndChild(pRectFTypeElement);
  1041. tinyxml2::XMLElement* pRectFElementA = schemaDocument.NewElement("xs:restriction");
  1042. pRectFElementA->SetAttribute("base", "xs:string");
  1043. pRectFTypeElement->LinkEndChild(pRectFElementA);
  1044. tinyxml2::XMLElement* pRectFElementB = schemaDocument.NewElement("xs:pattern");
  1045. pRectFElementB->SetAttribute("value", "(\\b[-]?(b[0-9]+)?\\.)?[0-9]+\\b");
  1046. pRectFElementA->LinkEndChild(pRectFElementB);
  1047. // AssetId.
  1048. tinyxml2::XMLComment* pAssetIdComment = schemaDocument.NewComment("AssetId Console Type");
  1049. pSchemaElement->LinkEndChild(pAssetIdComment);
  1050. tinyxml2::XMLElement* pAssetIdTypeElement = schemaDocument.NewElement("xs:simpleType");
  1051. pAssetIdTypeElement->SetAttribute("name", "AssetId_ConsoleType");
  1052. pSchemaElement->LinkEndChild(pAssetIdTypeElement);
  1053. tinyxml2::XMLElement* pAssetIdElementA = schemaDocument.NewElement("xs:restriction");
  1054. pAssetIdElementA->SetAttribute("base", "xs:string");
  1055. pAssetIdTypeElement->LinkEndChild(pAssetIdElementA);
  1056. tinyxml2::XMLElement* pAssetIdElementB = schemaDocument.NewElement("xs:pattern");
  1057. 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);
  1058. pAssetIdElementB->SetAttribute("value", buffer);
  1059. pAssetIdElementA->LinkEndChild(pAssetIdElementB);
  1060. // Color Enums.
  1061. tinyxml2::XMLComment* pColorEnumsComment = schemaDocument.NewComment("Color Enums");
  1062. pSchemaElement->LinkEndChild(pColorEnumsComment);
  1063. tinyxml2::XMLElement* pColorEnumsTypeElement = schemaDocument.NewElement("xs:simpleType");
  1064. pColorEnumsTypeElement->SetAttribute("name", "Color_Enums");
  1065. pSchemaElement->LinkEndChild(pColorEnumsTypeElement);
  1066. tinyxml2::XMLElement* pColorEnumsRestrictionElement = schemaDocument.NewElement("xs:restriction");
  1067. pColorEnumsRestrictionElement->SetAttribute("base", "xs:string");
  1068. pColorEnumsTypeElement->LinkEndChild(pColorEnumsRestrictionElement);
  1069. const S32 ColorEnumsCount = StockColor::getCount();
  1070. for (S32 index = 0; index < ColorEnumsCount; ++index)
  1071. {
  1072. // Add enumeration element.
  1073. tinyxml2::XMLElement* pColorEnumsAttributeEnumerationElement = schemaDocument.NewElement("xs:enumeration");
  1074. pColorEnumsAttributeEnumerationElement->SetAttribute("value", StockColor::getColorItem(index)->getColorName());
  1075. pColorEnumsRestrictionElement->LinkEndChild(pColorEnumsAttributeEnumerationElement);
  1076. }
  1077. // LinearColorF.
  1078. tinyxml2::XMLComment* pColorFValuesComment = schemaDocument.NewComment("LinearColorF Values");
  1079. pSchemaElement->LinkEndChild(pColorFValuesComment);
  1080. tinyxml2::XMLElement* pColorFValuesTypeElement = schemaDocument.NewElement("xs:simpleType");
  1081. pColorFValuesTypeElement->SetAttribute("name", "ColorF_Values");
  1082. pSchemaElement->LinkEndChild(pColorFValuesTypeElement);
  1083. tinyxml2::XMLElement* pColorFValuesElementA = schemaDocument.NewElement("xs:restriction");
  1084. pColorFValuesElementA->SetAttribute("base", "xs:string");
  1085. pColorFValuesTypeElement->LinkEndChild(pColorFValuesElementA);
  1086. tinyxml2::XMLElement* pColorFValuesElementB = schemaDocument.NewElement("xs:pattern");
  1087. 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");
  1088. pColorFValuesElementA->LinkEndChild(pColorFValuesElementB);
  1089. tinyxml2::XMLComment* pColorFComment = schemaDocument.NewComment("LinearColorF Console Type");
  1090. pSchemaElement->LinkEndChild(pColorFComment);
  1091. tinyxml2::XMLElement* pColorFTypeElement = schemaDocument.NewElement("xs:simpleType");
  1092. pColorFTypeElement->SetAttribute("name", "ColorF_ConsoleType");
  1093. pSchemaElement->LinkEndChild(pColorFTypeElement);
  1094. tinyxml2::XMLElement* pColorFUnionElement = schemaDocument.NewElement("xs:union");
  1095. pColorFUnionElement->SetAttribute("memberTypes", "ColorF_Values Color_Enums");
  1096. pColorFTypeElement->LinkEndChild(pColorFUnionElement);
  1097. // ColorI.
  1098. tinyxml2::XMLComment* pColorIValuesComment = schemaDocument.NewComment("ColorI Values");
  1099. pSchemaElement->LinkEndChild(pColorIValuesComment);
  1100. tinyxml2::XMLElement* pColorIValuesTypeElement = schemaDocument.NewElement("xs:simpleType");
  1101. pColorIValuesTypeElement->SetAttribute("name", "ColorI_Values");
  1102. pSchemaElement->LinkEndChild(pColorIValuesTypeElement);
  1103. tinyxml2::XMLElement* pColorIValuesElementA = schemaDocument.NewElement("xs:restriction");
  1104. pColorIValuesElementA->SetAttribute("base", "xs:string");
  1105. pColorIValuesTypeElement->LinkEndChild(pColorIValuesElementA);
  1106. tinyxml2::XMLElement* pColorIValuesElementB = schemaDocument.NewElement("xs:pattern");
  1107. pColorIValuesElementB->SetAttribute("value", "[-]?[0-9]* [-]?[0-9]* [-]?[0-9]* [-]?[0-9]*");
  1108. pColorIValuesElementA->LinkEndChild(pColorIValuesElementB);
  1109. tinyxml2::XMLComment* pColorIComment = schemaDocument.NewComment("ColorI Console Type");
  1110. pSchemaElement->LinkEndChild(pColorIComment);
  1111. tinyxml2::XMLElement* pColorITypeElement = schemaDocument.NewElement("xs:simpleType");
  1112. pColorITypeElement->SetAttribute("name", "ColorI_ConsoleType");
  1113. pSchemaElement->LinkEndChild(pColorITypeElement);
  1114. tinyxml2::XMLElement* pColorIUnionElement = schemaDocument.NewElement("xs:union");
  1115. pColorIUnionElement->SetAttribute("memberTypes", "ColorI_Values Color_Enums");
  1116. pColorITypeElement->LinkEndChild(pColorIUnionElement);
  1117. // *************************************************************
  1118. // Generate engine type elements.
  1119. // *************************************************************
  1120. // Generate the engine type elements.
  1121. tinyxml2::XMLComment* tComment = schemaDocument.NewComment("Type Elements");
  1122. pSchemaElement->LinkEndChild(tComment);
  1123. for (AbstractClassRep* pType = pRootType; pType != NULL; pType = pType->getNextClass())
  1124. {
  1125. // Add type.
  1126. tinyxml2::XMLElement* pTypeElement = schemaDocument.NewElement("xs:element");
  1127. pTypeElement->SetAttribute("name", pType->getClassName());
  1128. dSprintf(buffer, sizeof(buffer), "%s_Type", pType->getClassName());
  1129. pTypeElement->SetAttribute("type", buffer);
  1130. pSchemaElement->LinkEndChild(pTypeElement);
  1131. }
  1132. // *************************************************************
  1133. // Generate the engine complex types.
  1134. // *************************************************************
  1135. for (AbstractClassRep* pType = pRootType; pType != NULL; pType = pType->getNextClass())
  1136. {
  1137. // Add complex type comment.
  1138. dSprintf(buffer, sizeof(buffer), " %s Type ", pType->getClassName());
  1139. tinyxml2::XMLComment* ctComment = schemaDocument.NewComment(buffer);
  1140. pSchemaElement->LinkEndChild(ctComment);
  1141. // Add complex type.
  1142. tinyxml2::XMLElement* pComplexTypeElement = schemaDocument.NewElement("xs:complexType");
  1143. dSprintf(buffer, sizeof(buffer), "%s_Type", pType->getClassName());
  1144. pComplexTypeElement->SetAttribute("name", buffer);
  1145. pSchemaElement->LinkEndChild(pComplexTypeElement);
  1146. // Add sequence.
  1147. tinyxml2::XMLElement* pAllElement = schemaDocument.NewElement("xs:all");
  1148. pComplexTypeElement->LinkEndChild(pAllElement);
  1149. // Fetch container child class.
  1150. AbstractClassRep* pContainerChildClass = pType->getContainerChildClass(true);
  1151. // Is the type allowed children?
  1152. if (pContainerChildClass != NULL)
  1153. {
  1154. // Yes, so add choice element.
  1155. tinyxml2::XMLElement* pChoiceElement = schemaDocument.NewElement("xs:choice");
  1156. pChoiceElement->SetAttribute("minOccurs", 0);
  1157. pChoiceElement->SetAttribute("maxOccurs", "unbounded");
  1158. pAllElement->LinkEndChild(pChoiceElement);
  1159. // Find child group.
  1160. HashTable<AbstractClassRep*, StringTableEntry>::Iterator childGroupItr = childGroups.find(pContainerChildClass);
  1161. // Does the group exist?
  1162. if (childGroupItr == childGroups.end())
  1163. {
  1164. // No, so format group name.
  1165. dSprintf(buffer, sizeof(buffer), "%s_ChildrenTypes", pContainerChildClass->getClassName());
  1166. // Insert into child group hash.
  1167. childGroupItr = childGroups.insertUnique(pContainerChildClass, StringTable->insert(buffer));
  1168. // Add the group.
  1169. tinyxml2::XMLElement* pChildrenGroupElement = schemaDocument.NewElement("xs:group");
  1170. pChildrenGroupElement->SetAttribute("name", buffer);
  1171. pSchemaElement->LinkEndChild(pChildrenGroupElement);
  1172. // Add choice element.
  1173. tinyxml2::XMLElement* pChildreGroupChoiceElement = schemaDocument.NewElement("xs:choice");
  1174. pChildrenGroupElement->LinkEndChild(pChildreGroupChoiceElement);
  1175. // Add choice members.
  1176. for (AbstractClassRep* pChoiceType = pRootType; pChoiceType != NULL; pChoiceType = pChoiceType->getNextClass())
  1177. {
  1178. // Skip if not derived from the container child class.
  1179. if (!pChoiceType->isClass(pContainerChildClass))
  1180. continue;
  1181. // Add choice member.
  1182. tinyxml2::XMLElement* pChildrenMemberElement = schemaDocument.NewElement("xs:element");
  1183. pChildrenMemberElement->SetAttribute("name", pChoiceType->getClassName());
  1184. dSprintf(buffer, sizeof(buffer), "%s_Type", pChoiceType->getClassName());
  1185. pChildrenMemberElement->SetAttribute("type", buffer);
  1186. pChildreGroupChoiceElement->LinkEndChild(pChildrenMemberElement);
  1187. }
  1188. }
  1189. // Reference the child group.
  1190. tinyxml2::XMLElement* pChoiceGroupReferenceElement = schemaDocument.NewElement("xs:group");
  1191. pChoiceGroupReferenceElement->SetAttribute("ref", childGroupItr->value);
  1192. pChoiceGroupReferenceElement->SetAttribute("minOccurs", 0);
  1193. pChoiceElement->LinkEndChild(pChoiceGroupReferenceElement);
  1194. }
  1195. // Generate the custom Taml schema.
  1196. for (AbstractClassRep* pCustomSchemaType = pType; pCustomSchemaType != NULL; pCustomSchemaType = pCustomSchemaType->getParentClass())
  1197. {
  1198. // Fetch the types custom TAML schema function.
  1199. AbstractClassRep::WriteCustomTamlSchema customSchemaFn = pCustomSchemaType->getCustomTamlSchema();
  1200. // Skip if no function avilable.
  1201. if (customSchemaFn == NULL)
  1202. continue;
  1203. // Call schema generation function.
  1204. customSchemaFn(pType, pAllElement);
  1205. }
  1206. // Fetch field list.
  1207. const AbstractClassRep::FieldList& fields = pType->mFieldList;
  1208. // Fetch field count.
  1209. const S32 fieldCount = fields.size();
  1210. // Generate array attribute groups
  1211. for (S32 index = 0; index < fieldCount; ++index)
  1212. {
  1213. // Fetch field.
  1214. const AbstractClassRep::Field& field = fields[index];
  1215. if (field.type == AbstractClassRep::StartArrayFieldType)
  1216. {
  1217. // Add the top-level array identifier
  1218. tinyxml2::XMLElement* pArrayElement = schemaDocument.NewElement("xs:element");
  1219. dSprintf(buffer, sizeof(buffer), "%s.%s", pType->getClassName(), g_sanitize_schema_element_name(field.pGroupname).c_str());
  1220. pArrayElement->SetAttribute("name", buffer);
  1221. pArrayElement->SetAttribute("minOccurs", 0);
  1222. pAllElement->LinkEndChild(pArrayElement);
  1223. // Inline type specification
  1224. tinyxml2::XMLElement* pArrayComplexTypeElement = schemaDocument.NewElement("xs:complexType");
  1225. pArrayElement->LinkEndChild(pArrayComplexTypeElement);
  1226. // Add the actual (repeating) array elements
  1227. tinyxml2::XMLElement* pInnerArrayElement = schemaDocument.NewElement("xs:element");
  1228. pInnerArrayElement->SetAttribute("name", g_sanitize_schema_element_name(field.pFieldname).c_str());
  1229. pInnerArrayElement->SetAttribute("minOccurs", 0);
  1230. pInnerArrayElement->SetAttribute("maxOccurs", field.elementCount);
  1231. pArrayComplexTypeElement->LinkEndChild(pInnerArrayElement);
  1232. // Inline type specification
  1233. tinyxml2::XMLElement* pInnerComplexTypeElement = schemaDocument.NewElement("xs:complexType");
  1234. pInnerArrayElement->LinkEndChild(pInnerComplexTypeElement);
  1235. // Add a reference to the attribute group for the array
  1236. tinyxml2::XMLElement* pInnerAttributeGroupElement = schemaDocument.NewElement("xs:attributeGroup");
  1237. dSprintf(buffer, sizeof(buffer), "%s_%s_Array_Fields", pType->getClassName(), g_sanitize_schema_element_name(field.pFieldname).c_str());
  1238. pInnerAttributeGroupElement->SetAttribute("ref", buffer);
  1239. pInnerComplexTypeElement->LinkEndChild(pInnerAttributeGroupElement);
  1240. // Add the attribute group itself
  1241. tinyxml2::XMLElement* pFieldAttributeGroupElement = schemaDocument.NewElement("xs:attributeGroup");
  1242. pFieldAttributeGroupElement->SetAttribute("name", buffer);
  1243. pSchemaElement->LinkEndChild(pFieldAttributeGroupElement);
  1244. // Keep adding fields to attribute group until we hit the end of the array
  1245. for (; index < fieldCount; ++index)
  1246. {
  1247. const AbstractClassRep::Field& array_field = fields[index];
  1248. if (array_field.type == AbstractClassRep::EndArrayFieldType)
  1249. {
  1250. break;
  1251. }
  1252. tinyxml2::XMLElement* pAttributeElement = g__write_schema_attribute_element(array_field, pType, schemaDocument);
  1253. if (pAttributeElement == NULL)
  1254. {
  1255. continue;
  1256. }
  1257. pFieldAttributeGroupElement->LinkEndChild(pAttributeElement);
  1258. }
  1259. }
  1260. }
  1261. // Generate field attribute group.
  1262. tinyxml2::XMLElement* pFieldAttributeGroupElement = schemaDocument.NewElement("xs:attributeGroup");
  1263. dSprintf(buffer, sizeof(buffer), "%s_Fields", pType->getClassName());
  1264. pFieldAttributeGroupElement->SetAttribute("name", buffer);
  1265. pSchemaElement->LinkEndChild(pFieldAttributeGroupElement);
  1266. // Iterate static fields (in reverse as most types are organized from the root-fields up).
  1267. for (S32 index = fieldCount - 1; index > 0; --index)
  1268. {
  1269. // Fetch field.
  1270. const AbstractClassRep::Field& field = fields[index];
  1271. // Skip fields inside arrays
  1272. if (field.type == AbstractClassRep::EndArrayFieldType)
  1273. {
  1274. for (; index > 0; --index)
  1275. {
  1276. if (field.type == AbstractClassRep::StartArrayFieldType)
  1277. {
  1278. break;
  1279. }
  1280. }
  1281. continue;
  1282. }
  1283. tinyxml2::XMLElement* pAttributeElement = g__write_schema_attribute_element(field, pType, schemaDocument);
  1284. if (pAttributeElement == NULL)
  1285. {
  1286. continue;
  1287. }
  1288. pFieldAttributeGroupElement->LinkEndChild(pAttributeElement);
  1289. }
  1290. // Is this the SimObject Type?
  1291. if (pType == pSimObjectType)
  1292. {
  1293. // Yes, so add reserved Taml field attributes here...
  1294. // Add Taml "Name" attribute element.
  1295. tinyxml2::XMLElement* pNameAttributeElement = schemaDocument.NewElement("xs:attribute");
  1296. pNameAttributeElement->SetAttribute("name", tamlNamedObjectName);
  1297. pNameAttributeElement->SetAttribute("type", "xs:normalizedString");
  1298. pFieldAttributeGroupElement->LinkEndChild(pNameAttributeElement);
  1299. // Add Taml "TamlId" attribute element.
  1300. tinyxml2::XMLElement* pTamlIdAttributeElement = schemaDocument.NewElement("xs:attribute");
  1301. pTamlIdAttributeElement->SetAttribute("name", tamlRefIdName);
  1302. pTamlIdAttributeElement->SetAttribute("type", "xs:nonNegativeInteger");
  1303. pFieldAttributeGroupElement->LinkEndChild(pTamlIdAttributeElement);
  1304. // Add Taml "TamlRefId" attribute element.
  1305. tinyxml2::XMLElement* pTamlRefIdAttributeElement = schemaDocument.NewElement("xs:attribute");
  1306. pTamlRefIdAttributeElement->SetAttribute("name", tamlRefToIdName);
  1307. pTamlRefIdAttributeElement->SetAttribute("type", "xs:nonNegativeInteger");
  1308. pFieldAttributeGroupElement->LinkEndChild(pTamlRefIdAttributeElement);
  1309. }
  1310. // Add attribute group types.
  1311. for (AbstractClassRep* pAttributeGroupsType = pType; pAttributeGroupsType != NULL; pAttributeGroupsType = pAttributeGroupsType->getParentClass())
  1312. {
  1313. tinyxml2::XMLElement* pFieldAttributeGroupRefElement = schemaDocument.NewElement("xs:attributeGroup");
  1314. dSprintf(buffer, sizeof(buffer), "%s_Fields", pAttributeGroupsType->getClassName());
  1315. pFieldAttributeGroupRefElement->SetAttribute("ref", buffer);
  1316. pComplexTypeElement->LinkEndChild(pFieldAttributeGroupRefElement);
  1317. }
  1318. // Add "any" attribute element (dynamic fields).
  1319. tinyxml2::XMLElement* pAnyAttributeElement = schemaDocument.NewElement("xs:anyAttribute");
  1320. pAnyAttributeElement->SetAttribute("processContents", "skip");
  1321. pComplexTypeElement->LinkEndChild(pAnyAttributeElement);
  1322. }
  1323. // Write the schema document.
  1324. schemaDocument.SaveFile(filePathBuffer);
  1325. // Close file.
  1326. stream.close();
  1327. return true;
  1328. }
  1329. //-----------------------------------------------------------------------------
  1330. void Taml::WriteUnrestrictedCustomTamlSchema(const char* pCustomNodeName, const AbstractClassRep* pClassRep, tinyxml2::XMLElement* pParentElement)
  1331. {
  1332. // Sanity!
  1333. AssertFatal(pCustomNodeName != NULL, "Taml::WriteDefaultCustomTamlSchema() - Node name cannot be NULL.");
  1334. AssertFatal(pClassRep != NULL, "Taml::WriteDefaultCustomTamlSchema() - ClassRep cannot be NULL.");
  1335. AssertFatal(pParentElement != NULL, "Taml::WriteDefaultCustomTamlSchema() - Parent Element cannot be NULL.");
  1336. tinyxml2::XMLDocument* doc = pParentElement->GetDocument();
  1337. char buffer[1024];
  1338. // Add custom type element.
  1339. tinyxml2::XMLElement* pCustomElement = doc->NewElement("xs:element");
  1340. dSprintf(buffer, sizeof(buffer), "%s.%s", pClassRep->getClassName(), pCustomNodeName);
  1341. pCustomElement->SetAttribute("name", buffer);
  1342. pCustomElement->SetAttribute("minOccurs", 0);
  1343. pCustomElement->SetAttribute("maxOccurs", 1);
  1344. pParentElement->LinkEndChild(pCustomElement);
  1345. // Add complex type element.
  1346. tinyxml2::XMLElement* pComplexTypeElement = doc->NewElement("xs:complexType");
  1347. pCustomElement->LinkEndChild(pComplexTypeElement);
  1348. // Add choice element.
  1349. tinyxml2::XMLElement* pChoiceElement = doc->NewElement("xs:choice");
  1350. pChoiceElement->SetAttribute("minOccurs", 0);
  1351. pChoiceElement->SetAttribute("maxOccurs", "unbounded");
  1352. pComplexTypeElement->LinkEndChild(pChoiceElement);
  1353. // Add sequence element.
  1354. tinyxml2::XMLElement* pSequenceElement = doc->NewElement("xs:sequence");
  1355. pChoiceElement->LinkEndChild(pSequenceElement);
  1356. // Add "any" element.
  1357. tinyxml2::XMLElement* pAnyElement = doc->NewElement("xs:any");
  1358. pAnyElement->SetAttribute("processContents", "skip");
  1359. pSequenceElement->LinkEndChild(pAnyElement);
  1360. }