From 53f319164b709028f10c291ad7fb6daed9b82a4d Mon Sep 17 00:00:00 2001 From: Imple Lee <80144331+ImpleLee@users.noreply.github.com> Date: Mon, 9 Oct 2023 13:45:28 +0800 Subject: [PATCH 1/2] simplify two code pieces in record manipulation --- parts/data.lua | 69 ++++++++++++++++++-------------------------------- 1 file changed, 25 insertions(+), 44 deletions(-) diff --git a/parts/data.lua b/parts/data.lua index 530059a43..b19dc361c 100644 --- a/parts/data.lua +++ b/parts/data.lua @@ -250,9 +250,19 @@ end ]] function DATA.dumpRecording(list,ptr) local out="" - local buffer,buffer2="" + local buffer="" if not ptr then ptr=1 end local prevFrm=list[ptr-2] or 0 + local function encode(t) + if t<128 then return char(t) end + local buffer2=char(t%128) + t=floor(t/128) + while t>=128 do + buffer2=char(128+t%128)..buffer2 + t=floor(t/128) + end + return char(128+t)..buffer2 + end while list[ptr] do -- Flush buffer if #buffer>10 then @@ -263,31 +273,10 @@ function DATA.dumpRecording(list,ptr) -- Encode time local t=list[ptr]-prevFrm prevFrm=list[ptr] - if t>=128 then - buffer2=char(t%128) - t=floor(t/128) - while t>=128 do - buffer2=char(128+t%128)..buffer2 - t=floor(t/128) - end - buffer=buffer..char(128+t)..buffer2 - else - buffer=buffer..char(t) - end + buffer=buffer..encode(t) -- Encode event - t=list[ptr+1] - if t>=128 then - buffer2=char(t%128) - t=floor(t/128) - while t>=128 do - buffer2=char(128+t%128)..buffer2 - t=floor(t/128) - end - buffer=buffer..char(128+t)..buffer2 - else - buffer=buffer..char(t) - end + buffer=buffer..encode(list[ptr+1]) -- Step ptr=ptr+2 @@ -299,29 +288,21 @@ function DATA.pumpRecording(str,L) local p=1 local curFrm=L[#L-1] or 0 - local code + local function decode() + local ret=0 + repeat + local b=byte(str,p) + p=p+1 + ret=ret*128+(b<128 and b or b-128) + until b<128 + return ret + end while p<=len do -- Read delta time - code=0 - local b=byte(str,p) - while b>=128 do - code=code*128+b-128 - p=p+1 - b=byte(str,p) - end - curFrm=curFrm+code*128+b - L[#L+1]=curFrm - p=p+1 + curFrm=curFrm+decode() + ins(L,curFrm) - local event=0 - b=byte(str,p) - while b>=128 do - event=event*128+b-128 - p=p+1 - b=byte(str,p) - end - L[#L+1]=event*128+b - p=p+1 + ins(L,decode()) end end do-- function DATA.saveReplay() From 9d8472d384b449f89e2d8aafc987c95959f971b4 Mon Sep 17 00:00:00 2001 From: Imple Lee <80144331+ImpleLee@users.noreply.github.com> Date: Mon, 9 Oct 2023 23:14:40 +0800 Subject: [PATCH 2/2] move 2 functions outside --- parts/data.lua | 49 ++++++++++++++++++++++++++----------------------- 1 file changed, 26 insertions(+), 23 deletions(-) diff --git a/parts/data.lua b/parts/data.lua index b19dc361c..d3163b973 100644 --- a/parts/data.lua +++ b/parts/data.lua @@ -226,6 +226,25 @@ function DATA.pasteQuestArgs(str) return true end +local function _encode(t) + if t<128 then return char(t) end + local buffer2=char(t%128) + t=floor(t/128) + while t>=128 do + buffer2=char(128+t%128)..buffer2 + t=floor(t/128) + end + return char(128+t)..buffer2 +end +local function _decode(str,p) + local ret=0 + repeat + local b=byte(str,p) + p=p+1 + ret=ret*128+(b<128 and b or b-128) + until b<128 + return ret,p +end --[[ Replay file: a zlib-compressed json table @@ -253,16 +272,6 @@ function DATA.dumpRecording(list,ptr) local buffer="" if not ptr then ptr=1 end local prevFrm=list[ptr-2] or 0 - local function encode(t) - if t<128 then return char(t) end - local buffer2=char(t%128) - t=floor(t/128) - while t>=128 do - buffer2=char(128+t%128)..buffer2 - t=floor(t/128) - end - return char(128+t)..buffer2 - end while list[ptr] do -- Flush buffer if #buffer>10 then @@ -273,10 +282,10 @@ function DATA.dumpRecording(list,ptr) -- Encode time local t=list[ptr]-prevFrm prevFrm=list[ptr] - buffer=buffer..encode(t) + buffer=buffer.._encode(t) -- Encode event - buffer=buffer..encode(list[ptr+1]) + buffer=buffer.._encode(list[ptr+1]) -- Step ptr=ptr+2 @@ -288,21 +297,15 @@ function DATA.pumpRecording(str,L) local p=1 local curFrm=L[#L-1] or 0 - local function decode() - local ret=0 - repeat - local b=byte(str,p) - p=p+1 - ret=ret*128+(b<128 and b or b-128) - until b<128 - return ret - end while p<=len do + local code,event -- Read delta time - curFrm=curFrm+decode() + code,p=_decode(str,p) + curFrm=curFrm+code ins(L,curFrm) - ins(L,decode()) + event,p=_decode(str,p) + ins(L,event) end end do-- function DATA.saveReplay()