Make a new FILE API and add a simple error screen in case most thing went down

This commit is contained in:
Squishy (C6H12O6+NaCl+H2O)
2024-05-26 16:39:08 +07:00
parent c45104ce88
commit 4b001cdf57
6 changed files with 117 additions and 49 deletions

View File

@@ -140,7 +140,7 @@ end
function GameMode:readGradeHistory() function GameMode:readGradeHistory()
if love.filesystem.getInfo(SAVE_DIR..self.player_name.."_grade_history.sav") then if love.filesystem.getInfo(SAVE_DIR..self.player_name.."_grade_history.sav") then
self.grade_history = bitser.loadLoveFile(SAVE_DIR..self.player_name.."_grade_history.sav") self.grade_history = FILE.read(SAVE_DIR..self.player_name.."_grade_history.sav")
else else
self.grade_history = {1,2,0,0} self.grade_history = {1,2,0,0}
end end
@@ -148,13 +148,13 @@ function GameMode:readGradeHistory()
self.starting_grade = self.grade self.starting_grade = self.grade
if self.grade > 1 then if self.grade > 1 then
local temp_grade = copy(self.grade_history); temp_grade[2] = 0 local temp_grade = copy(self.grade_history); temp_grade[2] = 0
bitser.dumpLoveFile(SAVE_DIR..self.player_name.."_grade_history.sav", temp_grade) FILE.write(SAVE_DIR..self.player_name.."_grade_history.sav", temp_grade)
end end
end end
function GameMode:readHiScores() function GameMode:readHiScores()
if love.filesystem.getInfo(HIscoreFILE) then if love.filesystem.getInfo(HIscoreFILE) then
self.hi_scores = bitser.loadLoveFile(HIscoreFILE) self.hi_scores = FILE.read(HIscoreFILE)
else else
self.hi_scores = {"TRO",0,"MIT",0,"ROM",0,"ITR",0,"OMI",0} self.hi_scores = {"TRO",0,"MIT",0,"ROM",0,"ITR",0,"OMI",0}
end end
@@ -220,7 +220,7 @@ function GameMode:updateHiScores()
self.hi_scores[score_position] = self.grade_score self.hi_scores[score_position] = self.grade_score
hiscore_pos = {score_position-1, score_position} hiscore_pos = {score_position-1, score_position}
end end
bitser.dumpLoveFile(HIscoreFILE, self.hi_scores) FILE.write(HIscoreFILE, self.hi_scores)
return hiscore_pos return hiscore_pos
end end
@@ -684,7 +684,7 @@ function GameMode:onGameOver()
self:updateGradeHistory() self:updateGradeHistory()
end end
hiscore_pos = self:updateHiScores() hiscore_pos = self:updateHiScores()
bitser.dumpLoveFile(SAVE_DIR..self.player_name.."_grade_history.sav", self.grade_history) FILE.write(SAVE_DIR..self.player_name.."_grade_history.sav", self.grade_history)
self.did_grades = true self.did_grades = true
end end
self:drawEndScoringInfo() self:drawEndScoringInfo()

View File

