96 lines
2.8 KiB
Lua
96 lines
2.8 KiB
Lua
local fs=love.filesystem
|
|
local FILE={}
|
|
function FILE.load(name,mode)
|
|
if fs.getInfo(name)then
|
|
local F=fs.newFile(name)
|
|
if F:open'r'then
|
|
local s=F:read()
|
|
F:close()
|
|
if mode=='luaon'or not mode and s:sub(1,6)=="return{"then
|
|
s=loadstring(s)
|
|
if s then
|
|
setfenv(s,{})
|
|
return s()
|
|
end
|
|
elseif mode=='json'or not mode and s:sub(1,1)=="["and s:sub(-1)=="]"or s:sub(1,1)=="{"and s:sub(-1)=="}"then
|
|
local res=JSON.decode(s)
|
|
if res then
|
|
return res
|
|
end
|
|
elseif mode=='string'or not mode then
|
|
return s
|
|
else
|
|
MES.new("No file loading mode called "..tostring(mode))
|
|
end
|
|
else
|
|
MES.new('error',name.." "..text.loadError)
|
|
end
|
|
end
|
|
end
|
|
function FILE.save(data,name,mode)
|
|
if not mode then mode=""end
|
|
if type(data)=='table'then
|
|
if mode:find'l'then
|
|
data=TABLE.dump(data)
|
|
if not data then
|
|
MES.new('error',name.." "..text.saveError.."dump error")
|
|
return
|
|
end
|
|
else
|
|
data=JSON.encode(data)
|
|
if not data then
|
|
MES.new('error',name.." "..text.saveError.."json error")
|
|
return
|
|
end
|
|
end
|
|
else
|
|
data=tostring(data)
|
|
end
|
|
|
|
if mode:find'd'and fs.getInfo(name)then
|
|
MES.new('error',text.saveError_duplicate)
|
|
return
|
|
end
|
|
local F=fs.newFile(name)
|
|
F:open'w'
|
|
local success,mes=F:write(data)
|
|
F:flush()F:close()
|
|
if success then
|
|
return true
|
|
else
|
|
MES.new('error',text.saveError..(mes or"unknown error"))
|
|
MES.traceback()
|
|
end
|
|
end
|
|
function FILE.clear(path)
|
|
if fs.getRealDirectory(path)==SAVEDIR and fs.getInfo(path).type=='directory'then
|
|
for _,name in next,fs.getDirectoryItems(path)do
|
|
name=path..'/'..name
|
|
if fs.getRealDirectory(name)==SAVEDIR then
|
|
local t=fs.getInfo(name).type
|
|
if t=='file'then
|
|
fs.remove(name)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
function FILE.clear_s(path)
|
|
if path==''or(fs.getRealDirectory(path)==SAVEDIR and fs.getInfo(path).type=='directory')then
|
|
for _,name in next,fs.getDirectoryItems(path)do
|
|
name=path..'/'..name
|
|
if fs.getRealDirectory(name)==SAVEDIR then
|
|
local t=fs.getInfo(name).type
|
|
if t=='file'then
|
|
fs.remove(name)
|
|
elseif t=='directory'then
|
|
FILE.clear_s(name)
|
|
fs.remove(name)
|
|
end
|
|
end
|
|
end
|
|
fs.remove(path)
|
|
end
|
|
end
|
|
return FILE
|