WriteDump.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2024, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file WriteDump.cpp
  35. * @brief Implementation of the 'assimp dump' utility
  36. */
  37. #include "Main.h"
  38. #include "PostProcessing/ProcessHelper.h"
  39. const char *AICMD_MSG_DUMP_HELP =
  40. "assimp dump <model> [<out>] [-b] [-s] [-z] [common parameters]\n"
  41. "\t -b Binary output \n"
  42. "\t -s Shortened \n"
  43. "\t -z Compressed \n"
  44. "\t[See the assimp_cmd docs for a full list of all common parameters] \n"
  45. "\t -cfast Fast post processing preset, runs just a few important steps \n"
  46. "\t -cdefault Default post processing: runs all recommended steps\n"
  47. "\t -cfull Fires almost all post processing steps \n";
  48. #include "Common/assbin_chunks.h"
  49. #include <assimp/DefaultIOSystem.h>
  50. #include "AssetLib/Assbin/AssbinFileWriter.h"
  51. #include "AssetLib/Assxml/AssxmlFileWriter.h"
  52. #include <memory>
  53. FILE *out = nullptr;
  54. bool shortened = false;
  55. #ifndef ASSIMP_BUILD_NO_EXPORT
  56. // -----------------------------------------------------------------------------------
  57. int Assimp_Dump(const char *const *params, unsigned int num) {
  58. const char *fail = "assimp dump: Invalid number of arguments. "
  59. "See \'assimp dump --help\'\r\n";
  60. // --help
  61. if (!strcmp(params[0], "-h") || !strcmp(params[0], "--help") || !strcmp(params[0], "-?")) {
  62. printf("%s", AICMD_MSG_DUMP_HELP);
  63. return AssimpCmdError::Success;
  64. }
  65. // asssimp dump in out [options]
  66. if (num < 1) {
  67. printf("%s", fail);
  68. return AssimpCmdError::InvalidNumberOfArguments;
  69. }
  70. std::string in = std::string(params[0]);
  71. std::string cur_out = (num > 1 ? std::string(params[1]) : std::string("-"));
  72. // store full command line
  73. std::string cmd;
  74. for (unsigned int i = (cur_out[0] == '-' ? 1 : 2); i < num; ++i) {
  75. if (!params[i]) continue;
  76. cmd.append(params[i]);
  77. cmd.append(" ");
  78. }
  79. // get import flags
  80. ImportData import;
  81. ProcessStandardArguments(import, params + 1, num - 1);
  82. bool binary = false, cur_shortened = false, compressed = false;
  83. // process other flags
  84. for (unsigned int i = 1; i < num; ++i) {
  85. if (!params[i]) {
  86. continue;
  87. }
  88. if (!strcmp(params[i], "-b") || !strcmp(params[i], "--binary")) {
  89. binary = true;
  90. } else if (!strcmp(params[i], "-s") || !strcmp(params[i], "--short")) {
  91. cur_shortened = true;
  92. } else if (!strcmp(params[i], "-z") || !strcmp(params[i], "--compressed")) {
  93. compressed = true;
  94. }
  95. #if 0
  96. else if (i > 2 || params[i][0] == '-') {
  97. ::printf("Unknown parameter: %s\n",params[i]);
  98. return 10;
  99. }
  100. #endif
  101. }
  102. if (cur_out[0] == '-') {
  103. // take file name from input file
  104. std::string::size_type pos = in.find_last_of('.');
  105. if (pos == std::string::npos) {
  106. pos = in.length();
  107. }
  108. cur_out = in.substr(0, pos);
  109. cur_out.append((binary ? ".assbin" : ".assxml"));
  110. if (cur_shortened && binary) {
  111. cur_out.append(".regress");
  112. }
  113. }
  114. // import the main model
  115. const aiScene *scene = ImportModel(import, in);
  116. if (!scene) {
  117. printf("assimp dump: Unable to load input file %s\n", in.c_str());
  118. return AssimpCmdError::FailedToLoadInputFile;
  119. }
  120. try {
  121. // Dump the main model, using the appropriate method.
  122. std::unique_ptr<IOSystem> pIOSystem(new DefaultIOSystem());
  123. if (binary) {
  124. DumpSceneToAssbin(cur_out.c_str(), cmd.c_str(), pIOSystem.get(),
  125. scene, shortened, compressed);
  126. } else {
  127. DumpSceneToAssxml(cur_out.c_str(), cmd.c_str(), pIOSystem.get(),
  128. scene, shortened);
  129. }
  130. } catch (const std::exception &e) {
  131. printf("%s", ("assimp dump: " + std::string(e.what())).c_str());
  132. return AssimpCmdError::ExceptionWasRaised;
  133. } catch (...) {
  134. printf("assimp dump: An unknown exception occurred.\n");
  135. return AssimpCmdError::ExceptionWasRaised;
  136. }
  137. printf("assimp dump: Wrote output dump %s\n", cur_out.c_str());
  138. return AssimpCmdError::Success;
  139. }
  140. #else
  141. int Assimp_Dump(const char *const *, unsigned int ) {
  142. printf("assimp dump: Export disabled.\n");
  143. return AssimpCmdError::UnrecognizedCommand;
  144. }
  145. #endif