taml.cpp 60 KB

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