1
0
Fork 0
mirror of https://github.com/Reuh/anselme.git synced 2025-10-27 16:49:31 +00:00

Use love.filesystem instead of io.open to read files if running inside Löve

Also small improvement to error reporting
This commit is contained in:
Étienne Fildadut 2021-04-09 18:12:11 +02:00
parent 0df120eeaf
commit f93d6fab6b
2 changed files with 20 additions and 8 deletions

View file

@ -171,19 +171,23 @@ local vm_mt = {
-- * main file: string, name (without .ans extension) of a file that will be loaded into the root namespace -- * main file: string, name (without .ans extension) of a file that will be loaded into the root namespace
-- * main file, if defined in config.ans -- * main file, if defined in config.ans
-- * every other file in the path and subdirectories, using their path as namespace (i.e., contents of path/world1/john.ans will be defined in a function world1.john) -- * every other file in the path and subdirectories, using their path as namespace (i.e., contents of path/world1/john.ans will be defined in a function world1.john)
-- returns self in case of success
-- returns nil, err in case of error
loadgame = function(self, path) loadgame = function(self, path)
-- get config -- get config
if is_file(path.."/config.ans") then if is_file(path.."/config.ans") then
self:loadfile(path.."/config.ans", "config") local s, e = self:loadfile(path.."/config.ans", "config")
if not s then return s, e end
end end
local seen_alias = self:eval("config.alias 👁️") local seen_alias = self:eval("config.alias 👁️")
local checkpoint_alias = self:eval("config.alias 🏁") local checkpoint_alias = self:eval("config.alias 🏁")
local main_file = assert(self:eval("config.main file")) local main_file = self:eval("config.main file")
-- set aliases -- set aliases
self:setaliases(seen_alias, checkpoint_alias) self:setaliases(seen_alias, checkpoint_alias)
-- load main file -- load main file
if main_file then if main_file then
self:loadfile(path.."/"..main_file..".ans") local s, e = self:loadfile(path.."/"..main_file..".ans")
if not s then return s, e end
end end
-- load other files -- load other files
for _, item in ipairs(list_directory(path)) do for _, item in ipairs(list_directory(path)) do
@ -212,10 +216,18 @@ local vm_mt = {
return self return self
end, end,
loadfile = function(self, path, name) loadfile = function(self, path, name)
local f, e = io.open(path, "r") local content
if not f then return f, e end if love then
local s, err = self:loadstring(f:read("*a"), name, path) local e
f:close() content, e = love.filesystem.read(path)
if not content then return content, e end
else
local f, e = io.open(path, "r")
if not f then return f, e end
content = f:read("*a")
f:close()
end
local s, err = self:loadstring(content, name, path)
if not s then return s, err end if not s then return s, err end
return self return self
end, end,

View file

@ -3,7 +3,7 @@ local truthy, flush_state, to_lua, eval_text
local function write_event(state, type, data) local function write_event(state, type, data)
if state.interpreter.event_buffer and state.interpreter.event_type ~= type then if state.interpreter.event_buffer and state.interpreter.event_type ~= type then
error("previous event has not been flushed") error(("previous event of type %q has not been flushed, can't write new %q event"):format(state.interpreter.event_type, type))
end end
if not state.interpreter.event_buffer then if not state.interpreter.event_buffer then
state.interpreter.event_type = type state.interpreter.event_type = type