Last active
December 8, 2020 04:11
-
-
Save Meshiest/79c124eab69650dd065e21d608351fbe to your computer and use it in GitHub Desktop.
convert a save into microbricks but don't resize any bricks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<script src="https://cdn.jsdelivr.net/npm/brs-js/dist/dist.js"></script> | |
<!-- Files uploaded will be --> | |
<input id="fileInput" type="file"> | |
<a id="anchor" download="micro.brs">Download</a> | |
<!-- This will be filled with the save object as JSON or the error message --> | |
<pre id="jsonElem"></pre> | |
<script> | |
fileInput.addEventListener('change', e => { | |
const file = e.target.files[0]; | |
if (file) { | |
// Read the file into a byte array | |
file.arrayBuffer() | |
.then(buff => { | |
const save = BRS.read(buff); | |
let brickIndex = save.brick_assets.indexOf('PB_DefaultMicroBrick'); | |
if (brickIndex === -1) { | |
brickIndex = save.brick_assets.length; | |
save.brick_assets.push('PB_DefaultMicroBrick'); | |
} | |
let wedgeIndex = save.brick_assets.indexOf('PB_DefaultMicroWedge'); | |
if (wedgeIndex === -1) { | |
wedgeIndex = save.brick_assets.length; | |
save.brick_assets.push('PB_DefaultMicroWedge'); | |
} | |
for (const brick of save.bricks) { | |
const asset = save.brick_assets[brick.asset_name_index]; | |
if (asset === 'PB_DefaultBrick' || asset === 'PB_DefaultTile') { | |
brick.asset_name_index = brickIndex; | |
} | |
if (asset === 'PB_DefaultSideWedge' || asset === 'PB_DefaultSideWedgeTile') { | |
brick.asset_name_index = wedgeIndex; | |
} | |
} | |
// Render the save object as formatted JSON | |
jsonElem.innerText = ''; | |
const blob = new Blob([BRS.write(save)]); | |
anchor.href = URL.createObjectURL(blob); | |
}) | |
.catch(err => { | |
// Display the error | |
jsonElem.innerText = 'Error: ' + err.message; | |
}); | |
} | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment