aboutsummaryrefslogtreecommitdiff
path: root/gamemode/shared/sh_pac.lua
blob: 1ab9f88eab1057a278cf2d29138fc0d6a92e2da7 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
--[[
    All the functions related to networking pac's
]]
if CLIENT then
    local function applypac(who,pacname)
        local pactxt = file.Read("artery/pacs/"..pacname..".txt","DATA")
        print("got pac txt",pactxt)
        if pactxt == nil then
            print("requesting pac")
            net.Start("requestpac")
            net.WriteString(pacname)
            net.SendToServer()
            timer.Simple(5,function() --5 seconds is probably enough time to load a pac, right?
                print("trying to re-apply pac")
                applypac(who,pacname)
            end)
        else
            if who.AttachPACPart == nil then
                print("setting up ent for pac")
                pac.SetupENT(who)
            end
            print("pactxt was",pactxt)
            local pactbl = CompileString("return {"..pactxt.."}",pacname)()
            print("pactbl is")
            PrintTable(pactbl)
            print("who is",who)
            who:AttachPACPart(pactbl)
            print("Pac Equiped!")
        end
    end

    local function removepac(who,pacname)
        local pactxt = file.Read("artery/pacs/"..pacname..".txt","DATA")
        assert(pactxt ~= nil, "Attempted to remove a pac that dosn't exist")
        assert(who.RemovePACPart ~= nil,"That entity isn't set up to have pacs!")
        local pactbl = CompileString("return {" .. pactxt .. "}", pacname)()
        who:RemovePACPart(pactbl)
    end

    net.Receive("loadpac",function()
        local pacname = net.ReadString()
        local pacdata = util.Decompress(net.ReadString())
        file.Write("artery/pacs/"..pacname..".txt",pacdata)
    end)

    net.Receive("applypac",function()
        print("apply ing pac")
        local towho = net.ReadEntity()
        local pacname = net.ReadString()
        applypac(towho,pacname)
    end)

    net.Receive("removepac",function()
        local towho = net.ReadEntity()
        local pacname = net.ReadString()
        removepac(towho,pacname)
    end)
else
    for _,v in pairs({
        "applypac",
        "removepac",
        "requestpac",
        "loadpac",
    })do util.AddNetworkString(v) end

    net.Receive("requestpac",function(ln,ply)
        local pacname = net.ReadString()
        local pacdata = file.Read("artery/pacs/"..pacname..".txt","DATA")
        assert(pacdata ~= nil,string.format("Client %q requested pac that dosn't exist %q",ply:Nick(),pacname))
        net.Start("loadpac")
        net.WriteString(pacname)
        net.WriteString(util.Compress(pacdata))
        net.Send(ply)
    end)

    local playerpacs = {}
    ART.ApplyPAC = function(to,pacname)
        print("Applying pac")
        net.Start("applypac")
        net.WriteEntity(to)
        net.WriteString(pacname)
        net.Broadcast()
        playerpacs[to] = playerpacs[to]  or {}
        table.insert(playerpacs[to],pacname)
    end

    hook.Add( "PlayerInitialSpawn", "loadpacs", function(ply)
        timer.Simple(5,function() --Wait 5 seconds, hopefully they're ok to go by then
            for k,v in pairs(playerpacs) do
                for i,j in pairs(v) do
                    net.Start("applypac")
                    net.WriteEntity(k)
                    net.WriteString(j)
                    net.Send(ply)
                end
            end
        end)
    end)

    ART.RemovePAC = function(to,pacname)
        net.Start("removepac")
        net.WriteEntity(to)
        net.WriteString(pacname)
        net.Broadcast()
        table.RemoveByValue(playerpacs[to], pacname)
    end
end
local paclist