From 291b109c0639b5aa15a89fc178dd6bd0c06bc4ea Mon Sep 17 00:00:00 2001 From: Timo Smit Date: Tue, 7 Feb 2017 23:10:02 +0100 Subject: [PATCH] Cleaned up files module --- luamods/wolfadmin/admin/rules.lua | 14 +++++----- luamods/wolfadmin/game/sprees.lua | 8 ++++-- luamods/wolfadmin/players/greetings.lua | 18 ++++++++----- luamods/wolfadmin/util/files.lua | 36 +++++-------------------- luamods/wolfadmin/util/logs.lua | 4 +-- luamods/wolfadmin/util/settings.lua | 4 +-- 6 files changed, 34 insertions(+), 50 deletions(-) diff --git a/luamods/wolfadmin/admin/rules.lua b/luamods/wolfadmin/admin/rules.lua index 0eb574d..f945874 100644 --- a/luamods/wolfadmin/admin/rules.lua +++ b/luamods/wolfadmin/admin/rules.lua @@ -27,25 +27,25 @@ function rules.get(shortcut) if shortcut then return data[shortcut] end - + return data end function rules.load() local fileName = settings.get("g_fileRules") - + if fileName == "" then 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 - + for id, rule in ipairs(array["rule"]) do data[rule["shortcut"]] = rule["rule"] end - + return amount end diff --git a/luamods/wolfadmin/game/sprees.lua b/luamods/wolfadmin/game/sprees.lua index cc7462a..c072bb6 100644 --- a/luamods/wolfadmin/game/sprees.lua +++ b/luamods/wolfadmin/game/sprees.lua @@ -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 diff --git a/luamods/wolfadmin/players/greetings.lua b/luamods/wolfadmin/players/greetings.lua index c5b87bb..bc22c20 100644 --- a/luamods/wolfadmin/players/greetings.lua +++ b/luamods/wolfadmin/players/greetings.lua @@ -81,19 +81,23 @@ 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"], diff --git a/luamods/wolfadmin/util/files.lua b/luamods/wolfadmin/util/files.lua index 46505bf..ec3dfaf 100644 --- a/luamods/wolfadmin/util/files.lua +++ b/luamods/wolfadmin/util/files.lua @@ -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.loadFromCFG(fileName, idExpr, fileCreate) + local fileDescriptor, fileLength = et.trap_FS_FOpenFile(fileName, et.FS_READ) -function files.open(fileName, fileMode, fileCreate) - local fileDescriptor, fileLength = et.trap_FS_FOpenFile(fileName, fileMode) - if fileLength == -1 then - if not fileCreate then - error("failed to open "..fileName.."\n") - end - - fileDescriptor, fileLength = files.create(fileName) + return nil 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 diff --git a/luamods/wolfadmin/util/logs.lua b/luamods/wolfadmin/util/logs.lua index 8e4f202..ae727c5 100644 --- a/luamods/wolfadmin/util/logs.lua +++ b/luamods/wolfadmin/util/logs.lua @@ -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 diff --git a/luamods/wolfadmin/util/settings.lua b/luamods/wolfadmin/util/settings.lua index 0d872b9..7987957 100644 --- a/luamods/wolfadmin/util/settings.lua +++ b/luamods/wolfadmin/util/settings.lua @@ -75,8 +75,8 @@ 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 data[blocksname.."_"..k] = v