Module Stacktrace

Automatic mod-name and load-stage/phase detection.

Negative stack levels indicate a relative offset from the bottom.

Level 0 is any Stacktrace.* function. Level 1 is the function that called any public Stacktrace.* function. Level -1 is the bottom of the stack.

Module Status: Experimental 2020-10-31.

Usage:

  • local Stacktrace = require('__eradicators-library__/erlib/factorio/Stacktrace')()
    
  • An example stack. l=level
     l  l
     0 -4 erlib/factorio/Stacktrace.lua           -- top    (called last )
     1 -3 core/lualib/util.lua (table.deepcopy)   --
     2 -2 prototypes/entity/my-modded-entity.lua  --
     3 -1 data.lua                                -- bottom (called first)
    

raw

get_info([l=1]) Gets debug info of the function at stack level l.
get_all_info() Retrieves info for the whole stack.
get_pos([l=1]) Gets file:line at stack level l.
print_info([l=1]) Prints a stacktrace directly to stdout, starting at level l.

factorio generic

get_mod_name([l=1]) → "my-mod-name"
get_mod_root([l=1]) → "__my-mod-name__/"
get_file_name([l=1]) → "my-modded-file.lua"
get_directory([l=1]) → "__my-mod-name__/sub/directory/"
path2name(path) "__my-mod-name__/sub/directory" → "my-mod"
name2root(name) "my-mod-name" → "__my-mod-name__/"
_unsafe_get_stage_and_phase() Retrieves the current load stage and phase from the filenames on the stack.

factorio load stage + phase

get_load_stage() Creates a fresh LoadStageTable.
get_load_phase() Creates a fresh LoadPhaseTable.
LoadStageName The name of a load stage.
LoadPhaseName The name of a load phase.
LoadStageTable A table that contains three (key → value) pairs.
LoadPhaseTable A table that contains three (key → value) pairs.


raw

You shouldn't be using these unless you really know what you're doing.
get_info([l=1])
Gets debug info of the function at stack level l. l=0 the last file on the stack (the file containing this function).

Parameters:

  • l integer The stack level at which to get the info. (default 1)

Returns:

    {source=,...} The output of debug.getinfo at the given level.
get_all_info()
Retrieves info for the whole stack.

Returns:

    Array info for each level of the stack. Starting at 0.
get_pos([l=1])
Gets file:line at stack level l.

Parameters:

  • l integer (default 1)

Returns:

    string "filename:number"
print_info([l=1])
Prints a stacktrace directly to stdout, starting at level l.

Parameters:

  • l integer (default 1)

factorio generic

get_mod_name([l=1])
→ "my-mod-name"

Parameters:

  • l integer (default 1)

Returns:

  1. string name of the mod at level l: "my-mod-name" or "unknown-or-scenario" if the check failed.
  2. boolean if the name was found.
get_mod_root([l=1])
→ "__my-mod-name__/"

Parameters:

  • l integer (default 1)

Returns:

  1. string "__my-mod-name__/" root of the mod at level l or "__unknown-or-scenario__/" if the check failed.
  2. boolean if the root was found.
get_file_name([l=1])
→ "my-modded-file.lua"

Parameters:

  • l integer (default 1)

Returns:

  1. string or nil "my-modded-file.lua" file of the mod at level l or "file-not-found.lua" if the check failed.
  2. boolean if the root was found.
get_directory([l=1])
→ "__my-mod-name__/sub/directory/"

Parameters:

  • l integer (default 1)

Returns:

  1. string "__my-mod-name__/sub/directory/" directory of the mod at level l or "__unknown-or-scenario__/" if the check failed.
  2. boolean if the root was found.

Usage:

    local full_path = Stacktrace.get_directory(1) .. Stacktrace.get_file_name(1)
    print(full_path)
    > __my-mod-name__/sub/directory/my-modded-file.lua
    
path2name(path)
"__my-mod-name__/sub/directory" → "my-mod"

Parameters:

  • path string "__my-mod-name__/sub/directory" any path

Returns:

    string "my-mod" the undecorated name of the mod
name2root(name)
"my-mod-name" → "__my-mod-name__/"

Parameters:

  • name string "my-mod-name" the undecorated name of a mod

Returns:

    string "__my-mod-name__/" the absolute root of the mod
_unsafe_get_stage_and_phase()
Retrieves the current load stage and phase from the filenames on the stack. In some situations like scenarios, cross loading, or metatable methods the stage/phase can not be correctly determined. In these cases nil,nil is returned. Be aware that the returned strings use _ underscore instead of - dash.

Should only be used when the error throwing behavior of Stacktrace.get_load_stage or Stacktrace.get_load_phase is undesired.

Returns:

  1. LoadStageName or nil
  2. LoadPhaseName or nil

Usage:

    local stage,phase = Stacktrace._unsafe_get_stage_and_phase()

factorio load stage + phase

get_load_stage()
Creates a fresh LoadStageTable. The stage name is internally cached so this is quite fast.

Returns:

    LoadStageTable

Raises:

It is an error if the stage could not be detected. I.e. when calling from inside a scenario or a data stage metatable.

Usage:

    if Stacktrace.get_load_stage().control then
      script.on_event(defines.events.on_tick,function()end)
      end
    
get_load_phase()
Creates a fresh LoadPhaseTable. The phase name is internally cached so this is quite fast.

Returns:

    LoadPhaseTable

Raises:

It is an error if the phase could not be detected. I.e. when calling from inside a scenario or a data stage metatable.

Usage:

    if Stacktrace.get_load_phase().data_updates then
      -- do_something()
      end
    
LoadStageName
The name of a load stage. One of three strings: "settings", "data" or "control".
LoadPhaseName
The name of a load phase. This is one of 7 strings: "settings", "settings_updates", "settings_final_fixes" "data", "data_updates", "data_final_fixes" or "control".

Be aware that unlike the corresponding file names these strings use _ underscores instead of - dashes for ease of use.

LoadStageTable
A table that contains three (key → value) pairs. It is used for stage based conditional code execution.

Fields:

LoadPhaseTable
A table that contains three (key → value) pairs. It is used for stage based conditional code execution.

Fields:

generated by LDoc 1.4.6 Last updated 2021-09-10 19:51:19