?? dongle.lua
字號:
sv[section] = nil end end end end end endendfunction Dongle.SetProfile(db, name) assert(3, databases[db], string.format(L["MUST_CALLFROM_DBOBJECT"], "SetProfile")) argcheck(name, 2, "string") local old = db.profile local defaults = db.defaults and db.defaults.profile if defaults then -- Remove the defaults from the old profile removeDefaults(old, defaults) end db.profile = nil db.keys["profile"] = name db.sv.profileKeys[db.keys.char] = name Dongle:TriggerMessage("DONGLE_PROFILE_CHANGED", db, db.parent, db.sv_name, db.keys.profile)endfunction Dongle.GetProfiles(db, t) assert(3, databases[db], string.format(L["MUST_CALLFROM_DBOBJECT"], "GetProfiles")) argcheck(t, 2, "table", "nil") t = t or {} local i = 1 for profileKey in pairs(db.sv.profiles) do t[i] = profileKey i = i + 1 end return t, i - 1endfunction Dongle.GetCurrentProfile(db) assert(e, databases[db], string.format(L["MUST_CALLFROM_DBOBJECT"], "GetCurrentProfile")) return db.keys.profileendfunction Dongle.DeleteProfile(db, name) assert(3, databases[db], string.format(L["MUST_CALLFROM_DBOBJECT"], "DeleteProfile")) argcheck(name, 2, "string") if db.keys.profile == name then error(L["CANNOT_DELETE_ACTIVE_PROFILE"], 2) end assert(type(db.sv.profiles[name]) == "table", L["DELETE_NONEXISTANT_PROFILE"]) db.sv.profiles[name] = nil Dongle:TriggerMessage("DONGLE_PROFILE_DELETED", db, db.parent, db.sv_name, name)endfunction Dongle.CopyProfile(db, name) assert(3, databases[db], string.format(L["MUST_CALLFROM_DBOBJECT"], "CopyProfile")) argcheck(name, 2, "string") assert(3, db.keys.profile ~= name, L["SAME_SOURCE_DEST"]) assert(3, type(db.sv.profiles[name]) == "table", string.format(L["PROFILE_DOES_NOT_EXIST"], name)) local profile = db.profile local source = db.sv.profiles[name] copyDefaults(profile, source, true) Dongle:TriggerMessage("DONGLE_PROFILE_COPIED", db, db.parent, db.sv_name, name, db.keys.profile)endfunction Dongle.ResetProfile(db) assert(3, databases[db], string.format(L["MUST_CALLFROM_DBOBJECT"], "ResetProfile")) local profile = db.profile for k,v in pairs(profile) do profile[k] = nil end local defaults = db.defaults and db.defaults.profile if defaults then copyDefaults(profile, defaults) end Dongle:TriggerMessage("DONGLE_PROFILE_RESET", db, db.parent, db.sv_name, db.keys.profile)endfunction Dongle.ResetDB(db, defaultProfile) assert(3, databases[db], string.format(L["MUST_CALLFROM_DBOBJECT"], "ResetDB")) argcheck(defaultProfile, 2, "nil", "string") local sv = db.sv for k,v in pairs(sv) do sv[k] = nil end local parent = db.parent initdb(parent, db.sv_name, db.defaults, defaultProfile, db) Dongle:TriggerMessage("DONGLE_DATABASE_RESET", db, parent, db.sv_name, db.keys.profile) Dongle:TriggerMessage("DONGLE_PROFILE_CHANGED", db, db.parent, db.sv_name, db.keys.profile) return dbendfunction Dongle.RegisterNamespace(db, name, defaults) assert(3, databases[db], string.format(L["MUST_CALLFROM_DBOBJECT"], "RegisterNamespace")) argcheck(name, 2, "string") argcheck(defaults, 3, "nil", "string") local sv = db.sv if not sv.namespaces then sv.namespaces = {} end if not sv.namespaces[name] then sv.namespaces[name] = {} end local newDB = initdb(db, sv.namespaces[name], defaults, db.keys.profile) -- Remove the :SetProfile method from newDB newDB.SetProfile = nil if not db.children then db.children = {} end table.insert(db.children, newDB) return newDBend--[[------------------------------------------------------------------------- Slash Command System---------------------------------------------------------------------------]]local slashCmdMethods = { "InjectDBCommands", "RegisterSlashHandler", "PrintUsage",}local function OnSlashCommand(cmd, cmd_line) if cmd.patterns then for idx,tbl in pairs(cmd.patterns) do local pattern = tbl.pattern if string.match(cmd_line, pattern) then local handler = tbl.handler if type(tbl.handler) == "string" then local obj -- Look in the command object before we look at the parent object if cmd[handler] then obj = cmd end if cmd.parent[handler] then obj = cmd.parent end if obj then obj[handler](obj, string.match(cmd_line, pattern)) end else handler(string.match(cmd_line, pattern)) end return end end end cmd:PrintUsage()endfunction Dongle:InitializeSlashCommand(desc, name, ...) local reg = lookup[self] assert(3, reg, string.format(L["MUST_CALLFROM_REGISTERED"], "InitializeSlashCommand")) argcheck(desc, 2, "string") argcheck(name, 3, "string") argcheck(select(1, ...), 4, "string") for i = 2,select("#", ...) do argcheck(select(i, ...), i+2, "string") end local cmd = {} cmd.desc = desc cmd.name = name cmd.parent = self cmd.slashes = { ... } for idx,method in pairs(slashCmdMethods) do cmd[method] = Dongle[method] end local genv = getfenv(0) for i = 1,select("#", ...) do genv["SLASH_"..name..tostring(i)] = "/"..select(i, ...) end genv.SlashCmdList[name] = function(...) OnSlashCommand(cmd, ...) end commands[cmd] = true return cmdendfunction Dongle.RegisterSlashHandler(cmd, desc, pattern, handler) assert(3, commands[cmd], string.format(L["MUST_CALLFROM_SLASH"], "RegisterSlashHandler")) argcheck(desc, 2, "string") argcheck(pattern, 3, "string") argcheck(handler, 4, "function", "string") if not cmd.patterns then cmd.patterns = {} end table.insert(cmd.patterns, { ["desc"] = desc, ["handler"] = handler, ["pattern"] = pattern, })endfunction Dongle.PrintUsage(cmd) assert(3, commands[cmd], string.format(L["MUST_CALLFROM_SLASH"], "PrintUsage")) local parent = cmd.parent parent:Echo(cmd.desc.."\n".."/"..table.concat(cmd.slashes, ", /")..":\n") if cmd.patterns then for idx,tbl in ipairs(cmd.patterns) do parent:Echo(" - " .. tbl.desc) end endendlocal dbcommands = { ["copy"] = { L["DBSLASH_PROFILE_COPY_DESC"], L["DBSLASH_PROFILE_COPY_PATTERN"], "CopyProfile", }, ["delete"] = { L["DBSLASH_PROFILE_DELETE_DESC"], L["DBSLASH_PROFILE_DELETE_PATTERN"], "DeleteProfile", }, ["list"] = { L["DBSLASH_PROFILE_LIST_DESC"], L["DBSLASH_PROFILE_LIST_PATTERN"], }, ["reset"] = { L["DBSLASH_PROFILE_RESET_DESC"], L["DBSLASH_PROFILE_RESET_PATTERN"], "ResetProfile", }, ["set"] = { L["DBSLASH_PROFILE_SET_DESC"], L["DBSLASH_PROFILE_SET_PATTERN"], "SetProfile", },}function Dongle.InjectDBCommands(cmd, db, ...) assert(3, commands[cmd], string.format(L["MUST_CALLFROM_SLASH"], "InjectDBCommands")) assert(3, databases[db], string.format(L["BAD_ARGUMENT_DB"], 2, "InjectDBCommands")) local argc = select("#", ...) assert(3, argc > 0, L["INJECTDB_USAGE"]) for i=1,argc do local cmdname = string.lower(select(i, ...)) local entry = dbcommands[cmdname] assert(entry, L["INJECTDB_USAGE"]) local func = entry[3] local handler if cmdname == "list" then handler = function(...) local profiles = db:GetProfiles() db.parent:Print(L["DBSLASH_PROFILE_LIST_OUT"] .. "\n" .. strjoin("\n", unpack(profiles))) end else handler = function(...) db[entry[3]](db, ...) end end cmd:RegisterSlashHandler(entry[1], entry[2], handler) endend--[[------------------------------------------------------------------------- Internal Message/Event Handlers---------------------------------------------------------------------------]]local function PLAYER_LOGOUT(event) Dongle:ClearDBDefaults() for k,v in pairs(registry) do local obj = v.obj if type(obj["Disable"]) == "function" then safecall(obj["Disable"], obj) end endendlocal function PLAYER_LOGIN() Dongle.initialized = true for i=1, #loadorder do local obj = loadorder[i] if type(obj.Enable) == "function" then safecall(obj.Enable, obj) end loadorder[i] = nil endendlocal function ADDON_LOADED(event, ...) for i=1, #loadqueue do local obj = loadqueue[i] table.insert(loadorder, obj) if type(obj.Initialize) == "function" then safecall(obj.Initialize, obj) end loadqueue[i] = nil end if not Dongle.initialized then if type(IsLoggedIn) == "function" then Dongle.initialized = IsLoggedIn() else Dongle.initialized = ChatFrame1.defaultLanguage end end if Dongle.initialized then for i=1, #loadorder do local obj = loadorder[i] if type(obj.Enable) == "function" then safecall(obj.Enable, obj) end loadorder[i] = nil end endendlocal function DONGLE_PROFILE_CHANGED(msg, db, parent, sv_name, profileKey) local children = db.children if children then for i,namespace in ipairs(children) do local old = namespace.profile local defaults = namespace.defaults and namespace.defaults.profile if defaults then -- Remove the defaults from the old profile removeDefaults(old, defaults) end namespace.profile = nil namespace.keys["profile"] = profileKey end endend--[[------------------------------------------------------------------------- DongleStub required functions and registration---------------------------------------------------------------------------]]function Dongle:GetVersion() return major,minor endlocal function Activate(self, old) if old then registry = old.registry or registry lookup = old.lookup or lookup loadqueue = old.loadqueue or loadqueue loadorder = old.loadorder or loadorder events = old.events or events databases = old.databases or databases commands = old.commands or commands messages = old.messages or messages frame = old.frame or CreateFrame("Frame") else frame = CreateFrame("Frame") local reg = {obj = self, name = "Dongle"} registry[major] = reg lookup[self] = reg lookup[major] = reg end self.registry = registry self.lookup = lookup self.loadqueue = loadqueue self.loadorder = loadorder self.events = events self.databases = databases self.commands = commands self.messages = messages self.frame = frame frame:SetScript("OnEvent", OnEvent) local lib = old or self -- Lets make sure the lookup table has us. lookup[lib] = lookup[major] -- Register for events using Dongle itself lib:RegisterEvent("ADDON_LOADED", ADDON_LOADED) lib:RegisterEvent("PLAYER_LOGIN", PLAYER_LOGIN) lib:RegisterEvent("PLAYER_LOGOUT", PLAYER_LOGOUT) lib:RegisterMessage("DONGLE_PROFILE_CHANGED", DONGLE_PROFILE_CHANGED) -- Convert all the modules handles for name,obj in pairs(registry) do for k,v in ipairs(methods) do obj[k] = self[v] end end -- Convert all database methods for db in pairs(databases) do for idx,method in ipairs(dbMethods) do db[method] = self[method] end end -- Convert all slash command methods for cmd in pairs(commands) do for idx,method in ipairs(slashCmdMethods) do cmd[method] = self[method] end endend-- Lets nuke any Dongle deactivate functions, please-- I hate nasty hacks.if DongleStub.versions and DongleStub.versions[major] then local reg = DongleStub.versions[major] reg.deactivate = nilendDongle = DongleStub:Register(Dongle, Activate)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -