From 0c03fb3bca01737d9b3d124cff7d342d5fe44517 Mon Sep 17 00:00:00 2001 From: Viktor Kovacs Date: Wed, 31 Mar 2021 08:14:35 +0200 Subject: [PATCH] Revoking urls is now the responsibility of the main importer object. --- source/import/importer.js | 55 +++++++++++++++++++++--------------- test/tests/importer_test.js | 56 ++++++++++++++++++++++++++++++++++++- test/utils/globals.js | 5 +++- website/o3dv/website.js | 3 -- 4 files changed, 91 insertions(+), 28 deletions(-) diff --git a/source/import/importer.js b/source/import/importer.js index a14ca78..3bd5825 100644 --- a/source/import/importer.js +++ b/source/import/importer.js @@ -179,21 +179,6 @@ OV.FileList = class } }; -OV.RevokeModelUrls = function (model) -{ - if (model === null) { - return; - } - for (let i = 0; i < model.MaterialCount (); i++) { - let material = model.GetMaterial (i); - material.EnumerateTextureMaps (function (texture) { - if (texture.url !== null) { - OV.RevokeObjectUrl (texture.url); - } - }); - } -}; - OV.ImportErrorCode = { NoImportableFile : 1, @@ -217,8 +202,8 @@ OV.ImportResult = class this.model = null; this.mainFile = null; this.upVector = null; - this.usedFiles = []; - this.missingFiles = []; + this.usedFiles = null; + this.missingFiles = null; } }; @@ -275,6 +260,8 @@ OV.Importer = class new OV.ImporterGltf () ]; this.fileList = new OV.FileList (this.importers); + this.model = null; + this.usedFiles = []; this.missingFiles = []; } @@ -296,9 +283,14 @@ OV.Importer = class return; } + this.RevokeModelUrls (); + this.model = null; + this.usedFiles = []; + this.missingFiles = []; + this.usedFiles.push (mainFile.file.name); + let result = new OV.ImportResult (); result.mainFile = mainFile.file.name; - result.usedFiles.push (mainFile.file.name); let obj = this; let importer = mainFile.importer; @@ -307,11 +299,10 @@ OV.Importer = class let file = obj.fileList.FindFileByPath (fileName); if (file === null || file.content === null) { obj.missingFiles.push (fileName); - result.missingFiles.push (fileName); fileBuffer = null; } else { fileBuffer = file.content; - result.usedFiles.push (fileName); + obj.usedFiles.push (fileName); } return fileBuffer; }); @@ -336,8 +327,12 @@ OV.Importer = class return; } - result.model = importer.GetModel (); - result.model.SetName (mainFile.file.name); + this.model = importer.GetModel (); + this.model.SetName (mainFile.file.name); + + result.model = this.model; + result.usedFiles = this.usedFiles; + result.missingFiles = this.missingFiles; result.upVector = importer.GetUpDirection (); callbacks.success (result); } @@ -372,7 +367,6 @@ OV.Importer = class if (reset) { this.fileList = newFileList; } - this.missingFiles = []; this.fileList.GetContent (function () { onReady (); }); @@ -387,4 +381,19 @@ OV.Importer = class { return this.fileList.IsOnlySource (source); } + + RevokeModelUrls () + { + if (this.model === null) { + return; + } + for (let i = 0; i < this.model.MaterialCount (); i++) { + let material = this.model.GetMaterial (i); + material.EnumerateTextureMaps (function (texture) { + if (texture.url !== null) { + OV.RevokeObjectUrl (texture.url); + } + }); + } + } }; diff --git a/test/tests/importer_test.js b/test/tests/importer_test.js index ec26f1a..395d230 100644 --- a/test/tests/importer_test.js +++ b/test/tests/importer_test.js @@ -92,6 +92,26 @@ describe ('Importer Test', function () { }); }); + it ('Multiple files', function () { + let files = [ + new FileObject ('obj', 'cube_with_materials.obj'), + new FileObject ('obj', 'cube_with_materials.mtl'), + new FileObject ('obj', 'cube_texture.png') + ] + + let theImporter = new OV.Importer (); + ImportFilesWithImporter (theImporter, files, { + 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, []); + }, + error : function (importer, importError) { + assert.fail (); + } + }); + }); + it ('Missing files', function () { let files = []; files.push (new FileObject ('obj', 'cube_with_materials.obj')); @@ -178,5 +198,39 @@ describe ('Importer Test', function () { assert.fail (); } }); - }); + }); + + it ('Reuse importer', function () { + let files1 = [ + new FileObject ('obj', 'cube_with_materials.obj'), + new FileObject ('obj', 'cube_with_materials.mtl'), + new FileObject ('obj', 'cube_texture.png') + ] + 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, []); + + ImportFilesWithImporter (theImporter, files2, { + success : function (importer, importResult) { + assert (!OV.IsModelEmpty (importResult.model)); + assert.deepStrictEqual (importResult.usedFiles, ['single_triangle.obj']); + assert.deepStrictEqual (importResult.missingFiles, []); + }, + error : function (importer, importError) { + assert.fail (); + } + }); + }, + error : function (importer, importError) { + assert.fail (); + } + }); + }); }); diff --git a/test/utils/globals.js b/test/utils/globals.js index 4f66e2c..4fecf30 100644 --- a/test/utils/globals.js +++ b/test/utils/globals.js @@ -14,7 +14,10 @@ global.URL = { createObjectURL : function () { objectUrlCounter += 1; return 'ObjectUrl:' + objectUrlCounter.toString (); - } + }, + revokeObjectURL : function () { + + } }; global.FileObject = function (folderName, fileName) diff --git a/website/o3dv/website.js b/website/o3dv/website.js index 38f8462..c7631ef 100644 --- a/website/o3dv/website.js +++ b/website/o3dv/website.js @@ -71,9 +71,6 @@ OV.Website = class this.dialog.Hide (); this.dialog = null; } - if (this.model !== null) { - OV.RevokeModelUrls (this.model); - } this.model = null; this.parameters.introDiv.hide (); this.ShowViewer (false);