squishjs 1.3.6 → 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.6",
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
  };
@@ -95,10 +91,10 @@ class Asset {
95
91
  existsLocally() {
96
92
  return new Promise((resolve, reject) => {
97
93
  if (this.data) {
98
- resolve(true);
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
  });
@@ -118,14 +114,14 @@ class Asset {
118
114
  } else {
119
115
  const fileLocation2 = await downloadFileSync(this.info.id, HG_ASSET_PATH);
120
116
  this.initialized = true;
121
- return fileLocation;
117
+ return fileLocation2;
122
118
  }
123
119
  }
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 = this.https;
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/BaseNode.js CHANGED
@@ -104,7 +104,7 @@ class BaseNode {
104
104
  this.node.fill = params.fill;
105
105
  }
106
106
  if (params.coordinates2d) {
107
- this.node.coordinates2d = coordinates2d;
107
+ this.node.coordinates2d = params.coordinates2d;
108
108
  }
109
109
  }
110
110
  this.node.onStateChange();
package/src/Game.js CHANGED
@@ -11,7 +11,7 @@ class Game {
11
11
  }
12
12
 
13
13
  removeStateListener(listener) {
14
- this.listeners.remove(listener);
14
+ this.listeners.delete(listener);
15
15
  }
16
16
 
17
17
  findNode(id) {
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;
@@ -42,7 +42,7 @@ class Squisher {
42
42
  }
43
43
 
44
44
  removeListener(listener) {
45
- this.listeners.remove(listener);
45
+ this.listeners.delete(listener);
46
46
  }
47
47
 
48
48
  unsquish(node) {
@@ -97,9 +97,11 @@ class Squisher {
97
97
  }
98
98
 
99
99
  handleNewAsset(key, asset) {
100
- return new Promise((resolve, reject) => {
101
- this.initialize().then(resolve);
102
- });
100
+ // Invalidate just this key so initialize() re-fetches only its data
101
+ if (this._assetBufferCache) {
102
+ delete this._assetBufferCache[key];
103
+ }
104
+ return this.initialize();
103
105
  }
104
106
 
105
107
  squishHelper(node, squishedNodes, scale = {x: 1, y: 1}, playerMap = {}, playerIdFilter = new Set()) {
@@ -171,9 +173,6 @@ class Squisher {
171
173
 
172
174
  initialize() {
173
175
  return new Promise((resolve, reject) => {
174
-
175
-
176
- const assets = Object.assign({}, this.assets || {});
177
176
 
178
177
  const gameMetadata = this.game.constructor.metadata && this.game.constructor.metadata();
179
178
 
@@ -192,64 +191,89 @@ class Squisher {
192
191
  Object.assign(gameAssets, this.game.getAssets());
193
192
  }
194
193
 
195
- const allAssets = Object.assign(assets, gameAssets);
194
+ const assetTypeMap = {
195
+ 'image': 1,
196
+ 'audio': 2,
197
+ 'font': 3
198
+ };
199
+
200
+ const assetKeys = Object.keys(gameAssets);
201
+ const totalCount = assetKeys.length;
202
+
203
+ if (totalCount === 0) {
204
+ this.assetBundle = Buffer.alloc(0);
205
+ resolve(this.assetBundle);
206
+ return;
207
+ }
208
+
209
+ if (!this._assetBufferCache) {
210
+ this._assetBufferCache = {};
211
+ }
196
212
 
197
- let assetBundleSize = 0;
198
213
  let finishedCount = 0;
199
- const totalCount = Object.keys(allAssets).length;
200
-
201
- for (const key in allAssets) {
202
- allAssets[key].getData().then(buf => {
203
- const assetKeyLength = 32;
204
- let keyIndex = 0;
205
- const assetKeyArray = new Array(32);
206
- while (keyIndex < assetKeyLength && keyIndex < key.length) {
207
- assetKeyArray[keyIndex] = key.charCodeAt(keyIndex);
208
- keyIndex++;
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);
209
226
  }
210
-
211
- const encodedLength = (buf.length + assetKeyLength).toString(36);
212
-
213
- const assetTypeMap = {
214
- 'image': 1,
215
- 'audio': 2,
216
- 'font': 3
217
- };
218
-
219
- const assetType = assetTypeMap[allAssets[key].info.type];
220
-
227
+ continue;
228
+ }
229
+
230
+ gameAssets[key].getData().then(buf => {
231
+ const assetKeyLength = 32;
221
232
  const encodedMaxLength = 10;
222
- let encodedLengthString = '';
223
- for (let i = 0; i < (encodedMaxLength - encodedLength.length); i++) {
224
- 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
225
245
  }
226
- for (let j = encodedLength.length; j < encodedMaxLength; j++) {
227
- encodedLengthString += encodedLength.charAt(j - encodedLength.length);
246
+ for (let i = 0; i < encodedLength.length; i++) {
247
+ assetBuf[2 + padLen + i] = encodedLength.charCodeAt(i);
228
248
  }
229
- const encodedLengthArray = new Array(encodedMaxLength);
230
- for (let i = 0; i < encodedMaxLength; i++) {
231
- 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);
232
254
  }
233
-
234
- this.assets[key] = [ASSET_TYPE, assetType, ...encodedLengthArray, ...assetKeyArray, ...buf];
235
- assetBundleSize += this.assets[key].length;
236
- finishedCount += 1;
237
255
 
238
- if (finishedCount == totalCount) {
239
- const newAssetBundle = new Array(assetBundleSize);
240
- for (let index = 0; index < assetBundleSize; index++) {
241
- for (const key in this.assets) {
242
- for (let y = 0; y < this.assets[key].length; y++) {
243
- newAssetBundle[index++] = this.assets[key][y];
244
- }
245
- }
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];
246
262
  }
247
- this.assetBundle = newAssetBundle;
248
- 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);
249
272
  }
250
273
  }).catch(err => {
251
274
  console.error('Unable to get asset data for key ' + key);
252
275
  console.error(err);
276
+ reject(err);
253
277
  });
254
278
  }
255
279