aboutsummaryrefslogtreecommitdiff
path: root/gamemode/core/pac/cl_pac.lua
blob: 4f1890cabc474d727a02f2b5ab0e77eab54270aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
--[[
	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)

local CLIENT_PAC_DIR = "artery/client/pacs"

file.CreateDir(CLIENT_PAC_DIR)

local function loadpac(ent,name,hash)
	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)
		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(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)
	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)