isaacscript-common 13.2.0 → 13.3.1

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.
Files changed (58) hide show
  1. package/dist/index.d.ts +37 -8
  2. package/dist/isaacscript-common.lua +6695 -6631
  3. package/dist/src/callbacks/customRevive.d.ts.map +1 -1
  4. package/dist/src/callbacks/customRevive.lua +2 -1
  5. package/dist/src/callbacks/postNewRoomEarly.lua +2 -2
  6. package/dist/src/classes/ModUpgraded.d.ts +5 -2
  7. package/dist/src/classes/ModUpgraded.d.ts.map +1 -1
  8. package/dist/src/classes/ModUpgraded.lua +39 -3
  9. package/dist/src/core/upgradeMod.d.ts +6 -1
  10. package/dist/src/core/upgradeMod.d.ts.map +1 -1
  11. package/dist/src/core/upgradeMod.lua +9 -2
  12. package/dist/src/features/customStage/exports.lua +2 -2
  13. package/dist/src/features/extraConsoleCommands/listCommands.lua +5 -5
  14. package/dist/src/features/pause.lua +2 -2
  15. package/dist/src/features/saveDataManager/load.d.ts.map +1 -1
  16. package/dist/src/features/saveDataManager/load.lua +2 -1
  17. package/dist/src/features/saveDataManager/main.lua +2 -2
  18. package/dist/src/features/taintedLazarusPlayers.lua +2 -2
  19. package/dist/src/functions/benchmark.d.ts +12 -0
  20. package/dist/src/functions/benchmark.d.ts.map +1 -1
  21. package/dist/src/functions/benchmark.lua +19 -0
  22. package/dist/src/functions/debugFunctions.d.ts +1 -0
  23. package/dist/src/functions/debugFunctions.d.ts.map +1 -1
  24. package/dist/src/functions/debugFunctions.lua +15 -23
  25. package/dist/src/functions/globals.lua +1 -1
  26. package/dist/src/functions/hex.lua +2 -2
  27. package/dist/src/functions/jsonHelpers.lua +2 -2
  28. package/dist/src/functions/log.d.ts +9 -70
  29. package/dist/src/functions/log.d.ts.map +1 -1
  30. package/dist/src/functions/log.lua +18 -504
  31. package/dist/src/functions/logMisc.d.ts +69 -0
  32. package/dist/src/functions/logMisc.d.ts.map +1 -0
  33. package/dist/src/functions/logMisc.lua +498 -0
  34. package/dist/src/index.d.ts +1 -0
  35. package/dist/src/index.d.ts.map +1 -1
  36. package/dist/src/index.lua +8 -0
  37. package/dist/src/indexLua.d.ts +185 -0
  38. package/dist/src/indexLua.d.ts.map +1 -0
  39. package/dist/src/indexLua.lua +1314 -0
  40. package/package.json +2 -2
  41. package/src/callbacks/customRevive.ts +2 -1
  42. package/src/callbacks/postNewRoomEarly.ts +1 -1
  43. package/src/classes/ModUpgraded.ts +50 -4
  44. package/src/core/upgradeMod.ts +10 -2
  45. package/src/features/customStage/exports.ts +1 -1
  46. package/src/features/extraConsoleCommands/listCommands.ts +1 -1
  47. package/src/features/pause.ts +1 -1
  48. package/src/features/saveDataManager/load.ts +2 -1
  49. package/src/features/saveDataManager/main.ts +1 -1
  50. package/src/features/taintedLazarusPlayers.ts +1 -1
  51. package/src/functions/benchmark.ts +23 -0
  52. package/src/functions/debugFunctions.ts +15 -25
  53. package/src/functions/globals.ts +1 -1
  54. package/src/functions/hex.ts +1 -1
  55. package/src/functions/jsonHelpers.ts +1 -1
  56. package/src/functions/log.ts +24 -455
  57. package/src/functions/logMisc.ts +441 -0
  58. package/src/index.ts +1 -0
