Skip to content

Instantly share code, notes, and snippets.

@Srlion
Last active February 21, 2019 14:19
Show Gist options
  • Select an option

  • Save Srlion/961bd379d438cb8411bfd2515e6b739b to your computer and use it in GitHub Desktop.

Select an option

Save Srlion/961bd379d438cb8411bfd2515e6b739b to your computer and use it in GitHub Desktop.
local sv_load = function(file)
if (SERVER) then
return include(file)
end
end
local cl_load = function(file)
if (SERVER) then
AddCSLuaFile(file)
else
return include(file)
end
end
local sh_load = function(file)
if (SERVER) then
cl_load(file)
return sv_load(file)
else
return cl_load(file)
end
end
local types = {sv = sv_load, cl = cl_load, sh = sh_load}
function loadDirectory(folder, order, exclude)
if (folder:sub(#folder) != "/") then
folder = folder .. "/"
end
local files, folders = file.Find(folder .. "*", "LUA")
local folderName = folder:gsub("^[%w_]+/", "")
if (order) then
for k = 1, #order do
local v = order[k]
if (v == nil) then continue end
local name, cb
if (istable(v)) then
name = v[1]
cb = v[2]
else
name = v
end
for k2, v2 in ipairs(files) do
if (folderName .. v2 == name) then
order[k] = nil
table.RemoveByValue(files, v2)
local _type = v2:match("[^_]+")
local load = types[_type] || sh_load
if (cb && ((_type == "sv" && SERVER) || (_type == "cl" && CLIENT) || _type == "sh" || !types[_type])) then
cb(load(folder .. v2))
else
load(folder .. v2)
end
goto END
end
end
for k2, v2 in ipairs(folders) do
if (folderName .. v2 == name) then
order[k] = nil
table.RemoveByValue(folders, v2)
loadDirectory(folder .. v2, order)
goto END
end
end
::END::
end
end
for _, v in ipairs(files) do
if (v:GetExtensionFromFilename(v) != "lua") then continue end
if (exclude && table.HasValue(exclude, folderName .. v)) then continue end
local load = types[v:match("[^_]+")] || sh_load
load(folder .. v)
end
for _, v in ipairs(folders) do
if (exclude && table.HasValue(exclude, folderName .. v)) then continue end
loadDirectory(folder .. v, order, exclude)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment