diff --git a/projectfiles/Xcode/Wesnoth.xcodeproj/project.pbxproj b/projectfiles/Xcode/Wesnoth.xcodeproj/project.pbxproj index 569a08864d0e..d5ce15d13d02 100644 --- a/projectfiles/Xcode/Wesnoth.xcodeproj/project.pbxproj +++ b/projectfiles/Xcode/Wesnoth.xcodeproj/project.pbxproj @@ -279,7 +279,6 @@ 91A2156B1CAD6DA400927AEA /* lua_race.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECC2FFF91A51A00900023AF4 /* lua_race.cpp */; }; 91A2156C1CAD6DA500927AEA /* lua_rng.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECA4A67A1A1EC319006BCCF2 /* lua_rng.cpp */; }; 91A2156D1CAD6DA500927AEA /* lua_team.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC5430211A4E6024006D206C /* lua_team.cpp */; }; - 91A2156E1CAD6DA500927AEA /* lua_types.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C02A0E182F1F64008525C6 /* lua_types.cpp */; }; 91A2156F1CAD6DA500927AEA /* lua_unit_type.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC5430221A4E6024006D206C /* lua_unit_type.cpp */; }; 91A215701CAD6E7500927AEA /* context.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC59F25F1A4529D2001910CB /* context.cpp */; }; 91A215711CAD6E7500927AEA /* manager.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC59F2601A4529D2001910CB /* manager.cpp */; }; @@ -1260,7 +1259,6 @@ F480CD4B14035038007175D6 /* sourceparser.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F480CD3C14035038007175D6 /* sourceparser.cpp */; }; F480CD4C14035038007175D6 /* tag.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F480CD3E14035038007175D6 /* tag.cpp */; }; F49F134A15BC627C00B64B0B /* edit_label.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F49F134815BC627C00B64B0B /* edit_label.cpp */; }; - F4C02A11182F1F64008525C6 /* lua_types.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C02A0E182F1F64008525C6 /* lua_types.cpp */; }; F4C5DD68158CFD5E0044F754 /* point.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C5DD66158CFD5E0044F754 /* point.cpp */; }; F4D2A99614DAED0E00CAFF31 /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4D2A99514DAED0E00CAFF31 /* CoreFoundation.framework */; }; F4D2A9D514DAED4200CAFF31 /* CoreFoundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F4D2A99514DAED0E00CAFF31 /* CoreFoundation.framework */; }; @@ -4144,8 +4142,6 @@ 91B621EC1B76BB2200B00E0F /* lua_rng.hpp */, EC5430211A4E6024006D206C /* lua_team.cpp */, 91B621ED1B76BB2600B00E0F /* lua_team.hpp */, - F4C02A0E182F1F64008525C6 /* lua_types.cpp */, - F4C02A0F182F1F64008525C6 /* lua_types.hpp */, 9193FC801D5C2CF7004F6C07 /* lua_unit.cpp */, 9193FC811D5C2CF8004F6C07 /* lua_unit.hpp */, 9193FC841D5D7452004F6C07 /* lua_unit_attacks.cpp */, @@ -4953,7 +4949,6 @@ ECC2FFFA1A51A00900023AF4 /* lua_race.cpp in Sources */, ECA4A67D1A1EC319006BCCF2 /* lua_rng.cpp in Sources */, EC5430231A4E6024006D206C /* lua_team.cpp in Sources */, - F4C02A11182F1F64008525C6 /* lua_types.cpp in Sources */, EC5430241A4E6024006D206C /* lua_unit_type.cpp in Sources */, EC89A1481879D17D00A3B0B1 /* lundump.cpp in Sources */, EC89A1491879D17D00A3B0B1 /* lvm.cpp in Sources */, @@ -5612,7 +5607,6 @@ 91A2156B1CAD6DA400927AEA /* lua_race.cpp in Sources */, 91A2156C1CAD6DA500927AEA /* lua_rng.cpp in Sources */, 91A2156D1CAD6DA500927AEA /* lua_team.cpp in Sources */, - 91A2156E1CAD6DA500927AEA /* lua_types.cpp in Sources */, 91A2156F1CAD6DA500927AEA /* lua_unit_type.cpp in Sources */, 91A215701CAD6E7500927AEA /* context.cpp in Sources */, 91A215711CAD6E7500927AEA /* manager.cpp in Sources */, diff --git a/src/ai/contexts.cpp b/src/ai/contexts.cpp index 9b94eefbb7a6..7b5ec5608229 100644 --- a/src/ai/contexts.cpp +++ b/src/ai/contexts.cpp @@ -383,8 +383,7 @@ void readonly_context_impl::calculate_moves(const unit_map& units, std::map(&*un_it); - const unit_movement_resetter move_resetter(*held_unit,enemy || assume_full_movement); + const unit_movement_resetter move_resetter(*un_it,enemy || assume_full_movement); // Insert the trivial moves of staying on the same map location. if (un_it->movement_left() > 0) { diff --git a/src/ai/lua/core.cpp b/src/ai/lua/core.cpp index 15d9e2dad62d..7f4d1bf5fec8 100644 --- a/src/ai/lua/core.cpp +++ b/src/ai/lua/core.cpp @@ -50,7 +50,8 @@ static lg::log_domain log_ai_engine_lua("ai/engine/lua"); #define LOG_LUA LOG_STREAM(info, log_ai_engine_lua) #define ERR_LUA LOG_STREAM(err, log_ai_engine_lua) -static char const aisKey = 0; +// This is an array so we can use sizeof() +static char const aisKey[] = "ai contexts"; namespace ai { @@ -59,7 +60,7 @@ static void push_attack_analysis(lua_State *L, const attack_analysis&); void lua_ai_context::init(lua_State *L) { // Create the ai elements table. - lua_pushlightuserdata(L, static_cast(const_cast(&aisKey))); + lua_pushlstring(L, aisKey, sizeof(aisKey)); lua_newtable(L); lua_rawset(L, LUA_REGISTRYINDEX); } @@ -68,7 +69,7 @@ void lua_ai_context::get_arguments(config &cfg) const { int top = lua_gettop(L); - lua_pushlightuserdata(L, static_cast(const_cast(&aisKey))); + lua_pushlstring(L, aisKey, sizeof(aisKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_rawgeti(L, -1, num_); @@ -82,7 +83,7 @@ void lua_ai_context::set_arguments(const config &cfg) { int top = lua_gettop(L); - lua_pushlightuserdata(L, static_cast(const_cast(&aisKey))); + lua_pushlstring(L, aisKey, sizeof(aisKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_rawgeti(L, -1, num_); @@ -96,7 +97,7 @@ void lua_ai_context::get_persistent_data(config &cfg) const { int top = lua_gettop(L); - lua_pushlightuserdata(L, static_cast(const_cast(&aisKey))); + lua_pushlstring(L, aisKey, sizeof(aisKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_rawgeti(L, -1, num_); @@ -110,7 +111,7 @@ void lua_ai_context::set_persistent_data(const config &cfg) { int top = lua_gettop(L); - lua_pushlightuserdata(L, static_cast(const_cast(&aisKey))); + lua_pushlstring(L, aisKey, sizeof(aisKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_rawgeti(L, -1, num_); @@ -955,7 +956,7 @@ static void generate_and_push_ai_table(lua_State* L, ai::engine_lua* engine) { static size_t generate_and_push_ai_state(lua_State* L, ai::engine_lua* engine) { // Retrieve the ai elements table from the registry. - lua_pushlightuserdata(L, static_cast(const_cast(&aisKey))); + lua_pushlstring(L, aisKey, sizeof(aisKey)); lua_rawget(L, LUA_REGISTRYINDEX); // [-1: AIs registry table] size_t length_ai = lua_rawlen(L, -1); // length of table lua_newtable(L); // [-1: AI state table -2: AIs registry table] @@ -1022,7 +1023,7 @@ lua_ai_action_handler* lua_ai_action_handler::create(lua_State *L, char const *c } // Retrieve the ai elements table from the registry. - lua_pushlightuserdata(L, static_cast(const_cast(&aisKey))); + lua_pushlstring(L, aisKey, sizeof(aisKey)); lua_rawget(L, LUA_REGISTRYINDEX); //stack size is now 2 [-1: ais_table -2: f] // Push the function in the table so that it is not collected. size_t length = lua_rawlen(L, -1);//length of ais_table @@ -1054,7 +1055,7 @@ lua_ai_load::lua_ai_load(lua_ai_context& ctx, bool read_only) : L(ctx.L), was_re return; // Leave the AI table on the stack, as requested } lua_pop(L, 1); // Pop the nil value off the stack - lua_pushlightuserdata(L, static_cast(const_cast(&aisKey))); // [-1: key] + lua_pushlstring(L, aisKey, sizeof(aisKey)); // [-1: key] lua_rawget(L, LUA_REGISTRYINDEX); // [-1: AI registry] lua_rawgeti(L, -1, ctx.num_); // [-1: AI state -2: AI registry] lua_remove(L,-2); // [-1: AI state] @@ -1087,7 +1088,7 @@ lua_ai_load::~lua_ai_load() lua_ai_context::~lua_ai_context() { // Remove the ai context from the registry, so that it can be collected. - lua_pushlightuserdata(L, static_cast(const_cast(&aisKey))); + lua_pushlstring(L, aisKey, sizeof(aisKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_pushnil(L); lua_rawseti(L, -2, num_); @@ -1102,7 +1103,7 @@ void lua_ai_action_handler::handle(const config &cfg, bool read_only, lua_object lua_ai_load ctx(context_, read_only); // [-1: AI state table] // Load the user function from the registry. - lua_pushlightuserdata(L, static_cast(const_cast(&aisKey))); // [-1: key -2: AI state] + lua_pushlstring(L, aisKey, sizeof(aisKey)); // [-1: key -2: AI state] lua_rawget(L, LUA_REGISTRYINDEX); // [-1: AI registry -2: AI state] lua_rawgeti(L, -1, num_); // [-1: AI action -2: AI registry -3: AI state] lua_remove(L, -2); // [-1: AI action -2: AI state] @@ -1125,7 +1126,7 @@ void lua_ai_action_handler::handle(const config &cfg, bool read_only, lua_object lua_ai_action_handler::~lua_ai_action_handler() { // Remove the function from the registry, so that it can be collected. - lua_pushlightuserdata(L, static_cast(const_cast(&aisKey))); + lua_pushlstring(L, aisKey, sizeof(aisKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_pushnil(L); lua_rawseti(L, -2, num_); diff --git a/src/ai/lua/lua_object.cpp b/src/ai/lua/lua_object.cpp index 4504bf445633..b75a0c4122ae 100644 --- a/src/ai/lua/lua_object.cpp +++ b/src/ai/lua/lua_object.cpp @@ -21,7 +21,6 @@ #include "ai/lua/lua_object.hpp" #include "ai/lua/engine_lua.hpp" #include "ai/default/aspect_attacks.hpp" -#include "scripting/lua_types.hpp" #include "scripting/lua_common.hpp" #include "resources.hpp" diff --git a/src/config.cpp b/src/config.cpp index ab92fa04c354..8c2338a17e08 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -863,8 +863,10 @@ void config::recursive_clear_value(const std::string& key) values.erase(key); - for (const any_child &value : all_children_range()) { - const_cast(&value.cfg)->recursive_clear_value(key); + for(std::pair& p : children) { + for(config* cfg : p.second) { + cfg->recursive_clear_value(key); + } } } @@ -1339,8 +1341,10 @@ void config::clear_diff_track(const config& diff) itor->second[index]->clear_diff_track(item.cfg); } } - for (const any_child &value : all_children_range()) { - const_cast(&value.cfg)->remove_attribute(diff_track_attribute); + for(std::pair& p : children) { + for(config* cfg : p.second) { + cfg->remove_attribute(diff_track_attribute); + } } } diff --git a/src/game_initialization/mp_options.cpp b/src/game_initialization/mp_options.cpp index be01b065dc17..9c3704f55d3b 100644 --- a/src/game_initialization/mp_options.cpp +++ b/src/game_initialization/mp_options.cpp @@ -213,9 +213,9 @@ const config &manager::get_values() config& manager::get_value_cfg(const std::string& id) { { - const config& value_cfg = get_value_cfg_or_empty(id); - if (!value_cfg.empty()) { - return const_cast(value_cfg); + config& value_cfg = values_.find_child("option", "id", id); + if(value_cfg) { + return value_cfg; } } @@ -237,16 +237,8 @@ config& manager::get_value_cfg(const std::string& id) const config& manager::get_value_cfg_or_empty(const std::string& id) const { static const config empty; - - for (const config::any_child& i : values_.all_children_range()) { - for (const config& j : i.cfg.child_range("option")) { - if (j["id"] == id) { - return j; - } - } - } - - return empty; + const config& cfg = values_.find_child("option", "id", id); + return cfg ? cfg : empty; } config::any_child manager::get_option_parent(const std::string& id) const diff --git a/src/map/label.cpp b/src/map/label.cpp index 42e264b3a207..13326b98d203 100644 --- a/src/map/label.cpp +++ b/src/map/label.cpp @@ -94,7 +94,7 @@ void map_labels::read(const config &cfg) recalculate_labels(); } -const terrain_label* map_labels::get_label(const map_location& loc, const std::string& team_name) const +terrain_label* map_labels::get_label_private(const map_location& loc, const std::string& team_name) { team_label_map::const_iterator label_map = labels_.find(team_name); if (label_map != labels_.end()) { diff --git a/src/map/label.hpp b/src/map/label.hpp index 4c00a20fc4d7..4bda334ecaab 100644 --- a/src/map/label.hpp +++ b/src/map/label.hpp @@ -41,7 +41,9 @@ class map_labels void write(config& res) const; void read(const config &cfg); - const terrain_label* get_label(const map_location& loc, const std::string& team_name) const; + const terrain_label* get_label(const map_location& loc, const std::string& team_name) const { + return const_cast(this)->get_label_private(loc, team_name); + } // search a team-only label, if fails then try public labels const terrain_label* get_label(const map_location& loc) const; const terrain_label* set_label(const map_location& loc, @@ -81,10 +83,7 @@ class map_labels void add_label(const map_location &, terrain_label *); void clear_map(label_map &, bool); - /// For our private use, a wrapper for get_label() that can return a pointer - /// to a non-const terrain_label. - terrain_label* get_label_private(const map_location& loc, const std::string& team_name) - { return const_cast(get_label(loc, team_name)); } + terrain_label* get_label_private(const map_location& loc, const std::string& team_name); // Note: this is not an overload of get_label() so that we do not block // outsiders from calling get_label for a non-const map_labels object. diff --git a/src/pathfind/pathfind.cpp b/src/pathfind/pathfind.cpp index a74cf55c36f4..7d2b7eaf33ae 100644 --- a/src/pathfind/pathfind.cpp +++ b/src/pathfind/pathfind.cpp @@ -458,7 +458,7 @@ static void find_routes( } -static paths::dest_vect::iterator lower_bound(paths::dest_vect &v, const map_location &loc) +static paths::dest_vect::const_iterator lower_bound(const paths::dest_vect &v, const map_location &loc) { size_t sz = v.size(), pos = 0; while (sz) @@ -473,14 +473,14 @@ static paths::dest_vect::iterator lower_bound(paths::dest_vect &v, const map_loc paths::dest_vect::const_iterator paths::dest_vect::find(const map_location &loc) const { - const_iterator i = lower_bound(const_cast(*this), loc), i_end = end(); + const_iterator i = lower_bound(*this, loc), i_end = end(); if (i != i_end && i->curr != loc) i = i_end; return i; } void paths::dest_vect::insert(const map_location &loc) { - iterator i = lower_bound(*this, loc), i_end = end(); + const_iterator i = lower_bound(*this, loc), i_end = end(); if (i != i_end && i->curr == loc) return; paths::step s = { loc, map_location(), 0 }; std::vector::insert(i, s); diff --git a/src/play_controller.cpp b/src/play_controller.cpp index 6d7bbf7f5b2a..6f7c35049284 100644 --- a/src/play_controller.cpp +++ b/src/play_controller.cpp @@ -739,7 +739,7 @@ const mp_game_settings& play_controller::get_mp_settings() return saved_game_.mp_settings(); } -const game_classification& play_controller::get_classification() +game_classification& play_controller::get_classification() { return saved_game_.classification(); } diff --git a/src/play_controller.hpp b/src/play_controller.hpp index 6cf0e6878d7d..aed8823dda90 100644 --- a/src/play_controller.hpp +++ b/src/play_controller.hpp @@ -198,7 +198,7 @@ class play_controller : public controller_base, public events::observer, public std::shared_ptr get_whiteboard(); const mp_game_settings& get_mp_settings(); - const game_classification& get_classification(); + game_classification& get_classification(); int get_server_request_number() const { return gamestate().server_request_number_; } void increase_server_request_number() { ++gamestate().server_request_number_; } diff --git a/src/scripting/application_lua_kernel.cpp b/src/scripting/application_lua_kernel.cpp index d096b01d2af4..b9a5014b7e9a 100644 --- a/src/scripting/application_lua_kernel.cpp +++ b/src/scripting/application_lua_kernel.cpp @@ -34,7 +34,6 @@ #include "scripting/lua_cpp_function.hpp" #include "scripting/lua_fileops.hpp" #include "scripting/lua_kernel_base.hpp" -#include "scripting/lua_types.hpp" #include "scripting/plugins/context.hpp" #include "scripting/plugins/manager.hpp" diff --git a/src/scripting/game_lua_kernel.cpp b/src/scripting/game_lua_kernel.cpp index 30931e407eda..339ec43e965f 100644 --- a/src/scripting/game_lua_kernel.cpp +++ b/src/scripting/game_lua_kernel.cpp @@ -80,7 +80,6 @@ #include "scripting/lua_pathfind_cost_calculator.hpp" #include "scripting/lua_race.hpp" #include "scripting/lua_team.hpp" -#include "scripting/lua_types.hpp" // for getunitKey, dlgclbkKey, etc #include "scripting/lua_unit_type.hpp" #include "scripting/push_check.hpp" #include "sdl/utils.hpp" // for surface @@ -714,14 +713,14 @@ int game_lua_kernel::intf_clear_menu_item(lua_State *L) int game_lua_kernel::intf_set_end_campaign_credits(lua_State *L) { - game_classification &classification = const_cast (play_controller_.get_classification()); + game_classification &classification = play_controller_.get_classification(); classification.end_credits = luaW_toboolean(L, 1); return 0; } int game_lua_kernel::intf_set_end_campaign_text(lua_State *L) { - game_classification &classification = const_cast (play_controller_.get_classification()); + game_classification &classification = play_controller_.get_classification(); classification.end_text = luaW_checktstring(L, 1); if (lua_isnumber(L, 2)) { classification.end_text_duration = static_cast (lua_tonumber(L, 2)); diff --git a/src/scripting/lua_common.cpp b/src/scripting/lua_common.cpp index 2238c2270af8..d89e4e7aa009 100644 --- a/src/scripting/lua_common.cpp +++ b/src/scripting/lua_common.cpp @@ -27,7 +27,6 @@ #include "config.hpp" #include "scripting/lua_unit.hpp" -#include "scripting/lua_types.hpp" // for gettextKey, tstringKey, etc #include "tstring.hpp" // for t_string #include "variable.hpp" // for vconfig #include "log.hpp" @@ -44,11 +43,13 @@ #include "lua/lauxlib.h" #include "lua/lua.h" -static const char * gettextKey = "gettext"; -static const char * vconfigKey = "vconfig"; -static const char * vconfigpairsKey = "vconfig pairs"; -static const char * vconfigipairsKey = "vconfig ipairs"; -const char * tstringKey = "translatable string"; +// These are arrays so we can use sizeof +static const char gettextKey[] = "gettext"; +static const char vconfigKey[] = "vconfig"; +static const char vconfigpairsKey[] = "vconfig pairs"; +static const char vconfigipairsKey[] = "vconfig ipairs"; +static const char tstringKey[] = "translatable string"; +static const char executeKey[] = "err"; static lg::log_domain log_scripting_lua("scripting/lua"); #define LOG_LUA LOG_STREAM(info, log_scripting_lua) @@ -535,13 +536,11 @@ bool luaW_toscalar(lua_State *L, int index, config::attribute_value& v) return true; } -bool luaW_hasmetatable(lua_State *L - , int index - , luatypekey key) +bool luaW_hasmetatable(lua_State *L, int index, const char* key) { if (!lua_getmetatable(L, index)) return false; - lua_pushlightuserdata(L, key); + lua_pushstring(L, key); lua_rawget(L, LUA_REGISTRYINDEX); bool ok = lua_rawequal(L, -1, -2) == 1; lua_pop(L, 2); @@ -912,7 +911,7 @@ int luaW_pcall_internal(lua_State *L, int nArgs, int nRets); void push_error_handler(lua_State *L) { - lua_pushlightuserdata(L, executeKey); + lua_pushlstring(L, executeKey, sizeof(executeKey)); lua_getglobal(L, "debug"); lua_getfield(L, -1, "traceback"); lua_remove(L, -2); @@ -923,7 +922,7 @@ void push_error_handler(lua_State *L) int luaW_pcall_internal(lua_State *L, int nArgs, int nRets) { // Load the error handler before the function and its arguments. - lua_pushlightuserdata(L, executeKey); + lua_pushlstring(L, executeKey, sizeof(executeKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_insert(L, -2 - nArgs); diff --git a/src/scripting/lua_common.hpp b/src/scripting/lua_common.hpp index 50bc910ddbfc..93638fa0a1d8 100644 --- a/src/scripting/lua_common.hpp +++ b/src/scripting/lua_common.hpp @@ -25,7 +25,6 @@ class t_string; class vconfig; #include "config.hpp" -#include "scripting/lua_types.hpp" #include "variable_info.hpp" #include "map/location.hpp" #include @@ -40,7 +39,6 @@ namespace lua_common { std::string register_vconfig_metatable(lua_State *L); } -extern const char * tstringKey; void* operator new(size_t sz, lua_State *L); void operator delete(void* p, lua_State *L); @@ -69,7 +67,7 @@ bool luaW_toscalar(lua_State *L, int index, config::attribute_value& v); /** * Returns true if the metatable of the object is the one found in the registry. */ -bool luaW_hasmetatable(lua_State *L, int index, luatypekey key); +bool luaW_hasmetatable(lua_State *L, int index, const char* key); /** * Converts a scalar to a translatable string. diff --git a/src/scripting/lua_formula_bridge.cpp b/src/scripting/lua_formula_bridge.cpp index 1dac82bb5577..799374fafb52 100644 --- a/src/scripting/lua_formula_bridge.cpp +++ b/src/scripting/lua_formula_bridge.cpp @@ -27,6 +27,9 @@ #include "resources.hpp" #include "units/map.hpp" +// This is an array so we can use sizeof() +static const char formulaKey[] = "formula"; + void luaW_pushfaivariant(lua_State* L, variant val); variant luaW_tofaivariant(lua_State* L, int i); @@ -233,7 +236,7 @@ int lua_formula_bridge::intf_compile_formula(lua_State* L) luaL_typerror(L, 1, "string"); } new(L) fwrapper(lua_tostring(L, 1)); - lua_pushlightuserdata(L, formulaKey); + lua_pushlstring(L, formulaKey, sizeof(formulaKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_setmetatable(L, -2); return 1; @@ -277,7 +280,7 @@ static int impl_formula_tostring(lua_State* L) std::string lua_formula_bridge::register_metatables(lua_State* L) { - lua_pushlightuserdata(L, formulaKey); + lua_pushlstring(L, formulaKey, sizeof(formulaKey)); lua_createtable(L, 0, 4); lua_pushcfunction(L, impl_formula_collect); lua_setfield(L, -2, "__gc"); diff --git a/src/scripting/lua_gui2.cpp b/src/scripting/lua_gui2.cpp index af6bb2f81e4a..ba88626c5a62 100644 --- a/src/scripting/lua_gui2.cpp +++ b/src/scripting/lua_gui2.cpp @@ -42,7 +42,6 @@ #include "config.hpp" #include "log.hpp" #include "scripting/lua_common.hpp" -#include "scripting/lua_types.hpp" // for getunitKey, dlgclbkKey, etc #include "serialization/string_utils.hpp" #include "tstring.hpp" diff --git a/src/scripting/lua_kernel_base.cpp b/src/scripting/lua_kernel_base.cpp index c14a28ab58cb..0fac3c41de20 100644 --- a/src/scripting/lua_kernel_base.cpp +++ b/src/scripting/lua_kernel_base.cpp @@ -34,7 +34,6 @@ #include "scripting/lua_gui2.hpp" #include "scripting/lua_map_location_ops.hpp" #include "scripting/lua_rng.hpp" -#include "scripting/lua_types.hpp" #include "scripting/push_check.hpp" #include "version.hpp" // for do_version_check, etc diff --git a/src/scripting/lua_types.cpp b/src/scripting/lua_types.cpp deleted file mode 100644 index e395b9635ba9..000000000000 --- a/src/scripting/lua_types.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - Copyright (C) 2014 - 2016 by David White - Part of the Battle for Wesnoth Project http://www.wesnoth.org/ - - 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 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY. - - See the COPYING file for more details. -*/ - -#include "lua_types.hpp" - -/* Dummy pointer for getting unique keys for Lua's registry. */ -static char const v_executeKey = 0; -static char const v_getunitKey = 0; -static char const v_unitvarKey = 0; -static char const v_ustatusKey = 0; -static char const v_uattacksKey = 0; -static char const v_uattackKey = 0; -static char const v_formulaKey = 0; - - -luatypekey const executeKey = static_cast(const_cast(&v_executeKey)); -luatypekey const getunitKey = static_cast(const_cast(&v_getunitKey)); -luatypekey const unitvarKey = static_cast(const_cast(&v_unitvarKey)); -luatypekey const ustatusKey = static_cast(const_cast(&v_ustatusKey)); -luatypekey const uattacksKey = static_cast(const_cast(&v_uattacksKey)); -luatypekey const uattackKey = static_cast(const_cast(&v_uattackKey)); -luatypekey const formulaKey = static_cast(const_cast(&v_formulaKey)); diff --git a/src/scripting/lua_types.hpp b/src/scripting/lua_types.hpp deleted file mode 100644 index dd5d2fd7d068..000000000000 --- a/src/scripting/lua_types.hpp +++ /dev/null @@ -1,30 +0,0 @@ -/* - Copyright (C) 2014 - 2016 by David White - Part of the Battle for Wesnoth Project http://www.wesnoth.org/ - - 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 of the License, or - (at your option) any later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY. - - See the COPYING file for more details. -*/ - -#ifndef SCRIPTING_LUA_TYPES_HPP -#define SCRIPTING_LUA_TYPES_HPP - -typedef void* luatypekey; - -// i dont want to cast to void* each time .... -// a drawback is, that these are now normal static variables wich are initialised at initialisation time (so you shoudn't use these at/before initialisation time). -extern luatypekey const executeKey; -extern luatypekey const getunitKey; -extern luatypekey const unitvarKey; -extern luatypekey const ustatusKey; -extern luatypekey const uattacksKey; -extern luatypekey const uattackKey; -extern luatypekey const formulaKey; - -#endif diff --git a/src/scripting/lua_unit.cpp b/src/scripting/lua_unit.cpp index 4fc523375bf1..c1910a2db449 100644 --- a/src/scripting/lua_unit.cpp +++ b/src/scripting/lua_unit.cpp @@ -13,7 +13,6 @@ */ #include "lua_unit.hpp" -#include "lua_types.hpp" #include "game_board.hpp" #include "log.hpp" @@ -33,6 +32,11 @@ static lg::log_domain log_scripting_lua("scripting/lua"); #define LOG_LUA LOG_STREAM(info, log_scripting_lua) #define ERR_LUA LOG_STREAM(err, log_scripting_lua) +// These are arrays so we can use sizeof +static const char getunitKey[] = "unit"; +static const char ustatusKey[] = "unit status"; +static const char unitvarKey[] = "unit variables"; + lua_unit::~lua_unit() { } @@ -196,7 +200,7 @@ lua_unit* luaW_checkunit_ref(lua_State *L, int index) void lua_unit::setmetatable(lua_State *L) { - lua_pushlightuserdata(L, getunitKey); + lua_pushlstring(L, getunitKey, sizeof(getunitKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_setmetatable(L, -2); } @@ -328,7 +332,7 @@ static int impl_unit_get(lua_State *L) lua_createtable(L, 1, 0); lua_pushvalue(L, 1); lua_rawseti(L, -2, 1); - lua_pushlightuserdata(L, ustatusKey); + lua_pushlstring(L, ustatusKey, sizeof(ustatusKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_setmetatable(L, -2); return 1; @@ -337,7 +341,7 @@ static int impl_unit_get(lua_State *L) lua_createtable(L, 1, 0); lua_pushvalue(L, 1); lua_rawseti(L, -2, 1); - lua_pushlightuserdata(L, unitvarKey); + lua_pushlstring(L, unitvarKey, sizeof(unitvarKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_setmetatable(L, -2); return 1; @@ -531,7 +535,7 @@ namespace lua_units { // Create the getunit metatable. cmd_out << "Adding getunit metatable...\n"; - lua_pushlightuserdata(L, getunitKey); + lua_pushlstring(L, getunitKey, sizeof(getunitKey)); lua_createtable(L, 0, 5); lua_pushcfunction(L, impl_unit_collect); lua_setfield(L, -2, "__gc"); @@ -548,7 +552,7 @@ namespace lua_units { // Create the unit status metatable. cmd_out << "Adding unit status metatable...\n"; - lua_pushlightuserdata(L, ustatusKey); + lua_pushlstring(L, ustatusKey, sizeof(ustatusKey)); lua_createtable(L, 0, 3); lua_pushcfunction(L, impl_unit_status_get); lua_setfield(L, -2, "__index"); @@ -561,7 +565,7 @@ namespace lua_units { // Create the unit variables metatable. cmd_out << "Adding unit variables metatable...\n"; - lua_pushlightuserdata(L, unitvarKey); + lua_pushlstring(L, unitvarKey, sizeof(unitvarKey)); lua_createtable(L, 0, 3); lua_pushcfunction(L, impl_unit_variables_get); lua_setfield(L, -2, "__index"); diff --git a/src/scripting/lua_unit.hpp b/src/scripting/lua_unit.hpp index a3a95d665ccc..b0e5481b43fa 100644 --- a/src/scripting/lua_unit.hpp +++ b/src/scripting/lua_unit.hpp @@ -17,7 +17,6 @@ #include #include -#include "lua_types.hpp" // the luatype typedef #include "scripting/lua_common.hpp" #include "units/ptr.hpp" diff --git a/src/scripting/lua_unit_attacks.cpp b/src/scripting/lua_unit_attacks.cpp index 4a580e234355..e51c117e3976 100644 --- a/src/scripting/lua_unit_attacks.cpp +++ b/src/scripting/lua_unit_attacks.cpp @@ -13,7 +13,6 @@ */ #include "lua_unit_attacks.hpp" -#include "lua_types.hpp" #include "scripting/lua_common.hpp" #include "scripting/lua_unit.hpp" @@ -22,13 +21,17 @@ #include "lua/lauxlib.h" #include "lua/lua.h" // for lua_State, lua_settop, etc +// These are arrays so we can use sizeof +static const char uattacksKey[] = "unit attacks table"; +static const char uattackKey[] = "unit attack"; + void push_unit_attacks_table(lua_State* L, int idx) { lua_createtable(L, 1, 0); lua_pushvalue(L, idx); // hack: store the unit_type at -1 because we want positive indices to refer to the attacks. lua_rawseti(L, -2, -1); - lua_pushlightuserdata(L, uattacksKey); + lua_pushlstring(L, uattacksKey, sizeof(uattacksKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_setmetatable(L, -2); } @@ -84,7 +87,7 @@ static int impl_unit_attacks_get(lua_State *L) // stack { lua_unit }, id/index, lua_unit, table, attack id lua_rawseti(L, -2, 2); // stack { lua_unit }, id/index, lua_unit, table - lua_pushlightuserdata(L, uattackKey); + lua_pushlstring(L, uattackKey, sizeof(uattackKey)); lua_rawget(L, LUA_REGISTRYINDEX); lua_setmetatable(L, -2); return 1; @@ -210,7 +213,7 @@ namespace lua_units { // Create the unit attacks metatable. cmd_out << "Adding unit attacks metatable...\n"; - lua_pushlightuserdata(L, uattacksKey); + lua_pushlstring(L, uattacksKey, sizeof(uattacksKey)); lua_createtable(L, 0, 3); lua_pushcfunction(L, impl_unit_attacks_get); lua_setfield(L, -2, "__index"); @@ -221,7 +224,7 @@ namespace lua_units { lua_rawset(L, LUA_REGISTRYINDEX); // Create the unit attack metatable - lua_pushlightuserdata(L, uattackKey); + lua_pushlstring(L, uattackKey, sizeof(uattackKey)); lua_createtable(L, 0, 3); lua_pushcfunction(L, impl_unit_attack_get); lua_setfield(L, -2, "__index"); diff --git a/src/units/unit.cpp b/src/units/unit.cpp index de65f2069204..3fe94ca53c5d 100644 --- a/src/units/unit.cpp +++ b/src/units/unit.cpp @@ -2383,11 +2383,11 @@ unit& unit::clone(bool is_temporary) } -unit_movement_resetter::unit_movement_resetter(unit &u, bool operate) : - u_(u), moves_(u.movement_left(true)) +unit_movement_resetter::unit_movement_resetter(const unit &u, bool operate) : + u_(const_cast(u)), moves_(u.movement_left(true)) { if (operate) { - u.set_movement(u.total_movement()); + u_.set_movement(u_.total_movement()); } } diff --git a/src/units/unit.hpp b/src/units/unit.hpp index 9513b63c5010..0f24404f56d1 100644 --- a/src/units/unit.hpp +++ b/src/units/unit.hpp @@ -548,7 +548,7 @@ class unit struct unit_movement_resetter : private boost::noncopyable { - unit_movement_resetter(unit& u, bool operate=true); + unit_movement_resetter(const unit& u, bool operate=true); ~unit_movement_resetter(); private: