Skip to content

Instantly share code, notes, and snippets.

@ITotalJustice
Created April 25, 2021 05:33
Show Gist options
  • Save ITotalJustice/8ce95ef8644e3c7d2bc0e67be46320ca to your computer and use it in GitHub Desktop.
Save ITotalJustice/8ce95ef8644e3c7d2bc0e67be46320ca to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>TotalGB</title>
</head>
<body style="background-color: #2c0c2c;">
<!-- Our filepicker! -->
<input type="file" id="RomFilePicker" name="LoadRom" accept=".gb, .gbc, .zip">
<!-- This is our javascript, it's role is to attach an event to the filepicker
so that when a file is selected, a function callback will be called.
This callback will load the rom as an array of bytes, and then pass that data,
along with it's len, to the C rom loading function! -->
<script>
var input = document.getElementById("RomFilePicker");
// sanity check to make sure we still have the ID (we might've renamed it!)
if (input) {
input.addEventListener("input", OnInput);
function OnInput() {
const MAX_SIZE = 0x400000;
if (input.files.length == 1 && input.files[0].size <= MAX_SIZE) {
console.log(
"name:", input.files[0].name,
"num files:", input.files.length,
"file_size:", input.files[0].size
);
let reader = new FileReader();
reader.addEventListener("load", () => {
let data = new Uint8Array(reader.result);
// these are the ptr's that are passed the C function.
var data_ptr = allocate(data, ALLOC_NORMAL);
var name_ptr = allocate(intArrayFromString(input.files[0].name), ALLOC_NORMAL);
// call out rom load function
ccall('em_load_file_as_data',
null, // no return (void)
['number', 'number', 'number'], // type of the params (ptr is a num, len is a num)
[name_ptr, data_ptr, data.length] // ptr and len
);
// freeeeeeeeeee
_free(data_ptr);
_free(name_ptr);
});
reader.readAsArrayBuffer(input.files[0]);
}
else {
console.log("Invalid file!");
}
}
}
</script>
{{{ SCRIPT }}}
</body>
</html>
<!-- the C function that gets called.
EMSCRIPTEN_KEEPALIVE
void em_load_file_as_data(const char* name, const uint8_t* data, int len) {
printf("we got data! name: %s len: %d\n", name, len);
const struct GB_CartHeader* header = GB_get_rom_header_ptr_from_data(data);
cart_header_print(header);
} -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment