文件模块重构

This commit is contained in:
MrZ626
2020-12-14 18:33:48 +08:00
parent b6b87f5480
commit 654a5b9dfe
17 changed files with 76 additions and 238 deletions

View File

@@ -1,176 +1,51 @@
local fs=love.filesystem
local files={
data= fs.newFile("data.dat"),
setting=fs.newFile("settings.dat"),
network=fs.newFile("network.dat"),
VK= fs.newFile("virtualkey.dat"),
keyMap= fs.newFile("key.dat"),
unlock= fs.newFile("unlock.dat"),
}
local FILE={}
function FILE.loadRecord(N)
local F=fs.newFile(N..".dat")
function FILE.load(name)
name=name..".dat"
local F=fs.newFile(name)
if F:open("r")then
local s=loadstring(F:read())
local s=F:read()
F:close()
if s then
setfenv(s,{})
return s()
if s:sub(1,6)=="return"then
s=loadstring(s)
if s then
setfenv(s,{})
return s()
end
else
return{}
local _,res=json.decode(s)
if _ then
return res
else
LOG.print(name.." "..text.loadError..(mes or"unknown error"),COLOR.red)
end
end
end
end
function FILE.saveRecord(N,L)
local F=fs.newFile(N..".dat")
F:open("w")
local _,mes=F:write(dumpTable(L))
F:flush()F:close()
if not _ then
LOG.print(text.recSavingError..(mes or"unknown error"),COLOR.red)
function FILE.save(data,name,mode,luacode)
if not mode then mode="m"end
name=name..".dat"
local _,mes
if not luacode then
_,data=json.encode(data)
if not _ then
LOG.print(name.." "..text.saveError..(mes or"json error"),"error")
return
end
else
data=dumpTable(data)
end
end
function FILE.delRecord(N)
fs.remove(N..".dat")
end
function FILE.loadUnlock()
local F=files.unlock
if F:open("r")then
local s=F:read()
if s:sub(1,6)~="return"then s="return{"..s.."}"end
s=loadstring(s)
F:close()
if s then
setfenv(s,{})
RANKS=s()
end
end
end
function FILE.saveUnlock()
local F=files.unlock
local F=fs.newFile(name)
F:open("w")
local _,mes=F:write(dumpTable(RANKS))
_,mes=F:write(data)
F:flush()F:close()
if not _ then
LOG.print(text.unlockSavingError..(mes or"unknown error"),COLOR.red)
end
end
function FILE.loadData()
local F=files.data
if F:open("r")then
local s=F:read()
if s:sub(1,6)~="return"then
s="return{"..s:gsub("\n",",").."}"
if _ then
if mode:find("m")then
LOG.print(text.saveDone,COLOR.green)
end
s=loadstring(s)
F:close()
if s then
setfenv(s,{})
local S=s()
addToTable(S,STAT)
end
end
end
function FILE.saveData()
local F=files.data
F:open("w")
local _,mes=F:write(dumpTable(STAT))
F:flush()F:close()
if not _ then
LOG.print(text.statSavingError..(mes or"unknown error"),COLOR.red)
end
end
function FILE.loadSetting()
local F=files.setting
if F:open("r")then
local s=F:read()
if s:sub(1,6)~="return"then
s="return{"..s:gsub("\n",",").."}"
end
s=loadstring(s)
F:close()
if s then
setfenv(s,{})
addToTable(s(),SETTING)
end
end
end
function FILE.saveSetting()
local F=files.setting
F:open("w")
local _,mes=F:write(dumpTable(SETTING))
F:flush()F:close()
if _ then LOG.print(text.settingSaved,COLOR.green)
else LOG.print(text.settingSavingError..(mes or"unknown error"),COLOR.red)
end
end
function FILE.loadAccount()
local F=files.setting
if F:open("r")then
local s=F:read()
if s:sub(1,6)~="return"then
s="return{"..s:gsub("\n",",").."}"
end
s=loadstring(s)
F:close()
if s then
setfenv(s,{})
addToTable(s(),ACCOUNT)
end
end
end
function FILE.saveAccount()
local F=files.setting
F:open("w")
F:write(dumpTable(ACCOUNT))
F:flush()F:close()
end
function FILE.loadKeyMap()
local F=files.keyMap
if F:open("r")then
local s=loadstring(F:read())
F:close()
if s then
setfenv(s,{})
addToTable(s(),keyMap)
end
end
end
function FILE.saveKeyMap()
local F=files.keyMap
F:open("w")
local _,mes=F:write(dumpTable(keyMap))
F:flush()F:close()
if _ then LOG.print(text.keyMapSaved,COLOR.green)
else LOG.print(text.keyMapSavingError..(mes or"unknown error"),COLOR.red)
end
end
function FILE.loadVK()
local F=files.VK
if F:open("r")then
local s=loadstring(F:read())
F:close()
if s then
setfenv(s,{})
addToTable(s(),VK_org)
end
end
end
function FILE.saveVK()
local F=files.VK
F:open("w")
local _,mes=F:write(dumpTable(VK_org))
F:flush()F:close()
if _ then LOG.print(text.VKSaved,COLOR.green)
else LOG.print(text.VKSavingError..(mes or"unknown error"),COLOR.red)
else
LOG.print(text.saveError..(mes or"unknown error"),"error")
end
end
return FILE

