Move file related stuff to their own file

This commit is contained in:
Garrett Brown 2019-05-04 03:09:37 -04:00
parent 7b67d4f329
commit 454ca60078
5 changed files with 250 additions and 248 deletions

103
lua/advdupe2/cl_file.lua Normal file
View File

@ -0,0 +1,103 @@
local function AdvDupe2_ReceiveFile(len, ply)
local Autosave = net.ReadUInt(8) == 1
net.ReadStream(nil, function(data)
AdvDupe2.RemoveProgressBar()
if(!data)then
AdvDupe2.Notify("File was not saved!",NOTIFY_ERROR,5)
return
end
local path = ""
if(AutoSave)then
if(LocalPlayer():GetInfo("advdupe2_auto_save_overwrite")~="1")then
path = AdvDupe2.GetFilename(AdvDupe2.AutoSavePath)
end
else
path = AdvDupe2.GetFilename(AdvDupe2.SavePath)
end
local dupefile = file.Open(path..".txt", "wb", "DATA")
if(!dupefile)then
AdvDupe2.Notify("File was not saved!",NOTIFY_ERROR,5)
return
end
dupefile:Write(data)
dupefile:Close()
local errored = false
if(LocalPlayer():GetInfo("advdupe2_debug_openfile")=="1")then
if(not file.Exists(path..".txt", "DATA"))then AdvDupe2.Notify("File does not exist", NOTIFY_ERROR) return end
local readFile = file.Open(path..".txt", "rb", "DATA")
if not readFile then AdvDupe2.Notify("File could not be read", NOTIFY_ERROR) return end
local readData = readFile:Read(readFile:Size())
readFile:Close()
local success,dupe,info,moreinfo = AdvDupe2.Decode(readData)
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)
filename = filename[#filename]
if(AutoSave)then
if(IsValid(AdvDupe2.FileBrowser.AutoSaveNode))then
local add = true
for i=1, #AdvDupe2.FileBrowser.AutoSaveNode.Files do
if(filename==AdvDupe2.FileBrowser.AutoSaveNode.Files[i].Label:GetText())then
add=false
break
end
end
if(add)then
AdvDupe2.FileBrowser.AutoSaveNode:AddFile(filename)
AdvDupe2.FileBrowser.Browser.pnlCanvas:Sort(AdvDupe2.FileBrowser.AutoSaveNode)
end
end
else
AdvDupe2.FileBrowser.Browser.pnlCanvas.ActionNode:AddFile(filename)
AdvDupe2.FileBrowser.Browser.pnlCanvas:Sort(AdvDupe2.FileBrowser.Browser.pnlCanvas.ActionNode)
end
if(!errored)then
AdvDupe2.Notify("File successfully saved!",NOTIFY_GENERIC, 5)
end
end)
end
net.Receive("AdvDupe2_ReceiveFile", AdvDupe2_ReceiveFile)
local uploading = nil
function AdvDupe2.UploadFile(ReadPath, ReadArea)
if uploading then AdvDupe2.Notify("Already opening file, please wait.", NOTIFY_ERROR) return end
if(ReadArea==0)then
ReadPath = AdvDupe2.DataFolder.."/"..ReadPath..".txt"
elseif(ReadArea==1)then
ReadPath = AdvDupe2.DataFolder.."/-Public-/"..ReadPath..".txt"
else
ReadPath = "adv_duplicator/"..ReadPath..".txt"
end
if(not file.Exists(ReadPath, "DATA"))then AdvDupe2.Notify("File does not exist", NOTIFY_ERROR) return end
local read = file.Read(ReadPath)
if not read then AdvDupe2.Notify("File could not be read", NOTIFY_ERROR) return end
local name = string.Explode("/", ReadPath)
name = name[#name]
name = string.sub(name, 1, #name-4)
local success, dupe, info, moreinfo = AdvDupe2.Decode(read)
if(success)then
net.Start("AdvDupe2_ReceiveFile")
net.WriteString(name)
uploading = net.WriteStream(read, function()
uploading = nil
AdvDupe2.File = nil
AdvDupe2.RemoveProgressBar()
end)
net.SendToServer()
else
AdvDupe2.Notify("File could not be decoded. ("..dupe..") Upload Canceled.", NOTIFY_ERROR)
end
end

144
lua/advdupe2/sv_file.lua Normal file
View File

@ -0,0 +1,144 @@
--Save a file to the client
local function SaveFile(ply, cmd, args)
if(not ply.AdvDupe2 or not ply.AdvDupe2.Entities or table.Count(ply.AdvDupe2.Entities)==0)then AdvDupe2.Notify(ply,"Duplicator is empty, nothing to save.", NOTIFY_ERROR) return end
if(not game.SinglePlayer() and CurTime()-(ply.AdvDupe2.FileMod or 0) < 0)then
AdvDupe2.Notify(ply,"Cannot save at the moment. Please Wait...", NOTIFY_ERROR)
return
end
if(ply.AdvDupe2.Pasting || ply.AdvDupe2.Downloading)then
AdvDupe2.Notify(ply,"Advanced Duplicator 2 is busy.",NOTIFY_ERROR)
return false
end
ply.AdvDupe2.FileMod = CurTime()+tonumber(GetConVarString("AdvDupe2_FileModificationDelay")+2)
local name = string.Explode("/", args[1])
ply.AdvDupe2.Name = name[#name]
net.Start("AdvDupe2_SetDupeInfo")
net.WriteString(ply.AdvDupe2.Name)
net.WriteString(ply:Nick())
net.WriteString(os.date("%d %B %Y"))
net.WriteString(os.date("%I:%M %p"))
net.WriteString("")
net.WriteString(args[2] or "")
net.WriteString(table.Count(ply.AdvDupe2.Entities))
net.WriteString(#ply.AdvDupe2.Constraints)
net.Send(ply)
local Tab = {Entities = ply.AdvDupe2.Entities, Constraints = ply.AdvDupe2.Constraints, HeadEnt = ply.AdvDupe2.HeadEnt, Description=args[2]}
AdvDupe2.Encode( Tab, AdvDupe2.GenerateDupeStamp(ply), function(data)
AdvDupe2.SendToClient(ply, data, 0)
end)
end
concommand.Add("AdvDupe2_SaveFile", SaveFile)
function AdvDupe2.SendToClient(ply, data, autosave)
if(not IsValid(ply))then return end
ply.AdvDupe2.Downloading = true
AdvDupe2.InitProgressBar(ply,"Saving:")
net.Start("AdvDupe2_ReceiveFile")
net.WriteUInt(autosave, 8)
net.WriteStream(data, function()
ply.AdvDupe2.Downloading = false
end)
net.Send(ply)
end
function AdvDupe2.LoadDupe(ply,success,dupe,info,moreinfo)
if(not IsValid(ply))then return end
if not success then
AdvDupe2.Notify(ply,"Could not open "..dupe,NOTIFY_ERROR)
return
end
if(not game.SinglePlayer())then
if(tonumber(GetConVarString("AdvDupe2_MaxConstraints"))~=0 and #dupe["Constraints"]>tonumber(GetConVarString("AdvDupe2_MaxConstraints")))then
AdvDupe2.Notify(ply,"Amount of constraints is greater than "..GetConVarString("AdvDupe2_MaxConstraints"),NOTIFY_ERROR)
return false
end
end
ply.AdvDupe2.Entities = {}
ply.AdvDupe2.Constraints = {}
ply.AdvDupe2.HeadEnt={}
if(info.ad1)then
ply.AdvDupe2.HeadEnt.Index = tonumber(moreinfo.Head)
local spx,spy,spz = moreinfo.StartPos:match("^(.-),(.-),(.+)$")
ply.AdvDupe2.HeadEnt.Pos = Vector(tonumber(spx) or 0, tonumber(spy) or 0, tonumber(spz) or 0)
local z = (tonumber(moreinfo.HoldPos:match("^.-,.-,(.+)$")) or 0)*-1
ply.AdvDupe2.HeadEnt.Z = z
ply.AdvDupe2.HeadEnt.Pos.Z = ply.AdvDupe2.HeadEnt.Pos.Z + z
local Pos
local Ang
for k,v in pairs(dupe["Entities"])do
Pos = nil
Ang = nil
if(v.SavedParentIdx)then
if(not v.BuildDupeInfo)then v.BuildDupeInfo = {} end
v.BuildDupeInfo.DupeParentID = v.SavedParentIdx
Pos = v.LocalPos*1
Ang = v.LocalAngle*1
end
for i,p in pairs(v.PhysicsObjects)do
p.Pos = Pos or (p.LocalPos*1)
p.Pos.Z = p.Pos.Z - z
p.Angle = Ang or (p.LocalAngle*1)
p.LocalPos = nil
p.LocalAngle = nil
p.Frozen = not p.Frozen -- adv dupe 2 does this wrong way
end
v.LocalPos = nil
v.LocalAngle = nil
end
ply.AdvDupe2.Entities = dupe["Entities"]
ply.AdvDupe2.Constraints = dupe["Constraints"]
else
ply.AdvDupe2.Entities = dupe["Entities"]
ply.AdvDupe2.Constraints = dupe["Constraints"]
ply.AdvDupe2.HeadEnt = dupe["HeadEnt"]
end
if(game.SinglePlayer())then AdvDupe2.SendGhosts(ply) end
AdvDupe2.ResetOffsets(ply, true)
end
local function AdvDupe2_ReceiveFile(len, ply)
if(not IsValid(ply))then return end
if ply.AdvDupe2.Uploading then
umsg.Start("AdvDupe2_UploadRejected", ply)
umsg.Bool(false)
umsg.End()
AdvDupe2.Notify(ply, "Duplicator is Busy!",NOTIFY_ERROR,5)
return
end
ply.AdvDupe2.Uploading = true
AdvDupe2.InitProgressBar(ply, "Opening: ")
local name = net.ReadString()
local _1, _2, _3 = string.find(name, "([%w_]+)")
if _3 then
ply.AdvDupe2.Name = string.sub(_3, 1, 32)
else
ply.AdvDupe2.Name = "Advanced Duplication"
end
net.ReadStream(ply, function(data)
AdvDupe2.LoadDupe(ply, AdvDupe2.Decode(data))
ply.AdvDupe2.Uploading = false
umsg.Start("AdvDupe2_UploadRejected", ply)
umsg.Bool(true)
umsg.End()
end)
end
net.Receive("AdvDupe2_ReceiveFile", AdvDupe2_ReceiveFile)

View File

@ -14,6 +14,7 @@ end
include "advdupe2/file_browser.lua"
include "advdupe2/sh_codec.lua"
include "advdupe2/sh_netstream.lua"
include "advdupe2/cl_file.lua"
function AdvDupe2.Notify(msg,typ,dur)
surface.PlaySound(typ == 1 and "buttons/button10.wav" or "ambient/water/drip1.wav")

View File

@ -11,11 +11,13 @@ include "advdupe2/sv_clipboard.lua"
include "advdupe2/sh_codec.lua"
include "advdupe2/sh_netstream.lua"
include "advdupe2/sv_misc.lua"
include "advdupe2/sv_file.lua"
AddCSLuaFile "autorun/client/advdupe2_cl_init.lua"
AddCSLuaFile "advdupe2/file_browser.lua"
AddCSLuaFile "advdupe2/sh_codec.lua"
AddCSLuaFile "advdupe2/sh_netstream.lua"
AddCSLuaFile "advdupe2/cl_file.lua"
function AdvDupe2.Notify(ply,msg,typ, showsvr, dur)
umsg.Start("AdvDupe2Notify",ply)

View File

@ -632,151 +632,7 @@ if(SERVER)then
return spawner
end
duplicator.RegisterEntityClass("gmod_contr_spawner", MakeContraptionSpawner, "Pos", "Ang", "HeadEnt", "EntityTable", "ConstraintTable", "delay", "undo_delay", "model", "key", "undo_key", "disgrav", "disdrag", "addvel", "hideprops")
--Save a file to the client
local function SaveFile(ply, cmd, args)
if(not ply.AdvDupe2 or not ply.AdvDupe2.Entities or table.Count(ply.AdvDupe2.Entities)==0)then AdvDupe2.Notify(ply,"Duplicator is empty, nothing to save.", NOTIFY_ERROR) return end
if(not game.SinglePlayer() and CurTime()-(ply.AdvDupe2.FileMod or 0) < 0)then
AdvDupe2.Notify(ply,"Cannot save at the moment. Please Wait...", NOTIFY_ERROR)
return
end
if(ply.AdvDupe2.Pasting || ply.AdvDupe2.Downloading)then
AdvDupe2.Notify(ply,"Advanced Duplicator 2 is busy.",NOTIFY_ERROR)
return false
end
ply.AdvDupe2.FileMod = CurTime()+tonumber(GetConVarString("AdvDupe2_FileModificationDelay")+2)
local name = string.Explode("/", args[1])
ply.AdvDupe2.Name = name[#name]
net.Start("AdvDupe2_SetDupeInfo")
net.WriteString(ply.AdvDupe2.Name)
net.WriteString(ply:Nick())
net.WriteString(os.date("%d %B %Y"))
net.WriteString(os.date("%I:%M %p"))
net.WriteString("")
net.WriteString(args[2] or "")
net.WriteString(table.Count(ply.AdvDupe2.Entities))
net.WriteString(#ply.AdvDupe2.Constraints)
net.Send(ply)
local Tab = {Entities = ply.AdvDupe2.Entities, Constraints = ply.AdvDupe2.Constraints, HeadEnt = ply.AdvDupe2.HeadEnt, Description=args[2]}
AdvDupe2.Encode( Tab, AdvDupe2.GenerateDupeStamp(ply), function(data)
AdvDupe2.SendToClient(ply, data, 0)
end)
end
concommand.Add("AdvDupe2_SaveFile", SaveFile)
function AdvDupe2.SendToClient(ply, data, autosave)
if(not IsValid(ply))then return end
ply.AdvDupe2.Downloading = true
AdvDupe2.InitProgressBar(ply,"Saving:")
net.Start("AdvDupe2_ReceiveFile")
net.WriteUInt(autosave, 8)
net.WriteStream(data, function()
ply.AdvDupe2.Downloading = false
end)
net.Send(ply)
end
function AdvDupe2.LoadDupe(ply,success,dupe,info,moreinfo)
if(not IsValid(ply))then return end
if not success then
AdvDupe2.Notify(ply,"Could not open "..dupe,NOTIFY_ERROR)
return
end
if(not game.SinglePlayer())then
if(tonumber(GetConVarString("AdvDupe2_MaxConstraints"))~=0 and #dupe["Constraints"]>tonumber(GetConVarString("AdvDupe2_MaxConstraints")))then
AdvDupe2.Notify(ply,"Amount of constraints is greater than "..GetConVarString("AdvDupe2_MaxConstraints"),NOTIFY_ERROR)
return false
end
end
ply.AdvDupe2.Entities = {}
ply.AdvDupe2.Constraints = {}
ply.AdvDupe2.HeadEnt={}
if(info.ad1)then
ply.AdvDupe2.HeadEnt.Index = tonumber(moreinfo.Head)
local spx,spy,spz = moreinfo.StartPos:match("^(.-),(.-),(.+)$")
ply.AdvDupe2.HeadEnt.Pos = Vector(tonumber(spx) or 0, tonumber(spy) or 0, tonumber(spz) or 0)
local z = (tonumber(moreinfo.HoldPos:match("^.-,.-,(.+)$")) or 0)*-1
ply.AdvDupe2.HeadEnt.Z = z
ply.AdvDupe2.HeadEnt.Pos.Z = ply.AdvDupe2.HeadEnt.Pos.Z + z
local Pos
local Ang
for k,v in pairs(dupe["Entities"])do
Pos = nil
Ang = nil
if(v.SavedParentIdx)then
if(not v.BuildDupeInfo)then v.BuildDupeInfo = {} end
v.BuildDupeInfo.DupeParentID = v.SavedParentIdx
Pos = v.LocalPos*1
Ang = v.LocalAngle*1
end
for i,p in pairs(v.PhysicsObjects)do
p.Pos = Pos or (p.LocalPos*1)
p.Pos.Z = p.Pos.Z - z
p.Angle = Ang or (p.LocalAngle*1)
p.LocalPos = nil
p.LocalAngle = nil
p.Frozen = not p.Frozen -- adv dupe 2 does this wrong way
end
v.LocalPos = nil
v.LocalAngle = nil
end
ply.AdvDupe2.Entities = dupe["Entities"]
ply.AdvDupe2.Constraints = dupe["Constraints"]
else
ply.AdvDupe2.Entities = dupe["Entities"]
ply.AdvDupe2.Constraints = dupe["Constraints"]
ply.AdvDupe2.HeadEnt = dupe["HeadEnt"]
end
if(game.SinglePlayer())then AdvDupe2.SendGhosts(ply) end
AdvDupe2.ResetOffsets(ply, true)
end
local function AdvDupe2_ReceiveFile(len, ply)
if(not IsValid(ply))then return end
if ply.AdvDupe2.Uploading then
umsg.Start("AdvDupe2_UploadRejected", ply)
umsg.Bool(false)
umsg.End()
AdvDupe2.Notify(ply, "Duplicator is Busy!",NOTIFY_ERROR,5)
return
end
ply.AdvDupe2.Uploading = true
AdvDupe2.InitProgressBar(ply, "Opening: ")
local name = net.ReadString()
local _1, _2, _3 = string.find(name, "([%w_]+)")
if _3 then
ply.AdvDupe2.Name = string.sub(_3, 1, 32)
else
ply.AdvDupe2.Name = "Advanced Duplication"
end
net.ReadStream(ply, function(data)
AdvDupe2.LoadDupe(ply, AdvDupe2.Decode(data))
ply.AdvDupe2.Uploading = false
umsg.Start("AdvDupe2_UploadRejected", ply)
umsg.Bool(true)
umsg.End()
end)
end
net.Receive("AdvDupe2_ReceiveFile", AdvDupe2_ReceiveFile)
function AdvDupe2.InitProgressBar(ply,label)
umsg.Start("AdvDupe2_InitProgressBar",ply)
@ -1785,110 +1641,6 @@ if(CLIENT)then
tryToBuild()
end
local function AdvDupe2_ReceiveFile(len, ply)
local Autosave = net.ReadUInt(8) == 1
net.ReadStream(nil, function(data)
AdvDupe2.RemoveProgressBar()
if(!data)then
AdvDupe2.Notify("File was not saved!",NOTIFY_ERROR,5)
return
end
local path = ""
if(AutoSave)then
if(LocalPlayer():GetInfo("advdupe2_auto_save_overwrite")~="1")then
path = AdvDupe2.GetFilename(AdvDupe2.AutoSavePath)
end
else
path = AdvDupe2.GetFilename(AdvDupe2.SavePath)
end
local dupefile = file.Open(path..".txt", "wb", "DATA")
if(!dupefile)then
AdvDupe2.Notify("File was not saved!",NOTIFY_ERROR,5)
return
end
dupefile:Write(data)
dupefile:Close()
local errored = false
if(LocalPlayer():GetInfo("advdupe2_debug_openfile")=="1")then
if(not file.Exists(path..".txt", "DATA"))then AdvDupe2.Notify("File does not exist", NOTIFY_ERROR) return end
local readFile = file.Open(path..".txt", "rb", "DATA")
if not readFile then AdvDupe2.Notify("File could not be read", NOTIFY_ERROR) return end
local readData = readFile:Read(readFile:Size())
readFile:Close()
local success,dupe,info,moreinfo = AdvDupe2.Decode(readData)
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)
filename = filename[#filename]
if(AutoSave)then
if(IsValid(AdvDupe2.FileBrowser.AutoSaveNode))then
local add = true
for i=1, #AdvDupe2.FileBrowser.AutoSaveNode.Files do
if(filename==AdvDupe2.FileBrowser.AutoSaveNode.Files[i].Label:GetText())then
add=false
break
end
end
if(add)then
AdvDupe2.FileBrowser.AutoSaveNode:AddFile(filename)
AdvDupe2.FileBrowser.Browser.pnlCanvas:Sort(AdvDupe2.FileBrowser.AutoSaveNode)
end
end
else
AdvDupe2.FileBrowser.Browser.pnlCanvas.ActionNode:AddFile(filename)
AdvDupe2.FileBrowser.Browser.pnlCanvas:Sort(AdvDupe2.FileBrowser.Browser.pnlCanvas.ActionNode)
end
if(!errored)then
AdvDupe2.Notify("File successfully saved!",NOTIFY_GENERIC, 5)
end
end)
end
net.Receive("AdvDupe2_ReceiveFile", AdvDupe2_ReceiveFile)
local uploading = nil
function AdvDupe2.UploadFile(ReadPath, ReadArea)
if uploading then AdvDupe2.Notify("Already opening file, please wait.", NOTIFY_ERROR) return end
if(ReadArea==0)then
ReadPath = AdvDupe2.DataFolder.."/"..ReadPath..".txt"
elseif(ReadArea==1)then
ReadPath = AdvDupe2.DataFolder.."/-Public-/"..ReadPath..".txt"
else
ReadPath = "adv_duplicator/"..ReadPath..".txt"
end
if(not file.Exists(ReadPath, "DATA"))then AdvDupe2.Notify("File does not exist", NOTIFY_ERROR) return end
local read = file.Read(ReadPath)
if not read then AdvDupe2.Notify("File could not be read", NOTIFY_ERROR) return end
local name = string.Explode("/", ReadPath)
name = name[#name]
name = string.sub(name, 1, #name-4)
local success, dupe, info, moreinfo = AdvDupe2.Decode(read)
if(success)then
net.Start("AdvDupe2_ReceiveFile")
net.WriteString(name)
uploading = net.WriteStream(read, function()
uploading = nil
AdvDupe2.File = nil
AdvDupe2.RemoveProgressBar()
end)
net.SendToServer()
else
AdvDupe2.Notify("File could not be decoded. ("..dupe..") Upload Canceled.", NOTIFY_ERROR)
end
end
function AdvDupe2.LoadGhosts(dupe, info, moreinfo, name, preview)
AdvDupe2.RemoveGhosts()