Lua: Further centralize code, fix Bizhawk 2.9 (#1685)

* Fixes the socket library for bizhawk 2.9/lua 5.4 by including another one in parallel
* Fixes lua 5.4 support by making socket.lua into a "modern" module (the `module` keyword is gone)
* Adds the linux version and 32 bit windows socket dlls because why not
* Merges common functions into `common.lua` - the only functional change of this should be that:
  * Some things that were locals are globals now - this can be changed, I just was lazy and it likely doesn't matter
  * `drawText` now uses middle/bottom for all prints - feel free to do what you like with that change
This commit is contained in:
zig-for
2023-04-15 00:17:33 -07:00
committed by GitHub
parent 808203a50f
commit 5a7d20d393
41 changed files with 255 additions and 2570 deletions

View File

@@ -10,8 +10,53 @@
local base = _G
local string = require("string")
local math = require("math")
local socket = require("socket.core")
module("socket")
function get_lua_version()
local major, minor = _VERSION:match("Lua (%d+)%.(%d+)")
assert(tonumber(major) == 5)
if tonumber(minor) >= 4 then
return "5-4"
end
return "5-1"
end
function get_os()
local the_os, ext, arch
if package.config:sub(1,1) == "\\" then
the_os, ext = "windows", "dll"
arch = os.getenv"PROCESSOR_ARCHITECTURE"
else
-- TODO: macos?
the_os, ext = "linux", "so"
arch = "x86_64" -- TODO: read ELF header from /proc/$PID/exe to get arch
end
if arch:find("64") ~= nil then
arch = "x64"
else
arch = "x86"
end
return the_os, ext, arch
end
function get_socket_path()
local the_os, ext, arch = get_os()
-- for some reason ./ isn't working, so use a horrible hack to get the pwd
local pwd = (io.popen and io.popen("cd"):read'*l') or "."
return pwd .. "/" .. arch .. "/socket-" .. the_os .. "-" .. get_lua_version() .. "." .. ext
end
local socket_path = get_socket_path()
local socket = assert(package.loadlib(socket_path, "luaopen_socket_core"))()
-- http://lua-users.org/wiki/ModulesTutorial
local M = {}
if setfenv then
setfenv(1, M) -- for 5.1
else
_ENV = M -- for 5.2
end
-----------------------------------------------------------------------------
-- Exported auxiliar functions
@@ -39,7 +84,7 @@ function bind(host, port, backlog)
return sock
end
try = newtry()
try = socket.newtry()
function choose(table)
return function(name, opt1, opt2)
@@ -130,3 +175,5 @@ end
sourcet["default"] = sourcet["until-closed"]
source = choose(sourcet)
return M