From f26dc74f21202a1d83634b68cb281798a3a21374 Mon Sep 17 00:00:00 2001 From: RedSkittleFox Date: Wed, 13 Apr 2022 02:51:12 +0200 Subject: [PATCH] Fixed MMD Importer not loading files from buffer and enabled unit test --- code/AssetLib/MMD/MMDImporter.cpp | 33 +++++++++++++++++++------------ test/unit/utPMXImporter.cpp | 6 +++--- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/code/AssetLib/MMD/MMDImporter.cpp b/code/AssetLib/MMD/MMDImporter.cpp index e0e68f7c5..4468cb775 100644 --- a/code/AssetLib/MMD/MMDImporter.cpp +++ b/code/AssetLib/MMD/MMDImporter.cpp @@ -55,6 +55,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include static const aiImporterDesc desc = { "MMD Importer", "", @@ -102,26 +103,32 @@ const aiImporterDesc *MMDImporter::GetInfo() const { // ------------------------------------------------------------------------------------------------ // MMD import implementation 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)) { + IOSystem* pIOHandler) { + + auto streamCloser = [&](IOStream *pStream) { + pIOHandler->Close(pStream); + }; + + static const std::string mode = "rb"; + const std::unique_ptr fileStream(pIOHandler->Open(file, mode), streamCloser); + + if (fileStream == nullptr) { throw DeadlyImportError("Failed to open file ", file, "."); } - std::istream fileStream(&fb); - - // Get the file-size and validate it, throwing an exception when fails - fileStream.seekg(0, fileStream.end); - size_t fileSize = static_cast(fileStream.tellg()); - fileStream.seekg(0, fileStream.beg); - - if (fileSize < sizeof(pmx::PmxModel)) { + const size_t fileSize = fileStream->FileSize(); + if (fileSize < sizeof(pmx::PmxModel)) + { throw DeadlyImportError(file, " is too small."); } + std::vector contents(fileStream->FileSize()); + fileStream->Read(std::data(contents), 1, std::size(contents)); + + std::istrstream is(static_cast(std::data(contents)), std::size(contents)); + pmx::PmxModel model; - model.Read(&fileStream); + model.Read(&is); CreateDataFromImport(&model, pScene); } diff --git a/test/unit/utPMXImporter.cpp b/test/unit/utPMXImporter.cpp index a0d663d29..167fb234a 100644 --- a/test/unit/utPMXImporter.cpp +++ b/test/unit/utPMXImporter.cpp @@ -42,6 +42,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "UnitTestPCH.h" #include "AbstractImportExportBase.h" #include "AssetLib/MMD/MMDImporter.h" +#include "assimp/postprocess.h" #include @@ -51,9 +52,8 @@ class utPMXImporter : public AbstractImportExportBase { public: virtual bool importerTest() { Assimp::Importer importer; - /*const aiScene *scene = importer.ReadFile( ASSIMP_TEST_MODELS_DIR "/../models-nonbsd/MMD/Alicia_blade.pmx", aiProcess_ValidateDataStructure ); - return nullptr != scene;*/ - return true; + const aiScene *scene = importer.ReadFile( ASSIMP_TEST_MODELS_DIR "/../models-nonbsd/MMD/Alicia_blade.pmx", aiProcess_ValidateDataStructure ); + return nullptr != scene; } };