@@ -1,15 +1,16 @@
-- Bigint library
bigint = require "libs.bigint.bigint"
number_names = require "libs.bigint.named-powers-of-ten"
require "libs.simple-slider"
-- Fonts -- Fonts
FONT_tromi = love.graphics.newFont('res/fonts/monofonto rg.otf', 28) FONT_tromi = love.graphics.newFont('res/fonts/monofonto rg.otf', 28)
FONT_big = love.graphics.newFont('res/fonts/monofonto rg.otf', 56) FONT_big = love.graphics.newFont('res/fonts/monofonto rg.otf', 56)
local font_height = FONT_tromi:getHeight() * 0.5 local font_height = FONT_tromi:getHeight() * 0.5
local font_big_height = FONT_big:getHeight() * 0.5 local font_big_height = FONT_big:getHeight() * 0.5
-- Bigint library
bigint = require "libs.bigint.bigint"
number_names = require "libs.bigint.named-powers-of-ten"
-- BUTTON library -- BUTTON library
require "libs.simple-slider"
BUTTON = require "libs.simple-button" BUTTON = require "libs.simple-button"
BUTTON.setDefaultOption{ BUTTON.setDefaultOption{
draw = function(self) draw = function(self)

View File

@@ -2,7 +2,6 @@ if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE")=="1" then
LLDEBUGGER=require('lldebugger') LLDEBUGGER=require('lldebugger')
LLDEBUGGER.start() LLDEBUGGER.start()
end end
require 'funcs' require 'funcs'
DEBUG_showKey = false DEBUG_showKey = false
@@ -36,6 +35,7 @@ end
function love.load() function love.load()
math.randomseed(os.time()) math.randomseed(os.time())
require "modules.file" "binser"
require "settings" require "settings"
-- Window stuffs -- Window stuffs
@@ -48,8 +48,8 @@ function love.load()
-- Now it's real time to load all stuffs! -- Now it's real time to load all stuffs!
require "load" -- Most game's resources are loaded in here require "load" -- Most game's resources are loaded in here
require "game.vctrl" -- VCTRL
require "modules.scene" require "modules.scene"
require "game.vctrl" -- VCTRL
function SCENE.update() function SCENE.update()
SCENE = SETTINGS.firstTime and InputConfigScene(true) or TitleScene() SCENE = SETTINGS.firstTime and InputConfigScene(true) or TitleScene()
@@ -367,35 +367,6 @@ function love.errorhandler(msg)
local errorCopied = false local errorCopied = false
local enter_fullscreen = SETTINGS and SETTINGS["fullscreen"] or false local enter_fullscreen = SETTINGS and SETTINGS["fullscreen"] or false
-- Render everything again in a canva
love.graphics.origin()
local screenshot_canva, screenshot_canva_scale
local ok, _ = pcall(function()
if love.graphics.getSystemLimits().texturesize >= 1280 then
screenshot_canva = love.graphics.newCanvas(1280, 960)
screenshot_canva_scale = 0.5
else
error()
end
end)
if not ok then
screenshot_canva = love.graphics.newCanvas(640, 480)
screenshot_canva_scale = 1
end
love.graphics.setCanvas(screenshot_canva)
pcall(
function()
love.graphics.origin()
local transformer = love.math.newTransform(0, 0, 0, 2, 2)
love.graphics.replaceTransform(transformer)
SCENE:render()
end
)
love.graphics.setCanvas()
love.audio.stop()
-- Handling the error -- Handling the error
local err={"Error:"..msg} local err={"Error:"..msg}
local c=2 local c=2
@@ -411,7 +382,62 @@ function love.errorhandler(msg)
end end
end end
print("\n"..table.concat(err,"\n",1,c-2)) print("\n"..table.concat(err,"\n",1,c-2))
local p = table.concat(err,"\n", 4) local tracebacks = table.concat(err,"\n", 4)
if drawText == nil then
pcall(function()
FONT_tromi = love.graphics.newFont('res/fonts/monofonto rg.otf', 20)
require "funcs"
end)
if drawText == nil then
love.window.setMode(640, 480, {resizable = true})
return function() -- If "funcs" failed to load, we can only return a more simple version of error screen
love.event.pump()
for e, a, b, c in love.event.poll() do
if e == "quit" then return 1 end
end
love.graphics.origin()
love.graphics.clear()
love.graphics.setColor(1, 1, 1)
love.graphics.setFont(FONT_tromi)
love.graphics.printf(
err[1]:sub(7).."\nTraceback:\n"..tracebacks,
30, 30, love.graphics.getWidth() - 10, "left"
)
love.graphics.present()
if love.timer then love.timer.sleep(0.1) end
end
end
end
-- Try to create a canva
love.graphics.origin()
local screenshot_canva, screenshot_canva_scale
local ok, _ = pcall(function()
if love.graphics.getSystemLimits().texturesize >= 1280 then
screenshot_canva = love.graphics.newCanvas(1280, 960)
screenshot_canva_scale = 0.5
else
error()
end
end)
if not ok then
screenshot_canva = love.graphics.newCanvas(640, 480)
screenshot_canva_scale = 1
end
-- Then draw everything again
love.graphics.setCanvas(screenshot_canva)
pcall(
function()
love.graphics.origin()
local transformer = love.math.newTransform(0, 0, 0, 2, 2)
love.graphics.replaceTransform(transformer)
SCENE:render()
end
)
love.audio.stop()
love.graphics.setCanvas()
local function draw() local function draw()
love.graphics.origin() love.graphics.origin()
@@ -436,7 +462,7 @@ Space : show/hide screenshot | OK : Quit Copy: copy error info
Escape : Quit | Cancel: Go back Show: show/hide screenshot Escape : Quit | Cancel: Go back Show: show/hide screenshot
]], 20, 10, 620, "left") ]], 20, 10, 620, "left")
drawText(err[1]:sub(7).."\nTraceback:"..(errorCopied and " (Copied to clipboard)\n" or "\n")..p, 20, 180, 620, "left") drawText(err[1]:sub(7).."\nTraceback:"..(errorCopied and " (Copied to clipboard)\n" or "\n")..tracebacks, 20, 180, 620, "left")
else else
love.graphics.setColor(0, 0, 0, 0.8) love.graphics.setColor(0, 0, 0, 0.8)
love.graphics.rectangle("fill", 15, 450, 400, 25, 5, 5) love.graphics.rectangle("fill", 15, 450, 400, 25, 5, 5)
@@ -446,7 +472,7 @@ Escape : Quit | Cancel: Go back Show: show/hide screensh
love.graphics.present() love.graphics.present()
end end
local fullErrorText = p local fullErrorText = tracebacks
local function copyToClipboard() local function copyToClipboard()
love.system.setClipboardText(fullErrorText) love.system.setClipboardText(fullErrorText)
errorCopied = true errorCopied = true

