Skip to content

Commit bc6c298

Browse files
committed
don't stringify members of unions
1 parent a84b80f commit bc6c298

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

docs/gui/gm-editor.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ Options
7575
Don't attempt to provide helpful string representations of potentially
7676
unsafe fields like language_name when browsing the data structures. Specify
7777
this option when you know you will be browsing garbage data that could lead
78-
to crashes if accessed for stringification.
78+
to crashes if accessed for stringification. Note that fields in union data
79+
structures are never stringified.
7980

8081
Screenshot
8182
----------

gui/gm-editor.lua

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -624,30 +624,32 @@ function GmEditorUi:onInput(keys)
624624
end
625625

626626
function GmEditorUi:getStringValue(trg, field)
627-
local obj=trg.target
627+
local obj = trg.target
628+
local is_union = obj._type._union
628629

629630
local text=tostring(obj[field])
630631
pcall(function()
631-
if obj._field ~= nil then
632-
local f = obj:_field(field)
633-
if self.helpers then
634-
if df.coord:is_instance(f) then
635-
text=('(%d, %d, %d) %s'):format(f.x, f.y, f.z, text)
636-
elseif df.coord2d:is_instance(f) then
637-
text=('(%d, %d) %s'):format(f.x, f.y, text)
638-
elseif df.language_name:is_instance(f) then
639-
text=('%s (%s) %s'):format(dfhack.TranslateName(f, false), dfhack.TranslateName(f, true), text)
640-
end
641-
end
642-
local enum=f._type
643-
if enum._kind=="enum-type" then
644-
text=text.." ("..tostring(enum[obj[field]])..")"
645-
end
646-
local ref_target=f.ref_target
647-
if ref_target then
648-
text=text.. " (ref-target: "..getmetatable(ref_target)..")"
632+
if obj._field == nil then return end
633+
local f = obj:_field(field)
634+
if self.helpers and not is_union then
635+
if df.coord:is_instance(f) then
636+
text=('(%d, %d, %d) %s'):format(f.x, f.y, f.z, text)
637+
elseif df.coord2d:is_instance(f) then
638+
text=('(%d, %d) %s'):format(f.x, f.y, text)
639+
elseif df.language_name:is_instance(f) then
640+
text=('%s (%s) %s'):format(dfhack.TranslateName(f, false), dfhack.TranslateName(f, true), text)
649641
end
650642
end
643+
local enum = f._type
644+
if enum._kind=="enum-type" then
645+
text=text.." ("..tostring(enum[obj[field]])..")"
646+
end
647+
-- this will throw for types that have no ref target; pcall will catch it, but make sure this bit stays
648+
-- at the end of the pcall function body
649+
local ref_target=f.ref_target
650+
if ref_target then
651+
text=text.. " (ref-target: "..getmetatable(ref_target)..")"
652+
end
651653
end)
652654
return text
653655
end

0 commit comments

Comments
 (0)