From 9ade1460d9b625b0855fc12374d6f3f7228a7329 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Wed, 5 Jan 2022 05:26:07 +0200 Subject: [PATCH 33/33] Split general parts from api_game_specenum to a new api_specenum See osdn #43544 Signed-off-by: Marko Lindqvist --- client/luascript/script_client.c | 2 +- common/scriptcore/Makefile.am | 2 + common/scriptcore/api_game_specenum.c | 66 +++------------------------ common/scriptcore/api_game_specenum.h | 2 +- common/scriptcore/api_specenum.c | 48 +++++++++++++++++++ common/scriptcore/api_specenum.h | 62 +++++++++++++++++++++++++ meson.build | 1 + server/scripting/script_server.c | 4 +- 8 files changed, 124 insertions(+), 63 deletions(-) create mode 100644 common/scriptcore/api_specenum.c create mode 100644 common/scriptcore/api_specenum.h diff --git a/client/luascript/script_client.c b/client/luascript/script_client.c index a592c5df58..d651b0aa00 100644 --- a/client/luascript/script_client.c +++ b/client/luascript/script_client.c @@ -209,7 +209,7 @@ bool script_client_init(void) } tolua_common_a_open(main_fcl->state); - api_specenum_open(main_fcl->state); + api_game_specenum_open(main_fcl->state); tolua_game_open(main_fcl->state); tolua_signal_open(main_fcl->state); diff --git a/common/scriptcore/Makefile.am b/common/scriptcore/Makefile.am index 9f1066b749..344dc71dfb 100644 --- a/common/scriptcore/Makefile.am +++ b/common/scriptcore/Makefile.am @@ -28,6 +28,8 @@ libscriptcore_la_SOURCES = \ api_game_specenum.h \ api_signal_base.c \ api_signal_base.h \ + api_specenum.c \ + api_specenum.h \ luascript.c \ luascript.h \ luascript_func.c \ diff --git a/common/scriptcore/api_game_specenum.c b/common/scriptcore/api_game_specenum.c index 804437be6f..e041c83b10 100644 --- a/common/scriptcore/api_game_specenum.c +++ b/common/scriptcore/api_game_specenum.c @@ -22,70 +22,17 @@ #include "lualib.h" #include "lauxlib.h" -/* common */ -#include "events.h" - /* utility */ #include "support.h" -#include "api_game_specenum.h" - +/* common */ +#include "events.h" -#define API_SPECENUM_INDEX_NAME(type) api_specenum_##type##_index -#define API_SPECENUM_CREATE_TABLE(L, type, name) \ - api_specenum_create_table((L), (name), API_SPECENUM_INDEX_NAME(type)) +/* common/scriptcore */ +#include "api_specenum.h" -/**********************************************************************//** - Define a the __index (table, key) -> value metamethod - Return the enum value whose name is the concatenation of prefix and key. - The fetched value is written back to the lua table, and further accesses - will resolve there instead of this function. -**************************************************************************/ -#define API_SPECENUM_DEFINE_INDEX(type_name, prefix) \ - static int (API_SPECENUM_INDEX_NAME(type_name))(lua_State *L) \ - { \ - static char _buf[128]; \ - const char *_key; \ - enum type_name _value; \ - luaL_checktype(L, 1, LUA_TTABLE); \ - _key = luaL_checkstring(L, 2); \ - fc_snprintf(_buf, sizeof(_buf), prefix "%s", _key); \ - _value = type_name##_by_name(_buf, strcmp); \ - if (_value != type_name##_invalid()) { \ - /* T[_key] = _value */ \ - lua_pushstring(L, _key); \ - lua_pushinteger(L, _value); \ - lua_rawset(L, 1); \ - lua_pushinteger(L, _value); \ - } else { \ - lua_pushnil(L); \ - } \ - return 1; \ - } +#include "api_game_specenum.h" -/**********************************************************************//** - Create a module table and set the member lookup function. -**************************************************************************/ -static void api_specenum_create_table(lua_State *L, const char *name, - lua_CFunction findex) -{ - /* Insert a module table in the global environment, - * or reuse any preexisting table */ - lua_getglobal(L, name); - if (lua_isnil(L, -1)) { - lua_newtable(L); - lua_pushvalue(L, -1); - lua_setglobal(L, name); - } - fc_assert_ret(lua_istable(L, -1)); - /* Create a metatable */ - lua_newtable(L); /* stack: module mt */ - lua_pushliteral(L, "__index"); - lua_pushcfunction(L, findex); /* stack: module mt '__index' index */ - lua_rawset(L, -3); /* stack: module mt */ - lua_setmetatable(L, -2); /* stack: module */ - lua_pop(L, 1); -} /**********************************************************************//** Define the __index function for each exported specenum type. @@ -95,8 +42,9 @@ API_SPECENUM_DEFINE_INDEX(event_type, "E_") /**********************************************************************//** Load the specenum modules into Lua state L. **************************************************************************/ -int api_specenum_open(lua_State *L) +int api_game_specenum_open(lua_State *L) { API_SPECENUM_CREATE_TABLE(L, event_type, "E"); + return 0; } diff --git a/common/scriptcore/api_game_specenum.h b/common/scriptcore/api_game_specenum.h index 7007d9afd7..b2b1591651 100644 --- a/common/scriptcore/api_game_specenum.h +++ b/common/scriptcore/api_game_specenum.h @@ -19,7 +19,7 @@ extern "C" { struct lua_State; -int api_specenum_open(lua_State *L); +int api_game_specenum_open(lua_State *L); #ifdef __cplusplus } diff --git a/common/scriptcore/api_specenum.c b/common/scriptcore/api_specenum.c new file mode 100644 index 0000000000..3906e65e17 --- /dev/null +++ b/common/scriptcore/api_specenum.c @@ -0,0 +1,48 @@ +/***************************************************************************** + Freeciv - Copyright (C) 2010 - The Freeciv Project + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. +*****************************************************************************/ + +#ifdef HAVE_CONFIG_H +#include +#endif + +/* dependencies/lua */ +#include "lua.h" + +/* utilit */ +#include "log.h" + +#include "api_specenum.h" + +/**********************************************************************//** + Create a module table and set the member lookup function. +**************************************************************************/ +void api_specenum_create_table(lua_State *L, const char *name, + lua_CFunction findex) +{ + /* Insert a module table in the global environment, + * or reuse any preexisting table */ + lua_getglobal(L, name); + if (lua_isnil(L, -1)) { + lua_newtable(L); + lua_pushvalue(L, -1); + lua_setglobal(L, name); + } + fc_assert_ret(lua_istable(L, -1)); + /* Create a metatable */ + lua_newtable(L); /* stack: module mt */ + lua_pushliteral(L, "__index"); + lua_pushcfunction(L, findex); /* stack: module mt '__index' index */ + lua_rawset(L, -3); /* stack: module mt */ + lua_setmetatable(L, -2); /* stack: module */ + lua_pop(L, 1); +} diff --git a/common/scriptcore/api_specenum.h b/common/scriptcore/api_specenum.h new file mode 100644 index 0000000000..0012e9553e --- /dev/null +++ b/common/scriptcore/api_specenum.h @@ -0,0 +1,62 @@ +/***************************************************************************** + Freeciv - Copyright (C) 2010 - The Freeciv Project + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. +*****************************************************************************/ +#ifndef FC__API_SPECENUM_H +#define FC__API_SPECENUM_H + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define API_SPECENUM_INDEX_NAME(type) api_specenum_##type##_index + +/**********************************************************************//** + Define a the __index (table, key) -> value metamethod + Return the enum value whose name is the concatenation of prefix and key. + The fetched value is written back to the lua table, and further accesses + will resolve there instead of this function. +**************************************************************************/ +#define API_SPECENUM_DEFINE_INDEX(type_name, prefix) \ + static int (API_SPECENUM_INDEX_NAME(type_name))(lua_State *L) \ + { \ + static char _buf[128]; \ + const char *_key; \ + enum type_name _value; \ + luaL_checktype(L, 1, LUA_TTABLE); \ + _key = luaL_checkstring(L, 2); \ + fc_snprintf(_buf, sizeof(_buf), prefix "%s", _key); \ + _value = type_name##_by_name(_buf, strcmp); \ + if (_value != type_name##_invalid()) { \ + /* T[_key] = _value */ \ + lua_pushstring(L, _key); \ + lua_pushinteger(L, _value); \ + lua_rawset(L, 1); \ + lua_pushinteger(L, _value); \ + } else { \ + lua_pushnil(L); \ + } \ + return 1; \ + } + +void api_specenum_create_table(lua_State *L, const char *name, + lua_CFunction findex); + +#define API_SPECENUM_CREATE_TABLE(L, type, name) \ + api_specenum_create_table((L), (name), API_SPECENUM_INDEX_NAME(type)) + + + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* FC__API_SPECENUM_H */ diff --git a/meson.build b/meson.build index 8eec1344d9..18803df489 100644 --- a/meson.build +++ b/meson.build @@ -585,6 +585,7 @@ common_lib = library('freeciv', 'common/scriptcore/api_game_methods.c', 'common/scriptcore/api_game_specenum.c', 'common/scriptcore/api_signal_base.c', + 'common/scriptcore/api_specenum.c', 'common/scriptcore/luascript.c', 'common/scriptcore/luascript_func.c', 'common/scriptcore/luascript_signal.c', diff --git a/server/scripting/script_server.c b/server/scripting/script_server.c index ee4e44eb33..da7462e3f2 100644 --- a/server/scripting/script_server.c +++ b/server/scripting/script_server.c @@ -306,7 +306,7 @@ bool script_server_init(void) } tolua_common_a_open(fcl_main->state); - api_specenum_open(fcl_main->state); + api_game_specenum_open(fcl_main->state); tolua_game_open(fcl_main->state); tolua_signal_open(fcl_main->state); @@ -340,7 +340,7 @@ bool script_server_init(void) } tolua_common_a_open(fcl_unsafe->state); - api_specenum_open(fcl_unsafe->state); + api_game_specenum_open(fcl_unsafe->state); tolua_game_open(fcl_unsafe->state); #ifdef MESON_BUILD -- 2.34.1