- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenum-debug-util.lua
More file actions
Latest commit
96 lines (88 loc) · 3.02 KB
/
Copy pathenum-debug-util.lua
File metadata and controls
96 lines (88 loc) · 3.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
local__DebugAdapter=__DebugAdapter
localnext=next
localscript=script
localsetmetatable=setmetatable
localtype=type
---replaces numbers with tables with metamethods to display enum names instead of raw numbers
---puts the enums_table in global to make it accessible in the variables view and debug console
---@paramenums_tabletable<string, table<string, number>> @ the actual enums table to hook
---@paramenums_table_namestring @ the name of the enums table used for displaying and the global variable name
localfunctionhook_enums(enums_table, enums_table_name)
__DebugAdapter.defineGlobal(enums_table_name)
_ENV[enums_table_name] =enums_table-- for the variables view and debug console
locallookups= {}
-- metatable for all enum values to nicely display their actual name in debug views
localmeta= {
__eq=function(left, right)
returnleft.__value==right.__valueandleft.__enum_name==right.__enum_name
end,
__debugline=function(enum_value)
localenum_name=enum_value.__enum_name
returnenums_table_name.."." ..enum_name.."." ..lookups[enum_name][enum_value.__value]
end,
__debugchildren=false,
__debugtype="number",
}
-- replace all enum values in the enums_table with tables
-- which are given the metatable above
forenum_name, valuesinnext, enums_tabledo
locallookup= {}
fork, vinnext, valuesdo
lookup[v] =k
end
lookups[enum_name] =lookup
forvalue_name, valueinnext, valuesdo
localnew_value= {
__is_custom_enum=true,
__enum_name=enum_name,
__value=value,
}
values[value_name] =setmetatable(new_value, meta)
end
end
-- hook functions to set metatables in the factorio global table
localhook_enums_in_table_recursive
localfunctiontry_hook_value(value, processed_tables)
ifprocessed_tables[value] thenreturnend
iftype(value) =="table" then
processed_tables[value] =true
ifvalue.__is_custom_enumthen
setmetatable(value, meta)
else
hook_enums_in_table_recursive(value, processed_tables)
end
end
end
functionhook_enums_in_table_recursive(t, processed_tables)
fork, vinnext, tdo
try_hook_value(k, processed_tables)
try_hook_value(v, processed_tables)
end
end
localfunctionhook_enums_in_global()
hook_enums_in_table_recursive(global, {})
end
-- replace on_load with a custom one in order for
-- the previous hook functions to actually get run
localon_load=script.on_load
localcurrently_no_actual_handler=false
functionscript.on_load(func)
ifcurrently_no_actual_handlerthen
on_load(nil)
end
currently_no_actual_handler=notfunc
iffuncthen
on_load(function()
hook_enums_in_global()
returnfunc()
end)
else
on_load(hook_enums_in_global)
end
end
script.on_load(nil)
-- if script.on_load was already called before this then RIP that handler
end
return {
hook_enums=hook_enums,
}