From fd875ef3f1f64924bf0286982ce26f45b9067646 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 18 Feb 2019 11:22:21 -0600 Subject: [PATCH 1/8] attempt to resend packets when a response is not received within 5 seconds --- model/interaction/choice.lua | 3 ++- model/interaction/handshake.lua | 4 +++- unitynpc.lua | 2 ++ util/packets.lua | 40 +++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/model/interaction/choice.lua b/model/interaction/choice.lua index 73d00ea..287687b 100644 --- a/model/interaction/choice.lua +++ b/model/interaction/choice.lua @@ -36,6 +36,7 @@ end function Choice:OnIncomingData(id, pkt) -- TODO failure condition? 0x052 with 5th byte set differently? if id == 0x052 then + packets.clear() self._on_success() return true else @@ -54,7 +55,7 @@ end function Choice:__call(data) local pkts = self:_GeneratePackets(data) for _, pkt in pairs(pkts) do - packets.inject(pkt) + packets.send(pkt) end end diff --git a/model/interaction/handshake.lua b/model/interaction/handshake.lua index aefc8c3..fd0445f 100644 --- a/model/interaction/handshake.lua +++ b/model/interaction/handshake.lua @@ -29,9 +29,11 @@ end -------------------------------------------------------------------------------- function Handshake:OnIncomingData(id, _) if id == 0x052 then + packets.clear() self._on_failure() return true elseif id == 0x034 or id == 0x032 then + packets.clear() self._on_success() return true else @@ -50,7 +52,7 @@ end function Handshake:__call(data) local pkts = self:_GeneratePackets(data) for _, pkt in pairs(pkts) do - packets.inject(pkt) + packets.send(pkt) end end diff --git a/unitynpc.lua b/unitynpc.lua index 244a2fa..73ebae5 100644 --- a/unitynpc.lua +++ b/unitynpc.lua @@ -19,6 +19,7 @@ local command = NilCommand:NilCommand() -------------------------------------------------------------------------------- local function OnCommandFinished() command = NilCommand:NilCommand() + packets.stop() end -------------------------------------------------------------------------------- @@ -33,6 +34,7 @@ local function OnCommand(cmd, p1, p2) command = CommandFactory.CreateCommand(cmd, p1, p2) command:SetSuccessCallback(OnCommandFinished) command:SetFailureCallback(OnCommandFinished) + packets.start() command() else log('Already running a complex command') diff --git a/util/packets.lua b/util/packets.lua index ee56bdc..b998192 100644 --- a/util/packets.lua +++ b/util/packets.lua @@ -1,5 +1,8 @@ local Packets = require('packets') +local running = false +local last = { pkt = nil, time = 0 } + -------------------------------------------------------------------------------- -- Interprets a section of data as a number. -- @@ -29,4 +32,41 @@ function Packets.get_bit_packed(dat_string, start, stop) return newval end +-------------------------------------------------------------------------------- +function Packets.start() + running = true + Packets._update() +end + +-------------------------------------------------------------------------------- +function Packets.stop() + running = false +end + +-------------------------------------------------------------------------------- +function Packets._update() + if not running then + return + end + + local time = os.time() + if last.pkt and ((time - last.pkt.time) >= 5) then + log('Resending packet') + Packets.send(last.pkt) + end + + coroutine.schedule(Packets._update, 1) +end + +-------------------------------------------------------------------------------- +function Packets.send(pkt) + last = { pkt = pkt, time = os.time() } + Packets.inject(pkt) +end + +-------------------------------------------------------------------------------- +function Packets.clear() + last = { pkt = nil, time = 0 } +end + return Packets \ No newline at end of file From 80d01d043908cdcdeb04ee34469a4631c424388e Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 18 Feb 2019 12:02:34 -0600 Subject: [PATCH 2/8] use the correct time --- util/packets.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/packets.lua b/util/packets.lua index b998192..d74b7d2 100644 --- a/util/packets.lua +++ b/util/packets.lua @@ -50,7 +50,7 @@ function Packets._update() end local time = os.time() - if last.pkt and ((time - last.pkt.time) >= 5) then + if last.pkt and ((time - last.time) >= 5) then log('Resending packet') Packets.send(last.pkt) end From a0afd2a5668733d8d4b24bb18ddf65a854233c88 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 18 Feb 2019 12:24:54 -0600 Subject: [PATCH 3/8] condense --- util/packets.lua | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/util/packets.lua b/util/packets.lua index d74b7d2..8474072 100644 --- a/util/packets.lua +++ b/util/packets.lua @@ -1,7 +1,6 @@ local Packets = require('packets') -local running = false -local last = { pkt = nil, time = 0 } +local last = nil -------------------------------------------------------------------------------- -- Interprets a section of data as a number. @@ -34,18 +33,18 @@ end -------------------------------------------------------------------------------- function Packets.start() - running = true + last = { pkt = nil, time = 0 } Packets._update() end -------------------------------------------------------------------------------- function Packets.stop() - running = false + last = nil end -------------------------------------------------------------------------------- function Packets._update() - if not running then + if not last then return end @@ -60,7 +59,9 @@ end -------------------------------------------------------------------------------- function Packets.send(pkt) - last = { pkt = pkt, time = os.time() } + if last then + last = { pkt = pkt, time = os.time() } + end Packets.inject(pkt) end From 2d7e2fe1ae28bc59f93fe1ba8d0941e4e7fec892 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 18 Feb 2019 12:28:42 -0600 Subject: [PATCH 4/8] bump version --- unitynpc.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/unitynpc.lua b/unitynpc.lua index 73ebae5..55ce1a9 100644 --- a/unitynpc.lua +++ b/unitynpc.lua @@ -1,6 +1,6 @@ _addon.name = 'UnityNPC' _addon.author = 'Areint/Alzade' -_addon.version = '1.1.1' +_addon.version = '1.1.2' _addon.commands = {'unpc'} -------------------------------------------------------------------------------- @@ -20,6 +20,7 @@ local command = NilCommand:NilCommand() local function OnCommandFinished() command = NilCommand:NilCommand() packets.stop() + log('Finished') end -------------------------------------------------------------------------------- From 88a9c5754b0edb45231359f94a1dca39d7c6e05f Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 18 Feb 2019 18:08:22 -0600 Subject: [PATCH 5/8] remove resending and add duplicate packet prevention --- model/interaction/choice.lua | 3 +- model/interaction/handshake.lua | 4 +-- unitynpc.lua | 10 ++++--- util/packets.lua | 49 +++++++++------------------------ 4 files changed, 21 insertions(+), 45 deletions(-) diff --git a/model/interaction/choice.lua b/model/interaction/choice.lua index 287687b..73d00ea 100644 --- a/model/interaction/choice.lua +++ b/model/interaction/choice.lua @@ -36,7 +36,6 @@ end function Choice:OnIncomingData(id, pkt) -- TODO failure condition? 0x052 with 5th byte set differently? if id == 0x052 then - packets.clear() self._on_success() return true else @@ -55,7 +54,7 @@ end function Choice:__call(data) local pkts = self:_GeneratePackets(data) for _, pkt in pairs(pkts) do - packets.send(pkt) + packets.inject(pkt) end end diff --git a/model/interaction/handshake.lua b/model/interaction/handshake.lua index fd0445f..aefc8c3 100644 --- a/model/interaction/handshake.lua +++ b/model/interaction/handshake.lua @@ -29,11 +29,9 @@ end -------------------------------------------------------------------------------- function Handshake:OnIncomingData(id, _) if id == 0x052 then - packets.clear() self._on_failure() return true elseif id == 0x034 or id == 0x032 then - packets.clear() self._on_success() return true else @@ -52,7 +50,7 @@ end function Handshake:__call(data) local pkts = self:_GeneratePackets(data) for _, pkt in pairs(pkts) do - packets.send(pkt) + packets.inject(pkt) end end diff --git a/unitynpc.lua b/unitynpc.lua index 55ce1a9..f3205ab 100644 --- a/unitynpc.lua +++ b/unitynpc.lua @@ -1,6 +1,6 @@ _addon.name = 'UnityNPC' _addon.author = 'Areint/Alzade' -_addon.version = '1.1.2' +_addon.version = '1.1.3' _addon.commands = {'unpc'} -------------------------------------------------------------------------------- @@ -19,7 +19,6 @@ local command = NilCommand:NilCommand() -------------------------------------------------------------------------------- local function OnCommandFinished() command = NilCommand:NilCommand() - packets.stop() log('Finished') end @@ -35,7 +34,6 @@ local function OnCommand(cmd, p1, p2) command = CommandFactory.CreateCommand(cmd, p1, p2) command:SetSuccessCallback(OnCommandFinished) command:SetFailureCallback(OnCommandFinished) - packets.start() command() else log('Already running a complex command') @@ -44,7 +42,11 @@ end -------------------------------------------------------------------------------- local function OnIncomingData(id, _, pkt, b, i) - return command:OnIncomingData(id, pkt) + if not packets.is_duplicate(id, pkt) then + return command:OnIncomingData(id, pkt) + else + return false + end end -------------------------------------------------------------------------------- diff --git a/util/packets.lua b/util/packets.lua index 8474072..923f659 100644 --- a/util/packets.lua +++ b/util/packets.lua @@ -1,6 +1,7 @@ local Packets = require('packets') -local last = nil +local duplicates = {} +local tracking = {[0x032] = true, [0x034] = true, [0x052] = true, [0x05C] = true} -------------------------------------------------------------------------------- -- Interprets a section of data as a number. @@ -32,42 +33,18 @@ function Packets.get_bit_packed(dat_string, start, stop) end -------------------------------------------------------------------------------- -function Packets.start() - last = { pkt = nil, time = 0 } - Packets._update() -end - --------------------------------------------------------------------------------- -function Packets.stop() - last = nil -end - --------------------------------------------------------------------------------- -function Packets._update() - if not last then - return - end - - local time = os.time() - if last.pkt and ((time - last.time) >= 5) then - log('Resending packet') - Packets.send(last.pkt) - end - - coroutine.schedule(Packets._update, 1) -end - --------------------------------------------------------------------------------- -function Packets.send(pkt) - if last then - last = { pkt = pkt, time = os.time() } +function Packets.is_duplicate(id, pkt) + if tracking[id] then + local pid = Packets.get_bit_packed(pkt, 0, 32) + if not duplicates[id] or duplicates[id] ~= pid then + duplicates[id] = pid + return false + else + log('Duplicate Packet') + return true + end end - Packets.inject(pkt) -end - --------------------------------------------------------------------------------- -function Packets.clear() - last = { pkt = nil, time = 0 } + return false end return Packets \ No newline at end of file From 206aa6ae2681b8113faaf834b493d93c6c803626 Mon Sep 17 00:00:00 2001 From: Tony Date: Mon, 18 Feb 2019 21:14:34 -0600 Subject: [PATCH 6/8] removed useless logging --- unitynpc.lua | 2 +- util/packets.lua | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/unitynpc.lua b/unitynpc.lua index f3205ab..6cc4f55 100644 --- a/unitynpc.lua +++ b/unitynpc.lua @@ -1,6 +1,6 @@ _addon.name = 'UnityNPC' _addon.author = 'Areint/Alzade' -_addon.version = '1.1.3' +_addon.version = '1.1.4' _addon.commands = {'unpc'} -------------------------------------------------------------------------------- diff --git a/util/packets.lua b/util/packets.lua index 923f659..c0d4cc3 100644 --- a/util/packets.lua +++ b/util/packets.lua @@ -40,7 +40,6 @@ function Packets.is_duplicate(id, pkt) duplicates[id] = pid return false else - log('Duplicate Packet') return true end end From def1bc275b41c4cc7e3da88f4a1c436278d2d007 Mon Sep 17 00:00:00 2001 From: Tony Date: Tue, 19 Feb 2019 11:08:11 -0600 Subject: [PATCH 7/8] cleaned up duplication detection --- unitynpc.lua | 1 - util/packets.lua | 10 ++++------ 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/unitynpc.lua b/unitynpc.lua index 6cc4f55..a06d9ab 100644 --- a/unitynpc.lua +++ b/unitynpc.lua @@ -19,7 +19,6 @@ local command = NilCommand:NilCommand() -------------------------------------------------------------------------------- local function OnCommandFinished() command = NilCommand:NilCommand() - log('Finished') end -------------------------------------------------------------------------------- diff --git a/util/packets.lua b/util/packets.lua index c0d4cc3..cd248d1 100644 --- a/util/packets.lua +++ b/util/packets.lua @@ -1,7 +1,7 @@ local Packets = require('packets') -local duplicates = {} -local tracking = {[0x032] = true, [0x034] = true, [0x052] = true, [0x05C] = true} +local last_packets = {} +local tracking = {[0x032] = true, [0x034] = true, [0x052] = true, [0x05C] = true, [0x00B] = true} -------------------------------------------------------------------------------- -- Interprets a section of data as a number. @@ -36,12 +36,10 @@ end function Packets.is_duplicate(id, pkt) if tracking[id] then local pid = Packets.get_bit_packed(pkt, 0, 32) - if not duplicates[id] or duplicates[id] ~= pid then - duplicates[id] = pid - return false - else + if last_packets[id] and last_packets[id] == pid then return true end + last_packets[id] = pid end return false end From 8c5be3fa117ed3b6df29c75bf5222cbbefe20d18 Mon Sep 17 00:00:00 2001 From: Tony Date: Tue, 19 Feb 2019 11:09:29 -0600 Subject: [PATCH 8/8] bump version and update readme --- README.md | 4 +++- unitynpc.lua | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3e86324..2baa980 100644 --- a/README.md +++ b/README.md @@ -20,4 +20,6 @@ Buy Warp Scroll | //buywarp \ should be replaced with zone names (you can use the in-game auto-translate feature). \ should be replaced with item names from [data/items.lua](https://github.com/Tny5989/UnityNPC/blob/master/data/items.lua) -This addon does not check your accolades. It is up to you to verify your accolades amount before buying keys. +This addon does not check your accolades. It is up to you to verify your accolades amount before buying keys. + +As with all my addons, this is still a work in progress and should be used at your own risk. diff --git a/unitynpc.lua b/unitynpc.lua index a06d9ab..54f56f0 100644 --- a/unitynpc.lua +++ b/unitynpc.lua @@ -1,6 +1,6 @@ _addon.name = 'UnityNPC' _addon.author = 'Areint/Alzade' -_addon.version = '1.1.4' +_addon.version = '1.1.5' _addon.commands = {'unpc'} --------------------------------------------------------------------------------