Move buffer cache to the main importer class.

This commit is contained in:
Viktor Kovacs 2021-03-30 19:22:38 +02:00
parent 45e4a13d53
commit 11d2f04580
7 changed files with 87 additions and 87 deletions

View File

@ -222,6 +222,46 @@ OV.ImportResult = class
}
};
OV.ImporterBuffers = class
{
constructor (getBufferCallback)
{
this.getBufferCallback = getBufferCallback;
this.fileBuffers = {};
this.textureBuffers = {};
}
GetFileBuffer (filePath)
{
let fileName = OV.GetFileName (filePath);
let buffer = this.fileBuffers[fileName];
if (buffer === undefined) {
buffer = this.getBufferCallback (fileName);
this.fileBuffers[fileName] = buffer;
}
return buffer;
}
GetTextureBuffer (filePath)
{
let fileName = OV.GetFileName (filePath);
let buffer = this.textureBuffers[fileName];
if (buffer === undefined) {
let texBuffer = this.getBufferCallback (fileName);
if (texBuffer !== null) {
buffer = {
url : OV.CreateObjectUrl (texBuffer),
buffer : texBuffer
};
} else {
buffer = null;
}
this.textureBuffers[fileName] = buffer;
}
return buffer;
}
};
OV.Importer = class
{
constructor ()
@ -262,24 +302,31 @@ OV.Importer = class
let obj = this;
let importer = mainFile.importer;
let buffers = new OV.ImporterBuffers (function (fileName) {
let fileBuffer = null;
let file = obj.fileList.FindFileByPath (fileName);
if (file === null || file.content === null) {
result.missingFiles.push (fileName);
obj.missingFiles.push (fileName);
fileBuffer = null;
} else {
result.usedFiles.push (fileName);
fileBuffer = file.content;
}
return fileBuffer;
});
importer.Import (mainFile.file.content, mainFile.file.extension, {
getDefaultMaterial : function () {
let material = new OV.Material ();
material.diffuse = new OV.Color (200, 200, 200);
return material;
},
getFileBuffer : function (fileName) {
let fileBuffer = null;
let file = obj.fileList.FindFileByPath (fileName);
if (file === null || file.content === null) {
result.missingFiles.push (fileName);
obj.missingFiles.push (fileName);
fileBuffer = null;
} else {
result.usedFiles.push (fileName);
fileBuffer = file.content;
}
return fileBuffer;
getFileBuffer : function (filePath) {
return buffers.GetFileBuffer (filePath);
},
getTextureBuffer : function (filePath) {
return buffers.GetTextureBuffer (filePath);
}
});

View File

@ -177,7 +177,7 @@ OV.Importer3ds = class extends OV.ImporterBase
this.ReadChunks (reader, endByte, function (chunkId, chunkLength) {
if (chunkId === OV.CHUNK3DS.MAT_TEXMAP_NAME) {
let textureName = obj.ReadName (reader);
let textureBuffer = obj.GetTextureBuffer (textureName);
let textureBuffer = obj.callbacks.getTextureBuffer (textureName);
texture.name = textureName;
if (textureBuffer !== null) {
texture.url = textureBuffer.url;

View File

@ -1,61 +1,18 @@
OV.ImporterBuffers = class
{
constructor (getBufferCallback)
{
this.getBufferCallback = getBufferCallback;
this.fileBuffers = {};
this.textureBuffers = {};
}
GetFileBuffer (filePath)
{
let fileName = OV.GetFileName (filePath);
let buffer = this.fileBuffers[fileName];
if (buffer === undefined) {
buffer = this.getBufferCallback (fileName);
this.fileBuffers[fileName] = buffer;
}
return buffer;
}
GetTextureBuffer (filePath)
{
let fileName = OV.GetFileName (filePath);
let buffer = this.textureBuffers[fileName];
if (buffer === undefined) {
let texBuffer = this.getBufferCallback (fileName);
if (texBuffer !== null) {
buffer = {
url : OV.CreateObjectUrl (texBuffer),
buffer : texBuffer
};
} else {
buffer = null;
}
this.textureBuffers[fileName] = buffer;
}
return buffer;
}
};
OV.ImporterBase = class
{
constructor ()
{
this.extension = null;
this.callbacks = null;
this.buffers = null;
this.model = null;
this.error = null;
this.message = null;
this.buffers = null;
}
Import (content, extension, callbacks)
{
this.extension = extension;
this.callbacks = callbacks;
this.buffers = new OV.ImporterBuffers (this.callbacks.getFileBuffer);
this.model = new OV.Model ();
this.error = false;
this.message = null;
@ -74,16 +31,6 @@ OV.ImporterBase = class
OV.FinalizeModel (this.model, this.callbacks.getDefaultMaterial);
}
GetFileBuffer (filePath)
{
return this.buffers.GetFileBuffer (filePath);
}
GetTextureBuffer (filePath)
{
return this.buffers.GetTextureBuffer (filePath);
}
ResetState ()
{

View File

@ -405,7 +405,7 @@ OV.ImporterGltf = class extends OV.ImporterBase
if (base64Buffer !== null) {
buffer = base64Buffer.buffer;
} else {
let fileBuffer = this.GetFileBuffer (gltfBuffer.uri);
let fileBuffer = this.callbacks.getFileBuffer (gltfBuffer.uri);
if (fileBuffer !== null) {
buffer = fileBuffer;
}
@ -636,7 +636,7 @@ OV.ImporterGltf = class extends OV.ImporterBase
textureParams.url = OV.CreateObjectUrlWithMimeType (base64Buffer.buffer, base64Buffer.mimeType);
textureParams.buffer = base64Buffer.buffer;
} else {
let textureBuffer = this.GetTextureBuffer (gltfImage.uri);
let textureBuffer = this.callbacks.getTextureBuffer (gltfImage.uri);
textureParams.name = gltfImage.uri;
if (textureBuffer !== null) {
textureParams.url = textureBuffer.url;

View File

@ -166,11 +166,11 @@ OV.ImporterObj = class extends OV.ImporterBase
);
}
function CreateTexture (importer, keyword, line)
function CreateTexture (keyword, line, callbacks)
{
let texture = new OV.TextureMap ();
let textureName = OV.NameFromLine (line, keyword.length, '#');
let textureBuffer = importer.GetTextureBuffer (textureName);
let textureBuffer = callbacks.getTextureBuffer (textureName);
texture.name = textureName;
if (textureBuffer !== null) {
texture.url = textureBuffer.url;
@ -208,7 +208,7 @@ OV.ImporterObj = class extends OV.ImporterBase
return true;
}
let fileName = OV.NameFromLine (line, keyword.length, '#');
let fileBuffer = this.GetFileBuffer (fileName);
let fileBuffer = this.callbacks.getFileBuffer (fileName);
if (fileBuffer !== null) {
OV.ReadLines (fileBuffer, function (line) {
if (!obj.IsError ()) {
@ -221,19 +221,19 @@ OV.ImporterObj = class extends OV.ImporterBase
if (this.currentMaterial === null || parameters.length === 0) {
return true;
}
this.currentMaterial.diffuseMap = CreateTexture (this, keyword, line);
this.currentMaterial.diffuseMap = CreateTexture (keyword, line, this.callbacks);
return true;
} else if (keyword === 'map_ks') {
if (this.currentMaterial === null || parameters.length === 0) {
return true;
}
this.currentMaterial.specularMap = CreateTexture (this, keyword, line);
this.currentMaterial.specularMap = CreateTexture (keyword, line, this.callbacks);
return true;
} else if (keyword === 'map_bump' || keyword === 'bump') {
if (this.currentMaterial === null || parameters.length === 0) {
return true;
}
this.currentMaterial.bumpMap = CreateTexture (this, keyword, line);
this.currentMaterial.bumpMap = CreateTexture (keyword, line, this.callbacks);
return true;
} else if (keyword === 'ka') {
if (this.currentMaterial === null || parameters.length < 3) {

View File

@ -319,6 +319,9 @@ describe ('Exporter', function () {
return binFile.GetContent ();
}
return null;
},
getTextureBuffer (filePath) {
return null;
}
});
@ -350,6 +353,9 @@ describe ('Exporter', function () {
},
getFileBuffer (filePath) {
return null;
},
getTextureBuffer (filePath) {
return null;
}
});

View File

@ -45,8 +45,14 @@ module.exports =
ImportFile : function (importer, format, folder, fileName)
{
function GetFileBuffer (filePath, importer)
{
var content = null;
if (format == OV.FileFormat.Text) {
content = testUtils.GetTextFileContent (folder, fileName);
} else if (format == OV.FileFormat.Binary) {
content = testUtils.GetArrayBufferFileContent (folder, fileName);
}
var extension = OV.GetFileExtension (fileName);
let buffers = new OV.ImporterBuffers (function (filePath) {
let extension = OV.GetFileExtension (filePath);
let knownFormats = importer.GetKnownFileFormats ();
let format = OV.FileFormat.Binary;
@ -60,23 +66,17 @@ module.exports =
fileContent = testUtils.GetArrayBufferFileContent (folder, filePath);
}
return fileContent;
}
var obj = this;
var content = null;
if (format == OV.FileFormat.Text) {
content = testUtils.GetTextFileContent (folder, fileName);
} else if (format == OV.FileFormat.Binary) {
content = testUtils.GetArrayBufferFileContent (folder, fileName);
}
var extension = OV.GetFileExtension (fileName);
});
importer.Import (content, extension, {
getDefaultMaterial : function () {
var material = new OV.Material ();
return material;
},
getFileBuffer : function (filePath) {
return GetFileBuffer (filePath, importer);
return buffers.GetFileBuffer (filePath);
},
getTextureBuffer : function (filePath) {
return buffers.GetTextureBuffer (filePath);
}
});
let model = importer.GetModel ();