@@ -1,61 +1,36 @@
1
- import {
2
- CollectibleType,
3
- DamageFlag,
4
- EntityFlag,
5
- GameStateFlag,
6
- GridRoom,
7
- HeartSubType,
8
- LevelStateFlag,
9
- ProjectileFlag,
10
- SeedEffect,
11
- SoundEffect,
12
- TearFlag,
13
- UseFlag,
14
- } from "isaac-typescript-definitions";
15
- import { game, sfxManager } from "../core/cachedClasses";
16
- import { arrayToString } from "./array";
17
- import { getCollectibleName } from "./collectibles";
18
- import { getEntityID } from "./entities";
19
- import { getEnumEntries } from "./enums";
20
- import { hasFlag } from "./flag";
21
- import { getIsaacAPIClassName } from "./isaacAPIClass";
22
- import { getPlayerHealth } from "./playerHealth";
23
- import { getEffectsList, getPlayerName } from "./players";
24
- import { getRoomData, getRoomGridIndex, getRoomListIndex } from "./roomData";
25
- import { combineSets, getSortedSetValues } from "./set";
26
- import { iterateTableInOrder } from "./table";
27
- import { getTrinketName } from "./trinkets";
28
- import { isTable, isUserdata } from "./types";
29
- import { printConsole } from "./utils";
30
- import { vectorToString } from "./vector";
31
-
32
1
  /**
33
- * Helper function to prefix the name of the function and the line number before a debug message.
2
+ * Helper function to get the name and the line number of the current calling function.
3
+ *
4
+ * For this function to work properly, the "--luadebug" flag must be enabled. Otherwise, it will
5
+ * always return undefined.
6
+ *
7
+ * @param levels Optional. The amount of levels to look backwards in the call stack. Default is 3
8
+ * (because the first level is this function, the second level is the calling
9
+ * function, and the third level is the parent of the calling function).
34
10
  */
