taml_ScriptBinding.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. ConsoleMethodGroupBeginWithDocs(Taml, SimObject)
  23. /*! Sets the format that Taml should use to read/write.
  24. @param format The format to use: 'xml' or 'binary'.
  25. @return No return value.
  26. */
  27. ConsoleMethodWithDocs(Taml, setFormat, ConsoleVoid, 3, 3, (format))
  28. {
  29. // Fetch format mode.
  30. const Taml::TamlFormatMode formatMode = Taml::getFormatModeEnum(argv[2]);
  31. // Was the format valid?
  32. if ( formatMode == Taml::InvalidFormat )
  33. {
  34. // No, so warn.
  35. Con::warnf( "Taml::setFormat() - Invalid format mode used: '%s'.", argv[2] );
  36. return;
  37. }
  38. // Set format mode.
  39. object->setFormatMode( formatMode );
  40. }
  41. //-----------------------------------------------------------------------------
  42. /*! Gets the format that Taml should use to read/write.
  43. @return The format that Taml should use to read/write.
  44. */
  45. ConsoleMethodWithDocs(Taml, getFormat, ConsoleString, 2, 2, ())
  46. {
  47. // Fetch format mode.
  48. return Taml::getFormatModeDescription( object->getFormatMode() );
  49. }
  50. //-----------------------------------------------------------------------------
  51. /*! Sets whether the format type is automatically determined by the filename extension or not.
  52. @param autoFormat Whether the format type is automatically determined by the filename extension or not.
  53. @return No return value.
  54. */
  55. ConsoleMethodWithDocs(Taml, setAutoFormat, ConsoleVoid, 3, 3, (autoFormat))
  56. {
  57. object->setAutoFormat( dAtob(argv[2]) );
  58. }
  59. //-----------------------------------------------------------------------------
  60. /*! Gets whether the format type is automatically determined by the filename extension or not.
  61. @return Whether the format type is automatically determined by the filename extension or not.
  62. */
  63. ConsoleMethodWithDocs(Taml, getAutoFormat, ConsoleBool, 2, 2, ())
  64. {
  65. return object->getAutoFormat();
  66. }
  67. //-----------------------------------------------------------------------------
  68. /*! Sets whether to write static fields that are at their default or not.
  69. @param writeDefaults Whether to write static fields that are at their default or not.
  70. @return No return value.
  71. */
  72. ConsoleMethodWithDocs(Taml, setWriteDefaults, ConsoleVoid, 3, 3, (writeDefaults))
  73. {
  74. object->setWriteDefaults( dAtob(argv[2]) );
  75. }
  76. //-----------------------------------------------------------------------------
  77. /*! Gets whether to write static fields that are at their default or not.
  78. @return Whether to write static fields that are at their default or not.
  79. */
  80. ConsoleMethodWithDocs(Taml, getWriteDefaults, ConsoleBool, 2, 2, ())
  81. {
  82. return object->getWriteDefaults();
  83. }
  84. //-----------------------------------------------------------------------------
  85. /*! Sets whether to update each type instances file-progenitor or not.
  86. If not updating then the progenitor stay as the script that executed the call to Taml.
  87. @param progenitorUpdate Whether to update each type instances file-progenitor or not.
  88. @return No return value.
  89. */
  90. ConsoleMethodWithDocs(Taml, setProgenitorUpdate, ConsoleVoid, 3, 3, (progenitorUpdate))
  91. {
  92. object->setProgenitorUpdate( dAtob(argv[2]) );
  93. }
  94. //-----------------------------------------------------------------------------
  95. /*! Gets whether to update each type instances file-progenitor or not.
  96. @return Whether to update each type instances file-progenitor or not.
  97. */
  98. ConsoleMethodWithDocs(Taml, getProgenitorUpdate, ConsoleBool, 2, 2, ())
  99. {
  100. return object->getProgenitorUpdate();
  101. }
  102. //-----------------------------------------------------------------------------
  103. /*! Sets the extension (end of filename) used to detect the XML format.
  104. @param extension The extension (end of filename) used to detect the XML format.
  105. @return No return value.
  106. */
  107. ConsoleMethodWithDocs(Taml, setAutoFormatXmlExtension, ConsoleVoid, 3, 3, (extension))
  108. {
  109. object->setAutoFormatXmlExtension( argv[2] );
  110. }
  111. //-----------------------------------------------------------------------------
  112. /*! Gets the extension (end of filename) used to detect the XML format.
  113. @return The extension (end of filename) used to detect the XML format.
  114. */
  115. ConsoleMethodWithDocs(Taml, getAutoFormatXmlExtension, ConsoleString, 3, 3, ())
  116. {
  117. return object->getAutoFormatXmlExtension();
  118. }
  119. //-----------------------------------------------------------------------------
  120. /*! Sets the extension (end of filename) used to detect the Binary format.
  121. @param extension The extension (end of filename) used to detect the Binary format.
  122. @return No return value.
  123. */
  124. ConsoleMethodWithDocs(Taml, setAutoFormatBinaryExtension, ConsoleVoid, 3, 3, (extension))
  125. {
  126. object->setAutoFormatBinaryExtension( argv[2] );
  127. }
  128. //-----------------------------------------------------------------------------
  129. /*! Gets the extension (end of filename) used to detect the Binary format.
  130. @return The extension (end of filename) used to detect the Binary format.
  131. */
  132. ConsoleMethodWithDocs(Taml, getAutoFormatBinaryExtension, ConsoleString, 3, 3, ())
  133. {
  134. return object->getAutoFormatBinaryExtension();
  135. }
  136. //-----------------------------------------------------------------------------
  137. /*! Sets whether ZIP compression is used on binary formatting or not.
  138. @param compressed Whether compression is on or off.
  139. @return No return value.
  140. */
  141. ConsoleMethodWithDocs(Taml, setBinaryCompression, ConsoleVoid, 3, 3, (compressed))
  142. {
  143. // Set compression.
  144. object->setBinaryCompression( dAtob(argv[2]) );
  145. }
  146. //-----------------------------------------------------------------------------
  147. /*! Gets whether ZIP compression is used on binary formatting or not.
  148. @return Whether ZIP compression is used on binary formatting or not.
  149. */
  150. ConsoleMethodWithDocs(Taml, getBinaryCompression, ConsoleBool, 2, 2, ())
  151. {
  152. // Fetch compression.
  153. return object->getBinaryCompression();
  154. }
  155. //-----------------------------------------------------------------------------
  156. /*! Sets whether to write JSON that is strictly compatible with RFC4627 or not.
  157. @param jsonStrict Whether to write JSON that is strictly compatible with RFC4627 or not.
  158. @return No return value.
  159. */
  160. ConsoleMethodWithDocs(Taml, setJSONStrict, ConsoleVoid, 3, 3, (jsonStrict))
  161. {
  162. // Set JSON Strict.
  163. object->setJSONStrict( dAtob(argv[2]) );
  164. }
  165. //-----------------------------------------------------------------------------
  166. /*! Gets whether to write JSON that is strictly compatible with RFC4627 or not.
  167. @return whether to write JSON that is strictly compatible with RFC4627 or not.
  168. */
  169. ConsoleMethodWithDocs(Taml, getJSONStrict, ConsoleBool, 2, 2, ())
  170. {
  171. // Get RFC strict.
  172. return object->getJSONStrict();
  173. }
  174. //-----------------------------------------------------------------------------
  175. /*! Writes an object to a file using Taml.
  176. @param object The object to write.
  177. @param filename The filename to write to.
  178. @return Whether the write was successful or not.
  179. */
  180. ConsoleMethodWithDocs(Taml, write, ConsoleBool, 4, 4, (object, filename))
  181. {
  182. // Fetch filename.
  183. const char* pFilename = argv[3];
  184. // Find object.
  185. SimObject* pSimObject = Sim::findObject( argv[2] );
  186. // Did we find the object?
  187. if ( pSimObject == NULL )
  188. {
  189. // No, so warn.
  190. Con::warnf( "Taml::write() - Could not find object '%s' to write to file '%s'.", argv[2], pFilename );
  191. return false;
  192. }
  193. return object->write( pSimObject, pFilename );
  194. }
  195. //-----------------------------------------------------------------------------
  196. /*! Read an object from a file using Taml.
  197. @param filename The filename to read from.
  198. @return (Object) The object read from the file or an empty string if read failed.
  199. */
  200. ConsoleMethodWithDocs(Taml, read, ConsoleString, 3, 3, (filename))
  201. {
  202. // Fetch filename.
  203. const char* pFilename = argv[2];
  204. // Read object.
  205. SimObject* pSimObject = object->read( pFilename );
  206. // Did we find the object?
  207. if ( pSimObject == NULL )
  208. {
  209. // No, so warn.
  210. Con::warnf( "Taml::read() - Could not read object from file '%s'.", pFilename );
  211. return StringTable->EmptyString;
  212. }
  213. return pSimObject->getIdString();
  214. }
  215. //-----------------------------------------------------------------------------
  216. ConsoleMethodGroupEndWithDocs(Taml)
  217. /*! Writes an object to a file using Taml.
  218. @param object The object to write.
  219. @param filename The filename to write to.
  220. @param format The file format to use. Optional: Defaults to 'xml'. Can be set to 'binary'.
  221. @param compressed Whether ZIP compression is used on binary formatting or not. Optional: Defaults to 'true'.
  222. @return Whether the write was successful or not.
  223. */
  224. ConsoleFunctionWithDocs(TamlWrite, ConsoleBool, 3, 5, (object, filename, [format]?, [compressed]?))
  225. {
  226. // Fetch filename.
  227. const char* pFilename = argv[2];
  228. // Find object.
  229. SimObject* pSimObject = Sim::findObject( argv[1] );
  230. // Did we find the object?
  231. if ( pSimObject == NULL )
  232. {
  233. // No, so warn.
  234. Con::warnf( "TamlWrite() - Could not find object '%s' to write to file '%s'.", argv[2], pFilename );
  235. return false;
  236. }
  237. Taml taml;
  238. // Was the format mode specified?
  239. if ( argc > 3 )
  240. {
  241. // Yes, so set it.
  242. taml.setFormatMode( Taml::getFormatModeEnum( argv[3] ) );
  243. // Was binary compression specified?
  244. if ( argc > 4 )
  245. {
  246. // Yes, so is the format mode binary?
  247. if ( taml.getFormatMode() == Taml::BinaryFormat )
  248. {
  249. // Yes, so set binary compression.
  250. taml.setBinaryCompression( dAtob(argv[4]) );
  251. }
  252. else
  253. {
  254. // No, so warn.
  255. Con::warnf( "TamlWrite() - Setting binary compression is only valid for XML formatting." );
  256. }
  257. }
  258. // Turn-off auto-formatting.
  259. taml.setAutoFormat( false );
  260. }
  261. // Write.
  262. return taml.write( pSimObject, pFilename );
  263. }
  264. //-----------------------------------------------------------------------------
  265. /*! Read an object from a file using Taml.
  266. @param filename The filename to read from.
  267. @param format The file format to use. Optional: Defaults to 'xml'. Can be set to 'binary'.
  268. @return (Object) The object read from the file or an empty string if read failed.
  269. */
  270. ConsoleFunctionWithDocs(TamlRead, ConsoleString, 2, 4, (filename, [format]?))
  271. {
  272. // Fetch filename.
  273. const char* pFilename = argv[1];
  274. // Set the format mode.
  275. Taml taml;
  276. // Was a format mode specified?
  277. if ( argc > 2 )
  278. {
  279. // Yes, so set it.
  280. taml.setFormatMode( Taml::getFormatModeEnum( argv[2] ) );
  281. // Turn-off auto-formatting.
  282. taml.setAutoFormat( false );
  283. }
  284. // Read object.
  285. SimObject* pSimObject = taml.read( pFilename );
  286. // Did we find the object?
  287. if ( pSimObject == NULL )
  288. {
  289. // No, so warn.
  290. Con::warnf( "TamlRead() - Could not read object from file '%s'.", pFilename );
  291. return StringTable->EmptyString;
  292. }
  293. return pSimObject->getIdString();
  294. }
  295. //-----------------------------------------------------------------------------
  296. /*! Generate a TAML schema file of all engine types.
  297. The schema file is specified using the console variable ' TAML_SCHEMA_VARIABLE '.
  298. @return Whether the schema file was writtent or not.
  299. */
  300. ConsoleFunctionWithDocs(GenerateTamlSchema, ConsoleBool, 1, 1, ())
  301. {
  302. // Generate the schema.
  303. return Taml::generateTamlSchema();
  304. }