Skip to content

Commit

Permalink
feat: update to get actual file paths
Browse files Browse the repository at this point in the history
- utilize path searching to get the actual paths, and delete them
  • Loading branch information
beersandrew committed Jul 11, 2024
1 parent d0fb696 commit 337d9f3
Showing 1 changed file with 50 additions and 29 deletions.
79 changes: 50 additions & 29 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 @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit 337d9f3

Please sign in to comment.