Atualização lib memor

master
BrunoMine 2018-08-03 17:45:08 -03:00
parent b99f549b57
commit 4f2aff65bd
1 changed files with 52 additions and 29 deletions

View File

@ -1,13 +1,12 @@
--[[
Lib Memor para Minetest
Memor v2.0.0 Copyright (C) 2017 BrunoMine (https://github.com/BrunoMine)
Memor v1.3 Copyright (C) 2018 BrunoMine (https://github.com/BrunoMine)
Recebeste uma cópia da GNU Lesser General
Public License junto com esse software,
se não, veja em <http://www.gnu.org/licenses/>.
Autoria do código:
Originalmente por BrunoMine, Bruno Borges <borgesdossantosbruno@gmail.com>
Script para manipulação simples de banco de dados de um mod
]]
local modname = minetest.get_current_modname()
@ -15,7 +14,7 @@ local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)
-- Variavel global
memor = {}
local memor = {}
-- Rotinas de interação com arquivos
@ -40,32 +39,43 @@ function memor.mkdir(dir)
end
-- Criar um arquivo com os dados serializados (Salvar)
function memor.escrever(dir, arquivo, dados)
function memor.escrever(dir, arquivo, dados, is_text)
if dir == nil or arquivo == nil or dados == nil then
minetest.log("error", "[Memor] Faltou dados (em memor.escrever)")
return false
end
local dados = minetest.serialize(dados)
if is_text ~= true then
dados = minetest.serialize(dados)
else
dados = string.format(dados)
end
if dados == "" then
minetest.log("error", "[Memor] Dado fornecido invalido (em memor.escrever)")
return false
end
local saida = io.open(wpath .. "/" .. modname .. "/" .. dir .. "/" .. arquivo, "w")
if saida then
saida:write(dados)
io.close(saida)
return true
else
minetest.log("info", "[Memor] memor.escrever tentou escrever num diretorio inexistente")
return false
end
-- Cria diretorio (tabela) caso nao exista
memor.mkdir(modname.."/"..dir)
saida = io.open(wpath .. "/" .. modname .. "/" .. dir .. "/" .. arquivo, "w")
if saida then
saida:write(dados)
io.close(saida)
return true
end
minetest.log("error", "[Memor] Impossivel escrever dados em "..modname.."/"..dir.."/"..arquivo.." (em memor.escrever)")
return false
end
-- Ler dados de um arquivo de memória (Carregar)
function memor.ler(dir, arquivo)
function memor.ler(dir, arquivo, is_text)
if dir == nil or arquivo == nil then
minetest.log("error", "[Memor] Faltou dados (em memor.ler)")
@ -73,10 +83,14 @@ function memor.ler(dir, arquivo)
end
local entrada = io.open(wpath .. "/" .. modname .. "/" .. dir .. "/" .. arquivo, "r")
if entrada then
local dados = entrada:read("*l")
if dados ~= "" or dados ~= nil then
if entrada ~= nil then
local dados
if is_text ~= true then
dados = entrada:read("*l")
dados = minetest.deserialize(dados)
else
dados = entrada:read("*a")
dados = dados
end
io.close(entrada)
return dados
@ -122,27 +136,26 @@ end
-- Rotinas de consutas a arquivos
-- Verifica diretorios e corrige
verificar = function(subdir)
local verificar = function(subdir)
local dir = modname
-- Verifica e corrige diretorio
local list = minetest.get_dir_list(minetest.get_worldpath(), true)
local r = false
for n, ndir in ipairs(list) do
if ndir == dir then
if ndir == modname then
r = true
break
end
end
-- Diretorio inexistente
if r == false then
memor.mkdir(dir)
memor.mkdir(modname)
end
-- Verifica e corrige subdiretorio
list = minetest.get_dir_list(minetest.get_worldpath().."/"..dir, true)
r = false
local list = minetest.get_dir_list(minetest.get_worldpath().."/"..modname, true)
local r = false
for n, ndir in ipairs(list) do
if ndir == subdir then
r = true
@ -151,21 +164,21 @@ verificar = function(subdir)
end
-- Subdiretorio inexistente
if r == false then
memor.mkdir(dir.."/"..subdir)
memor.mkdir(modname.."/"..subdir)
end
end
-- Inserir dados
memor.inserir = function(tb, index, valor)
memor.inserir = function(tb, index, valor, is_text)
-- Tenta inserir direto
if memor.escrever(tb, index, valor) == true then return true end
if memor.escrever(tb, index, valor, is_text) == true then return true end
verificar(tb)
if memor.escrever(tb, index, valor) then
if memor.escrever(tb, index, valor, is_text) then
return true
else
minetest.log("error", "[Memor] Impossivel salvar dados (em memor.inserir)")
@ -176,9 +189,9 @@ end
-- Ler dados
memor.consultar = function(tb, index)
memor.consultar = function(tb, index, is_text)
local r = memor.ler(tb, index)
local r = memor.ler(tb, index, is_text)
if r == nil then
local mod = modname
minetest.log("error", "[Memor] Registro acessado inexistente ("..dump(mod).."/"..dump(tb).."/"..dump(index)..") (em memor.consultar)")
@ -194,8 +207,8 @@ memor.verificar = function(subdir, arquivo)
local dir = modname
list = minetest.get_dir_list(wpath .. "/" .. dir .. "/" .. subdir)
r = false
local list = minetest.get_dir_list(wpath .. "/" .. dir .. "/" .. subdir)
local r = false
for n, arq in ipairs(list) do
if arq == arquivo then
r = true
@ -244,16 +257,26 @@ end
bd = {}
-- Inserir dados
-- Inserir dados comuns
bd.salvar = function(tb, index, valor)
return memor.inserir(tb, index, valor)
end
-- Inserir textos complexos
bd.salvar_texto = function(tb, index, valor)
return memor.inserir(tb, index, valor, true)
end
-- Consultar dados
bd.pegar = function(tb, index)
return memor.consultar(tb, index)
end
-- Inserir dados
bd.pegar_texto = function(tb, index, valor)
return memor.consultar(tb, index, true)
end
-- Verificar dados
bd.verif = function(tb, index)
return memor.verificar(tb, index)