xray16 1.0.8 → 1.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "xray16",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "author": "Neloreck",
5
5
  "repository": "https://github.com/stalker-xrts/xray-16-types",
6
6
  "private": false,
@@ -0,0 +1,89 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const typescript_1 = require("typescript");
4
+ const typescript_to_lua_1 = require("typescript-to-lua");
5
+ const diagnostics_1 = require("./utils/diagnostics");
6
+ const INLINE_MACROS = "$inline";
7
+ const expectedFunctionExpressionInInline = (0, diagnostics_1.createErrorDiagnosticFactory)("Expected a function expression in '__inline'.");
8
+ /**
9
+ * Plugin for transformation of casting methods.
10
+ * Simplifies TS/Lua testing and interoperation.
11
+ *
12
+ * Reference: https://gist.github.com/Cheatoid/ea4573c6bd1992fc4940090543ec9380
13
+ */
14
+ const plugin = {
15
+ visitors: {
16
+ [typescript_1.SyntaxKind.ExpressionStatement]: (node, context) => {
17
+ const result = context.superTransformStatements(node);
18
+ if ((0, typescript_1.isExpressionStatement)(node)) {
19
+ const expr = node.expression;
20
+ if ((0, typescript_1.isCallExpression)(expr) && (0, typescript_1.isIdentifier)(expr.expression)) {
21
+ switch (expr.expression.text) {
22
+ case INLINE_MACROS: {
23
+ if (expr.arguments.length > 0) {
24
+ let bodyArg = expr.arguments[0];
25
+ if ((0, typescript_1.isIdentifier)(bodyArg)) {
26
+ try {
27
+ bodyArg = context.checker.getSymbolAtLocation(bodyArg).getDeclarations()[0];
28
+ }
29
+ catch (error) {
30
+ context.diagnostics.push(expectedFunctionExpressionInInline(node));
31
+ break;
32
+ }
33
+ if (!bodyArg) {
34
+ context.diagnostics.push(expectedFunctionExpressionInInline(node));
35
+ break;
36
+ }
37
+ }
38
+ const paramNames = [];
39
+ let funcExpr;
40
+ if ((0, typescript_1.isFunctionLike)(bodyArg)) {
41
+ const bodyNode = context.transformNode(bodyArg)[0];
42
+ if (!bodyNode) {
43
+ context.diagnostics.push(expectedFunctionExpressionInInline(node));
44
+ break;
45
+ }
46
+ if ((0, typescript_to_lua_1.isVariableDeclarationStatement)(bodyNode) && bodyNode.right) {
47
+ if ((0, typescript_to_lua_1.isFunctionExpression)(bodyNode.right[0])) {
48
+ funcExpr = bodyNode.right[0];
49
+ }
50
+ }
51
+ paramNames.push(...bodyArg.parameters.map((p) => p.name.getText()));
52
+ }
53
+ for (const stmt of result) {
54
+ if ((0, typescript_to_lua_1.isExpressionStatement)(stmt)) {
55
+ const callExpr = stmt.expression;
56
+ if ((0, typescript_to_lua_1.isCallExpression)(callExpr) &&
57
+ (0, typescript_to_lua_1.isIdentifier)(callExpr.expression) &&
58
+ callExpr.expression.text === expr.expression.text) {
59
+ const paramCount = callExpr.params.length;
60
+ if (paramCount > 0) {
61
+ let body = callExpr.params[0];
62
+ if ((0, typescript_to_lua_1.isIdentifier)(body)) {
63
+ body = funcExpr;
64
+ }
65
+ if (body && (0, typescript_to_lua_1.isFunctionExpression)(body)) {
66
+ const statements = body.body.statements;
67
+ for (let index = 1; index < paramCount; ++index) {
68
+ // Skip the body parameter.
69
+ const param = callExpr.params[index];
70
+ statements.unshift((0, typescript_to_lua_1.createVariableDeclarationStatement)([(0, typescript_to_lua_1.createIdentifier)(paramNames[index - 1])], [param]));
71
+ }
72
+ return (0, typescript_to_lua_1.createDoStatement)(statements, node);
73
+ }
74
+ }
75
+ }
76
+ }
77
+ }
78
+ }
79
+ context.diagnostics.push(expectedFunctionExpressionInInline(node));
80
+ break;
81
+ }
82
+ }
83
+ }
84
+ }
85
+ return result;
86
+ },
87
+ },
88
+ };
89
+ exports.default = plugin;
package/types/index.d.ts CHANGED
@@ -49,4 +49,6 @@ import "./xr_ui/xr_ui_event";
49
49
  import "./xr_ui/xr_ui_interface";
50
50
  import "./xr_ui/xr_ui_menu";
51
51
 
52
+ import "./xrf_plugin";
53
+
52
54
  declare module "xray16" {}
