taml_ScriptBinding.h 14 KB

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