aboutsummaryrefslogtreecommitdiff
path: root/gamemode/core/dataloader/sv_loadglobals.lua
blob: 85bbc5f1939817f709e701d99cb4fca7dd24f958 (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
print("Load globals called")

local function ExecuteOnFolder(dir, recursive, func)
    recursive = recursive ~= nil and recursive or false
    local path = "data/artery/"
    local fpath = table.concat({path,dir,"/*"})
    local files, directories = file.Find(fpath,"GAME")
    for k,v in pairs(files) do
        local callpath = table.concat({path,dir,"/",v})
        func(callpath)
    end
    if recursive then
        for k,v in pairs(directories) do
            local npath = table.concat({dir,"/",v})
            ExecuteOnFolder(npath,true,func)
        end
    end
end

util.AddNetworkString("artery_loadfile")
util.AddNetworkString("artery_requestcsfile")
util.AddNetworkString("artery_respondfile")
local toload = {}
local function loadglobals()
	ExecuteOnFolder("global",true,function(f)
        local filetxt = file.Read(f,"GAME")
        local filename = string.GetFileFromFilename(f)
        if string.find(filename,"^cl_") then
            toload[f] = filetxt
        elseif string.find(filename,"^sv_") then
            CompileString(filetxt,f)()
        else            
            toload[f] = filetxt
            CompileString(filetxt,f)()
        end
	end)
end

local function load_cs_files(ply)
    for k,v in pairs(toload) do
        net.Start("artery_loadfile")
        net.WriteString(k)
        local hash = util.CRC(v)
        net.WriteUInt(tonumber(hash),32)
        net.Send(ply)
    end
end

hook.Add("PlayerInitialSpawn","artery_loadglobals",function(ply)
    timer.Simple(1,function()
        load_cs_files(ply)
    end)
end)

net.Receive("artery_requestcsfile",function(ln,ply)
    local which = net.ReadString()
    net.Start("artery_respondfile")
    net.WriteString(which)
    net.WriteString(toload[which])
    net.Send(ply)
end)

concommand.Add("artery_reloadglobals",function(ply,cmd,args)
	if not ply:IsAdmin() then return end
	loadglobals()
	for k,v in pairs(player.GetAll()) do
        load_cs_files(v)
    end
end)
loadglobals()