Skip to content

Commit

Permalink
Strict Mode Dialog
Browse files Browse the repository at this point in the history
Bringing the latest NBTify changes over to Dovetail! The first of the features is the new handling for strict mode, which allows for files that have trailing bytes at the end to be opened, if specified by the user. Since I want to ensure no data to be lost as much as possible, I made it visible to the user when the file they are trying to open has trailing bytes at the end of the file. Since some files have this intentionally it may seem (LCE?), I'm making this optional to decide which way you'd like to handle it. It's possible that there actually is an error, and NBTify isn't reading all of the data correctly, or that the file just actually is a little bigger than the NBT data itself that it holds. I think that may be what's going on for the LCE player files, and other NBT libraries appear to skip over the rest of the file, even though it goes as unread. So this seemed a bit unsafe to me, as with NBTify's format auto-detection, you can't always be sure that the extra bytes not being read is intentional or not, because it might just be reading it in the wrong format. I have seen it where NBTify can successfully open a file early using a different format, when there are still a lot of bytes left, and they actually do hold what the NBT data should be. So this could be dangerous to the user's data if this isn't managed safely and made visible during the reading steps.

Noticed that `.dat_old` files couldn't be opened, so I added them as a file association too.

I tried adding NBTify's new Deflate-Raw handling into the Format Options menu to allow Deflate-Raw inputs and outputs, but I tried opening a file I wrote with Deflate-Raw in the editor, and realized that NBTify currently doesn't have a way to auto-detect Deflate-Raw, which means Dovetail also doesn't have a way to open files compressed with that. It will simply error out and not be able to open the file, because Deflate-Raw is something that it knows how to handle, but isn't something that it can discern against from the other formats yet. So in JavaScript land, you can specify that it's Deflate-Raw, and it will open perfect, but Dovetail can't because that relies on figuring out the format for you, so you don't have to tell it how to open it.

Skull Bubbles is a great song.
  • Loading branch information
Offroaders123 committed May 19, 2023
1 parent daea22b commit b346f26
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
9 changes: 3 additions & 6 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<header>
<img height="32" draggable="false" src="./resources/app-512.png">
<button onclick="fileOpener.click();">Open</button>
<input id="fileOpener" type="file" accept="application/octet-stream, .nbt, .dat">
<input id="fileOpener" type="file" accept="application/octet-stream, .nbt, .dat, .dat_old">
<button id="saver" disabled>Save</button>
<button id="formatOpener" disabled>Format Options...</button>
</header>
Expand Down Expand Up @@ -73,19 +73,16 @@ <h3>Format Options</h3>
</label>
<label>
<input type="radio" name="compression" value="deflate">
zlib
deflate (zlib)
</label>
</fieldset>

<fieldset>
<legend>Bedrock Level</legend>

<label>
<input type="number" name="bedrockLevel" placeholder="&lt;false&gt;" min="0" max="4294967295" list="bedrockLevelVersion">
<input type="number" name="bedrockLevel" placeholder="&lt;false&gt;" min="0" max="4294967295">
<code>(Uint32)</code>
<datalist id="bedrockLevelVersion">
<option value="8">(Most common)</option>
</datalist>
</label>
</fieldset>
</form>
Expand Down
2 changes: 1 addition & 1 deletion manifest.webmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{
"action": "./",
"accept": {
"application/octet-stream": [".nbt", ".dat"]
"application/octet-stream": [".nbt", ".dat", ".dat_old"]
}
}
]
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"dependencies": {
"compression-streams-polyfill": "^0.1.3",
"es-module-shims": "^1.7.2",
"nbtify": "^1.20.1"
"nbtify": "^1.35.2"
},
"devDependencies": {
"better-typescript": "^0.1.2",
Expand Down
2 changes: 1 addition & 1 deletion service-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// <reference no-default-lib="true"/>
/// <reference types="better-typescript/worker"/>

const version = "Dovetail v1.4.0";
const version = "Dovetail v1.5.0";

self.addEventListener("activate",event => {
event.waitUntil(removeOutdatedVersions());
Expand Down
24 changes: 15 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "./compression-polyfill.js";
import { read, write, parse, stringify, Name, Endian, Compression, BedrockLevel, Int, NBTData, NBTDataOptions } from "nbtify";
import { read, write, parse, stringify, Name, Endian, Compression, BedrockLevel, Int32, NBTData, NBTDataOptions, CompressionFormat } from "nbtify";

if (window.isSecureContext){
await navigator.serviceWorker.register("./service-worker.js");
Expand Down Expand Up @@ -131,8 +131,8 @@ export function openOptions({ name, endian, compression, bedrockLevel }: NBTData
elements.disableName.checked = true;
}
elements.endian.value = endian;
elements.compression.value = compression ?? "none";
elements.bedrockLevel.value = (bedrockLevel === undefined) ? "" : `${bedrockLevel}`;
elements.compression.value = (compression === null) ? "none" : compression;
elements.bedrockLevel.value = (bedrockLevel === null) ? "" : `${bedrockLevel}`;

const options: NBTDataOptions = { name, endian, compression, bedrockLevel };
return options;
Expand All @@ -142,13 +142,19 @@ export function openOptions({ name, endian, compression, bedrockLevel }: NBTData
* Attempts to create an NBTData object from a File object.
*/
export async function readFile(file: File){
const buffer = await file.arrayBuffer();
try {
const buffer = await file.arrayBuffer();
const nbt = await read(buffer);
return nbt;
} catch (error){
alert(error);
return null;
} catch (error: any){
if ((error as Error).message.includes("unread bytes remaining")){
const reattempt = confirm(`${error}\n\nEncountered extra data at the end of the file. Would you like to try opening it again without 'strict mode' enabled? The trailing data will be lost when re-saving your file again.`);
if (!reattempt) return null;
return read(buffer,{ strict: false });
} else {
alert(error);
return null;
}
}
}

Expand All @@ -160,8 +166,8 @@ export function saveOptions(){

const name: Name = (elements.disableName.checked) ? null : elements.name.value;
const endian: Endian = elements.endian.value as Endian;
const compression: Compression | null = (elements.compression.value === "none") ? null : elements.compression.value as Compression;
const bedrockLevel: BedrockLevel | null = (elements.bedrockLevel.value === "") ? null : new Int(parseInt(elements.bedrockLevel.value));
const compression: Compression = (elements.compression.value === "none") ? null : elements.compression.value as CompressionFormat;
const bedrockLevel: BedrockLevel = (elements.bedrockLevel.value === "") ? null : new Int32(parseInt(elements.bedrockLevel.value));

const options: NBTDataOptions = { name, endian, compression, bedrockLevel };
return options;
Expand Down

0 comments on commit b346f26

Please sign in to comment.