From d0fb69649ce904cddeebeaaa359bf170fb963750 Mon Sep 17 00:00:00 2001 From: andy Date: Mon, 8 Jul 2024 15:34:13 -0400 Subject: [PATCH 1/2] fix: allow same asset to be loaded again - add file as currentRootfile when dropped - clear stage when dropping a new asset --- public/index.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/public/index.js b/public/index.js index f7bccff..756687c 100644 --- a/public/index.js +++ b/public/index.js @@ -480,6 +480,7 @@ async function loadFile(fileOrHandle, isRootFile = true, fullPath = undefined) { reader.onerror = reject; }); reader.onload = function(event) { + clearStage(); let fileName = file.name; let directory = "/"; if (fullPath !== undefined) { @@ -489,6 +490,7 @@ async function loadFile(fileOrHandle, isRootFile = true, fullPath = undefined) { } Usd.FS_createPath("", directory, true, true); Usd.FS_createDataFile(directory, fileName, new Uint8Array(event.target.result), true, true, true); + currentRootFileName = fileName; loadUsdFile(directory, fileName, fullPath, isRootFile); }; reader.readAsArrayBuffer(file); From 337d9f37adef53b944abe70e97613893a7fff52e Mon Sep 17 00:00:00 2001 From: andy Date: Thu, 11 Jul 2024 11:49:47 -0400 Subject: [PATCH 2/2] feat: update to get actual file paths - utilize path searching to get the actual paths, and delete them --- public/index.js | 79 +++++++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 29 deletions(-) diff --git a/public/index.js b/public/index.js index 756687c..2fd7139 100644 --- a/public/index.js +++ b/public/index.js @@ -143,21 +143,61 @@ if (gltfExportBtn) gltfExportBtn.addEventListener('click', (evt) => { evt.preventDefault(); }); -const loadedFiles = []; +function getAllLoadedFiles(){ + const filePaths = []; + + getAllLoadedFilePaths("/", filePaths); + + return filePaths; +} + +function getAllLoadedFilePaths(currentPath, paths) { + const files = Usd.FS_readdir(currentPath); + for (const file of files) { + // skip self and parent + if (file === "." || file === "..") continue; + const newPath = currentPath + file + "/"; + const data = Usd.FS_analyzePath(currentPath + file + "/"); + if (data.object.node_ops.readdir) { + // default directories we're not interested in + if (newPath == "/dev/" || newPath == "/proc/" || newPath== "/home/" || newPath== "/tmp/" || newPath== "/usd/") continue; + getAllLoadedFilePaths(newPath, paths); + } + else { + paths.push(data.path); + } + } +} + function clearStage() { - console.log("Clearing stage.", [currentRootFileName, ...loadedFiles]) - window.usdRoot.clear(); + var allFilePaths = getAllLoadedFiles(); + console.log("Clearing stage.", allFilePaths) - for (const file of loadedFiles) { + for (const file of allFilePaths) { Usd.FS_unlink(file, true); } - loadedFiles.length = 0; - if (currentRootFileName !== undefined) { - Usd.FS_unlink(currentRootFileName, true); - currentRootFileName = undefined; - } + window.usdRoot.clear(); +} + +function addPath(root, path) { + const files = Usd.FS_readdir(path); + for (const file of files) { + // skip self and parent + if (file === "." || file === "..") continue; + const newPath = path + file + "/"; + const data = Usd.FS_analyzePath(path + file + "/"); + if (data.object.node_ops.readdir) { + // default directories we're not interested in + if (newPath == "/dev/" || newPath == "/proc/" || newPath== "/home/" || newPath== "/tmp/" || newPath== "/usd/") continue; + root[file] = {}; + addPath(root[file], newPath); + } + else { + root[file] = data; + } + } } async function loadUsdFile(directory, filename, path, isRootFile = true) { @@ -217,24 +257,6 @@ async function loadUsdFile(directory, filename, path, isRootFile = true) { // So when content in a USDZ is changed > update the USDZ file and then reload // This might be recursive (USDZ in USDZ in USDZ) const root = {}; - function addPath(root, path) { - const files = Usd.FS_readdir(path); - for (const file of files) { - // skip self and parent - if (file === "." || file === "..") continue; - const newPath = path + file + "/"; - const data = Usd.FS_analyzePath(path + file + "/"); - if (data.object.node_ops.readdir) { - // default directories we're not interested in - if (newPath == "/dev/" || newPath == "/proc/" || newPath== "/home/") continue; - root[file] = {}; - addPath(root[file], newPath); - } - else { - root[file] = data; - } - } - } addPath(root, "/"); console.log("File system", root, Usd.FS_analyzePath("/")); } @@ -480,7 +502,6 @@ async function loadFile(fileOrHandle, isRootFile = true, fullPath = undefined) { reader.onerror = reject; }); reader.onload = function(event) { - clearStage(); let fileName = file.name; let directory = "/"; if (fullPath !== undefined) { @@ -490,7 +511,7 @@ async function loadFile(fileOrHandle, isRootFile = true, fullPath = undefined) { } Usd.FS_createPath("", directory, true, true); Usd.FS_createDataFile(directory, fileName, new Uint8Array(event.target.result), true, true, true); - currentRootFileName = fileName; + loadUsdFile(directory, fileName, fullPath, isRootFile); }; reader.readAsArrayBuffer(file);