xnb.js
xnb.js is the javascript library that allows you to handle xnb files on the web or in a node.js environment.
Description
The xnb format is a compressed data format used by Microsoft XNA Game Studio and is known for its use in games such as the Stardew Valley. xnb.js allows xnb files to be handled on the web without installing any other programs, making it easy to create utility web app or programs, for games using xnb files.
Currently, xnb decompression has been fully implemented, and due to the absence of an open-source JavaScript library that implements the LZX compression algorithm, packing returns uncompressed or compressed xnb files with the lz4 compression algorithm.
Based on LeonBlade's XnbCli, xnb.js is rewritten some code dedicated to node.js into universal code regardless of environment. All credits to LeonBlade.
Try this!
Web XNB Previewer : https://lybell-art.github.io/xnb-js
Install
You can install xnb.js with npm, or download the library directly and upload it to your own web server or use the existing CDN without a build system.
I recommend that you load xnb.js using the es6 module.
cdn
You can load and use a library hosted online. Here's how to use it:
Load as ES6 Module(Recommended)
import * as XNB from "https://cdn.jsdelivr.net/npm/xnb@1.3.0/dist/xnb.module.js";
Load as UMD
<script src="https://cdn.jsdelivr.net/npm/xnb@1.3.0/dist/xnb.min.js"></script>
If you need to support ES5, such as IE11, I recommend using xnb.es5.min.js.
npm
npm install xnb
There's no default export. You can use it like this:
import * as XNB from 'xnb';
Examples
Browsers example
Load and unpack the xnb file, and show the result as image.
import { unpackToContent } from "xnb";
let previewUrl="";
const outputImageCanvas = document.getElementById("output");
document.getElementById("input").addEventListener("change",
function handleFiles()
{
if(!this.files || this.files.length === 0) return;
const file=this.files[0];
unpackToContent(file).then(function(content){
if(content.type === "png")
{
window.URL.revokeObjectURL(previewUrl);
previewUrl = window.URL.createObjectURL(content.content);
outputImageCanvas.src = previewUrl;
}
});
});
Node.js example
Load and unpack the xnb file, and save the results as files.
import { unpackToFiles } from "xnb";
import { readFile } from 'node:fs/promises';
readFile("./Abigail.xnb")
.then(xnb=>unpackToFiles(xnb, {fileName:"Abigail.xnb"}))
.then(outputs=>{
const writers = [];
for(let {data, extension} of outputs)
{
const writePath = path.resolve("./outputs", `Abigail.${extension}`);
writers.push(writeFile(writePath, data));
}
return Promise.all(writers);
});
API
See this link.
Customs
After 1.1 update, you can load and use only a portion of the existing readers, or you can add a custom reader to use it.
After 1.3 update, we bring up a file that specifies the type structure of data to be imported into the custom scheme and suggest how to unpack the easier custom data structure.
Load only part of existing readers
import * as XNB from "@xnb-js/core";
import { LightweightTexture2DReader as Texture2DReader } from "@xnb-js/readers";
XNB.setReaders({Texture2DReader});
xnb.unpackToXnbData(file);
Add custom plugins
import * as XNB from "xnb";
class CustomReader extends XNB.readers.BaseReader{
static isTypeOf(type) {
}
static hasSubType() {
}
static parseTypeList() {
}
static type()
{
}
isValueType() {
}
get type() {
}
read(buffer, resolver) {
}
write(buffer, content, resolver) {
}
parseTypeList() {
}
}
XNB.addReaders({CustomReader});
...
Add custom scheme
Custom scheme is a data structure which defines the c# class that the reflective reader reads, and has the following grammar;
const myScheme = {
DisplayName: "String",
$Description: "String",
IsDebuff: "Boolean",
IconSpriteIndex: "Int32",
$Effects: "StardewValley.GameData.Buffs.BuffAttributesData",
$ActionsOnApply: ["String"],
$CustomFields: {"String": "String"}
}
XNB.addSchemes({"StardewValley.GameData.Buffs.BuffData": myScheme});
External resource
xnb.js contains dxt.js, lz4.js, and png.js as bundle. libsquish(=dxt.js) and lz4.js were rewritten for es6 module system.
The licenses for the original libraries are as follows.
License
GNU LGPL 3.0
Other language