/
MooZone-1.0.lua
199 lines (169 loc) · 5.54 KB
/
MooZone-1.0.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
--[[--------------------------------------------------------------------
Copyright (C) 2018 Johnny C. Lam.
See the file LICENSE.txt for copying permission.
--]]--------------------------------------------------------------------
local MAJOR, MINOR = "MooZone-1.0", 2
assert(LibStub, MAJOR .. " requires LibStub")
assert(LibStub("CallbackHandler-1.0", true), MAJOR .. " requires CallbackHandler-1.0")
local lib, oldminor = LibStub:NewLibrary(MAJOR, MINOR)
if not lib then return end
------------------------------------------------------------------------
local format = string.format
local next = next
local pairs = pairs
local setmetatable = setmetatable
local strfind = string.find
local strjoin = strjoin
local strmatch = string.match
local tonumber = tonumber
local tostring = tostring
local tostringall = tostringall
local type = type
local wipe = wipe
-- GLOBALS: _G
-- GLOBALS: GetAddOnMetadata
local FONT_COLOR_CODE_CLOSE = FONT_COLOR_CODE_CLOSE
local GREEN_FONT_COLOR_CODE = GREEN_FONT_COLOR_CODE
local NORMAL_FONT_COLOR_CODE = NORMAL_FONT_COLOR_CODE
--[[--------------------------------------------------------------------
Debugging code from LibResInfo-1.0 by Phanx.
https://github.com/Phanx/LibResInfo
--]]--------------------------------------------------------------------
local isAddon = GetAddOnMetadata(MAJOR, "Version")
local DEBUG_LEVEL = isAddon and 2 or 0
local DEBUG_FRAME = ChatFrame3
local function debug(level, text, ...)
if level <= DEBUG_LEVEL then
if ... then
if type(text) == "string" and strfind(text, "%%[dfqsx%d%.]") then
text = format(text, ...)
else
text = strjoin(" ", tostringall(text, ...))
end
else
text = tostring(text)
end
DEBUG_FRAME:AddMessage(GREEN_FONT_COLOR_CODE .. MAJOR .. FONT_COLOR_CODE_CLOSE .. " " .. text)
end
end
if isAddon then
-- GLOBALS: SLASH_MOOZONE1
-- GLOBALS: SlashCmdList
SLASH_MOOZONE1 = "/moozone"
SlashCmdList.MOOZONE = function(input)
input = tostring(input or "")
local CURRENT_CHAT_FRAME
for i = 1, 10 do
local cf = _G["ChatFrame"..i]
if cf and cf:IsVisible() then
CURRENT_CHAT_FRAME = cf
break
end
end
local of = DEBUG_FRAME
DEBUG_FRAME = CURRENT_CHAT_FRAME
if strmatch(input, "^%s*[0-9]%s*$") then
local v = tonumber(input)
debug(0, "Debug level set to", input)
DEBUG_LEVEL = v
DEBUG_FRAME = of
return
end
local f = _G[input]
if type(f) == "table" and type(f.AddMessage) == "function" then
debug(0, "Debug frame set to", input)
DEBUG_FRAME = f
return
end
debug(0, "Version " .. MINOR .. " loaded. Usage:")
debug(0, format("%s%s %s%s - change debug verbosity, valid range is 0-6",
NORMAL_FONT_COLOR_CODE, SLASH_MOOZONE1, DEBUG_LEVEL, FONT_COLOR_CODE_CLOSE))
debug(0, format("%s%s %s%s -- change debug output frame",
NORMAL_FONT_COLOR_CODE, SLASH_MOOZONE1, of:GetName(), FONT_COLOR_CODE_CLOSE))
DEBUG_FRAME = of
end
end
------------------------------------------------------------------------
lib.callbacks = lib.callbacks or LibStub("CallbackHandler-1.0"):New(lib)
lib.callbacksInUse = lib.callbacksInUse or {}
local eventFrame = lib.eventFrame or CreateFrame("Frame")
lib.eventFrame = eventFrame
eventFrame:UnregisterAllEvents()
eventFrame:SetScript("OnEvent", function(self, event, ...)
return self[event] and self[event](self, event, ...)
end)
function lib.callbacks:OnUsed(lib, callback)
if not next(lib.callbacksInUse) then
debug(1, "Callbacks in use! Starting up...")
eventFrame:RegisterEvent("PLAYER_ENTERING_WORLD")
eventFrame:RegisterEvent("ZONE_CHANGED_NEW_AREA")
end
lib.callbacksInUse[callback] = true
end
function lib.callbacks:OnUnused(lib, callback)
lib.callbacksInUse[callback] = nil
if not next(lib.callbacksInUse) then
debug(1, "No callbacks in use. Shutting down...")
eventFrame:UnregisterAllEvents()
end
end
------------------------------------------------------------------------
-- Localized names for every possible zone type.
local zoneNames = lib.zoneNames or {}
lib.zoneNames = zoneNames
do
zoneNames.world = _G.CHANNEL_CATEGORY_WORLD
zoneNames.arena = _G.ARENA
zoneNames.battleground = _G.BATTLEFIELDS
zoneNames.dungeon = _G.CALENDAR_TYPE_DUNGEON
zoneNames.raid = _G.RAID
zoneNames.scenario = _G.SCENARIOS
zoneNames.lfg_dungeon = _G.LOOKING_FOR_DUNGEON
zoneNames.lfg_raid = _G.RAID_FINDER
end
function lib:GetLocalizedZone(zone)
return zoneNames[zone] or zone
end
function lib:ZoneIterator()
return pairs(zoneNames)
end
------------------------------------------------------------------------
-- GLOBALS: IsInGroup
-- GLOBALS: IsInInstance
-- GLOBALS: IsInRaid
local LE_PARTY_CATEGORY_INSTANCE = LE_PARTY_CATEGORY_INSTANCE
local zone = "world"
function lib:GetZone()
return zone
end
local function UpdateZone(self, event)
local newZone
if IsInRaid(LE_PARTY_CATEGORY_INSTANCE) then
newZone = "lfg_raid"
elseif IsInGroup(LE_PARTY_CATEGORY_INSTANCE) then
newZone = "lfg_dungeon"
else
-- instanceType is "arena", "none", "party", "pvp", "raid", or "scenario".
local _, instanceType = IsInInstance()
if instanceType == "arena" then
newZone = "arena"
elseif instanceType == "party" then
newZone = "dungeon"
elseif instanceType == "pvp" then
newZone = "battleground"
elseif instanceType == "scenario" then
newZone = "scenario"
elseif IsInRaid() then
newZone = "raid"
else
newZone = "world"
end
end
if zone ~= newZone then
debug(2, "MooZone_ZoneChanged", zone, newZone)
lib.callbacks:Fire("MooZone_ZoneChanged", zone, newZone)
zone = newZone
end
end
eventFrame.PLAYER_ENTERING_WORLD = UpdateZone
eventFrame.ZONE_CHANGED_NEW_AREA = UpdateZone