-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathexternal-Questie.lua
More file actions
458 lines (445 loc) · 13 KB
/
external-Questie.lua
File metadata and controls
458 lines (445 loc) · 13 KB
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
--[[--
Read communication of Questie
by ALA
CREDIT shagu/pfQuest(MIT LICENSE) @ https://github.com/shagu
--]]--
----------------------------------------------------------------------------------------------------
local __addon, __private = ...;
local MT = __private.MT;
local CT = __private.CT;
local VT = __private.VT;
local DT = __private.DT;
local _G = _G;
local _ = nil;
----------------------------------------------------------------------------------------------------
--> variables
local GetTime = GetTime;
local setmetatable = setmetatable;
local next = next;
local table_concat = table.concat;
local strbyte, strchar, strlen, strsub, strfind = string.byte, string.char, string.len, string.sub, string.find;
local bit_band, bit_lshift, bit_rshift = bit.band, bit.lshift, bit.rshift;
local floor, frexpm ldexp = math.floor, math.frexp, math.ldexp;
local tonumber = tonumber;
local huge = math.huge;
local RegisterAddonMessagePrefix = RegisterAddonMessagePrefix or C_ChatInfo.RegisterAddonMessagePrefix;
local IsAddonMessagePrefixRegistered = IsAddonMessagePrefixRegistered or C_ChatInfo.IsAddonMessagePrefixRegistered;
local GetRegisteredAddonMessagePrefixes = GetRegisteredAddonMessagePrefixes or C_ChatInfo.GetRegisteredAddonMessagePrefixes;
local SendAddonMessage = SendAddonMessage or C_ChatInfo.SendAddonMessage;
local SendAddonMessageLogged = SendAddonMessageLogged or C_ChatInfo.SendAddonMessageLogged;
-->
local l10n = CT.l10n;
-->
MT.BuildEnv("external-Questie");
--> MAIN
--
local ExternalQuestie = { };
-- Decompress
--
local _HT = { };
local function _Hash(value)
local len = strlen(value);
if len <= 0 then
return 0
end
local h = 5381;
for i = 1, len do
h = bit_band((31 * h + strbyte(value, i)), 4294967295);
end
return h;
end
local function _AddHash(value)
local hash = _Hash(value);
_HT[hash] = value;
_HT[value] = hash;
end
--
local function floatBitsToInt(n)
local sign = 0
if n < 0.0 then
sign = 0x80
n = -n
end
local mant, expo = frexp(n)
if mant ~= mant then
-- return _pack(0xFF, 0x88, 0x00, 0x00) -- nan
return 0xFF880000;
elseif mant == huge or expo > 0x80 then
if sign == 0 then
-- return _pack(0x7F, 0x80, 0x00, 0x00) -- inf
return 0x7F800000;
else
-- return _pack(0xFF, 0x80, 0x00, 0x00) -- -inf
return 0xFF800000;
end
elseif (mant == 0.0 and expo == 0) or expo < -0x7E then
-- return _pack(sign, 0x00, 0x00, 0x00)-- zero
return bit_lshift(sign, 24);
else
expo = expo + 0x7E
mant = floor((mant * 2.0 - 1.0) * ldexp(0.5, 24))
return
bit_lshift(sign + floor(expo / 0x2), 24)
+ bit_lshift((expo % 0x2) * 0x80 + floor(mant / 0x10000), 16)
+ bit_lshift(floor(mant / 0x100) % 0x100, 8)
+ mant % 0x100;
end
end
local function intBitsToFloat(int)
local b1 = bit_rshift(val, 24) % 256;
local b2 = bit_rshift(val, 16) % 256;
local b3 = bit_rshift(val, 8) % 256;
local b4 = val % 256;
local sign = b1 > 0x7F
local expo = (b1 % 0x80) * 0x2 + floor(b2 / 0x80)
local mant = ((b2 % 0x80) * 0x100 + b3) * 0x100 + b4
if sign then
sign = -1
else
sign = 1
end
local n
if mant == 0 and expo == 0 then
n = sign * 0.0
elseif expo == 0xFF then
if mant == 0 then
n = sign * huge
else
n = 0.0 / 0.0
end
else
n = sign * ldexp(1.0 + mant / 0x800000, expo - 0x7F)
end
return n
end
--
local _ReadByte, _ReadShort, _ReadInt, _ReadLong, _ReadTinyString, _ReadShortString;
local _ReadObject, _ReadTable, _ReadArray;
--
function _ReadByte(buffer)
local val = buffer[buffer.ptr];
buffer.ptr = buffer.ptr + 1;
if val == 238 then
return 0;
elseif val == 237 then
val = buffer[buffer.ptr];
buffer.ptr = buffer.ptr + 1;
if val == 1 then
return 237;
elseif val == 2 then
return 238;
end
else
return val;
end
end
function _ReadShort(buffer)
return bit_lshift(_ReadByte(buffer), 8) + _ReadByte(buffer);
end
function _ReadInt(buffer)
return bit_lshift(_ReadByte(buffer), 24) + bit_lshift(_ReadByte(buffer), 16) + bit_lshift(_ReadByte(buffer), 8) + _ReadByte(buffer);
end
function _ReadLong(buffer)
return bit_lshift(_ReadByte(buffer), 56) + bit_lshift(_ReadByte(buffer), 48) + bit_lshift(_ReadByte(buffer), 40) + bit_lshift(_ReadByte(buffer), 32)
+ bit_lshift(_ReadByte(buffer), 24) + bit_lshift(_ReadByte(buffer), 16) + bit_lshift(_ReadByte(buffer), 8) + _ReadByte(buffer);
end
function _ReadTinyString(buffer)
local len = _ReadByte(buffer);
local str = { };
for i = 1, len do
str[i] = strchar(_ReadByte(buffer));
end
return table_concat(str);
end
function _ReadShortString(buffer)
local len = _ReadShort(buffer);
local str = { };
for i = 1, len do
str[i] = strchar(_ReadByte(buffer));
end
return table_concat(str);
end
--
local _MethodTable = {
[1] = function(buff)
return nil;
end,
[2] = _ReadInt,
[3] = function(buffer)
return -_ReadInt(buffer);
end,
[4] = _ReadLong,
[5] = function(buffer)
return -_ReadLong(buffer);
end,
[6] = function(buffer)
return intBitsToFloat(_ReadInt(buffer));
end,
[7] = _ReadTinyString,
[8] = _ReadShortString,
[9] = function(buffer)
return _HT[_ReadInt(buffer)];
end,
[10] = function(buffer)
return _ReadTable(buffer, _ReadByte(buffer));
end,
[11] = function(buffer)
return _ReadTable(buffer, _ReadShort(buffer));
end,
[12] = _ReadByte,
[13] = function(buffer)
return -_ReadByte(buffer);
end,
[14] = _ReadShort,
[15] = function(buffer)
return -_ReadShort(buffer);
end,
[16] = function(buffer)
return false;
end,
[17] = function(buffer)
return true;
end,
[18] = function(buffer)
return nil;
end,
[19] = function(buffer)
return nil;
end,
[20] = function(buffer)
return _ReadArray(buffer, _ReadByte(buffer));
end,
[21] = function(buffer)
return _ReadArray(buffer, _ReadShort(buffer));
end,
[22] = function(buffer)
return _ReadArray(buffer, _ReadInt(buffer));
end,
--up to 31
};
function _ReadObject(buffer)
local method = _ReadByte(buffer);
-- print('byte', method, buffer.ptr);
if method > 31 then
return method - 32;
else
return _MethodTable[method](buffer);
end
end
function _ReadTable(buffer, num)
local tbl = { };
local key, val;
buffer.tbl = buffer.tbl or tbl;
for index = 1, num do
key = _ReadObject(buffer);
val = _ReadObject(buffer);
tbl[key] = val;
-- print(key, val, buffer.ptr);
end
return tbl;
end
function _ReadArray(buffer, num)
local tbl = { };
local val;
for index = 1, num do
val = _ReadObject(buffer);
tbl[index] = val;
-- print(index, val, buffer.ptr);
end
return tbl;
end
--
--
--C_ChatInfo.SendAddonMessage("questie", '\33\10\3\7\3\118\101\114\7\5\54\46\56\46\49\7\6\109\115\103\86\101\114\37\7\5\109\115\103\73\100\43', 'WHISPER', '兔灰灰');
-- '\33\10\3\7\3\118\101\114\7\5\54\46\56\46\49\7\6\109\115\103\86\101\114\37\7\5\109\115\103\73\100\43'
-- '\33\10\3\7\3 v e r \7\5 6 . 8 . 1 \7\6 m s g V e r \37\7\5 m s g I d \43'
-- '\33\10\3\7\3' .. 'ver' .. '\7\5' .. '6.8.1' .. '\7\6' .. 'msgVer' .. '\37' .. '\7\5' .. 'msgId' .. '\43'
local COLON = strfind(_G.QUEST_MONSTERS_KILLED, ":") and ":" or ": ";
local TypeList = {
m = 'monster',
i = 'item',
o = 'object',
e = 'event',
};
local LocList = {
m = l10n.unit,
i = l10n.item,
o = l10n.object,
e = setmetatable({ }, { __index = function() return "event"; end, }),
};
local _Inited, _META, _OnCommInit, _OnCommQuestAdd, _OnCommQuestDel, _OnCommQuestLine;
local function AddQuest(name, QuestInfo)
local quest = QuestInfo.id;
if quest == nil then
return;
end
-- print("Quest: ", quest);
local objectives = QuestInfo.objectives;
if objectives ~= nil then
local num_lines = #objectives;
local completed = 1;
for line = 1, num_lines do
local obj = objectives[line];
if obj.ful ~= nil and obj.req ~= nil and obj.ful < obj.req then
completed = 0;
end
end
local title = l10n.quest[quest];
if title ~= nil then
title = title[1];
end
title = title or ("quest:" .. quest);
_OnCommQuestAdd(name, quest, completed, num_lines, title);
MT.Debug('|cff00ff7fQ-Q|r|cff00ff00Add|r', name, quest, completed, num_lines, title);
for line = 1, num_lines do
local obj = objectives[line];
local type = obj.typ;
local id = obj.id;
local cur = obj.ful;
local req = obj.req;
-- print(" ", type, id, cur, req);
local text = LocList[type];
if text ~= nil and id ~= nil then
text = text[id] or type;
else
text = type;
end
if req ~= nil and cur ~= nil and req > 0 then
text = text .. COLON .. cur .. "/" .. req;
end
_OnCommQuestLine(name, quest, line, TypeList[type], id, text, cur == nil or req == nil or cur >= req);
-- MT.Debug('|cff00ff7fQ-Q|r|cff00ffffLine|r', name, quest, line, TypeList[type], id, cur == nil or req == nil or cur >= req, text);
end
else
local title = l10n.quest[quest];
if title ~= nil then
title = title[1];
end
title = title or ("quest:" .. quest);
_OnCommQuestAdd(name, quest, 1, 0, title);
MT.Debug('|cff00ff7fQ-Q|r|cff00ff00Add|r', name, quest, 1, 0, title);
end
end
local function DelQuest(name, Info)
-- print("DelQuest", Info.id);
_OnCommQuestDel(name, Info.id);
MT.Debug('|cff00ff7fQ-Q|r|cffff0000Del|r', name, Info.id);
end
local function PullSingle(name, ver, msgVer)
-- local msg = '\33\10\3\7\3\118\101\114\7\5\54\46\56\46\49\7\6\109\115\103\86\101\114\37\7\5\109\115\103\73\100\43';
local msg = '\33\10\3\7\3' .. 'ver' .. '\7' .. strchar(strlen(ver)) .. ver .. '\7\6' .. 'msgVer' .. strchar(32 + msgVer) .. '\7\5' .. 'msgId' .. '\43';
MT.ScheduleMessage("questie", msg, 'WHISPER', name);
end
local function OnComm(msg, name, channel)
if _META[name] ~= nil then
local buffer = { ptr = 1, len = strlen(msg), strbyte(msg, 1, -1) };
-- local len = strlen(msg);
-- print(len);
-- for i = 1, len / 16 do
-- print(strbyte(msg, i * 16 - 15, i * 16));
-- end
-- do return end
if channel ~= "YELL" then
ExternalQuestie.__prev = buffer;
local val = _ReadTable(buffer, 1);
if val ~= nil then
-- MT.Debug('|cff00ff7fOnCommQuestie|r', val, name);
val = val[1];
if val ~= nil then
local msgId = val.msgId;
if msgId == 1 then
local QuestInfo = val.quest;
if QuestInfo ~= nil then
AddQuest(name, QuestInfo);
end
elseif msgId == 2 then
DelQuest(name, val);
elseif msgId == 11 then
ExternalQuestie._PullSingle(name);
elseif msgId == 12 then
_Inited[name] = GetTime();
local data = val[1];
local NumQuest = data[1];
local pos = 2;
-- print("NumQuest", data[1]);
for QuestIndex = 1, NumQuest do
local quest = data[pos];
local num = data[pos + 1] or 0;
pos = pos + 2;
local QuestInfo = {
id = quest,
objectives = num > 0 and { } or nil,
};
local index = 0;
while index < num and data[pos + 1] ~= nil do
index = index + 1;
QuestInfo.objectives[index] = {
typ = strchar(data[pos + 1]),
id = data[pos],
ful = data[pos + 2],
req = data[pos + 3],
};
pos = pos + 4;
end
AddQuest(name, QuestInfo);
end
end
end
end
end
end
end
local CommCache = { };
local function OnCommFirst(msg, name, channel)
CommCache[name] = CommCache[name] or { };
CommCache[name][channel] = msg;
end
local function OnCommNext(msg, name, channel)
local cache = CommCache[name];
if cache[channel] ~= nil then
cache[channel] = cache[channel] .. msg;
end
end
local function OnCommLast(msg, name, channel)
local cache = CommCache[name];
if cache[channel] ~= nil then
OnComm(cache[channel] .. msg, name, channel)
end
end
--
function ExternalQuestie._DelUnit(name)
_Inited[name] = nil;
end
function ExternalQuestie._PullSingle(name)
if _Inited[name] == nil then
PullSingle(name, '6.8.1', 5);
end
end
function ExternalQuestie._OnComm(msg, name, channel)
local cc = strbyte(msg, 1, 1);
if cc >= 1 and cc <= 9 then
if cc == 1 then -- FIRST
OnCommFirst(strsub(msg, 2), name, channel);
elseif cc == 2 then -- NEXT
OnCommNext(strsub(msg, 2), name, channel);
elseif cc == 3 then -- LAST
OnCommLast(strsub(msg, 2), name, channel);
elseif cc == 4 then -- ESCAPE
OnComm(strsub(msg, 2), name, channel);
end
else
OnComm(msg, name, channel);
end
end
function ExternalQuestie._Init(Inited, META, OnCommInit, OnCommQuestAdd, OnCommQuestDel, OnCommQuestLine)
if RegisterAddonMessagePrefix("questie") then
_Inited = Inited;
_META = META;
_OnCommInit = OnCommInit;
_OnCommQuestAdd = OnCommQuestAdd;
_OnCommQuestDel = OnCommQuestDel;
_OnCommQuestLine = OnCommQuestLine;
end
end
VT.ExternalQuestie = ExternalQuestie;
-->