From 40378e91063c869f86a0bcdd1bb4136959782f54 Mon Sep 17 00:00:00 2001 From: Alexander Lantsev Date: Sun, 25 Oct 2015 21:13:21 +0000 Subject: MirLua: added metatables for PROTOCOLDESCRIPTOR and PROTOACCOUNT in m_protocols git-svn-id: http://svn.miranda-ng.org/main/trunk@15617 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c --- plugins/MirLua/src/m_database.cpp | 4 +- plugins/MirLua/src/m_protocols.cpp | 108 +++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 2 deletions(-) (limited to 'plugins/MirLua') diff --git a/plugins/MirLua/src/m_database.cpp b/plugins/MirLua/src/m_database.cpp index bd21d1ec26..1854f5d1e1 100644 --- a/plugins/MirLua/src/m_database.cpp +++ b/plugins/MirLua/src/m_database.cpp @@ -344,8 +344,8 @@ static int lua_SettingIterator(lua_State *L) static int lua_AllSettings(lua_State *L) { - const char* szModule = luaL_checkstring(L, 1); - MCONTACT hContact = lua_tointeger(L, 2); + MCONTACT hContact = lua_tointeger(L, 1); + const char* szModule = luaL_checkstring(L, 2); enumDBSettingsParam* param = (enumDBSettingsParam*)mir_alloc(sizeof(enumDBSettingsParam)); param->count = 0; diff --git a/plugins/MirLua/src/m_protocols.cpp b/plugins/MirLua/src/m_protocols.cpp index 696b519cae..dcf6819fe6 100644 --- a/plugins/MirLua/src/m_protocols.cpp +++ b/plugins/MirLua/src/m_protocols.cpp @@ -15,6 +15,8 @@ static void MapToTable(lua_State *L, const PROTOCOLDESCRIPTOR* pd) static int lua_GetProto(lua_State *L) { + ObsoleteMethod(L, "Use totable(x, \"PROTOCOLDESCRIPTOR\") instead"); + ptrA name(mir_utf8decodeA(luaL_checkstring(L, 1))); PROTOCOLDESCRIPTOR* pd = ::Proto_IsProtocolLoaded(name); @@ -61,6 +63,8 @@ static int lua_AllProtos(lua_State *L) static int lua_EnumProtos(lua_State *L) { + ObsoleteMethod(L, "Use \"for x in protos.AllProtos \" instead"); + if (!lua_isfunction(L, 1)) { lua_pushlightuserdata(L, NULL); @@ -116,6 +120,8 @@ static void MapToTable(lua_State *L, const PROTOACCOUNT* pa) static int lua_GetAccount(lua_State *L) { + ObsoleteMethod(L, "Use totable(x, \"PROTOACCOUNT\") instead"); + ptrA moduleName(mir_utf8decodeA(luaL_checkstring(L, 1))); PROTOACCOUNT* pa = ::Proto_GetAccount(moduleName); @@ -162,6 +168,8 @@ static int lua_AllAccounts(lua_State *L) static int lua_EnumAccounts(lua_State *L) { + ObsoleteMethod(L, "Use \"for x in protos.AllAccounts \" instead"); + if (!lua_isfunction(L, 1)) { lua_pushlightuserdata(L, NULL); @@ -323,6 +331,98 @@ static luaL_Reg protocolsApi[] = { NULL, NULL } }; +#define MT_PROTOCOLDESCRIPTOR "PROTOCOLDESCRIPTOR" + +static int pd__init(lua_State *L) +{ + PROTOCOLDESCRIPTOR *udata = (PROTOCOLDESCRIPTOR*)lua_touserdata(L, 1); + if (udata == NULL) + { + lua_pushnil(L); + return 1; + } + + PROTOCOLDESCRIPTOR **pd = (PROTOCOLDESCRIPTOR**)lua_newuserdata(L, sizeof(PROTOCOLDESCRIPTOR*)); + *pd = udata; + + luaL_setmetatable(L, MT_PROTOCOLDESCRIPTOR); + + return 1; +} + +static int pd__index(lua_State *L) +{ + PROTOCOLDESCRIPTOR *pd = *(PROTOCOLDESCRIPTOR**)luaL_checkudata(L, 1, MT_PROTOCOLDESCRIPTOR); + const char *key = lua_tostring(L, 2); + + if (mir_strcmpi(key, "Name") == 0) + lua_pushstring(L, ptrA(mir_utf8encode(pd->szName))); + else if (mir_strcmpi(key, "Type") == 0) + lua_pushinteger(L, pd->type); + else + lua_pushnil(L); + + return 1; +} + +static luaL_Reg pdMeta[] = +{ + { "__init", pd__init }, + { "__index", pd__index }, + { NULL, NULL } +}; + +#define MT_PROTOACCOUNT "PROTOACCOUNT" + +static int pa__init(lua_State *L) +{ + PROTOACCOUNT *udata = (PROTOACCOUNT*)lua_touserdata(L, 1); + if (udata == NULL) + { + lua_pushnil(L); + return 1; + } + + PROTOACCOUNT **pa = (PROTOACCOUNT**)lua_newuserdata(L, sizeof(PROTOACCOUNT*)); + *pa = udata; + + luaL_setmetatable(L, MT_PROTOCOLDESCRIPTOR); + + return 1; +} + +static int pa__index(lua_State *L) +{ + PROTOACCOUNT *pa = *(PROTOACCOUNT**)luaL_checkudata(L, 1, MT_PROTOACCOUNT); + const char *key = lua_tostring(L, 2); + + if (mir_strcmpi(key, "InternalName") == 0) + lua_pushstring(L, ptrA(mir_utf8encode(pa->szModuleName))); + if (mir_strcmpi(key, "AccountName") == 0) + lua_pushstring(L, ptrA(mir_utf8encodeT(pa->tszAccountName))); + if (mir_strcmpi(key, "ProtoName") == 0) + lua_pushstring(L, ptrA(mir_utf8encode(pa->szProtoName))); + if (mir_strcmpi(key, "IsEnabled") == 0) + lua_pushboolean(L, pa->bIsEnabled); + if (mir_strcmpi(key, "IsVisible") == 0) + lua_pushboolean(L, pa->bIsVisible); + if (mir_strcmpi(key, "IsVirtual") == 0) + lua_pushboolean(L, pa->bIsVirtual); + if (mir_strcmpi(key, "OldProto") == 0) + lua_pushboolean(L, pa->bOldProto); + else + lua_pushnil(L); + + return 1; +} + +static luaL_Reg paMeta[] = +{ + { "__init", pa__init }, + { "__index", pa__index }, + { NULL, NULL } +}; + #define MT_ACKDATA "ACKDATA" static int ack__init(lua_State *L) @@ -420,6 +520,14 @@ LUAMOD_API int luaopen_m_protocols(lua_State *L) { luaL_newlib(L, protocolsApi); + luaL_newmetatable(L, MT_PROTOCOLDESCRIPTOR); + luaL_setfuncs(L, pdMeta, 0); + lua_pop(L, 1); + + luaL_newmetatable(L, MT_PROTOACCOUNT); + luaL_setfuncs(L, paMeta, 0); + lua_pop(L, 1); + luaL_newmetatable(L, MT_ACKDATA); luaL_setfuncs(L, ackMeta, 0); lua_pop(L, 1); -- cgit v1.2.3