View File

@@ -17,13 +17,13 @@ SYSTEM=love.system.getOS()
MOBILE=SYSTEM=="Android"or SYSTEM=="iOS"
SAVEDIR=fs.getSaveDirectory()
--Global Vars & Settings
MARKING=true
LOADED=false
NOGAME=false
LOGIN=false
EDITING=""
--Global Setting & Vars
math.randomseed(os.time()*626)
love.keyboard.setKeyRepeat(true)
love.keyboard.setTextInput(false)
@@ -140,7 +140,7 @@ end
--Load files & settings
if fs.getInfo("settings.dat")then
FILE.loadSetting()
SETTING=FILE.load("settings")
else
if MOBILE then
SETTING.VKSwitch=true
@@ -155,10 +155,11 @@ end
LANG.set(SETTING.lang)
if SETTING.fullscreen then love.window.setFullscreen(true)end
if fs.getInfo("unlock.dat")then FILE.loadUnlock()end
if fs.getInfo("data.dat")then FILE.loadData()end
if fs.getInfo("key.dat")then FILE.loadKeyMap()end
if fs.getInfo("virtualkey.dat")then FILE.loadVK()end
if fs.getInfo("unlock.dat")then RANKS=FILE.load("unlock")end
if fs.getInfo("data.dat")then STAT=FILE.load("data")end
print(STAT.version)
if fs.getInfo("key.dat")then keyMap=FILE.load("key")end
if fs.getInfo("virtualkey.dat")then VK_org=FILE.load("virtualkey")end
if fs.getInfo("tech_ultimate.dat")then fs.remove("tech_ultimate.dat")end
if fs.getInfo("tech_ultimate+.dat")then fs.remove("tech_ultimate+.dat")end
@@ -197,22 +198,9 @@ do
if S.extraRate then
S.finesseRate=5*(S.piece-S.extraRate)
end
if S.version~=VERSION_NAME then
S.version=VERSION_NAME
if S.version~=VERSION_CODE then
newVersionLaunch=true
if not VERSION_NAME:find("0.12")or VERSION_NAME:find("0.12.0")then
local function delRecord(n)
if R[n]then
R[n]=0
fs.remove(n..".dat")
end
end
delRecord("solo_1")delRecord("solo_2")delRecord("solo_3")delRecord("solo_4")delRecord("solo_5")
delRecord("dig_10")delRecord("dig_40")delRecord("dig_100")delRecord("dig_400")
delRecord("classic_fast")
end
--Try unlock modes which should be unlocked
for name,rank in next,RANKS do
if rank and rank>0 then
@@ -227,8 +215,10 @@ do
end
end
end
FILE.saveUnlock()
FILE.saveData()
S.version=VERSION_CODE
FILE.save(RANKS,"unlock","")
FILE.save(STAT,"data","")
end
if MOBILE and not SETTING.fullscreen then
LOG.print("如果手机上方状态栏不消失,请到设置界面开启全屏",300,COLOR.yellow)

