diff options
author | aunsane <aunsane@gmail.com> | 2018-05-28 21:26:54 +0300 |
---|---|---|
committer | aunsane <aunsane@gmail.com> | 2018-05-28 21:26:54 +0300 |
commit | 3bfd3b9c9062048fde854b4c45ab62c29624d8f8 (patch) | |
tree | 0003dc8a93986529e0618df81ce1059dc5ed00fd /plugins/MirLua/src/Modules | |
parent | d5c8ecc7a45798e8ba6674e6323244e2f2c291fb (diff) |
MirLua: removed global Load & Unload
Diffstat (limited to 'plugins/MirLua/src/Modules')
-rw-r--r-- | plugins/MirLua/src/Modules/m_core.cpp | 6 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_database.cpp | 48 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_http.cpp | 2 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_json.cpp | 120 | ||||
-rw-r--r-- | plugins/MirLua/src/Modules/m_srmm.cpp | 6 |
5 files changed, 111 insertions, 71 deletions
diff --git a/plugins/MirLua/src/Modules/m_core.cpp b/plugins/MirLua/src/Modules/m_core.cpp index 365b89bb43..281aa288fe 100644 --- a/plugins/MirLua/src/Modules/m_core.cpp +++ b/plugins/MirLua/src/Modules/m_core.cpp @@ -89,12 +89,12 @@ static int core_HookTemporaryEvent(lua_State *L) static int core_UnhookEvent(lua_State *L) { luaL_checktype(L, 1, LUA_TLIGHTUSERDATA); - HANDLE hEvent = lua_touserdata(L, 1); + HANDLE hHook = lua_touserdata(L, 1); CMLuaEnvironment *env = CMLuaEnvironment::GetEnvironment(L); int res = env != nullptr - ? env->UnhookEvent(hEvent) - : UnhookEvent(hEvent); + ? env->UnhookEvent(hHook) + : UnhookEvent(hHook); lua_pushboolean(L, !res); return 1; diff --git a/plugins/MirLua/src/Modules/m_database.cpp b/plugins/MirLua/src/Modules/m_database.cpp index 3d0d53ba48..875b1e6eb0 100644 --- a/plugins/MirLua/src/Modules/m_database.cpp +++ b/plugins/MirLua/src/Modules/m_database.cpp @@ -1,5 +1,7 @@ #include "../stdafx.h" +#define MT_CONTACT "CONTACT" + void luaM_pushdbvt(lua_State *L, const DBVARIANT &value) { switch (value.type) { @@ -134,7 +136,7 @@ static int db_GetContactInfo(lua_State *L) static int db_GetEventCount(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); int res = db_event_count(hContact); lua_pushinteger(L, res); @@ -144,7 +146,7 @@ static int db_GetEventCount(lua_State *L) static int db_GetFirstEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT res = db_event_first(hContact); lua_pushinteger(L, res); @@ -154,7 +156,7 @@ static int db_GetFirstEvent(lua_State *L) static int db_GetPrevEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT hDbEvent = luaL_checkinteger(L, 2); MEVENT res = db_event_prev(hContact, hDbEvent); @@ -165,7 +167,7 @@ static int db_GetPrevEvent(lua_State *L) static int db_GetNextEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT hDbEvent = luaL_checkinteger(L, 2); MEVENT res = db_event_next(hContact, hDbEvent); @@ -176,7 +178,7 @@ static int db_GetNextEvent(lua_State *L) static int db_GetLastEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT res = db_event_last(hContact); lua_pushinteger(L, res); @@ -186,7 +188,7 @@ static int db_GetLastEvent(lua_State *L) static int db_GetFirstUnreadEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT res = db_event_firstUnread(hContact); lua_pushinteger(L, res); @@ -216,7 +218,7 @@ static int db_EventIterator(lua_State *L) static int db_Events(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); lua_pushinteger(L, hContact); lua_pushinteger(L, NULL); @@ -247,7 +249,7 @@ static int db_EventReverseIterator(lua_State *L) static int db_EventsFromEnd(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); lua_pushinteger(L, hContact); lua_pushinteger(L, NULL); @@ -276,7 +278,7 @@ static int db_UnreadEventIterator(lua_State *L) static int db_UnreadEvents(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); lua_pushinteger(L, hContact); lua_pushinteger(L, NULL); @@ -327,8 +329,8 @@ void MakeDbEvent(lua_State *L, DBEVENTINFO &dbei) static int db_AddEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); - + MCONTACT hContact = luaL_optinteger(L, 1, 0); + DBEVENTINFO dbei; MakeDbEvent(L, dbei); MEVENT hDbEvent = db_event_add(hContact, &dbei); @@ -341,9 +343,20 @@ static int db_AddEvent(lua_State *L) return 1; } +static int db_DeleteEvent(lua_State *L) +{ + MCONTACT hContact = luaL_optinteger(L, 1, 0); + MEVENT hDbEvent = luaL_checkinteger(L, 2); + + int res = db_event_delete(hContact, hDbEvent); + lua_pushboolean(L, res == 0); + + return 1; +} + static int db_MarkReadEvent(lua_State *L) { - MCONTACT hContact = luaL_checkinteger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); MEVENT hDbEvent = luaL_checkinteger(L, 2); int res = db_event_markRead(hContact, hDbEvent); @@ -398,7 +411,7 @@ static int db_Modules(lua_State *L) static int db_DeleteModule(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); const char *szModule = luaL_checkstring(L, 2); INT_PTR res = db_delete_module(hContact, szModule); @@ -437,7 +450,7 @@ static int db_SettingIterator(lua_State *L) static int db_Settings(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); const char* szModule = luaL_checkstring(L, 2); LIST<char> *param = new LIST<char>(5, PtrKeySortT); @@ -452,7 +465,7 @@ static int db_Settings(lua_State *L) static int db_GetSetting(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); const char *szModule = luaL_checkstring(L, 2); const char *szSetting = luaL_checkstring(L, 3); @@ -475,7 +488,7 @@ static int db_GetSetting(lua_State *L) static int db_WriteSetting(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); const char *szModule = luaL_checkstring(L, 2); const char *szSetting = luaL_checkstring(L, 3); luaL_checkany(L, 4); @@ -549,7 +562,7 @@ static int db_WriteSetting(lua_State *L) static int db_DeleteSetting(lua_State *L) { - MCONTACT hContact = lua_tointeger(L, 1); + MCONTACT hContact = luaL_optinteger(L, 1, 0); LPCSTR szModule = luaL_checkstring(L, 2); LPCSTR szSetting = luaL_checkstring(L, 3); @@ -577,6 +590,7 @@ static luaL_Reg databaseApi[] = { "EventsFromEnd", db_EventsFromEnd }, { "UnreadEvents", db_UnreadEvents }, { "AddEvent", db_AddEvent }, + { "DeleteEvent", db_DeleteEvent }, { "MarkReadEvent", db_MarkReadEvent }, { "Settings", db_Settings }, diff --git a/plugins/MirLua/src/Modules/m_http.cpp b/plugins/MirLua/src/Modules/m_http.cpp index 31def57a9a..81750619da 100644 --- a/plugins/MirLua/src/Modules/m_http.cpp +++ b/plugins/MirLua/src/Modules/m_http.cpp @@ -165,7 +165,7 @@ static const luaL_Reg contentApi[] = static NETLIBHTTPREQUEST* response_Create(lua_State *L, NETLIBHTTPREQUEST *request) { NETLIBHTTPREQUEST **response = (NETLIBHTTPREQUEST**)lua_newuserdata(L, sizeof(NETLIBHTTPREQUEST*)); - *response = Netlib_HttpTransaction(hNetlib, request); + *response = Netlib_HttpTransaction(g_hNetlib, request); luaL_setmetatable(L, MT_NETLIBHTTPRESPONSE); return *response; diff --git a/plugins/MirLua/src/Modules/m_json.cpp b/plugins/MirLua/src/Modules/m_json.cpp index 24e32cab09..907e0f582b 100644 --- a/plugins/MirLua/src/Modules/m_json.cpp +++ b/plugins/MirLua/src/Modules/m_json.cpp @@ -17,7 +17,7 @@ static void lua2json(lua_State *L, JSONNode &node) case LUA_TNUMBER: { lua_Integer val = lua_tointeger(L, -1); - if (lua_isinteger(L, -1) && val >= LONG_MIN && val <= LONG_MIN) + if (lua_isinteger(L, -1) && val >= LONG_MIN && val <= LONG_MAX) node = (long)val; else node = lua_tonumber(L, -1); @@ -48,6 +48,21 @@ static void lua2json(lua_State *L, JSONNode &node) break; } + case LUA_TLIGHTUSERDATA: + if (lua_touserdata(L, -1)) + luaL_argerror(L, -1, luaL_typename(L, -1)); + node.nullify(); + break; + case LUA_TUSERDATA: + { + ptrA name(mir_strdup(node.name())); + JSONNode *other = *(JSONNode**)luaL_checkudata(L, -1, MT_JSON); + node = other->duplicate(); + node.set_name((char*)name); + break; + } + default: + luaL_argerror(L, -1, luaL_typename(L, -1)); } } @@ -167,73 +182,86 @@ static int lua_Decode(lua_State *L) return 1; } +/***********************************************/ + static int lua_Encode(lua_State *L) { + //JSONNode node; + //lua_pushnil(L); + //lua_pushvalue(L, 1); + //lua2json(L, node); + //lua_pop(L, 2); + //lua_pushstring(L, node.write().c_str()); + switch (lua_type(L, 1)) { - case LUA_TNIL: - lua_pushliteral(L, "null"); - break; - case LUA_TBOOLEAN: - lua_pushstring(L, lua_toboolean(L, 1) ? "true" : "false"); - break; - case LUA_TNUMBER: - { - if (lua_isinteger(L, 1)) { - lua_pushfstring(L, "%I", lua_tointeger(L, 1)); + case LUA_TNIL: + lua_pushliteral(L, "null"); + break; + case LUA_TBOOLEAN: + lua_pushstring(L, lua_toboolean(L, 1) ? "true" : "false"); + break; + case LUA_TNUMBER: + { + if (lua_isinteger(L, 1)) { + lua_pushfstring(L, "%I", lua_tointeger(L, 1)); + break; + } + char decpoint = lua_getlocaledecpoint(); + if (decpoint != '.') { + char p[2] = { decpoint }; + luaL_gsub(L, lua_tostring(L, 1), p, "."); + } + else + lua_pushfstring(L, "%f", lua_tonumber(L, 1)); break; } - char decpoint = lua_getlocaledecpoint(); - if (decpoint != '.') { - char p[2] = { decpoint }; - luaL_gsub(L, lua_tostring(L, 1), p, "."); + case LUA_TSTRING: + lua_pushfstring(L, "\"%s\"", lua_tostring(L, 1)); + break; + case LUA_TTABLE: + { + JSONNode node; + lua_pushnil(L); + lua_pushvalue(L, 1); + lua2json(L, node); + lua_pop(L, 2); + lua_pushstring(L, node.write().c_str()); + break; } - else - lua_pushfstring(L, "%f", lua_tonumber(L, 1)); - break; - } - case LUA_TSTRING: - lua_pushfstring(L, "\"%s\"", lua_tostring(L, 1)); - break; - case LUA_TTABLE: - { - JSONNode node; - lua_pushnil(L); - lua_pushvalue(L, 1); - lua2json(L, node); - lua_pop(L, 2); - lua_pushstring(L, node.write().c_str()); - break; - } - case LUA_TUSERDATA: - { - JSONNode *node = *(JSONNode**)luaL_checkudata(L, 1, MT_JSON); - lua_pushstring(L, node->write().c_str()); - break; - } - case LUA_TLIGHTUSERDATA: - if (lua_touserdata(L, 1) == nullptr) + case LUA_TUSERDATA: { - lua_pushliteral(L, "null"); + JSONNode *node = *(JSONNode**)luaL_checkudata(L, 1, MT_JSON); + lua_pushstring(L, node->write().c_str()); break; } - default: - luaL_argerror(L, 1, luaL_typename(L, 1)); - } + case LUA_TLIGHTUSERDATA: + if (lua_touserdata(L, 1) == nullptr) + { + lua_pushliteral(L, "null"); + break; + } + default: + luaL_argerror(L, 1, luaL_typename(L, 1)); + } - return 1; -} + return 1; + } static const luaL_Reg methods[] = { { "Decode", lua_Decode }, { "Encode", lua_Encode }, + { "null", nullptr }, + { nullptr, nullptr } }; LUAMOD_API int luaopen_m_json(lua_State *L) { luaL_newlib(L, methods); + lua_pushlightuserdata(L, nullptr); + lua_setfield(L, -2, "null"); luaL_newmetatable(L, MT_JSON); luaL_setfuncs(L, jsonApi, 0); diff --git a/plugins/MirLua/src/Modules/m_srmm.cpp b/plugins/MirLua/src/Modules/m_srmm.cpp index f7296d1d70..bbaf3edc3f 100644 --- a/plugins/MirLua/src/Modules/m_srmm.cpp +++ b/plugins/MirLua/src/Modules/m_srmm.cpp @@ -51,8 +51,7 @@ static int lua_AddButton(lua_State *L) HANDLE res = Srmm_AddButton(&bbb, hScriptLangpack); CleanBBButton(bbb); - if (!res) - { + if (!res) { lua_pushnil(L); return 1; } @@ -82,8 +81,7 @@ static int lua_RemoveButton(lua_State *L) { BBButton bbb = {}; - switch (lua_type(L, 1)) - { + switch (lua_type(L, 1)) { case LUA_TSTRING: bbb.pszModuleName = mir_utf8decodeA(lua_tostring(L, 1)); bbb.dwButtonID = luaL_checkinteger(L, 2); |