mcbe-ipc 2.0.0
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/LICENSE +21 -0
- package/README.md +90 -0
- package/dist/ipc.d.ts +88 -0
- package/dist/ipc.js +515 -0
- package/dist/ipc.min.js +1 -0
- package/package.json +25 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 OmniacDev
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# MCBE-IPC 📡
|
|
2
|
+
|
|
3
|
+
An IPC[^1] system for MCBE Script API projects
|
|
4
|
+
|
|
5
|
+
[^1]: Inter-Pack Communication
|
|
6
|
+
|
|
7
|
+
## Dependencies
|
|
8
|
+
|
|
9
|
+
| Name | Version |
|
|
10
|
+
|---|---|
|
|
11
|
+
| `@minecraft/server` | Any |
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
**JavaScript**
|
|
15
|
+
1. Download `ipc.js` and `ipc.d.ts` from the latest [release](https://github.com/OmniacDev/MCBE-IPC/releases/latest)
|
|
16
|
+
2. Copy the files into your project
|
|
17
|
+
|
|
18
|
+
**TypeScript**
|
|
19
|
+
1. Download `ipc.ts` from the latest [release](https://github.com/OmniacDev/MCBE-IPC/releases/latest)
|
|
20
|
+
2. Copy file into your project
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
### Sending & Receiving
|
|
25
|
+
|
|
26
|
+
`IPC.send()` and `IPC.on()` can be used to send messages or data between packs.
|
|
27
|
+
|
|
28
|
+
_Pack 1_
|
|
29
|
+
```js
|
|
30
|
+
import IPC from 'ipc.js'
|
|
31
|
+
|
|
32
|
+
IPC.on('message_channel', (args) => {
|
|
33
|
+
console.log(`Message: ${args}`)
|
|
34
|
+
})
|
|
35
|
+
|
|
36
|
+
IPC.on('data_channel', (args) => {
|
|
37
|
+
console.log(`Data: ${args.example_bool}, ${args.example_number}`)
|
|
38
|
+
})
|
|
39
|
+
```
|
|
40
|
+
_Pack 2_
|
|
41
|
+
```js
|
|
42
|
+
import IPC from 'ipc.js'
|
|
43
|
+
|
|
44
|
+
IPC.send('message_channel', 'Example Message')
|
|
45
|
+
|
|
46
|
+
IPC.send('data_channel', { example_number: 100, example_bool: true })
|
|
47
|
+
```
|
|
48
|
+
_Console Output_
|
|
49
|
+
```
|
|
50
|
+
Message: Example Message
|
|
51
|
+
Data: true, 100
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Requesting & Serving
|
|
55
|
+
|
|
56
|
+
`IPC.invoke()` and `IPC.handle()` can be used to request and serve data between packs.
|
|
57
|
+
|
|
58
|
+
_Pack 1_
|
|
59
|
+
```js
|
|
60
|
+
import IPC from 'ipc.js'
|
|
61
|
+
|
|
62
|
+
IPC.handle('request_channel', (args) => {
|
|
63
|
+
switch (args) {
|
|
64
|
+
case 'status':
|
|
65
|
+
return 'inactive'
|
|
66
|
+
case 'size':
|
|
67
|
+
return 100
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
```
|
|
71
|
+
_Pack 2_
|
|
72
|
+
```js
|
|
73
|
+
import IPC from 'ipc.js'
|
|
74
|
+
|
|
75
|
+
IPC.invoke('request_channel', 'status').then(result => {
|
|
76
|
+
console.log(`Status: ${result}`)
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
IPC.invoke('request_channel', 'size').then(result => {
|
|
80
|
+
console.log(`Size: ${result}`)
|
|
81
|
+
})
|
|
82
|
+
```
|
|
83
|
+
_Console Output_
|
|
84
|
+
```
|
|
85
|
+
Status: inactive
|
|
86
|
+
Size: 100
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
|
package/dist/ipc.d.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* MIT License
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2024 OmniacDev
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
* copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
* SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
declare namespace IPC {
|
|
26
|
+
type SendTypes = {};
|
|
27
|
+
type InvokeTypes = {};
|
|
28
|
+
type HandleTypes = {};
|
|
29
|
+
export class Connection {
|
|
30
|
+
private readonly _from;
|
|
31
|
+
private readonly _to;
|
|
32
|
+
private readonly _enc;
|
|
33
|
+
private readonly _terminators;
|
|
34
|
+
private MAYBE_ENCRYPT;
|
|
35
|
+
private MAYBE_DECRYPT;
|
|
36
|
+
get from(): string;
|
|
37
|
+
get to(): string;
|
|
38
|
+
constructor(from: string, to: string, encryption: string | false);
|
|
39
|
+
terminate(notify?: boolean): void;
|
|
40
|
+
send<T extends any[]>(channel: string, ...args: T): void;
|
|
41
|
+
invoke<T extends any[], R extends any>(channel: string, ...args: T): Promise<R>;
|
|
42
|
+
on<T extends any[]>(channel: string, listener: (...args: T) => void): () => void;
|
|
43
|
+
once<T extends any[]>(channel: string, listener: (...args: T) => void): () => void;
|
|
44
|
+
handle<T extends any[], R extends any>(channel: string, listener: (...args: T) => R): () => void;
|
|
45
|
+
}
|
|
46
|
+
export class ConnectionManager {
|
|
47
|
+
private readonly _id;
|
|
48
|
+
private readonly _enc_map;
|
|
49
|
+
private readonly _con_map;
|
|
50
|
+
private readonly _enc_force;
|
|
51
|
+
private MAYBE_ENCRYPT;
|
|
52
|
+
private MAYBE_DECRYPT;
|
|
53
|
+
get id(): string;
|
|
54
|
+
constructor(id: string, force_encryption?: boolean);
|
|
55
|
+
connect(to: string, encrypted?: boolean, timeout?: number): Promise<Connection>;
|
|
56
|
+
send<T extends any[]>(channel: string, ...args: T): void;
|
|
57
|
+
invoke<T extends any[], R extends any>(channel: string, ...args: T): Promise<R>[];
|
|
58
|
+
on<T extends any[]>(channel: string, listener: (...args: T) => void): () => void;
|
|
59
|
+
once<T extends any[]>(channel: string, listener: (...args: T) => void): () => void;
|
|
60
|
+
handle<T extends any[], R extends any>(channel: string, listener: (...args: T) => R): () => void;
|
|
61
|
+
}
|
|
62
|
+
/** Sends a message with `args` to `channel` */
|
|
63
|
+
export function send<C extends keyof SendTypes>(channel: C, ...args: SendTypes[C]): void;
|
|
64
|
+
/** Sends a message with `args` to `channel` */
|
|
65
|
+
export function send<T extends any[]>(channel: string, ...args: T): void;
|
|
66
|
+
/** Sends an `invoke` message through IPC, and expects a result asynchronously. */
|
|
67
|
+
export function invoke<C extends keyof (HandleTypes & InvokeTypes)>(channel: C, ...args: InvokeTypes[C]): Promise<HandleTypes[C]>;
|
|
68
|
+
/** Sends an `invoke` message through IPC, and expects a result asynchronously. */
|
|
69
|
+
export function invoke<T extends any[], R extends any>(channel: string, ...args: T): Promise<R>;
|
|
70
|
+
/** Listens to `channel`. When a new message arrives, `listener` will be called with `listener(args)`. */
|
|
71
|
+
export function on<C extends keyof SendTypes>(channel: C, listener: (...args: SendTypes[C]) => void): () => void;
|
|
72
|
+
/** Listens to `channel`. When a new message arrives, `listener` will be called with `listener(args)`. */
|
|
73
|
+
export function on<T extends any[]>(channel: string, listener: (...args: T) => void): () => void;
|
|
74
|
+
/** Listens to `channel` once. When a new message arrives, `listener` will be called with `listener(args)`, and then removed. */
|
|
75
|
+
export function once<C extends keyof SendTypes>(channel: C, listener: (...args: SendTypes[C]) => void): () => void;
|
|
76
|
+
/** Listens to `channel` once. When a new message arrives, `listener` will be called with `listener(args)`, and then removed. */
|
|
77
|
+
export function once<T extends any[]>(channel: string, listener: (...args: T) => void): () => void;
|
|
78
|
+
/** Adds a handler for an `invoke` IPC. This handler will be called whenever `invoke(channel, ...args)` is called */
|
|
79
|
+
export function handle<C extends keyof (HandleTypes & InvokeTypes)>(channel: C, listener: (...args: InvokeTypes[C]) => HandleTypes[C]): () => void;
|
|
80
|
+
/** Adds a handler for an `invoke` IPC. This handler will be called whenever `invoke(channel, ...args)` is called */
|
|
81
|
+
export function handle<T extends any[], R extends any>(channel: string, listener: (...args: T) => R): () => void;
|
|
82
|
+
export {};
|
|
83
|
+
}
|
|
84
|
+
export default IPC;
|
|
85
|
+
export declare namespace NET {
|
|
86
|
+
function emit<T = any>(namespace: string, channel: string, args: T): Generator<void, void, void>;
|
|
87
|
+
function listen<T = any>(namespace: string, channel: string, callback: (args: T) => Generator<void, void, void>): () => void;
|
|
88
|
+
}
|
package/dist/ipc.js
ADDED
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license
|
|
3
|
+
* MIT License
|
|
4
|
+
*
|
|
5
|
+
* Copyright (c) 2024 OmniacDev
|
|
6
|
+
*
|
|
7
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
* in the Software without restriction, including without limitation the rights
|
|
10
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
* furnished to do so, subject to the following conditions:
|
|
13
|
+
*
|
|
14
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
* copies or substantial portions of the Software.
|
|
16
|
+
*
|
|
17
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
* SOFTWARE.
|
|
24
|
+
*/
|
|
25
|
+
import { world, system, ScriptEventSource } from '@minecraft/server';
|
|
26
|
+
var IPC;
|
|
27
|
+
(function (IPC) {
|
|
28
|
+
class Connection {
|
|
29
|
+
*MAYBE_ENCRYPT(args) {
|
|
30
|
+
return this._enc !== false ? yield* CRYPTO.encrypt(JSON.stringify(args), this._enc) : args;
|
|
31
|
+
}
|
|
32
|
+
*MAYBE_DECRYPT(args) {
|
|
33
|
+
return this._enc !== false ? JSON.parse(yield* CRYPTO.decrypt(args[1], this._enc)) : args[1];
|
|
34
|
+
}
|
|
35
|
+
get from() {
|
|
36
|
+
return this._from;
|
|
37
|
+
}
|
|
38
|
+
get to() {
|
|
39
|
+
return this._to;
|
|
40
|
+
}
|
|
41
|
+
constructor(from, to, encryption) {
|
|
42
|
+
this._from = from;
|
|
43
|
+
this._to = to;
|
|
44
|
+
this._enc = encryption;
|
|
45
|
+
this._terminators = new Array();
|
|
46
|
+
}
|
|
47
|
+
terminate(notify = true) {
|
|
48
|
+
const $ = this;
|
|
49
|
+
$._terminators.forEach(terminate => terminate());
|
|
50
|
+
$._terminators.length = 0;
|
|
51
|
+
if (notify) {
|
|
52
|
+
system.runJob(NET.emit('ipc', `${$._to}:terminate`, [$._from]));
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
send(channel, ...args) {
|
|
56
|
+
const $ = this;
|
|
57
|
+
system.runJob((function* () {
|
|
58
|
+
const data = yield* $.MAYBE_ENCRYPT(args);
|
|
59
|
+
yield* NET.emit('ipc', `${$._to}:${channel}:send`, [$._from, data]);
|
|
60
|
+
})());
|
|
61
|
+
}
|
|
62
|
+
invoke(channel, ...args) {
|
|
63
|
+
const $ = this;
|
|
64
|
+
system.runJob((function* () {
|
|
65
|
+
const data = yield* $.MAYBE_ENCRYPT(args);
|
|
66
|
+
yield* NET.emit('ipc', `${$._to}:${channel}:invoke`, [$._from, data]);
|
|
67
|
+
})());
|
|
68
|
+
return new Promise(resolve => {
|
|
69
|
+
const terminate = NET.listen('ipc', `${$._from}:${channel}:handle`, function* (args) {
|
|
70
|
+
if (args[0] === $._to) {
|
|
71
|
+
const data = yield* $.MAYBE_DECRYPT(args);
|
|
72
|
+
resolve(data);
|
|
73
|
+
terminate();
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
on(channel, listener) {
|
|
79
|
+
const $ = this;
|
|
80
|
+
const terminate = NET.listen('ipc', `${$._from}:${channel}:send`, function* (args) {
|
|
81
|
+
if (args[0] === $._to) {
|
|
82
|
+
const data = yield* $.MAYBE_DECRYPT(args);
|
|
83
|
+
listener(...data);
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
$._terminators.push(terminate);
|
|
87
|
+
return terminate;
|
|
88
|
+
}
|
|
89
|
+
once(channel, listener) {
|
|
90
|
+
const $ = this;
|
|
91
|
+
const terminate = NET.listen('ipc', `${$._from}:${channel}:send`, function* (args) {
|
|
92
|
+
if (args[0] === $._to) {
|
|
93
|
+
const data = yield* $.MAYBE_DECRYPT(args);
|
|
94
|
+
listener(...data);
|
|
95
|
+
terminate();
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
$._terminators.push(terminate);
|
|
99
|
+
return terminate;
|
|
100
|
+
}
|
|
101
|
+
handle(channel, listener) {
|
|
102
|
+
const $ = this;
|
|
103
|
+
const terminate = NET.listen('ipc', `${$._from}:${channel}:invoke`, function* (args) {
|
|
104
|
+
if (args[0] === $._to) {
|
|
105
|
+
const data = yield* $.MAYBE_DECRYPT(args);
|
|
106
|
+
const result = listener(...data);
|
|
107
|
+
const return_data = yield* $.MAYBE_ENCRYPT([result]);
|
|
108
|
+
yield* NET.emit('ipc', `${$._to}:${channel}:handle`, [$._from, return_data]);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
$._terminators.push(terminate);
|
|
112
|
+
return terminate;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
IPC.Connection = Connection;
|
|
116
|
+
class ConnectionManager {
|
|
117
|
+
*MAYBE_ENCRYPT(args, encryption) {
|
|
118
|
+
return encryption !== false ? yield* CRYPTO.encrypt(JSON.stringify(args), encryption) : args;
|
|
119
|
+
}
|
|
120
|
+
*MAYBE_DECRYPT(args, encryption) {
|
|
121
|
+
return encryption !== false ? JSON.parse(yield* CRYPTO.decrypt(args[1], encryption)) : args[1];
|
|
122
|
+
}
|
|
123
|
+
get id() {
|
|
124
|
+
return this._id;
|
|
125
|
+
}
|
|
126
|
+
constructor(id, force_encryption = false) {
|
|
127
|
+
const $ = this;
|
|
128
|
+
this._id = id;
|
|
129
|
+
this._enc_map = new Map();
|
|
130
|
+
this._con_map = new Map();
|
|
131
|
+
this._enc_force = force_encryption;
|
|
132
|
+
NET.listen('ipc', `${this._id}:handshake:SYN`, function* (args) {
|
|
133
|
+
const secret = CRYPTO.make_secret(args[4]);
|
|
134
|
+
const public_key = yield* CRYPTO.make_public(secret, args[4], args[3]);
|
|
135
|
+
const enc = args[1] === 1 || $._enc_force ? yield* CRYPTO.make_shared(secret, args[2], args[3]) : false;
|
|
136
|
+
$._enc_map.set(args[0], enc);
|
|
137
|
+
yield* NET.emit('ipc', `${args[0]}:handshake:ACK`, [
|
|
138
|
+
$._id,
|
|
139
|
+
$._enc_force ? 1 : 0,
|
|
140
|
+
public_key
|
|
141
|
+
]);
|
|
142
|
+
});
|
|
143
|
+
NET.listen('ipc', `${this._id}:terminate`, function* (args) {
|
|
144
|
+
$._enc_map.delete(args[0]);
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
connect(to, encrypted = false, timeout = 20) {
|
|
148
|
+
const $ = this;
|
|
149
|
+
return new Promise((resolve, reject) => {
|
|
150
|
+
const con = this._con_map.get(to);
|
|
151
|
+
if (con !== undefined) {
|
|
152
|
+
con.terminate(false);
|
|
153
|
+
resolve(con);
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
const secret = CRYPTO.make_secret();
|
|
157
|
+
const enc_flag = encrypted ? 1 : 0;
|
|
158
|
+
system.runJob((function* () {
|
|
159
|
+
const public_key = yield* CRYPTO.make_public(secret);
|
|
160
|
+
yield* NET.emit('ipc', `${to}:handshake:SYN`, [
|
|
161
|
+
$._id,
|
|
162
|
+
enc_flag,
|
|
163
|
+
public_key,
|
|
164
|
+
CRYPTO.PRIME,
|
|
165
|
+
CRYPTO.MOD
|
|
166
|
+
]);
|
|
167
|
+
})());
|
|
168
|
+
function clear() {
|
|
169
|
+
terminate();
|
|
170
|
+
system.clearRun(timeout_handle);
|
|
171
|
+
}
|
|
172
|
+
const timeout_handle = system.runTimeout(() => {
|
|
173
|
+
reject();
|
|
174
|
+
clear();
|
|
175
|
+
}, timeout);
|
|
176
|
+
const terminate = NET.listen('ipc', `${this._id}:handshake:ACK`, function* (args) {
|
|
177
|
+
if (args[0] === to) {
|
|
178
|
+
const enc = args[1] === 1 || encrypted ? yield* CRYPTO.make_shared(secret, args[2]) : false;
|
|
179
|
+
const new_con = new Connection($._id, to, enc);
|
|
180
|
+
$._con_map.set(to, new_con);
|
|
181
|
+
resolve(new_con);
|
|
182
|
+
clear();
|
|
183
|
+
}
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
send(channel, ...args) {
|
|
189
|
+
const $ = this;
|
|
190
|
+
system.runJob((function* () {
|
|
191
|
+
for (const [key, value] of $._enc_map) {
|
|
192
|
+
const data = yield* $.MAYBE_ENCRYPT(args, value);
|
|
193
|
+
yield* NET.emit('ipc', `${key}:${channel}:send`, [$._id, data]);
|
|
194
|
+
}
|
|
195
|
+
})());
|
|
196
|
+
}
|
|
197
|
+
invoke(channel, ...args) {
|
|
198
|
+
const $ = this;
|
|
199
|
+
const promises = [];
|
|
200
|
+
for (const [key, value] of $._enc_map) {
|
|
201
|
+
system.runJob((function* () {
|
|
202
|
+
const data = yield* $.MAYBE_ENCRYPT(args, value);
|
|
203
|
+
yield* NET.emit('ipc', `${key}:${channel}:invoke`, [$._id, data]);
|
|
204
|
+
})());
|
|
205
|
+
promises.push(new Promise(resolve => {
|
|
206
|
+
const terminate = NET.listen('ipc', `${$._id}:${channel}:handle`, function* (args) {
|
|
207
|
+
if (args[0] === key) {
|
|
208
|
+
const data = yield* $.MAYBE_DECRYPT(args, value);
|
|
209
|
+
resolve(data);
|
|
210
|
+
terminate();
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}));
|
|
214
|
+
}
|
|
215
|
+
return promises;
|
|
216
|
+
}
|
|
217
|
+
on(channel, listener) {
|
|
218
|
+
const $ = this;
|
|
219
|
+
return NET.listen('ipc', `${$._id}:${channel}:send`, function* (args) {
|
|
220
|
+
const enc = $._enc_map.get(args[0]);
|
|
221
|
+
if (enc !== undefined) {
|
|
222
|
+
const data = yield* $.MAYBE_DECRYPT(args, enc);
|
|
223
|
+
listener(...data);
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
once(channel, listener) {
|
|
228
|
+
const $ = this;
|
|
229
|
+
const terminate = NET.listen('ipc', `${$._id}:${channel}:send`, function* (args) {
|
|
230
|
+
const enc = $._enc_map.get(args[0]);
|
|
231
|
+
if (enc !== undefined) {
|
|
232
|
+
const data = yield* $.MAYBE_DECRYPT(args, enc);
|
|
233
|
+
listener(...data);
|
|
234
|
+
terminate();
|
|
235
|
+
}
|
|
236
|
+
});
|
|
237
|
+
return terminate;
|
|
238
|
+
}
|
|
239
|
+
handle(channel, listener) {
|
|
240
|
+
const $ = this;
|
|
241
|
+
return NET.listen('ipc', `${$._id}:${channel}:invoke`, function* (args) {
|
|
242
|
+
const enc = $._enc_map.get(args[0]);
|
|
243
|
+
if (enc !== undefined) {
|
|
244
|
+
const data = yield* $.MAYBE_DECRYPT(args, enc);
|
|
245
|
+
const result = listener(...data);
|
|
246
|
+
const return_data = yield* $.MAYBE_ENCRYPT([result], enc);
|
|
247
|
+
yield* NET.emit('ipc', `${args[0]}:${channel}:handle`, [$._id, return_data]);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
IPC.ConnectionManager = ConnectionManager;
|
|
253
|
+
/** Sends a message with `args` to `channel` */
|
|
254
|
+
function send(channel, ...args) {
|
|
255
|
+
system.runJob(NET.emit('ipc', `${channel}:send`, args));
|
|
256
|
+
}
|
|
257
|
+
IPC.send = send;
|
|
258
|
+
function invoke(channel, ...args) {
|
|
259
|
+
system.runJob(NET.emit('ipc', `${channel}:invoke`, args));
|
|
260
|
+
return new Promise(resolve => {
|
|
261
|
+
const terminate = NET.listen('ipc', `${channel}:handle`, function* (args) {
|
|
262
|
+
resolve(args);
|
|
263
|
+
terminate();
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
IPC.invoke = invoke;
|
|
268
|
+
/** Listens to `channel`. When a new message arrives, `listener` will be called with `listener(args)`. */
|
|
269
|
+
function on(channel, listener) {
|
|
270
|
+
return NET.listen('ipc', `${channel}:send`, function* (args) {
|
|
271
|
+
listener(...args);
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
IPC.on = on;
|
|
275
|
+
/** Listens to `channel` once. When a new message arrives, `listener` will be called with `listener(args)`, and then removed. */
|
|
276
|
+
function once(channel, listener) {
|
|
277
|
+
const terminate = NET.listen('ipc', `${channel}:send`, function* (args) {
|
|
278
|
+
listener(...args);
|
|
279
|
+
terminate();
|
|
280
|
+
});
|
|
281
|
+
return terminate;
|
|
282
|
+
}
|
|
283
|
+
IPC.once = once;
|
|
284
|
+
/** Adds a handler for an `invoke` IPC. This handler will be called whenever `invoke(channel, ...args)` is called */
|
|
285
|
+
function handle(channel, listener) {
|
|
286
|
+
return NET.listen('ipc', `${channel}:invoke`, function* (args) {
|
|
287
|
+
const result = listener(...args);
|
|
288
|
+
yield* NET.emit('ipc', `${channel}:handle`, result);
|
|
289
|
+
});
|
|
290
|
+
}
|
|
291
|
+
IPC.handle = handle;
|
|
292
|
+
})(IPC || (IPC = {}));
|
|
293
|
+
export default IPC;
|
|
294
|
+
var SERDE;
|
|
295
|
+
(function (SERDE) {
|
|
296
|
+
const INVALID_START_CODES = [48, 49, 50, 51, 52, 53, 54, 55, 56, 57];
|
|
297
|
+
const INVALID_CODES = [
|
|
298
|
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30,
|
|
299
|
+
31, 32, 33, 34, 35, 36, 37, 38, 39, 42, 43, 44, 47, 58, 59, 60, 61, 62, 63, 64, 91, 92, 93, 94, 96, 123, 124, 125,
|
|
300
|
+
126, 127
|
|
301
|
+
];
|
|
302
|
+
const sequence_regex = /\?[0-9a-zA-Z.\-]{2}|[^?]+/g;
|
|
303
|
+
const encoded_regex = /^\?[0-9a-zA-Z.\-]{2}$/;
|
|
304
|
+
const BASE64 = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-';
|
|
305
|
+
function* b64_encode(char) {
|
|
306
|
+
let encoded = '';
|
|
307
|
+
for (let code = char.charCodeAt(0); code > 0; code = Math.floor(code / 64)) {
|
|
308
|
+
encoded = BASE64[code % 64] + encoded;
|
|
309
|
+
yield;
|
|
310
|
+
}
|
|
311
|
+
return encoded;
|
|
312
|
+
}
|
|
313
|
+
function* b64_decode(enc) {
|
|
314
|
+
let code = 0;
|
|
315
|
+
for (let i = 0; i < enc.length; i++) {
|
|
316
|
+
code += 64 ** (enc.length - 1 - i) * BASE64.indexOf(enc[i]);
|
|
317
|
+
yield;
|
|
318
|
+
}
|
|
319
|
+
return String.fromCharCode(code);
|
|
320
|
+
}
|
|
321
|
+
function* encode(str) {
|
|
322
|
+
let result = '';
|
|
323
|
+
for (let i = 0; i < str.length; i++) {
|
|
324
|
+
const char = str.charAt(i);
|
|
325
|
+
const char_code = char.charCodeAt(0);
|
|
326
|
+
if ((i === 0 && INVALID_START_CODES.includes(char_code)) || INVALID_CODES.includes(char_code)) {
|
|
327
|
+
result += `?${(yield* b64_encode(char)).padStart(2, '0')}`;
|
|
328
|
+
}
|
|
329
|
+
else {
|
|
330
|
+
result += char;
|
|
331
|
+
}
|
|
332
|
+
yield;
|
|
333
|
+
}
|
|
334
|
+
return result;
|
|
335
|
+
}
|
|
336
|
+
SERDE.encode = encode;
|
|
337
|
+
function* decode(str) {
|
|
338
|
+
let result = '';
|
|
339
|
+
const seqs = str.match(sequence_regex) ?? [];
|
|
340
|
+
for (let i = 0; i < seqs.length; i++) {
|
|
341
|
+
const seq = seqs[i];
|
|
342
|
+
if (seq.startsWith('?') && encoded_regex.test(seq))
|
|
343
|
+
result += yield* b64_decode(seq.slice(1));
|
|
344
|
+
else {
|
|
345
|
+
result += seq;
|
|
346
|
+
}
|
|
347
|
+
yield;
|
|
348
|
+
}
|
|
349
|
+
return result;
|
|
350
|
+
}
|
|
351
|
+
SERDE.decode = decode;
|
|
352
|
+
})(SERDE || (SERDE = {}));
|
|
353
|
+
var CRYPTO;
|
|
354
|
+
(function (CRYPTO) {
|
|
355
|
+
CRYPTO.PRIME = 19893121;
|
|
356
|
+
CRYPTO.MOD = 341;
|
|
357
|
+
const to_HEX = (n) => n.toString(16).toUpperCase();
|
|
358
|
+
const to_NUM = (h) => parseInt(h, 16);
|
|
359
|
+
function* mod_exp(base, exp, mod) {
|
|
360
|
+
let result = 1;
|
|
361
|
+
let b = base % mod;
|
|
362
|
+
for (let e = exp; e > 0; e = Math.floor(e / 2)) {
|
|
363
|
+
if (e % 2 === 1) {
|
|
364
|
+
result = (result * b) % mod;
|
|
365
|
+
}
|
|
366
|
+
b = (b * b) % mod;
|
|
367
|
+
yield;
|
|
368
|
+
}
|
|
369
|
+
return result;
|
|
370
|
+
}
|
|
371
|
+
function make_secret(mod = CRYPTO.MOD) {
|
|
372
|
+
return Math.floor(Math.random() * (mod - 1)) + 1;
|
|
373
|
+
}
|
|
374
|
+
CRYPTO.make_secret = make_secret;
|
|
375
|
+
function* make_public(secret, mod = CRYPTO.MOD, prime = CRYPTO.PRIME) {
|
|
376
|
+
return to_HEX(yield* mod_exp(mod, secret, prime));
|
|
377
|
+
}
|
|
378
|
+
CRYPTO.make_public = make_public;
|
|
379
|
+
function* make_shared(secret, other, prime = CRYPTO.PRIME) {
|
|
380
|
+
return to_HEX(yield* mod_exp(to_NUM(other), secret, prime));
|
|
381
|
+
}
|
|
382
|
+
CRYPTO.make_shared = make_shared;
|
|
383
|
+
function* encrypt(raw, key) {
|
|
384
|
+
let encrypted = '';
|
|
385
|
+
for (let i = 0; i < raw.length; i++) {
|
|
386
|
+
encrypted += String.fromCharCode(raw.charCodeAt(i) ^ key.charCodeAt(i % key.length));
|
|
387
|
+
yield;
|
|
388
|
+
}
|
|
389
|
+
return encrypted;
|
|
390
|
+
}
|
|
391
|
+
CRYPTO.encrypt = encrypt;
|
|
392
|
+
function* decrypt(encrypted, key) {
|
|
393
|
+
let decrypted = '';
|
|
394
|
+
for (let i = 0; i < encrypted.length; i++) {
|
|
395
|
+
decrypted += String.fromCharCode(encrypted.charCodeAt(i) ^ key.charCodeAt(i % key.length));
|
|
396
|
+
yield;
|
|
397
|
+
}
|
|
398
|
+
return decrypted;
|
|
399
|
+
}
|
|
400
|
+
CRYPTO.decrypt = decrypt;
|
|
401
|
+
})(CRYPTO || (CRYPTO = {}));
|
|
402
|
+
export var NET;
|
|
403
|
+
(function (NET) {
|
|
404
|
+
const FRAG_MAX = 2048;
|
|
405
|
+
const namespace_listeners = new Map();
|
|
406
|
+
system.afterEvents.scriptEventReceive.subscribe(event => {
|
|
407
|
+
system.runJob((function* () {
|
|
408
|
+
const ids = event.id.split(':');
|
|
409
|
+
const namespace = yield* SERDE.decode(ids[0]);
|
|
410
|
+
const listeners = namespace_listeners.get(namespace);
|
|
411
|
+
if (event.sourceType === ScriptEventSource.Server && listeners) {
|
|
412
|
+
const payload = Payload.fromString(yield* SERDE.decode(ids[1]));
|
|
413
|
+
for (let i = 0; i < listeners.length; i++) {
|
|
414
|
+
yield* listeners[i](payload, event.message);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
})());
|
|
418
|
+
});
|
|
419
|
+
function create_listener(namespace, listener) {
|
|
420
|
+
let listeners = namespace_listeners.get(namespace);
|
|
421
|
+
if (!listeners) {
|
|
422
|
+
listeners = new Array();
|
|
423
|
+
namespace_listeners.set(namespace, listeners);
|
|
424
|
+
}
|
|
425
|
+
listeners.push(listener);
|
|
426
|
+
return () => {
|
|
427
|
+
const idx = listeners.indexOf(listener);
|
|
428
|
+
if (idx !== -1)
|
|
429
|
+
listeners.splice(idx, 1);
|
|
430
|
+
if (listeners.length === 0) {
|
|
431
|
+
namespace_listeners.delete(namespace);
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
let Payload;
|
|
436
|
+
(function (Payload) {
|
|
437
|
+
function toString(p) {
|
|
438
|
+
return JSON.stringify(p);
|
|
439
|
+
}
|
|
440
|
+
Payload.toString = toString;
|
|
441
|
+
function fromString(s) {
|
|
442
|
+
return JSON.parse(s);
|
|
443
|
+
}
|
|
444
|
+
Payload.fromString = fromString;
|
|
445
|
+
})(Payload || (Payload = {}));
|
|
446
|
+
function generate_id() {
|
|
447
|
+
const r = (Math.random() * 0x100000000) >>> 0;
|
|
448
|
+
return ((r & 0xff).toString(16).padStart(2, '0') +
|
|
449
|
+
((r >> 8) & 0xff).toString(16).padStart(2, '0') +
|
|
450
|
+
((r >> 16) & 0xff).toString(16).padStart(2, '0') +
|
|
451
|
+
((r >> 24) & 0xff).toString(16).padStart(2, '0')).toUpperCase();
|
|
452
|
+
}
|
|
453
|
+
function* emit(namespace, channel, args) {
|
|
454
|
+
const id = generate_id();
|
|
455
|
+
const enc_namespace = yield* SERDE.encode(namespace);
|
|
456
|
+
const enc_args_str = yield* SERDE.encode(JSON.stringify(args));
|
|
457
|
+
const RUN = function* (payload, data_str) {
|
|
458
|
+
const enc_payload = yield* SERDE.encode(Payload.toString(payload));
|
|
459
|
+
world.getDimension('overworld').runCommand(`scriptevent ${enc_namespace}:${enc_payload} ${data_str}`);
|
|
460
|
+
};
|
|
461
|
+
let len = 0;
|
|
462
|
+
let str = '';
|
|
463
|
+
let str_size = 0;
|
|
464
|
+
for (let i = 0; i < enc_args_str.length; i++) {
|
|
465
|
+
const char = enc_args_str[i];
|
|
466
|
+
const code = char.charCodeAt(0);
|
|
467
|
+
const char_size = code <= 0x7f ? 1 : code <= 0x7ff ? 2 : code <= 0xffff ? 3 : 4;
|
|
468
|
+
if (str_size + char_size < FRAG_MAX) {
|
|
469
|
+
str += char;
|
|
470
|
+
str_size += char_size;
|
|
471
|
+
}
|
|
472
|
+
else {
|
|
473
|
+
yield* RUN([channel, id, len], str);
|
|
474
|
+
len++;
|
|
475
|
+
str = char;
|
|
476
|
+
str_size = char_size;
|
|
477
|
+
}
|
|
478
|
+
yield;
|
|
479
|
+
}
|
|
480
|
+
yield* RUN(len === 0 ? [channel, id] : [channel, id, len, 1], str);
|
|
481
|
+
}
|
|
482
|
+
NET.emit = emit;
|
|
483
|
+
function listen(namespace, channel, callback) {
|
|
484
|
+
const buffer = new Map();
|
|
485
|
+
const listener = function* ([p_channel, p_id, p_index, p_final], data) {
|
|
486
|
+
if (p_channel === channel) {
|
|
487
|
+
if (p_index === undefined) {
|
|
488
|
+
yield* callback(JSON.parse(yield* SERDE.decode(data)));
|
|
489
|
+
}
|
|
490
|
+
else {
|
|
491
|
+
let fragment = buffer.get(p_id);
|
|
492
|
+
if (!fragment) {
|
|
493
|
+
fragment = { size: -1, data_strs: [], data_size: 0 };
|
|
494
|
+
buffer.set(p_id, fragment);
|
|
495
|
+
}
|
|
496
|
+
if (p_final === 1)
|
|
497
|
+
fragment.size = p_index + 1;
|
|
498
|
+
fragment.data_strs[p_index] = data;
|
|
499
|
+
fragment.data_size += p_index + 1;
|
|
500
|
+
if (fragment.size !== -1 && fragment.data_size === (fragment.size * (fragment.size + 1)) / 2) {
|
|
501
|
+
let full_str = '';
|
|
502
|
+
for (let i = 0; i < fragment.data_strs.length; i++) {
|
|
503
|
+
full_str += fragment.data_strs[i];
|
|
504
|
+
yield;
|
|
505
|
+
}
|
|
506
|
+
yield* callback(JSON.parse(yield* SERDE.decode(full_str)));
|
|
507
|
+
buffer.delete(p_id);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
}
|
|
511
|
+
};
|
|
512
|
+
return create_listener(namespace, listener);
|
|
513
|
+
}
|
|
514
|
+
NET.listen = listen;
|
|
515
|
+
})(NET || (NET = {}));
|
package/dist/ipc.min.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{world,system,ScriptEventSource}from"@minecraft/server";var IPC,SERDE,CRYPTO,NET;(e=>{class _{*MAYBE_ENCRYPT(e){return!1!==this._enc?yield*CRYPTO.encrypt(JSON.stringify(e),this._enc):e}*MAYBE_DECRYPT(e){return!1!==this._enc?JSON.parse(yield*CRYPTO.decrypt(e[1],this._enc)):e[1]}get from(){return this._from}get to(){return this._to}constructor(e,t,n){this._from=e,this._to=t,this._enc=n,this._terminators=new Array}terminate(e=!0){var t=this;t._terminators.forEach(e=>e()),t._terminators.length=0,e&&system.runJob(NET.emit("ipc",t._to+":terminate",[t._from]))}send(t,...n){let i=this;system.runJob(function*(){var e=yield*i.MAYBE_ENCRYPT(n);yield*NET.emit("ipc",i._to+`:${t}:send`,[i._from,e])}())}invoke(i,...t){let r=this;return system.runJob(function*(){var e=yield*r.MAYBE_ENCRYPT(t);yield*NET.emit("ipc",r._to+`:${i}:invoke`,[r._from,e])}()),new Promise(t=>{let n=NET.listen("ipc",r._from+`:${i}:handle`,function*(e){e[0]===r._to&&(e=yield*r.MAYBE_DECRYPT(e),t(e),n())})})}on(e,t){let n=this;e=NET.listen("ipc",n._from+`:${e}:send`,function*(e){e[0]===n._to&&(e=yield*n.MAYBE_DECRYPT(e),t(...e))});return n._terminators.push(e),e}once(e,t){let n=this,i=NET.listen("ipc",n._from+`:${e}:send`,function*(e){e[0]===n._to&&(e=yield*n.MAYBE_DECRYPT(e),t(...e),i())});return n._terminators.push(i),i}handle(t,n){let i=this;var e=NET.listen("ipc",i._from+`:${t}:invoke`,function*(e){e[0]===i._to&&(e=yield*i.MAYBE_DECRYPT(e),e=n(...e),e=yield*i.MAYBE_ENCRYPT([e]),yield*NET.emit("ipc",i._to+`:${t}:handle`,[i._from,e]))});return i._terminators.push(e),e}}e.Connection=_,e.ConnectionManager=class{*MAYBE_ENCRYPT(e,t){return!1!==t?yield*CRYPTO.encrypt(JSON.stringify(e),t):e}*MAYBE_DECRYPT(e,t){return!1!==t?JSON.parse(yield*CRYPTO.decrypt(e[1],t)):e[1]}get id(){return this._id}constructor(e,t=!1){let i=this;this._id=e,this._enc_map=new Map,this._con_map=new Map,this._enc_force=t,NET.listen("ipc",this._id+":handshake:SYN",function*(e){var t=CRYPTO.make_secret(e[4]),n=yield*CRYPTO.make_public(t,e[4],e[3]),t=!(1!==e[1]&&!i._enc_force)&&(yield*CRYPTO.make_shared(t,e[2],e[3]));i._enc_map.set(e[0],t),yield*NET.emit("ipc",e[0]+":handshake:ACK",[i._id,i._enc_force?1:0,n])}),NET.listen("ipc",this._id+":terminate",function*(e){i._enc_map.delete(e[0])})}connect(l,d=!1,c=20){let a=this;return new Promise((r,o)=>{var e=this._con_map.get(l);if(void 0!==e)e.terminate(!1),r(e);else{let t=CRYPTO.make_secret(),n=d?1:0;function s(){i(),system.clearRun(e)}system.runJob(function*(){var e=yield*CRYPTO.make_public(t);yield*NET.emit("ipc",l+":handshake:SYN",[a._id,n,e,CRYPTO.PRIME,CRYPTO.MOD])}());let e=system.runTimeout(()=>{o(),s()},c),i=NET.listen("ipc",this._id+":handshake:ACK",function*(e){e[0]===l&&(e=!(1!==e[1]&&!d)&&(yield*CRYPTO.make_shared(t,e[2])),e=new _(a._id,l,e),a._con_map.set(l,e),r(e),s())})}})}send(n,...i){let r=this;system.runJob(function*(){for(var[e,t]of r._enc_map){t=yield*r.MAYBE_ENCRYPT(i,t);yield*NET.emit("ipc",e+`:${n}:send`,[r._id,t])}}())}invoke(o,...t){let s=this;var e=[];for(let[i,r]of s._enc_map)system.runJob(function*(){var e=yield*s.MAYBE_ENCRYPT(t,r);yield*NET.emit("ipc",i+`:${o}:invoke`,[s._id,e])}()),e.push(new Promise(t=>{let n=NET.listen("ipc",s._id+`:${o}:handle`,function*(e){e[0]===i&&(e=yield*s.MAYBE_DECRYPT(e,r),t(e),n())})}));return e}on(e,n){let i=this;return NET.listen("ipc",i._id+`:${e}:send`,function*(e){var t=i._enc_map.get(e[0]);void 0!==t&&(e=yield*i.MAYBE_DECRYPT(e,t),n(...e))})}once(e,n){let i=this,r=NET.listen("ipc",i._id+`:${e}:send`,function*(e){var t=i._enc_map.get(e[0]);void 0!==t&&(e=yield*i.MAYBE_DECRYPT(e,t),n(...e),r())});return r}handle(i,r){let o=this;return NET.listen("ipc",o._id+`:${i}:invoke`,function*(e){var t,n=o._enc_map.get(e[0]);void 0!==n&&(t=yield*o.MAYBE_DECRYPT(e,n),t=r(...t),t=yield*o.MAYBE_ENCRYPT([t],n),yield*NET.emit("ipc",e[0]+`:${i}:handle`,[o._id,t]))})}},e.send=function(e,...t){system.runJob(NET.emit("ipc",e+":send",t))},e.invoke=function(e,...t){return system.runJob(NET.emit("ipc",e+":invoke",t)),new Promise(t=>{let n=NET.listen("ipc",e+":handle",function*(e){t(e[0]),n()})})},e.on=function(e,t){return NET.listen("ipc",e+":send",function*(e){t(...e)})},e.once=function(e,t){let n=NET.listen("ipc",e+":send",function*(e){t(...e),n()});return n},e.handle=function(t,n){return NET.listen("ipc",t+":invoke",function*(e){e=n(...e);yield*NET.emit("ipc",t+":handle",[e])})}})(IPC=IPC||{});export default IPC;(e=>{let o=[48,49,50,51,52,53,54,55,56,57],s=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,42,43,44,47,58,59,60,61,62,63,64,91,92,93,94,96,123,124,125,126,127],r=/\?[0-9a-zA-Z.\-]{2}|[^?]+/g,l=/^\?[0-9a-zA-Z.\-]{2}$/,d="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-";e.encode=function*(t){let n="";for(let e=0;e<t.length;e++){var i=t.charAt(e),r=i.charCodeAt(0);0===e&&o.includes(r)||s.includes(r)?n+="?"+(yield*function*(t){let n="";for(let e=t.charCodeAt(0);0<e;e=Math.floor(e/64))n=d[e%64]+n,yield;return n}(i)).padStart(2,"0"):n+=i,yield}return n},e.decode=function*(e){let t="";var n=e.match(r)??[];for(let e=0;e<n.length;e++){var i=n[e];i.startsWith("?")&&l.test(i)?t+=yield*function*(t){let n=0;for(let e=0;e<t.length;e++)n+=64**(t.length-1-e)*d.indexOf(t[e]),yield;return String.fromCharCode(n)}(i.slice(1)):t+=i,yield}return t}})(SERDE=SERDE||{}),(i=>{i.PRIME=19893121,i.MOD=341;let r=e=>e.toString(16).toUpperCase();function*o(e,t,n){let i=1,r=e%n;for(let e=t;0<e;e=Math.floor(e/2))e%2==1&&(i=i*r%n),r=r*r%n,yield;return i}i.make_secret=function(e=i.MOD){return Math.floor(Math.random()*(e-1))+1},i.make_public=function*(e,t=i.MOD,n=i.PRIME){return r(yield*o(t,e,n))},i.make_shared=function*(e,t,n=i.PRIME){return r(yield*o(parseInt(t,16),e,n))},i.encrypt=function*(t,n){let i="";for(let e=0;e<t.length;e++)i+=String.fromCharCode(t.charCodeAt(e)^n.charCodeAt(e%n.length)),yield;return i},i.decrypt=function*(t,n){let i="";for(let e=0;e<t.length;e++)i+=String.fromCharCode(t.charCodeAt(e)^n.charCodeAt(e%n.length)),yield;return i}})(CRYPTO=CRYPTO||{}),(e=>{let o=new Map;system.afterEvents.scriptEventReceive.subscribe(r=>{system.runJob(function*(){var e=r.id.split(":"),t=yield*SERDE.decode(e[0]),n=o.get(t);if(r.sourceType===ScriptEventSource.Server&&n){var i=E.fromString(yield*SERDE.decode(e[1]));for(let e=0;e<n.length;e++)yield*n[e](i,r.message)}}())});let E;var t;(t=E=E||{}).toString=function(e){return JSON.stringify(e)},t.fromString=function(e){return JSON.parse(e)},e.emit=function*(e,t,n){var i,r=((255&(i=4294967296*Math.random()>>>0)).toString(16).padStart(2,"0")+(i>>8&255).toString(16).padStart(2,"0")+(i>>16&255).toString(16).padStart(2,"0")+(i>>24&255).toString(16).padStart(2,"0")).toUpperCase();let o=yield*SERDE.encode(e);function*s(e,t){e=yield*SERDE.encode(E.toString(e)),world.getDimension("overworld").runCommand(`scriptevent ${o}:${e} `+t)}var l=yield*SERDE.encode(JSON.stringify(n));let d=0,c="",a=0;for(let e=0;e<l.length;e++){var _=l[e],u=_.charCodeAt(0),u=u<=127?1:u<=2047?2:u<=65535?3:4;a+u<2048?(c+=_,a+=u):(yield*s([t,r,d],c),d++,c=_,a=u),yield}yield*s(0===d?[t,r]:[t,r,d,1],c)},e.listen=function(e,n,s){let l=new Map;{var i=e,r=function*([e,i,t,r],o){if(e===n)if(void 0===t)yield*s(JSON.parse(yield*SERDE.decode(o)));else{let n=l.get(i);if(n||(n={size:-1,data_strs:[],data_size:0},l.set(i,n)),1===r&&(n.size=t+1),n.data_strs[t]=o,n.data_size+=t+1,-1!==n.size&&n.data_size===n.size*(n.size+1)/2){let t="";for(let e=0;e<n.data_strs.length;e++)t+=n.data_strs[e],yield;yield*s(JSON.parse(yield*SERDE.decode(t))),l.delete(i)}}};let t=o.get(i);return t||(t=new Array,o.set(i,t)),t.push(r),()=>{var e=t.indexOf(r);-1!==e&&t.splice(e,1),0===t.length&&o.delete(i)}}}})(NET=NET||{});export{NET};
|
package/package.json
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "mcbe-ipc",
|
|
3
|
+
"author": "OmniacDev",
|
|
4
|
+
"description": "IPC system for MCBE Script API projects",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"version": "2.0.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "dist/ipc.js",
|
|
9
|
+
"types": "dist/ipc.d.ts",
|
|
10
|
+
"files": ["dist"],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc && uglifyjs dist/ipc.js --mangle --compress --output dist/ipc.min.js",
|
|
13
|
+
"format": "prettier --write src",
|
|
14
|
+
"build-clean": "tsc",
|
|
15
|
+
"prepublishOnly": "npm run build-clean"
|
|
16
|
+
},
|
|
17
|
+
"devDependencies": {
|
|
18
|
+
"prettier": "^3.3.3",
|
|
19
|
+
"typescript": "^5.5.4",
|
|
20
|
+
"uglify-js": "^3.19.3"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@minecraft/server": "^1.13.0"
|
|
24
|
+
}
|
|
25
|
+
}
|