Well I've got command hook setup inside of interpret(), and all is well. I am using stat() and S_ISDIR() to verify the file exists and is not a directory, then interpret() calls my function lua_command().
Relevant parts from my lua.c file:
extern "C" int luaCHSend(lua_State * L);
static const luaL_reg luaMudlib[] =
{
{"send_to_char", luaCHSend},
{NULL, NULL}
};
extern "C" int luaRegisterMudlib(lua_State * L)
{
int i = 0;
while (luaMudlib[i].name != NULL && luaMudlib[i].func != NULL)
{
lua_register(L, luaMudlib[i].name, luaMudlib[i].func);
i++;
}
return RET_OK;
}
int luaOpen()
{
// Create a new Lua state.
g_luaState = luaL_newstate();
if (!g_luaState)
{
log_lua("luaOpen: Failed to create lua_State!");
return RET_ERR;
}
// Load standard libraries.
luaL_openlibs(g_luaState);
// Load mud library.
lua_pushcfunction(g_luaState, luaRegisterMudlib);
lua_call(g_luaState, 0, 0);
// None of these methods work?
// luaRegisterMudlib(g_luaState);
// luaL_register(g_luaState, "mud", luaMudlib);
// Load mud interfaces.
// luaRegisterCH(g_luaState);
// luaRegisterRoom(g_luaState);
// luaRegisterObj(g_luaState);
lua_settop(g_luaState, 0);
return RET_OK;
}
void lua_command(CHAR_DATA * ch, const char * command, const char * argument)
{
char path[MAX_INPUT_LENGTH];
char func[MAX_INPUT_LENGTH];
int err;
if (!g_luaState)
{
log_lua("lua_command: FATAL error: g_luaState == NULL!");
return;
}
snprintf(path, MAX_INPUT_LENGTH, "%sdo_%s.lua", LUA_CMD_DIR, command);
if ((err = luaL_loadfile(g_luaState, path)) != 0)
{
log_lua("lua_command: luaL_loadfile() error %d: %s", err, lua_tostring(g_luaState, -1));
return;
}
snprintf(func, MAX_INPUT_LENGTH, "do_%s", command);
lua_getglobal(g_luaState, func); // Get name of Lua func
lua_pushlightuserdata(g_luaState, (void*)ch->GetId().Value()); // Push character ID
lua_pushstring(g_luaState, argument); // Push argument
if ((err = lua_pcall(g_luaState, 3, 0, 0)) != 0)
{
log_lua("lua_command: lua_pcall() error: %d: %s", err, lua_tostring(g_luaState, -1));
return;
}
return;
}
And lua_funs.c:
extern "C" int luaCHSend(lua_State * L)
{
CHAR_DATA * ch;
const char * arg;
char buf[MAX_STRING_LENGTH];
uint64 id;
bug("luaCHSend"); // Just to see if we ever make it here, which we aren't...
if (!L)
{
log_lua("luaCHSend: FATAL error: L == NULL");
return 0;
}
if (lua_gettop(L) != 2)
{
log_lua("luaCHSend: Invalid number of arguments.");
luaL_error(L, "luaCHSend: Invalid number of arguments.");
return 0;
}
id = (uint64) lua_touserdata(L, -2);
ch = char_map[id];
if (ch == NULL)
{
log_lua("luaCHSend: NULL ch.");
luaL_error(L, "luaCHSend: NULL ch.");
return 0;
}
if (IS_NPC(ch))
return 0;
arg = lua_tostring(L, -1);
if (arg == NULL)
{
log_lua("luaCHSend: NULL argument.");
luaL_error(L, "luaCHSend: NULL argument.");
return 0;
}
snprintf(buf, MAX_STRING_LENGTH, "%s\n\r", arg);
send_to_char(buf, ch);
return 0;
}
Continued in next post... |