@@ -58,6 +58,11 @@ declare module "xray16" {
58
58
  */
59
59
  type Maybe<T> = T | null | undefined;
60
60
 
61
+ /**
62
+ * @group xr_type
63
+ */
64
+ type AnyObject = Record<string, any>;
65
+
61
66
  /**
62
67
  * @group xr_type
63
68
  */
@@ -178,10 +178,10 @@ declare module "xray16" {
178
178
  /**
179
179
  * 2 todo;
180
180
  */
181
- public set_callback(
181
+ public set_callback<T extends AnyObject>(
182
182
  type: TXR_callbacks["trade_sell_buy_item"],
183
183
  cb?: ((this: void, item: game_object, money_direction: boolean, money: number) => void) | null,
184
- object?: object_binder | null
184
+ object?: Maybe<T>
185
185
  ): void;
186
186
 
187
187
  // 3 todo: trade_perform_operation
@@ -189,19 +189,19 @@ declare module "xray16" {
189
189
  /**
190
190
  * 4 todo;
191
191
  */
192
- public set_callback(
192
+ public set_callback<T extends AnyObject>(
193
193
  type: TXR_callbacks["zone_enter"],
194
194
  cb?: ((this: void, zone: game_object, object: game_object) => void) | null,
195
- object?: object_binder | null
195
+ object?: Maybe<T>
196
196
  ): void;
197
197
 
198
198
  /**
199
199
  * 4 todo;
200
200
  */
201
- public set_callback(
201
+ public set_callback<T extends AnyObject>(
202
202
  type: TXR_callbacks["zone_exit"],
203
203
  cb?: ((this: void, zone: game_object, object: game_object) => void) | null,
204
- object?: object_binder | null
204
+ object?: Maybe<T>
205
205
  ): void;
206
206
 
207
207
  // 6 todo: level_border_exit
@@ -211,19 +211,19 @@ declare module "xray16" {
211
211
  /**
212
212
  * 8 todo;
213
213
  */
214
- public set_callback(
214
+ public set_callback<T extends AnyObject>(
215
215
  type: TXR_callbacks["death"],
216
216
  cb?: (this: void, target: game_object, killer: game_object) => void,
217
- object?: object_binder
217
+ object?: Maybe<T>
218
218
  ): void;
219
219
 
220
220
  /**
221
221
  * 9 todo;
222
222
  */
223
- public set_callback(
223
+ public set_callback<T extends AnyObject>(
224
224
  type: TXR_callbacks["patrol_path_in_point"],
225
225
  cb?: ((this: void, object: game_object, action_type: number, point_index: number) => void) | null,
226
- object?: object_binder | null
226
+ object?: Maybe<T>
227
227
  ): void;
228
228
 
229
229
  // 10 todo: inventory_pda
@@ -231,10 +231,10 @@ declare module "xray16" {
231
231
  /**
232
232
  * 11 todo:
233
233
  */
234
- public set_callback(
234
+ public set_callback<T extends AnyObject>(
235
235
  type: TXR_callbacks["inventory_info"],
236
236
  cb?: ((this: void, npc: game_object, info_id: string) => void) | null,
237
- object?: object_binder | null
237
+ object?: Maybe<T>
238
238
  ): void;
239
239
 
240
240
  // 12 todo: article_info
@@ -242,10 +242,10 @@ declare module "xray16" {
242
242
  /**
243
243
  * 13 todo;
244
244
  */
245
- public set_callback(
245
+ public set_callback<T extends AnyObject>(
246
246
  type: TXR_callbacks["task_state"],
247
247
  cb?: ((this: void, task: CGameTask, state: TXR_TaskState) => void) | null,
248
- object?: object_binder | null
248
+ object?: Maybe<T>
249
249
  ): void;
250
250
 
251
251
  // 14 todo: map_location_added
@@ -253,21 +253,21 @@ declare module "xray16" {
253
253
  /**
254
254
  * 15 Use some object.
255
255
  */
256
- public set_callback(
256
+ public set_callback<T extends AnyObject>(
257
257
  type: TXR_callbacks["use_object"],
258
258
  cb?: ((this: void, object: game_object) => void) | null,
259
- object?: object_binder | null
259
+ object?: Maybe<T>
260
260
  ): void;
261
- public set_callback(
261
+ public set_callback<T extends AnyObject>(
262
262
  type: TXR_callbacks["use_object"],
263
263
  cb?: ((this: void, object: game_object, who: game_object) => void) | null,
264
- object?: object_binder | null
264
+ object?: Maybe<T>
265
265
  ): void;
266
266
 
267
267
  /**
268
268
  * 16 Entity got hit.
269
269
  */
270
- public set_callback(
270
+ public set_callback<T extends AnyObject>(
271
271
  type: TXR_callbacks["hit"],
272
272
  cb?:
273
273
  | ((
@@ -279,13 +279,13 @@ declare module "xray16" {
279
279
  bone_id: number
280
280
  ) => void)
281
281
  | null,
282
- object?: object_binder | null
282
+ object?: Maybe<T>
283
283
  ): void;
284
284
 
285
285
  /**
286
286
  * 17 todo;
287
287
  */
288
- public set_callback(
288
+ public set_callback<T extends AnyObject>(
289
289
  type: TXR_callbacks["sound"],
290
290
  cb?:
291
291
  | ((
@@ -297,7 +297,7 @@ declare module "xray16" {
297
297
  sound_power: number
298
298
  ) => void)
299
299
  | null,
300
- object?: object_binder | null
300
+ object?: Maybe<T>
301
301
  ): void;
302
302
 
303
303
  // 18 todo: action_movement
@@ -319,43 +319,43 @@ declare module "xray16" {
319
319
  /**
320
320
  * 26 todo;
321
321
  */
322
- public set_callback(
322
+ public set_callback<T extends AnyObject>(
323
323
  type: TXR_callbacks["helicopter_on_point"],
324
324
  cb?: ((this: void, distance: number, current_position: vector, vertex_id: number) => void) | null,
325
- object?: object_binder | null
325
+ object?: Maybe<T>
326
326
  ): void;
327
327
 
328
328
  /**
329
329
  * 27 todo;
330
330
  */
331
- public set_callback(
331
+ public set_callback<T extends AnyObject>(
332
332
  type: TXR_callbacks["helicopter_on_hit"],
333
333
  cb?: ((this: void, damage: number, impulse: number, hit_type: number, who_id: number) => void) | null,
334
- object?: object_binder | null
334
+ object?: Maybe<T>
335
335
  ): void;
336
336
 
337
337
  /**
338
338
  * 28 todo;
339
339
  */
340
- public set_callback(
340
+ public set_callback<T extends AnyObject>(
341
341
  type: TXR_callbacks["on_item_take"],
342
342
  cb?: ((this: void, object: game_object, item: game_object) => void) | null,
343
- object?: object_binder | null
343
+ object?: Maybe<T>
344
344
  ): void;
345
345
 
346
346
  /**
347
347
  * 29 todo;
348
348
  */
349
- public set_callback(
349
+ public set_callback<T extends AnyObject>(
350
350
  type: TXR_callbacks["on_item_drop"],
351
351
  cb?: ((this: void, object: game_object, item: game_object) => void) | null,
352
- object?: object_binder | null
352
+ object?: Maybe<T>
353
353
  ): void;
354
354
 
355
355
  /**
356
356
  * 30 todo;
357
357
  */
358
- public set_callback(
358
+ public set_callback<T extends AnyObject>(
359
359
  type: TXR_callbacks["script_animation"],
360
360
  cb?: ((this: void, skip_multi_anim_check?: boolean) => void) | null,
361
361
  object?: object | null
@@ -370,10 +370,10 @@ declare module "xray16" {
370
370
  /**
371
371
  * 34 todo;
372
372
  */
373
- public set_callback(
373
+ public set_callback<T extends AnyObject>(
374
374
  type: TXR_callbacks["take_item_from_box"],
375
375
  cb?: ((this: void, object: game_object, box: game_object, item: game_object) => void) | null,
376
- object?: object_binder | null
376
+ object?: Maybe<T>
377
377
  ): void;
378
378
 
379
379
  // 35 todo: weapon_no_ammo
@@ -0,0 +1,44 @@
1
+ declare global {
2
+ /**
3
+ * Utility to get current filename, similar to __filename in nodejs.
4
+ *
5
+ * @group xrf_plugin
6
+ */
7
+ const $filename: string;
8
+
9
+ /**
10
+ * Utility to transform TS provided array to a lua one.
11
+ * Just wrapper that is stripped compile time, but simplifies unit testing with TS.
12
+ *
13
+ * @group xrf_plugin
14
+ */
15
+ function $fromArray<T>(array: Array<T>): LuaTable<number, T>;
16
+
17
+ /**
18
+ * Utility to transform LUA array to JS array.
19
+ * Just wrapper that is stripped compile time, but simplifies unit testing with TS.
20
+ *
21
+ * @group xrf_plugin
22
+ */
23
+ function $fromLuaArray<T>(array: LuaTable<number, T>): Array<T>;
24
+
25
+ /**
26
+ * Utility to transform TS provided object to a lua table.
27
+ * Just wrapper that is stripped compile time, but simplifies unit testing with TS.
28
+ *
29
+ * @group xrf_plugin
30
+ */
31
+ function $fromObject<K extends string | number, T>(object: Record<K, T>): LuaTable<K, T>;
32
+ function $fromObject<D>(object: D): LuaTable<keyof D, D[keyof D]>;
33
+
34
+ /**
35
+ * Utility to transform LUA provided table to a TS one.
36
+ * Just wrapper that is stripped compile time, but simplifies unit testing with TS.
37
+ *
38
+ * @group xrf_plugin
39
+ */
40
+ function $fromLuaTable<K extends string | number, T>(object: LuaTable<K, T>): Record<K, T>;
41
+ function $fromLuaTable<D>(object: LuaTable<keyof D, D[keyof D]>): D;
42
+ }
43
+
44
+ export {};