From 3338a68bd4b09c0c2101b9f5a01a4dc386faa64f Mon Sep 17 00:00:00 2001 From: RodrigoDornelles Date: Wed, 31 Jul 2024 18:15:10 -0300 Subject: [PATCH] fix: lua 5.1 compatibility --- examples/launcher/game.lua | 5 ++-- src/engine/core/ginga/main.lua | 1 + src/engine/core/html5/main.lua | 1 + src/engine/core/love/main.lua | 1 + src/engine/core/nintendo_wii/main.lua | 1 + src/lib/engine/module.lua | 38 +++++++++++++++++++-------- src/lib/protocol/http_ginga.lua | 19 +++++++++++++- 7 files changed, 51 insertions(+), 15 deletions(-) diff --git a/examples/launcher/game.lua b/examples/launcher/game.lua index 7e06cab..a19e302 100644 --- a/examples/launcher/game.lua +++ b/examples/launcher/game.lua @@ -143,8 +143,7 @@ local function loop(std, game) next_state(game, 6) elseif game._state == 6 then halt_state(game)(function() - game._app = load(game._source) - game._app = game._app() + game._app = std.game.load(game._source) game._app.callbacks.init(std, game) game._source = '' next_state(game, 7) @@ -240,7 +239,7 @@ local P = { version='1.0.0' }, config={ - require='http random math csv' + require='http random math csv load' }, callbacks={ init=init, diff --git a/src/engine/core/ginga/main.lua b/src/engine/core/ginga/main.lua index 790ede3..d9621f5 100644 --- a/src/engine/core/ginga/main.lua +++ b/src/engine/core/ginga/main.lua @@ -86,6 +86,7 @@ local function install(evt, gamefile) :package('@draw', engine_draw, ginga) :package('@draw_fps', engine_draw_fps) :package('@draw_poly', engine_draw_poly, polygons) + :package('load', zeebo_module.load) :package('csv', engine_csv) :package('math', engine_math.clib) :package('random', engine_math.clib_random) diff --git a/src/engine/core/html5/main.lua b/src/engine/core/html5/main.lua index 696dc2c..babee23 100644 --- a/src/engine/core/html5/main.lua +++ b/src/engine/core/html5/main.lua @@ -33,6 +33,7 @@ local function browser_init(width, height) :package('@game', engine_game) :package('@math', engine_math) :package('@color', engine_color) + :package('load', zeebo_module.load) :package('math', engine_math.clib) :package('random', engine_math.clib_random) :package('http', engine_http, browser_protocol_http) diff --git a/src/engine/core/love/main.lua b/src/engine/core/love/main.lua index 64b0881..fa025d6 100644 --- a/src/engine/core/love/main.lua +++ b/src/engine/core/love/main.lua @@ -43,6 +43,7 @@ function love.load(args) :package('@color', engine_color) :package('@draw_fps', engine_draw_fps) :package('@draw_poly', engine_draw_poly, polygons) + :package('load', zeebo_module.load) :package('math', engine_math.clib) :package('random', engine_math.clib_random) :package('http', engine_http, protocol_curl) diff --git a/src/engine/core/nintendo_wii/main.lua b/src/engine/core/nintendo_wii/main.lua index 936d453..ceaa9c9 100644 --- a/src/engine/core/nintendo_wii/main.lua +++ b/src/engine/core/nintendo_wii/main.lua @@ -32,6 +32,7 @@ function love.load(args) :package('@loop', engine_loop) :package('@color', engine_color) :package('@draw_poly', engine_draw_poly, polygons) + :package('load', zeebo_module.load) :package('math', engine_math.clib) :package('random', engine_math.clib_random) :run() diff --git a/src/lib/engine/module.lua b/src/lib/engine/module.lua index 75be78b..9ba6d89 100644 --- a/src/lib/engine/module.lua +++ b/src/lib/engine/module.lua @@ -5,21 +5,30 @@ local zeebo_pipeline = require('src/lib/util/pipeline') --! @li https://love2d.org/wiki/love.filesystem.getSource local function loadgame(game_file) local cwd = '.' - local application = nil + local application = type(game_file) == 'function' and game_file local game_title = game_file and game_file:gsub('%.lua$', '') or 'game' - - if love and love.filesystem and love.filesystem.getSource then - cwd = love.filesystem.getSource() - end - if loadfile then - application = loadfile(cwd..'/'..game_title..'.lua') - end - if not application and pcall and require then - local ok, app = pcall(require, game_title) + + + if not application and game_file and game_file:find('\n') then + local ok, app = pcall(load, game_file) + if not ok then + ok, app = pcall(loadstring, game_file) + end application = ok and app + else + if love and love.filesystem and love.filesystem.getSource then + cwd = love.filesystem.getSource() + end + if not application then + application = loadfile(cwd..'/'..game_title..'.lua') + end + if not application then + local ok, app = pcall(require, game_title) + application = ok and app + end end - if type(application) == 'function' then + while application and type(application) == 'function' do application = application() end @@ -114,7 +123,14 @@ local function require(std, game, application) return self end +local function install(std, game, application, exit_func) + std.game = std.game or {} + std.game.load = loadgame + return {load=loadgame} +end + local P = { + load={install=install}, loadgame = loadgame, require = require } diff --git a/src/lib/protocol/http_ginga.lua b/src/lib/protocol/http_ginga.lua index 9c1b410..e70a80a 100644 --- a/src/lib/protocol/http_ginga.lua +++ b/src/lib/protocol/http_ginga.lua @@ -155,7 +155,7 @@ local function http_handler(self) local protocol, location = self.url:match('(%w*)://?(.*)') local url, uri = (location or self.url):match('^([^/]+)(.*)$') local host, port_str = url:match("^(.-):?(%d*)$") - local port = tonumber(port_str and #port_str > 0 or 80) + local port = tonumber(port_str and #port_str > 0 and port_str or 80) self.p_url = url self.p_uri = uri or '/' @@ -191,6 +191,7 @@ end local function context_pull(evt, contexts) local host = evt.host local connection = evt.connection + local index = host and contexts.by_host[host] and #contexts.by_host[host] if evt.type == 'connect' and host and contexts.by_host[host] then local index = #contexts.by_host[host] @@ -204,6 +205,22 @@ local function context_pull(evt, contexts) contexts.by_connection[connection] = self contexts.by_host[host][index] = nil return self + elseif evt.type == 'disconnect' then + local self = {speed=''} + if host and index and contexts.by_host[host][index] then + self = contexts.by_host[host][index] + contexts.by_host[host][index] = nil + end + if connection and contexts.by_connection[connection] then + self = contexts.by_connection[connection] + contexts.by_connection[connection] = nil + end + if evt.error then + self.evt = {type = 'error', error = evt.error} + else + self.evt = evt + end + return self elseif connection and contexts.by_connection[connection] then local self = contexts.by_connection[connection] if evt.error then