aboutsummaryrefslogtreecommitdiff
path: root/gamemode/autolua.lua
blob: d550b83e7b402386797289d06305a42b157b3811 (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
--[[
    A few functions to make it easier to include files and folders
    Thanks to TheMaw for this idea, this entire file is basically ripped form GearFox.

    You can AddLua(SV|SH|CS)Folder(foldername) to add that folder in the domain.
    Additionally, in the shared domain, if a folder has the folders sv_*.lua and cl_*.lua, and optionally sh_*.lua, these will be included in the server, client, and shared domain respectively.
]]
-- @module autolua
local auto = {}

local function ExecuteOnFolder(dir, recursive, func)
    recursive = recursive ~= nil and recursive or false
    local path = GM.Folder:gsub("gamemodes/","") .. "/gamemode/"
    local fpath = table.concat({path,dir,"/*"})
    local files, directories = file.Find(fpath,"LUA")
    for k,v in pairs(files) do
        if string.GetExtensionFromFilename(v) ~= "lua" then continue end
        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

--- Adds files client side.
-- Calls AddCSLuaFile on all files under a folder server side, and include() client side, if the recursive flag is checked, calls all files under all folders as well.
-- @string dir The directory relative to /gamemode/ to add client side
-- @param recursive boolean value that when true includes all sub-folders recursively.
auto.AddLuaCSFolder = function(dir,recursive)
    ExecuteOnFolder(dir,recursive,function(f)
        if SERVER then AddCSLuaFile(f)
        else include(f) end
    end)
end

--- Adds files server side.
-- Calls include() on all files under a folder, if the recursive flag is checked, calls all files under all folders as well.
-- @string dir The directory relative to /gamemode/ to add client side
-- @param recursive boolean value that when true includes all sub-folders recursively.
auto.AddLuaSVFolder = function(dir,recursive)
    if CLIENT then return end
    ExecuteOnFolder(dir,recursive,function(f)
        include(f)
    end)
end

--- Adds files server side.
-- Calls include() and AddCSLuaFile() on all files under a folder server side, and include() client side, if the recursive flag is checked, calls all files under all folders as well. If the file starts with cl_ it will only be included on the client side, if the files starts with sv_ it will only be included on the server side.
-- @string dir The directory relative to /gamemode/ to add client side
-- @param recursive boolean value that when true includes all sub-folders recursively.
auto.AddLuaSHFolder = function(dir,recursive)
    ExecuteOnFolder(dir,recursive,function(f)
        if f:find("cl_%a*.lua") then
            AddCSLuaFile(f)
        elseif f:find("sv_%a.lua") then
            if SERVER then include(f) end
        else
            if SERVER then AddCSLuaFile(f) end
            include(f)
        end
    end)
end

--- Adds resources for the client to download
-- Calls resource.AddFile() on all files under a directory, recursive.
-- @param the path to add all files under, relative to the /content/ directory.
auto.AddContentFolder = function(dir)
    local path = string.format("gamemodes/artery/content/%s/",dir)
    local files, directories = file.Find(path .. "*","MOD")
    for k,v in pairs(files) do
        resource.AddFile(path .. v)
    end
    for k,v in pairs(directories) do
        local npath = table.concat({dir,"/",v})
        auto.AddContentFolder(npath)
    end
end

return auto