taml.cpp 58 KB

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