From 2e7b7fe19fe47d7f616949db771b2813ff62af8f Mon Sep 17 00:00:00 2001 From: Harrison <53527582+HTV04@users.noreply.github.com> Date: Sat, 13 Aug 2022 09:07:15 -0400 Subject: [PATCH] Fix main.lua error detection and love.filesystem.load --- data/api.lua | 10 +++++++ data/boot.lua | 44 ++++++++++++++++-------------- src/wiilove/main.cpp | 1 - src/wiilove/modules/filesystem.cpp | 5 ---- src/wiilove/modules/filesystem.hpp | 1 - 5 files changed, 33 insertions(+), 28 deletions(-) diff --git a/data/api.lua b/data/api.lua index 5429c2d..8022c27 100644 --- a/data/api.lua +++ b/data/api.lua @@ -36,6 +36,7 @@ do love.audio = {} + -- Custom newSource function that registers the Source function love.audio.newSource(...) local source = newSource(...) @@ -45,6 +46,7 @@ do end function love.audio.pause(...) + local _ local sourceList = {...} for _, source in ipairs(sourceList) do @@ -52,6 +54,7 @@ do end end function love.audio.play(...) + local _ local sourceList = {...} for _, source in ipairs(sourceList) do @@ -59,6 +62,7 @@ do end end function love.audio.resume(...) + local _ local sourceList = {...} for _, source in ipairs(sourceList) do @@ -66,6 +70,7 @@ do end end function love.audio.stop(...) + local _ local sourceList = {...} if #sourceList == 0 then @@ -90,6 +95,11 @@ do end end +-- love.filesystem +function love.filesystem.load(filename) + return loadstring(love.filesystem.read(filename), filename) +end + -- love.graphics do local newFont = love.graphics.newFont diff --git a/data/boot.lua b/data/boot.lua index bd7b97c..3f6b418 100644 --- a/data/boot.lua +++ b/data/boot.lua @@ -22,8 +22,6 @@ License along with this program. If not, see local love = love -local result = 0 - -- Redirect package paths package.path = "save/?.lua;save/?/init.lua;data/?.lua;data/?/init.lua" package.cpath = "" -- Disable C modules @@ -87,7 +85,7 @@ function love.run() for name, a,b,c,d,e,f in love.event.poll do if name == "homepressed" then if not love.homepressed or not love.homepressed(a) then - return 0 + return end else love.handlers[name](a,b,c,d,e,f) @@ -111,18 +109,12 @@ function love.run() end end -function love.errhand(err) +function love.errorhandler(err) local msg = "Error\n\n" .. tostring(err) .. "\n\n\n" .. string.gsub(string.gsub(debug.traceback(), "\t", ""), "stack traceback:", "Traceback\n") .. "\n\n\nPress HOME to return to loader\n" - local msgTable = {} - - -- Temporary workaround for newlines - for line in string.gmatch(msg, "([^\n]*)\n") do - table.insert(msgTable, line) - end -- Stop all Wiimote vibrations if love.system.getConsole() ~= "GameCube" then @@ -141,16 +133,12 @@ function love.errhand(err) for name, a,b,c,d,e,f in love.event.poll do if name == "homepressed" then - return 1 + return end end love.graphics.clear(89, 157, 220) - - for i, line in ipairs(msgTable) do - love.graphics.print(line, 70, 60 + i * 18) - end - + love.graphics.print(msg, 40, 40) love.graphics.present() end end @@ -159,13 +147,27 @@ if love.getMode() == "final" then -- Run everything unprotected -- Load main.lua to intialize functions love.filesystem.load("main.lua")() - love.run() + return love.run() else -- Run everything protected + local main, error + + local success + -- Load main.lua to intialize functions - xpcall(love.filesystem.load("main.lua"), love.errhand) + if not love.filesystem.exists("main.lua") then + love.errorhandler("Could not find main.lua, no code to run") - _, result = xpcall(love.run, love.errhand) -end + return + end + main, error = love.filesystem.load("main.lua") + if not main then + love.errorhandler(error) + return + end + success = xpcall(main, love.errorhandler) -return result + if success == true then + success = xpcall(love.run, love.errorhandler) + end +end diff --git a/src/wiilove/main.cpp b/src/wiilove/main.cpp index ec7c07d..36f3f91 100644 --- a/src/wiilove/main.cpp +++ b/src/wiilove/main.cpp @@ -104,7 +104,6 @@ int main(int argc, char **argv) { "filesystem", lua.create_table_with( "exists", love::filesystem::module::exists, - "load", love::filesystem::module::load, "read", love::filesystem::module::read, "write", love::filesystem::module::write ), diff --git a/src/wiilove/modules/filesystem.cpp b/src/wiilove/modules/filesystem.cpp index c64eecc..f5ca117 100644 --- a/src/wiilove/modules/filesystem.cpp +++ b/src/wiilove/modules/filesystem.cpp @@ -93,11 +93,6 @@ namespace module { bool exists(const char *filename) { return std::filesystem::exists(getFilePath(filename)); } -sol::protected_function load(const char *filename, sol::this_state s) { - sol::state_view lua(s); - - return lua.load_file(getFilePath(filename)).get(); // TODO: Add error handling -} std::string read(const char *filename) { std::stringstream stream; diff --git a/src/wiilove/modules/filesystem.hpp b/src/wiilove/modules/filesystem.hpp index 78eebae..aa90b1c 100644 --- a/src/wiilove/modules/filesystem.hpp +++ b/src/wiilove/modules/filesystem.hpp @@ -36,7 +36,6 @@ void getFileData(const char *filename, void *&data, int &size); namespace module { bool exists(const char *filename); -sol::protected_function load(const char *filename, sol::this_state s); std::string read(const char *filename); void write (const std::string &filename, const char *data);