squishjs 0.7.55 → 0.7.56

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/index.js CHANGED
@@ -10,6 +10,7 @@ const viewUtils = require('./src/util/views');
10
10
  const { terrainGenerator } = require('./src/terrain');
11
11
  const subtypes = require('./src/subtypes');
12
12
  const Squisher = require('./src/Squisher');
13
+ const Asset = require('./src/Asset');
13
14
  const GeometryUtils = require('./src/util/geometry');
14
15
 
15
16
  module.exports = {
@@ -26,5 +27,6 @@ module.exports = {
26
27
  ShapeUtils: shapeUtils,
27
28
  ViewUtils: viewUtils,
28
29
  TerrainGenerator: terrainGenerator,
29
- GeometryUtils
30
+ GeometryUtils,
31
+ Asset
30
32
  };
package/metest.js ADDED
@@ -0,0 +1,26 @@
1
+ const { GameNode, squish, unsquish } = require('./');
2
+
3
+ const ting = new GameNode.Shape({
4
+ coordinates2d: [
5
+ [0, 0],
6
+ [0, 0],
7
+ [0, 0],
8
+ [0, 0]
9
+ ],
10
+ shapeType: 1
11
+ });
12
+
13
+ ting.node.id = 529025047768;
14
+ ting.id = 529025047768;
15
+
16
+ const squished = squish(ting);
17
+
18
+ const unsquished = unsquish(squished);
19
+
20
+ if (unsquished.node.id != Number(529025047768)) {
21
+ console.log('fuck man');
22
+ console.log(unsquished.node.id);
23
+ console.log(Number(ting.node.id));
24
+ } else {
25
+ console.log('um what');
26
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "squishjs",
3
- "version": "0.7.55",
3
+ "version": "0.7.56",
4
4
  "description": "squish & unsquish stuff",
5
5
  "scripts": {
6
6
  "test": "node testRunner.js"
package/src/Asset.js ADDED
@@ -0,0 +1,183 @@
1
+ class Asset {
2
+ constructor(info, data = null) {
3
+ this.info = info;
4
+ if (data) {
5
+ this.data = data;
6
+ }
7
+
8
+
9
+ console.log('ayyyy lmao doing this sdf')
10
+ this.https = require('https');
11
+ this.http = require('http');
12
+ this.fs = require('fs');
13
+ this.crypto = require('crypto');
14
+ this.path = require('path');
15
+ this.process = require('process');
16
+ }
17
+
18
+ getConfigValue(key, _default = undefined) {
19
+ const config = this.getConfig();
20
+
21
+ let envValue = process.env[key] && `${process.env[key]}`;
22
+ if (envValue !== undefined) {
23
+ if (envValue === 'true') {
24
+ envValue = true;
25
+ } else if (envValue === 'false') {
26
+ envValue = false;
27
+ }
28
+ console.log(`Using environment value: ${envValue} for key: ${key}`);
29
+ return envValue;
30
+ }
31
+ if (config[key] === undefined && _default === undefined) {
32
+ throw new Error(`No value for ${key} found in config`);
33
+ } else if (config[key] === undefined && _default !== undefined) {
34
+ return _default;
35
+ }
36
+ console.log(`Found value ${config[key]} in config`);
37
+ return config[key];
38
+ };
39
+
40
+ getConfig() {
41
+
42
+ if (this.cachedConfig) {
43
+ return this.cachedConfig;
44
+ }
45
+
46
+ const options = [process.cwd(), require.main.filename, process.mainModule.filename, __dirname]
47
+ let _config = {};
48
+
49
+ for (let i = 0; i < options.length; i++) {
50
+ if (this.fs.existsSync(`${options[i]}/config.json`)) {
51
+ console.log(`Using config at ${options[i]}`);
52
+ _config = JSON.parse(this.fs.readFileSync(`${options[i]}/config.json`));
53
+ break;
54
+ }
55
+ }
56
+
57
+ this.cachedConfig = _config;
58
+
59
+ return _config;
60
+ }
61
+
62
+
63
+ getHash(str) {
64
+ const shasum = this.crypto.createHash('sha1');
65
+ shasum.update(str);
66
+ return shasum.digest('hex');
67
+ };
68
+
69
+ getFileLocation() {
70
+
71
+ const HG_ASSET_PATH = this.getConfigValue('HG_ASSET_PATH', `${process.cwd()}/.asset_cache`);
72
+
73
+ const fileHash = this.getHash(this.info.id);
74
+ return `${HG_ASSET_PATH}/${fileHash}`;
75
+ }
76
+
77
+ existsLocallySync() {
78
+ const fileLocation = this.getFileLocation(this.info.id);
79
+ return fs.existsSync(fileLocation);
80
+ }
81
+
82
+ existsLocally() {
83
+ return new Promise((resolve, reject) => {
84
+ if (this.data) {
85
+ resolve(true);
86
+ }
87
+ const fileLocation = this.getFileLocation(this.info.id);
88
+ this.fs.exists(fileLocation, (exists) => {
89
+ resolve(exists && fileLocation);
90
+ });
91
+ });
92
+ }
93
+
94
+ async downloadSync(force) {
95
+
96
+ const HG_ASSET_PATH = this.getConfigValue('HG_ASSET_PATH', `${process.cwd()}/.asset_cache`);
97
+
98
+ if (!fs.existsSync(HG_ASSET_PATH)) {
99
+ fs.mkdirSync(HG_ASSET_PATH);
100
+ }
101
+ const fileLocationExists = this.existsLocallySync();
102
+ const fileLocation = this.getFileLocation(this.info.id);
103
+
104
+ if (fileLocationExists && !force) {
105
+ this.initialized = true;
106
+ return fileLocation;
107
+ } else {
108
+ const fileLocation2 = await downloadFileSync(this.info.id, HG_ASSET_PATH);
109
+ this.initialized = true;
110
+ return fileLocation;
111
+ }
112
+ }
113
+
114
+ download(force) {
115
+
116
+ const HG_ASSET_PATH = this.getConfigValue('HG_ASSET_PATH', `${process.cwd()}/.asset_cache`);
117
+
118
+ if (!this.fs.existsSync(HG_ASSET_PATH)) {
119
+ this.fs.mkdirSync(HG_ASSET_PATH);
120
+ }
121
+ return new Promise((resolve, reject) => {
122
+ this.existsLocally().then(fileLocation => {
123
+ if (fileLocation && !force) {
124
+ this.initialized = true;
125
+ resolve(fileLocation);
126
+ } else {
127
+ this.doDownload(this.info.id, HG_ASSET_PATH).then((fileLocation) => {
128
+ this.initialized = true;
129
+ resolve(fileLocation);
130
+ });
131
+ }
132
+ });
133
+ });
134
+ }
135
+
136
+ getData() {
137
+ return new Promise((resolve, reject) => {
138
+ if (this.data) {
139
+ resolve(this.data);
140
+ } else {
141
+ this.download().then(fileLocation => {
142
+ this.fs.readFile(fileLocation, (err, buf) => {
143
+ if (err) {
144
+ reject(err);
145
+ } else {
146
+ resolve(buf);
147
+ }
148
+ });
149
+ });
150
+ }
151
+ });
152
+ }
153
+
154
+ doDownload(assetId, path) {
155
+ return new Promise((resolve, reject) => {
156
+ const fileHash = this.getHash(assetId);
157
+ const filePath = `${path}/${fileHash}`;
158
+
159
+ const writeStream = this.fs.createWriteStream(filePath);
160
+ const getModule = this.https;
161
+
162
+ writeStream.on('close', () => {
163
+ resolve(filePath);
164
+ });
165
+
166
+ let data = '';
167
+ getModule.get(`https://assets.homegames.io/${assetId}`, (res) => {
168
+ res.on('data', (chunk) => {
169
+ data += chunk;
170
+ writeStream.write(chunk);
171
+ });
172
+ res.on('end', () => {
173
+ writeStream.end();
174
+ });
175
+ }).on('error', error => {
176
+ reject(error);
177
+ });
178
+ });
179
+ }
180
+
181
+ }
182
+
183
+ module.exports = Asset;
@@ -35,7 +35,7 @@ const squishId = {
35
35
  // build up from right to left then parse
36
36
  let str = '';
37
37
  for (let i = arr.length - 1; i >= 0; i--) {
38
- const val = arr[i];
38
+ const val = arr[i] > 9 ? arr[i] : '0' + arr[i];
39
39
  str = val + str
40
40
  }
41
41
 
@@ -46,4 +46,4 @@ const squishId = {
46
46
  module.exports = {
47
47
  ID_SUBTYPE,
48
48
  squishId
49
- };
49
+ };