COM_ImmedExecute for Lua

This commit is contained in:
James R 2020-10-20 19:13:41 -07:00
parent 1cd73315f1
commit 2c4809f76f
3 changed files with 40 additions and 18 deletions

View file

@ -262,20 +262,34 @@ void COM_BufExecute(void)
/** Executes a string immediately. Used for skirting around WAIT commands. /** Executes a string immediately. Used for skirting around WAIT commands.
*/ */
void COM_ImmedExecute(const char *ptext) void COM_ImmedExecuteEx(const char *ptext, int flags)
{ {
size_t i = 0, j = 0; size_t i = 0, j = 0;
char line[1024] = ""; char line[1024] = "";
char * text = NULL;
size_t length = strlen(ptext);
INT32 quotes; INT32 quotes;
while (i < strlen(ptext)) if (flags)
{
text = ZZ_Alloc( length += 2 );
text[0] = '\033';
text[1] = flags;
strcpy(&text[2], ptext);
ptext = text;
}
while (i < length)
{ {
quotes = 0; quotes = 0;
for (j = 0; i < strlen(ptext); i++,j++) for (j = 0; i < length; i++,j++)
{ {
if (ptext[i] == '\"' && !quotes && i > 0 && ptext[i-1] != ' ') // Malformed command if (ptext[i] == '\"' && !quotes && i > 0 && ptext[i-1] != ' ') // Malformed command
{
Z_Free(text);
return; return;
}
if (ptext[i] == '\"') if (ptext[i] == '\"')
quotes++; quotes++;
// don't break if inside a quoted string // don't break if inside a quoted string
@ -291,6 +305,8 @@ void COM_ImmedExecute(const char *ptext)
i++; // move to next character i++; // move to next character
} }
Z_Free(text);
} }
// ========================================================================= // =========================================================================

View file

@ -60,7 +60,8 @@ void COM_BufAddTextEx(const char *btext, int flags);
void COM_BufInsertTextEx(const char *btext, int flags); void COM_BufInsertTextEx(const char *btext, int flags);
// don't bother inserting, just do immediately // don't bother inserting, just do immediately
void COM_ImmedExecute(const char *ptext); #define COM_ImmedExecute(s) COM_ImmedExecuteEx(s, 0)
void COM_ImmedExecuteEx(const char *ptext, int flags);
// Execute commands in buffer, flush them // Execute commands in buffer, flush them
void COM_BufExecute(void); void COM_BufExecute(void);

View file

@ -233,38 +233,42 @@ static int lib_comAddCommand(lua_State *L)
return 0; return 0;
} }
static int lib_comBufAddText(lua_State *L) static int ValidateCommandBuffer(lua_State *L, const char * function)
{ {
int n = lua_gettop(L); /* number of arguments */ int n = lua_gettop(L); /* number of arguments */
player_t *plr = NULL; player_t *plr = NULL;
if (n < 2) if (n < 2)
return luaL_error(L, "COM_BufAddText requires two arguments: player and text."); return luaL_error(L, "%s requires two arguments: player and text.", function);
NOHUD NOHUD
lua_settop(L, 2); lua_settop(L, 2);
if (!lua_isnoneornil(L, 1)) if (!lua_isnoneornil(L, 1))
plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER));
if (plr && plr != &players[consoleplayer]) if (plr && plr != &players[consoleplayer])
return 0; return 0;
return 1;
}
static int lib_comBufAddText(lua_State *L)
{
if (ValidateCommandBuffer(L, "COM_BufAddText"))
COM_BufAddTextEx(va("%s\n", luaL_checkstring(L, 2)), COM_SAFE); COM_BufAddTextEx(va("%s\n", luaL_checkstring(L, 2)), COM_SAFE);
return 0; return 0;
} }
static int lib_comBufInsertText(lua_State *L) static int lib_comBufInsertText(lua_State *L)
{ {
int n = lua_gettop(L); /* number of arguments */ if (ValidateCommandBuffer(L, "COM_BufInsertText"))
player_t *plr = NULL;
if (n < 2)
return luaL_error(L, "COM_BufInsertText requires two arguments: player and text.");
NOHUD
lua_settop(L, 2);
if (!lua_isnoneornil(L, 1))
plr = *((player_t **)luaL_checkudata(L, 1, META_PLAYER));
if (plr && plr != &players[consoleplayer])
return 0;
COM_BufInsertTextEx(va("%s\n", luaL_checkstring(L, 2)), COM_SAFE); COM_BufInsertTextEx(va("%s\n", luaL_checkstring(L, 2)), COM_SAFE);
return 0; return 0;
} }
static int lib_comImmedExecute(lua_State *L)
{
if (ValidateCommandBuffer(L, "COM_ImmedExecute"))
COM_ImmedExecuteEx(luaL_checkstring(L, 2), COM_SAFE);
return 0;
}
void LUA_CVarChanged(const char *name) void LUA_CVarChanged(const char *name)
{ {
cvname = name; cvname = name;
@ -470,6 +474,7 @@ static luaL_Reg lib[] = {
{"COM_AddCommand", lib_comAddCommand}, {"COM_AddCommand", lib_comAddCommand},
{"COM_BufAddText", lib_comBufAddText}, {"COM_BufAddText", lib_comBufAddText},
{"COM_BufInsertText", lib_comBufInsertText}, {"COM_BufInsertText", lib_comBufInsertText},
{"COM_ImmedExecute", lib_comImmedExecute},
{"CV_RegisterVar", lib_cvRegisterVar}, {"CV_RegisterVar", lib_cvRegisterVar},
{"CV_FindVar", lib_cvFindVar}, {"CV_FindVar", lib_cvFindVar},
{"CONS_Printf", lib_consPrintf}, {"CONS_Printf", lib_consPrintf},