Post by Revan on Aug 29, 2009 14:47:28 GMT -5
This thread will contain various useful pieces of code that I write (or acquire with permission) that can be applied in many situations, enjoy:
This is a recursive function to load in all files of specific directory...
This is the gamemode version of this code... if you wanted to use it for an addon, change the for loop's in pairs section to read 'in pairs(base .. "*") do'
The same concepts demonstrated here can be applied to load resources also (.mdl, .vmt, .vtf, etc files)
A more efficient version of string.Explode
Several functions used to load a file in a specific manor. Essentially, this will load your file into tables and sub-tables based on tabbing. Below the code is an example file. Note: the curtabs parameter should be 0 when you call the function.
Example file for usage with the above:
Here's another file loading system, this one doesn't bother to account for tabs and thus makes a good way to load settings.. hence the function name..
The function looks for a file called "settings.txt" inside of garrysmod/data/modname/
Note: assumes that the table 'settings' already exists.
And I'll add more to this post as I write it/look over old code...
This is a recursive function to load in all files of specific directory...
This is the gamemode version of this code... if you wanted to use it for an addon, change the for loop's in pairs section to read 'in pairs(base .. "*") do'
The same concepts demonstrated here can be applied to load resources also (.mdl, .vmt, .vtf, etc files)
function LoadDir(base, client)
for i, thefile in pairs(file.FindInLua(GM.FolderName .. "/gamemode/" .. base .. "*")) do
local name = string.sub(thefile, -4)
if name == ".lua" then
print("MMOFPS Loading: " .. thefile)
if client then
AddCSLuaFile(base .. thefile)
else
include(base .. thefile)
end
end
end
end
A more efficient version of string.Explode
function Explode(delim, str)
if (not delim) || delim == "" then
str = delim
delim = " "
end
local res = {}
for k in str.gmatch(str, "[^" .. delim .. "]+") do
table.insert(res, k)
end
return res
end
Several functions used to load a file in a specific manor. Essentially, this will load your file into tables and sub-tables based on tabbing. Below the code is an example file. Note: the curtabs parameter should be 0 when you call the function.
local function getStartingTabs(line)
local tabs = 0
for i, char in pairs(string.ToTable(line)) do
if char == "\t" then
tabs = tabs + 1
else
break
end
end
return tabs
end
function parseByTabs(lines, curtabs)
//starts at 0 tabs all lines
local tbl = {}
local groupIndex = nil
for i, ln in pairs(lines) do
if ln && string.Trim(string.Trim(ln), "\t") != "" then
local tabs = getStartingTabs(ln)
if tabs == curtabs then
ln = string.Trim(ln, "\t")
local colon = string.find(ln, ":")
if colon then
local index = string.Trim(string.sub(ln, 1, colon - 1))
local data = string.Trim(string.sub(ln, colon + 1, string.len(ln)))
if index && index != "" && data then
if data != "" then
tbl[index] = data
else
groupIndex = index
end
end
else
table.insert(tbl, ln)
end
elseif tabs > curtabs then
local lns = { ln }
local count = 0
for x = i + 1, table.Count(lines) do
if getStartingTabs(lines[x]) >= tabs then
table.insert(lns, lines[x])
count = count + 1
else
break
end
end
i = i + count
if groupIndex then
tbl[groupIndex] = parseByTabs(lns, tabs)
groupIndex = nil
end
elseif tabs < curtabs then
return tbl
end
end
end
return tbl
end
Example file for usage with the above:
Name: Allies
Icon: vgui/flag_allies
Classes:
Soldier:
Model: player/american_mg vgui/cls_us_bar
Loadout:
weapon_bar
weapon_m1carbine
Medic:
Model: player/american_rifleman vgui/cls_us_thompson
Loadout:
weapon_thompsonm1a1
Loadout:
weapon_colt1911
Here's another file loading system, this one doesn't bother to account for tabs and thus makes a good way to load settings.. hence the function name..
The function looks for a file called "settings.txt" inside of garrysmod/data/modname/
Note: assumes that the table 'settings' already exists.
local function loadSettings()
if not file.Exists("modname/settings.txt") then
file.Write("modname/settings.txt", "")
end
local lines = string.Explode("\n", file.Read("modname/settings.txt"))
for i, ln in pairs(lines) do
ln = string.lower(ln)
local parts = Explode(" ", ln)
if table.Count(parts) >= 2 then
local name = parts[1]
table.remove(parts, 1)
settings[name] = table.concat(parts, " ")
end
end
end
And I'll add more to this post as I write it/look over old code...