From 1acd03f96f9b1133e1f76d76004f5fcddd0c4788 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Mon, 28 Jul 2014 17:09:39 +0200 Subject: Added cClientHandle:GetUUIDsFromPlayerNames() to Lua API. --- src/Bindings/ManualBindings.cpp | 65 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 3 deletions(-) (limited to 'src/Bindings') diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index df9687fc0..28ee00b36 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -26,6 +26,7 @@ #include "../BlockEntities/MobHeadEntity.h" #include "../BlockEntities/FlowerPotEntity.h" #include "../LineBlockTracer.h" +#include "../Protocol/Authenticator.h" #include "../WorldStorage/SchematicFileSerializer.h" #include "../CompositeChat.h" @@ -2156,6 +2157,63 @@ static int tolua_cClientHandle_SendPluginMessage(lua_State * L) +static int tolua_cClientHandle_GetUUIDsFromPlayerNames(lua_State * L) +{ + cLuaState S(L); + if ( + !S.CheckParamUserTable(1, "cClientHandle") || + !S.CheckParamTable(2) || + !S.CheckParamEnd(3) + ) + { + return 0; + } + + // Convert the input table into AStringVector: + AStringVector PlayerNames; + int NumNames = luaL_getn(L, 2); + PlayerNames.reserve(NumNames); + for (int i = 1; i <= NumNames; i++) + { + lua_rawgeti(L, 2, i); + AString Name; + S.GetStackValue(3, Name); + if (!Name.empty()) + { + PlayerNames.push_back(Name); + } + lua_pop(L, 1); + } + + // Push the output table onto the stack: + lua_newtable(L); // stack index 3 + + // Get the UUIDs: + AStringVector UUIDs = cRoot::Get()->GetAuthenticator().GetUUIDsFromPlayerNames(PlayerNames); + if (UUIDs.size() != PlayerNames.size()) + { + // A hard error has occured while processing the request, no UUIDs were returned. Return an empty table: + return 1; + } + + // Convert to output table, PlayerName -> UUID: + for (int i = 0; i < NumNames; i++) + { + if (UUIDs[i].empty()) + { + // No UUID was provided for PlayerName[i], skip it in the resulting table + continue; + } + lua_pushlstring(L, UUIDs[i].c_str(), UUIDs[i].length()); + lua_setfield(L, 3, PlayerNames[i].c_str()); + } + return 1; +} + + + + + static int Lua_ItemGrid_GetSlotCoords(lua_State * L) { tolua_Error tolua_err; @@ -3083,9 +3141,10 @@ void ManualBindings::Bind(lua_State * tolua_S) tolua_endmodule(tolua_S); tolua_beginmodule(tolua_S, "cClientHandle"); - tolua_constant(tolua_S, "MAX_VIEW_DISTANCE", cClientHandle::MAX_VIEW_DISTANCE); - tolua_constant(tolua_S, "MIN_VIEW_DISTANCE", cClientHandle::MIN_VIEW_DISTANCE); - tolua_function(tolua_S, "SendPluginMessage", tolua_cClientHandle_SendPluginMessage); + tolua_constant(tolua_S, "MAX_VIEW_DISTANCE", cClientHandle::MAX_VIEW_DISTANCE); + tolua_constant(tolua_S, "MIN_VIEW_DISTANCE", cClientHandle::MIN_VIEW_DISTANCE); + tolua_function(tolua_S, "SendPluginMessage", tolua_cClientHandle_SendPluginMessage); + tolua_function(tolua_S, "GetUUIDsFromPlayerNames", tolua_cClientHandle_GetUUIDsFromPlayerNames); tolua_endmodule(tolua_S); tolua_beginmodule(tolua_S, "cItemGrid"); -- cgit v1.2.3 From 4dd858f8997488e2252f5a04df9df1654a70d67f Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Tue, 29 Jul 2014 17:45:55 +0200 Subject: Added a cMojangAPI class for PlayerName -> UUID lookups, with cache. The cache is persisted into a SQLite DB file on server shutdown. --- src/Bindings/ManualBindings.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Bindings') diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 28ee00b36..6d69e2595 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -2189,7 +2189,7 @@ static int tolua_cClientHandle_GetUUIDsFromPlayerNames(lua_State * L) lua_newtable(L); // stack index 3 // Get the UUIDs: - AStringVector UUIDs = cRoot::Get()->GetAuthenticator().GetUUIDsFromPlayerNames(PlayerNames); + AStringVector UUIDs = cRoot::Get()->GetMojangAPI().GetUUIDsFromPlayerNames(PlayerNames); if (UUIDs.size() != PlayerNames.size()) { // A hard error has occured while processing the request, no UUIDs were returned. Return an empty table: -- cgit v1.2.3 From 6476bd0e2ee7e128e3eaa56159f169f0a53736ff Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 30 Jul 2014 13:44:03 +0200 Subject: Exported cMojangAPI to Lua. --- src/Bindings/AllToLua.pkg | 1 + src/Bindings/ManualBindings.cpp | 9 ++++++--- 2 files changed, 7 insertions(+), 3 deletions(-) (limited to 'src/Bindings') diff --git a/src/Bindings/AllToLua.pkg b/src/Bindings/AllToLua.pkg index 1e5dfd2fe..d3e3f5b45 100644 --- a/src/Bindings/AllToLua.pkg +++ b/src/Bindings/AllToLua.pkg @@ -77,6 +77,7 @@ $cfile "../Map.h" $cfile "../MapManager.h" $cfile "../Scoreboard.h" $cfile "../Statistics.h" +$cfile "../Protocol/MojangAPI.h" diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 6d69e2595..026a6bad5 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -2157,11 +2157,11 @@ static int tolua_cClientHandle_SendPluginMessage(lua_State * L) -static int tolua_cClientHandle_GetUUIDsFromPlayerNames(lua_State * L) +static int tolua_cMojangAPI_GetUUIDsFromPlayerNames(lua_State * L) { cLuaState S(L); if ( - !S.CheckParamUserTable(1, "cClientHandle") || + !S.CheckParamUserTable(1, "cMojangAPI") || !S.CheckParamTable(2) || !S.CheckParamEnd(3) ) @@ -3144,9 +3144,12 @@ void ManualBindings::Bind(lua_State * tolua_S) tolua_constant(tolua_S, "MAX_VIEW_DISTANCE", cClientHandle::MAX_VIEW_DISTANCE); tolua_constant(tolua_S, "MIN_VIEW_DISTANCE", cClientHandle::MIN_VIEW_DISTANCE); tolua_function(tolua_S, "SendPluginMessage", tolua_cClientHandle_SendPluginMessage); - tolua_function(tolua_S, "GetUUIDsFromPlayerNames", tolua_cClientHandle_GetUUIDsFromPlayerNames); tolua_endmodule(tolua_S); + tolua_beginmodule(tolua_S, "cMojangAPI"); + tolua_function(tolua_S, "GetUUIDsFromPlayerNames", tolua_cMojangAPI_GetUUIDsFromPlayerNames); + tolua_endmodule(tolua_S); + tolua_beginmodule(tolua_S, "cItemGrid"); tolua_function(tolua_S, "GetSlotCoords", Lua_ItemGrid_GetSlotCoords); tolua_endmodule(tolua_S); -- cgit v1.2.3 From 426773df17dbd7748b51021a27119240de5b1922 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 30 Jul 2014 14:03:14 +0200 Subject: ManualBindings: Fixed alignment. --- src/Bindings/ManualBindings.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/Bindings') diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index 026a6bad5..b1d302560 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -3141,9 +3141,9 @@ void ManualBindings::Bind(lua_State * tolua_S) tolua_endmodule(tolua_S); tolua_beginmodule(tolua_S, "cClientHandle"); - tolua_constant(tolua_S, "MAX_VIEW_DISTANCE", cClientHandle::MAX_VIEW_DISTANCE); - tolua_constant(tolua_S, "MIN_VIEW_DISTANCE", cClientHandle::MIN_VIEW_DISTANCE); - tolua_function(tolua_S, "SendPluginMessage", tolua_cClientHandle_SendPluginMessage); + tolua_constant(tolua_S, "MAX_VIEW_DISTANCE", cClientHandle::MAX_VIEW_DISTANCE); + tolua_constant(tolua_S, "MIN_VIEW_DISTANCE", cClientHandle::MIN_VIEW_DISTANCE); + tolua_function(tolua_S, "SendPluginMessage", tolua_cClientHandle_SendPluginMessage); tolua_endmodule(tolua_S); tolua_beginmodule(tolua_S, "cMojangAPI"); -- cgit v1.2.3 From 8b519bf6e20a987c9544ef11f0df6467831cc069 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Thu, 31 Jul 2014 10:02:50 +0200 Subject: MojangAPI: Added a UseCachedOnly param to GetUUIDsFromPlayerNames(). --- src/Bindings/ManualBindings.cpp | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src/Bindings') diff --git a/src/Bindings/ManualBindings.cpp b/src/Bindings/ManualBindings.cpp index b1d302560..038173c99 100644 --- a/src/Bindings/ManualBindings.cpp +++ b/src/Bindings/ManualBindings.cpp @@ -2163,7 +2163,7 @@ static int tolua_cMojangAPI_GetUUIDsFromPlayerNames(lua_State * L) if ( !S.CheckParamUserTable(1, "cMojangAPI") || !S.CheckParamTable(2) || - !S.CheckParamEnd(3) + !S.CheckParamEnd(4) ) { return 0; @@ -2177,19 +2177,27 @@ static int tolua_cMojangAPI_GetUUIDsFromPlayerNames(lua_State * L) { lua_rawgeti(L, 2, i); AString Name; - S.GetStackValue(3, Name); + S.GetStackValue(-1, Name); if (!Name.empty()) { PlayerNames.push_back(Name); } lua_pop(L, 1); } + + // If the UseOnlyCached param was given, read it; default to false + bool ShouldUseCacheOnly = false; + if (lua_gettop(L) == 3) + { + ShouldUseCacheOnly = (lua_toboolean(L, 3) != 0); + lua_pop(L, 1); + } // Push the output table onto the stack: - lua_newtable(L); // stack index 3 + lua_newtable(L); // Get the UUIDs: - AStringVector UUIDs = cRoot::Get()->GetMojangAPI().GetUUIDsFromPlayerNames(PlayerNames); + AStringVector UUIDs = cRoot::Get()->GetMojangAPI().GetUUIDsFromPlayerNames(PlayerNames, ShouldUseCacheOnly); if (UUIDs.size() != PlayerNames.size()) { // A hard error has occured while processing the request, no UUIDs were returned. Return an empty table: @@ -2197,7 +2205,8 @@ static int tolua_cMojangAPI_GetUUIDsFromPlayerNames(lua_State * L) } // Convert to output table, PlayerName -> UUID: - for (int i = 0; i < NumNames; i++) + size_t len = UUIDs.size(); + for (size_t i = 0; i < len; i++) { if (UUIDs[i].empty()) { -- cgit v1.2.3