41
modules/file.lua Normal file
View File

@@ -0,0 +1,41 @@
local FILE = {}
local binser = require "libs.binser"
local bitser = require "libs.bitser"
local serializer_used
function FILE.serialize(data)
if serializer_used == 'bitser' then
return bitser.dumps(data)
else
return binser.serialize(data)
end
end
function FILE.deserialize(data)
if serializer_used == 'bitser' then
return bitser.loads(data)
else
return binser.deserialize(data)[1]
end
end
function FILE.read(path)
if love.filesystem.getInfo(path) then
return FILE.deserialize(love.filesystem.read(path))
else
error("No file: "..path)
end
end
function FILE.write(path, data)
love.filesystem.write(path, FILE.serialize(data))
end
---@param lib_name 'bitser'|'binser'
---Init the FILE module with chosen serializer
return function(lib_name)
assert(lib_name == 'bitser' or lib_name == 'binser', '[lib_name] must be "bitser" or "binser"')
serializer_used = lib_name
_G.FILE = FILE
end

View File

@@ -56,7 +56,7 @@ function NameEntryScene:new()
self.entry_pos = 3 self.entry_pos = 3
end end
if love.filesystem.getInfo(HIscoreFILE) then if love.filesystem.getInfo(HIscoreFILE) then
self.hi_scores = bitser.loadLoveFile(HIscoreFILE) self.hi_scores = FILE.read(HIscoreFILE)
else else
self.hi_scores = {"TRO",0,"MIT",0,"ROM",0,"ITR",0,"OMI",0} self.hi_scores = {"TRO",0,"MIT",0,"ROM",0,"ITR",0,"OMI",0}
end end
@@ -150,7 +150,7 @@ function NameEntryScene:onInputPress(e)
if self.entry_pos == 3 then if self.entry_pos == 3 then
name = string.lower(self.name_entry[1]..self.name_entry[2]..self.name_entry[3]) name = string.lower(self.name_entry[1]..self.name_entry[2]..self.name_entry[3])
if love.filesystem.getInfo((SAVE_DIR..name.."_grade_history.sav")) then if love.filesystem.getInfo((SAVE_DIR..name.."_grade_history.sav")) then
grade_history = bitser.loadLoveFile(SAVE_DIR..name.."_grade_history.sav") grade_history = FILE.read(SAVE_DIR..name.."_grade_history.sav")
self.grade = grade_history[1] self.grade = grade_history[1]
self.wins = grade_history[2] self.wins = grade_history[2]
self.plays = grade_history[4] self.plays = grade_history[4]

View File

@@ -1,7 +1,7 @@
local bitser = require 'libs.bitser' local bitser = require 'libs.bitser'
local fs = love.filesystem local fs = love.filesystem
local _settings = fs.read(CONFIG_FILE) ~= nil and bitser.loadLoveFile(CONFIG_FILE) or {} local _settings = fs.read(CONFIG_FILE) ~= nil and FILE.read(CONFIG_FILE) or {}
local _defaultSettings = { local _defaultSettings = {
firstTime = true, firstTime = true,
@@ -37,13 +37,13 @@ SETTINGS = setmetatable(
__index = function(_, k) __index = function(_, k)
if _settings[k] == nil then if _settings[k] == nil then
_settings[k] = _defaultSettings[k] _settings[k] = _defaultSettings[k]
bitser.dumpLoveFile(CONFIG_FILE,_settings) FILE.write(CONFIG_FILE,_settings)
end end
return _settings[k] return _settings[k]
end, end,
__newindex = function(_, k, v) __newindex = function(_, k, v)
_settings[k] = v _settings[k] = v
bitser.dumpLoveFile(CONFIG_FILE,_settings) FILE.write(CONFIG_FILE,_settings)
end end
} }
) )