Skip to content

Commit

Permalink
Clean up file I/O
Browse files Browse the repository at this point in the history
  • Loading branch information
Thysbelon committed Aug 5, 2023
1 parent a3521d2 commit deae26d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 48 deletions.
92 changes: 45 additions & 47 deletions docs/gbaconv-web-frontend.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ savpicker.addEventListener('change', myClick);
function myClick(e){
// replace 4gs with srm
// replace whatever the file name is with "savedata", c can't handle paths with spaces !!!!
// emscripten can't write to any directory other than the mounted one !!!!
var filename=e.target.files[0].name.replace(/.*\.(.*)/, "data/savedata.$1").replace(".4gs", ".srm").replace(".4gz", ".srm").replace(".44gbasav",".srm")
var filename=e.target.files[0].name.replace(/.*\.(.*)/, "home/web_user/savedata.$1").replace(".4gs", ".srm").replace(".4gz", ".srm").replace(".44gbasav",".srm")
console.log(filename)
const reader=new FileReader();
reader.readAsArrayBuffer(e.target.files[0]);
Expand All @@ -18,51 +17,50 @@ function myClick(e){
for (let i=0; i<10; i++) {
console.log(data[i])
}
FS.syncfs(true, function (err) {
// handle callback
console.log("syncfs error: "+err);
FS.mkdir('/data');
FS.mount(IDBFS, {}, '/data');
FS.writeFile("/"+filename, data, {flags:"w+"})
Module.ccall(
"GBAConv",
"number",
["string"],
["/"+filename]
)
FS.syncfs(false, function (err) {
// handle callback
console.log("syncFS 2 error: "+err);
if (filename.includes(".srm")){filename=filename.replace(".srm", ".sav")} else {filename=filename.replace(".sav", ".srm")}
let db;
const request = indexedDB.open("/data");
request.onsuccess = (event) => {
db = event.target.result;
db.transaction("FILE_DATA").objectStore("FILE_DATA").get( "/"+filename ).onsuccess = (event) => {
var savedataObj=event.target.result.contents
savedataValues=Object.values(savedataObj) // array
//const buffer=new ArrayBuffer(romdataValues.length)
//const view = new Uint8Array(buffer);
const savedata8array = Uint8Array.from(savedataValues);

const file = new File([savedata8array], e.target.files[0].name.replace(/\.\w\w\w/, "."+filename.slice(-3) ) )
download(file)
//var req = indexedDB.deleteDatabase("/data");
//req.onsuccess = function () {
// console.log("Deleted database successfully");
//};
//FS.unlink("/data/savedata.sav")
//FS.unlink("/data/savedata.srm")
//FS.unmount('/data');
//FS.rmdir("/data")
if (typeof DebugOn === 'undefined') {
window.location.reload(); // I tried so hard to make this app work without reloading, but all of my attempts to delete the contents of memfs failed.
}
};
};
})

});
//FS.mount(IDBFS, {}, '/data');
FS.writeFile("/"+filename, data, {flags:"w+"})
Module.ccall(
"GBAConv",
"number",
["string"],
["/"+filename]
)
if (filename.includes(".srm")){filename=filename.replace(".srm", ".sav")} else {filename=filename.replace(".sav", ".srm")}
//const request = indexedDB.open("/data");
//request.onsuccess = (event) => {
// db = event.target.result;
// db.transaction("FILE_DATA").objectStore("FILE_DATA").get( "/"+filename ).onsuccess = (event) => {
// var savedataObj=event.target.result.contents
// savedataValues=Object.values(savedataObj) // array
// //const buffer=new ArrayBuffer(romdataValues.length)
// //const view = new Uint8Array(buffer);
// const savedata8array = Uint8Array.from(savedataValues);
//
// const file = new File([savedata8array], e.target.files[0].name.replace(/\.\w\w\w/, "."+filename.slice(-3) ) )
// download(file)
// //var req = indexedDB.deleteDatabase("/data");
// //req.onsuccess = function () {
// // console.log("Deleted database successfully");
// //};
// //FS.unlink("/data/savedata.sav")
// //FS.unlink("/data/savedata.srm")
// //FS.unmount('/data');
// //FS.rmdir("/data")
// if (typeof DebugOn === 'undefined') {
// window.location.reload(); // I tried so hard to make this app work without reloading, but all of my attempts to delete the contents of memfs failed.
// }
// };
//};
console.log("filename: "+filename)
//stream = FS.open(filename, "r")
//FS.close(stream)
savedata8array=FS.readFile('/'+filename) // is opts optional?
const file = new File([savedata8array], e.target.files[0].name.replace(/\.\w*$/, "."+filename.slice(-3) ) )
download(file)
FS.unlink('/'+filename);
//if (typeof DebugOn === 'undefined') {
// window.location.reload(); // I tried so hard to make this app work without reloading, but all of my attempts to delete the contents of memfs failed.
//}

}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset=utf-8>
<title>GBA 4gz, 4gs, srm and sav Converter</title>
<meta name=color-scheme content="light dark">
<meta name=viewport content=width=device-width,initial-scale=1.0>
<meta name=viewport content="width=device-width,initial-scale=1.0">
<script src=minified-assets/gbaconv-web.js></script>
<script src=minified-assets/pako.min.js></script>
<link rel=preconnect href=https://fonts.bunny.net>
Expand Down

1 comment on commit deae26d

@Thysbelon
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removes IDBFS.

Please sign in to comment.