-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcomms.lua
379 lines (312 loc) · 9.99 KB
/
comms.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
------------------------------------------------------------
-- DressUp by Sonaza (https://sonaza.com)
-- Licensed under MIT License
-- See attached license text in file LICENSE
------------------------------------------------------------
local ADDON_NAME, Addon = ...;
local ADDON_CHANNEL_PREFIX = ADDON_NAME;
local ADDON_MESSAGE_SENT = {};
local ADDON_MESSAGE_CALLBACKS = {};
local MESSAGE_TYPES = {
QUERY_VERSION = 0x001,
SEND_PREVIEW_ITEMS = 0x002,
};
local AceSerializer = LibStub("AceSerializer-3.0");
PENDING_PREVIEWS = {};
function Addon:PreviewReceivedListID(previewID)
if(not PENDING_PREVIEWS[previewID]) then return end
local preview = PENDING_PREVIEWS[previewID];
Addon:LoadItemList(preview.items);
end
StaticPopupDialogs["DRESSUP_VIEW_WHISPERED_PREVIEW"] = {
text = "%s sent you their previewed items. Click accept if you wish to preview them.",
button1 = ACCEPT,
button2 = CANCEL,
OnAccept = function(self)
-- Get newest preview
Addon:PreviewReceivedListID(#PENDING_PREVIEWS);
end,
timeout = 0,
exclusive = 0,
hideOnEscape = 1,
};
local function CapitalizeWord(word)
if(strlen(word) <= 1) then return string.upper(word) end
return string.upper(word:sub(1, 1)) .. string.lower(word:sub(2));
end
local function Capitalize(str)
if(strlen(str) <= 1) then return string.upper(str) end
str = string.gsub(str, "-", " ");
local resultwords = {};
local words = { strsplit(" ", str) };
for _, word in ipairs(words) do
tinsert(resultwords, CapitalizeWord(word));
end
if(#resultwords == 1) then
return resultwords[1];
end
local result = table.concat(resultwords, "-", 1, 2);
if(#resultwords >= 3) then
result = result .. " " .. table.concat(resultwords, " ", 3);
end
return result;
end
StaticPopupDialogs["DRESSUP_ASK_WHISPER_TARGET"] = {
text = "Give outfit preview recipient's name (with realm if needed):",
button1 = ACCEPT,
button2 = CANCEL,
hasEditBox = 1,
OnAccept = function(self, data)
local recipient = Capitalize(strtrim(self.editBox:GetText()));
if(strlen(recipient) > 0) then
Addon:SendPreviewedItems(recipient);
end
end,
EditBoxOnEnterPressed = function(self, data)
local parent = self:GetParent();
local recipient = Capitalize(strtrim(parent.editBox:GetText()));
if(strlen(recipient) > 0) then
Addon:SendPreviewedItems(recipient);
end
parent:Hide();
end,
OnShow = function(self, data)
self.editBox:SetFocus();
end,
OnHide = function(self, data)
ChatEdit_FocusActiveWindow();
self.editBox:SetText("");
end,
timeout = 0,
exclusive = 0,
hideOnEscape = 1,
};
function Addon:CheckValidRecipientRealm(recipient)
local name, realm = strsplit("-", recipient);
-- If no realm then it's assumed it is current realm and thus is valid
if(not realm) then
return true;
end
realm = string.gsub(realm, " ", "");
local connectedRealms = Addon:GetConnectedRealms();
for _, connectedRealm in ipairs(connectedRealms) do
if(string.lower(realm) == string.lower(connectedRealm)) then
return true;
end
end
return false;
end
function Addon:InitializeComms()
Addon:RegisterEvent("CHAT_MSG_ADDON");
Addon:RegisterEvent("CHAT_MSG_SYSTEM");
C_ChatInfo.RegisterAddonMessagePrefix("DressUp");
Addon:RegisterMessageCallback(MESSAGE_TYPES.QUERY_VERSION, function(payload, sender)
Addon:ReplyToMessage(payload, {
version = GetAddOnMetadata("DressUp", "Version"),
});
end);
Addon:RegisterMessageCallback(MESSAGE_TYPES.SEND_PREVIEW_ITEMS, function(payload, sender)
tinsert(PENDING_PREVIEWS, {
from = sender,
items = payload.items,
});
local previewID = #PENDING_PREVIEWS;
Addon:AddMessage("Received preview from %s: |Hdressup:%d|h|cffffc809[View]|r|h.", sender, previewID);
if(Addon.db.global.PromptForPreviews and not InCombatLockdown()) then
StaticPopup_Show("DRESSUP_VIEW_WHISPERED_PREVIEW", sender);
end
end);
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER_INFORM", function(self, ...)
return Addon:FilterWhispers(...);
end);
end
local ShouldHideWhisper = false;
function Addon:FilterWhispers(event, message, target)
if(ShouldHideWhisper) then
-- if(strfind(string.lower(target), string.lower(ShouldHideWhisper)) ~= nil) then
ShouldHideWhisper = false;
return true;
-- end
end
return false;
end
hooksecurefunc("ChatFrame_OnHyperlinkShow", function(self, link, text, button)
if(link and link:sub(0, 7) ~= "dressup") then
return;
end
if(IsShiftKeyDown()) then
return;
end
local link, previewID = strsplit(":", link);
previewID = tonumber(previewID);
if(previewID) then
Addon:PreviewReceivedListID(previewID);
end
end);
local OriginalSetHyperlink = ItemRefTooltip.SetHyperlink
function ItemRefTooltip:SetHyperlink(link, ...)
if(link and link:sub(0, 7) == "dressup") then
return;
end
return OriginalSetHyperlink(self, link, ...);
end
local OriginalHandleModifiedItemClick = HandleModifiedItemClick
function HandleModifiedItemClick(link, ...)
if(link and type(link) == "string" and link:find("|Hdressup")) then
return;
end
return OriginalHandleModifiedItemClick(link, ...);
end
function Addon:GetConnectedRealms()
local realms = GetAutoCompleteRealms();
if(realms) then
return realms;
end
return { GetRealmName() };
end
local ERR_CHAT_PLAYER_NOT_FOUND_PATTERN = string.gsub(ERR_CHAT_PLAYER_NOT_FOUND_S, "%%s", "(.+)");
function Addon:CHAT_MSG_SYSTEM(event, msg)
local playerName = string.match(msg, ERR_CHAT_PLAYER_NOT_FOUND_PATTERN);
if(playerName) then
for id, data in pairs(ADDON_MESSAGE_SENT) do
if(string.lower(data.target) == string.lower(playerName)) then
ADDON_MESSAGE_SENT[id] = nil;
return;
end
end
end
end
function Addon:GetUniqueMessageID()
local messageID;
repeat
messageID = random(100, 999);
until(ADDON_MESSAGE_SENT[messageID] == nil);
return messageID;
end
function Addon:ReplyToMessage(oldpayload, newpayload, callbacks)
newpayload.replyID = oldpayload.messageID;
Addon:SendAddonMessage(oldpayload.sender, newpayload, callbacks);
end
function Addon:SendAddonMessage(target, payload, callbacks)
if(not target or not payload) then return end
local messageID = Addon:GetUniqueMessageID();
payload.messageID = messageID;
local serialized = AceSerializer:Serialize(payload);
if(strlen(serialized) > 250) then
error(("Serialized string length exceeds message limit."), 2)
end
if(callbacks) then
ADDON_MESSAGE_SENT[messageID] = {
tag = payload.tag,
payload = serialized,
target = target,
timestamp = GetTime(),
callbacks = {
onReply = callbacks.onReply,
onTimeout = callbacks.onTimeout,
},
};
end
C_ChatInfo.SendAddonMessage(ADDON_CHANNEL_PREFIX, serialized, "WHISPER", target);
C_Timer.After(1.0, function()
-- If message still exists on the table then it time outed
local data = ADDON_MESSAGE_SENT[messageID];
if(data) then
if(data.callbacks.onTimeout) then
pcall(data.callbacks.onTimeout, target);
end
ADDON_MESSAGE_SENT[messageID] = nil;
end
end);
return messageID;
end
function DressUpFrameWhisperButton_OnEnter(self)
GameTooltip:SetOwner(self, "ANCHOR_BOTTOMRIGHT");
GameTooltip:AddLine("Send Outfit");
if(self:GetID() == 1) then
GameTooltip:AddLine("Send currently previewed items to another player.", 1, 1, 1, true);
elseif(self:GetID() == 2) then
GameTooltip:AddLine("Send current transmog to another player.", 1, 1, 1, true);
end
GameTooltip:AddLine(" ");
GameTooltip:AddLine("Note: target recipient must have DressUp for this feature to work.", 1, 1, 1, true);
GameTooltip:Show();
end
function DressUpFrameWhisperButton_OnClick(self)
if(not DressUpFrame:IsVisible()) then
DressUpFrame_Show();
end
if(self:GetID() == 1) then
DressUpPreviewWhisperButtonAlertCloseButton_OnClick();
end
StaticPopup_Show("DRESSUP_ASK_WHISPER_TARGET");
GameTooltip:Hide();
end
function Addon:SendPreviewedItems(target)
local validRecipient = Addon:CheckValidRecipientRealm(target);
if(not validRecipient) then
Addon:AddMessage("%s is not a valid recipient. Previews cannot be sent to unconnected realms.", target);
return;
end
-- Check for target version first
Addon:PokeForVersion(target,
{
onReply = function(payload, sender)
Addon:DoSendPreviewedItems(target);
Addon:AddMessage("Sent currently previewed items to %s.", target);
end,
onTimeout = function()
ShouldHideWhisper = true;
local msg = ("%s wishes to whisper their previewed items to you but you do not have DressUp or it is outdated. "..
"Get the addon from http://wow.curseforge.com/addons/dressup/"):format(UnitName("player"));
SendChatMessage(msg, "WHISPER", nil, target);
Addon:AddMessage("%s doesn't have DressUp or it is outdated. They were notified of it.", target);
end,
});
end
function Addon:PokeForVersion(target, callbacks)
Addon:SendAddonMessage(target, {
tag = MESSAGE_TYPES.QUERY_VERSION
}, callbacks);
end
function Addon:DoSendPreviewedItems(target)
local itemlist = Addon:GetPreviewedItemsList();
Addon:SendAddonMessage(target, {
tag = MESSAGE_TYPES.SEND_PREVIEW_ITEMS,
items = itemlist,
});
end
function Addon:CHAT_MSG_ADDON(event, prefix, message, msgtype, sender)
if(prefix ~= ADDON_CHANNEL_PREFIX) then return end
local deserializeSuccess, payload = AceSerializer:Deserialize(message);
if(not deserializeSuccess) then
return;
end
payload.sender = sender;
if(payload.replyID) then
-- Execute reply callback
local data = ADDON_MESSAGE_SENT[payload.replyID];
if(data.callbacks.onReply) then
pcall(data.callbacks.onReply, payload, sender);
end
ADDON_MESSAGE_SENT[payload.replyID] = nil;
end
if(payload.tag) then
local callback = ADDON_MESSAGE_CALLBACKS[payload.tag];
if(callback) then
pcall(callback, payload, sender);
end
end
end
function Addon:RegisterMessageCallback(tag, func)
if(not tag) then
error("DressUp:RegisterMessageCallback(tag, func): Invalid tag.", 2)
end
if(ADDON_MESSAGE_CALLBACKS[tag]) then
error(("DressUp:RegisterMessageCallback(tag, func): %s is already registered."):format(tag), 2)
end
if(not func) then
error("DressUp:RegisterMessageCallback(tag, func): Missing callback function.", 2)
end
ADDON_MESSAGE_CALLBACKS[tag] = func;
end