Simplify importer interface.
This commit is contained in:
parent
afc741203a
commit
33a8464321
@ -53,14 +53,15 @@ global.FileReader = class
|
||||
}
|
||||
};
|
||||
|
||||
importer.LoadFilesFromFileObjects (files, function () {
|
||||
let settings = new OV.ImportSettings ();
|
||||
importer.Import (settings, {
|
||||
onSuccess : function (importResult) {
|
||||
console.log (importResult);
|
||||
},
|
||||
onError : function (importError) {
|
||||
console.log (importError);
|
||||
}
|
||||
});
|
||||
let settings = new OV.ImportSettings ();
|
||||
importer.ImportFilesFromFileObjects (files, settings, {
|
||||
onFilesLoaded : function () {
|
||||
|
||||
},
|
||||
onImportSuccess : function (importResult) {
|
||||
console.log (importResult);
|
||||
},
|
||||
onImportError : function (importError) {
|
||||
console.log (importError);
|
||||
}
|
||||
});
|
||||
|
||||
@ -104,21 +104,68 @@ OV.Importer = class
|
||||
this.importers.push (importer);
|
||||
}
|
||||
|
||||
LoadFilesFromUrls (fileList, onReady)
|
||||
ImportFilesFromUrls (urlList, settings, callbacks)
|
||||
{
|
||||
this.LoadFiles (fileList, OV.FileSource.Url, onReady);
|
||||
this.ImportFiles (urlList, OV.FileSource.Url, settings, callbacks);
|
||||
}
|
||||
|
||||
LoadFilesFromFileObjects (fileList, onReady)
|
||||
ImportFilesFromFileObjects (fileList, settings, callbacks)
|
||||
{
|
||||
this.LoadFiles (fileList, OV.FileSource.File, onReady);
|
||||
this.ImportFiles (fileList, OV.FileSource.File, settings, callbacks);
|
||||
}
|
||||
|
||||
Import (settings, callbacks)
|
||||
ImportFiles (fileList, fileSource, settings, callbacks)
|
||||
{
|
||||
this.LoadFiles (fileList, fileSource, () => {
|
||||
callbacks.onFilesLoaded ();
|
||||
OV.RunTaskAsync (() => {
|
||||
this.ImportLoadedFiles (settings, callbacks);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
LoadFiles (fileList, fileSource, onReady)
|
||||
{
|
||||
let newFileList = new OV.FileList (this.importers);
|
||||
if (fileSource === OV.FileSource.Url) {
|
||||
newFileList.FillFromFileUrls (fileList);
|
||||
} else if (fileSource === OV.FileSource.File) {
|
||||
newFileList.FillFromFileObjects (fileList);
|
||||
}
|
||||
let reset = false;
|
||||
if (this.HasMainFile (newFileList)) {
|
||||
reset = true;
|
||||
} else {
|
||||
let foundMissingFile = false;
|
||||
for (let i = 0; i < this.missingFiles.length; i++) {
|
||||
let missingFile = this.missingFiles[i];
|
||||
if (newFileList.ContainsFileByPath (missingFile)) {
|
||||
foundMissingFile = true;
|
||||
}
|
||||
}
|
||||
if (!foundMissingFile) {
|
||||
reset = true;
|
||||
} else {
|
||||
let newFiles = newFileList.GetFiles ();
|
||||
this.fileList.ExtendFromFileList (newFiles);
|
||||
reset = false;
|
||||
}
|
||||
}
|
||||
if (reset) {
|
||||
this.fileList = newFileList;
|
||||
}
|
||||
this.fileList.GetContent (() => {
|
||||
this.DecompressArchives (this.fileList, () => {
|
||||
onReady ();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
ImportLoadedFiles (settings, callbacks)
|
||||
{
|
||||
let mainFile = this.GetMainFile (this.fileList);
|
||||
if (mainFile === null || mainFile.file === null || mainFile.file.content === null) {
|
||||
callbacks.onError (new OV.ImportError (OV.ImportErrorCode.NoImportableFile, null));
|
||||
callbacks.onImportError (new OV.ImportError (OV.ImportErrorCode.NoImportableFile, null));
|
||||
return;
|
||||
}
|
||||
|
||||
@ -161,11 +208,11 @@ OV.Importer = class
|
||||
result.usedFiles = this.usedFiles;
|
||||
result.missingFiles = this.missingFiles;
|
||||
result.upVector = importer.GetUpDirection ();
|
||||
callbacks.onSuccess (result);
|
||||
callbacks.onImportSuccess (result);
|
||||
},
|
||||
onError : () => {
|
||||
let message = importer.GetErrorMessage ();
|
||||
callbacks.onError (new OV.ImportError (OV.ImportErrorCode.ImportFailed, message));
|
||||
callbacks.onImportError (new OV.ImportError (OV.ImportErrorCode.ImportFailed, message));
|
||||
},
|
||||
onComplete : () => {
|
||||
importer.Clear ();
|
||||
@ -173,43 +220,6 @@ OV.Importer = class
|
||||
});
|
||||
}
|
||||
|
||||
LoadFiles (fileList, fileSource, onReady)
|
||||
{
|
||||
let newFileList = new OV.FileList (this.importers);
|
||||
if (fileSource === OV.FileSource.Url) {
|
||||
newFileList.FillFromFileUrls (fileList);
|
||||
} else if (fileSource === OV.FileSource.File) {
|
||||
newFileList.FillFromFileObjects (fileList);
|
||||
}
|
||||
let reset = false;
|
||||
if (this.HasMainFile (newFileList)) {
|
||||
reset = true;
|
||||
} else {
|
||||
let foundMissingFile = false;
|
||||
for (let i = 0; i < this.missingFiles.length; i++) {
|
||||
let missingFile = this.missingFiles[i];
|
||||
if (newFileList.ContainsFileByPath (missingFile)) {
|
||||
foundMissingFile = true;
|
||||
}
|
||||
}
|
||||
if (!foundMissingFile) {
|
||||
reset = true;
|
||||
} else {
|
||||
let newFiles = newFileList.GetFiles ();
|
||||
this.fileList.ExtendFromFileList (newFiles);
|
||||
reset = false;
|
||||
}
|
||||
}
|
||||
if (reset) {
|
||||
this.fileList = newFileList;
|
||||
}
|
||||
this.fileList.GetContent (() => {
|
||||
this.DecompressArchives (this.fileList, () => {
|
||||
onReady ();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
DecompressArchives (fileList, onReady)
|
||||
{
|
||||
let files = fileList.GetFiles ();
|
||||
@ -254,10 +264,21 @@ OV.Importer = class
|
||||
|
||||
GetMainFile (fileList)
|
||||
{
|
||||
function FindImporter (file, importers)
|
||||
{
|
||||
for (let importerIndex = 0; importerIndex < importers.length; importerIndex++) {
|
||||
let importer = importers[importerIndex];
|
||||
if (importer.CanImportExtension (file.extension)) {
|
||||
return importer;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
let files = fileList.GetFiles ();
|
||||
for (let fileIndex = 0; fileIndex < files.length; fileIndex++) {
|
||||
let file = files[fileIndex];
|
||||
let importer = this.FindImporter (file);
|
||||
let importer = FindImporter (file, this.importers);
|
||||
if (importer !== null) {
|
||||
return {
|
||||
file : file,
|
||||
@ -268,17 +289,6 @@ OV.Importer = class
|
||||
return null;
|
||||
}
|
||||
|
||||
FindImporter (file)
|
||||
{
|
||||
for (let importerIndex = 0; importerIndex < this.importers.length; importerIndex++) {
|
||||
let importer = this.importers[importerIndex];
|
||||
if (importer.CanImportExtension (file.extension)) {
|
||||
return importer;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
RevokeModelUrls ()
|
||||
{
|
||||
if (this.model === null) {
|
||||
|
||||
@ -16,18 +16,15 @@ OV.ThreeModelLoader = class
|
||||
|
||||
LoadFromUrlList (urls, settings)
|
||||
{
|
||||
if (this.inProgress) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.inProgress = true;
|
||||
this.callbacks.onLoadStart ();
|
||||
this.importer.LoadFilesFromUrls (urls, () => {
|
||||
this.OnFilesLoaded (settings);
|
||||
});
|
||||
this.LoadFromSource (urls, OV.FileSource.Url, settings);
|
||||
}
|
||||
|
||||
LoadFromFileList (files, settings)
|
||||
{
|
||||
this.LoadFromSource (files, OV.FileSource.File, settings);
|
||||
}
|
||||
|
||||
LoadFromSource (files, fileSource, settings)
|
||||
{
|
||||
if (this.inProgress) {
|
||||
return;
|
||||
@ -35,24 +32,17 @@ OV.ThreeModelLoader = class
|
||||
|
||||
this.inProgress = true;
|
||||
this.callbacks.onLoadStart ();
|
||||
this.importer.LoadFilesFromFileObjects (files, () => {
|
||||
this.OnFilesLoaded (settings);
|
||||
});
|
||||
}
|
||||
|
||||
OnFilesLoaded (settings)
|
||||
{
|
||||
this.callbacks.onImportStart ();
|
||||
OV.RunTaskAsync (() => {
|
||||
this.importer.Import (settings, {
|
||||
onSuccess : (importResult) => {
|
||||
this.OnModelImported (importResult);
|
||||
},
|
||||
onError : (importError) => {
|
||||
this.callbacks.onLoadError (importError);
|
||||
this.inProgress = false;
|
||||
}
|
||||
});
|
||||
this.importer.ImportFiles (files, fileSource, settings, {
|
||||
onFilesLoaded : () => {
|
||||
this.callbacks.onImportStart ();
|
||||
},
|
||||
onImportSuccess : (importResult) => {
|
||||
this.OnModelImported (importResult);
|
||||
},
|
||||
onImportError : (importError) => {
|
||||
this.callbacks.onLoadError (importError);
|
||||
this.inProgress = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -11,16 +11,17 @@ function ExportImport (model, format, extension, onReady)
|
||||
fileObjects.push (new FileObject ('', file.name, file.content));
|
||||
}
|
||||
let importer = new OV.Importer ();
|
||||
importer.LoadFilesFromFileObjects (fileObjects, function () {
|
||||
let settings = new OV.ImportSettings ();
|
||||
importer.Import (settings, {
|
||||
onSuccess : function (importResult) {
|
||||
onReady (importResult.model)
|
||||
},
|
||||
onError : function (importError) {
|
||||
let settings = new OV.ImportSettings ();
|
||||
importer.ImportFilesFromFileObjects (fileObjects, settings, {
|
||||
onFilesLoaded : function () {
|
||||
|
||||
}
|
||||
});
|
||||
},
|
||||
onImportSuccess : function (importResult) {
|
||||
onReady (importResult.model)
|
||||
},
|
||||
onImportError : function (importError) {
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
@ -3,16 +3,17 @@ var path = require ('path');
|
||||
|
||||
function ImportFilesWithImporter (importer, files, callbacks)
|
||||
{
|
||||
importer.LoadFilesFromFileObjects (files, function () {
|
||||
let settings = new OV.ImportSettings ();
|
||||
importer.Import (settings, {
|
||||
onSuccess : function (importResult) {
|
||||
callbacks.success (importer, importResult);
|
||||
},
|
||||
onError : function (importError) {
|
||||
callbacks.error (importer, importError);
|
||||
}
|
||||
});
|
||||
let settings = new OV.ImportSettings ();
|
||||
importer.ImportFilesFromFileObjects (files, settings, {
|
||||
onFilesLoaded : function () {
|
||||
|
||||
},
|
||||
onImportSuccess : function (importResult) {
|
||||
callbacks.success (importer, importResult);
|
||||
},
|
||||
onImportError : function (importError) {
|
||||
callbacks.error (importer, importError);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@ -117,7 +118,7 @@ describe ('Importer Test', function () {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it ('Missing files', function (done) {
|
||||
let files = [];
|
||||
@ -198,12 +199,12 @@ describe ('Importer Test', function () {
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
@ -219,15 +220,15 @@ describe ('Importer Test', function () {
|
||||
]
|
||||
let files2 = [
|
||||
new FileObject ('obj', 'single_triangle.obj')
|
||||
];
|
||||
];
|
||||
|
||||
let theImporter = new OV.Importer ();
|
||||
ImportFilesWithImporter (theImporter, files1, {
|
||||
success : function (importer, importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
assert.deepStrictEqual (importResult.usedFiles, ['cube_with_materials.obj', 'cube_with_materials.mtl', 'cube_texture.png']);
|
||||
assert.deepStrictEqual (importResult.missingFiles, []);
|
||||
|
||||
assert.deepStrictEqual (importResult.missingFiles, []);
|
||||
|
||||
ImportFilesWithImporter (theImporter, files2, {
|
||||
success : function (importer, importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
@ -238,12 +239,12 @@ describe ('Importer Test', function () {
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
},
|
||||
error : function (importer, importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it ('Default color', function (done) {
|
||||
@ -251,22 +252,23 @@ describe ('Importer Test', function () {
|
||||
new FileObject ('stl', 'single_triangle.stl')
|
||||
];
|
||||
let theImporter = new OV.Importer ();
|
||||
theImporter.LoadFilesFromFileObjects (files, function () {
|
||||
let settings = new OV.ImportSettings ();
|
||||
settings.defaultColor = new OV.Color (200, 0, 0);
|
||||
theImporter.Import (settings, {
|
||||
onSuccess : function (importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
assert.deepStrictEqual (importResult.usedFiles, ['single_triangle.stl']);
|
||||
assert.deepStrictEqual (importResult.missingFiles, []);
|
||||
let material = importResult.model.GetMaterial (0);
|
||||
assert.deepStrictEqual (material.color, new OV.Color (200, 0, 0));
|
||||
done ();
|
||||
},
|
||||
onError : function (importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
let settings = new OV.ImportSettings ();
|
||||
settings.defaultColor = new OV.Color (200, 0, 0);
|
||||
theImporter.ImportFilesFromFileObjects (files, settings, {
|
||||
onFilesLoaded : function () {
|
||||
|
||||
},
|
||||
onImportSuccess : function (importResult) {
|
||||
assert (!OV.IsModelEmpty (importResult.model));
|
||||
assert.deepStrictEqual (importResult.usedFiles, ['single_triangle.stl']);
|
||||
assert.deepStrictEqual (importResult.missingFiles, []);
|
||||
let material = importResult.model.GetMaterial (0);
|
||||
assert.deepStrictEqual (material.color, new OV.Color (200, 0, 0));
|
||||
done ();
|
||||
},
|
||||
onImportError : function (importError) {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
@ -285,7 +287,7 @@ describe ('Importer Test', function () {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it ('Zip file with Folders', function (done) {
|
||||
let files = [
|
||||
@ -320,5 +322,5 @@ describe ('Importer Test', function () {
|
||||
assert.fail ();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
Reference in New Issue
Block a user