Skip to content

Commit

Permalink
Read Generic + Strict & File Length Flags
Browse files Browse the repository at this point in the history
```ts
const gg = await read(new Uint8Array(),{ name: "Level", endian: "big", compression: null, bedrockLevel: null });
```

This is an experimental demo to add 'generic propagation' to the `read()` function. It doesn't quite work fully because the parameters/options don't quite line up with `Format`, so I'm gonna decide how I want to handle that, since using a boolean for value-based types still seems easiest to use and understand from the user's perspective. I also have to handle the strict flag in the options too. Speaking of that, it might be related to whether the 'strict' flag should propagate to the resulting `NBTData` object & type, which I currently haven't added/fully looked into yet. It may make sense at the type level too though, would definitely be helpful for debugging between types of things! One thing about it is that it would show a type error if you tried opening a non-strict NBT file as a strict one, which thinking of it now, is a great think to also keep track of! Ok, I think I probably will end up adding that with time. Building on that, I may have to add a min file output size/length too, which would ensure that the output file can symmetrically parse to and from it's original content size. If there's any raw unnmanaged bytes in the deadspace for that original file though, those will have to be lost with time.

So together, possible `NBTData` strict and file length flags, which would help for symmetrical reads/writes, and would also be helpful for distinguishing parsed files at the type level too :)

#29
#31
  • Loading branch information
Offroaders123 committed Sep 13, 2023
1 parent 6d76698 commit 25aad9f
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions src/read.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export interface ReadOptions {
*
* If a format option isn't specified, the function will attempt reading the data using all options until it either throws or returns successfully.
*/
export async function read<T extends RootTagLike = RootTag, const U extends NBTDataOptions = NBTDataOptions>(data: Uint8Array | ArrayBufferLike, options?: ReadOptions): Promise<NBTData<T,U>>;
export async function read<T extends RootTagLike = RootTag, const U extends NBTDataOptions = NBTDataOptions>(data: Uint8Array | ArrayBufferLike, { name, endian, compression, bedrockLevel, strict }: ReadOptions = {}): Promise<NBTData<T,U>> {
export async function read<T extends RootTagLike = RootTag, const U extends NBTDataOptions = NBTDataOptions>(data: Uint8Array | ArrayBufferLike, options?: U): Promise<NBTData<T,U>>;
export async function read<T extends RootTagLike = RootTag, const U extends NBTDataOptions = NBTDataOptions>(data: Uint8Array | ArrayBufferLike, { name, endian, compression, bedrockLevel, strict }: U = {} as U): Promise<NBTData<T,U>> {
if (!("byteOffset" in data)){
data = new Uint8Array(data);
}
Expand Down Expand Up @@ -50,10 +50,10 @@ export async function read<T extends RootTagLike = RootTag, const U extends NBTD
case hasZlibHeader(data): compression = "deflate"; break compression;
}
try {
return await read<T,U>(data,{ name, endian, compression: null, bedrockLevel, strict });
return await read<T,U>(data,{ name, endian, compression: null, bedrockLevel, strict } as U);
} catch (error){
try {
return await read<T,U>(data,{ name, endian, compression: "deflate-raw", bedrockLevel, strict });
return await read<T,U>(data,{ name, endian, compression: "deflate-raw", bedrockLevel, strict } as U);
} catch {
throw error;
}
Expand All @@ -62,10 +62,10 @@ export async function read<T extends RootTagLike = RootTag, const U extends NBTD

if (endian === undefined){
try {
return await read<T,U>(data,{ name, endian: "big", compression, bedrockLevel, strict });
return await read<T,U>(data,{ name, endian: "big", compression, bedrockLevel, strict } as U);
} catch (error){
try {
return await read<T,U>(data,{ name, endian: "little", compression, bedrockLevel, strict });
return await read<T,U>(data,{ name, endian: "little", compression, bedrockLevel, strict } as U);
} catch {
throw error;
}
Expand All @@ -74,10 +74,10 @@ export async function read<T extends RootTagLike = RootTag, const U extends NBTD

if (name === undefined){
try {
return await read<T,U>(data,{ name: true, endian, compression, bedrockLevel, strict });
return await read<T,U>(data,{ name: true, endian, compression, bedrockLevel, strict } as U);
} catch (error){
try {
return await read<T,U>(data,{ name: false, endian, compression, bedrockLevel, strict });
return await read<T,U>(data,{ name: false, endian, compression, bedrockLevel, strict } as U);
} catch {
throw error;
}
Expand Down

0 comments on commit 25aad9f

Please sign in to comment.