mcbe-ipc 3.0.0 → 3.0.2

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/dist/ipc.js CHANGED
@@ -2,7 +2,7 @@
2
2
  * @license
3
3
  * MIT License
4
4
  *
5
- * Copyright (c) 2024 OmniacDev
5
+ * Copyright (c) 2025 OmniacDev
6
6
  *
7
7
  * Permission is hereby granted, free of charge, to any person obtaining a copy
8
8
  * of this software and associated documentation files (the "Software"), to deal
@@ -23,12 +23,18 @@
23
23
  * SOFTWARE.
24
24
  */
25
25
  import { ScriptEventSource, system, world } from '@minecraft/server';
26
- export var SERDE;
27
- (function (SERDE) {
28
- class ByteArray {
29
- get _end() {
26
+ export var PROTO;
27
+ (function (PROTO) {
28
+ class ByteQueue {
29
+ get end() {
30
30
  return this._length + this._offset;
31
31
  }
32
+ get front() {
33
+ return this._offset;
34
+ }
35
+ get data_view() {
36
+ return this._data_view;
37
+ }
32
38
  constructor(size = 256) {
33
39
  this._buffer = new Uint8Array(size);
34
40
  this._data_view = new DataView(this._buffer.buffer);
@@ -36,8 +42,8 @@ export var SERDE;
36
42
  this._offset = 0;
37
43
  }
38
44
  write(...values) {
39
- this._ensure_capacity(this._end + values.length);
40
- this._buffer.set(values, this._end);
45
+ this.ensure_capacity(values.length);
46
+ this._buffer.set(values, this.end);
41
47
  this._length += values.length;
42
48
  }
43
49
  read(amount = 1) {
@@ -46,248 +52,241 @@ export var SERDE;
46
52
  const values = this._buffer.subarray(this._offset, this._offset + max_amount);
47
53
  this._length -= max_amount;
48
54
  this._offset += max_amount;
49
- return Array.from(values);
55
+ return globalThis.Array.from(values);
50
56
  }
51
57
  return [];
52
58
  }
53
- write_uint8(value) {
54
- this._ensure_capacity(this._end + 1);
55
- this._data_view.setUint8(this._end, value);
56
- this._length += 1;
57
- }
58
- read_uint8() {
59
- if (this._length >= 1) {
60
- const value = this._data_view.getUint8(this._offset);
61
- this._length -= 1;
62
- this._offset += 1;
63
- return value;
64
- }
65
- return undefined;
66
- }
67
- write_uint16(value) {
68
- this._ensure_capacity(this._end + 2);
69
- this._data_view.setUint16(this._end, value);
70
- this._length += 2;
71
- }
72
- read_uint16() {
73
- if (this._length >= 2) {
74
- const value = this._data_view.getUint16(this._offset);
75
- this._length -= 2;
76
- this._offset += 2;
77
- return value;
78
- }
79
- return undefined;
80
- }
81
- write_uint32(value) {
82
- this._ensure_capacity(this._end + 4);
83
- this._data_view.setUint32(this._end, value);
84
- this._length += 4;
85
- }
86
- read_uint32() {
87
- if (this._length >= 4) {
88
- const value = this._data_view.getUint32(this._offset);
89
- this._length -= 4;
90
- this._offset += 4;
91
- return value;
92
- }
93
- return undefined;
94
- }
95
- write_int8(value) {
96
- this._ensure_capacity(this._end + 1);
97
- this._data_view.setInt8(this._end, value);
98
- this._length += 1;
99
- }
100
- read_int8() {
101
- if (this._length >= 1) {
102
- const value = this._data_view.getInt8(this._offset);
103
- this._length -= 1;
104
- this._offset += 1;
105
- return value;
106
- }
107
- return undefined;
108
- }
109
- write_int16(value) {
110
- this._ensure_capacity(this._end + 2);
111
- this._data_view.setInt16(this._end, value);
112
- this._length += 2;
113
- }
114
- read_int16() {
115
- if (this._length >= 2) {
116
- const value = this._data_view.getInt16(this._offset);
117
- this._length -= 2;
118
- this._offset += 2;
119
- return value;
120
- }
121
- return undefined;
122
- }
123
- write_int32(value) {
124
- this._ensure_capacity(this._end + 4);
125
- this._data_view.setInt32(this._end, value);
126
- this._length += 4;
127
- }
128
- read_int32() {
129
- if (this._length >= 4) {
130
- const value = this._data_view.getInt32(this._offset);
131
- this._length -= 4;
132
- this._offset += 4;
133
- return value;
134
- }
135
- return undefined;
136
- }
137
- write_f32(value) {
138
- this._ensure_capacity(this._end + 4);
139
- this._data_view.setFloat32(this._end, value);
140
- this._length += 4;
141
- }
142
- read_f32() {
143
- if (this._length >= 4) {
144
- const value = this._data_view.getFloat32(this._offset);
145
- this._length -= 4;
146
- this._offset += 4;
147
- return value;
148
- }
149
- return undefined;
150
- }
151
- write_f64(value) {
152
- this._ensure_capacity(this._end + 8);
153
- this._data_view.setFloat64(this._end, value);
154
- this._length += 8;
155
- }
156
- read_f64() {
157
- if (this._length >= 8) {
158
- const value = this._data_view.getFloat64(this._offset);
159
- this._length -= 8;
160
- this._offset += 8;
161
- return value;
162
- }
163
- return undefined;
164
- }
165
- _ensure_capacity(size) {
166
- if (size > this._buffer.length) {
167
- const larger_buffer = new Uint8Array(size * 2);
168
- larger_buffer.set(this._buffer.subarray(this._offset, this._end), 0);
59
+ ensure_capacity(size) {
60
+ if (this.end + size > this._buffer.length) {
61
+ const larger_buffer = new Uint8Array((this.end + size) * 2);
62
+ larger_buffer.set(this._buffer.subarray(this._offset, this.end), 0);
169
63
  this._buffer = larger_buffer;
170
64
  this._offset = 0;
171
65
  this._data_view = new DataView(this._buffer.buffer);
172
66
  }
173
67
  }
174
68
  static from_uint8array(array) {
175
- const byte_array = new ByteArray();
176
- byte_array._buffer = array;
177
- byte_array._length = array.length;
178
- byte_array._offset = 0;
179
- byte_array._data_view = new DataView(array.buffer);
180
- return byte_array;
69
+ const byte_queue = new ByteQueue();
70
+ byte_queue._buffer = array;
71
+ byte_queue._length = array.length;
72
+ byte_queue._offset = 0;
73
+ byte_queue._data_view = new DataView(array.buffer);
74
+ return byte_queue;
181
75
  }
182
76
  to_uint8array() {
183
- return this._buffer.subarray(this._offset, this._end);
184
- }
185
- }
186
- SERDE.ByteArray = ByteArray;
187
- function* serialize(byte_array, max_size = Infinity) {
188
- const uint8array = byte_array.to_uint8array();
189
- const result = [];
190
- let acc_str = '';
191
- let acc_size = 0;
192
- for (let i = 0; i < uint8array.length; i++) {
193
- const char_code = uint8array[i] | (uint8array[++i] << 8);
194
- const utf16_size = char_code <= 0x7f ? 1 : char_code <= 0x7ff ? 2 : char_code <= 0xffff ? 3 : 4;
195
- const char_size = char_code > 0xff ? utf16_size : 3;
196
- if (acc_size + char_size > max_size) {
197
- result.push(acc_str);
198
- acc_str = '';
199
- acc_size = 0;
200
- }
201
- if (char_code > 0xff) {
202
- acc_str += String.fromCharCode(char_code);
203
- acc_size += utf16_size;
204
- }
205
- else {
206
- acc_str += `?${char_code.toString(16).padStart(2, '0')}`;
207
- acc_size += 3;
77
+ return this._buffer.subarray(this._offset, this.end);
78
+ }
79
+ }
80
+ PROTO.ByteQueue = ByteQueue;
81
+ let MIPS;
82
+ (function (MIPS) {
83
+ function* serialize(byte_queue) {
84
+ const uint8array = byte_queue.to_uint8array();
85
+ let str = '(0x';
86
+ for (let i = 0; i < uint8array.length; i++) {
87
+ const hex = uint8array[i].toString(16).padStart(2, '0').toUpperCase();
88
+ str += hex;
89
+ yield;
208
90
  }
209
- yield;
91
+ str += ')';
92
+ return str;
210
93
  }
211
- result.push(acc_str);
212
- return result;
213
- }
214
- SERDE.serialize = serialize;
215
- function* deserialize(strings) {
216
- const result = [];
217
- for (let i = 0; i < strings.length; i++) {
218
- const str = strings[i];
219
- for (let j = 0; j < str.length; j++) {
220
- const char_code = str.charCodeAt(j);
221
- if (char_code === '?'.charCodeAt(0) && j + 2 < str.length) {
222
- const hex = str.slice(j + 1, j + 3);
223
- const hex_code = parseInt(hex, 16);
224
- result.push(hex_code & 0xff);
225
- result.push(hex_code >> 8);
226
- j += 2;
227
- }
228
- else {
229
- result.push(char_code & 0xff);
230
- result.push(char_code >> 8);
94
+ MIPS.serialize = serialize;
95
+ function* deserialize(str) {
96
+ if (str.startsWith('(0x') && str.endsWith(')')) {
97
+ const result = [];
98
+ const hex_str = str.slice(3, str.length - 1);
99
+ for (let i = 0; i < hex_str.length; i++) {
100
+ const hex = hex_str[i] + hex_str[++i];
101
+ result.push(parseInt(hex, 16));
102
+ yield;
231
103
  }
232
- yield;
104
+ return ByteQueue.from_uint8array(new Uint8Array(result));
233
105
  }
234
- yield;
106
+ return new ByteQueue();
107
+ }
108
+ MIPS.deserialize = deserialize;
109
+ })(MIPS = PROTO.MIPS || (PROTO.MIPS = {}));
110
+ PROTO.Void = {
111
+ *serialize() { },
112
+ *deserialize() { }
113
+ };
114
+ PROTO.Null = {
115
+ *serialize() { },
116
+ *deserialize() {
117
+ return null;
118
+ }
119
+ };
120
+ PROTO.Undefined = {
121
+ *serialize() { },
122
+ *deserialize() {
123
+ return undefined;
235
124
  }
236
- return ByteArray.from_uint8array(new Uint8Array(result));
237
- }
238
- SERDE.deserialize = deserialize;
239
- })(SERDE || (SERDE = {}));
240
- var CRYPTO;
241
- (function (CRYPTO) {
242
- CRYPTO.PRIME = 19893121;
243
- CRYPTO.MOD = 341;
244
- const to_HEX = (n) => n.toString(16).toUpperCase();
245
- const to_NUM = (h) => parseInt(h, 16);
246
- function* mod_exp(base, exp, mod) {
247
- let result = 1;
248
- let b = base % mod;
249
- for (let e = exp; e > 0; e = Math.floor(e / 2)) {
250
- if (e % 2 === 1) {
251
- result = (result * b) % mod;
125
+ };
126
+ PROTO.Int8 = {
127
+ *serialize(value, stream) {
128
+ const length = 1;
129
+ stream.write(...globalThis.Array(length).fill(0));
130
+ stream.data_view.setInt8(stream.end - length, value);
131
+ },
132
+ *deserialize(stream) {
133
+ const value = stream.data_view.getInt8(stream.front);
134
+ stream.read(1);
135
+ return value;
136
+ }
137
+ };
138
+ PROTO.Int16 = {
139
+ *serialize(value, stream) {
140
+ const length = 2;
141
+ stream.write(...globalThis.Array(length).fill(0));
142
+ stream.data_view.setInt16(stream.end - length, value);
143
+ },
144
+ *deserialize(stream) {
145
+ const value = stream.data_view.getInt16(stream.front);
146
+ stream.read(2);
147
+ return value;
148
+ }
149
+ };
150
+ PROTO.Int32 = {
151
+ *serialize(value, stream) {
152
+ const length = 4;
153
+ stream.write(...globalThis.Array(length).fill(0));
154
+ stream.data_view.setInt32(stream.end - length, value);
155
+ },
156
+ *deserialize(stream) {
157
+ const value = stream.data_view.getInt32(stream.front);
158
+ stream.read(4);
159
+ return value;
160
+ }
161
+ };
162
+ PROTO.UInt8 = {
163
+ *serialize(value, stream) {
164
+ const length = 1;
165
+ stream.write(...globalThis.Array(length).fill(0));
166
+ stream.data_view.setUint8(stream.end - length, value);
167
+ },
168
+ *deserialize(stream) {
169
+ const value = stream.data_view.getUint8(stream.front);
170
+ stream.read(1);
171
+ return value;
172
+ }
173
+ };
174
+ PROTO.UInt16 = {
175
+ *serialize(value, stream) {
176
+ const length = 2;
177
+ stream.write(...globalThis.Array(length).fill(0));
178
+ stream.data_view.setUint16(stream.end - length, value);
179
+ },
180
+ *deserialize(stream) {
181
+ const value = stream.data_view.getUint16(stream.front);
182
+ stream.read(2);
183
+ return value;
184
+ }
185
+ };
186
+ PROTO.UInt32 = {
187
+ *serialize(value, stream) {
188
+ const length = 4;
189
+ stream.write(...globalThis.Array(length).fill(0));
190
+ stream.data_view.setUint32(stream.end - length, value);
191
+ },
192
+ *deserialize(stream) {
193
+ const value = stream.data_view.getUint32(stream.front);
194
+ stream.read(4);
195
+ return value;
196
+ }
197
+ };
198
+ PROTO.UVarInt32 = {
199
+ *serialize(value, stream) {
200
+ while (value >= 0x80) {
201
+ stream.write((value & 0x7f) | 0x80);
202
+ value >>= 7;
203
+ yield;
252
204
  }
253
- b = (b * b) % mod;
254
- yield;
255
- }
256
- return result;
257
- }
258
- function make_secret(mod = CRYPTO.MOD) {
259
- return Math.floor(Math.random() * (mod - 1)) + 1;
260
- }
261
- CRYPTO.make_secret = make_secret;
262
- function* make_public(secret, mod = CRYPTO.MOD, prime = CRYPTO.PRIME) {
263
- return to_HEX(yield* mod_exp(mod, secret, prime));
264
- }
265
- CRYPTO.make_public = make_public;
266
- function* make_shared(secret, other, prime = CRYPTO.PRIME) {
267
- return to_HEX(yield* mod_exp(to_NUM(other), secret, prime));
268
- }
269
- CRYPTO.make_shared = make_shared;
270
- function* encrypt(raw, key) {
271
- let encrypted = new Uint8Array(raw.length);
272
- for (let i = 0; i < raw.length; i++) {
273
- encrypted[i] = raw[i] ^ key.charCodeAt(i % key.length);
274
- yield;
275
- }
276
- return encrypted;
277
- }
278
- CRYPTO.encrypt = encrypt;
279
- function* decrypt(encrypted, key) {
280
- let decrypted = new Uint8Array(encrypted.length);
281
- for (let i = 0; i < encrypted.length; i++) {
282
- decrypted[i] = encrypted[i] ^ key.charCodeAt(i % key.length);
283
- yield;
284
- }
285
- return decrypted;
286
- }
287
- CRYPTO.decrypt = decrypt;
288
- })(CRYPTO || (CRYPTO = {}));
289
- export class Proto {
290
- static Object(obj) {
205
+ stream.write(value);
206
+ },
207
+ *deserialize(stream) {
208
+ let value = 0;
209
+ let size = 0;
210
+ let byte;
211
+ do {
212
+ byte = stream.read()[0];
213
+ value |= (byte & 0x7f) << (size * 7);
214
+ size += 1;
215
+ yield;
216
+ } while ((byte & 0x80) !== 0 && size < 10);
217
+ return value;
218
+ }
219
+ };
220
+ PROTO.Float32 = {
221
+ *serialize(value, stream) {
222
+ const length = 4;
223
+ stream.write(...globalThis.Array(length).fill(0));
224
+ stream.data_view.setFloat32(stream.end - length, value);
225
+ },
226
+ *deserialize(stream) {
227
+ const value = stream.data_view.getFloat32(stream.front);
228
+ stream.read(4);
229
+ return value;
230
+ }
231
+ };
232
+ PROTO.Float64 = {
233
+ *serialize(value, stream) {
234
+ const length = 8;
235
+ stream.write(...globalThis.Array(length).fill(0));
236
+ stream.data_view.setFloat64(stream.end - length, value);
237
+ },
238
+ *deserialize(stream) {
239
+ const value = stream.data_view.getFloat64(stream.front);
240
+ stream.read(8);
241
+ return value;
242
+ }
243
+ };
244
+ PROTO.String = {
245
+ *serialize(value, stream) {
246
+ yield* PROTO.UVarInt32.serialize(value.length, stream);
247
+ for (let i = 0; i < value.length; i++) {
248
+ const code = value.charCodeAt(i);
249
+ yield* PROTO.UVarInt32.serialize(code, stream);
250
+ }
251
+ },
252
+ *deserialize(stream) {
253
+ const length = yield* PROTO.UVarInt32.deserialize(stream);
254
+ let value = '';
255
+ for (let i = 0; i < length; i++) {
256
+ const code = yield* PROTO.UVarInt32.deserialize(stream);
257
+ value += globalThis.String.fromCharCode(code);
258
+ }
259
+ return value;
260
+ }
261
+ };
262
+ PROTO.Boolean = {
263
+ *serialize(value, stream) {
264
+ stream.write(value ? 1 : 0);
265
+ },
266
+ *deserialize(stream) {
267
+ const value = stream.read()[0];
268
+ return value === 1;
269
+ }
270
+ };
271
+ PROTO.UInt8Array = {
272
+ *serialize(value, stream) {
273
+ yield* PROTO.UVarInt32.serialize(value.length, stream);
274
+ stream.write(...value);
275
+ },
276
+ *deserialize(stream) {
277
+ const length = yield* PROTO.UVarInt32.deserialize(stream);
278
+ return new Uint8Array(stream.read(length));
279
+ }
280
+ };
281
+ PROTO.Date = {
282
+ *serialize(value, stream) {
283
+ yield* PROTO.Float64.serialize(value.getTime(), stream);
284
+ },
285
+ *deserialize(stream) {
286
+ return new globalThis.Date(yield* PROTO.Float64.deserialize(stream));
287
+ }
288
+ };
289
+ function Object(obj) {
291
290
  return {
292
291
  *serialize(value, stream) {
293
292
  for (const key in obj) {
@@ -303,68 +302,73 @@ export class Proto {
303
302
  }
304
303
  };
305
304
  }
306
- static Array(items) {
305
+ PROTO.Object = Object;
306
+ function Array(value) {
307
307
  return {
308
- *serialize(value, stream) {
309
- yield* Proto.VarInt.serialize(value.length, stream);
310
- for (const item of value) {
311
- yield* items.serialize(item, stream);
308
+ *serialize(array, stream) {
309
+ yield* PROTO.UVarInt32.serialize(array.length, stream);
310
+ for (const item of array) {
311
+ yield* value.serialize(item, stream);
312
312
  }
313
313
  },
314
314
  *deserialize(stream) {
315
315
  const result = [];
316
- const length = yield* Proto.VarInt.deserialize(stream);
316
+ const length = yield* PROTO.UVarInt32.deserialize(stream);
317
317
  for (let i = 0; i < length; i++) {
318
- result[i] = yield* items.deserialize(stream);
318
+ result[i] = yield* value.deserialize(stream);
319
319
  }
320
320
  return result;
321
321
  }
322
322
  };
323
323
  }
324
- static Tuple(...items) {
324
+ PROTO.Array = Array;
325
+ function Tuple(...values) {
325
326
  return {
326
- *serialize(value, stream) {
327
- for (let i = 0; i < items.length; i++) {
328
- yield* items[i].serialize(value[i], stream);
327
+ *serialize(tuple, stream) {
328
+ for (let i = 0; i < values.length; i++) {
329
+ yield* values[i].serialize(tuple[i], stream);
329
330
  }
330
331
  },
331
332
  *deserialize(stream) {
332
333
  const result = [];
333
- for (let i = 0; i < items.length; i++) {
334
- result[i] = yield* items[i].deserialize(stream);
334
+ for (let i = 0; i < values.length; i++) {
335
+ result[i] = yield* values[i].deserialize(stream);
335
336
  }
336
337
  return result;
337
338
  }
338
339
  };
339
340
  }
340
- static Optional(item) {
341
+ PROTO.Tuple = Tuple;
342
+ function Optional(value) {
341
343
  return {
342
- *serialize(value, stream) {
343
- yield* Proto.Boolean.serialize(value !== undefined, stream);
344
- if (value !== undefined) {
345
- yield* item.serialize(value, stream);
344
+ *serialize(optional, stream) {
345
+ yield* PROTO.Boolean.serialize(value !== undefined, stream);
346
+ if (optional !== undefined) {
347
+ yield* value.serialize(optional, stream);
346
348
  }
347
349
  },
348
350
  *deserialize(stream) {
349
- const defined = yield* Proto.Boolean.deserialize(stream);
351
+ const defined = yield* PROTO.Boolean.deserialize(stream);
350
352
  if (defined) {
351
- return yield* item.deserialize(stream);
353
+ return yield* value.deserialize(stream);
352
354
  }
355
+ return undefined;
353
356
  }
354
357
  };
355
358
  }
356
- static Map(key, value) {
359
+ PROTO.Optional = Optional;
360
+ function Map(key, value) {
357
361
  return {
358
362
  *serialize(map, stream) {
359
- yield* Proto.VarInt.serialize(map.size, stream);
363
+ yield* PROTO.UVarInt32.serialize(map.size, stream);
360
364
  for (const [k, v] of map.entries()) {
361
365
  yield* key.serialize(k, stream);
362
366
  yield* value.serialize(v, stream);
363
367
  }
364
368
  },
365
369
  *deserialize(stream) {
366
- const size = yield* Proto.VarInt.deserialize(stream);
367
- const result = new Map();
370
+ const size = yield* PROTO.UVarInt32.deserialize(stream);
371
+ const result = new globalThis.Map();
368
372
  for (let i = 0; i < size; i++) {
369
373
  const k = yield* key.deserialize(stream);
370
374
  const v = yield* value.deserialize(stream);
@@ -374,17 +378,18 @@ export class Proto {
374
378
  }
375
379
  };
376
380
  }
377
- static Set(value) {
381
+ PROTO.Map = Map;
382
+ function Set(value) {
378
383
  return {
379
384
  *serialize(set, stream) {
380
- yield* Proto.VarInt.serialize(set.size, stream);
385
+ yield* PROTO.UVarInt32.serialize(set.size, stream);
381
386
  for (const [_, v] of set.entries()) {
382
387
  yield* value.serialize(v, stream);
383
388
  }
384
389
  },
385
390
  *deserialize(stream) {
386
- const size = yield* Proto.VarInt.deserialize(stream);
387
- const result = new Set();
391
+ const size = yield* PROTO.UVarInt32.deserialize(stream);
392
+ const result = new globalThis.Set();
388
393
  for (let i = 0; i < size; i++) {
389
394
  const v = yield* value.deserialize(stream);
390
395
  result.add(v);
@@ -393,164 +398,80 @@ export class Proto {
393
398
  }
394
399
  };
395
400
  }
396
- }
397
- Proto.Int8 = {
398
- *serialize(value, stream) {
399
- stream.write_int8(value);
400
- },
401
- *deserialize(stream) {
402
- return stream.read_int8();
403
- }
404
- };
405
- Proto.Int16 = {
406
- *serialize(value, stream) {
407
- stream.write_int16(value);
408
- },
409
- *deserialize(stream) {
410
- return stream.read_int16();
411
- }
412
- };
413
- Proto.Int32 = {
414
- *serialize(value, stream) {
415
- stream.write_int32(value);
416
- },
417
- *deserialize(stream) {
418
- return stream.read_int32();
419
- }
420
- };
421
- Proto.UInt8 = {
422
- *serialize(value, stream) {
423
- stream.write_uint8(value);
424
- },
425
- *deserialize(stream) {
426
- return stream.read_uint8();
427
- }
428
- };
429
- Proto.UInt16 = {
430
- *serialize(value, stream) {
431
- stream.write_uint16(value);
432
- },
433
- *deserialize(stream) {
434
- return stream.read_uint16();
435
- }
436
- };
437
- Proto.UInt32 = {
438
- *serialize(value, stream) {
439
- stream.write_uint32(value);
440
- },
441
- *deserialize(stream) {
442
- return stream.read_uint32();
443
- }
444
- };
445
- Proto.Float32 = {
446
- *serialize(value, stream) {
447
- stream.write_f32(value);
448
- },
449
- *deserialize(stream) {
450
- return stream.read_f32();
451
- }
452
- };
453
- Proto.Float64 = {
454
- *serialize(value, stream) {
455
- stream.write_f64(value);
456
- },
457
- *deserialize(stream) {
458
- return stream.read_f64();
459
- }
460
- };
461
- Proto.VarInt = {
462
- *serialize(value, stream) {
463
- while (value >= 0x80) {
464
- stream.write((value & 0x7f) | 0x80);
465
- value >>= 7;
401
+ PROTO.Set = Set;
402
+ PROTO.Endpoint = PROTO.String;
403
+ PROTO.Header = PROTO.Object({
404
+ guid: PROTO.String,
405
+ encoding: PROTO.String,
406
+ index: PROTO.UVarInt32,
407
+ final: PROTO.Boolean
408
+ });
409
+ })(PROTO || (PROTO = {}));
410
+ export var NET;
411
+ (function (NET) {
412
+ const FRAG_MAX = 2048;
413
+ const ENCODING = 'mcbe-ipc:v3';
414
+ const ENDPOINTS = new Map();
415
+ function* serialize(byte_queue, max_size = Infinity) {
416
+ const uint8array = byte_queue.to_uint8array();
417
+ const result = [];
418
+ let acc_str = '';
419
+ let acc_size = 0;
420
+ for (let i = 0; i < uint8array.length; i++) {
421
+ const char_code = uint8array[i] | (uint8array[++i] << 8);
422
+ const utf16_size = char_code <= 0x7f ? 1 : char_code <= 0x7ff ? 2 : char_code <= 0xffff ? 3 : 4;
423
+ const char_size = char_code > 0xff ? utf16_size : 3;
424
+ if (acc_size + char_size > max_size) {
425
+ result.push(acc_str);
426
+ acc_str = '';
427
+ acc_size = 0;
428
+ }
429
+ if (char_code > 0xff) {
430
+ acc_str += String.fromCharCode(char_code);
431
+ acc_size += utf16_size;
432
+ }
433
+ else {
434
+ acc_str += char_code.toString(16).padStart(2, '0').toUpperCase();
435
+ acc_size += 2;
436
+ }
466
437
  yield;
467
438
  }
468
- stream.write(value);
469
- },
470
- *deserialize(stream) {
471
- let value = 0;
472
- let shift = 0;
473
- let byte;
474
- do {
475
- byte = stream.read()[0];
476
- value |= (byte & 0x7f) << shift;
477
- shift += 7;
478
- yield;
479
- } while (byte & 0x80);
480
- return value;
481
- }
482
- };
483
- Proto.String = {
484
- *serialize(value, stream) {
485
- yield* Proto.VarInt.serialize(value.length, stream);
486
- for (let i = 0; i < value.length; i++) {
487
- const code = value.charCodeAt(i);
488
- yield* Proto.VarInt.serialize(code, stream);
489
- }
490
- },
491
- *deserialize(stream) {
492
- const length = yield* Proto.VarInt.deserialize(stream);
493
- let value = '';
494
- for (let i = 0; i < length; i++) {
495
- const code = yield* Proto.VarInt.deserialize(stream);
496
- value += String.fromCharCode(code);
497
- }
498
- return value;
499
- }
500
- };
501
- Proto.Boolean = {
502
- *serialize(value, stream) {
503
- stream.write(value ? 1 : 0);
504
- },
505
- *deserialize(stream) {
506
- const value = stream.read()[0];
507
- return value === 1;
439
+ result.push(acc_str);
440
+ return result;
508
441
  }
509
- };
510
- Proto.UInt8Array = {
511
- *serialize(value, stream) {
512
- yield* Proto.VarInt.serialize(value.length, stream);
513
- for (const item of value) {
514
- stream.write_uint8(item);
442
+ NET.serialize = serialize;
443
+ function* deserialize(strings) {
444
+ const result = [];
445
+ for (let i = 0; i < strings.length; i++) {
446
+ const str = strings[i];
447
+ for (let j = 0; j < str.length; j++) {
448
+ const char_code = str.charCodeAt(j);
449
+ if (char_code <= 0xff) {
450
+ const hex = str[j] + str[++j];
451
+ const hex_code = parseInt(hex, 16);
452
+ result.push(hex_code & 0xff);
453
+ result.push(hex_code >> 8);
454
+ }
455
+ else {
456
+ result.push(char_code & 0xff);
457
+ result.push(char_code >> 8);
458
+ }
459
+ yield;
460
+ }
515
461
  yield;
516
462
  }
517
- },
518
- *deserialize(stream) {
519
- const length = yield* Proto.VarInt.deserialize(stream);
520
- const result = new Uint8Array(length);
521
- for (let i = 0; i < length; i++) {
522
- result[i] = stream.read_uint8();
523
- }
524
- return result;
525
- }
526
- };
527
- Proto.Date = {
528
- *serialize(value, stream) {
529
- yield* Proto.VarInt.serialize(value.getTime(), stream);
530
- },
531
- *deserialize(stream) {
532
- return new Date(yield* Proto.VarInt.deserialize(stream));
463
+ return PROTO.ByteQueue.from_uint8array(new Uint8Array(result));
533
464
  }
534
- };
535
- export var NET;
536
- (function (NET) {
537
- var ByteArray = SERDE.ByteArray;
538
- const FRAG_MAX = 2048;
539
- const Header = Proto.Object({
540
- guid: Proto.String,
541
- index: Proto.VarInt,
542
- final: Proto.Boolean
543
- });
544
- const endpoint_map = new Map();
465
+ NET.deserialize = deserialize;
545
466
  system.afterEvents.scriptEventReceive.subscribe(event => {
546
467
  system.runJob((function* () {
547
468
  const [serialized_endpoint, serialized_header] = event.id.split(':');
548
- const endpoint_stream = yield* SERDE.deserialize([serialized_endpoint]);
549
- const endpoint = yield* Proto.String.deserialize(endpoint_stream);
550
- const listeners = endpoint_map.get(endpoint);
469
+ const endpoint_stream = yield* PROTO.MIPS.deserialize(serialized_endpoint);
470
+ const endpoint = yield* PROTO.Endpoint.deserialize(endpoint_stream);
471
+ const listeners = ENDPOINTS.get(endpoint);
551
472
  if (event.sourceType === ScriptEventSource.Server && listeners) {
552
- const header_stream = yield* SERDE.deserialize([serialized_header]);
553
- const header = yield* Header.deserialize(header_stream);
473
+ const header_stream = yield* PROTO.MIPS.deserialize(serialized_header);
474
+ const header = yield* PROTO.Header.deserialize(header_stream);
554
475
  for (let i = 0; i < listeners.length; i++) {
555
476
  yield* listeners[i](header, event.message);
556
477
  }
@@ -558,10 +479,10 @@ export var NET;
558
479
  })());
559
480
  });
560
481
  function create_listener(endpoint, listener) {
561
- let listeners = endpoint_map.get(endpoint);
482
+ let listeners = ENDPOINTS.get(endpoint);
562
483
  if (!listeners) {
563
484
  listeners = new Array();
564
- endpoint_map.set(endpoint, listeners);
485
+ ENDPOINTS.set(endpoint, listeners);
565
486
  }
566
487
  listeners.push(listener);
567
488
  return () => {
@@ -569,7 +490,7 @@ export var NET;
569
490
  if (idx !== -1)
570
491
  listeners.splice(idx, 1);
571
492
  if (listeners.length === 0) {
572
- endpoint_map.delete(endpoint);
493
+ ENDPOINTS.delete(endpoint);
573
494
  }
574
495
  };
575
496
  }
@@ -582,23 +503,23 @@ export var NET;
582
503
  }
583
504
  function* emit(endpoint, serializer, value) {
584
505
  const guid = generate_id();
585
- const endpoint_stream = new ByteArray();
586
- yield* Proto.String.serialize(endpoint, endpoint_stream);
587
- const [serialized_endpoint] = yield* SERDE.serialize(endpoint_stream);
506
+ const endpoint_stream = new PROTO.ByteQueue();
507
+ yield* PROTO.Endpoint.serialize(endpoint, endpoint_stream);
508
+ const serialized_endpoint = yield* PROTO.MIPS.serialize(endpoint_stream);
588
509
  const RUN = function* (header, serialized_packet) {
589
- const header_stream = new SERDE.ByteArray();
590
- yield* Header.serialize(header, header_stream);
591
- const [serialized_header] = yield* SERDE.serialize(header_stream);
510
+ const header_stream = new PROTO.ByteQueue();
511
+ yield* PROTO.Header.serialize(header, header_stream);
512
+ const serialized_header = yield* PROTO.MIPS.serialize(header_stream);
592
513
  world
593
514
  .getDimension('overworld')
594
515
  .runCommand(`scriptevent ${serialized_endpoint}:${serialized_header} ${serialized_packet}`);
595
516
  };
596
- const packet_stream = new ByteArray();
517
+ const packet_stream = new PROTO.ByteQueue();
597
518
  yield* serializer.serialize(value, packet_stream);
598
- const serialized_packets = yield* SERDE.serialize(packet_stream, FRAG_MAX);
519
+ const serialized_packets = yield* serialize(packet_stream, FRAG_MAX);
599
520
  for (let i = 0; i < serialized_packets.length; i++) {
600
521
  const serialized_packet = serialized_packets[i];
601
- yield* RUN({ guid, index: i, final: i === serialized_packets.length - 1 }, serialized_packet);
522
+ yield* RUN({ guid, encoding: ENCODING, index: i, final: i === serialized_packets.length - 1 }, serialized_packet);
602
523
  }
603
524
  }
604
525
  NET.emit = emit;
@@ -616,7 +537,7 @@ export var NET;
616
537
  fragment.serialized_packets[payload.index] = serialized_packet;
617
538
  fragment.data_size += payload.index + 1;
618
539
  if (fragment.size !== -1 && fragment.data_size === (fragment.size * (fragment.size + 1)) / 2) {
619
- const stream = yield* SERDE.deserialize(fragment.serialized_packets);
540
+ const stream = yield* deserialize(fragment.serialized_packets);
620
541
  const value = yield* serializer.deserialize(stream);
621
542
  yield* callback(value);
622
543
  buffer.delete(payload.guid);
@@ -628,296 +549,6 @@ export var NET;
628
549
  })(NET || (NET = {}));
629
550
  export var IPC;
630
551
  (function (IPC) {
631
- const ConnectionSerializer = Proto.Object({
632
- from: Proto.String,
633
- bytes: Proto.UInt8Array
634
- });
635
- const HandshakeSynchronizeSerializer = Proto.Object({
636
- from: Proto.String,
637
- encryption_enabled: Proto.Boolean,
638
- encryption_public_key: Proto.String,
639
- encryption_prime: Proto.VarInt,
640
- encryption_modulus: Proto.VarInt
641
- });
642
- const HandshakeAcknowledgeSerializer = Proto.Object({
643
- from: Proto.String,
644
- encryption_enabled: Proto.Boolean,
645
- encryption_public_key: Proto.String
646
- });
647
- class Connection {
648
- *MAYBE_ENCRYPT(bytes) {
649
- return this._enc !== false ? yield* CRYPTO.encrypt(bytes, this._enc) : bytes;
650
- }
651
- *MAYBE_DECRYPT(bytes) {
652
- return this._enc !== false ? yield* CRYPTO.decrypt(bytes, this._enc) : bytes;
653
- }
654
- get from() {
655
- return this._from;
656
- }
657
- get to() {
658
- return this._to;
659
- }
660
- constructor(from, to, encryption) {
661
- this._from = from;
662
- this._to = to;
663
- this._enc = encryption;
664
- this._terminators = new Array();
665
- }
666
- terminate(notify = true) {
667
- const $ = this;
668
- $._terminators.forEach(terminate => terminate());
669
- $._terminators.length = 0;
670
- if (notify) {
671
- system.runJob(NET.emit(`ipc:${$._to}:terminate`, Proto.String, $._from));
672
- }
673
- }
674
- send(channel, serializer, value) {
675
- const $ = this;
676
- system.runJob((function* () {
677
- const stream = new SERDE.ByteArray();
678
- yield* serializer.serialize(value, stream);
679
- const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array());
680
- yield* NET.emit(`ipc:${$._to}:${channel}:send`, ConnectionSerializer, {
681
- from: $._from,
682
- bytes
683
- });
684
- })());
685
- }
686
- invoke(channel, serializer, value, deserializer) {
687
- const $ = this;
688
- system.runJob((function* () {
689
- const stream = new SERDE.ByteArray();
690
- yield* serializer.serialize(value, stream);
691
- const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array());
692
- yield* NET.emit(`ipc:${$._to}:${channel}:invoke`, ConnectionSerializer, {
693
- from: $._from,
694
- bytes
695
- });
696
- })());
697
- return new Promise(resolve => {
698
- const terminate = NET.listen(`ipc:${$._from}:${channel}:handle`, ConnectionSerializer, function* (data) {
699
- if (data.from === $._to) {
700
- const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
701
- const stream = SERDE.ByteArray.from_uint8array(bytes);
702
- const value = yield* deserializer.deserialize(stream);
703
- resolve(value);
704
- terminate();
705
- }
706
- });
707
- });
708
- }
709
- on(channel, deserializer, listener) {
710
- const $ = this;
711
- const terminate = NET.listen(`ipc:${$._from}:${channel}:send`, ConnectionSerializer, function* (data) {
712
- if (data.from === $._to) {
713
- const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
714
- const stream = SERDE.ByteArray.from_uint8array(bytes);
715
- const value = yield* deserializer.deserialize(stream);
716
- listener(value);
717
- }
718
- });
719
- $._terminators.push(terminate);
720
- return terminate;
721
- }
722
- once(channel, deserializer, listener) {
723
- const $ = this;
724
- const terminate = NET.listen(`ipc:${$._from}:${channel}:send`, ConnectionSerializer, function* (data) {
725
- if (data.from === $._to) {
726
- const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
727
- const stream = SERDE.ByteArray.from_uint8array(bytes);
728
- const value = yield* deserializer.deserialize(stream);
729
- listener(value);
730
- terminate();
731
- }
732
- });
733
- $._terminators.push(terminate);
734
- return terminate;
735
- }
736
- handle(channel, deserializer, serializer, listener) {
737
- const $ = this;
738
- const terminate = NET.listen(`ipc:${$._from}:${channel}:invoke`, ConnectionSerializer, function* (data) {
739
- if (data.from === $._to) {
740
- const bytes = yield* $.MAYBE_DECRYPT(data.bytes);
741
- const stream = SERDE.ByteArray.from_uint8array(bytes);
742
- const value = yield* deserializer.deserialize(stream);
743
- const result = listener(value);
744
- const return_stream = new SERDE.ByteArray();
745
- yield* serializer.serialize(result, return_stream);
746
- const return_bytes = yield* $.MAYBE_ENCRYPT(return_stream.to_uint8array());
747
- yield* NET.emit(`ipc:${$._to}:${channel}:handle`, ConnectionSerializer, {
748
- from: $._from,
749
- bytes: return_bytes
750
- });
751
- }
752
- });
753
- $._terminators.push(terminate);
754
- return terminate;
755
- }
756
- }
757
- IPC.Connection = Connection;
758
- class ConnectionManager {
759
- *MAYBE_ENCRYPT(bytes, encryption) {
760
- return encryption !== false ? yield* CRYPTO.encrypt(bytes, encryption) : bytes;
761
- }
762
- *MAYBE_DECRYPT(bytes, encryption) {
763
- return encryption !== false ? yield* CRYPTO.decrypt(bytes, encryption) : bytes;
764
- }
765
- get id() {
766
- return this._id;
767
- }
768
- constructor(id, force_encryption = false) {
769
- const $ = this;
770
- this._id = id;
771
- this._enc_map = new Map();
772
- this._con_map = new Map();
773
- this._enc_force = force_encryption;
774
- NET.listen(`ipc:${this._id}:handshake:synchronize`, HandshakeSynchronizeSerializer, function* (data) {
775
- const secret = CRYPTO.make_secret(data.encryption_modulus);
776
- const public_key = yield* CRYPTO.make_public(secret, data.encryption_modulus, data.encryption_prime);
777
- const enc = data.encryption_enabled || $._enc_force
778
- ? yield* CRYPTO.make_shared(secret, data.encryption_public_key, data.encryption_prime)
779
- : false;
780
- $._enc_map.set(data.from, enc);
781
- yield* NET.emit(`ipc:${data.from}:handshake:acknowledge`, HandshakeAcknowledgeSerializer, {
782
- from: $._id,
783
- encryption_public_key: public_key,
784
- encryption_enabled: $._enc_force
785
- });
786
- });
787
- NET.listen(`ipc:${this._id}:terminate`, Proto.String, function* (value) {
788
- $._enc_map.delete(value);
789
- });
790
- }
791
- connect(to, encrypted = false, timeout = 20) {
792
- const $ = this;
793
- return new Promise((resolve, reject) => {
794
- const con = this._con_map.get(to);
795
- if (con !== undefined) {
796
- con.terminate(false);
797
- resolve(con);
798
- }
799
- else {
800
- const secret = CRYPTO.make_secret();
801
- system.runJob((function* () {
802
- const public_key = yield* CRYPTO.make_public(secret);
803
- yield* NET.emit(`ipc:${to}:handshake:synchronize`, HandshakeSynchronizeSerializer, {
804
- from: $._id,
805
- encryption_enabled: encrypted,
806
- encryption_public_key: public_key,
807
- encryption_prime: CRYPTO.PRIME,
808
- encryption_modulus: CRYPTO.MOD
809
- });
810
- })());
811
- function clear() {
812
- terminate();
813
- system.clearRun(timeout_handle);
814
- }
815
- const timeout_handle = system.runTimeout(() => {
816
- reject();
817
- clear();
818
- }, timeout);
819
- const terminate = NET.listen(`ipc:${this._id}:handshake:acknowledge`, HandshakeAcknowledgeSerializer, function* (data) {
820
- if (data.from === to) {
821
- const enc = data.encryption_enabled || encrypted
822
- ? yield* CRYPTO.make_shared(secret, data.encryption_public_key)
823
- : false;
824
- const new_con = new Connection($._id, to, enc);
825
- $._con_map.set(to, new_con);
826
- resolve(new_con);
827
- clear();
828
- }
829
- });
830
- }
831
- });
832
- }
833
- send(channel, serializer, value) {
834
- const $ = this;
835
- system.runJob((function* () {
836
- for (const [key, enc] of $._enc_map) {
837
- const stream = new SERDE.ByteArray();
838
- yield* serializer.serialize(value, stream);
839
- const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array(), enc);
840
- yield* NET.emit(`ipc:${key}:${channel}:send`, ConnectionSerializer, {
841
- from: $._id,
842
- bytes
843
- });
844
- }
845
- })());
846
- }
847
- invoke(channel, serializer, value, deserializer) {
848
- const $ = this;
849
- const promises = [];
850
- for (const [key, enc] of $._enc_map) {
851
- system.runJob((function* () {
852
- const stream = new SERDE.ByteArray();
853
- yield* serializer.serialize(value, stream);
854
- const bytes = yield* $.MAYBE_ENCRYPT(stream.to_uint8array(), enc);
855
- yield* NET.emit(`ipc:${key}:${channel}:invoke`, ConnectionSerializer, {
856
- from: $._id,
857
- bytes
858
- });
859
- })());
860
- promises.push(new Promise(resolve => {
861
- const terminate = NET.listen(`ipc:${$._id}:${channel}:handle`, ConnectionSerializer, function* (data) {
862
- if (data.from === key) {
863
- const bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
864
- const stream = SERDE.ByteArray.from_uint8array(bytes);
865
- const value = yield* deserializer.deserialize(stream);
866
- resolve(value);
867
- terminate();
868
- }
869
- });
870
- }));
871
- }
872
- return promises;
873
- }
874
- on(channel, deserializer, listener) {
875
- const $ = this;
876
- return NET.listen(`ipc:${$._id}:${channel}:send`, ConnectionSerializer, function* (data) {
877
- const enc = $._enc_map.get(data.from);
878
- if (enc !== undefined) {
879
- const bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
880
- const stream = SERDE.ByteArray.from_uint8array(bytes);
881
- const value = yield* deserializer.deserialize(stream);
882
- listener(value);
883
- }
884
- });
885
- }
886
- once(channel, deserializer, listener) {
887
- const $ = this;
888
- const terminate = NET.listen(`ipc:${$._id}:${channel}:send`, ConnectionSerializer, function* (data) {
889
- const enc = $._enc_map.get(data.from);
890
- if (enc !== undefined) {
891
- const bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
892
- const stream = SERDE.ByteArray.from_uint8array(bytes);
893
- const value = yield* deserializer.deserialize(stream);
894
- listener(value);
895
- terminate();
896
- }
897
- });
898
- return terminate;
899
- }
900
- handle(channel, deserializer, serializer, listener) {
901
- const $ = this;
902
- return NET.listen(`ipc:${$._id}:${channel}:invoke`, ConnectionSerializer, function* (data) {
903
- const enc = $._enc_map.get(data.from);
904
- if (enc !== undefined) {
905
- const input_bytes = yield* $.MAYBE_DECRYPT(data.bytes, enc);
906
- const input_stream = SERDE.ByteArray.from_uint8array(input_bytes);
907
- const input_value = yield* deserializer.deserialize(input_stream);
908
- const result = listener(input_value);
909
- const output_stream = new SERDE.ByteArray();
910
- yield* serializer.serialize(result, output_stream);
911
- const output_bytes = yield* $.MAYBE_ENCRYPT(output_stream.to_uint8array(), enc);
912
- yield* NET.emit(`ipc:${data.from}:${channel}:handle`, ConnectionSerializer, {
913
- from: $._id,
914
- bytes: output_bytes
915
- });
916
- }
917
- });
918
- }
919
- }
920
- IPC.ConnectionManager = ConnectionManager;
921
552
  /** Sends a message with `args` to `channel` */
922
553
  function send(channel, serializer, value) {
923
554
  system.runJob(NET.emit(`ipc:${channel}:send`, serializer, value));