aboutsummaryrefslogtreecommitdiff
path: root/src/script/lua_api
diff options
context:
space:
mode:
authorsfan5 <sfan5@live.de>2022-10-17 13:56:28 +0200
committerGitHub <noreply@github.com>2022-10-17 07:56:28 -0400
commit87051fca268944248af8aa5edd027a9acd800bcf (patch)
treed1301b2f2314a020608eb6f04ae9732fdbe13a5f /src/script/lua_api
parent5d8a4917c570e85917a3b4cf3a7b012a299012c6 (diff)
downloadminetest-87051fca268944248af8aa5edd027a9acd800bcf.tar.xz
Take geographic distance into account for server list ordering (#12790)
Diffstat (limited to 'src/script/lua_api')
-rw-r--r--src/script/lua_api/l_mainmenu.cpp41
-rw-r--r--src/script/lua_api/l_mainmenu.h3
2 files changed, 44 insertions, 0 deletions
diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp
index 789096d23..9c828430c 100644
--- a/src/script/lua_api/l_mainmenu.cpp
+++ b/src/script/lua_api/l_mainmenu.cpp
@@ -39,6 +39,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client/renderingengine.h"
#include "network/networkprotocol.h"
#include "content/mod_configuration.h"
+#include "threading/mutex_auto_lock.h"
/******************************************************************************/
std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
@@ -1008,6 +1009,44 @@ int ModApiMainMenu::l_do_async_callback(lua_State *L)
}
/******************************************************************************/
+// this is intentionally a global and not part of MainMenuScripting or such
+namespace {
+ std::unordered_map<std::string, std::string> once_values;
+ std::mutex once_mutex;
+}
+
+int ModApiMainMenu::l_set_once(lua_State *L)
+{
+ std::string key = readParam<std::string>(L, 1);
+ if (lua_isnil(L, 2))
+ return 0;
+ std::string value = readParam<std::string>(L, 2);
+
+ {
+ MutexAutoLock lock(once_mutex);
+ once_values[key] = value;
+ }
+
+ return 0;
+}
+
+int ModApiMainMenu::l_get_once(lua_State *L)
+{
+ std::string key = readParam<std::string>(L, 1);
+
+ {
+ MutexAutoLock lock(once_mutex);
+ auto it = once_values.find(key);
+ if (it == once_values.end())
+ lua_pushnil(L);
+ else
+ lua_pushstring(L, it->second.c_str());
+ }
+
+ return 1;
+}
+
+/******************************************************************************/
void ModApiMainMenu::Initialize(lua_State *L, int top)
{
API_FCT(update_formspec);
@@ -1054,6 +1093,8 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(open_dir);
API_FCT(share_file);
API_FCT(do_async_callback);
+ API_FCT(set_once);
+ API_FCT(get_once);
}
/******************************************************************************/
diff --git a/src/script/lua_api/l_mainmenu.h b/src/script/lua_api/l_mainmenu.h
index 9dc40c7f4..f2c2aed74 100644
--- a/src/script/lua_api/l_mainmenu.h
+++ b/src/script/lua_api/l_mainmenu.h
@@ -156,6 +156,9 @@ private:
static int l_share_file(lua_State *L);
+ static int l_set_once(lua_State *L);
+
+ static int l_get_once(lua_State *L);
// async
static int l_do_async_callback(lua_State *L);