From 90574a97d6243a9cbe21df586b52d0516d4f54fb Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Thu, 26 Oct 2023 01:31:54 +0300 Subject: [PATCH 04/12] Lua: Add versions_compare() method See osdn #48905 Signed-off-by: Marko Lindqvist --- common/scriptcore/Makefile.am | 1 + common/scriptcore/api_common_utilities.c | 33 ++++++++++++++++++++++++ common/scriptcore/api_common_utilities.h | 2 ++ common/scriptcore/tolua_common_a.pkg | 2 ++ server/Makefile.am | 3 ++- tools/Makefile.am | 1 + tools/manual/Makefile.am | 1 + tools/ruledit/Makefile.am | 1 + 8 files changed, 43 insertions(+), 1 deletion(-) diff --git a/common/scriptcore/Makefile.am b/common/scriptcore/Makefile.am index 162d467ff2..a4427391fd 100644 --- a/common/scriptcore/Makefile.am +++ b/common/scriptcore/Makefile.am @@ -9,6 +9,7 @@ AM_CPPFLAGS = \ -I$(top_srcdir)/common/aicore \ -I$(top_srcdir)/common/networking \ -I$(top_srcdir)/dependencies/tinycthread \ + -I$(top_srcdir)/dependencies/cvercmp \ $(LUA_CFLAGS) $(LUASQL_CFLAGS) $(TOLUA_CFLAGS) # tolua_[common_a|common_z|game]_gen.[ch] are now distributed to aid in diff --git a/common/scriptcore/api_common_utilities.c b/common/scriptcore/api_common_utilities.c index 42ba7ada84..d4f027cdc2 100644 --- a/common/scriptcore/api_common_utilities.c +++ b/common/scriptcore/api_common_utilities.c @@ -17,6 +17,9 @@ #include +/* dependencies/cvercmp */ +#include "cvercmp.h" + /* utilities */ #include "deprecations.h" #include "log.h" @@ -90,6 +93,36 @@ const char *api_utilities_version_string(lua_State *L) return freeciv_datafile_version(); } +/********************************************************************//** + Compare two version strings. Return which one is bigger, or zero + if they are equal. +************************************************************************/ +int api_utilities_versions_compare(lua_State *L, + const char *ver1, const char *ver2) +{ + enum cvercmp_type result; + + LUASCRIPT_CHECK_STATE(L, 0); + LUASCRIPT_CHECK_ARG_NIL(L, ver1, 2, string, 0); + LUASCRIPT_CHECK_ARG_NIL(L, ver2, 3, string, 0); + + result = cvercmp_cmp(ver1, ver2); + + switch (result) { + case CVERCMP_EQUAL: + return 0; + case CVERCMP_GREATER: + return 1; + case CVERCMP_LESSER: + return -1; + default: + fc_assert(result == CVERCMP_EQUAL + || result == CVERCMP_GREATER + || result == CVERCMP_LESSER); + return 0; + } +} + /********************************************************************//** One log message. This module is used by script_game and script_auth. ************************************************************************/ diff --git a/common/scriptcore/api_common_utilities.h b/common/scriptcore/api_common_utilities.h index f5e2342a41..2689f63071 100644 --- a/common/scriptcore/api_common_utilities.h +++ b/common/scriptcore/api_common_utilities.h @@ -38,6 +38,8 @@ const char *api_utilities_name_version(lua_State *L); const char *api_utilities_comparable_version(lua_State *L); const char *api_utilities_version_string(lua_State *L); +int api_utilities_versions_compare(lua_State *L, const char *ver1, const char *ver2); + void api_utilities_log_base(lua_State *L, int level, const char *message); void api_utilities_deprecation_warning(lua_State *L, char *method, diff --git a/common/scriptcore/tolua_common_a.pkg b/common/scriptcore/tolua_common_a.pkg index 513ad66d4a..b703d51fda 100644 --- a/common/scriptcore/tolua_common_a.pkg +++ b/common/scriptcore/tolua_common_a.pkg @@ -128,6 +128,8 @@ const char *api_utilities_comparable_version const char *api_utilities_version_string @ version_string (lua_State *L); +int api_utilities_versions_compare + @ versions_compare (lua_State *L, const char *ver1, const char *ver2); $[ -- *************************************************************************** -- Dump the state of user scalar variables to a Lua code string. diff --git a/server/Makefile.am b/server/Makefile.am index 0080912783..99d278b0fe 100644 --- a/server/Makefile.am +++ b/server/Makefile.am @@ -143,7 +143,7 @@ if AI_MOD_STATIC_STUB da_libs += $(top_builddir)/ai/stub/libstubai.la endif -# These files are not generated to builddir, but to srcdir */ +# These files are not generated to builddir, but to srcdir MAINTAINERCLEANFILES = \ $(srcdir)/hand_gen.c \ $(srcdir)/hand_gen.h @@ -166,6 +166,7 @@ exe_ldflags = exe_ldadd = \ ./libfreeciv-srv.la \ $(top_builddir)/common/libfreeciv.la \ + $(top_builddir)/dependencies/cvercmp/libcvercmp.la \ $(INTLLIBS) \ $(MAPIMG_WAND_LIBS) \ $(TINYCTHR_LIBS) \ diff --git a/tools/Makefile.am b/tools/Makefile.am index 634f8a949d..a88fd6dc62 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -39,4 +39,5 @@ freeciv_ruleup_LDADD = \ $(top_builddir)/common/libfreeciv.la \ $(top_builddir)/tools/ruleutil/libfcruleutil.la \ $(top_builddir)/tools/shared/libtoolsshared.la \ + $(top_builddir)/dependencies/cvercmp/libcvercmp.la \ $(TINYCTHR_LIBS) $(MAPIMG_WAND_LIBS) $(SERVER_LIBS) diff --git a/tools/manual/Makefile.am b/tools/manual/Makefile.am index 3e7e06920e..ea04b1c7b6 100644 --- a/tools/manual/Makefile.am +++ b/tools/manual/Makefile.am @@ -34,5 +34,6 @@ freeciv_manual_LDADD = \ $(top_builddir)/client/libfc_helpdata.la \ $(top_builddir)/common/libfreeciv.la \ $(top_builddir)/tools/shared/libtoolsshared.la \ + $(top_builddir)/dependencies/cvercmp/libcvercmp.la \ $(INTLLIBS) $(TINYCTHR_LIBS) $(MAPIMG_WAND_LIBS) \ $(SERVER_LIBS) diff --git a/tools/ruledit/Makefile.am b/tools/ruledit/Makefile.am index fd30a5e94f..724eb55edd 100644 --- a/tools/ruledit/Makefile.am +++ b/tools/ruledit/Makefile.am @@ -110,6 +110,7 @@ freeciv_ruledit_LDADD = \ $(top_builddir)/common/libfreeciv.la \ $(top_builddir)/tools/ruleutil/libfcruleutil.la \ $(top_builddir)/tools/shared/libtoolsshared.la \ + $(top_builddir)/dependencies/cvercmp/libcvercmp.la \ $(TINYCTHR_LIBS) $(MAPIMG_WAND_LIBS) $(SERVER_LIBS) \ $(REICON) -- 2.42.0