View File

@@ -45,15 +45,9 @@ return{
ai_fixed="The AI is incompatible with fixed sequences.",
ai_prebag="The AI is incompatible with custom sequences.",
ai_mission="The AI is incompatible with custom missions.",
settingSaved="Settings Saved",
settingSavingError="Failed to save settings:",
keyMapSaved="Key settings saved",
keyMapSavingError="Failed to save key settings:",
VKSaved="Virtual keys saved",
VKSavingError="Failed to save virtual key configuration:",
statSavingError="Failed to save statistics:",
unlockSavingError="Failed to save unlocked content:",
recSavingError="Failed to save record:",
saveDone="Data Saved",
saveError="Failed to save:",
loadError="Failed to read:",
switchSpawnSFX="Switch on spawn SFX to play",
ranks={"D","C","B","A","S"},

View File

@@ -48,15 +48,9 @@ return{
ai_fixed="L'IA est incompatible avec les séquences fixes.",
ai_prebag="L'IA est incompatible avec les séquences personnalisées.",
ai_mission="L'IA est incompatible avec les missions personnalisées.",
settingSaved="Paramètres sauvegardés",
settingSavingError="Erreur de sauvegarde des paramètres :",
keyMapSaved="Touches sauvegardées.",
keyMapSavingError="Erreur de sauvegarde des touches :",
VKSaved="Touches virtuelles sauvegardées",
VKSavingError="Erreur de sauvegarde des touches virtuelles :",
statSavingError="Erreur de sauvegarde des statistiques :",
unlockSavingError="Erreur de sauvegarde du contenu débloqué :",
recSavingError="Erreur de sauvegarde des records :",
-- saveDone="Data Saved",
-- saveError="Failed to save:",
-- loadError="Failed to read:",
switchSpawnSFX="Activez les effets sonores d'apparition des pièces pour jouer",
getRank="Rang: ",

View File

@@ -48,14 +48,9 @@ return{
ai_prebag="La IA no es compatible con secuencias de piezas personalizadas.",
ai_mission="La IA no es compatible con misiones personalizadas.",
settingSaved="Cambios guardados",
settingSavingError="Error al guardar los cambios:",
keyMapSaved="Configuración de controles guardada",
keyMapSavingError="Error al guardar la configración de controles:",
VKSaved="Configuración de botones en pantalla guardada",
VKSavingError="Error al guardar la configuración de botones en pantalla:",
statSavingError="Error al guardar las estadísticas:",
unlockSavingError="Error al guardar el contenido desbloqueado:",
recSavingError="Error al guardar la grabación:",
-- saveDone="Data Saved",
-- saveError="Failed to save:",
-- loadError="Failed to read:",
switchSpawnSFX="Habilita los sonidos de aparición de las piezas ;)",
getRank="Rango obtenido:",

View File

@@ -38,15 +38,9 @@ return{
ai_fixed="X!!!",
ai_prebag="X!!!",
ai_mission="X!!!",
settingSaved="~~~",
settingSavingError="x!:",
keyMapSaved="~~~",
keyMapSavingError="x!:",
VKSaved="~~~",
VKSavingError="x!:",
statSavingError="x!:",
unlockSavingError="x!:",
recSavingError="x!:",
saveDone="~~~",
saveError="x!:",
loadError="x!:",
needRestart="!!*#R#*!!",
exportSuccess="~Out~",

View File

@@ -46,15 +46,9 @@ return{
ai_fixed="不能同时开启AI和固定序列",
ai_prebag="不能同时开启AI和自定义序列",
ai_mission="不能同时开启AI和自定义任务",
settingSaved="设置已保存",
settingSavingError="设置保存失败:",
keyMapSaved="键位设置已保存",
keyMapSavingError="键位保存失败:",
VKSaved="虚拟按键设置已保存",
VKSavingError="虚拟按键保存失败:",
statSavingError="统计保存失败:",
unlockSavingError="解锁保存失败:",
recSavingError="纪录保存失败:",
saveDone="保存成功!",
saveError="保存失败:",
loadError="读取失败:",
switchSpawnSFX="请开启方块出生音效",
ranks={"D","C","B","A","S"},

View File

@@ -1358,7 +1358,7 @@ end
--------------------------<Events>--------------------------
local function gameOver()--Save record
if GAME.replaying then return end
FILE.saveData()
FILE.save(STAT,"data")
local M=GAME.curMode
local R=M.getRank
if R then
@@ -1388,7 +1388,7 @@ local function gameOver()--Save record
end
end
if needSave then
FILE.saveUnlock()
FILE.save(RANKS,"unlock")
end
local D=M.score(P)
local L=M.records
@@ -1406,7 +1406,7 @@ local function gameOver()--Save record
D.date=os.date("%Y/%m/%d %H:%M")
ins(L,p+1,D)
if L[11]then L[11]=nil end
FILE.saveRecord(M.name,L)
FILE.save(L,M.name,"",true)
end
end
end

View File

@@ -82,7 +82,7 @@ function keyDown.calculator(k)
RANKS[name]=M.score and 0 or 6
end
end
FILE.saveUnlock()
FILE.save(RANKS,"unlock")
LOG.print("\68\69\86\58\85\78\76\79\67\75\65\76\76","message")
SFX.play("clear_2")
elseif v==1379e8+2626e4+1379 then

View File

@@ -1,5 +1,5 @@
function sceneBack.lang()
FILE.saveSetting()
FILE.save(SETTING,"settings")
end
local function setLang(n)return function()SETTING.lang=n LANG.set(n)end end

View File

@@ -70,7 +70,7 @@ function Tmr.load()
for k,v in next,m do
M[k]=v
end
M.records=FILE.loadRecord(m.name)or M.score and{}
M.records=FILE.load(m.name)or M.score and{}
if M.score then
if RANKS[M.name]==6 then
RANKS[M.name]=0

View File

@@ -117,7 +117,9 @@ function sceneBack.pause()
if not GAME.replaying then
mergeStat(STAT,PLAYERS[1].stat)
end
FILE.saveData()
if not GAME.result then
FILE.save(STAT,"data")
end
end
function keyDown.pause(key)

View File

@@ -7,7 +7,7 @@ function sceneInit.setting_game()
BG.set("space")
end
function sceneBack.setting_game()
FILE.saveSetting()
FILE.save(SETTING,"settings")
end
function Pnt.setting_game()

View File

@@ -13,7 +13,7 @@ function sceneInit.setting_key()
}
end
function sceneBack.setting_key()
FILE.saveKeyMap()
FILE.save(keyMap,"key")
end
function keyDown.setting_key(key)

View File

@@ -13,7 +13,7 @@ function sceneInit.setting_sound()
BG.set("space")
end
function sceneBack.setting_sound()
FILE.saveSetting()
FILE.save(SETTING,"settings")
end
function mouseDown.setting_sound(x,y)

View File

@@ -13,7 +13,7 @@ function sceneInit.setting_touch()
}
end
function sceneBack.setting_touch()
FILE.saveVK()
FILE.save(VK_org,"virtualkey")
end
local function onVK_org(x,y)

View File

@@ -2,7 +2,7 @@ function sceneInit.setting_video()
BG.set("space")
end
function sceneBack.setting_video()
FILE.saveSetting()
FILE.save(SETTING,"settings")
end
WIDGET.init("setting_video",{