Skip to content

Commit

Permalink
don't stringify members of unions
Browse files Browse the repository at this point in the history
  • Loading branch information
myk002 committed Oct 3, 2024
1 parent a84b80f commit bc6c298
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
3 changes: 2 additions & 1 deletion docs/gui/gm-editor.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ Options
Don't attempt to provide helpful string representations of potentially
unsafe fields like language_name when browsing the data structures. Specify
this option when you know you will be browsing garbage data that could lead
to crashes if accessed for stringification.
to crashes if accessed for stringification. Note that fields in union data
structures are never stringified.

Screenshot
----------
Expand Down
40 changes: 21 additions & 19 deletions gui/gm-editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -624,30 +624,32 @@ function GmEditorUi:onInput(keys)
end

function GmEditorUi:getStringValue(trg, field)
local obj=trg.target
local obj = trg.target
local is_union = obj._type._union

local text=tostring(obj[field])
pcall(function()
if obj._field ~= nil then
local f = obj:_field(field)
if self.helpers then
if df.coord:is_instance(f) then
text=('(%d, %d, %d) %s'):format(f.x, f.y, f.z, text)
elseif df.coord2d:is_instance(f) then
text=('(%d, %d) %s'):format(f.x, f.y, text)
elseif df.language_name:is_instance(f) then
text=('%s (%s) %s'):format(dfhack.TranslateName(f, false), dfhack.TranslateName(f, true), text)
end
end
local enum=f._type
if enum._kind=="enum-type" then
text=text.." ("..tostring(enum[obj[field]])..")"
end
local ref_target=f.ref_target
if ref_target then
text=text.. " (ref-target: "..getmetatable(ref_target)..")"
if obj._field == nil then return end
local f = obj:_field(field)
if self.helpers and not is_union then
if df.coord:is_instance(f) then
text=('(%d, %d, %d) %s'):format(f.x, f.y, f.z, text)
elseif df.coord2d:is_instance(f) then
text=('(%d, %d) %s'):format(f.x, f.y, text)
elseif df.language_name:is_instance(f) then
text=('%s (%s) %s'):format(dfhack.TranslateName(f, false), dfhack.TranslateName(f, true), text)
end
end
local enum = f._type
if enum._kind=="enum-type" then
text=text.." ("..tostring(enum[obj[field]])..")"
end
-- this will throw for types that have no ref target; pcall will catch it, but make sure this bit stays
-- at the end of the pcall function body
local ref_target=f.ref_target
if ref_target then
text=text.. " (ref-target: "..getmetatable(ref_target)..")"
end
end)
return text
end
Expand Down

0 comments on commit bc6c298

Please sign in to comment.