Skip to content

Commit

Permalink
add: export as binary string
Browse files Browse the repository at this point in the history
  • Loading branch information
doubleactii committed Nov 6, 2024
1 parent 54470ea commit 4bf01e9
Show file tree
Hide file tree
Showing 10 changed files with 404 additions and 76 deletions.
26 changes: 13 additions & 13 deletions docs/Frame.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/Icon.html

Large diffs are not rendered by default.

259 changes: 243 additions & 16 deletions docs/VYI.html

Large diffs are not rendered by default.

13 changes: 3 additions & 10 deletions docs/frame.mjs.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/icon.mjs.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/index.html

Large diffs are not rendered by default.

84 changes: 69 additions & 15 deletions docs/vyi.mjs.html

Large diffs are not rendered by default.

4 changes: 2 additions & 2 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
@@ -1,6 +1,6 @@
{
"name": "vyi",
"version": "1.61.0",
"version": "1.62.0",
"description": "A lightweight module to read / manage .vyi files created in the Vylocity Game Engine.",
"main": "src/vyi.mjs",
"scripts": {
Expand Down
80 changes: 67 additions & 13 deletions src/vyi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,22 @@ class VYI {
let vyi;

if (typeof pVyiData === 'string') {
const isNodeEnv = typeof window === 'undefined';
vyi = isNodeEnv
? await this.readFileAndGetVYI(pVyiData)
: await this.fetchAndParseJSON(pVyiData);
try {
vyi = JSON.parse(pVyiData);
} catch(pError) {
// Binary string
if (!pVyiData.includes('.vyr') && !pVyiData.includes('.vyi')) {
vyi = pVyiData;
if (!vyi) {
throw new Error('Non vyi data found from binary string');
}
} else {
const isNodeEnv = typeof window === 'undefined';
vyi = isNodeEnv
? await this.readFileAndGetVYI(pVyiData)
: await this.fetchAndParseJSON(pVyiData);
}
}
} else if (pVyiData instanceof VYI) {
vyi = pVyiData.export();
} else if (pVyiData instanceof Object && !Array.isArray(pVyiData) && !(pVyiData instanceof ArrayBuffer || pVyiData instanceof Uint8Array)) {
Expand All @@ -72,11 +84,40 @@ class VYI {
throw new Error('Error processing: Invalid input type provided.');
}

if (!vyi || (!vyi.v || !vyi.i)) {
throw new Error('Non vyi data found.');
}

this.processVyiData(vyi);
} catch (pError) {
VYI.logger.prefix('Vyi-module').error(`${pError.message}`);
}
}
/**
* Read the file from a binary string.
* @param {string} pVyiData - The compressed binary string representing the vyi data.
* @returns {Promise<VYI>} - An vyi that was compressed in the file.
*/
readFile(pVyiData) {
return new Promise((pResolve, pReject) => {
const isNodeEnv = typeof window === 'undefined';
if (isNodeEnv) {
const data = pako.inflate(pVyiData, { to: 'string' });
const vyi = JSON.parse(data);
pResolve(vyi);
} else {
const file = new File(decompressed, 'temp');
const fileReader = new FileReader();
fileReader.readAsArrayBuffer(file);
fileReader.onload = (pEvent) => {
pResolve(pEvent.result);
}
fileReader.onerror = (pEvent) => {
pReject(new Error('Error reading file'));
}
}
});
}
/**
* Reads a file and returns the vyi from it.
* @private
Expand Down Expand Up @@ -110,16 +151,23 @@ class VYI {
* @returns {Object|string|null} - The parsed JSON object or decoded string, or null if it fails.
*/
handleBinaryData(pBinaryData) {
const arrayBuffer = pBinaryData instanceof ArrayBuffer ? pBinaryData : pBinaryData.buffer;

const byteArray = pBinaryData instanceof ArrayBuffer ? new Uint8Array(pBinaryData) : pBinaryData;
try {
// Attempt to decompress the binary data
const decompressed = pako.inflate(arrayBuffer, { to: 'string' });
const decompressed = pako.inflate(byteArray, { to: 'string' });
if (!decompressed) return null;
return JSON.parse(decompressed);
} catch {
// If decompression fails, decode it as plain text
const decodedText = new TextDecoder().decode(arrayBuffer);
return JSON.parse(decodedText);
} catch (pError) {
// Fallback attempt to decode directly as UTF-8 string
try {
const decodedText = new TextDecoder().decode(byteArray);
if (!decodedText) return null;
return JSON.parse(decodedText);
} catch (pDecodeError) {
console.error('Decoding failed:', pDecodeError);
return null; // Return null if both decompression and decoding fail
}
}
}
/**
Expand Down Expand Up @@ -263,12 +311,18 @@ class VYI {
}
/**
* Exports this VYI into VYI format.
* @returns {Object} Returns the vyi data.
* @param {boolean} [pCompressed] - Whether the data will be compressed.
* @returns {Object|string} Returns the vyi data in either a binary string or an object.
*/
export() {
export(pCompressed) {
const vyi = {};
vyi.v = this.formatVersion;
vyi.i = this.getIcons().map((pIcon) => pIcon.export());
if (pCompressed) {
const binaryData = new TextEncoder().encode(JSON.stringify(vyi));
const compressed = pako.deflate(binaryData);
return compressed;
}
return vyi;
}
}
Expand Down

0 comments on commit 4bf01e9

Please sign in to comment.