EngineHub / SchematicWebViewer

An NPM package to facilitate importing and viewing of modern Minecraft schematics.
https://madelinemiller.dev/contact/
MIT License
64 stars 7 forks source link

Invalid tag value #25

Open IndexOutOfMJ opened 2 months ago

IndexOutOfMJ commented 2 months ago

Hey,

I tried to load a .schem file and a .nbt file and ran in both cases in this issue:

Error: Invalid tag value at getTagType (nbt.js:15:9) at nbt.js:30:12 at Array.forEach () at tagToRecord (nbt.js:28:20) at loadVersion2 (version2.js:49:5) at loadSchematic (loader.js:45:15) at renderer.js:113:13 at Generator.next () at renderer.js:44:66 at new ZoneAwarePromise (zone.js:2702:25)

This is the configuration i used:

renderSchematic(document.querySelector('#schematicRenderer'), base64, {
        size: {
          width: 889,
          height: 500
        },
        corsBypassUrl: 'https://corsproxy.io/'
      });
octylFractal commented 2 months ago

Can you provide the files you tested?

IndexOutOfMJ commented 2 months ago

Sure

https://downloads.thenewempire.net/schematics/FINISHED_Medieval-Medium-Bridge_1.21.schem

octylFractal commented 2 months ago

That is a valid schematic, so no issues there. How are you getting the base64 value?

IndexOutOfMJ commented 2 months ago

With this function:

async getData(reader: FileReader, file: Blob): Promise<string | null> { return new Promise((resolve) => { reader.onloadend = () => { if (typeof reader.result === 'string') { resolve(reader.result.replace('data:', '').replace(/^.+,/, '')); } }; reader.readAsDataURL(file); }); }

octylFractal commented 2 months ago

My best guess at this is that you're somehow loading the older nbt-ts library instead of @enginehub/nbt-ts, or some other such type mismatch is occurring. I'm personally unable to reproduce this. If you could provide a minimal project that reproduces the issue, that would be helpful. Otherwise, I would recommend double-checking the versions of everything.

IndexOutOfMJ commented 2 months ago

Okay, the only two packages i'm using in this context are those:

"@enginehub/schematicwebviewer": "^4.9.0",
"buffer": "^6.0.3",

Can you approve this ?

DanielSchmerber commented 1 month ago

I also ran into this same issue. To replicate i created a new Vite Project Added latest SchematicWebViewer, buffer, and blob-to-base64

This is my JS code

import {renderSchematic} from "@enginehub/schematicwebviewer";
import {Buffer} from "buffer";
import blobToBase64 from "blob-to-base64";

globalThis.Buffer = Buffer;

let temp = await fetch("./schem.schem")

let schematic = blobToBase64(await temp.blob(),(r,o)=>render(o))

function render(schematic) {
schematic = schematic.substring(37)
alert(schematic)
renderSchematic(document.querySelector('#schematicRenderer'),schematic , {
size: 500,
renderArrow: false,
renderBars: false,
corsBypassUrl: 'https://corsproxy.io/?',
});
}
me4502 commented 1 month ago

in general this error means the NBT data is invalid. in your provided code i'm a bit confused by the random substring call there?

DanielSchmerber commented 1 month ago

Yeah, that was the base64 api adding metadata about the encoding to the string, should have commented that

DanielSchmerber commented 1 month ago

I managed to find the Root of this issue

The Problem is the getTagType function of the tag.js file. a transative dependency of SchematicJS

it checks what type the tag is instanceof.

However the function receives an object with following format ```{value:"examplevalue"}```` I added a unwrap to this function, and got it working again


if(tag instanceof Object){
        tag = tag.value;
}````
me4502 commented 1 month ago

hmm, is that from a deeply nested tag, or the root tag? i wonder if this is just an issue around root tag handling, as that differs between Sponge Schematic v2 & v3

DanielSchmerber commented 1 month ago

I printed the object that is being passed into the function to the console using console.dir grafik

It is instanceof Int2, wich is not being tested for in the getTagType function. It is the only time getTagType is called when displaying a schematic using SchematicWebviewer I noticed the code in my previous comment is faulty and only fixed this specific usecase

IndexOutOfMJ commented 1 month ago

So is there a way to fix this problem ?