Revoking urls is now the responsibility of the main importer object.

This commit is contained in:
Viktor Kovacs 2021-03-31 08:14:35 +02:00
parent 2450cb8feb
commit 0c03fb3bca
4 changed files with 91 additions and 28 deletions

View File

@ -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);
}
});
}
}
};

View File

@ -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 ();
}
});
});
});

View File

@ -14,7 +14,10 @@ global.URL = {
createObjectURL : function () {
objectUrlCounter += 1;
return 'ObjectUrl:' + objectUrlCounter.toString ();
}
},
revokeObjectURL : function () {
}
};
global.FileObject = function (folderName, fileName)

View File

@ -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);