local log = nrequire("log.lua") --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("CapsAdmin's PAC3 is required for items, please download it 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 local CLIENT_PAC_DIR = "artery/client/pacs" file.CreateDir(CLIENT_PAC_DIR) local function loadpac(ent,name,hash) if (not ent) or (not ent:IsValid()) then timer.Simple(1,function() loadpac(ent,name,hash) end) return end print("Told to apply pac", name, "to ent", ent) local filepath = string.format(CLIENT_PAC_DIR .. "/%s.txt",name) local filetext = file.Read(filepath,"DATA") if ent.AttachPACPart == nil then pac.SetupENT(ent) if ent.AttachPACPart == nil then timer.Simple(1,function() loadpac(ent,name,hash) end) return end end if filetext and tonumber(util.CRC(filetext)) then print("The file on our local system is up to date, applying!") local pactbl = CompileString(string.format("return {%s}",filetext),name)() ent:AttachPACPart(pactbl) else--Cache is old, download the new pac! log.debug(string.format("File out of date (%d vs %d) Re-downloading...", filetext and tonumber(util.CRC(filetext)) or 0 , hash)) 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(CLIENT_PAC_DIR .. "/%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) log.debug(string.format("Received file %s with hash %d", pac_name, pac_hash)) local filepath = string.format(CLIENT_PAC_DIR .. "/%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)