|
@@ -52,9 +52,10 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
#include <assimp/scene.h>
|
|
#include <assimp/scene.h>
|
|
#include <assimp/Importer.hpp>
|
|
#include <assimp/Importer.hpp>
|
|
|
|
|
|
-#include <fstream>
|
|
|
|
|
|
+
|
|
#include <iomanip>
|
|
#include <iomanip>
|
|
#include <memory>
|
|
#include <memory>
|
|
|
|
+#include <sstream>
|
|
|
|
|
|
static const aiImporterDesc desc = { "MMD Importer",
|
|
static const aiImporterDesc desc = { "MMD Importer",
|
|
"",
|
|
"",
|
|
@@ -102,26 +103,32 @@ const aiImporterDesc *MMDImporter::GetInfo() const {
|
|
// ------------------------------------------------------------------------------------------------
|
|
// ------------------------------------------------------------------------------------------------
|
|
// MMD import implementation
|
|
// MMD import implementation
|
|
void MMDImporter::InternReadFile(const std::string &file, aiScene *pScene,
|
|
void MMDImporter::InternReadFile(const std::string &file, aiScene *pScene,
|
|
- IOSystem * /*pIOHandler*/) {
|
|
|
|
- // Read file by istream
|
|
|
|
- std::filebuf fb;
|
|
|
|
- if (!fb.open(file, std::ios::in | std::ios::binary)) {
|
|
|
|
- throw DeadlyImportError("Failed to open file ", file, ".");
|
|
|
|
- }
|
|
|
|
|
|
+ IOSystem* pIOHandler) {
|
|
|
|
+
|
|
|
|
+ auto streamCloser = [&](IOStream *pStream) {
|
|
|
|
+ pIOHandler->Close(pStream);
|
|
|
|
+ };
|
|
|
|
|
|
- std::istream fileStream(&fb);
|
|
|
|
|
|
+ static const std::string mode = "rb";
|
|
|
|
+ const std::unique_ptr<IOStream, decltype(streamCloser)> fileStream(pIOHandler->Open(file, mode), streamCloser);
|
|
|
|
|
|
- // Get the file-size and validate it, throwing an exception when fails
|
|
|
|
- fileStream.seekg(0, fileStream.end);
|
|
|
|
- size_t fileSize = static_cast<size_t>(fileStream.tellg());
|
|
|
|
- fileStream.seekg(0, fileStream.beg);
|
|
|
|
|
|
+ if (fileStream == nullptr) {
|
|
|
|
+ throw DeadlyImportError("Failed to open file ", file, ".");
|
|
|
|
+ }
|
|
|
|
|
|
- if (fileSize < sizeof(pmx::PmxModel)) {
|
|
|
|
|
|
+ const size_t fileSize = fileStream->FileSize();
|
|
|
|
+ if (fileSize < sizeof(pmx::PmxModel))
|
|
|
|
+ {
|
|
throw DeadlyImportError(file, " is too small.");
|
|
throw DeadlyImportError(file, " is too small.");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ std::vector<char> contents(fileStream->FileSize());
|
|
|
|
+ fileStream->Read(contents.data(), 1, contents.size());
|
|
|
|
+
|
|
|
|
+ std::istringstream iss(std::string(contents.begin(), contents.end()));
|
|
|
|
+
|
|
pmx::PmxModel model;
|
|
pmx::PmxModel model;
|
|
- model.Read(&fileStream);
|
|
|
|
|
|
+ model.Read(&iss);
|
|
|
|
|
|
CreateDataFromImport(&model, pScene);
|
|
CreateDataFromImport(&model, pScene);
|
|
}
|
|
}
|