Merge pull request #168 from thegrb93/refactor-decode-error

Refactor decode error handling
This commit is contained in:
thegrb93 2018-12-02 22:39:01 -05:00 committed by GitHub
commit 87be23879c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 67 additions and 70 deletions

View File

@ -62,14 +62,13 @@ local function AdvDupe2_ReceiveFile(len, ply, len2)
local read = file.Read(path..".txt")
if not read then AdvDupe2.Notify("File could not be read", NOTIFY_ERROR) return end
AdvDupe2.Decode(read, function(success,dupe,info,moreinfo)
if(success)then
AdvDupe2.Notify("DEBUG CHECK: File successfully opens. No EOF errors.")
else
AdvDupe2.Notify("DEBUG CHECK: File contains EOF errors.", NOTIFY_ERROR)
errored = true
end
end)
local success,dupe,info,moreinfo = AdvDupe2.Decode(read)
if(success)then
AdvDupe2.Notify("DEBUG CHECK: File successfully opens. No EOF errors.")
else
AdvDupe2.Notify("DEBUG CHECK: " .. dupe, NOTIFY_ERROR)
errored = true
end
end
local filename = string.Explode("/", path)
@ -243,15 +242,14 @@ function AdvDupe2.InitializeUpload(ReadPath, ReadArea)
uploading = true
AdvDupe2.Decode(read, function(success, dupe, info, moreinfo)
if(success)then
AdvDupe2.PendingDupe = { read, dupe, info, moreinfo, name }
RunConsoleCommand("AdvDupe2_InitReceiveFile")
else
uploading = false
AdvDupe2.Notify("File could not be decoded. Upload Canceled.", NOTIFY_ERROR)
end
end)
local success, dupe, info, moreinfo = AdvDupe2.Decode(read)
if(success)then
AdvDupe2.PendingDupe = { read, dupe, info, moreinfo, name }
RunConsoleCommand("AdvDupe2_InitReceiveFile")
else
uploading = false
AdvDupe2.Notify("File could not be decoded. ("..dupe..") Upload Canceled.", NOTIFY_ERROR)
end
end
--[[

View File

@ -409,7 +409,8 @@ function BROWSER:DoNodeRightClick(node)
local name = string.Explode("/", ReadPath)
name = name[#name]
name = string.sub(name, 1, #name-4)
AdvDupe2.Decode(read, function(success,dupe,info,moreinfo) if(success)then AdvDupe2.LoadGhosts(dupe, info, moreinfo, name, true) end end)
local success,dupe,info,moreinfo = AdvDupe2.Decode(read)
if(success)then AdvDupe2.LoadGhosts(dupe, info, moreinfo, name, true) end
end)
else
Menu:AddOption("Open", function()
@ -434,7 +435,8 @@ function BROWSER:DoNodeRightClick(node)
local name = string.Explode("/", ReadPath)
name = name[#name]
name = string.sub(name, 1, #name-4)
AdvDupe2.Decode(read, function(success,dupe,info,moreinfo) if(success)then AdvDupe2.LoadGhosts(dupe, info, moreinfo, name, true) end end)
local success,dupe,info,moreinfo = AdvDupe2.Decode(read)
if(success)then AdvDupe2.LoadGhosts(dupe, info, moreinfo, name, true) end
end)
Menu:AddSpacer()
Menu:AddOption("Rename", function()

View File

@ -423,12 +423,12 @@ end
Params: <string> encodedDupe, <function> callback, <...> args
Return: runs callback(<boolean> success, <table/string> tbl, <table> info)
]]
function AdvDupe2.Decode(encodedDupe, callback, ...)
function AdvDupe2.Decode(encodedDupe)
local sig, rev = encodedDupe:match("^(....)(.)")
if not rev then
error("malformed dupe (wtf <5 chars long?!)")
return false, "malformed dupe (wtf <5 chars long?!)"
end
rev = rev:byte()
@ -445,14 +445,14 @@ function AdvDupe2.Decode(encodedDupe, callback, ...)
ErrorNoHalt(tbl)
end
callback(success, tbl, info, moreinfo, ...)
return success, tbl, info, moreinfo
else
error("unknown duplication format")
return false, "unknown duplication format"
end
elseif rev > REVISION then
error(format("Newer codec needed. (have rev %u, need rev %u) Update Advdupe2.",REVISION,rev))
return false, format("Newer codec needed. (have rev %u, need rev %u) Update Advdupe2.",REVISION,rev)
elseif rev < 1 then
error(format("attempt to use an invalid format revision (rev %d)", rev))
return false, format("attempt to use an invalid format revision (rev %d)", rev)
else
local success, tbl, info = pcall(versions[rev], encodedDupe)
@ -462,7 +462,6 @@ function AdvDupe2.Decode(encodedDupe, callback, ...)
ErrorNoHalt(tbl)
end
callback(success, tbl, info, ...)
return success, tbl, info
end
end

View File

@ -290,7 +290,7 @@ local function AdvDupe2_ReceiveFile(len, ply, len2)
Net.Data = Net.Data..net.ReadString()
if(status==1)then
AdvDupe2.Decode(AdvDupe2.Null.invesc(Net.Data), function(success,dupe,info,moreinfo) AdvDupe2.LoadDupe(ply, success, dupe, info, moreinfo) end)
AdvDupe2.LoadDupe(ply, AdvDupe2.Decode(AdvDupe2.Null.invesc(Net.Data)))
AdvDupe2.Network.ClientNetworks[id]=nil
ply.AdvDupe2.Downloading = false
ply.AdvDupe2.Uploading = false

View File

@ -85,48 +85,47 @@ local function PasteMap()
end
local map = file.Read("advdupe2_maps/"..filename..".txt")
AdvDupe2.Decode(map, function(success,dupe,info,moreinfo)
if not success then
print("[AdvDupe2Notify]\tCould not open map save "..dupe)
return
end
local Tab = {Entities=dupe["Entities"], Constraints=dupe["Constraints"], HeadEnt=dupe["HeadEnt"]}
local Entities = AdvDupe2.duplicator.Paste(nil, table.Copy(Tab.Entities), Tab.Constraints, nil, nil, Tab.HeadEnt.Pos, true)
local maptype = GetConVarString("AdvDupe2_LoadMap")
if(maptype=="1")then
local PhysObj
for k,v in pairs(Entities) do
if(IsValid(v))then
for i=0, #Tab.Entities[k].PhysicsObjects do
if(Tab.Entities[k].PhysicsObjects[i].Frozen)then
PhysObj = v:GetPhysicsObjectNum( i )
if IsValid(PhysObj) then
PhysObj:EnableMotion(true)
end
end
end
if v.CPPISetOwner then v:CPPISetOwner(game.GetWorld()) end
end
end
elseif(maptype=="2")then
local PhysObj
for k,v in pairs(Entities) do
if(IsValid(v))then
for i=0, #Tab.Entities[k].PhysicsObjects do
local success,dupe,info,moreinfo = AdvDupe2.Decode(map)
if not success then
print("[AdvDupe2Notify]\tCould not open map save "..dupe)
return
end
local Tab = {Entities=dupe["Entities"], Constraints=dupe["Constraints"], HeadEnt=dupe["HeadEnt"]}
local Entities = AdvDupe2.duplicator.Paste(nil, table.Copy(Tab.Entities), Tab.Constraints, nil, nil, Tab.HeadEnt.Pos, true)
local maptype = GetConVarString("AdvDupe2_LoadMap")
if(maptype=="1")then
local PhysObj
for k,v in pairs(Entities) do
if(IsValid(v))then
for i=0, #Tab.Entities[k].PhysicsObjects do
if(Tab.Entities[k].PhysicsObjects[i].Frozen)then
PhysObj = v:GetPhysicsObjectNum( i )
if IsValid(PhysObj) then
PhysObj:EnableMotion(true)
end
end
if v.CPPISetOwner then v:CPPISetOwner(game.GetWorld()) end
end
if v.CPPISetOwner then v:CPPISetOwner(game.GetWorld()) end
end
end
print("[AdvDupe2Notify]\tMap save pasted.")
end)
elseif(maptype=="2")then
local PhysObj
for k,v in pairs(Entities) do
if(IsValid(v))then
for i=0, #Tab.Entities[k].PhysicsObjects do
PhysObj = v:GetPhysicsObjectNum( i )
if IsValid(PhysObj) then
PhysObj:EnableMotion(true)
end
end
if v.CPPISetOwner then v:CPPISetOwner(game.GetWorld()) end
end
end
end
print("[AdvDupe2Notify]\tMap save pasted.")
end
util.AddNetworkString("AdvDupe2_AddFile")

View File

@ -568,7 +568,7 @@ if(SERVER)then
local name = string.Explode("/", path)
ply.AdvDupe2.Name = name[#name]
AdvDupe2.Decode(data, function(success,dupe,info,moreinfo) AdvDupe2.LoadDupe(ply, success, dupe, info, moreinfo) end)
AdvDupe2.LoadDupe(ply, AdvDupe2.Decode(data))
end
concommand.Add("AdvDupe2_OpenFile", OpenFile)
end
@ -617,13 +617,12 @@ if(SERVER)then
if(not file.Exists(dir, "DATA"))then AdvDupe2.Notify(ply, "File does not exist", NOTIFY_ERROR) return end
local read = file.Read(dir)
AdvDupe2.Decode(read, function(success,dupe,info,moreinfo)
if(success)then
AdvDupe2.Notify(ply, "DEBUG CHECK: File successfully opens. No EOF errors.")
else
AdvDupe2.Notify(ply, "DEBUG CHECK: File contains EOF errors.", NOTIFY_ERROR)
end
end)
local success,dupe,info,moreinfo = AdvDupe2.Decode(read)
if(success)then
AdvDupe2.Notify(ply, "DEBUG CHECK: File successfully opens. No EOF errors.")
else
AdvDupe2.Notify(ply, "DEBUG CHECK: ".. dupe, NOTIFY_ERROR)
end
end
else
if(not IsValid(ply))then return end