Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: allow same asset to be loaded again #23

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 50 additions & 27 deletions public/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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("/"));
}
Expand Down Expand Up @@ -489,6 +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);

loadUsdFile(directory, fileName, fullPath, isRootFile);
};
reader.readAsArrayBuffer(file);
Expand Down