exportSource:
@@ -379,7 +379,7 @@ getDataURL<
Source:
@@ -487,7 +487,7 @@ getDelaySource:
@@ -595,7 +595,7 @@ getHeightSource:
@@ -703,7 +703,7 @@ getIndexSource:
@@ -811,7 +811,7 @@ getParentSource:
@@ -919,7 +919,7 @@ getSizeSource:
@@ -1027,7 +1027,7 @@ getVyiSource:
@@ -1135,7 +1135,7 @@ getWidthSource:
@@ -1243,7 +1243,7 @@ setDataURL<
Source:
@@ -1403,7 +1403,7 @@ setDelaySource:
@@ -1532,7 +1532,7 @@ Returns:
diff --git a/docs/Icon.html b/docs/Icon.html
index 0fef7bc..e72f9c1 100644
--- a/docs/Icon.html
+++ b/docs/Icon.html
@@ -24,7 +24,7 @@
@@ -4462,7 +4462,7 @@ Returns:
diff --git a/docs/VYI.html b/docs/VYI.html
index b29be91..6993f68 100644
--- a/docs/VYI.html
+++ b/docs/VYI.html
@@ -24,7 +24,7 @@
@@ -332,7 +332,7 @@ addIconSource:
@@ -455,7 +455,7 @@ Returns:
- export() → {Object}
+ export(pCompressedopt) → {Object|string}
@@ -498,7 +498,7 @@ exportSource:
@@ -517,6 +517,70 @@ exportParameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+ Attributes
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pCompressed
+
+
+
+
+
+boolean
+
+
+
+
+
+
+
+
+ <optional>
+
+
+
+
+
+
+
+
+
+
+
+ Whether the data will be compressed.
+
+
+
+
+
+
+
+
+
@@ -541,6 +605,9 @@ Returns:
Object
+|
+
+string
@@ -548,7 +615,7 @@ Returns:
- Returns the vyi data.
+ Returns the vyi data in either a binary string or an object.
@@ -606,7 +673,7 @@ getIconSource:
@@ -769,7 +836,7 @@ getIconByI
Source:
@@ -929,7 +996,7 @@ getIconCo
Source:
@@ -1037,7 +1104,7 @@ getIconNa
Source:
@@ -1145,7 +1212,7 @@ getIconsSource:
@@ -1249,7 +1316,7 @@ getNameSource:
@@ -1508,6 +1575,166 @@ Returns:
+
+
+
+
+
+
+
+ readFile(pVyiData) → {Promise.<VYI>}
+
+
+
+
+
+
+ Read the file from a binary string.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ - Source:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Parameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pVyiData
+
+
+
+
+
+string
+
+
+
+
+
+
+
+
+
+
+ The compressed binary string representing the vyi data.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Returns:
+
+
+
+
+ -
+ Type:
+
+ -
+
+
Promise.<VYI>
+
+
+
+
+
+
+
+ - An vyi that was compressed in the file.
+
+
+
+
+
+
+
@@ -1558,7 +1785,7 @@ removeIcon<
Source:
@@ -1693,7 +1920,7 @@ removeI
Source:
@@ -1828,7 +2055,7 @@ remov
Source:
@@ -1963,7 +2190,7 @@ renameSource:
@@ -2067,7 +2294,7 @@ Parameters:
diff --git a/docs/frame.mjs.html b/docs/frame.mjs.html
index 117f2a5..24e501d 100644
--- a/docs/frame.mjs.html
+++ b/docs/frame.mjs.html
@@ -24,7 +24,7 @@
@@ -69,12 +69,6 @@ frame.mjs
* @type {Icon}
*/
parent;
- /**
- * The vyi this frame belongs to.
- * @private
- * @type {VYI}
- */
- vyi;
/**
* The default delay in ms of frames.
* @private
@@ -118,7 +112,6 @@ frame.mjs
if (!pParent || this.parent) return;
if (pParent instanceof Icon) {
this.parent = pParent;
- this.vyi = pParent.vyi;
// If no delay is found, get it from the parent.
if (!this.getDelay()) {
this.setDelay(this.parent.getDelay());
@@ -209,7 +202,7 @@ frame.mjs
* @returns {VYI} The vyi this frame belongs to.
*/
getVyi() {
- return this.vyi;
+ return this?.parent?.vyi;
}
/**
* Gets the icon this frame belongs to.
@@ -242,7 +235,7 @@ frame.mjs
diff --git a/docs/icon.mjs.html b/docs/icon.mjs.html
index e07bed7..c8b8c30 100644
--- a/docs/icon.mjs.html
+++ b/docs/icon.mjs.html
@@ -24,7 +24,7 @@
@@ -707,7 +707,7 @@ icon.mjs
diff --git a/docs/index.html b/docs/index.html
index d15e830..ac460d4 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@ Global Dependency
diff --git a/docs/vyi.mjs.html b/docs/vyi.mjs.html
index d765de8..dca054b 100644
--- a/docs/vyi.mjs.html
+++ b/docs/vyi.mjs.html
@@ -24,7 +24,7 @@
@@ -99,10 +99,22 @@ vyi.mjs
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)) {
@@ -113,11 +125,40 @@ vyi.mjs
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
@@ -151,16 +192,23 @@ vyi.mjs
* @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
+ }
}
}
/**
@@ -304,12 +352,18 @@ vyi.mjs
}
/**
* 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;
}
}
@@ -326,7 +380,7 @@ vyi.mjs
diff --git a/package-lock.json b/package-lock.json
index fb59a60..dd167ea 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-babel": "^6.0.4",
diff --git a/package.json b/package.json
index 0ec505b..0ba9ae3 100644
--- a/package.json
+++ b/package.json
@@ -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": {
diff --git a/src/vyi.mjs b/src/vyi.mjs
index 03e27ac..4af0553 100644
--- a/src/vyi.mjs
+++ b/src/vyi.mjs
@@ -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)) {
@@ -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} - 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
@@ -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
+ }
}
}
/**
@@ -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;
}
}
getDelaySource:
@@ -595,7 +595,7 @@ getHeightSource:
@@ -703,7 +703,7 @@ getIndexSource:
@@ -811,7 +811,7 @@ getParentSource:
@@ -919,7 +919,7 @@ getSizeSource:
@@ -1027,7 +1027,7 @@ getVyiSource:
@@ -1135,7 +1135,7 @@ getWidthSource:
@@ -1243,7 +1243,7 @@ setDataURL<
Source:
@@ -1403,7 +1403,7 @@ setDelaySource:
@@ -1532,7 +1532,7 @@ Returns:
diff --git a/docs/Icon.html b/docs/Icon.html
index 0fef7bc..e72f9c1 100644
--- a/docs/Icon.html
+++ b/docs/Icon.html
@@ -24,7 +24,7 @@
@@ -4462,7 +4462,7 @@ Returns:
diff --git a/docs/VYI.html b/docs/VYI.html
index b29be91..6993f68 100644
--- a/docs/VYI.html
+++ b/docs/VYI.html
@@ -24,7 +24,7 @@
@@ -332,7 +332,7 @@ addIconSource:
@@ -455,7 +455,7 @@ Returns:
- export() → {Object}
+ export(pCompressedopt) → {Object|string}
@@ -498,7 +498,7 @@ exportSource:
@@ -517,6 +517,70 @@ exportParameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+ Attributes
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pCompressed
+
+
+
+
+
+boolean
+
+
+
+
+
+
+
+
+ <optional>
+
+
+
+
+
+
+
+
+
+
+
+ Whether the data will be compressed.
+
+
+
+
+
+
+
+
+
@@ -541,6 +605,9 @@ Returns:
Object
+|
+
+string
@@ -548,7 +615,7 @@ Returns:
- Returns the vyi data.
+ Returns the vyi data in either a binary string or an object.
@@ -606,7 +673,7 @@ getIconSource:
@@ -769,7 +836,7 @@ getIconByI
Source:
@@ -929,7 +996,7 @@ getIconCo
Source:
@@ -1037,7 +1104,7 @@ getIconNa
Source:
@@ -1145,7 +1212,7 @@ getIconsSource:
@@ -1249,7 +1316,7 @@ getNameSource:
@@ -1508,6 +1575,166 @@ Returns:
+
+
+
+
+
+
+
+ readFile(pVyiData) → {Promise.<VYI>}
+
+
+
+
+
+
+ Read the file from a binary string.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ - Source:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Parameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pVyiData
+
+
+
+
+
+string
+
+
+
+
+
+
+
+
+
+
+ The compressed binary string representing the vyi data.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Returns:
+
+
+
+
+ -
+ Type:
+
+ -
+
+
Promise.<VYI>
+
+
+
+
+
+
+
+ - An vyi that was compressed in the file.
+
+
+
+
+
+
+
@@ -1558,7 +1785,7 @@ removeIcon<
Source:
@@ -1693,7 +1920,7 @@ removeI
Source:
@@ -1828,7 +2055,7 @@ remov
Source:
@@ -1963,7 +2190,7 @@ renameSource:
@@ -2067,7 +2294,7 @@ Parameters:
diff --git a/docs/frame.mjs.html b/docs/frame.mjs.html
index 117f2a5..24e501d 100644
--- a/docs/frame.mjs.html
+++ b/docs/frame.mjs.html
@@ -24,7 +24,7 @@
@@ -69,12 +69,6 @@ frame.mjs
* @type {Icon}
*/
parent;
- /**
- * The vyi this frame belongs to.
- * @private
- * @type {VYI}
- */
- vyi;
/**
* The default delay in ms of frames.
* @private
@@ -118,7 +112,6 @@ frame.mjs
if (!pParent || this.parent) return;
if (pParent instanceof Icon) {
this.parent = pParent;
- this.vyi = pParent.vyi;
// If no delay is found, get it from the parent.
if (!this.getDelay()) {
this.setDelay(this.parent.getDelay());
@@ -209,7 +202,7 @@ frame.mjs
* @returns {VYI} The vyi this frame belongs to.
*/
getVyi() {
- return this.vyi;
+ return this?.parent?.vyi;
}
/**
* Gets the icon this frame belongs to.
@@ -242,7 +235,7 @@ frame.mjs
diff --git a/docs/icon.mjs.html b/docs/icon.mjs.html
index e07bed7..c8b8c30 100644
--- a/docs/icon.mjs.html
+++ b/docs/icon.mjs.html
@@ -24,7 +24,7 @@
@@ -707,7 +707,7 @@ icon.mjs
diff --git a/docs/index.html b/docs/index.html
index d15e830..ac460d4 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@ Global Dependency
diff --git a/docs/vyi.mjs.html b/docs/vyi.mjs.html
index d765de8..dca054b 100644
--- a/docs/vyi.mjs.html
+++ b/docs/vyi.mjs.html
@@ -24,7 +24,7 @@
@@ -99,10 +99,22 @@ vyi.mjs
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)) {
@@ -113,11 +125,40 @@ vyi.mjs
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
@@ -151,16 +192,23 @@ vyi.mjs
* @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
+ }
}
}
/**
@@ -304,12 +352,18 @@ vyi.mjs
}
/**
* 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;
}
}
@@ -326,7 +380,7 @@ vyi.mjs
diff --git a/package-lock.json b/package-lock.json
index fb59a60..dd167ea 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-babel": "^6.0.4",
diff --git a/package.json b/package.json
index 0ec505b..0ba9ae3 100644
--- a/package.json
+++ b/package.json
@@ -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": {
diff --git a/src/vyi.mjs b/src/vyi.mjs
index 03e27ac..4af0553 100644
--- a/src/vyi.mjs
+++ b/src/vyi.mjs
@@ -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)) {
@@ -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} - 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
@@ -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
+ }
}
}
/**
@@ -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;
}
}
getIndexSource:
@@ -811,7 +811,7 @@ getParentSource:
@@ -919,7 +919,7 @@ getSizeSource:
@@ -1027,7 +1027,7 @@ getVyiSource:
@@ -1135,7 +1135,7 @@ getWidthSource:
@@ -1243,7 +1243,7 @@ setDataURL<
Source:
@@ -1403,7 +1403,7 @@ setDelaySource:
@@ -1532,7 +1532,7 @@ Returns:
diff --git a/docs/Icon.html b/docs/Icon.html
index 0fef7bc..e72f9c1 100644
--- a/docs/Icon.html
+++ b/docs/Icon.html
@@ -24,7 +24,7 @@
@@ -4462,7 +4462,7 @@ Returns:
diff --git a/docs/VYI.html b/docs/VYI.html
index b29be91..6993f68 100644
--- a/docs/VYI.html
+++ b/docs/VYI.html
@@ -24,7 +24,7 @@
@@ -332,7 +332,7 @@ addIconSource:
@@ -455,7 +455,7 @@ Returns:
- export() → {Object}
+ export(pCompressedopt) → {Object|string}
@@ -498,7 +498,7 @@ exportSource:
@@ -517,6 +517,70 @@ exportParameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+ Attributes
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pCompressed
+
+
+
+
+
+boolean
+
+
+
+
+
+
+
+
+ <optional>
+
+
+
+
+
+
+
+
+
+
+
+ Whether the data will be compressed.
+
+
+
+
+
+
+
+
+
@@ -541,6 +605,9 @@ Returns:
Object
+|
+
+string
@@ -548,7 +615,7 @@ Returns:
- Returns the vyi data.
+ Returns the vyi data in either a binary string or an object.
@@ -606,7 +673,7 @@ getIconSource:
@@ -769,7 +836,7 @@ getIconByI
Source:
@@ -929,7 +996,7 @@ getIconCo
Source:
@@ -1037,7 +1104,7 @@ getIconNa
Source:
@@ -1145,7 +1212,7 @@ getIconsSource:
@@ -1249,7 +1316,7 @@ getNameSource:
@@ -1508,6 +1575,166 @@ Returns:
+
+
+
+
+
+
+
+ readFile(pVyiData) → {Promise.<VYI>}
+
+
+
+
+
+
+ Read the file from a binary string.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ - Source:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Parameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pVyiData
+
+
+
+
+
+string
+
+
+
+
+
+
+
+
+
+
+ The compressed binary string representing the vyi data.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Returns:
+
+
+
+
+ -
+ Type:
+
+ -
+
+
Promise.<VYI>
+
+
+
+
+
+
+
+ - An vyi that was compressed in the file.
+
+
+
+
+
+
+
@@ -1558,7 +1785,7 @@ removeIcon<
Source:
@@ -1693,7 +1920,7 @@ removeI
Source:
@@ -1828,7 +2055,7 @@ remov
Source:
@@ -1963,7 +2190,7 @@ renameSource:
@@ -2067,7 +2294,7 @@ Parameters:
diff --git a/docs/frame.mjs.html b/docs/frame.mjs.html
index 117f2a5..24e501d 100644
--- a/docs/frame.mjs.html
+++ b/docs/frame.mjs.html
@@ -24,7 +24,7 @@
@@ -69,12 +69,6 @@ frame.mjs
* @type {Icon}
*/
parent;
- /**
- * The vyi this frame belongs to.
- * @private
- * @type {VYI}
- */
- vyi;
/**
* The default delay in ms of frames.
* @private
@@ -118,7 +112,6 @@ frame.mjs
if (!pParent || this.parent) return;
if (pParent instanceof Icon) {
this.parent = pParent;
- this.vyi = pParent.vyi;
// If no delay is found, get it from the parent.
if (!this.getDelay()) {
this.setDelay(this.parent.getDelay());
@@ -209,7 +202,7 @@ frame.mjs
* @returns {VYI} The vyi this frame belongs to.
*/
getVyi() {
- return this.vyi;
+ return this?.parent?.vyi;
}
/**
* Gets the icon this frame belongs to.
@@ -242,7 +235,7 @@ frame.mjs
diff --git a/docs/icon.mjs.html b/docs/icon.mjs.html
index e07bed7..c8b8c30 100644
--- a/docs/icon.mjs.html
+++ b/docs/icon.mjs.html
@@ -24,7 +24,7 @@
@@ -707,7 +707,7 @@ icon.mjs
diff --git a/docs/index.html b/docs/index.html
index d15e830..ac460d4 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@ Global Dependency
diff --git a/docs/vyi.mjs.html b/docs/vyi.mjs.html
index d765de8..dca054b 100644
--- a/docs/vyi.mjs.html
+++ b/docs/vyi.mjs.html
@@ -24,7 +24,7 @@
@@ -99,10 +99,22 @@ vyi.mjs
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)) {
@@ -113,11 +125,40 @@ vyi.mjs
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
@@ -151,16 +192,23 @@ vyi.mjs
* @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
+ }
}
}
/**
@@ -304,12 +352,18 @@ vyi.mjs
}
/**
* 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;
}
}
@@ -326,7 +380,7 @@ vyi.mjs
diff --git a/package-lock.json b/package-lock.json
index fb59a60..dd167ea 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-babel": "^6.0.4",
diff --git a/package.json b/package.json
index 0ec505b..0ba9ae3 100644
--- a/package.json
+++ b/package.json
@@ -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": {
diff --git a/src/vyi.mjs b/src/vyi.mjs
index 03e27ac..4af0553 100644
--- a/src/vyi.mjs
+++ b/src/vyi.mjs
@@ -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)) {
@@ -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} - 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
@@ -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
+ }
}
}
/**
@@ -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;
}
}
getSizeSource:
@@ -1027,7 +1027,7 @@ getVyiSource:
@@ -1135,7 +1135,7 @@ getWidthSource:
@@ -1243,7 +1243,7 @@ setDataURL<
Source:
@@ -1403,7 +1403,7 @@ setDelaySource:
@@ -1532,7 +1532,7 @@ Returns:
diff --git a/docs/Icon.html b/docs/Icon.html
index 0fef7bc..e72f9c1 100644
--- a/docs/Icon.html
+++ b/docs/Icon.html
@@ -24,7 +24,7 @@
@@ -4462,7 +4462,7 @@ Returns:
diff --git a/docs/VYI.html b/docs/VYI.html
index b29be91..6993f68 100644
--- a/docs/VYI.html
+++ b/docs/VYI.html
@@ -24,7 +24,7 @@
@@ -332,7 +332,7 @@ addIconSource:
@@ -455,7 +455,7 @@ Returns:
- export() → {Object}
+ export(pCompressedopt) → {Object|string}
@@ -498,7 +498,7 @@ exportSource:
@@ -517,6 +517,70 @@ exportParameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+ Attributes
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pCompressed
+
+
+
+
+
+boolean
+
+
+
+
+
+
+
+
+ <optional>
+
+
+
+
+
+
+
+
+
+
+
+ Whether the data will be compressed.
+
+
+
+
+
+
+
+
+
@@ -541,6 +605,9 @@ Returns:
Object
+|
+
+string
@@ -548,7 +615,7 @@ Returns:
- Returns the vyi data.
+ Returns the vyi data in either a binary string or an object.
@@ -606,7 +673,7 @@ getIconSource:
@@ -769,7 +836,7 @@ getIconByI
Source:
@@ -929,7 +996,7 @@ getIconCo
Source:
@@ -1037,7 +1104,7 @@ getIconNa
Source:
@@ -1145,7 +1212,7 @@ getIconsSource:
@@ -1249,7 +1316,7 @@ getNameSource:
@@ -1508,6 +1575,166 @@ Returns:
+
+
+
+
+
+
+
+ readFile(pVyiData) → {Promise.<VYI>}
+
+
+
+
+
+
+ Read the file from a binary string.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ - Source:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Parameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pVyiData
+
+
+
+
+
+string
+
+
+
+
+
+
+
+
+
+
+ The compressed binary string representing the vyi data.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Returns:
+
+
+
+
+ -
+ Type:
+
+ -
+
+
Promise.<VYI>
+
+
+
+
+
+
+
+ - An vyi that was compressed in the file.
+
+
+
+
+
+
+
@@ -1558,7 +1785,7 @@ removeIcon<
Source:
@@ -1693,7 +1920,7 @@ removeI
Source:
@@ -1828,7 +2055,7 @@ remov
Source:
@@ -1963,7 +2190,7 @@ renameSource:
@@ -2067,7 +2294,7 @@ Parameters:
diff --git a/docs/frame.mjs.html b/docs/frame.mjs.html
index 117f2a5..24e501d 100644
--- a/docs/frame.mjs.html
+++ b/docs/frame.mjs.html
@@ -24,7 +24,7 @@
@@ -69,12 +69,6 @@ frame.mjs
* @type {Icon}
*/
parent;
- /**
- * The vyi this frame belongs to.
- * @private
- * @type {VYI}
- */
- vyi;
/**
* The default delay in ms of frames.
* @private
@@ -118,7 +112,6 @@ frame.mjs
if (!pParent || this.parent) return;
if (pParent instanceof Icon) {
this.parent = pParent;
- this.vyi = pParent.vyi;
// If no delay is found, get it from the parent.
if (!this.getDelay()) {
this.setDelay(this.parent.getDelay());
@@ -209,7 +202,7 @@ frame.mjs
* @returns {VYI} The vyi this frame belongs to.
*/
getVyi() {
- return this.vyi;
+ return this?.parent?.vyi;
}
/**
* Gets the icon this frame belongs to.
@@ -242,7 +235,7 @@ frame.mjs
diff --git a/docs/icon.mjs.html b/docs/icon.mjs.html
index e07bed7..c8b8c30 100644
--- a/docs/icon.mjs.html
+++ b/docs/icon.mjs.html
@@ -24,7 +24,7 @@
@@ -707,7 +707,7 @@ icon.mjs
diff --git a/docs/index.html b/docs/index.html
index d15e830..ac460d4 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@ Global Dependency
diff --git a/docs/vyi.mjs.html b/docs/vyi.mjs.html
index d765de8..dca054b 100644
--- a/docs/vyi.mjs.html
+++ b/docs/vyi.mjs.html
@@ -24,7 +24,7 @@
@@ -99,10 +99,22 @@ vyi.mjs
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)) {
@@ -113,11 +125,40 @@ vyi.mjs
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
@@ -151,16 +192,23 @@ vyi.mjs
* @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
+ }
}
}
/**
@@ -304,12 +352,18 @@ vyi.mjs
}
/**
* 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;
}
}
@@ -326,7 +380,7 @@ vyi.mjs
diff --git a/package-lock.json b/package-lock.json
index fb59a60..dd167ea 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-babel": "^6.0.4",
diff --git a/package.json b/package.json
index 0ec505b..0ba9ae3 100644
--- a/package.json
+++ b/package.json
@@ -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": {
diff --git a/src/vyi.mjs b/src/vyi.mjs
index 03e27ac..4af0553 100644
--- a/src/vyi.mjs
+++ b/src/vyi.mjs
@@ -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)) {
@@ -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} - 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
@@ -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
+ }
}
}
/**
@@ -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;
}
}
getWidthSource:
@@ -1243,7 +1243,7 @@ setDataURL<
Source:
@@ -1403,7 +1403,7 @@ setDelaySource:
@@ -1532,7 +1532,7 @@ Returns:
diff --git a/docs/Icon.html b/docs/Icon.html
index 0fef7bc..e72f9c1 100644
--- a/docs/Icon.html
+++ b/docs/Icon.html
@@ -24,7 +24,7 @@
@@ -4462,7 +4462,7 @@ Returns:
diff --git a/docs/VYI.html b/docs/VYI.html
index b29be91..6993f68 100644
--- a/docs/VYI.html
+++ b/docs/VYI.html
@@ -24,7 +24,7 @@
@@ -332,7 +332,7 @@ addIconSource:
@@ -455,7 +455,7 @@ Returns:
- export() → {Object}
+ export(pCompressedopt) → {Object|string}
@@ -498,7 +498,7 @@ exportSource:
@@ -517,6 +517,70 @@ exportParameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+ Attributes
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pCompressed
+
+
+
+
+
+boolean
+
+
+
+
+
+
+
+
+ <optional>
+
+
+
+
+
+
+
+
+
+
+
+ Whether the data will be compressed.
+
+
+
+
+
+
+
+
+
@@ -541,6 +605,9 @@ Returns:
Object
+|
+
+string
@@ -548,7 +615,7 @@ Returns:
- Returns the vyi data.
+ Returns the vyi data in either a binary string or an object.
@@ -606,7 +673,7 @@ getIconSource:
@@ -769,7 +836,7 @@ getIconByI
Source:
@@ -929,7 +996,7 @@ getIconCo
Source:
@@ -1037,7 +1104,7 @@ getIconNa
Source:
@@ -1145,7 +1212,7 @@ getIconsSource:
@@ -1249,7 +1316,7 @@ getNameSource:
@@ -1508,6 +1575,166 @@ Returns:
+
+
+
+
+
+
+
+ readFile(pVyiData) → {Promise.<VYI>}
+
+
+
+
+
+
+ Read the file from a binary string.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ - Source:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Parameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pVyiData
+
+
+
+
+
+string
+
+
+
+
+
+
+
+
+
+
+ The compressed binary string representing the vyi data.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Returns:
+
+
+
+
+ -
+ Type:
+
+ -
+
+
Promise.<VYI>
+
+
+
+
+
+
+
+ - An vyi that was compressed in the file.
+
+
+
+
+
+
+
@@ -1558,7 +1785,7 @@ removeIcon<
Source:
@@ -1693,7 +1920,7 @@ removeI
Source:
@@ -1828,7 +2055,7 @@ remov
Source:
@@ -1963,7 +2190,7 @@ renameSource:
@@ -2067,7 +2294,7 @@ Parameters:
diff --git a/docs/frame.mjs.html b/docs/frame.mjs.html
index 117f2a5..24e501d 100644
--- a/docs/frame.mjs.html
+++ b/docs/frame.mjs.html
@@ -24,7 +24,7 @@
@@ -69,12 +69,6 @@ frame.mjs
* @type {Icon}
*/
parent;
- /**
- * The vyi this frame belongs to.
- * @private
- * @type {VYI}
- */
- vyi;
/**
* The default delay in ms of frames.
* @private
@@ -118,7 +112,6 @@ frame.mjs
if (!pParent || this.parent) return;
if (pParent instanceof Icon) {
this.parent = pParent;
- this.vyi = pParent.vyi;
// If no delay is found, get it from the parent.
if (!this.getDelay()) {
this.setDelay(this.parent.getDelay());
@@ -209,7 +202,7 @@ frame.mjs
* @returns {VYI} The vyi this frame belongs to.
*/
getVyi() {
- return this.vyi;
+ return this?.parent?.vyi;
}
/**
* Gets the icon this frame belongs to.
@@ -242,7 +235,7 @@ frame.mjs
diff --git a/docs/icon.mjs.html b/docs/icon.mjs.html
index e07bed7..c8b8c30 100644
--- a/docs/icon.mjs.html
+++ b/docs/icon.mjs.html
@@ -24,7 +24,7 @@
@@ -707,7 +707,7 @@ icon.mjs
diff --git a/docs/index.html b/docs/index.html
index d15e830..ac460d4 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@ Global Dependency
diff --git a/docs/vyi.mjs.html b/docs/vyi.mjs.html
index d765de8..dca054b 100644
--- a/docs/vyi.mjs.html
+++ b/docs/vyi.mjs.html
@@ -24,7 +24,7 @@
@@ -99,10 +99,22 @@ vyi.mjs
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)) {
@@ -113,11 +125,40 @@ vyi.mjs
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
@@ -151,16 +192,23 @@ vyi.mjs
* @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
+ }
}
}
/**
@@ -304,12 +352,18 @@ vyi.mjs
}
/**
* 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;
}
}
@@ -326,7 +380,7 @@ vyi.mjs
diff --git a/package-lock.json b/package-lock.json
index fb59a60..dd167ea 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-babel": "^6.0.4",
diff --git a/package.json b/package.json
index 0ec505b..0ba9ae3 100644
--- a/package.json
+++ b/package.json
@@ -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": {
diff --git a/src/vyi.mjs b/src/vyi.mjs
index 03e27ac..4af0553 100644
--- a/src/vyi.mjs
+++ b/src/vyi.mjs
@@ -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)) {
@@ -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} - 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
@@ -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
+ }
}
}
/**
@@ -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;
}
}
setDelaySource:
@@ -1532,7 +1532,7 @@ Returns:
diff --git a/docs/Icon.html b/docs/Icon.html
index 0fef7bc..e72f9c1 100644
--- a/docs/Icon.html
+++ b/docs/Icon.html
@@ -24,7 +24,7 @@
@@ -4462,7 +4462,7 @@ Returns:
diff --git a/docs/VYI.html b/docs/VYI.html
index b29be91..6993f68 100644
--- a/docs/VYI.html
+++ b/docs/VYI.html
@@ -24,7 +24,7 @@
@@ -332,7 +332,7 @@ addIconSource:
@@ -455,7 +455,7 @@ Returns:
- export() → {Object}
+ export(pCompressedopt) → {Object|string}
@@ -498,7 +498,7 @@ exportSource:
@@ -517,6 +517,70 @@ exportParameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+ Attributes
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pCompressed
+
+
+
+
+
+boolean
+
+
+
+
+
+
+
+
+ <optional>
+
+
+
+
+
+
+
+
+
+
+
+ Whether the data will be compressed.
+
+
+
+
+
+
+
+
+
@@ -541,6 +605,9 @@ Returns:
Object
+|
+
+string
@@ -548,7 +615,7 @@ Returns:
- Returns the vyi data.
+ Returns the vyi data in either a binary string or an object.
@@ -606,7 +673,7 @@ getIconSource:
@@ -769,7 +836,7 @@ getIconByI
Source:
@@ -929,7 +996,7 @@ getIconCo
Source:
@@ -1037,7 +1104,7 @@ getIconNa
Source:
@@ -1145,7 +1212,7 @@ getIconsSource:
@@ -1249,7 +1316,7 @@ getNameSource:
@@ -1508,6 +1575,166 @@ Returns:
+
+
+
+
+
+
+
+ readFile(pVyiData) → {Promise.<VYI>}
+
+
+
+
+
+
+ Read the file from a binary string.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ - Source:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ Parameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pVyiData
+
+
+
+
+
+string
+
+
+
+
+
+
+
+
+
+
+ The compressed binary string representing the vyi data.
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Returns:
+
+
+
+
+ -
+ Type:
+
+ -
+
+
Promise.<VYI>
+
+
+
+
+
+
+
+ - An vyi that was compressed in the file.
+
+
+
+
+
+
+
@@ -1558,7 +1785,7 @@ removeIcon<
Source:
@@ -1693,7 +1920,7 @@ removeI
Source:
@@ -1828,7 +2055,7 @@ remov
Source:
@@ -1963,7 +2190,7 @@ renameSource:
@@ -2067,7 +2294,7 @@ Parameters:
diff --git a/docs/frame.mjs.html b/docs/frame.mjs.html
index 117f2a5..24e501d 100644
--- a/docs/frame.mjs.html
+++ b/docs/frame.mjs.html
@@ -24,7 +24,7 @@
@@ -69,12 +69,6 @@ frame.mjs
* @type {Icon}
*/
parent;
- /**
- * The vyi this frame belongs to.
- * @private
- * @type {VYI}
- */
- vyi;
/**
* The default delay in ms of frames.
* @private
@@ -118,7 +112,6 @@ frame.mjs
if (!pParent || this.parent) return;
if (pParent instanceof Icon) {
this.parent = pParent;
- this.vyi = pParent.vyi;
// If no delay is found, get it from the parent.
if (!this.getDelay()) {
this.setDelay(this.parent.getDelay());
@@ -209,7 +202,7 @@ frame.mjs
* @returns {VYI} The vyi this frame belongs to.
*/
getVyi() {
- return this.vyi;
+ return this?.parent?.vyi;
}
/**
* Gets the icon this frame belongs to.
@@ -242,7 +235,7 @@ frame.mjs
diff --git a/docs/icon.mjs.html b/docs/icon.mjs.html
index e07bed7..c8b8c30 100644
--- a/docs/icon.mjs.html
+++ b/docs/icon.mjs.html
@@ -24,7 +24,7 @@
@@ -707,7 +707,7 @@ icon.mjs
diff --git a/docs/index.html b/docs/index.html
index d15e830..ac460d4 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@ Global Dependency
diff --git a/docs/vyi.mjs.html b/docs/vyi.mjs.html
index d765de8..dca054b 100644
--- a/docs/vyi.mjs.html
+++ b/docs/vyi.mjs.html
@@ -24,7 +24,7 @@
@@ -99,10 +99,22 @@ vyi.mjs
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)) {
@@ -113,11 +125,40 @@ vyi.mjs
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
@@ -151,16 +192,23 @@ vyi.mjs
* @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
+ }
}
}
/**
@@ -304,12 +352,18 @@ vyi.mjs
}
/**
* 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;
}
}
@@ -326,7 +380,7 @@ vyi.mjs
diff --git a/package-lock.json b/package-lock.json
index fb59a60..dd167ea 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-babel": "^6.0.4",
diff --git a/package.json b/package.json
index 0ec505b..0ba9ae3 100644
--- a/package.json
+++ b/package.json
@@ -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": {
diff --git a/src/vyi.mjs b/src/vyi.mjs
index 03e27ac..4af0553 100644
--- a/src/vyi.mjs
+++ b/src/vyi.mjs
@@ -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)) {
@@ -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} - 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
@@ -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
+ }
}
}
/**
@@ -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;
}
}
Returns:
diff --git a/docs/VYI.html b/docs/VYI.html index b29be91..6993f68 100644 --- a/docs/VYI.html +++ b/docs/VYI.html @@ -24,7 +24,7 @@
@@ -332,7 +332,7 @@ addIconSource:
+
+
+addIconSource:
@@ -455,7 +455,7 @@ Returns:
- export() → {Object}
+ export(pCompressedopt) → {Object|string}
@@ -498,7 +498,7 @@ exportSource:
@@ -517,6 +517,70 @@ exportParameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+ Attributes
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pCompressed
+
+
+
+
+
+boolean
+
+
+
+
+
+
+
+
+ <optional>
+
+
+
+
+
+
+
+
+
+
+
+ Whether the data will be compressed.
+
+
+
+
+
+
+
+
+
@@ -541,6 +605,9 @@ Returns:
Object
+|
+
+string
@@ -548,7 +615,7 @@ Returns:
- Returns the vyi data.
+ Returns the vyi data in either a binary string or an object.
@@ -606,7 +673,7 @@ getIconSource:
@@ -769,7 +836,7 @@ getIconByI
Source:
@@ -929,7 +996,7 @@ getIconCo
Source:
@@ -1037,7 +1104,7 @@ getIconNa
Source:
@@ -1145,7 +1212,7 @@ getIconsSource:
@@ -1249,7 +1316,7 @@ getNameSource:
@@ -1508,6 +1575,166 @@ Returns:
+
exportParameters:
+
+
+
+
+
+
+ Name
+
+
+ Type
+
+
+ Attributes
+
+
+
+
+ Description
+
+
+
+
+
+
+
+
+ pCompressed
+
+
+
+
+
+boolean
+
+
+
+
+
+
+
+
+ <optional>
+
+
+
+
+
+
+
+
+
+
+
+ Whether the data will be compressed.
+
+
+
+
+
+
+
+
+
@@ -541,6 +605,9 @@ Returns:
Object
+|
+
+string
@@ -548,7 +615,7 @@ Returns:
- Returns the vyi data.
+ Returns the vyi data in either a binary string or an object.
@@ -606,7 +673,7 @@ getIconSource:
@@ -769,7 +836,7 @@ getIconByI
Source:
@@ -929,7 +996,7 @@ getIconCo
Source:
@@ -1037,7 +1104,7 @@ getIconNa
Source:
@@ -1145,7 +1212,7 @@ getIconsSource:
@@ -1249,7 +1316,7 @@ getNameSource:
@@ -1508,6 +1575,166 @@ Returns:
+
pCompressed
boolean
+
+
+
+ + + + + + +
Object
+|
+
+string
getIconByI
Source:
@@ -929,7 +996,7 @@ getIconCo
Source:
@@ -1037,7 +1104,7 @@ getIconNa
Source:
@@ -1145,7 +1212,7 @@ getIconsSource:
@@ -1249,7 +1316,7 @@ getNameSource:
@@ -1508,6 +1575,166 @@ Returns:
+
getIconNa
Source:
@@ -1145,7 +1212,7 @@ getIconsSource:
@@ -1249,7 +1316,7 @@ getNameSource:
@@ -1508,6 +1575,166 @@ Returns:
+
getNameSource:
@@ -1508,6 +1575,166 @@ Returns:
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1558,7 +1785,7 @@ readFile(pVyiData) → {Promise.<VYI>}
+ + + + + +
+ Read the file from a binary string.
+
+
+
+
+
+
+-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
- Source: +
- + + + + + + + +
Parameters:
+ + +Name | + + +Type | + + + + + +Description | +
---|---|---|
pVyiData |
+
+
+
+
+
+string
+
+
+
+ |
+
+
+
+
+
+ + The compressed binary string representing the vyi data. + + | +
+
+
+
+
Returns:
+ + + +-
+
- + Type: + +
-
+
+
Promise.<VYI>
+ + +
+
+ - An vyi that was compressed in the file.
+
+
+
+removeIcon<
Source:
@@ -1693,7 +1920,7 @@ removeI
Source:
@@ -1828,7 +2055,7 @@ remov
Source:
@@ -1963,7 +2190,7 @@ renameSource:
@@ -2067,7 +2294,7 @@ Parameters:
diff --git a/docs/frame.mjs.html b/docs/frame.mjs.html
index 117f2a5..24e501d 100644
--- a/docs/frame.mjs.html
+++ b/docs/frame.mjs.html
@@ -24,7 +24,7 @@
@@ -69,12 +69,6 @@ frame.mjs
* @type {Icon}
*/
parent;
- /**
- * The vyi this frame belongs to.
- * @private
- * @type {VYI}
- */
- vyi;
/**
* The default delay in ms of frames.
* @private
@@ -118,7 +112,6 @@ frame.mjs
if (!pParent || this.parent) return;
if (pParent instanceof Icon) {
this.parent = pParent;
- this.vyi = pParent.vyi;
// If no delay is found, get it from the parent.
if (!this.getDelay()) {
this.setDelay(this.parent.getDelay());
@@ -209,7 +202,7 @@ frame.mjs
* @returns {VYI} The vyi this frame belongs to.
*/
getVyi() {
- return this.vyi;
+ return this?.parent?.vyi;
}
/**
* Gets the icon this frame belongs to.
@@ -242,7 +235,7 @@ frame.mjs
diff --git a/docs/icon.mjs.html b/docs/icon.mjs.html
index e07bed7..c8b8c30 100644
--- a/docs/icon.mjs.html
+++ b/docs/icon.mjs.html
@@ -24,7 +24,7 @@
@@ -707,7 +707,7 @@ icon.mjs
diff --git a/docs/index.html b/docs/index.html
index d15e830..ac460d4 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@ Global Dependency
diff --git a/docs/vyi.mjs.html b/docs/vyi.mjs.html
index d765de8..dca054b 100644
--- a/docs/vyi.mjs.html
+++ b/docs/vyi.mjs.html
@@ -24,7 +24,7 @@
@@ -99,10 +99,22 @@ vyi.mjs
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)) {
@@ -113,11 +125,40 @@ vyi.mjs
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
@@ -151,16 +192,23 @@ vyi.mjs
* @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
+ }
}
}
/**
@@ -304,12 +352,18 @@ vyi.mjs
}
/**
* 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;
}
}
@@ -326,7 +380,7 @@ vyi.mjs
diff --git a/package-lock.json b/package-lock.json
index fb59a60..dd167ea 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-babel": "^6.0.4",
diff --git a/package.json b/package.json
index 0ec505b..0ba9ae3 100644
--- a/package.json
+++ b/package.json
@@ -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": {
diff --git a/src/vyi.mjs b/src/vyi.mjs
index 03e27ac..4af0553 100644
--- a/src/vyi.mjs
+++ b/src/vyi.mjs
@@ -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)) {
@@ -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} - 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
@@ -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
+ }
}
}
/**
@@ -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;
}
}
remov
Source:
@@ -1963,7 +2190,7 @@ renameSource:
@@ -2067,7 +2294,7 @@ Parameters:
diff --git a/docs/frame.mjs.html b/docs/frame.mjs.html
index 117f2a5..24e501d 100644
--- a/docs/frame.mjs.html
+++ b/docs/frame.mjs.html
@@ -24,7 +24,7 @@
@@ -69,12 +69,6 @@ frame.mjs
* @type {Icon}
*/
parent;
- /**
- * The vyi this frame belongs to.
- * @private
- * @type {VYI}
- */
- vyi;
/**
* The default delay in ms of frames.
* @private
@@ -118,7 +112,6 @@ frame.mjs
if (!pParent || this.parent) return;
if (pParent instanceof Icon) {
this.parent = pParent;
- this.vyi = pParent.vyi;
// If no delay is found, get it from the parent.
if (!this.getDelay()) {
this.setDelay(this.parent.getDelay());
@@ -209,7 +202,7 @@ frame.mjs
* @returns {VYI} The vyi this frame belongs to.
*/
getVyi() {
- return this.vyi;
+ return this?.parent?.vyi;
}
/**
* Gets the icon this frame belongs to.
@@ -242,7 +235,7 @@ frame.mjs
diff --git a/docs/icon.mjs.html b/docs/icon.mjs.html
index e07bed7..c8b8c30 100644
--- a/docs/icon.mjs.html
+++ b/docs/icon.mjs.html
@@ -24,7 +24,7 @@
@@ -707,7 +707,7 @@ icon.mjs
diff --git a/docs/index.html b/docs/index.html
index d15e830..ac460d4 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@ Global Dependency
diff --git a/docs/vyi.mjs.html b/docs/vyi.mjs.html
index d765de8..dca054b 100644
--- a/docs/vyi.mjs.html
+++ b/docs/vyi.mjs.html
@@ -24,7 +24,7 @@
@@ -99,10 +99,22 @@ vyi.mjs
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)) {
@@ -113,11 +125,40 @@ vyi.mjs
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
@@ -151,16 +192,23 @@ vyi.mjs
* @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
+ }
}
}
/**
@@ -304,12 +352,18 @@ vyi.mjs
}
/**
* 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;
}
}
@@ -326,7 +380,7 @@ vyi.mjs
diff --git a/package-lock.json b/package-lock.json
index fb59a60..dd167ea 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -1,12 +1,12 @@
{
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "vyi",
- "version": "1.4.0",
+ "version": "1.62.0",
"license": "MIT",
"devDependencies": {
"@rollup/plugin-babel": "^6.0.4",
diff --git a/package.json b/package.json
index 0ec505b..0ba9ae3 100644
--- a/package.json
+++ b/package.json
@@ -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": {
diff --git a/src/vyi.mjs b/src/vyi.mjs
index 03e27ac..4af0553 100644
--- a/src/vyi.mjs
+++ b/src/vyi.mjs
@@ -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)) {
@@ -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} - 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
@@ -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
+ }
}
}
/**
@@ -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;
}
}
Parameters:
diff --git a/docs/frame.mjs.html b/docs/frame.mjs.html index 117f2a5..24e501d 100644 --- a/docs/frame.mjs.html +++ b/docs/frame.mjs.html @@ -24,7 +24,7 @@
@@ -69,12 +69,6 @@
diff --git a/docs/icon.mjs.html b/docs/icon.mjs.html index e07bed7..c8b8c30 100644 --- a/docs/icon.mjs.html +++ b/docs/icon.mjs.html @@ -24,7 +24,7 @@
frame.mjs
* @type {Icon} */ parent; - /** - * The vyi this frame belongs to. - * @private - * @type {VYI} - */ - vyi; /** * The default delay in ms of frames. * @private @@ -118,7 +112,6 @@frame.mjs
if (!pParent || this.parent) return; if (pParent instanceof Icon) { this.parent = pParent; - this.vyi = pParent.vyi; // If no delay is found, get it from the parent. if (!this.getDelay()) { this.setDelay(this.parent.getDelay()); @@ -209,7 +202,7 @@frame.mjs
* @returns {VYI} The vyi this frame belongs to. */ getVyi() { - return this.vyi; + return this?.parent?.vyi; } /** * Gets the icon this frame belongs to. @@ -242,7 +235,7 @@frame.mjs
diff --git a/docs/icon.mjs.html b/docs/icon.mjs.html index e07bed7..c8b8c30 100644 --- a/docs/icon.mjs.html +++ b/docs/icon.mjs.html @@ -24,7 +24,7 @@
@@ -707,7 +707,7 @@
diff --git a/docs/index.html b/docs/index.html index d15e830..ac460d4 100644 --- a/docs/index.html +++ b/docs/index.html @@ -24,7 +24,7 @@
icon.mjs
diff --git a/docs/index.html b/docs/index.html index d15e830..ac460d4 100644 --- a/docs/index.html +++ b/docs/index.html @@ -24,7 +24,7 @@
@@ -76,7 +76,7 @@
diff --git a/docs/vyi.mjs.html b/docs/vyi.mjs.html index d765de8..dca054b 100644 --- a/docs/vyi.mjs.html +++ b/docs/vyi.mjs.html @@ -24,7 +24,7 @@
Global Dependency
diff --git a/docs/vyi.mjs.html b/docs/vyi.mjs.html index d765de8..dca054b 100644 --- a/docs/vyi.mjs.html +++ b/docs/vyi.mjs.html @@ -24,7 +24,7 @@
@@ -99,10 +99,22 @@
diff --git a/package-lock.json b/package-lock.json index fb59a60..dd167ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "vyi", - "version": "1.4.0", + "version": "1.62.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "vyi", - "version": "1.4.0", + "version": "1.62.0", "license": "MIT", "devDependencies": { "@rollup/plugin-babel": "^6.0.4", diff --git a/package.json b/package.json index 0ec505b..0ba9ae3 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/vyi.mjs b/src/vyi.mjs index 03e27ac..4af0553 100644 --- a/src/vyi.mjs +++ b/src/vyi.mjs @@ -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)) { @@ -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} - 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
@@ -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
+ }
}
}
/**
@@ -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;
}
}
vyi.mjs
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)) { @@ -113,11 +125,40 @@vyi.mjs
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 @@ -151,16 +192,23 @@vyi.mjs
* @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 + } } } /** @@ -304,12 +352,18 @@vyi.mjs
} /** * 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; } } @@ -326,7 +380,7 @@vyi.mjs
diff --git a/package-lock.json b/package-lock.json index fb59a60..dd167ea 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "vyi", - "version": "1.4.0", + "version": "1.62.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "vyi", - "version": "1.4.0", + "version": "1.62.0", "license": "MIT", "devDependencies": { "@rollup/plugin-babel": "^6.0.4", diff --git a/package.json b/package.json index 0ec505b..0ba9ae3 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/src/vyi.mjs b/src/vyi.mjs index 03e27ac..4af0553 100644 --- a/src/vyi.mjs +++ b/src/vyi.mjs @@ -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)) { @@ -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