35
- export function getDebugPrependString(
36
- msg: string,
11
+ export function getParentFunctionDescription(
37
12
  // We use 3 as a default because:
38
- // - 1 - getDebugPrependString
39
- // - 2 - calling function
40
- // - 3 - the function that calls the calling function
41
- numParentFunctions = 3,
42
- ): string {
13
+ // - The first level is this function.
14
+ // - The second level is the calling function.
15
+ // - The third level is the parent of the calling function.
16
+ levels = 3,
17
+ ): string | undefined {
43
18
  // "debug" is not always defined like the Lua definitions imply.
44
19
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
45
20
  if (debug !== undefined) {
46
21
  // The "--luadebug" launch flag is enabled.
47
- const debugTable = debug.getinfo(numParentFunctions);
22
+ const debugTable = debug.getinfo(levels);
48
23
  if (debugTable !== undefined) {
49
- return `${debugTable.name}:${debugTable.linedefined} - ${msg}`;
24
+ return `${debugTable.name}:${debugTable.linedefined}`;
50
25
  }
51
26
  }
52
27
 
53
- if (getParentFunctionDescription !== undefined) {
28
+ if (SandboxGetParentFunctionDescription !== undefined) {
54
29
  // The Racing+ sandbox is enabled.
55
- return `${getParentFunctionDescription(numParentFunctions + 1)} - ${msg}`;
30
+ return SandboxGetParentFunctionDescription(levels);
56
31
  }
57
32
 
58
- return msg;
33
+ return undefined;
59
34
  }
60
35
 
61
36
  /**
@@ -65,416 +40,10 @@ export function getDebugPrependString(
65
40
  * function will also prepend the function name and the line number before the string.
66
41
  */
67
42
  export function log(msg: string): void {
68
- const debugMsg = getDebugPrependString(msg);
43
+ const parentFunctionDescription = getParentFunctionDescription();
44
+ const debugMsg =
45
+ parentFunctionDescription === undefined
46
+ ? msg
47
+ : `${parentFunctionDescription} - ${msg}`;
69
48
  Isaac.DebugString(debugMsg);
70
49
  }
71
-
72
- export function logArray<T>(array: T[] | readonly T[]): void {
73
- const arrayString = arrayToString(array);
74
- log(`Array: ${arrayString}`);
75
- }
76
-
77
- export function logCollectibleTypes(collectibleTypes: CollectibleType[]): void {
78
- log("Collectibles:");
79
-
80
- let i = 1;
81
- for (const collectibleType of collectibleTypes) {
82
- const collectibleName = getCollectibleName(collectibleType);
83
- log(`${i}) ${collectibleName} (${collectibleType})`);
84
- i++;
85
- }
86
- }
87
-
88
- export function logColor(color: Color): void {
89
- log(
90
- `Color: R${color.R}, G${color.G}, B${color.B}, A${color.A}, RO${color.RO}, BO${color.BO}, GO${color.GO}`,
91
- );
92
- }
93
-
94
- /** Helper function for printing out every damage flag that is turned on. Useful when debugging. */
95
- export function logDamageFlags(flags: DamageFlag | BitFlags<DamageFlag>): void {
96
- logFlags(flags, DamageFlag, "damage");
97
- }
98
-
99
- /** Helper function for printing out every entity flag that is turned on. Useful when debugging. */
100
- export function logEntityFlags(flags: EntityFlag | BitFlags<EntityFlag>): void {
101
- logFlags(flags, EntityFlag, "entity");
102
- }
103
-
104
- export function logEntityID(entity: Entity): void {
105
- const entityID = getEntityID(entity);
106
- log(`Entity: ${entityID}`);
107
- }
108
-
109
- /**
110
- * Helper function to log an error message and also print it to the console for better visibility.
111
- *
112
- * This is useful in situations where using the `error` function would be dangerous (since it
113
- * prevents all of the subsequent code in the callback from running).
114
- */
115
- export function logError(msg: string): void {
116
- const errorMsg = `Error: ${msg}`;
117
- log(errorMsg);
118
- printConsole(errorMsg);
119
- }
120
-
121
- /** Helper function for printing out every flag that is turned on. Useful when debugging. */
122
- export function logFlags<T extends BitFlag | BitFlag128>(
123
- flags: T | BitFlags<T>,
124
- flagEnum: Record<string, T>,
125
- description = "",
126
- ): void {
127
- if (description !== "") {
128
- description = "flag";
129
- }
130
-
131
- // eslint-disable-next-line @typescript-eslint/no-base-to-string
132
- log(`Logging ${description} values for: ${flags}`);
133
- let hasNoFlags = true;
134
- for (const [key, value] of getEnumEntries(flagEnum)) {
135
- if (hasFlag(flags, value)) {
136
- // eslint-disable-next-line @typescript-eslint/no-base-to-string
137
- log(` Has flag: ${key} (${value})`);
138
- hasNoFlags = false;
139
- }
140
- }
141
-
142
- if (hasNoFlags) {
143
- log(" n/a (no flags)");
144
- }
145
- }
146
-
147
- /**
148
- * Helper function for printing out every game state flag that is turned on. Useful when debugging.
149
- */
150
- export function logGameStateFlags(): void {
151
- log("Logging game state flags:");
152
-
153
- const gameStateFlagEntries = getEnumEntries(GameStateFlag);
154
-
155
- let hasNoFlags = true;
156
- for (const [key, gameStateFlag] of gameStateFlagEntries) {
157
- const flagValue = game.GetStateFlag(gameStateFlag);
158
- if (flagValue) {
159
- log(` Has flag: ${key} (${gameStateFlag})`);
160
- hasNoFlags = false;
161
- }
162
- }
163
-
164
- if (hasNoFlags) {
165
- log(" n/a (no flags)");
166
- }
167
- }
168
-
169
- export function logKColor(kColor: KColor): void {
170
- log(
171
- `Color: R${kColor.Red}, G${kColor.Green}, B${kColor.Blue}, A${kColor.Alpha}`,
172
- );
173
- }
174
-
175
- /**
176
- * Helper function for printing out every level state flag that is turned on. Useful when debugging.
177
- */
178
- export function logLevelStateFlags(): void {
179
- const level = game.GetLevel();
180
-
181
- const levelStateFlagEntries = getEnumEntries(LevelStateFlag);
182
-
183
- log("Logging level state flags:");
184
- let hasNoFlags = true;
185
- for (const [key, levelStateFlag] of levelStateFlagEntries) {
186
- const flagValue = level.GetStateFlag(levelStateFlag);
187
- if (flagValue) {
188
- log(` Has flag: ${key} (${levelStateFlag})`);
189
- hasNoFlags = false;
190
- }
191
- }
192
-
193
- if (hasNoFlags) {
194
- log(" n/a (no flags)");
195
- }
196
- }
197
-
198
- export function logMap(map: Map<AnyNotNil, unknown>): void {
199
- log("Printing out a TSTL Map:");
200
-
201
- const mapKeys = [...map.keys()];
202
- mapKeys.sort();
203
-
204
- for (const key of mapKeys) {
205
- const value = map.get(key);
206
- // eslint-disable-next-line @typescript-eslint/no-base-to-string
207
- log(` ${key} --> ${value}`);
208
- }
209
-
210
- log(` The size of the map was: ${map.size}`);
211
- }
212
-
213
- export function logPlayerEffects(player: EntityPlayer): void {
214
- const effects = getEffectsList(player);
215
-
216
- log("Logging player effects:");
217
-
218
- if (effects.length === 0) {
219
- log(" n/a (no effects)");
220
- return;
221
- }
222
-
223
- effects.forEach((effect, i) => {
224
- let effectDescription: string;
225
- if (effect.Item.IsCollectible()) {
226
- const collectibleName = getCollectibleName(effect.Item.ID);
227
- effectDescription = `Collectible: ${collectibleName}`;
228
- } else if (effect.Item.IsTrinket()) {
229
- const trinketName = getTrinketName(effect.Item.ID);
230
- effectDescription = `Trinket: ${trinketName}`;
231
- } else if (effect.Item.IsNull()) {
232
- effectDescription = `Null item: ${effect.Item.ID}`;
233
- } else {
234
- effectDescription = `Unknown type of effect: ${effect.Item.ID}`;
235
- }
236
-
237
- log(` ${i + 1}) ${effectDescription} (x${effect.Count})`);
238
- });
239
- }
240
-
241
- export function logPlayerHealth(player: EntityPlayer): void {
242
- const playerName = getPlayerName(player);
243
- const playerHealth = getPlayerHealth(player);
244
-
245
- log(`Player health for ${playerName}:`);
246
- log(` Max hearts: ${playerHealth.maxHearts}`);
247
- log(` Hearts: ${playerHealth.hearts}`);
248
- log(` Eternal hearts: ${playerHealth.eternalHearts}`);
249
- log(` Soul hearts: ${playerHealth.soulHearts}`);
250
- log(` Bone hearts: ${playerHealth.boneHearts}`);
251
- log(` Golden hearts: ${playerHealth.goldenHearts}`);
252
- log(` Rotten hearts: ${playerHealth.rottenHearts}`);
253
- log(` Broken hearts: ${playerHealth.brokenHearts}`);
254
- log(` Soul charges: ${playerHealth.soulCharges}`);
255
- log(` Blood charges: ${playerHealth.bloodCharges}`);
256
- log(" Soul heart types: [");
257
- for (const soulHeartType of playerHealth.soulHeartTypes) {
258
- log(` HeartSubType.${HeartSubType[soulHeartType]}`);
259
- }
260
- log(" ]");
261
- }
262
-
263
- /**
264
- * Helper function for printing out every projectile flag that is turned on. Useful when debugging.
265
- */
266
- export function logProjectileFlags(
267
- flags: ProjectileFlag | BitFlags<ProjectileFlag>,
268
- ): void {
269
- logFlags(flags, ProjectileFlag, "projectile");
270
- }
271
-
272
- /** Helper function for logging information about the current room. */
273
- export function logRoom(): void {
274
- const room = game.GetRoom();
275
- const bossID = room.GetBossID();
276
- const roomGridIndex = getRoomGridIndex();
277
- const roomListIndex = getRoomListIndex();
278
- const roomData = getRoomData();
279
-
280
- log("Current room information:");
281
- if (roomData === undefined) {
282
- log("- Room data is undefined.");
283
- } else {
284
- log(`- Room stage ID: ${roomData.StageID}`);
285
- log(
286
- `- Type/variant/sub-type: ${roomData.Type}.${roomData.Variant}.${roomData.Subtype}`,
287
- );
288
- log(`- Name: ${roomData.Name}`);
289
- }
290
-
291
- const roomGridIndexName = GridRoom[roomGridIndex];
292
- if (roomGridIndexName === undefined) {
293
- log(`- Grid index: ${roomGridIndex}`);
294
- } else {
295
- log(`- Grid index: GridRoom.${roomGridIndexName} (${roomGridIndex})`);
296
- }
297
-
298
- log(`- List index: ${roomListIndex}`);
299
- log(`- Boss ID: ${bossID}`);
300
- }
301
-
302
- /**
303
- * Helper function for printing out every seed effect (i.e. Easter Egg) that is turned on for the
304
- * particular run.
305
- */
306
- export function logSeedEffects(): void {
307
- const seeds = game.GetSeeds();
308
-
309
- const seedEffectEntries = getEnumEntries(SeedEffect);
310
-
311
- log("Logging seed effects:");
312
- let hasNoSeedEffects = true;
313
- for (const [key, seedEffect] of seedEffectEntries) {
314
- if (seeds.HasSeedEffect(seedEffect)) {
315
- log(` ${key} (${seedEffect})`);
316
- hasNoSeedEffects = false;
317
- }
318
- }
319
-
320
- if (hasNoSeedEffects) {
321
- log(" n/a (no seed effects)");
322
- }
323
- }
324
-
325
- export function logSet(set: Set<AnyNotNil> | ReadonlySet<AnyNotNil>): void {
326
- log("Printing out a TSTL Set:");
327
-
328
- const setValues = getSortedSetValues(set);
329
- for (const value of setValues) {
330
- // eslint-disable-next-line @typescript-eslint/no-base-to-string
331
- log(` Value: ${value}`);
332
- }
333
-
334
- log(` The size of the set was: ${set.size}`);
335
- }
336
-
337
- /** Helper function for logging every sound effect that is currently playing. */
338
- export function logSounds(): void {
339
- const soundEffects = getEnumEntries(SoundEffect);
340
-
341
- for (const [key, soundEffect] of soundEffects) {
342
- if (sfxManager.IsPlaying(soundEffect)) {
343
- log(`Currently playing sound effect: ${key} (${soundEffect})`);
344
- }
345
- }
346
- }
347
-
348
- /**
349
- * Helper function for logging every key and value of a table. This is a deep log; the function will
350
- * recursively call itself if it counters a table within a table.
351
- *
352
- * This function will only work on tables that have string keys (because it logs the keys in order,
353
- * instead of randomly). It will throw a run-time error if it encounters a non-string key.
354
- */
355
- export function logTable(luaTable: unknown, parentTables = 0): void {
356
- if (parentTables === 0) {
357
- log("Printing out a Lua table:");
358
- }
359
-
360
- const numSpaces = (parentTables + 1) * 2; // 2, 4, 6, etc.
361
- const indentation = " ".repeat(numSpaces);
362
-
363
- if (!isTable(luaTable)) {
364
- // Put it in an IIFE so that it will show as "func" instead of "logTable" and align with the
365
- // other text. We have to use a non-arrow function to prevent Lua language server errors with
366
- // the self argument.
367
- (function func() {
368
- log(
369
- `${indentation}n/a (encountered a variable of type "${typeof luaTable}" instead of a table)`,
370
- );
371
- })();
372
-
373
- return;
374
- }
375
-
376
- let numElements = 0;
377
- iterateTableInOrder(luaTable, (key, value) => {
378
- // eslint-disable-next-line @typescript-eslint/no-base-to-string
379
- log(`${indentation}${key} --> ${value}`);
380
-
381
- if (isTable(value)) {
382
- if (key === "__class") {
383
- log(
384
- `${indentation} (skipping enumerating this key to avoid infinite recursion)`,
385
- );
386
- } else {
387
- logTable(value, parentTables + 1);
388
- }
389
- }
390
-
391
- numElements++;
392
- });
393
-
394
- // Put it in an IIFE so that it will show as "func" instead of "logTable" and align with the other
395
- // text. We have to use a non-arrow function to prevent Lua language server errors with the self
396
- // argument.
397
- (function func() {
398
- log(`${indentation}The size of the table was: ${numElements}`);
399
- })();
400
- }
401
-
402
- /**
403
- * Helper function to print out the differences between the entries of two tables. Note that this
404
- * will only do a shallow comparison.
405
- */
406
- export function logTableDifferences<K extends AnyNotNil, V>(
407
- table1: LuaMap<K, V>,
408
- table2: LuaMap<K, V>,
409
- ): void {
410
- log("Comparing two Lua tables:");
411
-
412
- const table1Keys = Object.keys(table1);
413
- const table1KeysSet = new Set(table1Keys);
414
-
415
- const table2Keys = Object.keys(table2);
416
- const table2KeysSet = new Set(table2Keys);
417
-
418
- const keysSet = combineSets(table1KeysSet, table2KeysSet);
419
- const keys = [...keysSet.values()];
420
- keys.sort();
421
-
422
- for (const key of keys) {
423
- if (!table1KeysSet.has(key)) {
424
- // eslint-disable-next-line @typescript-eslint/no-base-to-string
425
- log(` Table 1 is missing key: ${key}`);
426
- } else if (!table2KeysSet.has(key)) {
427
- // eslint-disable-next-line @typescript-eslint/no-base-to-string
428
- log(` Table 2 is missing key: ${key}`);
429
- } else {
430
- const value1 = table1.get(key);
431
-
432
- const value2 = table2.get(key);
433
- if (value1 !== value2) {
434
- // eslint-disable-next-line @typescript-eslint/no-base-to-string
435
- log(` ${key} --> "${value1}" versus "${value2}"`);
436
- }
437
- }
438
- }
439
- }
440
-
441
- /** Helper function for printing out every tear flag that is turned on. Useful when debugging. */
442
- export function logTearFlags(flags: TearFlag | BitFlags<TearFlag>): void {
443
- logFlags(flags, TearFlag, "tear");
444
- }
445
-
446
- /** Helper function for printing out every use flag that is turned on. Useful when debugging. */
447
- export function logUseFlags(flags: UseFlag | BitFlags<UseFlag>): void {
448
- logFlags(flags, UseFlag, "use");
449
- }
450
-
451
- /**
452
- * Helper function to enumerate all of the properties of a "userdata" object (i.e. an object from
453
- * the Isaac API).
454
- */
455
- export function logUserdata(userdata: unknown): void {
456
- if (!isUserdata(userdata)) {
457
- log("Userdata: [not userdata]");
458
- return;
459
- }
460
-
461
- const metatable = getmetatable(userdata);
462
- if (metatable === undefined) {
463
- log("Userdata: [no metatable]");
464
- return;
465
- }
466
-
467
- const classType = getIsaacAPIClassName(userdata);
468
- if (classType === undefined) {
469
- log("Userdata: [no class type]");
470
- } else {
471
- log(`Userdata: ${classType}`);
472
- }
473
-
474
- logTable(metatable);
475
- }
476
-
477
- export function logVector(vector: Vector, round = false): void {
478
- const vectorString = vectorToString(vector, round);
479
- log(`Vector: ${vectorString}`);
480
- }