How to load a Lua configuration file into table


(fart11eleven) #1

Hi, im working on version 1.0 of this script here: http://forums.warchest.com/showthread.php/32647-Communal-Mailbox-script

I am not quite up to date on the ET Lua API. Is there a way to load a config file into the Lua script. Example: i have the script commail.lua and a table in there. From another file called commail.cfg i wanna load information into that table, is it possible somewhat easy or are there examples out there showing how to do this?


(Micha) #2

There are couple of luas which read stuff from files:
http://mnwa.bplaced.net/ftpfiles/Lua/kspree.lua
http://mnwa.bplaced.net/ftpfiles/Lua/randomspawn.lua
http://mnwa.bplaced.net/ftpfiles/Lua/seen.lua
http://mnwa.bplaced.net/ftpfiles/Lua/!NGadmin1.lua

You are in need of “pattern” then.
for example this line:
for name, guid, password in string.gfind(content_file, “([^
]+) (%w+) (%w+)”) do


(ryven) #3

I wrote some simple iterator to fetch all keys and values from et config like files: (key “value” etc)
I don’t know if this is what you wanted tho, but here are sample usage and code:

Usage (should be used in for loop):


-- use it in for loop to fetch all keys and values
for [B]key[/B], [B]value [/B]in [I]readconfig [/I][B]"sample.cfg"[/B] do
    -- you can fill your table here as well as do some value check
   configTable [[B]key[/B]] = [B]value[/B]
end


function readconfig (fileName)
    fd, len = et.trap_FS_FOpenFile(fileName, et.FS_READ)

    if len ~= -1 then
        filedata = et.trap_FS_Read(fd, len)
    end

    et.trap_FS_FCloseFile(fd)

    if not filedata then return nil end
    
    local lines = string.gmatch(filedata, "[^
]+")
    local i = 0
    -- basic iterator + line iterator
    return function ()
        
        while true do
            
            local key, value = parseConfig ( lines (i + 1) )
            -- skipping empty strings ...
            if key then
                if key ~= "" then
                    return key, value
                end 
            else
                return nil
            end 

        end

    end

end

function parseConfig (line)

    if not line then return nil end

    -- lets clean double slash comment, "set" and "exec" keywords
    line = line :gsub ("//.+", "")
                :gsub ("^%s*set%s+", "")
                :gsub ("^%s*exec%s+.+", "")
                :gsub ("^%s*(.-)%s*$", "%1") -- trim string
    -- skip
    if line == "" then return "" end
        
    local key, value = line:match "([%w_]+)%s+(.+)"
    
    if key and value then

        local temp = tonumber (value)

        if temp then
            return key, temp
        else
            temp = value:match "^\"(.+)\"$"
            -- all string values should be enclosed with quotes
            if temp then

                -- trying to conver "16" string numbers into numbers 
                value, temp = temp, tonumber (temp)

                if temp then 
                    value = temp
                end

                return key, value

            elseif value == '""' then
                -- empty values
                return key, ""
            end
        end
    end

    return ""
end


(fart11eleven) #4

Thank you both for the info, very useful :slight_smile: