Skip to content

Commit

Permalink
fix: issue with missing avant object in Lua script
Browse files Browse the repository at this point in the history
Added a pre-mount execution to ensure that the `avant` object is correctly initialized before the Lua code runs. This prevents errors related to the `avant` object not being found.
  • Loading branch information
gaowanlu committed Aug 1, 2024
1 parent 4e4023d commit b1a7f22
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
34 changes: 16 additions & 18 deletions bin/lua/init.lua → bin/lua/Init.lua
Original file line number Diff line number Diff line change
@@ -1,72 +1,70 @@
package.path = "./lua/?.lua;" .. package.path
require("log")
local Log = require("Log")

function OnMainInit(isHot)
Log("OnMainInit isHot %s", tostring(isHot));
Log:Error("OnMainInit isHot %s", tostring(isHot));
end

function OnMainStop(isHot)
local log = "OnMainStop isHot " .. tostring(isHot);
Log(log);
Log:Error(log);
end

function OnMainTick()
-- local log = "OnMainTick";
-- Log(log);
-- Log:Error(Log.data);
end

function OnWorkerInit(workerIdx, isHot)
local log = "OnWorkerInit workerIdx " .. workerIdx .. " isHot " ..
tostring(isHot);
Log(log)
local log = "OnWorkerInit workerIdx " .. workerIdx .. " isHot " .. tostring(isHot);
Log:Error(log)
end

function OnWorkerStop(workerIdx, isHot)
local log = "OnWorkerStop" .. workerIdx .. " isHot " .. tostring(isHot);
Log(log)
Log:Error(log)
end

function OnWorkerTick(workerIdx)
-- Log("OnWorkerTick " .. workerIdx)
-- Log:Error("OnWorkerTick " .. workerIdx)
-- local t = {
-- ["num"] = 2,
-- ["int32array"] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
-- ["data"] = {["str"] = "hello world"}
-- };
-- local res = avant.Lua2Protobuf(t, 8);
-- if res == nil then
-- Log("avant.Lua2Protobuf res nil " .. workerIdx);
-- Log:Error("avant.Lua2Protobuf res nil " .. workerIdx);
-- else
-- Log("avant.Lua2Protobuf res " .. workerIdx .. " " .. res);
-- Log:Error("avant.Lua2Protobuf res " .. workerIdx .. " " .. res);
-- end
end

function OnWorkerRecvMessage(workerIdx, cmd, message)
Log("avant.OnWorkerRecvMessage " .. workerIdx .. " " .. cmd .. " ");
Log:Error("avant.OnWorkerRecvMessage " .. workerIdx .. " " .. cmd .. " ");
if cmd == 8 then
local num = message["num"];
local int32array = message["int32array"];
local int32array_str = table.concat(int32array, " ")
local data = message["data"];
local data_str = data["str"];
Log("avant.OnWorkerRecvMessage num " .. num .. " " .. "int32array " ..
int32array_str .. " " .. data_str);
Log:Error("avant.OnWorkerRecvMessage num " .. num .. " " .. "int32array " .. int32array_str .. " " .. data_str);
else
Log("avant.OnWorkerRecvMessage cmd != 8");
Log:Error("avant.OnWorkerRecvMessage cmd != 8");
end
end

function OnOtherInit(isHot)
local log = "OnOtherInit isHot " .. tostring(isHot);
Log(log);
Log:Error(log);
end

function OnOtherStop(isHot)
local log = "OnOtherStop isHot " .. tostring(isHot);
Log(log);
Log:Error(log);
end

function OnOtherTick()
-- local log = "OnOtherTick";
-- Log(log)
-- Log:Error(log)
end
6 changes: 5 additions & 1 deletion bin/lua/log.lua → bin/lua/Log.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
function Log(...)
local Log = {}

function Log:Error(...)
local args = {...};
local formatString = table.remove(args, 1);

Expand All @@ -10,3 +12,5 @@ function Log(...)

avant.Logger(string.format("[%s:%d] %s", source, line, message));
end

return Log
24 changes: 12 additions & 12 deletions src/app/lua_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,20 @@ void lua_plugin::real_on_main_init(bool is_hot)
{
this->lua_state = luaL_newstate();
luaL_openlibs(this->lua_state);
std::string filename = this->lua_dir + "/init.lua";
main_mount();
std::string filename = this->lua_dir + "/Init.lua";
int isok = luaL_dofile(this->lua_state, filename.data());

if (isok == LUA_OK)
{
LOG_ERROR("main init.lua load succ");
LOG_ERROR("main Init.lua load succ");
}
else
{
LOG_ERROR("main init.lua load failed, %s", lua_tostring(this->lua_state, -1));
LOG_ERROR("main Init.lua load failed, %s", lua_tostring(this->lua_state, -1));
exit(-1);
}
}
main_mount();
exe_OnMainInit(is_hot);
}

Expand Down Expand Up @@ -148,19 +148,19 @@ void lua_plugin::on_worker_init(int worker_idx, bool is_hot)
{
this->worker_lua_state[worker_idx] = luaL_newstate();
luaL_openlibs(this->worker_lua_state[worker_idx]);
std::string filename = this->lua_dir + "/init.lua";
worker_mount(worker_idx);
std::string filename = this->lua_dir + "/Init.lua";
int isok = luaL_dofile(this->worker_lua_state[worker_idx], filename.data());
if (isok == LUA_OK)
{
LOG_ERROR("worker vm[%d] init.lua load succ", worker_idx);
LOG_ERROR("worker vm[%d] Init.lua load succ", worker_idx);
}
else
{
LOG_ERROR("main worker vm[%d] init.lua load failed, %s", worker_idx, lua_tostring(this->worker_lua_state[worker_idx], -1));
LOG_ERROR("main worker vm[%d] Init.lua load failed, %s", worker_idx, lua_tostring(this->worker_lua_state[worker_idx], -1));
exit(-1);
}
}
worker_mount(worker_idx);
exe_OnWorkerInit(worker_idx, is_hot);
}

Expand Down Expand Up @@ -298,20 +298,20 @@ void lua_plugin::on_other_init(bool is_hot)
{
this->other_lua_state = luaL_newstate();
luaL_openlibs(this->other_lua_state);
std::string filename = this->lua_dir + "/init.lua";
other_mount();
std::string filename = this->lua_dir + "/Init.lua";
int isok = luaL_dofile(this->other_lua_state, filename.data());

if (isok == LUA_OK)
{
LOG_ERROR("other init.lua load succ");
LOG_ERROR("other Init.lua load succ");
}
else
{
LOG_ERROR("other init.lua load failed, %s", lua_tostring(this->other_lua_state, -1));
LOG_ERROR("other Init.lua load failed, %s", lua_tostring(this->other_lua_state, -1));
exit(-1);
}
}
other_mount();
exe_OnOtherInit(is_hot);
}

Expand Down

0 comments on commit b1a7f22

Please sign in to comment.