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 +1 -1
- package/src/Asset.js +17 -21
- package/src/BaseNode.js +1 -1
- package/src/Game.js +1 -1
- package/src/Squisher.js +77 -53
package/package.json
CHANGED
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 (
|
|
61
|
+
if (fs.existsSync(`${options[i]}/config.json`)) {
|
|
66
62
|
console.log(`Using config at ${options[i]}`);
|
|
67
|
-
_config = JSON.parse(
|
|
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 =
|
|
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
|
-
|
|
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
|
|
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 (!
|
|
128
|
-
|
|
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
|
-
|
|
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,
|
|
163
|
+
doDownload(assetId, assetPath) {
|
|
168
164
|
return new Promise((resolve, reject) => {
|
|
169
165
|
const fileHash = this.getHash(assetId);
|
|
170
|
-
const filePath = `${
|
|
166
|
+
const filePath = `${assetPath}/${fileHash}`;
|
|
171
167
|
|
|
172
|
-
const writeStream =
|
|
173
|
-
const getModule =
|
|
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
package/src/Game.js
CHANGED
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.
|
|
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.
|
|
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
|
-
|
|
101
|
-
|
|
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
|
|
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
|
|
200
|
-
|
|
201
|
-
for (
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
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
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
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
|
-
|
|
223
|
-
|
|
224
|
-
|
|
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
|
|
227
|
-
|
|
246
|
+
for (let i = 0; i < encodedLength.length; i++) {
|
|
247
|
+
assetBuf[2 + padLen + i] = encodedLength.charCodeAt(i);
|
|
228
248
|
}
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
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
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
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
|
-
|
|
248
|
-
|
|
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
|
|