diff options
Diffstat (limited to 'gamemode/core/pac/cl_pac.lua')
| -rw-r--r-- | gamemode/core/pac/cl_pac.lua | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/gamemode/core/pac/cl_pac.lua b/gamemode/core/pac/cl_pac.lua new file mode 100644 index 0000000..9163d82 --- /dev/null +++ b/gamemode/core/pac/cl_pac.lua @@ -0,0 +1,90 @@ +--[[ + A lazy loader for pac3 costumes, used for weapons/armor/ect. +]] + +--As soon as the client connects, request the names of all the pac's on the server + +--If the player dosen't have PAC3 installed, then overwrite all pac-related network events to display an error. +if pac == nil then + local function no_pac_panic() + error("This gamemode require PAC3 to display armor/cloths, please be sure clients are downloading this addon from somewhere (perferably the workshop!)") + end + local networkmsgs = { + "artery_downloadpac", + "artery_applypac", + "artery_giveworldpacs", + "artery_removepac" + } + for k,v in pairs(networkmsgs) do + net.Receive(v,no_pac_panic) + end + no_pac_panic() + return --Don't execute this file! +end + +timer.Simple(0,function() + net.Start("artery_getworldpacs") + net.SendToServer() +end) + +file.CreateDir("artery/pacs") + +local function loadpac(ent,name,hash) + print("Told to apply pac", name, "to ent", ent) + local filepath = string.format("artery/pacs/%s.txt",name) + local filetext = file.Read(filepath,"DATA") + if ent.AttachPACPart == nil then + pac.SetupENT(ent) + assert(ent.AttachPACPart ~= nil,"Failed to set up ent",ent) + end + if filetext and (tonumber(util.CRC(filetext)) == hash) then + local pactbl = CompileString(string.format("return {%s}",filetext),name)() + ent:AttachPACPart(pactbl) + else--Cache is old, download the new pac! + net.Start("artery_requestpac") + net.WriteString(name) + net.SendToServer() + --1 second is probably long enough to download a pac, right? + timer.Simple(1,function() + loadpac(ent,name,hash) + end) + end +end + +local function unloadpac(ent,name,hash) + local filepath = string.format("artery/pacs/%s.txt",name) + local filetext = file.Read(filepath,"DATA") + local pactbl = CompileString(string.format("return {%s}",filetext),name)() + ent:RemovePACPart(pactbl) +end + +net.Receive("artery_downloadpac",function() + local pac_name = net.ReadString() + local pac_txt = net.ReadString() + local pac_hash = net.ReadUInt(32) + local filepath = string.format("artery/pacs/%s.txt",pac_name) + file.Write(filepath,pac_txt) +end) + +net.Receive("artery_applypac",function() + local pac_ent = net.ReadEntity() + local pac_name = net.ReadString() + local pac_hash = net.ReadUInt(32) + loadpac(pac_ent,pac_name,pac_hash) +end) + +net.Receive("artery_giveworldpacs",function() + local pactbl = net.ReadTable() + for ent,pacnames in pairs(pactbl) do + for name, hash in pairs(pacnames) do + loadpac(ent,name,hash) + end + end +end) + +net.Receive("artery_removepac",function() + local pac_ent = net.ReadEntity() + local pac_name = net.ReadString() + local pac_hash = net.ReadUInt(32) + unloadpac(pac_ent,pac_name,pac_hash) +end) |
