Skip to content

Instantly share code, notes, and snippets.

@justarandomgeek
Last active October 24, 2019 20:08
Show Gist options
  • Save justarandomgeek/3f4a3672dd3e98a301a8dfb531babf01 to your computer and use it in GitHub Desktop.
Save justarandomgeek/3f4a3672dd3e98a301a8dfb531babf01 to your computer and use it in GitHub Desktop.
function LuaObjectType(obj)
local t = rawget(obj, "luaObjectType")
if t == nil then
--[[No way to avoid a pcall unfortunately]]
local success, help = pcall(function(obj) return obj.help() end, obj)
local luaObjectType
if not success then
--[[Extract type from error message]]
t = string.sub(help, 1, string.find(help, " ") - 1)
else
--[[Extract type from help message]]
t = string.sub(help, 10, string.find(help, ":") - 1)
end
rawset(obj, "luaObjectType", t)
end
return t
end
--[[This is a magic string used by factorio to identify LuaObjects]]
isluaobjectmagic = "802384732948701238470123"
function extendedtype(obj)
local t = type(obj)
if t == "table" and obj.isluaobject == isluaobjectmagic and
--[[In addition to the magic, verify it really has the expected userdata and hidden metatable]]
type(obj.__self) == "userdata" and getmetatable(obj) == "private" then
return LuaObjectType(obj)
end
return t
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment