Skip to content

Commit

Permalink
Fix main.lua error detection and love.filesystem.load
Browse files Browse the repository at this point in the history
  • Loading branch information
htv04 committed Aug 13, 2022
1 parent 21c61bb commit 2e7b7fe
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 28 deletions.
10 changes: 10 additions & 0 deletions data/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ do

love.audio = {}

-- Custom newSource function that registers the Source
function love.audio.newSource(...)
local source = newSource(...)

Expand All @@ -45,27 +46,31 @@ do
end

function love.audio.pause(...)
local _
local sourceList = {...}

for _, source in ipairs(sourceList) do
source:pause()
end
end
function love.audio.play(...)
local _
local sourceList = {...}

for _, source in ipairs(sourceList) do
source:play()
end
end
function love.audio.resume(...)
local _
local sourceList = {...}

for _, source in ipairs(sourceList) do
source:resume()
end
end
function love.audio.stop(...)
local _
local sourceList = {...}

if #sourceList == 0 then
Expand All @@ -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
Expand Down
44 changes: 23 additions & 21 deletions data/boot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
1 change: 0 additions & 1 deletion src/wiilove/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
),
Expand Down
5 changes: 0 additions & 5 deletions src/wiilove/modules/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<sol::protected_function>(); // TODO: Add error handling
}
std::string read(const char *filename) {
std::stringstream stream;

Expand Down
1 change: 0 additions & 1 deletion src/wiilove/modules/filesystem.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 2e7b7fe

Please sign in to comment.