Not all libraries are compatible with gearswap. Normally, when texts.lua is loaded by an addon the following takes place:
- a table locally named texts is created and then keyed with all of the methods of the texts library
- a function is added to the list of functions that are run when the mouse event is called to handle the drag-and-drop behavior of text objects
- the local texts table is returned (which is why you need to write "texts = require('texts')" and not "require('texts')")
All functions, variables, etc. exist within an environment (a fancy word for a table). The function added to the mouse event
can access the functions, variables, etc. defined in texts.lua because they share an environment. However, because everything within the texts.lua is handled locally, anything past the line "texts = require('texts')"
cannot access the code within texts.lua because it does not share an environment.
When a user file is loaded by gearswap, the code is modified by some preexisting code (take a quick look only at the bold parts):
Code:
user_env = {gearswap = _G, _global = _global, _settings = _settings,_addon=_addon,
-- Player functions
equip = equip, cancel_spell=cancel_spell, change_target=change_target, cast_delay=cast_delay,
print_set=print_set,set_combine=set_combine,disable=disable,enable=user_enable,
send_command=send_cmd_user,windower=user_windower,include=include_user,
midaction=user_midaction,pet_midaction=user_pet_midaction,set_language=set_language,
show_swaps = show_swaps,debug_mode=debug_mode,include_path=user_include_path,
register_unhandled_command=user_unhandled_command,
-- Library functions
string=string,math=math,table=table,set=set,list=list,T=T,S=S,L=L,pack=pack,
os=os,texts=texts,type=type,tostring=tostring,tonumber=tonumber,pairs=pairs,
ipairs=ipairs, print=print, add_to_chat=add_to_chat_user,unpack=unpack,next=next,
select=select,lua_base_path=windower.addon_path,empty=empty,file=file,
loadstring=loadstring,assert=assert,error=error,pcall=pcall,io=io,dofile=dofile,
debug=debug,coroutine=coroutine,setmetatable=setmetatable,getmetatable=getmetatable,
rawset=rawset,rawget=rawget,require=include_user,
Code:
user_windower = {register_event = register_event_user, raw_register_event = raw_register_event_user,
unregister_event = unregister_event_user, send_command = send_cmd_user,add_to_chat=add_to_chat_user}
setmetatable(user_windower,{__index=windower})
Code:
function register_event_user(str,func)
if type(func)~='function' then
error('\nGearSwap: windower.register_event() was passed an invalid value ('..tostring(func)..'). (must be a function)', 2)
elseif type(str) ~= 'string' then
error('\nGearSwap: windower.register_event() was passed an invalid value ('..tostring(str)..'). (must be a string)', 2)
end
local id = windower.register_event(str,user_equip_sets(func))
registered_user_events[id] = true
return id
end
The windower table that the user file has access to is a slightly different version of the windower table gearswap has access to. Crucially, when the register_event function in the windower table is called by texts.lua (being loaded into your user file) to handle the drag-and-drop behavior, the modified windower.register_event calls this function, which changes the text library's mouse function's environment:
Code:
function user_equip_sets(func)
return setfenv(function(...)
if not gearswap.gearswap_disabled then
gearswap.refresh_globals(true)
return gearswap.equip_sets(func,nil,...)
end
end,user_env)
end
(setfenv is short for set_function_environment). As a result, the function can't see the code within texts.lua anymore, and you receive an error when it tries to look up a value in the table 'meta', which exists within texts.lua but not within the user file's user_env environment.