zipObject_ScriptBinding.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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(ZipObject, SimObject)
  23. static const struct
  24. {
  25. const char *strMode;
  26. Zip::ZipArchive::AccessMode mode;
  27. } gModeMap[]=
  28. {
  29. { "read", Zip::ZipArchive::Read },
  30. { "write", Zip::ZipArchive::Write },
  31. { "readwrite", Zip::ZipArchive::ReadWrite },
  32. { NULL, (Zip::ZipArchive::AccessMode)0 }
  33. };
  34. /*! Open a zip file
  35. @param filename The file's name
  36. @param accessMode The mode in which to open the file (default Read)
  37. @return Returns true on success, false otherwise
  38. @sa closeArchive, openFileForRead, openFileForWrite, closeFile
  39. */
  40. ConsoleMethodWithDocs(ZipObject, openArchive, ConsoleBool, 3, 4, (filename, [accessMode = Read]?))
  41. {
  42. Zip::ZipArchive::AccessMode mode = Zip::ZipArchive::Read;
  43. if(argc > 3)
  44. {
  45. for(S32 i = 0;gModeMap[i].strMode;++i)
  46. {
  47. if(dStricmp(gModeMap[i].strMode, argv[3]) == 0)
  48. {
  49. mode = gModeMap[i].mode;
  50. break;
  51. }
  52. }
  53. }
  54. char buf[512];
  55. Con::expandPath(buf, sizeof(buf), argv[2]);
  56. return object->openArchive(buf, mode);
  57. }
  58. /*! Close the zip file
  59. @return No return value.
  60. */
  61. ConsoleMethodWithDocs(ZipObject, closeArchive, ConsoleVoid, 2, 2, ())
  62. {
  63. object->closeArchive();
  64. }
  65. //////////////////////////////////////////////////////////////////////////
  66. /*! Open a file within the zip for reading
  67. @param filename The file's name to open in current zip file
  68. @return The file stream ID as an integer or zero on failure.
  69. */
  70. ConsoleMethodWithDocs(ZipObject, openFileForRead, ConsoleInt, 3, 3, (filename))
  71. {
  72. StreamObject *stream = object->openFileForRead(argv[2]);
  73. return stream ? stream->getId() : 0;
  74. }
  75. /*! Open a file within the zip for writing
  76. @param filename The file's name to open in current zip file
  77. @return The file stream ID as an integer or 0 on failure.
  78. */
  79. ConsoleMethodWithDocs(ZipObject, openFileForWrite, ConsoleInt, 3, 3, (filename))
  80. {
  81. StreamObject *stream = object->openFileForWrite(argv[2]);
  82. return stream ? stream->getId() : 0;
  83. }
  84. /*! Close a file within the zip
  85. @param stream The file stream ID
  86. @return No return value.
  87. */
  88. ConsoleMethodWithDocs(ZipObject, closeFile, ConsoleVoid, 3, 3, (stream))
  89. {
  90. StreamObject *stream = dynamic_cast<StreamObject *>(Sim::findObject(argv[2]));
  91. if(stream == NULL)
  92. {
  93. Con::errorf("ZipObject::closeFile - Invalid stream specified");
  94. return;
  95. }
  96. object->closeFile(stream);
  97. }
  98. //////////////////////////////////////////////////////////////////////////
  99. /*! Add a file to the zip
  100. @param filename The name of the file
  101. @param pathInZip The internal (to the zip) path to the file
  102. @param replace Set whether to replace the file if one already exists with the name in the path (default true)
  103. @return Returns true on success and false otherwise
  104. */
  105. ConsoleMethodWithDocs(ZipObject, addFile, ConsoleBool, 4, 5, (filename, pathInZip, [replace = true]?))
  106. {
  107. // Left this line commented out as it was useful when i had a problem
  108. // with the zip's i was creating. [2/21/2007 justind]
  109. // [tom, 2/21/2007] To is now a warnf() for better visual separation in the console
  110. // Con::errorf("zipAdd: %s", argv[2]);
  111. //Con::warnf(" To: %s", argv[3]);
  112. return object->addFile(argv[2], argv[3], argc > 4 ? dAtob(argv[4]) : true);
  113. }
  114. /*! Extract a file from the zip
  115. @param pathInZip The internal path of the desired file
  116. @param filename The file's name
  117. @return Returns true on success and false otherwise
  118. */
  119. ConsoleMethodWithDocs(ZipObject, extractFile, ConsoleBool, 4, 4, (pathInZip, filename))
  120. {
  121. return object->extractFile(argv[2], argv[3]);
  122. }
  123. /*! Delete a file from the zip
  124. @param pathInZip The full internal path of the file
  125. @return Returns true on success and false otherwise.
  126. */
  127. ConsoleMethodWithDocs(ZipObject, deleteFile, ConsoleBool, 3, 3, (pathInZip))
  128. {
  129. return object->deleteFile(argv[2]);
  130. }
  131. //////////////////////////////////////////////////////////////////////////
  132. /*! Get number of files in the zip
  133. @return Returns the number of files found in zip
  134. */
  135. ConsoleMethodWithDocs(ZipObject, getFileEntryCount, ConsoleInt, 2, 2, ())
  136. {
  137. return object->getFileEntryCount();
  138. }
  139. /*! Get file entry.
  140. @param index Index to file entry
  141. @return Returns tab separated string containing filename, uncompressed size, compressed size, compression method and CRC32
  142. */
  143. ConsoleMethodWithDocs(ZipObject, getFileEntry, ConsoleString, 3, 3, (index))
  144. {
  145. return object->getFileEntry(dAtoi(argv[2]));
  146. }
  147. ConsoleMethodGroupEndWithDocs(ZipObject)