From 8399ba8ad52d65cc63f9ac1aa80ef210c781d7fe Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <cazfi74@gmail.com>
Date: Thu, 26 Oct 2023 01:31:54 +0300
Subject: [PATCH 05/18] Lua: Add versions_compare() method

See osdn #48905

Signed-off-by: Marko Lindqvist <cazfi74@gmail.com>
---
 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 975e695d20..29f010c6e4 100644
--- a/common/scriptcore/Makefile.am
+++ b/common/scriptcore/Makefile.am
@@ -10,6 +10,7 @@ AM_CPPFLAGS = \
 	-I$(top_srcdir)/common/networking \
 	-I$(top_srcdir)/dependencies/tinycthread \
 	-I$(top_srcdir)/gen_headers/enums \
+	-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 <math.h>
 
+/* 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 f55e98169a..94fff72819 100644
--- a/server/Makefile.am
+++ b/server/Makefile.am
@@ -136,7 +136,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
@@ -159,6 +159,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 ea7fa0abe6..1c471e7487 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -40,4 +40,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 eb7ebeee39..e3105a6b29 100644
--- a/tools/manual/Makefile.am
+++ b/tools/manual/Makefile.am
@@ -35,5 +35,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 773611d4b6..0b2b712e60 100644
--- a/tools/ruledit/Makefile.am
+++ b/tools/ruledit/Makefile.am
@@ -111,6 +111,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