@tsukilabs/nil-ffi 0.7.0 → 0.7.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/index.js CHANGED
@@ -1,7 +1,22 @@
1
1
  import * as ffi from "node:ffi";
2
+ import { EventEmitter } from "node:events";
2
3
  import { isNil } from "es-toolkit/predicate";
3
4
  import * as process from "node:process";
5
+ import { ffi_Status } from "@tsukilabs/nil-bindings";
6
+ //#region src/request.ts
7
+ //! Copyright (C) Call of Nil contributors
8
+ //! SPDX-License-Identifier: AGPL-3.0-only
9
+ var RequestId = class {
10
+ #current = 0;
11
+ next() {
12
+ if (this.#current >= 4294967295) this.#current = 0;
13
+ return ++this.#current;
14
+ }
15
+ };
16
+ //#endregion
4
17
  //#region src/ptr.ts
18
+ //! Copyright (C) Call of Nil contributors
19
+ //! SPDX-License-Identifier: AGPL-3.0-only
5
20
  function allocBuffer(size) {
6
21
  return Buffer.alloc(size ?? pointerSize());
7
22
  }
@@ -14,20 +29,10 @@ function pointerSize() {
14
29
  else return 4;
15
30
  }
16
31
  //#endregion
17
- //#region ../bindings/dist/index.js
18
- var ffi_Status = /* @__PURE__ */ function(ffi_Status) {
19
- ffi_Status[ffi_Status["OK"] = 0] = "OK";
20
- ffi_Status[ffi_Status["ERR_NULL_POINTER"] = 1] = "ERR_NULL_POINTER";
21
- ffi_Status[ffi_Status["ERR_NOTHING_TO_POLL"] = 2] = "ERR_NOTHING_TO_POLL";
22
- ffi_Status[ffi_Status["ERR_SERIALIZATION"] = 3] = "ERR_SERIALIZATION";
23
- ffi_Status[ffi_Status["ERR_UNKNOWN"] = 2147483647] = "ERR_UNKNOWN";
24
- return ffi_Status;
25
- }({});
32
+ //#region src/error.ts
26
33
  //! Copyright (C) Call of Nil contributors
27
34
  //! SPDX-License-Identifier: AGPL-3.0-only
28
- //#endregion
29
- //#region src/error.ts
30
- var FfiError = class FfiError extends Error {
35
+ var NilError = class NilError extends Error {
31
36
  constructor(message, options) {
32
37
  const status = options?.status ?? ffi_Status.ERR_UNKNOWN;
33
38
  if (!message && status !== ffi_Status.OK) {
@@ -38,11 +43,11 @@ var FfiError = class FfiError extends Error {
38
43
  }
39
44
  message ??= "ERR_UNKNOWN";
40
45
  super(message, options);
41
- this.name = "FfiError";
46
+ this.name = "NilError";
42
47
  this.status = status;
43
48
  }
44
49
  static fromStatus(status) {
45
- return new FfiError(null, { status });
50
+ return new NilError(null, { status });
46
51
  }
47
52
  };
48
53
  var HandleClosedError = class extends Error {
@@ -51,8 +56,25 @@ var HandleClosedError = class extends Error {
51
56
  this.name = "HandleClosedError";
52
57
  }
53
58
  };
59
+ var UnknownResponseError = class extends Error {
60
+ constructor(response, options) {
61
+ super(`got unknown response with id ${response.id}`, options);
62
+ this.name = "UnknownResponseError";
63
+ this.response = response;
64
+ }
65
+ };
54
66
  //#endregion
55
67
  //#region src/queue.ts
68
+ //! Copyright (C) Call of Nil contributors
69
+ //! SPDX-License-Identifier: AGPL-3.0-only
70
+ var QueueEmitter = class extends EventEmitter {
71
+ emit(event, ...args) {
72
+ return super.emit(event, ...args);
73
+ }
74
+ on(event, listener) {
75
+ return super.on(event, listener);
76
+ }
77
+ };
56
78
  var Queue = class Queue {
57
79
  static {
58
80
  this.POLL_INTERVAL = 16;
@@ -60,79 +82,101 @@ var Queue = class Queue {
60
82
  static {
61
83
  this.LIMIT_PER_TICK = 200;
62
84
  }
63
- constructor(handle) {
64
- this.pending = /* @__PURE__ */ new Map();
85
+ constructor(handle, options) {
65
86
  this.disposed = false;
87
+ this.emitter = new QueueEmitter();
88
+ this.requestId = new RequestId();
89
+ this.pendingRequests = /* @__PURE__ */ new Map();
66
90
  this.functions = handle.functions;
91
+ this.errorHandler = options?.onError ?? console.error;
92
+ this.emitter.on("error", this.errorHandler);
93
+ this.emitter.on("response", this.onResponse.bind(this));
94
+ this.timer = setTimeout(() => this.drain(), 0);
67
95
  }
68
96
  [Symbol.dispose]() {
69
97
  if (this.disposed) return;
70
98
  this.disposed = true;
71
99
  this.rejectAll(new HandleClosedError());
100
+ this.emitter.removeAllListeners();
72
101
  }
73
102
  request(start) {
74
103
  if (this.disposed) return Promise.reject(new HandleClosedError());
104
+ const requestId = this.requestId.next();
75
105
  return new Promise((resolve, reject) => {
76
- let id;
106
+ this.pendingRequests.set(requestId, {
107
+ resolve,
108
+ reject
109
+ });
77
110
  try {
78
- id = start();
111
+ start(requestId);
79
112
  } catch (err) {
113
+ this.pendingRequests.delete(requestId);
80
114
  reject(err);
81
- return;
82
115
  }
83
- this.pending.set(id, {
84
- resolve,
85
- reject
86
- });
87
- this.startPolling();
88
116
  });
89
117
  }
90
- startPolling() {
91
- if (isNil(this.timer) && !this.disposed) this.timer = setTimeout(() => this.poll(), 0);
92
- }
93
- poll() {
94
- this.timer = null;
95
- if (!this.disposed && this.pending.size > 0) try {
96
- for (let i = 0; i < Queue.LIMIT_PER_TICK; i++) if (!this.pollOnce()) break;
118
+ drain() {
119
+ if (this.disposed) return;
120
+ this.clearTimeout();
121
+ try {
122
+ for (let i = 0; i < Queue.LIMIT_PER_TICK; i++) if (!this.poll()) break;
97
123
  } catch (err) {
98
- this.rejectAll(err);
99
- return;
124
+ this.emitter.emit("error", err);
100
125
  }
101
- if (!this.disposed && this.pending.size > 0) this.timer = setTimeout(() => this.poll(), Queue.POLL_INTERVAL);
126
+ this.timer = setTimeout(() => this.drain(), Queue.POLL_INTERVAL);
102
127
  }
103
- pollOnce() {
128
+ poll() {
104
129
  const out = allocBuffer();
105
130
  const status = this.functions.nil_ffi_poll(out);
106
131
  if (status === ffi_Status.ERR_NOTHING_TO_POLL) return false;
107
- if (status !== ffi_Status.OK) throw FfiError.fromStatus(status);
132
+ if (status !== ffi_Status.OK) throw NilError.fromStatus(status);
108
133
  const ptr = readBufferPtr(out);
109
- if (ptr === 0n) throw new Error("unexpected null pointer");
110
- let json;
134
+ if (ptr === 0n) throw new Error("failed to poll: unexpected null pointer");
111
135
  try {
112
- json = ffi.toString(ptr);
136
+ const json = ffi.toString(ptr) ?? "";
137
+ const entry = JSON.parse(json);
138
+ this.dispatch(entry);
113
139
  } finally {
114
140
  this.functions.nil_ffi_free_str(ptr);
115
141
  }
116
- const entry = JSON.parse(json);
117
- const response = JSON.parse(entry.json_str);
118
- const pending = this.pending.get(response.id);
119
- if (!pending) return true;
120
- this.pending.delete(response.id);
121
- if (response.kind === "ok") pending.resolve(response.data);
122
- else pending.reject(new FfiError(response.error, { status: response.status }));
123
142
  return true;
124
143
  }
144
+ dispatch(entry) {
145
+ switch (entry.kind) {
146
+ case "response": {
147
+ const response = JSON.parse(entry.json_str);
148
+ this.emitter.emit("response", response);
149
+ break;
150
+ }
151
+ }
152
+ }
125
153
  rejectAll(error) {
154
+ this.clearTimeout();
155
+ this.pendingRequests.values().forEach(({ reject }) => reject(error));
156
+ this.pendingRequests.clear();
157
+ }
158
+ clearTimeout() {
126
159
  if (!isNil(this.timer)) {
127
160
  clearTimeout(this.timer);
128
161
  this.timer = null;
129
162
  }
130
- for (const { reject } of this.pending.values()) reject(error);
131
- this.pending.clear();
163
+ }
164
+ onResponse(response) {
165
+ const pending = this.pendingRequests.get(response.id);
166
+ if (pending) {
167
+ this.pendingRequests.delete(response.id);
168
+ if (response.kind === "ok") pending.resolve(response.data);
169
+ else {
170
+ const { status, error } = response;
171
+ pending.reject(new NilError(error, { status }));
172
+ }
173
+ } else this.emitter.emit("error", new UnknownResponseError(response));
132
174
  }
133
175
  };
134
176
  //#endregion
135
177
  //#region src/def.ts
178
+ //! Copyright (C) Call of Nil contributors
179
+ //! SPDX-License-Identifier: AGPL-3.0-only
136
180
  var definitions = {
137
181
  nil_ffi_free_str: {
138
182
  arguments: [ffi.types.POINTER],
@@ -147,551 +191,556 @@ var definitions = {
147
191
  return: ffi.types.VOID
148
192
  },
149
193
  nil_ffi_version: {
150
- arguments: [],
151
- return: ffi.types.UINT_32
194
+ arguments: [ffi.types.UINT_32],
195
+ return: ffi.types.VOID
152
196
  },
153
197
  nil_client_version: {
154
- arguments: [],
155
- return: ffi.types.UINT_32
198
+ arguments: [ffi.types.UINT_32],
199
+ return: ffi.types.VOID
156
200
  },
157
201
  nil_is_local: {
158
- arguments: [],
159
- return: ffi.types.UINT_32
202
+ arguments: [ffi.types.UINT_32],
203
+ return: ffi.types.VOID
160
204
  },
161
205
  nil_is_ready: {
162
- arguments: [],
163
- return: ffi.types.UINT_32
206
+ arguments: [ffi.types.UINT_32],
207
+ return: ffi.types.VOID
164
208
  },
165
209
  nil_is_remote: {
166
- arguments: [],
167
- return: ffi.types.UINT_32
210
+ arguments: [ffi.types.UINT_32],
211
+ return: ffi.types.VOID
168
212
  },
169
213
  nil_server_addr: {
170
- arguments: [],
171
- return: ffi.types.UINT_32
214
+ arguments: [ffi.types.UINT_32],
215
+ return: ffi.types.VOID
172
216
  },
173
217
  nil_set_user_agent: {
174
- arguments: [ffi.types.POINTER],
175
- return: ffi.types.UINT_32
218
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
219
+ return: ffi.types.VOID
176
220
  },
177
221
  nil_stop_client: {
178
- arguments: [],
179
- return: ffi.types.UINT_32
222
+ arguments: [ffi.types.UINT_32],
223
+ return: ffi.types.VOID
180
224
  },
181
225
  nil_update_client: {
182
- arguments: [ffi.types.POINTER],
183
- return: ffi.types.UINT_32
226
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
227
+ return: ffi.types.VOID
184
228
  },
185
229
  nil_user_agent: {
186
- arguments: [],
187
- return: ffi.types.UINT_32
230
+ arguments: [ffi.types.UINT_32],
231
+ return: ffi.types.VOID
188
232
  },
189
233
  nil_world: {
190
- arguments: [],
191
- return: ffi.types.UINT_32
234
+ arguments: [ffi.types.UINT_32],
235
+ return: ffi.types.VOID
192
236
  },
193
237
  nil_add_academy_recruit_order: {
194
- arguments: [ffi.types.POINTER],
195
- return: ffi.types.UINT_32
238
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
239
+ return: ffi.types.VOID
196
240
  },
197
241
  nil_add_prefecture_build_order: {
198
- arguments: [ffi.types.POINTER],
199
- return: ffi.types.UINT_32
242
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
243
+ return: ffi.types.VOID
200
244
  },
201
245
  nil_add_stable_recruit_order: {
202
- arguments: [ffi.types.POINTER],
203
- return: ffi.types.UINT_32
246
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
247
+ return: ffi.types.VOID
204
248
  },
205
249
  nil_add_workshop_recruit_order: {
206
- arguments: [ffi.types.POINTER],
207
- return: ffi.types.UINT_32
250
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
251
+ return: ffi.types.VOID
208
252
  },
209
253
  nil_authorize: {
210
- arguments: [ffi.types.POINTER],
211
- return: ffi.types.UINT_32
254
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
255
+ return: ffi.types.VOID
212
256
  },
213
257
  nil_cancel_academy_recruit_order: {
214
- arguments: [ffi.types.POINTER],
215
- return: ffi.types.UINT_32
258
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
259
+ return: ffi.types.VOID
216
260
  },
217
261
  nil_cancel_maneuver: {
218
- arguments: [ffi.types.POINTER],
219
- return: ffi.types.UINT_32
262
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
263
+ return: ffi.types.VOID
220
264
  },
221
265
  nil_cancel_prefecture_build_order: {
222
- arguments: [ffi.types.POINTER],
223
- return: ffi.types.UINT_32
266
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
267
+ return: ffi.types.VOID
224
268
  },
225
269
  nil_cancel_stable_recruit_order: {
226
- arguments: [ffi.types.POINTER],
227
- return: ffi.types.UINT_32
270
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
271
+ return: ffi.types.VOID
228
272
  },
229
273
  nil_cancel_workshop_recruit_order: {
230
- arguments: [ffi.types.POINTER],
231
- return: ffi.types.UINT_32
274
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
275
+ return: ffi.types.VOID
232
276
  },
233
277
  nil_cheat_fill_world: {
234
- arguments: [ffi.types.POINTER],
235
- return: ffi.types.UINT_32
278
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
279
+ return: ffi.types.VOID
236
280
  },
237
281
  nil_cheat_get_academy_recruit_queue: {
238
- arguments: [ffi.types.POINTER],
239
- return: ffi.types.UINT_32
282
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
283
+ return: ffi.types.VOID
240
284
  },
241
285
  nil_cheat_get_academy_recruit_queues: {
242
- arguments: [ffi.types.POINTER],
243
- return: ffi.types.UINT_32
286
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
287
+ return: ffi.types.VOID
244
288
  },
245
289
  nil_cheat_get_all_academy_recruit_queues: {
246
- arguments: [ffi.types.POINTER],
247
- return: ffi.types.UINT_32
290
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
291
+ return: ffi.types.VOID
248
292
  },
249
293
  nil_cheat_get_all_prefecture_build_queues: {
250
- arguments: [ffi.types.POINTER],
251
- return: ffi.types.UINT_32
294
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
295
+ return: ffi.types.VOID
252
296
  },
253
297
  nil_cheat_get_all_stable_recruit_queues: {
254
- arguments: [ffi.types.POINTER],
255
- return: ffi.types.UINT_32
298
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
299
+ return: ffi.types.VOID
256
300
  },
257
301
  nil_cheat_get_build_steps: {
258
- arguments: [ffi.types.POINTER],
259
- return: ffi.types.UINT_32
302
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
303
+ return: ffi.types.VOID
260
304
  },
261
305
  nil_cheat_get_cities: {
262
- arguments: [ffi.types.POINTER],
263
- return: ffi.types.UINT_32
306
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
307
+ return: ffi.types.VOID
264
308
  },
265
309
  nil_cheat_get_city: {
266
- arguments: [ffi.types.POINTER],
267
- return: ffi.types.UINT_32
310
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
311
+ return: ffi.types.VOID
268
312
  },
269
313
  nil_cheat_get_ethics: {
270
- arguments: [ffi.types.POINTER],
271
- return: ffi.types.UINT_32
314
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
315
+ return: ffi.types.VOID
272
316
  },
273
317
  nil_cheat_get_idle_armies_at: {
274
- arguments: [ffi.types.POINTER],
275
- return: ffi.types.UINT_32
318
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
319
+ return: ffi.types.VOID
276
320
  },
277
321
  nil_cheat_get_idle_personnel_at: {
278
- arguments: [ffi.types.POINTER],
279
- return: ffi.types.UINT_32
322
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
323
+ return: ffi.types.VOID
280
324
  },
281
325
  nil_cheat_get_infrastructure: {
282
- arguments: [ffi.types.POINTER],
283
- return: ffi.types.UINT_32
326
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
327
+ return: ffi.types.VOID
284
328
  },
285
329
  nil_cheat_get_maneuvers: {
286
- arguments: [ffi.types.POINTER],
287
- return: ffi.types.UINT_32
330
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
331
+ return: ffi.types.VOID
288
332
  },
289
333
  nil_cheat_get_maneuvers_of: {
290
- arguments: [ffi.types.POINTER],
291
- return: ffi.types.UINT_32
334
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
335
+ return: ffi.types.VOID
292
336
  },
293
337
  nil_cheat_get_player: {
294
- arguments: [ffi.types.POINTER],
295
- return: ffi.types.UINT_32
338
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
339
+ return: ffi.types.VOID
296
340
  },
297
341
  nil_cheat_get_players: {
298
- arguments: [ffi.types.POINTER],
299
- return: ffi.types.UINT_32
342
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
343
+ return: ffi.types.VOID
300
344
  },
301
345
  nil_cheat_get_prefecture_build_queue: {
302
- arguments: [ffi.types.POINTER],
303
- return: ffi.types.UINT_32
346
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
347
+ return: ffi.types.VOID
304
348
  },
305
349
  nil_cheat_get_prefecture_build_queues: {
306
- arguments: [ffi.types.POINTER],
307
- return: ffi.types.UINT_32
350
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
351
+ return: ffi.types.VOID
308
352
  },
309
353
  nil_cheat_get_resources: {
310
- arguments: [ffi.types.POINTER],
311
- return: ffi.types.UINT_32
354
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
355
+ return: ffi.types.VOID
312
356
  },
313
357
  nil_cheat_get_stable_recruit_queue: {
314
- arguments: [ffi.types.POINTER],
315
- return: ffi.types.UINT_32
358
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
359
+ return: ffi.types.VOID
316
360
  },
317
361
  nil_cheat_get_stable_recruit_queues: {
318
- arguments: [ffi.types.POINTER],
319
- return: ffi.types.UINT_32
362
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
363
+ return: ffi.types.VOID
320
364
  },
321
365
  nil_cheat_get_storage_capacity: {
322
- arguments: [ffi.types.POINTER],
323
- return: ffi.types.UINT_32
366
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
367
+ return: ffi.types.VOID
324
368
  },
325
369
  nil_cheat_set_bot_ethics: {
326
- arguments: [ffi.types.POINTER],
327
- return: ffi.types.UINT_32
370
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
371
+ return: ffi.types.VOID
328
372
  },
329
373
  nil_cheat_set_building_level: {
330
- arguments: [ffi.types.POINTER],
331
- return: ffi.types.UINT_32
374
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
375
+ return: ffi.types.VOID
332
376
  },
333
377
  nil_cheat_set_food: {
334
- arguments: [ffi.types.POINTER],
335
- return: ffi.types.UINT_32
378
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
379
+ return: ffi.types.VOID
336
380
  },
337
381
  nil_cheat_set_iron: {
338
- arguments: [ffi.types.POINTER],
339
- return: ffi.types.UINT_32
382
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
383
+ return: ffi.types.VOID
340
384
  },
341
385
  nil_cheat_set_max_food: {
342
- arguments: [ffi.types.POINTER],
343
- return: ffi.types.UINT_32
386
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
387
+ return: ffi.types.VOID
344
388
  },
345
389
  nil_cheat_set_max_infrastructure: {
346
- arguments: [ffi.types.POINTER],
347
- return: ffi.types.UINT_32
390
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
391
+ return: ffi.types.VOID
348
392
  },
349
393
  nil_cheat_set_max_iron: {
350
- arguments: [ffi.types.POINTER],
351
- return: ffi.types.UINT_32
394
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
395
+ return: ffi.types.VOID
352
396
  },
353
397
  nil_cheat_set_max_resources: {
354
- arguments: [ffi.types.POINTER],
355
- return: ffi.types.UINT_32
398
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
399
+ return: ffi.types.VOID
356
400
  },
357
401
  nil_cheat_set_max_silo_resources: {
358
- arguments: [ffi.types.POINTER],
359
- return: ffi.types.UINT_32
402
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
403
+ return: ffi.types.VOID
360
404
  },
361
405
  nil_cheat_set_max_stone: {
362
- arguments: [ffi.types.POINTER],
363
- return: ffi.types.UINT_32
406
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
407
+ return: ffi.types.VOID
364
408
  },
365
409
  nil_cheat_set_max_warehouse_resources: {
366
- arguments: [ffi.types.POINTER],
367
- return: ffi.types.UINT_32
410
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
411
+ return: ffi.types.VOID
368
412
  },
369
413
  nil_cheat_set_max_wood: {
370
- arguments: [ffi.types.POINTER],
371
- return: ffi.types.UINT_32
414
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
415
+ return: ffi.types.VOID
372
416
  },
373
417
  nil_cheat_set_resources: {
374
- arguments: [ffi.types.POINTER],
375
- return: ffi.types.UINT_32
418
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
419
+ return: ffi.types.VOID
376
420
  },
377
421
  nil_cheat_set_stability: {
378
- arguments: [ffi.types.POINTER],
379
- return: ffi.types.UINT_32
422
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
423
+ return: ffi.types.VOID
380
424
  },
381
425
  nil_cheat_set_stone: {
382
- arguments: [ffi.types.POINTER],
383
- return: ffi.types.UINT_32
426
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
427
+ return: ffi.types.VOID
384
428
  },
385
429
  nil_cheat_set_wood: {
386
- arguments: [ffi.types.POINTER],
387
- return: ffi.types.UINT_32
430
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
431
+ return: ffi.types.VOID
388
432
  },
389
433
  nil_cheat_skip_round: {
390
- arguments: [ffi.types.POINTER],
391
- return: ffi.types.UINT_32
434
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
435
+ return: ffi.types.VOID
392
436
  },
393
437
  nil_cheat_spawn_bot: {
394
- arguments: [ffi.types.POINTER],
395
- return: ffi.types.UINT_32
438
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
439
+ return: ffi.types.VOID
396
440
  },
397
441
  nil_cheat_spawn_city: {
398
- arguments: [ffi.types.POINTER],
399
- return: ffi.types.UINT_32
442
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
443
+ return: ffi.types.VOID
400
444
  },
401
445
  nil_cheat_spawn_personnel: {
402
- arguments: [ffi.types.POINTER],
403
- return: ffi.types.UINT_32
446
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
447
+ return: ffi.types.VOID
404
448
  },
405
449
  nil_create_remote_world: {
406
- arguments: [ffi.types.POINTER],
407
- return: ffi.types.UINT_32
450
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
451
+ return: ffi.types.VOID
408
452
  },
409
453
  nil_create_user: {
410
- arguments: [ffi.types.POINTER],
411
- return: ffi.types.UINT_32
454
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
455
+ return: ffi.types.VOID
412
456
  },
413
457
  nil_delete_remote_world: {
414
- arguments: [ffi.types.POINTER],
415
- return: ffi.types.UINT_32
458
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
459
+ return: ffi.types.VOID
416
460
  },
417
461
  nil_forward_report: {
418
- arguments: [ffi.types.POINTER],
419
- return: ffi.types.UINT_32
462
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
463
+ return: ffi.types.VOID
420
464
  },
421
465
  nil_get_academy_recruit_catalog: {
422
- arguments: [ffi.types.POINTER],
423
- return: ffi.types.UINT_32
466
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
467
+ return: ffi.types.VOID
424
468
  },
425
469
  nil_get_armies: {
426
- arguments: [ffi.types.POINTER],
427
- return: ffi.types.UINT_32
470
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
471
+ return: ffi.types.VOID
428
472
  },
429
473
  nil_get_army: {
430
- arguments: [ffi.types.POINTER],
431
- return: ffi.types.UINT_32
474
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
475
+ return: ffi.types.VOID
432
476
  },
433
477
  nil_get_army_owner: {
434
- arguments: [ffi.types.POINTER],
435
- return: ffi.types.UINT_32
478
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
479
+ return: ffi.types.VOID
436
480
  },
437
481
  nil_get_bot_coords: {
438
- arguments: [ffi.types.POINTER],
439
- return: ffi.types.UINT_32
482
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
483
+ return: ffi.types.VOID
440
484
  },
441
485
  nil_get_chat_history: {
442
- arguments: [ffi.types.POINTER],
443
- return: ffi.types.UINT_32
486
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
487
+ return: ffi.types.VOID
444
488
  },
445
489
  nil_get_cities: {
446
- arguments: [ffi.types.POINTER],
447
- return: ffi.types.UINT_32
490
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
491
+ return: ffi.types.VOID
448
492
  },
449
493
  nil_get_city: {
450
- arguments: [ffi.types.POINTER],
451
- return: ffi.types.UINT_32
494
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
495
+ return: ffi.types.VOID
452
496
  },
453
497
  nil_get_city_score: {
454
- arguments: [ffi.types.POINTER],
455
- return: ffi.types.UINT_32
498
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
499
+ return: ffi.types.VOID
456
500
  },
457
501
  nil_get_continent_size: {
458
- arguments: [ffi.types.POINTER],
459
- return: ffi.types.UINT_32
502
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
503
+ return: ffi.types.VOID
460
504
  },
461
505
  nil_get_idle_armies_at: {
462
- arguments: [ffi.types.POINTER],
463
- return: ffi.types.UINT_32
506
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
507
+ return: ffi.types.VOID
464
508
  },
465
509
  nil_get_idle_armies_coords: {
466
- arguments: [ffi.types.POINTER],
467
- return: ffi.types.UINT_32
510
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
511
+ return: ffi.types.VOID
468
512
  },
469
513
  nil_get_maneuver: {
470
- arguments: [ffi.types.POINTER],
471
- return: ffi.types.UINT_32
514
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
515
+ return: ffi.types.VOID
472
516
  },
473
517
  nil_get_player: {
474
- arguments: [ffi.types.POINTER],
475
- return: ffi.types.UINT_32
518
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
519
+ return: ffi.types.VOID
476
520
  },
477
521
  nil_get_player_coords: {
478
- arguments: [ffi.types.POINTER],
479
- return: ffi.types.UINT_32
522
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
523
+ return: ffi.types.VOID
480
524
  },
481
525
  nil_get_player_ids: {
482
- arguments: [ffi.types.POINTER],
483
- return: ffi.types.UINT_32
526
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
527
+ return: ffi.types.VOID
484
528
  },
485
529
  nil_get_player_maintenance: {
486
- arguments: [ffi.types.POINTER],
487
- return: ffi.types.UINT_32
530
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
531
+ return: ffi.types.VOID
488
532
  },
489
533
  nil_get_player_military: {
490
- arguments: [ffi.types.POINTER],
491
- return: ffi.types.UINT_32
534
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
535
+ return: ffi.types.VOID
492
536
  },
493
537
  nil_get_player_status: {
494
- arguments: [ffi.types.POINTER],
495
- return: ffi.types.UINT_32
538
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
539
+ return: ffi.types.VOID
496
540
  },
497
541
  nil_get_player_storage_capacity: {
498
- arguments: [ffi.types.POINTER],
499
- return: ffi.types.UINT_32
542
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
543
+ return: ffi.types.VOID
500
544
  },
501
545
  nil_get_player_worlds: {
502
- arguments: [ffi.types.POINTER],
503
- return: ffi.types.UINT_32
546
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
547
+ return: ffi.types.VOID
504
548
  },
505
549
  nil_get_precursor_coords: {
506
- arguments: [ffi.types.POINTER],
507
- return: ffi.types.UINT_32
550
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
551
+ return: ffi.types.VOID
508
552
  },
509
553
  nil_get_prefecture_build_catalog: {
510
- arguments: [ffi.types.POINTER],
511
- return: ffi.types.UINT_32
554
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
555
+ return: ffi.types.VOID
512
556
  },
513
557
  nil_get_public_bot: {
514
- arguments: [ffi.types.POINTER],
515
- return: ffi.types.UINT_32
558
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
559
+ return: ffi.types.VOID
516
560
  },
517
561
  nil_get_public_bots: {
518
- arguments: [ffi.types.POINTER],
519
- return: ffi.types.UINT_32
562
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
563
+ return: ffi.types.VOID
520
564
  },
521
565
  nil_get_public_cities: {
522
- arguments: [ffi.types.POINTER],
523
- return: ffi.types.UINT_32
566
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
567
+ return: ffi.types.VOID
524
568
  },
525
569
  nil_get_public_city: {
526
- arguments: [ffi.types.POINTER],
527
- return: ffi.types.UINT_32
570
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
571
+ return: ffi.types.VOID
528
572
  },
529
573
  nil_get_public_field: {
530
- arguments: [ffi.types.POINTER],
531
- return: ffi.types.UINT_32
574
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
575
+ return: ffi.types.VOID
532
576
  },
533
577
  nil_get_public_fields: {
534
- arguments: [ffi.types.POINTER],
535
- return: ffi.types.UINT_32
578
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
579
+ return: ffi.types.VOID
536
580
  },
537
581
  nil_get_public_player: {
538
- arguments: [ffi.types.POINTER],
539
- return: ffi.types.UINT_32
582
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
583
+ return: ffi.types.VOID
540
584
  },
541
585
  nil_get_public_players: {
542
- arguments: [ffi.types.POINTER],
543
- return: ffi.types.UINT_32
586
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
587
+ return: ffi.types.VOID
544
588
  },
545
589
  nil_get_public_precursor: {
546
- arguments: [ffi.types.POINTER],
547
- return: ffi.types.UINT_32
590
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
591
+ return: ffi.types.VOID
548
592
  },
549
593
  nil_get_public_precursors: {
550
- arguments: [ffi.types.POINTER],
551
- return: ffi.types.UINT_32
594
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
595
+ return: ffi.types.VOID
552
596
  },
553
597
  nil_get_rank: {
554
- arguments: [ffi.types.POINTER],
555
- return: ffi.types.UINT_32
598
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
599
+ return: ffi.types.VOID
556
600
  },
557
601
  nil_get_ranking: {
558
- arguments: [ffi.types.POINTER],
559
- return: ffi.types.UINT_32
602
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
603
+ return: ffi.types.VOID
560
604
  },
561
605
  nil_get_remote_world: {
562
- arguments: [ffi.types.POINTER],
563
- return: ffi.types.UINT_32
606
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
607
+ return: ffi.types.VOID
564
608
  },
565
609
  nil_get_remote_world_limit: {
566
- arguments: [],
567
- return: ffi.types.UINT_32
610
+ arguments: [ffi.types.UINT_32],
611
+ return: ffi.types.VOID
568
612
  },
569
613
  nil_get_remote_world_limit_per_user: {
570
- arguments: [],
571
- return: ffi.types.UINT_32
614
+ arguments: [ffi.types.UINT_32],
615
+ return: ffi.types.VOID
572
616
  },
573
617
  nil_get_remote_worlds: {
574
- arguments: [],
575
- return: ffi.types.UINT_32
618
+ arguments: [ffi.types.UINT_32],
619
+ return: ffi.types.VOID
576
620
  },
577
621
  nil_get_round: {
578
- arguments: [ffi.types.POINTER],
579
- return: ffi.types.UINT_32
622
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
623
+ return: ffi.types.VOID
580
624
  },
581
625
  nil_get_server_kind: {
582
- arguments: [],
583
- return: ffi.types.UINT_32
626
+ arguments: [ffi.types.UINT_32],
627
+ return: ffi.types.VOID
584
628
  },
585
629
  nil_get_stable_recruit_catalog: {
586
- arguments: [ffi.types.POINTER],
587
- return: ffi.types.UINT_32
630
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
631
+ return: ffi.types.VOID
588
632
  },
589
633
  nil_get_workshop_recruit_catalog: {
590
- arguments: [ffi.types.POINTER],
591
- return: ffi.types.UINT_32
634
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
635
+ return: ffi.types.VOID
592
636
  },
593
637
  nil_get_world_bots: {
594
- arguments: [ffi.types.POINTER],
595
- return: ffi.types.UINT_32
638
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
639
+ return: ffi.types.VOID
596
640
  },
597
641
  nil_get_world_config: {
598
- arguments: [ffi.types.POINTER],
599
- return: ffi.types.UINT_32
642
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
643
+ return: ffi.types.VOID
600
644
  },
601
645
  nil_get_world_personnel: {
602
- arguments: [ffi.types.POINTER],
603
- return: ffi.types.UINT_32
646
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
647
+ return: ffi.types.VOID
604
648
  },
605
649
  nil_get_world_players: {
606
- arguments: [ffi.types.POINTER],
607
- return: ffi.types.UINT_32
650
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
651
+ return: ffi.types.VOID
608
652
  },
609
653
  nil_get_world_precursors: {
610
- arguments: [ffi.types.POINTER],
611
- return: ffi.types.UINT_32
654
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
655
+ return: ffi.types.VOID
612
656
  },
613
657
  nil_get_world_stats: {
614
- arguments: [ffi.types.POINTER],
615
- return: ffi.types.UINT_32
658
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
659
+ return: ffi.types.VOID
616
660
  },
617
661
  nil_player_exists: {
618
- arguments: [ffi.types.POINTER],
619
- return: ffi.types.UINT_32
662
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
663
+ return: ffi.types.VOID
620
664
  },
621
665
  nil_push_chat_message: {
622
- arguments: [ffi.types.POINTER],
623
- return: ffi.types.UINT_32
666
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
667
+ return: ffi.types.VOID
624
668
  },
625
669
  nil_rename_city: {
626
- arguments: [ffi.types.POINTER],
627
- return: ffi.types.UINT_32
670
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
671
+ return: ffi.types.VOID
628
672
  },
629
673
  nil_request_maneuver: {
630
- arguments: [ffi.types.POINTER],
631
- return: ffi.types.UINT_32
674
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
675
+ return: ffi.types.VOID
632
676
  },
633
677
  nil_save_local_world: {
634
- arguments: [ffi.types.POINTER],
635
- return: ffi.types.UINT_32
678
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
679
+ return: ffi.types.VOID
636
680
  },
637
681
  nil_search_city: {
638
- arguments: [ffi.types.POINTER],
639
- return: ffi.types.UINT_32
682
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
683
+ return: ffi.types.VOID
640
684
  },
641
685
  nil_search_public_city: {
642
- arguments: [ffi.types.POINTER],
643
- return: ffi.types.UINT_32
686
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
687
+ return: ffi.types.VOID
644
688
  },
645
689
  nil_server_version: {
646
- arguments: [],
647
- return: ffi.types.UINT_32
690
+ arguments: [ffi.types.UINT_32],
691
+ return: ffi.types.VOID
648
692
  },
649
693
  nil_set_player_ready: {
650
- arguments: [ffi.types.POINTER],
651
- return: ffi.types.UINT_32
694
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
695
+ return: ffi.types.VOID
652
696
  },
653
697
  nil_set_player_status: {
654
- arguments: [ffi.types.POINTER],
655
- return: ffi.types.UINT_32
698
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
699
+ return: ffi.types.VOID
656
700
  },
657
701
  nil_simulate_battle: {
658
- arguments: [ffi.types.POINTER],
659
- return: ffi.types.UINT_32
702
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
703
+ return: ffi.types.VOID
660
704
  },
661
705
  nil_spawn_player: {
662
- arguments: [ffi.types.POINTER],
663
- return: ffi.types.UINT_32
706
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
707
+ return: ffi.types.VOID
664
708
  },
665
709
  nil_start_round: {
666
- arguments: [ffi.types.POINTER],
667
- return: ffi.types.UINT_32
710
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
711
+ return: ffi.types.VOID
668
712
  },
669
713
  nil_toggle_building: {
670
- arguments: [ffi.types.POINTER],
671
- return: ffi.types.UINT_32
714
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
715
+ return: ffi.types.VOID
672
716
  },
673
717
  nil_user_exists: {
674
- arguments: [ffi.types.POINTER],
675
- return: ffi.types.UINT_32
718
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
719
+ return: ffi.types.VOID
676
720
  },
677
721
  nil_validate_token: {
678
- arguments: [ffi.types.POINTER],
679
- return: ffi.types.UINT_32
722
+ arguments: [ffi.types.UINT_32, ffi.types.POINTER],
723
+ return: ffi.types.VOID
680
724
  }
681
725
  };
682
726
  //#endregion
683
- //#region package.json
684
- var version = "0.7.0";
685
- //#endregion
686
727
  //#region src/index.ts
687
- var Nil = class {
728
+ //! Copyright (C) Call of Nil contributors
729
+ //! SPDX-License-Identifier: AGPL-3.0-only
730
+ var VERSION = "0.7.2";
731
+ var USER_AGENT = `nil-ffi-node/${VERSION}`;
732
+ var Nil = class Nil {
688
733
  constructor(dll) {
689
734
  this.disposed = false;
690
735
  if (!dll.endsWith(ffi.suffix)) dll = `${dll}.${ffi.suffix}`;
691
736
  this.handle = ffi.dlopen(dll, definitions);
692
737
  this.functions = this.handle.functions;
693
738
  this.queue = new Queue(this.handle);
694
- this.setUserAgent(`nil-ffi-node/${version}`);
739
+ }
740
+ static async init(dll) {
741
+ const nil = new Nil(dll);
742
+ await nil.setUserAgent(USER_AGENT);
743
+ return nil;
695
744
  }
696
745
  [Symbol.dispose]() {
697
746
  this.close();
@@ -705,32 +754,51 @@ var Nil = class {
705
754
  }
706
755
  async getClientVersion() {
707
756
  this.throwIfClosed();
708
- return this.queue.request(() => {
709
- return this.functions.nil_client_version();
757
+ return this.queue.request((requestId) => {
758
+ this.functions.nil_client_version(requestId);
710
759
  });
711
760
  }
712
761
  async getFfiVersion() {
713
762
  this.throwIfClosed();
714
- return this.queue.request(() => {
715
- return this.functions.nil_ffi_version();
763
+ return this.queue.request((requestId) => {
764
+ this.functions.nil_ffi_version(requestId);
765
+ });
766
+ }
767
+ async getRemoteWorldLimit() {
768
+ this.throwIfClosed();
769
+ return this.queue.request((requestId) => {
770
+ this.functions.nil_get_remote_world_limit(requestId);
771
+ });
772
+ }
773
+ async getRemoteWorldLimitPerUser() {
774
+ this.throwIfClosed();
775
+ return this.queue.request((requestId) => {
776
+ this.functions.nil_get_remote_world_limit_per_user(requestId);
777
+ });
778
+ }
779
+ async getRemoteWorlds() {
780
+ this.throwIfClosed();
781
+ return this.queue.request((requestId) => {
782
+ this.functions.nil_get_remote_worlds(requestId);
716
783
  });
717
784
  }
718
785
  async getUserAgent() {
719
786
  this.throwIfClosed();
720
- return this.queue.request(() => {
721
- return this.functions.nil_user_agent();
787
+ return this.queue.request((requestId) => {
788
+ this.functions.nil_user_agent(requestId);
722
789
  });
723
790
  }
724
791
  async getWorld() {
725
792
  this.throwIfClosed();
726
- return this.queue.request(() => {
727
- return this.functions.nil_world();
793
+ return this.queue.request((requestId) => {
794
+ this.functions.nil_world(requestId);
728
795
  });
729
796
  }
730
797
  async setUserAgent(userAgent) {
731
798
  this.throwIfClosed();
732
- await this.queue.request(() => {
733
- return this.functions.nil_set_user_agent(userAgent);
799
+ await this.queue.request((requestId) => {
800
+ const arg1 = JSON.stringify(userAgent);
801
+ this.functions.nil_set_user_agent(requestId, arg1);
734
802
  });
735
803
  }
736
804
  throwIfClosed() {
@@ -738,4 +806,4 @@ var Nil = class {
738
806
  }
739
807
  };
740
808
  //#endregion
741
- export { FfiError, HandleClosedError, Nil, definitions };
809
+ export { HandleClosedError, Nil, NilError, USER_AGENT, UnknownResponseError, VERSION, definitions };