isaacscript-common 13.3.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "isaacscript-common",
3
- "version": "13.3.0",
3
+ "version": "13.3.1",
4
4
  "description": "Helper functions and features for IsaacScript mods.",
5
5
  "keywords": [
6
6
  "isaac",
@@ -22,6 +22,6 @@
22
22
  "main": "dist/src/index",
23
23
  "types": "dist/src/index.d.ts",
24
24
  "dependencies": {
25
- "isaac-typescript-definitions": "^7.2.8"
25
+ "isaac-typescript-definitions": "^7.2.9"
26
26
  }
27
27
  }
@@ -1,7 +1,7 @@
1
1
  import { ModCallback } from "isaac-typescript-definitions";
2
2
  import { ModCallbackCustom } from "../enums/ModCallbackCustom";
3
3
  import { getTime } from "../functions/benchmark";
4
- import { log } from "../functions/log";
4
+ import { getParentFunctionDescription } from "../functions/log";
5
5
  import { AddCallbackParameterCustom } from "../interfaces/private/AddCallbackParameterCustom";
6
6
  import { CALLBACK_REGISTER_FUNCTIONS } from "../objects/callbackRegisterFunctions";
7
7
 
@@ -53,12 +53,19 @@ export class ModUpgraded implements Mod {
53
53
  const callback = args[0];
54
54
  const optionalArg = args[1];
55
55
 
56
+ const parentFunctionDescription = getParentFunctionDescription();
56
57
  const callbackName = `ModCallback.${ModCallback[modCallback]}`;
57
-
58
- // We can't use the "log" helper function since that would cause a dependency cycle.
58
+ const signature =
59
+ parentFunctionDescription === undefined
60
+ ? callbackName
61
+ : `${parentFunctionDescription} - ${callbackName}`;
62
+
63
+ /**
64
+ * We don't use the "log" helper function since it will always show the same "unknown" prefix.
65
+ */
59
66
  const callbackWithLogger = (...callbackArgs: unknown[]) => {
60
67
  const startTime = getTime();
61
- log(`${callbackName} - START`);
68
+ Isaac.DebugString(`${signature} - START`);
62
69
 
63
70
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
64
71
  // @ts-expect-error
@@ -70,9 +77,9 @@ export class ModUpgraded implements Mod {
70
77
  this.TimeThreshold === undefined ||
71
78
  this.TimeThreshold >= elapsedTime
72
79
  ) {
73
- log(`${callbackName} - END - time: ${elapsedTime}`);
80
+ Isaac.DebugString(`${signature} - END - time: ${elapsedTime}`);
74
81
  } else {
75
- log(`${callbackName} - END`);
82
+ Isaac.DebugString(`${signature} - END`);
76
83
  }
77
84
  };
78
85
 
@@ -1,33 +1,36 @@
1
1
  /**
2
- * 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).
3
10
  */
4
- export function getDebugPrependString(
5
- msg: string,
11
+ export function getParentFunctionDescription(
6
12
  // We use 3 as a default because:
7
- // - 1 - getDebugPrependString
8
- // - 2 - calling function
9
- // - 3 - the function that calls the calling function
10
- numParentFunctions = 3,
11
- ): 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 {
12
18
  // "debug" is not always defined like the Lua definitions imply.
13
19
  // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
14
20
  if (debug !== undefined) {
15
21
  // The "--luadebug" launch flag is enabled.
16
- const debugTable = debug.getinfo(numParentFunctions);
22
+ const debugTable = debug.getinfo(levels);
17
23
  if (debugTable !== undefined) {
18
- return `${debugTable.name}:${debugTable.linedefined} - ${msg}`;
24
+ return `${debugTable.name}:${debugTable.linedefined}`;
19
25
  }
20
26
  }
21
27
 
22
28
  if (SandboxGetParentFunctionDescription !== undefined) {
23
29
  // The Racing+ sandbox is enabled.
24
- const parentFunctionDescription = SandboxGetParentFunctionDescription(
25
- numParentFunctions + 1,
26
- );
27
- return `${parentFunctionDescription} - ${msg}`;
30
+ return SandboxGetParentFunctionDescription(levels);
28
31
  }
29
32
 
30
- return msg;
33
+ return undefined;
31
34
  }
32
35
 
33
36
  /**
@@ -37,6 +40,10 @@ export function getDebugPrependString(
37
40
  * function will also prepend the function name and the line number before the string.
38
41
  */
39
42
  export function log(msg: string): void {
40
- const debugMsg = getDebugPrependString(msg);
43
+ const parentFunctionDescription = getParentFunctionDescription();
44
+ const debugMsg =
45
+ parentFunctionDescription === undefined
46
+ ? msg
47
+ : `${parentFunctionDescription} - ${msg}`;
41
48
  Isaac.DebugString(debugMsg);
42
49
  }