Skip to content

Commit

Permalink
Bot V8 basic fixes
Browse files Browse the repository at this point in the history
Adapt load() calls to lua 5.1
  • Loading branch information
IvanClementino committed Oct 30, 2024
1 parent af77e53 commit 5d62b82
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,11 @@ CaveBot.registerAction("function", "red", function(value, retries, prev)
prefix = prefix .. "local " .. extension .. " = CaveBot.Extensions." .. extension .. "\n"
end
local status, result = pcall(function()
return assert(load(prefix .. value, "cavebot_function"))()
if _VERSION == "Lua 5.1" and type(jit) ~= "table" then
return assert(loadstring(prefix .. value))()
else
return assert(load(prefix .. value, "cavebot_function"))()
end
end)
if not status then
error("Error in cavebot function:\n" .. result)
Expand Down
8 changes: 6 additions & 2 deletions mods/game_bot/default_configs/cavebot_1.3/tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ end)
UI.Separator()

for _, scripts in ipairs({storage.ingame_macros, storage.ingame_hotkeys}) do
if type(scripts) == "string" and scripts:len() > 3 then
if type(scripts) == "string" and scripts:len() > 3 then
local status, result = pcall(function()
assert(load(scripts, "ingame_editor"))()
if _VERSION == "Lua 5.1" and type(jit) ~= "table" then
assert(loadstring(scripts))()
else
assert(load(scripts, "ingame_editor"))()
end
end)
if not status then
error("Ingame edior error:\n" .. result)
Expand Down
6 changes: 5 additions & 1 deletion mods/game_bot/default_configs/vBot_4.8/cavebot/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ CaveBot.registerAction("function", "red", function(value, retries, prev)
prefix = prefix .. "local " .. extension .. " = CaveBot.Extensions." .. extension .. "\n"
end
local status, result = pcall(function()
return assert(load(prefix .. value, "cavebot_function"))()
if _VERSION == "Lua 5.1" and type(jit) ~= "table" then
return assert(loadstring(prefix .. value))()
else
return assert(load(prefix .. value, "cavebot_function"))()
end
end)
if not status then
warn("warn in cavebot function:\n" .. result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ UI.Button("Ingame script editor", function(newText)
for _, scripts in pairs({storage.ingame_hotkeys}) do
if type(scripts) == "string" and scripts:len() > 3 then
local status, result = pcall(function()
assert(load(scripts, "ingame_editor"))()
if _VERSION == "Lua 5.1" and type(jit) ~= "table" then
return assert(loadstring(scripts))()
else
assert(load(scripts, "ingame_editor"))()
end
end)
if not status then
error("Ingame edior error:\n" .. result)
Expand Down
25 changes: 22 additions & 3 deletions mods/game_bot/executor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,23 @@ function executeBot(config, storage, tabs, msgCallback, saveConfigCallback, relo
date = os.date,
clock = os.clock
}
context.load = function(str) return assert(load(str, nil, nil, context)) end
if _VERSION == "Lua 5.1" and type(jit) ~= "table" then
context.load = function(str)
local func = assert(loadstring(str))
setfenv(func, context)
return func
end
context.dofile = function(file)
local func = assert(loadstring(g_resources.readFileContents("/bot/" .. config .. "/" .. file)))
setfenv(func, context)
func()
end
else
context.load = function(str) return assert(load(str, nil, nil, context)) end
context.dofile = function(file) assert(load(g_resources.readFileContents("/bot/" .. config .. "/" .. file), file, nil, context))() end
end
context.loadstring = context.load
context.assert = assert
context.dofile = function(file) assert(load(g_resources.readFileContents("/bot/" .. config .. "/" .. file), file, nil, context))() end
context.gcinfo = gcinfo
context.tr = tr
context.json = json
Expand Down Expand Up @@ -164,7 +177,13 @@ function executeBot(config, storage, tabs, msgCallback, saveConfigCallback, relo

-- run lua script
for i, file in ipairs(luaFiles) do
assert(load(g_resources.readFileContents(file), file, nil, context))()
if _VERSION == "Lua 5.1" and type(jit) ~= "table" then
local func = assert(loadstring(g_resources.readFileContents(file)))
setfenv(func, context)
func()
else
assert(load(g_resources.readFileContents(file), file, nil, context))()
end
context.panel = context.mainTab -- reset default tab
end

Expand Down
17 changes: 14 additions & 3 deletions mods/game_bot/functions/script_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ context.loadScript = function(path, onLoadCallback)
if not g_resources.fileExists(path) then
return context.error("File " .. path .. " doesn't exist")
end

local status, result = pcall(function()
assert(load(g_resources.readFileContents(path), path, nil, context))()
if _VERSION == "Lua 5.1" and type(jit) ~= "table" then
local func = assert(loadstring(g_resources.readFileContents(path)))
setfenv(func, context)
func()
else
assert(load(g_resources.readFileContents(path), path, nil, context))()
end
end)
if not status then
return context.error("Error while loading script from: " .. path .. ":\n" .. result)
Expand Down Expand Up @@ -42,7 +47,13 @@ context.loadRemoteScript = function(url, onLoadCallback)
end

local status, result = pcall(function()
assert(load(data, url, nil, context))()
if _VERSION == "Lua 5.1" and type(jit) ~= "table" then
local func = assert(loadstring(data))
setfenv(func, context)
func()
else
assert(load(data, url, nil, context))()
end
end)
if not status then
return context.error("Error while loading script from: " .. url .. ":\n" .. result)
Expand Down
8 changes: 7 additions & 1 deletion mods/game_bot/panels/waypoints.lua
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,13 @@ Panel
elseif command.command == "function" and lastGotoSuccesful then
usedGotoLabel = false
local status, result = pcall(function()
return assert(load("return " .. command.text, nil, nil, context))()(functions)
if _VERSION == "Lua 5.1" and type(jit) ~= "table" then
local func = assert(loadstring("return " .. command.text))
setfenv(func, context)
return func()(functions)
else
return assert(load("return " .. command.text, nil, nil, context))()(functions)
end
end)
if not status then
context.error("Waypoints function execution error:\n" .. result)
Expand Down

0 comments on commit 5d62b82

Please sign in to comment.