Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- include'kernel.class'
- local check=require'kernel.checker'
- local dump=require'utils.serpent'.dump
- local util=require'kernel.util'
- class.Var()
- local p=setmetatable({},{__mode='k'})
- function Var:__init(file,opt)
- p[self]={}
- p[self].file=file
- local dir=loadreq.getDir(p[self].file)
- if not fs.exists(dir) then fs.makeDir(dir) end
- if not fs.exists(file) then self:setAll{} end
- end
- function Var:getAll()
- local f=io.open(p[self].file,'r')
- local s=f:read'*a'
- f:close()
- local t=loadstring(s)()
- return t
- end
- function Var:setAll(t)
- local f=io.open(p[self].file,'w')
- local s=f:write(dump(t))
- f:close()
- end
- function Var:__get(k)
- local t=self:getAll()
- if not t then t={} self:setAll(t) end
- return t[k]
- end
- function Var:__set(k,v)
- local t=self:getAll()
- t[k]=v
- self:setAll(t)
- return true
- end
- return Var
Advertisement
Add Comment
Please, Sign In to add comment