taml_ScriptBinding.h 13 KB

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