1010#include < stdio.h>
1111#include < iostream>
1212
13- static const aiScene* ImportModelByMainFile (Assimp::Importer& importer, const File* file)
13+ static const aiScene* ImportFileListByMainFile (Assimp::Importer& importer, const File* file)
1414{
1515 try {
1616 const aiScene* scene = importer.ReadFile (file->path ,
@@ -30,24 +30,8 @@ static std::string CreateErrorJson (const std::string& errorCode)
3030 return " {\" error\" :\" " + errorCode + " \" }" ;
3131}
3232
33- std::string ImportModel (const FileList& fileList )
33+ static std::string CreateResultJson (const aiScene* scene )
3434{
35- if (fileList.FileCount () == 0 ) {
36- return CreateErrorJson (" no_file_specified" );
37- }
38-
39- Assimp::Importer importer;
40- importer.SetIOHandler (new FileListIOSystemAdapter (fileList));
41-
42- const aiScene* scene = nullptr ;
43- for (size_t fileIndex = 0 ; fileIndex < fileList.FileCount (); fileIndex++) {
44- const File* file = fileList.GetFile (fileIndex);
45- scene = ImportModelByMainFile (importer, file);
46- if (scene != nullptr ) {
47- break ;
48- }
49- }
50-
5135 if (scene == nullptr ) {
5236 return CreateErrorJson (" model_import_failed" );
5337 }
@@ -68,16 +52,92 @@ std::string ImportModel (const FileList& fileList)
6852 return resultJson;
6953}
7054
55+ std::string ImportFile (const File& file, const FileLoader& loader)
56+ {
57+ Assimp::Importer importer;
58+ importer.SetIOHandler (new DelayLoadedIOSystemAdapter (file, loader));
59+ const aiScene* scene = ImportFileListByMainFile (importer, &file);
60+ return CreateResultJson (scene);
61+ }
62+
63+ std::string ImportFileList (const FileList& fileList)
64+ {
65+ if (fileList.FileCount () == 0 ) {
66+ return CreateErrorJson (" no_file_specified" );
67+ }
68+
69+ Assimp::Importer importer;
70+ importer.SetIOHandler (new FileListIOSystemAdapter (fileList));
71+
72+ const aiScene* scene = nullptr ;
73+ for (size_t fileIndex = 0 ; fileIndex < fileList.FileCount (); fileIndex++) {
74+ const File* file = fileList.GetFile (fileIndex);
75+ scene = ImportFileListByMainFile (importer, file);
76+ if (scene != nullptr ) {
77+ break ;
78+ }
79+ }
80+
81+ return CreateResultJson (scene);
82+ }
83+
7184#ifdef EMSCRIPTEN
7285
86+ std::string ImportFileEmscripten (
87+ const std::string& name,
88+ const emscripten::val& content,
89+ const emscripten::val& existsFunc,
90+ const emscripten::val& loadFunc)
91+ {
92+ class FileLoaderEmscripten : public FileLoader
93+ {
94+ public:
95+ FileLoaderEmscripten (const emscripten::val& existsFunc, const emscripten::val& loadFunc) :
96+ existsFunc (existsFunc),
97+ loadFunc (loadFunc)
98+ {
99+ }
100+
101+ virtual bool Exists (const char * pFile) const override
102+ {
103+ if (existsFunc.isUndefined () || existsFunc.isNull ()) {
104+ return false ;
105+ }
106+ std::string fileName = GetFileName (pFile);
107+ emscripten::val exists = existsFunc (fileName);
108+ return exists.as <bool > ();
109+ }
110+
111+ virtual Buffer Load (const char * pFile) const override
112+ {
113+ if (loadFunc.isUndefined () || loadFunc.isNull ()) {
114+ return {};
115+ }
116+ std::string fileName = GetFileName (pFile);
117+ emscripten::val fileBuffer = loadFunc (fileName);
118+ return emscripten::vecFromJSArray<std::uint8_t > (fileBuffer);
119+ }
120+
121+ private:
122+ const emscripten::val& existsFunc;
123+ const emscripten::val& loadFunc;
124+ };
125+
126+ Buffer buffer = emscripten::vecFromJSArray<std::uint8_t > (content);
127+ File file (name, buffer);
128+ FileLoaderEmscripten loader (existsFunc, loadFunc);
129+ return ImportFile (file, loader);
130+ }
131+
73132EMSCRIPTEN_BINDINGS (assimpjs)
74133{
75134 emscripten::class_<FileList> (" FileList" )
76135 .constructor <> ()
77136 .function (" AddFile" , &FileList::AddFileEmscripten)
78137 ;
79138
80- emscripten::function<std::string, const FileList&> (" ImportModel" , &ImportModel);
139+ emscripten::function<std::string, const std::string&, const emscripten::val&, const emscripten::val&, const emscripten::val&> (" ImportFile" , &ImportFileEmscripten);
140+ emscripten::function<std::string, const FileList&> (" ImportFileList" , &ImportFileList);
81141}
82142
83143#endif
0 commit comments