squishjs 1.3.7 → 1.3.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squishjs",
3
- "version": "1.3.7",
3
+ "version": "1.3.8",
4
4
  "description": "squish & unsquish stuff",
5
5
  "scripts": {
6
6
  "test": "node testRunner.js"
package/src/Asset.js CHANGED
@@ -1,4 +1,8 @@
1
1
  const path = require('path');
2
+ const https = require('https');
3
+ const http = require('http');
4
+ const fs = require('fs');
5
+ const crypto = require('crypto');
2
6
  const { getAppDataPath } = require('./utils');
3
7
 
4
8
  // assets is the old stuff, save it for backward compatibility
@@ -20,14 +24,6 @@ class Asset {
20
24
  if (data) {
21
25
  this.data = data;
22
26
  }
23
-
24
- // this is dumb. was trying something but made it dumb.
25
- this.https = require('https');
26
- this.http = require('http');
27
- this.fs = require('fs');
28
- this.crypto = require('crypto');
29
- this.path = require('path');
30
- this.process = require('process');
31
27
  }
32
28
 
33
29
  getConfigValue(key, _default = undefined) {
@@ -62,9 +58,9 @@ class Asset {
62
58
  let _config = {};
63
59
 
64
60
  for (let i = 0; i < options.length; i++) {
65
- if (this.fs.existsSync(`${options[i]}/config.json`)) {
61
+ if (fs.existsSync(`${options[i]}/config.json`)) {
66
62
  console.log(`Using config at ${options[i]}`);
67
- _config = JSON.parse(this.fs.readFileSync(`${options[i]}/config.json`));
63
+ _config = JSON.parse(fs.readFileSync(`${options[i]}/config.json`));
68
64
  break;
69
65
  }
70
66
  }
@@ -76,7 +72,7 @@ class Asset {
76
72
 
77
73
 
78
74
  getHash(str) {
79
- const shasum = this.crypto.createHash('sha1');
75
+ const shasum = crypto.createHash('sha1');
80
76
  shasum.update(str);
81
77
  return shasum.digest('hex');
82
78
  };
@@ -98,7 +94,7 @@ class Asset {
98
94
  return resolve(true);
99
95
  }
100
96
  const fileLocation = this.getFileLocation(this.info.id);
101
- this.fs.exists(fileLocation, (exists) => {
97
+ fs.exists(fileLocation, (exists) => {
102
98
  resolve(exists && fileLocation);
103
99
  });
104
100
  });
@@ -124,8 +120,8 @@ class Asset {
124
120
 
125
121
  download(force) {
126
122
  const HG_ASSET_PATH = path.join(getAppDataPath(), 'asset-cache');
127
- if (!this.fs.existsSync(HG_ASSET_PATH)) {
128
- this.fs.mkdirSync(HG_ASSET_PATH);
123
+ if (!fs.existsSync(HG_ASSET_PATH)) {
124
+ fs.mkdirSync(HG_ASSET_PATH);
129
125
  }
130
126
  return new Promise((resolve, reject) => {
131
127
  this.existsLocally().then(fileLocation => {
@@ -150,7 +146,7 @@ class Asset {
150
146
  resolve(this.data);
151
147
  } else {
152
148
  this.download().then(fileLocation => {
153
- this.fs.readFile(fileLocation, (err, buf) => {
149
+ fs.readFile(fileLocation, (err, buf) => {
154
150
  if (err) {
155
151
  reject(err);
156
152
  } else {
@@ -164,13 +160,13 @@ class Asset {
164
160
  });
165
161
  }
166
162
 
167
- doDownload(assetId, path) {
163
+ doDownload(assetId, assetPath) {
168
164
  return new Promise((resolve, reject) => {
169
165
  const fileHash = this.getHash(assetId);
170
- const filePath = `${path}/${fileHash}`;
166
+ const filePath = `${assetPath}/${fileHash}`;
171
167
 
172
- const writeStream = this.fs.createWriteStream(filePath);
173
- const getModule = ASSET_URL.startsWith('https') ? this.https : this.http;
168
+ const writeStream = fs.createWriteStream(filePath);
169
+ const getModule = ASSET_URL.startsWith('https') ? https : http;
174
170
 
175
171
  writeStream.on('close', () => {
176
172
  resolve(filePath);
package/src/Squisher.js CHANGED
@@ -11,7 +11,7 @@ class Squisher {
11
11
 
12
12
  this.game = game;
13
13
  this.gameMetadata = game.constructor.metadata && game.constructor.metadata();
14
- this.assets = {};
14
+ this._assetBufferCache = {};
15
15
  this.playerSettings = {};
16
16
 
17
17
  this.customBottomLayer = customBottomLayer;
@@ -97,6 +97,10 @@ class Squisher {
97
97
  }
98
98
 
99
99
  handleNewAsset(key, asset) {
100
+ // Invalidate just this key so initialize() re-fetches only its data
101
+ if (this._assetBufferCache) {
102
+ delete this._assetBufferCache[key];
103
+ }
100
104
  return this.initialize();
101
105
  }
102
106
 
@@ -169,9 +173,6 @@ class Squisher {
169
173
 
170
174
  initialize() {
171
175
  return new Promise((resolve, reject) => {
172
-
173
-
174
- const assets = Object.assign({}, this.assets || {});
175
176
 
176
177
  const gameMetadata = this.game.constructor.metadata && this.game.constructor.metadata();
177
178
 
@@ -190,65 +191,84 @@ class Squisher {
190
191
  Object.assign(gameAssets, this.game.getAssets());
191
192
  }
192
193
 
193
- const allAssets = Object.assign(assets, gameAssets);
194
+ const assetTypeMap = {
195
+ 'image': 1,
196
+ 'audio': 2,
197
+ 'font': 3
198
+ };
194
199
 
195
- let assetBundleSize = 0;
196
- let finishedCount = 0;
197
- const totalCount = Object.keys(allAssets).length;
200
+ const assetKeys = Object.keys(gameAssets);
201
+ const totalCount = assetKeys.length;
198
202
 
199
203
  if (totalCount === 0) {
200
- this.assetBundle = [];
201
- resolve([]);
204
+ this.assetBundle = Buffer.alloc(0);
205
+ resolve(this.assetBundle);
202
206
  return;
203
207
  }
204
-
205
- for (const key in allAssets) {
206
- allAssets[key].getData().then(buf => {
207
- const assetKeyLength = 32;
208
- let keyIndex = 0;
209
- const assetKeyArray = new Array(32);
210
- while (keyIndex < assetKeyLength && keyIndex < key.length) {
211
- assetKeyArray[keyIndex] = key.charCodeAt(keyIndex);
212
- keyIndex++;
208
+
209
+ if (!this._assetBufferCache) {
210
+ this._assetBufferCache = {};
211
+ }
212
+
213
+ let finishedCount = 0;
214
+ const perAssetBuffers = new Array(totalCount);
215
+
216
+ for (let idx = 0; idx < totalCount; idx++) {
217
+ const key = assetKeys[idx];
218
+
219
+ // If we already have a packed buffer for this key, reuse it
220
+ if (this._assetBufferCache[key]) {
221
+ perAssetBuffers[idx] = this._assetBufferCache[key];
222
+ finishedCount += 1;
223
+ if (finishedCount === totalCount) {
224
+ this.assetBundle = Buffer.concat(perAssetBuffers);
225
+ resolve(this.assetBundle);
213
226
  }
214
-
215
- const encodedLength = (buf.length + assetKeyLength).toString(36);
216
-
217
- const assetTypeMap = {
218
- 'image': 1,
219
- 'audio': 2,
220
- 'font': 3
221
- };
222
-
223
- const assetType = assetTypeMap[allAssets[key].info.type];
224
-
227
+ continue;
228
+ }
229
+
230
+ gameAssets[key].getData().then(buf => {
231
+ const assetKeyLength = 32;
225
232
  const encodedMaxLength = 10;
226
- let encodedLengthString = '';
227
- for (let i = 0; i < (encodedMaxLength - encodedLength.length); i++) {
228
- encodedLengthString += '0';
233
+
234
+ const headerLength = 2 + encodedMaxLength + assetKeyLength;
235
+ const assetBuf = Buffer.alloc(headerLength + buf.length);
236
+
237
+ assetBuf[0] = ASSET_TYPE;
238
+ assetBuf[1] = assetTypeMap[gameAssets[key].info.type];
239
+
240
+ // Encode the length (data + key length) as base-36, left-padded
241
+ const encodedLength = (buf.length + assetKeyLength).toString(36);
242
+ const padLen = encodedMaxLength - encodedLength.length;
243
+ for (let i = 0; i < padLen; i++) {
244
+ assetBuf[2 + i] = 48; // '0' charCode
229
245
  }
230
- for (let j = encodedLength.length; j < encodedMaxLength; j++) {
231
- encodedLengthString += encodedLength.charAt(j - encodedLength.length);
246
+ for (let i = 0; i < encodedLength.length; i++) {
247
+ assetBuf[2 + padLen + i] = encodedLength.charCodeAt(i);
232
248
  }
233
- const encodedLengthArray = new Array(encodedMaxLength);
234
- for (let i = 0; i < encodedMaxLength; i++) {
235
- encodedLengthArray[i] = encodedLength.charCodeAt(i);
249
+
250
+ // Write asset key (up to 32 chars, rest stays 0)
251
+ const keyWriteLen = Math.min(assetKeyLength, key.length);
252
+ for (let i = 0; i < keyWriteLen; i++) {
253
+ assetBuf[2 + encodedMaxLength + i] = key.charCodeAt(i);
236
254
  }
237
-
238
- this.assets[key] = [ASSET_TYPE, assetType, ...encodedLengthArray, ...assetKeyArray, ...buf];
239
- assetBundleSize += this.assets[key].length;
240
- finishedCount += 1;
241
255
 
242
- if (finishedCount == totalCount) {
243
- const newAssetBundle = new Array(assetBundleSize);
244
- let index = 0;
245
- for (const key in this.assets) {
246
- for (let y = 0; y < this.assets[key].length; y++) {
247
- newAssetBundle[index++] = this.assets[key][y];
248
- }
256
+ // Copy the raw asset data
257
+ if (Buffer.isBuffer(buf)) {
258
+ buf.copy(assetBuf, headerLength);
259
+ } else {
260
+ for (let i = 0; i < buf.length; i++) {
261
+ assetBuf[headerLength + i] = buf[i];
249
262
  }
250
- this.assetBundle = newAssetBundle;
251
- resolve(newAssetBundle);
263
+ }
264
+
265
+ this._assetBufferCache[key] = assetBuf;
266
+ perAssetBuffers[idx] = assetBuf;
267
+ finishedCount += 1;
268
+
269
+ if (finishedCount === totalCount) {
270
+ this.assetBundle = Buffer.concat(perAssetBuffers);
271
+ resolve(this.assetBundle);
252
272
  }
253
273
  }).catch(err => {
254
274
  console.error('Unable to get asset data for key ' + key);