Skip to content

Commit

Permalink
Merge pull request #2 from lybell-art/indev
Browse files Browse the repository at this point in the history
Version 1.2 Update
  • Loading branch information
lybell-art committed Jun 22, 2022
2 parents 83ab96d + d0d81f8 commit 62ccecd
Show file tree
Hide file tree
Showing 46 changed files with 2,931 additions and 354 deletions.
2 changes: 1 addition & 1 deletion builder/builder-plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ for(let key of Object.keys(licensesDict))
if(includes[key] === true) licensesComments.push(licensesDict[key]);
}
return `/**
* xnb.js 1.1.0
* xnb.js 1.2.0
* made by Lybell( https://github.com/lybell-art/ )
* This library is based on the XnbCli made by Leonblade.
*
Expand Down
2 changes: 1 addition & 1 deletion builder/plugins-rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function header()
return {
renderChunk(code){
return `/**
* @xnb/stardewvalley 1.1.0
* @xnb/stardewvalley 1.2.0
* made by Lybell( https://github.com/lybell-art/ )
* special thanks to Concernedape(Stardew Valley Producer), 진의(Unoffical XnbCli updater)
*
Expand Down
139 changes: 135 additions & 4 deletions dist/core/xnb-core.cjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* xnb.js 1.1.0
* xnb.js 1.2.0
* made by Lybell( https://github.com/lybell-art/ )
* This library is based on the XnbCli made by Leonblade.
*
Expand Down Expand Up @@ -1840,6 +1840,96 @@ class XnbConverter {

}

function injectRGBA(data, i) {
let {
r,
g = r,
b = r,
a = 255
} = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
data[4 * i + 0] = r;
data[4 * i + 1] = g;
data[4 * i + 2] = b;
data[4 * i + 3] = a;
return [r, g, b, a];
}

function png16to8(data) {
const megascale = new Uint16Array(data);
const downscale = new Uint8Array(megascale.length);

for (let i = 0; i < megascale.length; i++) {
downscale[i] = megascale[i] >> 8;
}

return downscale;
}

function addChannels(data, originChannel) {
const size = data.length / originChannel;
const rgbaData = new Uint8Array(size * 4);
if (originChannel === 4) return data;

if (originChannel === 1) {
for (let i = 0; i < size; i++) {
injectRGBA(rgbaData, i, {
r: data[i]
});
}
} else if (originChannel === 2) {
for (let i = 0; i < size; i++) {
injectRGBA(rgbaData, i, {
r: data[i * 2],
a: data[i * 2 + 1]
});
}
} else if (originChannel === 3) {
for (let i = 0; i < size; i++) {
injectRGBA(rgbaData, i, {
r: data[i * 3],
g: data[i * 3 + 1],
b: data[i * 3 + 2]
});
}
}

return rgbaData;
}

function applyPalette(data, depth, palette) {
const oldData = new Uint8Array(data);
const length = oldData.length * 8 / depth;
const newData = new Uint8Array(length * 4);
let bitPosition = 0;

for (let i = 0; i < length; i++) {
const bytePosition = Math.floor(bitPosition / 8);
const bitOffset = 8 - bitPosition % 8 - depth;
let paletteIndex;
if (depth === 16) paletteIndex = oldData[bytePosition] << 8 | oldData[bytePosition + 1];else paletteIndex = oldData[bytePosition] >> bitOffset & 2 ** depth - 1;
[newData[i * 4], newData[i * 4 + 1], newData[i * 4 + 2], newData[i * 4 + 3]] = palette[paletteIndex];
bitPosition += depth;
}

return newData;
}

function fixPNG(pngdata) {
const {
width,
height,
channels,
depth
} = pngdata;
let {
data
} = pngdata;
if (pngdata.palette) return applyPalette(data, depth, pngdata.palette);
if (depth === 16) data = png16to8(data);
if (channels < 4) data = addChannels(data, channels);
return data;
}

var t = {
396: function _() {
!function (t) {
Expand Down Expand Up @@ -5124,7 +5214,7 @@ function resolveCompression(compressionString) {
}

async function readBlobasText(blob) {
if (typeof Blob === "function" && blob instanceof Blob) return blob.text();else if (typeof Buffer === "function" && blob instanceof Buffer) return blob.toString();
if (typeof Blob === "function" && blob instanceof Blob) return blob.text();else if (typeof Buffer === "function" && blob instanceof Buffer) return blob.toString();else return blob;
}

async function readBlobasArrayBuffer(blob) {
Expand All @@ -5134,7 +5224,8 @@ async function readBlobasArrayBuffer(blob) {
async function readExternFiles(extension, files) {
if (extension === "png") {
const rawPng = await readBlobasArrayBuffer(files.png);
const png = r(new Uint8Array(rawPng));
let png = r(new Uint8Array(rawPng));
if (png.channel !== 4 || png.depth !== 8 || png.palette !== undefined) png.data = fixPNG(png);
return {
type: "Texture2D",
data: png.data,
Expand Down Expand Up @@ -5199,6 +5290,35 @@ async function resolveImports(files) {
return jsonData;
}

function getReaderAssembly(extension) {
if (extension === "png") return "Microsoft.Xna.Framework.Content.Texture2DReader, Microsoft.Xna.Framework.Graphics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553";
if (extension === "tbin") return "xTile.Pipeline.TideReader, xTile";
if (extension === "xml") return "BmFont.XmlSourceReader, BmFont, Version=2012.1.7.0, Culture=neutral, PublicKeyToken=null";
}

function makeHeader(fileName) {
const [, extension] = extractFileName(fileName);
const readerType = getReaderAssembly(extension);
let content = {
export: fileName
};
if (extension === "png") content.format = 0;
const result = {
header: {
target: "w",
formatVersion: 5,
hidef: true,
compressed: true
},
readers: [{
type: readerType,
version: 0
}],
content
};
return JSON.stringify(result);
}

/** @api
* Asynchronously reads the file into binary and then unpacks the json data.
* XNB -> arrayBuffer -> XnbData
Expand Down Expand Up @@ -5310,14 +5430,25 @@ function xnbDataToContent(loadedXnb) {

function fileMapper(files) {
let returnMap = {};
let noHeaderMap = {};

for (let i = 0; i < files.length; i++) {
const file = files[i];
let [fileName, extension] = extractFileName(file.name);
if (extension === null) continue;
if (returnMap[fileName] === undefined) returnMap[fileName] = {};

if (returnMap[fileName] === undefined) {
returnMap[fileName] = {};
if (extension !== "json" && extension !== "yaml") noHeaderMap[fileName] = file.name;
}

const namedFileObj = returnMap[fileName];
if (typeof Blob === "function" && file instanceof Blob) namedFileObj[extension] = file;else namedFileObj[extension] = file.data;
if (extension === "json" || extension === "yaml") delete noHeaderMap[fileName];
}

for (let fileName of Object.keys(noHeaderMap)) {
returnMap[fileName].json = makeHeader(noHeaderMap[fileName]);
}

return returnMap;
Expand Down
Loading

0 comments on commit 62ccecd

Please sign in to comment.