5using Lua =
struct lua_State;
6using RegisterWithLuaFn = void (*)(
struct lua_State*);
14#include "RageUtil/Utils/RageUtil.h"
15#include "arch/Dialog/Dialog.h"
21 static void Register(RegisterWithLuaFn pfn);
27 void Release(Lua*& p);
38 void SetGlobal(
const std::string& sName,
int val);
39 void SetGlobal(
const std::string& sName,
float val);
40 void SetGlobal(
const std::string& sName,
const std::string& val);
41 void UnsetGlobal(
const std::string& sName);
44 lua_State* m_pLuaMain;
59 const std::string& sScript,
60 const std::string& sName,
61 std::string& sError) -> bool;
69ReportScriptError(std::string
const& Error,
70 std::string ErrorType =
"LUA_ERROR",
71 bool UseAbort =
false) -> Dialog::Result;
75ScriptErrorMessage(std::string
const& Error);
78template<
typename... Args>
80ReportScriptErrorFmt(std::string
const& msg, Args
const&... args)
82 std::string result = ssprintf(msg.c_str(), args...);
83 ReportScriptError(result);
95RunScriptOnStack(Lua* L,
99 bool ReportError =
false,
100 bool blank_env =
false) -> bool;
106 const std::string& Script,
107 const std::string& Name,
110 int ReturnValues = 0,
111 bool ReportError =
false,
112 bool blank_env =
false) -> bool;
118 const std::string& sExpression,
119 const std::string& sName =
"",
120 bool blank_env =
false) -> bool;
123RunScriptFile(
const std::string& sFile,
bool blank_env =
false) -> bool;
126run_script_file_in_state(lua_State* L,
127 std::string
const& filename,
129 bool blank_env) -> bool;
131string_can_be_lua_identifier(lua_State* L, std::string
const& str) -> bool;
133push_lua_escaped_string(lua_State* L, std::string
const& str);
137save_lua_table_to_file(lua_State* L,
139 std::string
const& filename);
144CreateTableFromArrayB(Lua* L,
const std::vector<bool>& aIn);
149CreateTableFromXNode(Lua* L,
const XNode* pNode);
154DeepCopy(lua_State* L);
158ReadArrayFromTableB(Lua* L, std::vector<bool>& aOut);
161rec_print_table(lua_State* L,
162 std::string
const& name,
163 std::string
const& indent);
166ParseCommandList(lua_State* L,
167 const std::string& sCommands,
168 const std::string& sName,
172GetLuaInformation() ->
XNode*;
177PushValueFunc(lua_State* L,
int iArgs);
181Push(lua_State* L,
const T& Object);
185FromStack(lua_State* L, T& Object,
int iOffset) -> bool;
189FromStack(lua_State* L,
char const* Object,
int iOffset) -> bool;
193Pop(lua_State* L, T& val) ->
bool
195 bool bRet = LuaHelpers::FromStack(L, val, -1);
202ReadArrayFromTable(std::vector<T>& aOut, lua_State* L)
204 luaL_checktype(L, -1, LUA_TTABLE);
207 while (lua_rawgeti(L, -1, ++i), !lua_isnil(L, -1)) {
209 LuaHelpers::Pop(L, value);
210 aOut.push_back(value);
216CreateTableFromArray(
const std::vector<T>& aIn, lua_State* L)
219 for (
unsigned i = 0; i < aIn.size(); ++i) {
220 LuaHelpers::Push(L, aIn[i]);
221 lua_rawseti(L, -2, i + 1);
226TypeError(Lua* L,
int narg, std::string
const& tname) -> int;
228AbsIndex(Lua* L,
int i) ->
int
230 if (i > 0 || i <= LUA_REGISTRYINDEX) {
233 return lua_gettop(L) + i + 1;
244 static void GetThreadVariable(lua_State* L);
249 void SetFromStack(lua_State* L);
250 auto AdjustCount(lua_State* L,
int iAdd) -> int;
251 static auto PushThreadTable(lua_State* L,
bool bCreate) -> bool;
252 static auto GetCurrentThreadIDString() -> std::string;
270#define FOREACH_LUATABLE(L, index) \
272 for (const int SM_UNIQUE_NAME(tab) = LuaHelpers::AbsIndex(L, index), \
273 SM_UNIQUE_NAME(top) = (lua_pushnil(L), lua_gettop(L)); \
274 lua_next(L, SM_UNIQUE_NAME(tab)) && (lua_pushvalue(L, -2), true); \
275 lua_settop(L, SM_UNIQUE_NAME(top)))
278#define FOREACH_LUATABLEI(L, index, i) \
279 for (int SM_UNIQUE_NAME(tab) = LuaHelpers::AbsIndex(L, index), \
280 SM_UNIQUE_NAME(top) = lua_gettop(L), \
282 lua_rawgeti(L, SM_UNIQUE_NAME(tab), i), \
284 ? (lua_pop(L, 1), false) \
286 lua_settop(L, SM_UNIQUE_NAME(top)), ++(i))
292#define REGISTER_WITH_LUA_FUNCTION(Fn) \
293 static RegisterLuaFunction register##Fn(Fn);
296MyLua_checkboolean(lua_State* L,
int numArg) ->
bool
298 luaL_checktype(L, numArg, LUA_TBOOLEAN);
299 return !(lua_toboolean(L, numArg) == 0);
306MyLua_checkintboolean(lua_State* L,
int iArg) ->
bool
308 luaL_checkany(L, iArg);
309 int iType = lua_type(L, iArg);
310 if (iType == LUA_TNUMBER) {
311 int iValue =
static_cast<int>(lua_tointeger(L, iArg));
315 return MyLua_checkboolean(L, iArg);
320TableContainsOnlyStrings(lua_State* L,
int index) ->
bool
324 while (lua_next(L, index) != 0) {
326 const char* pValue = lua_tostring(L, -1);
327 if (pValue ==
nullptr) {
339#define SArg(n) (luaL_checkstring(L, (n)))
340#define BIArg(n) (MyLua_checkintboolean(L, (n)))
341#define IArg(n) (luaL_checkint(L, (n)))
342#define BArg(n) (MyLua_checkboolean(L, (n)))
343#define FArg(n) (static_cast<float>(luaL_checknumber(L, (n))))
349SafeFArg(lua_State* L,
int index, std::string
const& err,
int def) ->
int
351 if (lua_isnumber(L, index) != 0) {
352 return static_cast<int>(lua_tonumber(L, index));
354 LuaHelpers::ReportScriptError(err);
359get_optional_double(lua_State* L,
int index,
char const* field,
double def)
363 lua_getfield(L, index, field);
364 if (lua_isnumber(L, -1) != 0) {
365 ret =
static_cast<double>(lua_tonumber(L, -1));
372get_optional_int(lua_State* L,
int index,
char const* field,
int def) ->
int
375 lua_getfield(L, index, field);
376 if (lua_isnumber(L, -1) != 0) {
377 ret =
static_cast<int>(lua_tonumber(L, -1));
384get_optional_bool(lua_State* L,
int index,
char const* field) ->
bool
386 lua_getfield(L, index, field);
387 bool ret = lua_toboolean(L, -1) == 1;
393value_is_in_table(lua_State* L,
int value_index,
int table_index) ->
bool
396 while (lua_next(L, table_index) != 0) {
397 if (lua_equal(L, value_index, -1) != 0) {
406#define LuaFunction(func, expr) \
408 int LuaFunc_##func(lua_State* L); \
410 int LuaFunc_##func(lua_State* L) \
412 LuaHelpers::Push(L, expr); \
416 void LuaFunc_Register_##func(lua_State* L); \
418 void LuaFunc_Register_##func(lua_State* L) \
420 lua_register(L, #func, LuaFunc_##func); \
423 REGISTER_WITH_LUA_FUNCTION(LuaFunc_Register_##func);
425#define LUAFUNC_REGISTER_COMMON(func_name) \
427 void LuaFunc_Register_##func_name(lua_State* L); \
429 void LuaFunc_Register_##func_name(lua_State* L) \
431 lua_register(L, #func_name, LuaFunc_##func_name); \
434 REGISTER_WITH_LUA_FUNCTION(LuaFunc_Register_##func_name);
Definition LuaManager.h:18
A self-cleaning Lua reference.
Definition LuaReference.h:10
Definition LuaManager.h:238
Definition RageThreads.h:232
Utilities for working with Lua.
Definition LuaReference.cpp:172
Definition LuaManager.h:289