diff options
author | aunsane <aunsane@gmail.com> | 2018-07-29 22:59:30 +0300 |
---|---|---|
committer | aunsane <aunsane@gmail.com> | 2018-07-29 23:00:42 +0300 |
commit | d11ddc8ef4b7f55c53abb735bca00e4192533579 (patch) | |
tree | 97f3aa2770fc4a10a024b505ed845d954aea4e97 /plugins | |
parent | c085ec9744d1012627970e8590939ce661042380 (diff) |
MirLua:
- added script table headers
- added some logic functions to variables parser
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/MirLua/res/resource.rc | 2 | ||||
-rw-r--r-- | plugins/MirLua/src/environment.cpp | 21 | ||||
-rw-r--r-- | plugins/MirLua/src/environment.h | 6 | ||||
-rw-r--r-- | plugins/MirLua/src/options.cpp | 12 | ||||
-rw-r--r-- | plugins/MirLua/src/plugin.cpp | 12 | ||||
-rw-r--r-- | plugins/MirLua/src/utils.cpp | 4 | ||||
-rw-r--r-- | plugins/MirLua/src/variables_loader.cpp | 188 |
7 files changed, 184 insertions, 61 deletions
diff --git a/plugins/MirLua/res/resource.rc b/plugins/MirLua/res/resource.rc index 3c6008c4ff..1efb61e7a4 100644 --- a/plugins/MirLua/res/resource.rc +++ b/plugins/MirLua/res/resource.rc @@ -85,7 +85,7 @@ STYLE DS_SETFONT | DS_FIXEDSYS | WS_CHILD EXSTYLE WS_EX_CONTROLPARENT
FONT 8, "MS Shell Dlg", 0, 0, 0x1
BEGIN
- CONTROL "",IDC_SCRIPTS,"SysListView32",LVS_REPORT | LVS_SINGLESEL | LVS_ALIGNLEFT | LVS_NOCOLUMNHEADER | WS_BORDER | WS_TABSTOP,14,66,282,137
+ CONTROL "",IDC_SCRIPTS,"SysListView32",LVS_REPORT | LVS_SINGLESEL | LVS_ALIGNLEFT | LVS_NOSORTHEADER | WS_BORDER | WS_TABSTOP,14,66,282,137
PUSHBUTTON "Reload all",IDC_RELOAD,220,207,76,14
GROUPBOX "Scripts",IDC_STATIC,7,54,296,173
CONTROL "Popup on script error",IDC_POPUPONERROR,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,14,17,282,10
diff --git a/plugins/MirLua/src/environment.cpp b/plugins/MirLua/src/environment.cpp index 517cff243b..24dea9ec58 100644 --- a/plugins/MirLua/src/environment.cpp +++ b/plugins/MirLua/src/environment.cpp @@ -154,27 +154,22 @@ wchar_t* CMLuaEnvironment::Error() return error; } -wchar_t* CMLuaEnvironment::Call() +int CMLuaEnvironment::Call() { CreateEnvironmentTable(); - luaM_pcall(L, 0, 1); - - wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1)); - lua_pop(L, 1); - - return result; + return lua_pcall(L, 0, 1, 0); } -wchar_t* CMLuaEnvironment::Eval(const wchar_t *script) +int CMLuaEnvironment::Eval(const wchar_t *script) { - if (luaL_loadstring(L, T2Utf(script))) - return Error(); + if (int res = luaL_loadstring(L, T2Utf(script))) + return res; return Call(); } -wchar_t* CMLuaEnvironment::Exec(const wchar_t *path) +int CMLuaEnvironment::Exec(const wchar_t *path) { - if (luaL_loadfile(L, T2Utf(path))) - return Error(); + if (int res = luaL_loadfile(L, T2Utf(path))) + return res; return Call(); } diff --git a/plugins/MirLua/src/environment.h b/plugins/MirLua/src/environment.h index 52b42e7722..4693376ebe 100644 --- a/plugins/MirLua/src/environment.h +++ b/plugins/MirLua/src/environment.h @@ -29,7 +29,7 @@ public: HANDLE CreateServiceFunction(const char *name, int ref); void DestroyServiceFunction(HANDLE hService); - wchar_t* Call(); - wchar_t* Eval(const wchar_t *script); - wchar_t* Exec(const wchar_t *path); + int Call(); + int Eval(const wchar_t *script); + int Exec(const wchar_t *path); }; diff --git a/plugins/MirLua/src/options.cpp b/plugins/MirLua/src/options.cpp index bfdacc6e6a..83a8b9d7c8 100644 --- a/plugins/MirLua/src/options.cpp +++ b/plugins/MirLua/src/options.cpp @@ -65,10 +65,10 @@ bool CMLuaOptionsMain::OnInitDialog() wchar_t header[MAX_PATH + 100];
mir_snwprintf(header, L"%s (%s)", TranslateT("Common scripts"), relativeScriptDir);
- m_scriptsList.AddColumn(0, L"Script", 370);
- m_scriptsList.AddColumn(1, nullptr, 32 - GetSystemMetrics(SM_CXVSCROLL));
- m_scriptsList.AddColumn(2, nullptr, 32 - GetSystemMetrics(SM_CXVSCROLL));
- m_scriptsList.AddColumn(3, nullptr, 32 - GetSystemMetrics(SM_CXVSCROLL));
+ m_scriptsList.AddColumn(0, L"Script", 346);
+ m_scriptsList.AddColumn(1, nullptr, 34 - GetSystemMetrics(SM_CXVSCROLL));
+ m_scriptsList.AddColumn(2, nullptr, 36 - GetSystemMetrics(SM_CXVSCROLL));
+ m_scriptsList.AddColumn(3, nullptr, 36 - GetSystemMetrics(SM_CXVSCROLL));
LoadScripts();
@@ -216,7 +216,9 @@ void CMLuaEvaluateOptions::OnEvaluate(CCtrlBase*) ptrW script(m_script.GetText());
CMLuaEnvironment env(L);
- m_result.SetText(env.Eval(script));
+ env.Eval(script);
+ m_result.SetText(ptrW(mir_utf8decodeW(lua_tostring(L, -1))));
+ lua_pop(L, 1);
env.Unload();
}
diff --git a/plugins/MirLua/src/plugin.cpp b/plugins/MirLua/src/plugin.cpp index bf2c47ced4..ceefe7d7b0 100644 --- a/plugins/MirLua/src/plugin.cpp +++ b/plugins/MirLua/src/plugin.cpp @@ -172,7 +172,9 @@ INT_PTR CMPlugin::Call(WPARAM wParam, LPARAM lParam) lua_pushcfunction(L, mlua_call); CMLuaEnvironment env(L); - wchar_t *result = env.Call(); + env.Call(); + wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1)); + lua_pop(L, 1); env.Unload(); return (INT_PTR)result; @@ -183,7 +185,9 @@ INT_PTR CMPlugin::Eval(WPARAM, LPARAM lParam) const wchar_t *script = (const wchar_t*)lParam; CMLuaEnvironment env(L); - wchar_t *result = env.Eval(script); + env.Eval(script); + wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1)); + lua_pop(L, 1); env.Unload(); return (INT_PTR)result; @@ -194,7 +198,9 @@ INT_PTR CMPlugin::Exec(WPARAM, LPARAM lParam) const wchar_t *path = (const wchar_t*)lParam; CMLuaEnvironment env(L); - wchar_t *result = env.Exec(path); + env.Exec(path); + wchar_t *result = mir_utf8decodeW(lua_tostring(L, -1)); + lua_pop(L, 1); env.Unload(); return (INT_PTR)result; diff --git a/plugins/MirLua/src/utils.cpp b/plugins/MirLua/src/utils.cpp index cc75303af7..d4a4578cd9 100644 --- a/plugins/MirLua/src/utils.cpp +++ b/plugins/MirLua/src/utils.cpp @@ -91,8 +91,8 @@ int luaM_getenv(lua_State *L) bool luaM_toboolean(lua_State *L, int idx) { - if (lua_isnumber(L, idx)) - return lua_tonumber(L, idx) > 0; + if (lua_isinteger(L, idx)) + return lua_tointeger(L, idx) > 0; return lua_toboolean(L, idx) > 0; } diff --git a/plugins/MirLua/src/variables_loader.cpp b/plugins/MirLua/src/variables_loader.cpp index 4fdd5975b2..b6a71d5f9f 100644 --- a/plugins/MirLua/src/variables_loader.cpp +++ b/plugins/MirLua/src/variables_loader.cpp @@ -6,13 +6,66 @@ CMLuaVariablesLoader::CMLuaVariablesLoader(lua_State *L) : L(L) /***********************************************/ -static int mlua__add(lua_State *L) +static int mlua__and(lua_State *L) { + int nargs = lua_gettop(L); + if (nargs < 1) + luaL_error(L, "bad count of arguments"); + int res = 0; + for (int i = 1; i <= nargs; i++) { + bool res = luaM_toboolean(L, i); + if (res == false) { + lua_pushboolean(L, 0); + return 1; + } + } + + lua_pushboolean(L, 1); + + return 1; +} +static int mlua__equal(lua_State *L) +{ int nargs = lua_gettop(L); + if (nargs != 2) + luaL_error(L, "bad count of arguments"); + + int x = atoi(luaL_checkstring(L, 1)); + int y = atoi(luaL_checkstring(L, 2)); + + lua_pushboolean(L, x == y); + + return 1; +} + +static int mlua__if(lua_State *L) +{ + int nargs = lua_gettop(L); + if (nargs != 3) + luaL_error(L, "bad count of arguments"); + + int x = luaM_toboolean(L, 1); + const char *res = x + ? luaL_checkstring(L, 2) + : luaL_checkstring(L, 3); + lua_pushstring(L, res); + + return 1; +} + +/***********************************************/ + +static int mlua__add(lua_State *L) +{ + int nargs = lua_gettop(L); + if (nargs < 1) + luaL_error(L, "bad count of arguments"); + + int res = 0; for (int i = 1; i <= nargs; i++) - res += luaL_optinteger(L, i, 0); + res += luaL_checkinteger(L, i); lua_pushfstring(L, "%d", res); @@ -21,8 +74,12 @@ static int mlua__add(lua_State *L) static int mlua__div(lua_State *L) { - int x = luaL_optinteger(L, 1, 0); - int y = luaL_optinteger(L, 2, 0); + int nargs = lua_gettop(L); + if (nargs != 2) + luaL_error(L, "bad count of arguments"); + + int x = luaL_checkinteger(L, 1); + int y = luaL_checkinteger(L, 2); lua_pushfstring(L, "%d", x - y); @@ -31,8 +88,12 @@ static int mlua__div(lua_State *L) static int mlua__hex(lua_State *L) { - int x = luaL_optinteger(L, 1, 0); - int pad = luaL_optinteger(L, 2, 0); + int nargs = lua_gettop(L); + if (nargs != 2) + luaL_error(L, "bad count of arguments"); + + int x = luaL_checkinteger(L, 1); + int pad = luaL_checkinteger(L, 2); CMStringA format(FORMAT, "0x%%0%dx", pad); CMStringA value(FORMAT, format, x); @@ -43,9 +104,11 @@ static int mlua__hex(lua_State *L) static int mlua__max(lua_State *L) { - int res = 0; - int nargs = lua_gettop(L); + if (nargs < 1) + luaL_error(L, "bad count of arguments"); + + int res = 0; for (int i = 1; i <= nargs; i++) { int val = luaL_optinteger(L, i, INT_MIN); if (val > res) @@ -59,9 +122,12 @@ static int mlua__max(lua_State *L) static int mlua__min(lua_State *L) { + int nargs = lua_gettop(L); + if (nargs < 1) + luaL_error(L, "bad count of arguments"); + int res = 0; - int nargs = lua_gettop(L); for (int i = 1; i <= nargs; i++) { int val = luaL_optinteger(L, i, INT_MAX); if (val < res) @@ -75,8 +141,12 @@ static int mlua__min(lua_State *L) static int mlua__mod(lua_State *L) { - int x = luaL_optinteger(L, 1, 0); - int y = luaL_optinteger(L, 2, 0); + int nargs = lua_gettop(L); + if (nargs != 2) + luaL_error(L, "bad count of arguments"); + + int x = luaL_checkinteger(L, 1); + int y = luaL_checkinteger(L, 2); lua_pushfstring(L, "%d", x % y); @@ -85,8 +155,12 @@ static int mlua__mod(lua_State *L) static int mlua__mul(lua_State *L) { - int x = luaL_optinteger(L, 1, 0); - int y = luaL_optinteger(L, 2, 0); + int nargs = lua_gettop(L); + if (nargs != 2) + luaL_error(L, "bad count of arguments"); + + int x = luaL_checkinteger(L, 1); + int y = luaL_checkinteger(L, 2); lua_pushfstring(L, "%d", x * y); @@ -95,9 +169,13 @@ static int mlua__mul(lua_State *L) static int mlua__muldiv(lua_State *L) { - int x = luaL_optinteger(L, 1, 0); - int y = luaL_optinteger(L, 2, 0); - int z = luaL_optinteger(L, 3, 0); + int nargs = lua_gettop(L); + if (nargs != 3) + luaL_error(L, "bad count of arguments"); + + int x = luaL_checkinteger(L, 1); + int y = luaL_checkinteger(L, 2); + int z = luaL_checkinteger(L, 3); lua_pushfstring(L, "%d", (x * y) / z); @@ -106,8 +184,12 @@ static int mlua__muldiv(lua_State *L) static int mlua__num(lua_State *L) { - int x = luaL_optinteger(L, 1, 0); - int pad = luaL_optinteger(L, 2, 0); + int nargs = lua_gettop(L); + if (nargs != 2) + luaL_error(L, "bad count of arguments"); + + int x = luaL_checkinteger(L, 1); + int pad = luaL_checkinteger(L, 2); CMStringA format(FORMAT, "%%0%dx", pad); CMStringA value(FORMAT, format, x); @@ -123,14 +205,43 @@ static int mlua__rand(lua_State *L) return 1; } +static luaL_Reg varsApi[] = +{ + // logic + { "_vand", mlua__and }, + { "_vequal", mlua__equal }, + { "_vif", mlua__if }, + // math + { "_vadd", mlua__add }, + { "_vdiv", mlua__div }, + { "_vhex", mlua__hex }, + { "_vmax", mlua__max }, + { "_vmin", mlua__min }, + { "_vmod", mlua__mod }, + { "_vmul", mlua__mul }, + { "_vmuldiv", mlua__muldiv }, + { "_vnum", mlua__num }, + { "_vrand", mlua__rand } +}; + static wchar_t* translate(lua_State *L, const wchar_t *format, const wchar_t* /*extra*/, MCONTACT /*hContact*/ = NULL) { - std::wregex regex(L"\\?([a-z_0-9]+)\\("); - std::wstring query = std::regex_replace(format, regex, L"_v$1("); + // this should quote string params but is not work yet + std::wregex regex(L"(?<=\\(|,)`?([^0-9][a-z_0-9 ]+)`?(?=,|\\))"); + std::wstring query = std::regex_replace(format, regex, L"'$1'"); + + regex = L"\\?([a-z_0-9]+)\\("; + query = std::regex_replace(query, regex, L"_v$1("); query.insert(0, L"return "); + wchar_t *result = nullptr; + CMLuaEnvironment env(L); - wchar_t *result = env.Eval(query.c_str()); + if (env.Eval(query.c_str()) == LUA_OK) + result = mir_utf8decodeW(lua_tostring(L, -1)); + else + result = mir_wstrdup(format); + lua_pop(L, 1); env.Unload(); return result; @@ -150,7 +261,7 @@ static int mlua_vars(lua_State *L) /***********************************************/ -INT_PTR FormatString(void *obj, WPARAM wParam, LPARAM) +static INT_PTR FormatString(void *obj, WPARAM wParam, LPARAM) { lua_State *L = (lua_State*)obj; FORMATINFO *fi = (FORMATINFO*)wParam; @@ -169,6 +280,22 @@ INT_PTR FormatString(void *obj, WPARAM wParam, LPARAM) return (INT_PTR)result.detach(); } +static int CompareTokens(const TOKENREGISTER *p1, const TOKENREGISTER *p2) +{ + return mir_wstrcmpi(p1->szTokenString.w, p2->szTokenString.w); +} + +LIST<TOKENREGISTER> tokens(10, CompareTokens); + +static INT_PTR RegisterToken(void *obj, WPARAM, LPARAM lParam) +{ + lua_State *L = (lua_State*)obj; + TOKENREGISTER *tr = (TOKENREGISTER*)lParam; + + if (tr == nullptr || tr->szTokenString.w == nullptr || tr->cbSize <= 0) + return -1; +} + /***********************************************/ void CMLuaVariablesLoader::LoadVariables() @@ -176,22 +303,15 @@ void CMLuaVariablesLoader::LoadVariables() if (ServiceExists(MS_VARS_FORMATSTRING)) return; - // CreateServiceFunctionObj(MS_VARS_FORMATSTRING, FormatString, L); + //CreateServiceFunctionObj(MS_VARS_FORMATSTRING, FormatString, L); + //CreateServiceFunctionObj(MS_VARS_REGISTERTOKEN, RegisterToken, L); Log("Loading variables functions"); lua_register(L, "vars", mlua_vars); - // math - lua_register(L, "_vadd", mlua__add); - lua_register(L, "_vdiv", mlua__div); - lua_register(L, "_vhex", mlua__hex); - lua_register(L, "_vmax", mlua__max); - lua_register(L, "_vmin", mlua__min); - lua_register(L, "_vmod", mlua__mod); - lua_register(L, "_vmul", mlua__mul); - lua_register(L, "_vmuldiv", mlua__muldiv); - lua_register(L, "_vnum", mlua__num); - lua_register(L, "_vrand", mlua__rand); + + lua_pushglobaltable(L); + luaL_setfuncs(L, varsApi, 0); } void CMLuaVariablesLoader::Load(lua_State *L) |