1
1
Fork 0
mirror of https://github.com/boxgaming/qbjs.git synced 2024-05-12 08:00:12 +00:00

Updated OPEN with ability to read files from URLs (105)

This commit is contained in:
boxgaming 2024-01-31 14:18:51 -06:00
parent 3af0751e66
commit 9d367351f3
3 changed files with 53 additions and 9 deletions

56
qb.js
View file

@ -58,7 +58,7 @@ var QB = new function() {
var _strokeLineThickness = 2;
var _windowAspect = [];
var _windowDef = [];
var _fileHandles = null;
var _fileHandles = {};
var _typeMap = {};
@ -2162,7 +2162,7 @@ var QB = new function() {
return n.toString(8).toUpperCase();
};
this.sub_Open = function(path, mode, handle) {
this.sub_Open = async function(path, mode, handle) {
var vfs = GX.vfs();
var vfsCwd = GX.vfsCwd();
if (mode == QB.OUTPUT || mode == QB.APPEND || mode == QB.BINARY) {
@ -2173,23 +2173,67 @@ var QB = new function() {
var file = null;
if (mode == QB.APPEND || mode == QB.BINARY) {
file = vfs.getNode(path, vfsCwd);
// TODO: make sure this is not a directory
}
if (!file) {
file = vfs.createFile(filename, parentNode);
if (mode == QB.APPEND || mode == QB.BINARY) {
file = await downloadFile(path);
}
if (!file) {
file = vfs.createFile(filename, parentNode);
}
}
else if (file.type != vfs.FILE) {
// make sure this is not a directory
throw new Error("Path is not a file.");
}
_fileHandles[handle] = { file: file, mode: mode, offset: 0 };
}
else if (mode == QB.INPUT) {
var file = vfs.getNode(path, vfsCwd);
if (!file || file.type != vfs.FILE) {
throw new Error("File not found");
if (!file) {
// attempt to copy the path to the local filesystem
var file = await downloadFile(path);
if (!file) {
throw new Error("File not found");
}
}
else if (file.type != vfs.FILE) {
throw new Error("Path is not a file.");
}
_fileHandles[handle] = { file: file, mode: mode, offset: 0 };
}
else {
throw new Error("Unsupported Open Method");
}
async function downloadFile(path) {
try {
var res = await fetch(path);
if (res.ok) {
var filename = vfs.getFileName(path);
var parentPath = vfs.getParentPath(path);
var parentNode = vfs.rootDirectory();
var dirs = parentPath.split("/");
for (var i=0; i < dirs.length; i++) {
if (dirs[i] == "") { continue; }
var node = vfs.getNode(dirs[i], parentNode);
if (!node) { node = vfs.createDirectory(dirs[i], parentNode); }
parentNode = node;
}
var file = vfs.createFile(filename, parentNode);
vfs.writeData(file, await res.arrayBuffer());
return file;
}
else {
return null;
}
}
catch (err) {
return null;
}
}
};
this.sub_Paint = function(sstep, startX, startY, fillColor, borderColor) {

View file

@ -2035,7 +2035,7 @@ if (QB.halted()) { return; };
var fline = ''; /* STRING */
var lineIndex = 0; /* INTEGER */
var rawJS = 0; /* SINGLE */
QB.sub_Open(filename, QB.INPUT, 1);
await QB.sub_Open(filename, QB.INPUT, 1);
var ___v1000522 = 0; while (!((QB.func_EOF( 1)))) { if (QB.halted()) { return; }___v1000522++; if (___v1000522 % 100 == 0) { await QB.autoLimit(); }
var ___v1030226 = new Array(1); await QB.sub_LineInputFromFile(1, ___v1030226); fline = ___v1030226[0];
lineIndex = lineIndex + 1;
@ -3556,7 +3556,7 @@ if (QB.halted()) { return; };
await sub_AddQBMethod( "FUNCTION" , "Mkl$" , False);
await sub_AddQBMethod( "SUB" , "Name" , False);
await sub_AddQBMethod( "FUNCTION" , "Oct$" , False);
await sub_AddQBMethod( "SUB" , "Open" , False);
await sub_AddQBMethod( "SUB" , "Open" , True);
await sub_AddQBMethod( "SUB" , "Paint" , False);
await sub_AddQBMethod( "FUNCTION" , "Point" , False);
await sub_AddQBMethod( "FUNCTION" , "Pos" , False);

View file

@ -3785,7 +3785,7 @@ Sub InitQBMethods
AddQBMethod "FUNCTION", "Mkl$", False
AddQBMethod "SUB", "Name", False
AddQBMethod "FUNCTION", "Oct$", False
AddQBMethod "SUB", "Open", False
AddQBMethod "SUB", "Open", True
AddQBMethod "SUB", "Paint", False
AddQBMethod "FUNCTION", "Point", False
AddQBMethod "FUNCTION", "Pos", False