Cleaned up files module

This commit is contained in:
Timo Smit 2017-02-07 23:10:02 +01:00
parent 0105d181ab
commit 291b109c06
6 changed files with 34 additions and 50 deletions

View file

@ -38,7 +38,7 @@ function rules.load()
return 0
end
local amount, array = files.loadCFG(fileName, "[a-z]+", true)
local amount, array = files.loadFromCFG(fileName, "[a-z]+")
if amount == 0 then return 0 end

View file

@ -103,13 +103,17 @@ function sprees.load()
local fileName = settings.get("g_fileSprees")
local amount, array = files.loadCFG(fileName, "[a-z]+", true)
for i = 0, sprees.RECORD_NUM - 1 do
spreeMessages[i] = {}
spreeMessagesByType[i] = {}
end
if fileName == "" then
return 0
end
local amount, array = files.loadFromCFG(fileName, "[a-z]+")
for name, block in pairs(array) do
for _, spree in ipairs(block) do
for k, v in pairs(spree) do

View file

@ -82,18 +82,22 @@ end
function greetings.load()
local fileName = settings.get("g_fileGreetings")
local amount, array = files.loadCFG(fileName, "[a-z]+", true)
if fileName == "" then
return 0
end
local amount, array = files.loadFromCFG(fileName, "[a-z]+")
if amount == 0 then return 0 end
for id, greeting in ipairs(array["level"]) do
for _, greeting in ipairs(array["level"]) do
levelGreetings[tonumber(greeting["level"])] = {
["text"] = greeting["greeting"],
["sound"] = greeting["sound"],
}
end
for id, greeting in ipairs(array["user"]) do
for _, greeting in ipairs(array["user"]) do
userGreetings[greeting["guid"]] = {
["text"] = greeting["greeting"],
["sound"] = greeting["sound"],

View file

@ -36,38 +36,14 @@ function files.ls(directory)
return entries
end
function files.create(fileName)
local fileDescriptor, fileLength = et.trap_FS_FOpenFile(fileName, et.FS_WRITE)
return fileDescriptor, fileLength
end
function files.open(fileName, fileMode, fileCreate)
local fileDescriptor, fileLength = et.trap_FS_FOpenFile(fileName, fileMode)
function files.loadFromCFG(fileName, idExpr, fileCreate)
local fileDescriptor, fileLength = et.trap_FS_FOpenFile(fileName, et.FS_READ)
if fileLength == -1 then
if not fileCreate then
error("failed to open "..fileName.."\n")
return nil
end
fileDescriptor, fileLength = files.create(fileName)
end
if fileMode == et.FS_READ then
local fileString = et.trap_FS_Read(fileDescriptor, fileLength)
et.trap_FS_FCloseFile(fileDescriptor)
return fileString
else
return fileDescriptor
end
return false
end
function files.loadCFG(fileName, idExpr, fileCreate)
local fileString = files.open(fileName, et.FS_READ, fileCreate).."\n\n"
local fileString = et.trap_FS_Read(fileDescriptor, fileLength).."\n\n"
local arrayCount = 0
local array = {}
@ -93,8 +69,8 @@ function files.loadCFG(fileName, idExpr, fileCreate)
return arrayCount, array
end
function files.save(fileName, array)
local fileDescriptor = files.open(fileName, et.FS_WRITE)
function files.saveToCFG(fileName, array)
local fileDescriptor, fileLength = et.trap_FS_FOpenFile(fileName, et.FS_WRITE)
local arrayCount = 0
for id, subdata in pairs(array) do

View file

@ -23,7 +23,7 @@ local settings = require (wolfa_getLuaPath()..".util.settings")
local logs = {}
function logs.writeChat(clientId, type, ...)
local fileDescriptor = files.open(settings.get("g_logChat"), et.FS_APPEND)
local fileDescriptor, fileLength = et.trap_FS_FOpenFile(settings.get("g_logChat"), et.FS_APPEND)
local logLine
@ -45,7 +45,7 @@ function logs.writeChat(clientId, type, ...)
end
function logs.writeAdmin(clientId, command, victimId, ...)
local fileDescriptor = files.open(settings.get("g_logAdmin"), et.FS_APPEND)
local fileDescriptor, fileLength = et.trap_FS_FOpenFile(settings.get("g_logAdmin"), et.FS_APPEND)
local logLine

View file

@ -75,7 +75,7 @@ function settings.load()
end
local files = require (wolfa_getLuaPath()..".util.files")
local amount, array = files.loadCFG("wolfadmin.cfg", "[a-z]+", true)
local amount, array = files.loadFromCFG("wolfadmin.cfg", "[a-z]+")
for blocksname, settings in pairs(array) do
for k, v in pairs(settings[1]) do