文件模块重构

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 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={} local FILE={}
function FILE.loadRecord(N) function FILE.load(name)
local F=fs.newFile(N..".dat") name=name..".dat"
local F=fs.newFile(name)
if F:open("r")then if F:open("r")then
local s=loadstring(F:read()) local s=F:read()
F:close() F:close()
if s then if s:sub(1,6)=="return"then
setfenv(s,{}) s=loadstring(s)
return s() if s then
setfenv(s,{})
return s()
end
else 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 end
end end
function FILE.saveRecord(N,L) function FILE.save(data,name,mode,luacode)
local F=fs.newFile(N..".dat") if not mode then mode="m"end
F:open("w") name=name..".dat"
local _,mes=F:write(dumpTable(L)) local _,mes
F:flush()F:close() if not luacode then
if not _ then _,data=json.encode(data)
LOG.print(text.recSavingError..(mes or"unknown error"),COLOR.red) if not _ then
LOG.print(name.." "..text.saveError..(mes or"json error"),"error")
return
end
else
data=dumpTable(data)
end end
end
function FILE.delRecord(N)
fs.remove(N..".dat")
end
function FILE.loadUnlock() local F=fs.newFile(name)
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
F:open("w") F:open("w")
local _,mes=F:write(dumpTable(RANKS)) _,mes=F:write(data)
F:flush()F:close() F:flush()F:close()
if not _ then if _ then
LOG.print(text.unlockSavingError..(mes or"unknown error"),COLOR.red) if mode:find("m")then
end LOG.print(text.saveDone,COLOR.green)
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",",").."}"
end end
s=loadstring(s) else
F:close() LOG.print(text.saveError..(mes or"unknown error"),"error")
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)
end end
end end
return FILE return FILE

View File

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

View File

@@ -45,15 +45,9 @@ return{
ai_fixed="The AI is incompatible with fixed sequences.", ai_fixed="The AI is incompatible with fixed sequences.",
ai_prebag="The AI is incompatible with custom sequences.", ai_prebag="The AI is incompatible with custom sequences.",
ai_mission="The AI is incompatible with custom missions.", ai_mission="The AI is incompatible with custom missions.",
settingSaved="Settings Saved", saveDone="Data Saved",
settingSavingError="Failed to save settings:", saveError="Failed to save:",
keyMapSaved="Key settings saved", loadError="Failed to read:",
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:",
switchSpawnSFX="Switch on spawn SFX to play", switchSpawnSFX="Switch on spawn SFX to play",
ranks={"D","C","B","A","S"}, 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_fixed="L'IA est incompatible avec les séquences fixes.",
ai_prebag="L'IA est incompatible avec les séquences personnalisées.", ai_prebag="L'IA est incompatible avec les séquences personnalisées.",
ai_mission="L'IA est incompatible avec les missions personnalisées.", ai_mission="L'IA est incompatible avec les missions personnalisées.",
settingSaved="Paramètres sauvegardés", -- saveDone="Data Saved",
settingSavingError="Erreur de sauvegarde des paramètres :", -- saveError="Failed to save:",
keyMapSaved="Touches sauvegardées.", -- loadError="Failed to read:",
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 :",
switchSpawnSFX="Activez les effets sonores d'apparition des pièces pour jouer", switchSpawnSFX="Activez les effets sonores d'apparition des pièces pour jouer",
getRank="Rang: ", getRank="Rang: ",

View File

@@ -48,14 +48,9 @@ return{
ai_prebag="La IA no es compatible con secuencias de piezas personalizadas.", ai_prebag="La IA no es compatible con secuencias de piezas personalizadas.",
ai_mission="La IA no es compatible con misiones personalizadas.", ai_mission="La IA no es compatible con misiones personalizadas.",
settingSaved="Cambios guardados", settingSaved="Cambios guardados",
settingSavingError="Error al guardar los cambios:", -- saveDone="Data Saved",
keyMapSaved="Configuración de controles guardada", -- saveError="Failed to save:",
keyMapSavingError="Error al guardar la configración de controles:", -- loadError="Failed to read:",
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:",
switchSpawnSFX="Habilita los sonidos de aparición de las piezas ;)", switchSpawnSFX="Habilita los sonidos de aparición de las piezas ;)",
getRank="Rango obtenido:", getRank="Rango obtenido:",

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,5 +1,5 @@
function sceneBack.lang() function sceneBack.lang()
FILE.saveSetting() FILE.save(SETTING,"settings")
end end
local function setLang(n)return function()SETTING.lang=n LANG.set(n)end 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 for k,v in next,m do
M[k]=v M[k]=v
end 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 M.score then
if RANKS[M.name]==6 then if RANKS[M.name]==6 then
RANKS[M.name]=0 RANKS[M.name]=0

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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