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

102
data/lua/common.lua Normal file
View File

@@ -0,0 +1,102 @@
local lua_major, lua_minor = _VERSION:match("Lua (%d+)%.(%d+)")
lua_major = tonumber(lua_major)
lua_minor = tonumber(lua_minor)
-- lua compat shims
if lua_major > 5 or (lua_major == 5 and lua_minor >= 3) then
require("lua_5_3_compat")
end
function table.empty (self)
for _, _ in pairs(self) do
return false
end
return true
end
local bizhawk_version = client.getversion()
local bizhawk_major, bizhawk_minor, bizhawk_patch = bizhawk_version:match("(%d+)%.(%d+)%.?(%d*)")
bizhawk_major = tonumber(bizhawk_major)
bizhawk_minor = tonumber(bizhawk_minor)
if bizhawk_patch == "" then
bizhawk_patch = 0
else
bizhawk_patch = tonumber(bizhawk_patch)
end
local is23Or24Or25 = (bizhawk_version=="2.3.1") or (bizhawk_major == 2 and bizhawk_minor >= 3 and bizhawk_minor <= 5)
local isGreaterOrEqualTo26 = bizhawk_major > 2 or (bizhawk_major == 2 and bizhawk_minor >= 6)
local isUntestedBizhawk = bizhawk_major > 2 or (bizhawk_major == 2 and bizhawk_minor > 9)
local untestedBizhawkMessage = "Warning: this version of bizhawk is newer than we know about. If it doesn't work, consider downgrading to 2.9"
u8 = memory.read_u8
wU8 = memory.write_u8
u16 = memory.read_u16_le
function getMaxMessageLength()
local denominator = 12
if is23Or24Or25 then
denominator = 11
end
return math.floor(client.screenwidth()/denominator)
end
function drawText(x, y, message, color)
if is23Or24Or25 then
gui.addmessage(message)
elseif isGreaterOrEqualTo26 then
gui.drawText(x, y, message, color, 0xB0000000, 18, "Courier New", "middle", "bottom", nil, "client")
end
end
function clearScreen()
if is23Or24Or25 then
return
elseif isGreaterOrEqualTo26 then
drawText(0, 0, "", "black")
end
end
itemMessages = {}
function drawMessages()
if table.empty(itemMessages) then
clearScreen()
return
end
local y = 10
found = false
maxMessageLength = getMaxMessageLength()
for k, v in pairs(itemMessages) do
if v["TTL"] > 0 then
message = v["message"]
while true do
drawText(5, y, message:sub(1, maxMessageLength), v["color"])
y = y + 16
message = message:sub(maxMessageLength + 1, message:len())
if message:len() == 0 then
break
end
end
newTTL = 0
if isGreaterOrEqualTo26 then
newTTL = itemMessages[k]["TTL"] - 1
end
itemMessages[k]["TTL"] = newTTL
found = true
end
end
if found == false then
clearScreen()
end
end
function checkBizhawkVersion()
if not is23Or24Or25 and not isGreaterOrEqualTo26 then
print("Must use a version of bizhawk 2.3.1 or higher")
return false
elseif isUntestedBizhawk then
print(untestedBizhawkMessage)
end
return true
end