widelands-dev team mailing list archive
-
widelands-dev team
-
Mailing list archive
-
Message #03555
[Merge] lp:~widelands-dev/widelands/split_lua_table into lp:widelands
SirVer has proposed merging lp:~widelands-dev/widelands/split_lua_table into lp:widelands.
Requested reviews:
Widelands Developers (widelands-dev)
For more details, see:
https://code.launchpad.net/~widelands-dev/widelands/split_lua_table/+merge/248192
Explode scripting into smaller parts for looser coupling.
--
Your team Widelands Developers is requested to review the proposed merge of lp:~widelands-dev/widelands/split_lua_table into lp:widelands.
=== modified file 'src/editor/CMakeLists.txt'
--- src/editor/CMakeLists.txt 2015-01-08 21:16:24 +0000
+++ src/editor/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -96,7 +96,8 @@
map_io_map_loader
profile
random
- scripting
+ scripting_lua_interface
+ scripting_lua_table
ui_basic
ui_fsmenu
widelands_ball_of_mud
=== modified file 'src/editor/editorinteractive.cc'
--- src/editor/editorinteractive.cc 2014-12-03 08:14:35 +0000
+++ src/editor/editorinteractive.cc 2015-01-31 16:06:41 +0000
@@ -44,8 +44,8 @@
#include "logic/world/world.h"
#include "map_io/widelands_map_loader.h"
#include "profile/profile.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
#include "ui_basic/messagebox.h"
#include "ui_basic/progresswindow.h"
#include "wlapplication.h"
=== modified file 'src/editor/map_generator.cc'
--- src/editor/map_generator.cc 2014-12-01 21:47:22 +0000
+++ src/editor/map_generator.cc 2015-01-31 16:06:41 +0000
@@ -29,8 +29,8 @@
#include "logic/map.h"
#include "logic/world/map_gen.h"
#include "logic/world/world.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
constexpr uint32_t kAverageElevation = 0x80000000;
constexpr uint32_t kMaxElevation = 0xffffffff;
=== modified file 'src/game_io/CMakeLists.txt'
--- src/game_io/CMakeLists.txt 2014-11-28 08:37:24 +0000
+++ src/game_io/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -35,7 +35,8 @@
map_io
map_io_map_loader
profile
- scripting
+ scripting_lua_interface
+ scripting_lua_table
wui
wui_mapview_pixelfunctions
wui_overlay_manager
=== modified file 'src/game_io/game_preload_packet.cc'
--- src/game_io/game_preload_packet.cc 2014-12-07 20:52:55 +0000
+++ src/game_io/game_preload_packet.cc 2015-01-31 16:06:41 +0000
@@ -32,8 +32,8 @@
#include "logic/map.h"
#include "logic/playersmanager.h"
#include "profile/profile.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
#include "wui/interactive_player.h"
#include "wui/mapviewpixelconstants.h"
#include "wui/mapviewpixelfunctions.h"
=== modified file 'src/graphic/CMakeLists.txt'
--- src/graphic/CMakeLists.txt 2014-12-14 14:38:37 +0000
+++ src/graphic/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -46,7 +46,6 @@
DEPENDS
base_log
base_macros
- graphic
graphic_image_io
graphic_surface
)
@@ -200,7 +199,7 @@
logic
notifications
profile
- scripting
+ scripting_lua_table
sound
wui_overlay_manager
)
=== modified file 'src/graphic/text/CMakeLists.txt'
--- src/graphic/text/CMakeLists.txt 2014-12-14 12:16:27 +0000
+++ src/graphic/text/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -21,6 +21,7 @@
DEPENDS
base_exceptions
base_geometry
+ base_i18n
base_log
base_macros
graphic_color
@@ -30,5 +31,6 @@
graphic_surface
io_fileread
io_filesystem
- scripting
+ scripting_lua_interface
+ scripting_lua_table
)
=== modified file 'src/graphic/text/font_set.cc'
--- src/graphic/text/font_set.cc 2014-12-06 09:07:19 +0000
+++ src/graphic/text/font_set.cc 2015-01-31 16:06:41 +0000
@@ -25,10 +25,11 @@
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
+#include "base/i18n.h"
#include "base/log.h"
#include "io/filesystem/layered_filesystem.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
namespace UI {
=== modified file 'src/logic/CMakeLists.txt'
--- src/logic/CMakeLists.txt 2014-12-06 12:22:35 +0000
+++ src/logic/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -12,7 +12,6 @@
io_filesystem
logic
map_io_map_loader
- scripting
)
wl_library(logic_widelands_geometry
@@ -230,10 +229,10 @@
game_io
graphic
graphic_color
+ graphic_image_io
+ graphic_surface
graphic_text
graphic_text_layout
- graphic_image_io
- graphic_surface
graphic_texture_atlas
helper
io_fileread
@@ -248,7 +247,10 @@
notifications
profile
random
- scripting
+ scripting_coroutine
+ scripting_logic
+ scripting_lua_interface
+ scripting_lua_table
sound
ui_basic
widelands_ball_of_mud
=== modified file 'src/logic/cmd_calculate_statistics.h'
--- src/logic/cmd_calculate_statistics.h 2014-09-19 12:54:54 +0000
+++ src/logic/cmd_calculate_statistics.h 2015-01-31 16:06:41 +0000
@@ -23,7 +23,6 @@
#include <string>
#include "logic/cmd_queue.h"
-#include "scripting/scripting.h"
namespace Widelands {
=== modified file 'src/logic/cmd_luacoroutine.cc'
--- src/logic/cmd_luacoroutine.cc 2014-09-30 07:55:22 +0000
+++ src/logic/cmd_luacoroutine.cc 2015-01-31 16:06:41 +0000
@@ -27,8 +27,8 @@
#include "logic/game_controller.h"
#include "logic/game_data_error.h"
#include "logic/player.h"
+#include "scripting/logic.h"
#include "scripting/lua_coroutine.h"
-#include "scripting/scripting.h"
namespace Widelands {
=== modified file 'src/logic/cmd_luacoroutine.h'
--- src/logic/cmd_luacoroutine.h 2014-09-19 12:54:54 +0000
+++ src/logic/cmd_luacoroutine.h 2015-01-31 16:06:41 +0000
@@ -24,7 +24,6 @@
#include "logic/cmd_queue.h"
#include "scripting/lua_coroutine.h"
-#include "scripting/scripting.h"
namespace Widelands {
=== modified file 'src/logic/cmd_luascript.cc'
--- src/logic/cmd_luascript.cc 2014-09-20 09:37:47 +0000
+++ src/logic/cmd_luascript.cc 2015-01-31 16:06:41 +0000
@@ -24,8 +24,8 @@
#include "io/filewrite.h"
#include "logic/game.h"
#include "logic/game_data_error.h"
+#include "scripting/logic.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
namespace Widelands {
=== modified file 'src/logic/editor_game_base.cc'
--- src/logic/editor_game_base.cc 2014-11-28 05:40:53 +0000
+++ src/logic/editor_game_base.cc 2015-01-31 16:06:41 +0000
@@ -47,8 +47,8 @@
#include "logic/ware_descr.h"
#include "logic/worker.h"
#include "logic/world/world.h"
+#include "scripting/logic.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
#include "sound/sound_handler.h"
#include "ui_basic/progresswindow.h"
#include "wui/interactive_base.h"
=== modified file 'src/logic/editor_game_base.h'
--- src/logic/editor_game_base.h 2014-09-19 12:54:54 +0000
+++ src/logic/editor_game_base.h 2015-01-31 16:06:41 +0000
@@ -31,11 +31,11 @@
#include "logic/map.h"
#include "logic/player_area.h"
#include "notifications/notifications.h"
+#include "scripting/lua_interface.h"
namespace UI {struct ProgressWindow;}
struct FullscreenMenuLaunchGame;
class InteractiveBase;
-class LuaInterface;
namespace Widelands {
@@ -190,7 +190,7 @@
}
/// Lua frontend, used to run Lua scripts
- LuaInterface& lua() {
+ virtual LuaInterface& lua() {
return *lua_;
}
=== modified file 'src/logic/game.cc'
--- src/logic/game.cc 2014-11-27 07:53:21 +0000
+++ src/logic/game.cc 2015-01-31 16:06:41 +0000
@@ -60,8 +60,8 @@
#include "map_io/widelands_map_loader.h"
#include "network/network.h"
#include "profile/profile.h"
+#include "scripting/logic.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
#include "sound/sound_handler.h"
#include "ui_basic/progresswindow.h"
#include "wlapplication.h"
@@ -907,6 +907,9 @@
(get_gametime(), ship.get_owner()->player_number(), ship.serial()));
}
+LuaGameInterface& Game::lua() {
+ return static_cast<LuaGameInterface&>(EditorGameBase::lua());
+}
/**
* Sample global statistics for the game.
=== modified file 'src/logic/game.h'
--- src/logic/game.h 2014-09-19 12:54:54 +0000
+++ src/logic/game.h 2015-01-31 16:06:41 +0000
@@ -26,6 +26,7 @@
#include "logic/editor_game_base.h"
#include "logic/save_handler.h"
#include "random/random.h"
+#include "scripting/logic.h"
namespace UI {struct ProgressWindow;}
struct ComputerPlayer;
@@ -107,6 +108,9 @@
enum StartGameType {NewSPScenario, NewNonScenario, Loaded, NewMPScenario};
bool run(UI::ProgressWindow * loader_ui, StartGameType, const std::string& script_to_run, bool replay);
+ // Returns the upcasted lua interface.
+ LuaGameInterface& lua() override;
+
// Run a single player scenario directly via --scenario on the cmdline. Will
// run the 'script_to_run' after any init scripts of the map.
// Returns the result of run().
=== modified file 'src/logic/map_info.cc'
--- src/logic/map_info.cc 2014-12-07 21:02:46 +0000
+++ src/logic/map_info.cc 2015-01-31 16:06:41 +0000
@@ -36,7 +36,6 @@
#include "logic/editor_game_base.h"
#include "logic/map.h"
#include "map_io/widelands_map_loader.h"
-#include "scripting/scripting.h"
using namespace Widelands;
=== modified file 'src/logic/player.cc'
--- src/logic/player.cc 2014-12-28 16:45:37 +0000
+++ src/logic/player.cc 2015-01-31 16:06:41 +0000
@@ -52,7 +52,6 @@
#include "logic/warehouse.h"
#include "profile/profile.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
#include "sound/sound_handler.h"
#include "wui/interactive_player.h"
=== modified file 'src/logic/terrain_affinity.cc'
--- src/logic/terrain_affinity.cc 2015-01-20 18:34:53 +0000
+++ src/logic/terrain_affinity.cc 2015-01-31 16:06:41 +0000
@@ -23,6 +23,7 @@
#include "logic/description_maintainer.h"
#include "logic/field.h"
+#include "logic/game_data_error.h"
#include "logic/map.h"
#include "logic/widelands_geometry.h"
#include "logic/world/terrain_description.h"
=== modified file 'src/logic/tribe.cc'
--- src/logic/tribe.cc 2014-11-28 09:02:24 +0000
+++ src/logic/tribe.cc 2015-01-31 16:06:41 +0000
@@ -50,8 +50,8 @@
#include "logic/world/resource_description.h"
#include "logic/world/world.h"
#include "profile/profile.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
using namespace std;
=== modified file 'src/logic/world/terrain_description.cc'
--- src/logic/world/terrain_description.cc 2015-01-20 18:34:53 +0000
+++ src/logic/world/terrain_description.cc 2015-01-31 16:06:41 +0000
@@ -23,6 +23,7 @@
#include <boost/format.hpp>
+#include "graphic/animation.h"
#include "graphic/graphic.h"
#include "graphic/texture.h"
#include "logic/game_data_error.h"
=== modified file 'src/map_io/CMakeLists.txt'
--- src/map_io/CMakeLists.txt 2014-12-04 20:54:13 +0000
+++ src/map_io/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -15,7 +15,7 @@
io_filesystem
logic
map_io
- scripting
+ scripting_lua_interface
)
# TODO(sirver): separate map_object_loader/saver into
@@ -101,6 +101,6 @@
logic
logic_widelands_geometry
profile
- scripting
+ scripting_logic
wui
)
=== modified file 'src/map_io/map_scripting_packet.cc'
--- src/map_io/map_scripting_packet.cc 2014-09-20 09:37:47 +0000
+++ src/map_io/map_scripting_packet.cc 2015-01-31 16:06:41 +0000
@@ -33,7 +33,7 @@
#include "logic/map.h"
#include "logic/world/world.h"
#include "profile/profile.h"
-#include "scripting/scripting.h"
+#include "scripting/logic.h"
namespace Widelands {
=== modified file 'src/map_io/s2map.cc'
--- src/map_io/s2map.cc 2014-12-01 21:47:22 +0000
+++ src/map_io/s2map.cc 2015-01-31 16:06:41 +0000
@@ -38,7 +38,7 @@
#include "logic/world/world.h"
#include "map_io/map_loader.h"
#include "map_io/one_world_legacy_lookup_table.h"
-#include "scripting/scripting.h"
+#include "scripting/lua_interface.h"
using std::cerr;
using std::endl;
=== modified file 'src/network/CMakeLists.txt'
--- src/network/CMakeLists.txt 2014-11-28 16:40:55 +0000
+++ src/network/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -41,7 +41,8 @@
logic_game_settings
map_io_map_loader
profile
- scripting
+ scripting_lua_interface
+ scripting_lua_table
ui_basic
ui_fsmenu
widelands_ball_of_mud
=== modified file 'src/network/netclient.cc'
--- src/network/netclient.cc 2014-10-11 16:03:18 +0000
+++ src/network/netclient.cc 2015-01-31 16:06:41 +0000
@@ -44,8 +44,8 @@
#include "network/network_protocol.h"
#include "network/network_system.h"
#include "profile/profile.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
#include "ui_basic/messagebox.h"
#include "ui_basic/progresswindow.h"
#include "ui_fsmenu/launch_mpg.h"
=== modified file 'src/network/nethost.cc'
--- src/network/nethost.cc 2014-11-28 09:02:24 +0000
+++ src/network/nethost.cc 2015-01-31 16:06:41 +0000
@@ -57,7 +57,7 @@
#include "network/network_protocol.h"
#include "network/network_system.h"
#include "profile/profile.h"
-#include "scripting/scripting.h"
+#include "scripting/lua_interface.h"
#include "ui_basic/progresswindow.h"
#include "ui_fsmenu/launch_mpg.h"
#include "wlapplication.h"
=== modified file 'src/scripting/CMakeLists.txt'
--- src/scripting/CMakeLists.txt 2014-12-06 12:22:35 +0000
+++ src/scripting/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -1,50 +1,109 @@
add_subdirectory(test)
-# TODO(sirver): Separate lua_table and lua_coroutine if possible.
-wl_library(scripting
- SRCS
- c_utils.cc
- c_utils.h
- eris.h
+wl_library(scripting_base
+ SRCS
+ lua.h
+ lua.cc
+ DEPENDS
+ third_party_eris
+)
+
+wl_library(scripting_errors
+ SRCS
+ lua_errors.cc
+ lua_errors.h
+ report_error.cc
+ report_error.h
+ DEPENDS
+ base_exceptions
+ scripting_base
+)
+
+wl_library(scripting_luna
+ SRCS
+ luna.h
+ luna_impl.cc
+ luna_impl.h
+ DEPENDS
+ base_log
+ scripting_base
+ scripting_errors
+)
+
+wl_library(scripting_lua_table
+ SRCS
+ lua_table.cc
+ lua_table.h
+ DEPENDS
+ base_log
+ scripting_base
+ scripting_coroutine
+ scripting_errors
+)
+
+wl_library(scripting_coroutine
+ SRCS
+ lua_coroutine.cc
+ lua_coroutine.h
+ DEPENDS
+ io_fileread
+ scripting_base
+ scripting_errors
+ # TODO(sirver): Cyclic dependency. introduce a seam so that logic can
+ # push/pull new parameters to coroutines.
+ scripting_logic
+)
+
+wl_library(scripting_lua_interface
+ SRCS
+ lua_globals.cc
+ lua_globals.h
+ lua_interface.cc
+ lua_interface.h
+ lua_path.cc
+ lua_path.h
+ run_script.cc
+ run_script.h
+ USES_BOOST_REGEX
+ DEPENDS
+ base_i18n
+ build_info
+ helper
+ io_filesystem
+ scripting_base
+ scripting_errors
+ scripting_lua_table
+ scripting_luna
+)
+
+wl_library(scripting_logic
+ SRCS
factory.cc
factory.h
+ globals.cc
+ globals.h
+ logic.cc
+ logic.h
lua_bases.cc
lua_bases.h
- lua_coroutine.cc
- lua_coroutine.h
lua_editor.cc
lua_editor.h
- lua_errors.h
lua_game.cc
lua_game.h
- lua_globals.cc
- lua_globals.h
lua_map.cc
lua_map.h
- lua_path.cc
- lua_path.h
lua_root.cc
lua_root.h
- lua_table.cc
- lua_table.h
lua_ui.cc
lua_ui.h
- luna.h
- luna_impl.cc
- luna_impl.h
persistence.cc
persistence.h
- scripting.cc
- scripting.h
- USES_BOOST_REGEX
+ eris.h
DEPENDS
- base_exceptions
base_log
base_macros
- build_info
economy
graphic
- helper
io_fileread
io_filesystem
logic
@@ -53,6 +112,12 @@
logic_widelands_geometry
map_io
profile
+ scripting_base
+ scripting_coroutine
+ scripting_errors
+ scripting_lua_interface
+ scripting_lua_table
+ scripting_luna
third_party_eris
ui_basic
wui
=== modified file 'src/scripting/factory.cc'
--- src/scripting/factory.cc 2014-09-10 17:52:49 +0000
+++ src/scripting/factory.cc 2015-01-31 16:06:41 +0000
@@ -30,3 +30,13 @@
to_lua<LuaGame::LuaPlayer>(L, new LuaGame::LuaPlayer(plr));
}
+Factory & get_factory(lua_State * const L) {
+ lua_getfield(L, LUA_REGISTRYINDEX, "factory");
+ Factory * fac = static_cast<Factory *>(lua_touserdata(L, -1));
+ lua_pop(L, 1); // pop this userdata
+
+ if (!fac)
+ throw LuaError("\"factory\" field was nil, which should be impossible!");
+
+ return *fac;
+}
=== modified file 'src/scripting/factory.h'
--- src/scripting/factory.h 2014-09-10 08:55:04 +0000
+++ src/scripting/factory.h 2015-01-31 16:06:41 +0000
@@ -22,7 +22,7 @@
#include "logic/widelands.h"
-#include "third_party/eris/lua.hpp"
+#include "scripting/lua.h"
/*
* Class to create the correct type for types shared between Editor and Game.
@@ -48,4 +48,6 @@
void push_player(lua_State * L, Widelands::PlayerNumber plr) override;
};
+Factory & get_factory(lua_State * const L);
+
#endif // end of include guard: WL_SCRIPTING_FACTORY_H
=== added file 'src/scripting/globals.cc'
--- src/scripting/globals.cc 1970-01-01 00:00:00 +0000
+++ src/scripting/globals.cc 2015-01-31 16:06:41 +0000
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2006-2015 by the Widelands Development Team
+ *
+ * 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; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include "scripting/globals.h"
+
+Widelands::Game & get_game(lua_State * const L) {
+ lua_getfield(L, LUA_REGISTRYINDEX, "game");
+ Widelands::Game * g = static_cast<Widelands::Game *>(lua_touserdata(L, -1));
+ lua_pop(L, 1); // pop this userdata
+
+ if (!g)
+ throw LuaError
+ ("\"game\" field was nil. get_game was not called in a game.");
+
+ return *g;
+}
+
+Widelands::EditorGameBase & get_egbase(lua_State * const L) {
+ lua_getfield(L, LUA_REGISTRYINDEX, "egbase");
+ Widelands::EditorGameBase * g = static_cast<Widelands::EditorGameBase *>
+ (lua_touserdata(L, -1));
+ lua_pop(L, 1); // pop this userdata
+
+ if (!g)
+ throw LuaError
+ ("\"egbase\" field was nil. This should be impossible.");
+
+
+ return *g;
+}
+
+Widelands::MapObjectLoader * get_mol(lua_State * const L) {
+ lua_pushstring(L, "mol");
+ lua_gettable(L, LUA_REGISTRYINDEX);
+
+ Widelands::MapObjectLoader * mol =
+ static_cast<Widelands::MapObjectLoader *>(lua_touserdata(L, -1));
+
+ lua_pop(L, 1); // pop this userdata
+
+ if (!mol)
+ throw LuaError
+ ("\"mol\" field was nil. This should be impossible.");
+
+ return mol;
+}
+
+Widelands::MapObjectSaver * get_mos(lua_State * const L) {
+ lua_pushstring(L, "mos");
+ lua_gettable(L, LUA_REGISTRYINDEX);
+
+ Widelands::MapObjectSaver * mos =
+ static_cast<Widelands::MapObjectSaver *>(lua_touserdata(L, -1));
+
+ lua_pop(L, 1); // pop this userdata
+
+ if (!mos)
+ throw LuaError
+ ("\"mos\" field was nil. This should be impossible.");
+
+ return mos;
+}
=== added file 'src/scripting/globals.h'
--- src/scripting/globals.h 1970-01-01 00:00:00 +0000
+++ src/scripting/globals.h 2015-01-31 16:06:41 +0000
@@ -0,0 +1,32 @@
+/*
+ * Copyright (C) 2006-2015 by the Widelands Development Team
+ *
+ * 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; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#ifndef WL_SCRIPTING_GLOBALS_H
+#define WL_SCRIPTING_GLOBALS_H
+
+#include "logic/game.h"
+#include "map_io/map_object_loader.h"
+#include "map_io/map_object_saver.h"
+
+Widelands::Game & get_game(lua_State *);
+Widelands::EditorGameBase & get_egbase(lua_State *);
+Widelands::MapObjectLoader * get_mol(lua_State *);
+Widelands::MapObjectSaver * get_mos(lua_State *);
+
+#endif // end of include guard: WL_SCRIPTING_GLOBALS_H
=== added file 'src/scripting/logic.cc'
--- src/scripting/logic.cc 1970-01-01 00:00:00 +0000
+++ src/scripting/logic.cc 2015-01-31 16:06:41 +0000
@@ -0,0 +1,243 @@
+/*
+ * Copyright (C) 2006-2015 by the Widelands Development Team
+ *
+ * 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; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include "scripting/logic.h"
+
+#include <memory>
+
+#include <boost/algorithm/string/predicate.hpp>
+
+#include "io/filesystem/layered_filesystem.h"
+#include "scripting/factory.h"
+#include "scripting/globals.h"
+#include "scripting/lua_bases.h"
+#include "scripting/lua_coroutine.h"
+#include "scripting/lua_editor.h"
+#include "scripting/lua_game.h"
+#include "scripting/lua_globals.h"
+#include "scripting/lua_map.h"
+#include "scripting/lua_root.h"
+#include "scripting/lua_table.h"
+#include "scripting/lua_ui.h"
+#include "scripting/persistence.h"
+#include "scripting/run_script.h"
+
+namespace {
+
+// Setup the basic Widelands functions and pushes egbase into the Lua registry
+// so that it is available for all the other Lua functions.
+void setup_for_editor_and_game(lua_State* L, Widelands::EditorGameBase * g) {
+ LuaBases::luaopen_wlbases(L);
+ LuaMaps::luaopen_wlmap(L);
+ LuaUi::luaopen_wlui(L);
+
+ // Push the editor game base
+ lua_pushlightuserdata(L, static_cast<void *>(g));
+ lua_setfield(L, LUA_REGISTRYINDEX, "egbase");
+}
+
+// Can run script also from the map.
+std::unique_ptr<LuaTable> run_script_maybe_from_map(lua_State* L, const std::string& path) {
+ if (boost::starts_with(path, "map:")) {
+ return run_script(L, path.substr(4), get_egbase(L).map().filesystem());
+ }
+ return run_script(L, path, g_fs);
+}
+
+} // namespace
+
+LuaEditorInterface::LuaEditorInterface(Widelands::EditorGameBase* g)
+ : m_factory(new EditorFactory())
+{
+ setup_for_editor_and_game(m_L, g);
+ LuaRoot::luaopen_wlroot(m_L, true);
+ LuaEditor::luaopen_wleditor(m_L);
+
+ // Push the factory class into the registry
+ lua_pushlightuserdata(m_L, reinterpret_cast<void*>(dynamic_cast<Factory*>(m_factory.get())));
+ lua_setfield(m_L, LUA_REGISTRYINDEX, "factory");
+}
+
+LuaEditorInterface::~LuaEditorInterface() {
+}
+
+std::unique_ptr<LuaTable> LuaEditorInterface::run_script(const std::string& script) {
+ return run_script_maybe_from_map(m_L, script);
+}
+
+// Special handling of math.random.
+
+// We inject this function to make sure that Lua uses our random number
+// generator. This guarantees that the game stays in sync over the network and
+// in replays. Obviously, we only do this for LuaGameInterface, not for
+// the others.
+
+// The function was designed to simulate the standard math.random function and
+// was therefore copied nearly verbatim from the Lua sources.
+static int L_math_random(lua_State * L) {
+ Widelands::Game & game = get_game(L);
+ uint32_t t = game.logic_rand();
+
+ lua_Number r = t / 4294967296.; // create a double in [0,1)
+
+ switch (lua_gettop(L)) { /* check number of arguments */
+ case 0:
+ { /* no arguments */
+ lua_pushnumber(L, r); /* Number between 0 and 1 */
+ break;
+ }
+ case 1:
+ { /* only upper limit */
+ int32_t u = luaL_checkint32(L, 1);
+ luaL_argcheck(L, 1 <= u, 1, "interval is empty");
+ lua_pushnumber(L, floor(r * u) + 1); /* int between 1 and `u' */
+ break;
+ }
+ case 2:
+ { /* lower and upper limits */
+ int32_t l = luaL_checkint32(L, 1);
+ int32_t u = luaL_checkint32(L, 2);
+ luaL_argcheck(L, l <= u, 2, "interval is empty");
+ /* int between `l' and `u' */
+ lua_pushnumber(L, floor(r * (u - l + 1)) + l);
+ break;
+ }
+ default: return luaL_error(L, "wrong number of arguments");
+ }
+ return 1;
+
+}
+
+LuaGameInterface::LuaGameInterface(Widelands::Game * g)
+ : m_factory(new GameFactory())
+{
+ setup_for_editor_and_game(m_L, g);
+
+ // Overwrite math.random
+ lua_getglobal(m_L, "math");
+ lua_pushcfunction(m_L, L_math_random);
+ lua_setfield(m_L, -2, "random");
+ lua_pop(m_L, 1); // pop "math"
+
+ LuaRoot::luaopen_wlroot(m_L, false);
+ LuaGame::luaopen_wlgame(m_L);
+
+ // Push the game into the registry
+ lua_pushlightuserdata(m_L, static_cast<void *>(g));
+ lua_setfield(m_L, LUA_REGISTRYINDEX, "game");
+
+ // Push the factory class into the registry
+ lua_pushlightuserdata
+ (m_L, reinterpret_cast<void *>(dynamic_cast<Factory *>(m_factory.get())));
+ lua_setfield(m_L, LUA_REGISTRYINDEX, "factory");
+}
+
+LuaGameInterface::~LuaGameInterface() {
+}
+
+LuaCoroutine* LuaGameInterface::read_coroutine(FileRead& fr) {
+ LuaCoroutine * rv = new LuaCoroutine(nullptr);
+ rv->read(m_L, fr);
+ return rv;
+}
+
+void LuaGameInterface::write_coroutine(FileWrite& fw, LuaCoroutine* cr) {
+ cr->write(fw);
+}
+
+
+void LuaGameInterface::read_global_env
+ (FileRead & fr, Widelands::MapObjectLoader & mol,
+ uint32_t size)
+{
+ // Clean out the garbage before loading.
+ lua_gc(m_L, LUA_GCCOLLECT, 0);
+
+ assert(lua_gettop(m_L) == 0); // S:
+ unpersist_object(m_L, fr, mol, size);
+ assert(lua_gettop(m_L) == 1); // S: unpersisted_object
+ luaL_checktype(m_L, -1, LUA_TTABLE);
+
+ // Now, we have to merge all keys from the loaded table
+ // into the global table
+ lua_pushnil(m_L); // S: table nil
+ while (lua_next(m_L, 1) != 0) {
+ // S: table key value
+ lua_rawgeti(m_L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); // S: table key value globals_table
+ lua_pushvalue(m_L, -3); // S: table key value globals_table key
+ lua_gettable(m_L, -2); // S: table key value globals_table value_in_globals
+ if (lua_compare(m_L, -1, -3, LUA_OPEQ)) {
+ lua_pop(m_L, 3); // S: table key
+ continue;
+ } else {
+ // Make this a global value
+ lua_pop(m_L, 1); // S: table key value globals_table
+ lua_pushvalue(m_L, -3); // S: table key value globals_table key
+ lua_pushvalue(m_L, -3); // S: table key value globals_table key value
+ lua_settable(m_L, -3); // S: table key value globals_table
+ lua_pop(m_L, 2); // S: table key
+ }
+ }
+
+ lua_pop(m_L, 1); // pop the table returned by unpersist_object
+
+ // Clean out the garbage before returning.
+ lua_gc(m_L, LUA_GCCOLLECT, 0);
+}
+
+uint32_t LuaGameInterface::write_global_env
+ (FileWrite & fw, Widelands::MapObjectSaver & mos)
+{
+ // Clean out the garbage before writing.
+ lua_gc(m_L, LUA_GCCOLLECT, 0);
+
+ // Empty table + object to persist on the stack Stack
+ lua_newtable(m_L);
+ lua_rawgeti(m_L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
+
+ uint32_t nwritten = persist_object(m_L, fw, mos);
+
+ // Garbage collect once more, so we do not return unnecessary stuff.
+ lua_gc(m_L, LUA_GCCOLLECT, 0);
+
+ return nwritten;
+}
+
+std::unique_ptr<LuaTable> LuaGameInterface::get_hook(const std::string& name) {
+ lua_getglobal(m_L, "hooks");
+ if (lua_isnil(m_L, -1)) {
+ lua_pop(m_L, 1);
+ return std::unique_ptr<LuaTable>();
+ }
+
+ lua_getfield(m_L, -1, name.c_str());
+ if (lua_isnil(m_L, -1)) {
+ lua_pop(m_L, 2);
+ return std::unique_ptr<LuaTable>();
+ }
+ lua_remove(m_L, -2);
+
+ std::unique_ptr<LuaTable> return_value(new LuaTable(m_L));
+ lua_pop(m_L, 1);
+ return return_value;
+}
+
+std::unique_ptr<LuaTable> LuaGameInterface::run_script(const std::string& script) {
+ return run_script_maybe_from_map(m_L, script);
+}
=== added file 'src/scripting/logic.h'
--- src/scripting/logic.h 1970-01-01 00:00:00 +0000
+++ src/scripting/logic.h 2015-01-31 16:06:41 +0000
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2006-2015 by the Widelands Development Team
+ *
+ * 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; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#ifndef WL_SCRIPTING_LOGIC_H
+#define WL_SCRIPTING_LOGIC_H
+
+#include <memory>
+
+#include "logic/editor_game_base.h"
+#include "scripting/lua_coroutine.h"
+#include "scripting/lua_interface.h"
+
+class EditorFactory;
+class GameFactory;
+
+class LuaEditorInterface : public LuaInterface {
+public:
+ LuaEditorInterface(Widelands::EditorGameBase * g);
+ virtual ~LuaEditorInterface();
+
+ std::unique_ptr<LuaTable> run_script(const std::string& script) override;
+
+private:
+ std::unique_ptr<EditorFactory> m_factory;
+};
+
+class LuaGameInterface : public LuaInterface {
+public:
+ LuaGameInterface(Widelands::Game * g);
+ virtual ~LuaGameInterface();
+
+ // Returns a given hook if one is defined, otherwise returns 0
+ std::unique_ptr<LuaTable> get_hook(const std::string& name);
+
+ std::unique_ptr<LuaTable> run_script(const std::string& script) override;
+
+ // Input/output for coroutines.
+ LuaCoroutine* read_coroutine(FileRead&);
+ void write_coroutine(FileWrite&, LuaCoroutine*);
+
+ // Input output for the global game state.
+ void read_global_env
+ (FileRead &, Widelands::MapObjectLoader &, uint32_t);
+ uint32_t write_global_env
+ (FileWrite &, Widelands::MapObjectSaver &);
+
+private:
+ std::unique_ptr<GameFactory> m_factory;
+};
+
+#endif // end of include guard: WL_SCRIPTING_LOGIC_H
=== added file 'src/scripting/lua.cc'
--- src/scripting/lua.cc 1970-01-01 00:00:00 +0000
+++ src/scripting/lua.cc 2015-01-31 16:06:41 +0000
@@ -0,0 +1,36 @@
+/*
+ * Copyright (C) 2006-2015 by the Widelands Development Team
+ *
+ * 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; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include "scripting/lua.h"
+
+void lua_pushstring (lua_State * L, const std::string & s) {
+ lua_pushstring(L, s.c_str());
+}
+
+lua_State * luaL_checkthread(lua_State * L, int n) {
+ luaL_checktype(L, n, LUA_TTHREAD);
+ lua_State * thread = lua_tothread(L, n);
+ return thread;
+}
+
+bool luaL_checkboolean(lua_State * L, int n) {
+ if (lua_isboolean(L, n))
+ return lua_toboolean(L, n);
+ return luaL_checkinteger(L, n);
+}
=== added file 'src/scripting/lua.h'
--- src/scripting/lua.h 1970-01-01 00:00:00 +0000
+++ src/scripting/lua.h 2015-01-31 16:06:41 +0000
@@ -0,0 +1,39 @@
+/*
+ * Copyright (C) 2006-2015 by the Widelands Development Team
+ *
+ * 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; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#ifndef WL_SCRIPTING_LUA_H
+#define WL_SCRIPTING_LUA_H
+
+#include <string>
+
+#include "third_party/eris/lua.hpp"
+
+#define luaL_checkint32(L, n) static_cast<int32_t>(luaL_checkinteger(L, (n)))
+#define luaL_checkuint32(L, n) static_cast<uint32_t>(luaL_checkinteger(L, (n)))
+
+#define lua_pushint32(L, n) (lua_pushinteger(L, static_cast<int32_t>(n)))
+#define lua_pushuint32(L, n) (lua_pushinteger(L, static_cast<uint32_t>(n)))
+
+void lua_pushstring (lua_State * L, const std::string & s);
+
+lua_State * luaL_checkthread(lua_State * L, int n);
+
+bool luaL_checkboolean(lua_State * L, int n);
+
+#endif // end of include guard: WL_SCRIPTING_LUA_H
=== modified file 'src/scripting/lua_bases.cc'
--- src/scripting/lua_bases.cc 2014-10-27 10:14:10 +0000
+++ src/scripting/lua_bases.cc 2015-01-31 16:06:41 +0000
@@ -27,6 +27,8 @@
#include "logic/player.h"
#include "logic/tribe.h"
#include "logic/ware_descr.h"
+#include "scripting/factory.h"
+#include "scripting/globals.h"
#include "scripting/lua_map.h"
=== modified file 'src/scripting/lua_bases.h'
--- src/scripting/lua_bases.h 2014-09-10 17:52:49 +0000
+++ src/scripting/lua_bases.h 2015-01-31 16:06:41 +0000
@@ -20,8 +20,10 @@
#ifndef WL_SCRIPTING_LUA_BASES_H
#define WL_SCRIPTING_LUA_BASES_H
+#include "logic/editor_game_base.h"
+#include "logic/player.h"
+#include "scripting/lua.h"
#include "scripting/luna.h"
-#include "third_party/eris/lua.hpp"
namespace LuaBases {
@@ -108,7 +110,7 @@
/*
* C methods
*/
- Widelands::Player & get(lua_State * L, Widelands::EditorGameBase &);
+ Widelands::Player& get(lua_State* L, Widelands::EditorGameBase&);
protected:
inline Widelands::PlayerNumber player_number() {return m_pl;}
=== modified file 'src/scripting/lua_coroutine.cc'
--- src/scripting/lua_coroutine.cc 2014-09-20 09:37:47 +0000
+++ src/scripting/lua_coroutine.cc 2015-01-31 16:06:41 +0000
@@ -21,12 +21,9 @@
#include "io/fileread.h"
#include "io/filewrite.h"
-#include "scripting/c_utils.h"
#include "scripting/lua_errors.h"
#include "scripting/lua_game.h"
#include "scripting/lua_map.h"
-#include "scripting/luna.h"
-#include "scripting/persistence.h"
namespace {
=== modified file 'src/scripting/lua_coroutine.h'
--- src/scripting/lua_coroutine.h 2014-07-28 16:59:54 +0000
+++ src/scripting/lua_coroutine.h 2015-01-31 16:06:41 +0000
@@ -24,7 +24,7 @@
#include <stdint.h>
-#include "third_party/eris/lua.hpp"
+#include "scripting/lua.h"
class FileRead;
class FileWrite;
=== modified file 'src/scripting/lua_editor.cc'
--- src/scripting/lua_editor.cc 2014-09-10 17:52:49 +0000
+++ src/scripting/lua_editor.cc 2015-01-31 16:06:41 +0000
@@ -22,7 +22,6 @@
#include "base/log.h"
#include "logic/game.h"
#include "logic/player.h"
-#include "scripting/c_utils.h"
#include "wui/interactive_player.h"
namespace LuaEditor {
=== modified file 'src/scripting/lua_editor.h'
--- src/scripting/lua_editor.h 2014-09-10 17:52:49 +0000
+++ src/scripting/lua_editor.h 2015-01-31 16:06:41 +0000
@@ -20,8 +20,8 @@
#ifndef WL_SCRIPTING_LUA_EDITOR_H
#define WL_SCRIPTING_LUA_EDITOR_H
+#include "scripting/lua.h"
#include "scripting/lua_bases.h"
-#include "third_party/eris/lua.hpp"
namespace LuaEditor {
=== added file 'src/scripting/lua_errors.cc'
--- src/scripting/lua_errors.cc 1970-01-01 00:00:00 +0000
+++ src/scripting/lua_errors.cc 2015-01-31 16:06:41 +0000
@@ -0,0 +1,27 @@
+/*
+ * Copyright (C) 2006-2015 by the Widelands Development Team
+ *
+ * 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; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include "scripting/lua_errors.h"
+
+LuaError::LuaError(const std::string& reason) : wexception("%s", reason.c_str()) {
+}
+
+LuaScriptNotExistingError::LuaScriptNotExistingError(const std::string& name)
+ : LuaError("The script '" + name + "' was not found!") {
+}
=== modified file 'src/scripting/lua_errors.h'
--- src/scripting/lua_errors.h 2014-09-09 17:15:20 +0000
+++ src/scripting/lua_errors.h 2015-01-31 16:06:41 +0000
@@ -26,15 +26,12 @@
class LuaError : public WException {
public:
- LuaError(const std::string& reason) : wexception("%s", reason.c_str()) {
- }
+ LuaError(const std::string& reason);
};
class LuaScriptNotExistingError : public LuaError {
public:
- LuaScriptNotExistingError(const std::string& name)
- : LuaError("The script '" + name + "' was not found!") {
- }
+ LuaScriptNotExistingError(const std::string& name);
};
#endif // end of include guard: WL_SCRIPTING_LUA_ERRORS_H
=== modified file 'src/scripting/lua_game.cc'
--- src/scripting/lua_game.cc 2014-12-28 16:45:37 +0000
+++ src/scripting/lua_game.cc 2015-01-31 16:06:41 +0000
@@ -35,9 +35,9 @@
#include "logic/playersmanager.h"
#include "logic/tribe.h"
#include "profile/profile.h"
-#include "scripting/c_utils.h"
+#include "scripting/globals.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_map.h"
-#include "scripting/scripting.h"
#include "wui/interactive_player.h"
#include "wui/story_message_box.h"
@@ -125,7 +125,7 @@
(RO) The name of this Player.
*/
int LuaPlayer::get_name(lua_State * L) {
- Game & game = get_game(L);
+ Game& game = get_game(L);
Player & p = get(L, game);
lua_pushstring(L, p.get_name());
return 1;
=== modified file 'src/scripting/lua_game.h'
--- src/scripting/lua_game.h 2014-09-29 12:37:07 +0000
+++ src/scripting/lua_game.h 2015-01-31 16:06:41 +0000
@@ -22,9 +22,9 @@
#include "logic/building.h"
#include "logic/message_id.h"
+#include "scripting/lua.h"
#include "scripting/lua_bases.h"
#include "scripting/luna.h"
-#include "third_party/eris/lua.hpp"
namespace Widelands {
struct TribeDescr;
=== modified file 'src/scripting/lua_globals.cc'
--- src/scripting/lua_globals.cc 2014-11-18 19:30:39 +0000
+++ src/scripting/lua_globals.cc 2015-01-31 16:06:41 +0000
@@ -23,12 +23,12 @@
#include <boost/format.hpp>
+#include "base/i18n.h"
#include "build_info.h"
#include "io/filesystem/layered_filesystem.h"
-#include "logic/game.h"
-#include "scripting/c_utils.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
+#include "scripting/report_error.h"
namespace LuaGlobals {
@@ -103,7 +103,8 @@
case LUA_TUSERDATA:
case LUA_TTHREAD:
case LUA_TLIGHTUSERDATA:
- report_error(L, "Cannot format the given type %s at index %i", lua_typename(L, i), i);
+ report_error(
+ L, "Cannot format the given type %s at index %i", lua_typename(L, i), i);
}
}
=== modified file 'src/scripting/lua_globals.h'
--- src/scripting/lua_globals.h 2014-07-05 16:41:51 +0000
+++ src/scripting/lua_globals.h 2015-01-31 16:06:41 +0000
@@ -20,11 +20,12 @@
#ifndef WL_SCRIPTING_LUA_GLOBALS_H
#define WL_SCRIPTING_LUA_GLOBALS_H
-#include "third_party/eris/lua.hpp"
+#include "scripting/lua.h"
namespace LuaGlobals {
void luaopen_globals(lua_State *);
+}
+
#endif // end of include guard: WL_SCRIPTING_LUA_GLOBALS_H
-}
=== renamed file 'src/scripting/scripting.cc' => 'src/scripting/lua_interface.cc'
--- src/scripting/scripting.cc 2015-01-26 06:55:05 +0000
+++ src/scripting/lua_interface.cc 2015-01-31 16:06:41 +0000
@@ -17,33 +17,18 @@
*
*/
-#include "scripting/scripting.h"
+#include "scripting/lua_interface.h"
#include <memory>
-#include <stdexcept>
#include <string>
#include <boost/algorithm/string/predicate.hpp>
-#ifdef _MSC_VER
-#include <ctype.h> // for tolower
-#endif
-#include <stdint.h>
-#include "base/log.h"
#include "io/filesystem/layered_filesystem.h"
-#include "scripting/c_utils.h"
-#include "scripting/factory.h"
-#include "scripting/lua_bases.h"
-#include "scripting/lua_coroutine.h"
-#include "scripting/lua_editor.h"
-#include "scripting/lua_game.h"
#include "scripting/lua_globals.h"
-#include "scripting/lua_map.h"
#include "scripting/lua_path.h"
-#include "scripting/lua_root.h"
#include "scripting/lua_table.h"
-#include "scripting/lua_ui.h"
-#include "scripting/persistence.h"
+#include "scripting/run_script.h"
namespace {
@@ -63,91 +48,9 @@
}
}
-// Checks the return value of a function all for nonzero state and throws the
-// string that the function hopefully pushed as an Error. Returns 'rv' if there
-// is no error.
-int check_return_value_for_errors(lua_State* L, int rv) {
- if (rv) {
- const std::string err = luaL_checkstring(L, -1);
- lua_pop(L, 1);
- throw LuaError(err);
- }
- return rv;
-}
-
-// Setup the basic Widelands functions and pushes egbase into the Lua registry
-// so that it is available for all the other Lua functions.
-void setup_for_editor_and_game(lua_State* L, Widelands::EditorGameBase * g) {
- LuaBases::luaopen_wlbases(L);
- LuaMaps::luaopen_wlmap(L);
- LuaUi::luaopen_wlui(L);
-
- // Push the editor game base
- lua_pushlightuserdata(L, static_cast<void *>(g));
- lua_setfield(L, LUA_REGISTRYINDEX, "egbase");
-}
-
-// Runs the 'content' as a lua script identified by 'identifier' in 'L'.
-std::unique_ptr<LuaTable>
-run_string_as_script(lua_State* L, const std::string& identifier, const std::string& content) {
- // Get the current value of __file__
- std::string last_file;
- lua_getglobal(L, "__file__");
- if (!lua_isnil(L, -1)) {
- last_file = luaL_checkstring(L, -1);
- }
- lua_pop(L, 1);
-
- // Set __file__.
- lua_pushstring(L, identifier);
- lua_setglobal(L, "__file__");
-
- check_return_value_for_errors(
- L,
- luaL_loadbuffer(L, content.c_str(), content.size(), identifier.c_str()) ||
- lua_pcall(L, 0, 1, 0));
-
- if (lua_isnil(L, -1)) {
- lua_pop(L, 1); // No return value from script
- lua_newtable(L); // Push an empty table
- }
- if (!lua_istable(L, -1))
- throw LuaError("Script did not return a table!");
-
- // Restore old value of __file__.
- if (last_file.empty()) {
- lua_pushnil(L);
- } else {
- lua_pushstring(L, last_file);
- }
- lua_setglobal(L, "__file__");
-
- std::unique_ptr<LuaTable> return_value(new LuaTable(L));
- lua_pop(L, 1);
- return return_value;
-}
-
-// Reads the 'filename' from the 'fs' and returns its content.
-std::string get_file_content(FileSystem* fs, const std::string& filename) {
- if (!fs || !fs->file_exists(filename)) {
- throw LuaScriptNotExistingError(filename);
- }
- size_t length;
- void* input_data = fs->load(filename, length);
- const std::string data(static_cast<char*>(input_data));
- // make sure the input_data is freed
- free(input_data);
- return data;
-}
-
} // namespace
-/*
-============================================
- Lua Interface
-============================================
-*/
LuaInterface::LuaInterface() {
m_L = luaL_newstate();
@@ -187,201 +90,5 @@
}
std::unique_ptr<LuaTable> LuaInterface::run_script(const std::string& path) {
- std::string content;
-
- if (boost::starts_with(path, "map:")) {
- content = get_file_content(get_egbase(m_L).map().filesystem(), path.substr(4));
- } else {
- content = get_file_content(g_fs, path);
- }
-
- return run_string_as_script(m_L, path, content);
-}
-
-/*
- * Returns a given hook if one is defined, otherwise returns 0
- */
-std::unique_ptr<LuaTable> LuaInterface::get_hook(const std::string& name) {
- lua_getglobal(m_L, "hooks");
- if (lua_isnil(m_L, -1)) {
- lua_pop(m_L, 1);
- return std::unique_ptr<LuaTable>();
- }
-
- lua_getfield(m_L, -1, name.c_str());
- if (lua_isnil(m_L, -1)) {
- lua_pop(m_L, 2);
- return std::unique_ptr<LuaTable>();
- }
- lua_remove(m_L, -2);
-
- std::unique_ptr<LuaTable> return_value(new LuaTable(m_L));
- lua_pop(m_L, 1);
- return return_value;
-}
-
-
-/*
- * ===========================
- * LuaEditorInterface
- * ===========================
- */
-LuaEditorInterface::LuaEditorInterface(Widelands::EditorGameBase* g)
- : m_factory(new EditorFactory())
-{
- setup_for_editor_and_game(m_L, g);
- LuaRoot::luaopen_wlroot(m_L, true);
- LuaEditor::luaopen_wleditor(m_L);
-
- // Push the factory class into the registry
- lua_pushlightuserdata(m_L, reinterpret_cast<void*>(dynamic_cast<Factory*>(m_factory.get())));
- lua_setfield(m_L, LUA_REGISTRYINDEX, "factory");
-}
-
-LuaEditorInterface::~LuaEditorInterface() {
-}
-
-
-/*
- * ===========================
- * LuaGameInterface
- * ===========================
- */
-
-// Special handling of math.random.
-
-// We inject this function to make sure that Lua uses our random number
-// generator. This guarantees that the game stays in sync over the network and
-// in replays. Obviously, we only do this for LuaGameInterface, not for
-// the others.
-
-// The function was designed to simulate the standard math.random function and
-// was therefore copied nearly verbatim from the Lua sources.
-static int L_math_random(lua_State * L) {
- Widelands::Game & game = get_game(L);
- uint32_t t = game.logic_rand();
-
- lua_Number r = t / 4294967296.; // create a double in [0,1)
-
- switch (lua_gettop(L)) { /* check number of arguments */
- case 0:
- { /* no arguments */
- lua_pushnumber(L, r); /* Number between 0 and 1 */
- break;
- }
- case 1:
- { /* only upper limit */
- int32_t u = luaL_checkint32(L, 1);
- luaL_argcheck(L, 1 <= u, 1, "interval is empty");
- lua_pushnumber(L, floor(r * u) + 1); /* int between 1 and `u' */
- break;
- }
- case 2:
- { /* lower and upper limits */
- int32_t l = luaL_checkint32(L, 1);
- int32_t u = luaL_checkint32(L, 2);
- luaL_argcheck(L, l <= u, 2, "interval is empty");
- /* int between `l' and `u' */
- lua_pushnumber(L, floor(r * (u - l + 1)) + l);
- break;
- }
- default: return luaL_error(L, "wrong number of arguments");
- }
- return 1;
-
-}
-
-LuaGameInterface::LuaGameInterface(Widelands::Game * g)
- : m_factory(new GameFactory())
-{
- setup_for_editor_and_game(m_L, g);
-
- // Overwrite math.random
- lua_getglobal(m_L, "math");
- lua_pushcfunction(m_L, L_math_random);
- lua_setfield(m_L, -2, "random");
- lua_pop(m_L, 1); // pop "math"
-
- LuaRoot::luaopen_wlroot(m_L, false);
- LuaGame::luaopen_wlgame(m_L);
-
- // Push the game into the registry
- lua_pushlightuserdata(m_L, static_cast<void *>(g));
- lua_setfield(m_L, LUA_REGISTRYINDEX, "game");
-
- // Push the factory class into the registry
- lua_pushlightuserdata
- (m_L, reinterpret_cast<void *>(dynamic_cast<Factory *>(m_factory.get())));
- lua_setfield(m_L, LUA_REGISTRYINDEX, "factory");
-}
-
-LuaGameInterface::~LuaGameInterface() {
-}
-
-LuaCoroutine* LuaGameInterface::read_coroutine(FileRead& fr) {
- LuaCoroutine * rv = new LuaCoroutine(nullptr);
- rv->read(m_L, fr);
- return rv;
-}
-
-void LuaGameInterface::write_coroutine(FileWrite& fw, LuaCoroutine* cr) {
- cr->write(fw);
-}
-
-
-void LuaGameInterface::read_global_env
- (FileRead & fr, Widelands::MapObjectLoader & mol,
- uint32_t size)
-{
- // Clean out the garbage before loading.
- lua_gc(m_L, LUA_GCCOLLECT, 0);
-
- assert(lua_gettop(m_L) == 0); // S:
- unpersist_object(m_L, fr, mol, size);
- assert(lua_gettop(m_L) == 1); // S: unpersisted_object
- luaL_checktype(m_L, -1, LUA_TTABLE);
-
- // Now, we have to merge all keys from the loaded table
- // into the global table
- lua_pushnil(m_L); // S: table nil
- while (lua_next(m_L, 1) != 0) {
- // S: table key value
- lua_rawgeti(m_L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS); // S: table key value globals_table
- lua_pushvalue(m_L, -3); // S: table key value globals_table key
- lua_gettable(m_L, -2); // S: table key value globals_table value_in_globals
- if (lua_compare(m_L, -1, -3, LUA_OPEQ)) {
- lua_pop(m_L, 3); // S: table key
- continue;
- } else {
- // Make this a global value
- lua_pop(m_L, 1); // S: table key value globals_table
- lua_pushvalue(m_L, -3); // S: table key value globals_table key
- lua_pushvalue(m_L, -3); // S: table key value globals_table key value
- lua_settable(m_L, -3); // S: table key value globals_table
- lua_pop(m_L, 2); // S: table key
- }
- }
-
- lua_pop(m_L, 1); // pop the table returned by unpersist_object
-
- // Clean out the garbage before returning.
- lua_gc(m_L, LUA_GCCOLLECT, 0);
-}
-
-uint32_t LuaGameInterface::write_global_env
- (FileWrite & fw, Widelands::MapObjectSaver & mos)
-{
- // Clean out the garbage before writing.
- lua_gc(m_L, LUA_GCCOLLECT, 0);
-
- // Empty table + object to persist on the stack Stack
- lua_newtable(m_L);
- lua_rawgeti(m_L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS);
-
- uint32_t nwritten = persist_object(m_L, fw, mos);
-
- // Garbage collect once more, so we do not return unnecessary stuff.
- lua_gc(m_L, LUA_GCCOLLECT, 0);
-
- return nwritten;
+ return ::run_script(m_L, path, g_fs);
}
=== renamed file 'src/scripting/scripting.h' => 'src/scripting/lua_interface.h'
--- src/scripting/scripting.h 2014-09-10 08:55:04 +0000
+++ src/scripting/lua_interface.h 2015-01-31 16:06:41 +0000
@@ -17,31 +17,15 @@
*
*/
-#ifndef WL_SCRIPTING_SCRIPTING_H
-#define WL_SCRIPTING_SCRIPTING_H
+#ifndef WL_SCRIPTING_LUA_INTERFACE_H
+#define WL_SCRIPTING_LUA_INTERFACE_H
-#include <map>
#include <memory>
#include <string>
-#include <stdint.h>
-
+#include "scripting/lua.h"
#include "scripting/lua_errors.h"
-#include "third_party/eris/lua.hpp"
-
-class FileRead;
-class FileWrite;
-
-namespace Widelands {
- class EditorGameBase;
- class Game;
- class MapObjectLoader;
- struct MapObjectSaver;
-}
-
-class EditorFactory;
-class GameFactory;
-class LuaCoroutine;
+
class LuaTable;
// Provides an interface to call and execute Lua Code.
@@ -53,38 +37,11 @@
// Interpret the given string, will throw 'LuaError' on any error.
void interpret_string(const std::string&);
- std::unique_ptr<LuaTable> run_script(const std::string& script);
- std::unique_ptr<LuaTable> get_hook(const std::string& name);
+ // Runs 'script' and returns the table it returned.
+ virtual std::unique_ptr<LuaTable> run_script(const std::string& script);
protected:
- lua_State * m_L;
-};
-
-class LuaEditorInterface : public LuaInterface {
-public:
- LuaEditorInterface(Widelands::EditorGameBase * g);
- virtual ~LuaEditorInterface();
-
-private:
- std::unique_ptr<EditorFactory> m_factory;
-};
-
-class LuaGameInterface : public LuaInterface {
-public:
- LuaGameInterface(Widelands::Game * g);
- virtual ~LuaGameInterface();
-
- // Input/output for coroutines.
- LuaCoroutine* read_coroutine(FileRead&);
- void write_coroutine(FileWrite&, LuaCoroutine*);
-
- // Input output for the global game state.
- void read_global_env
- (FileRead &, Widelands::MapObjectLoader &, uint32_t);
- uint32_t write_global_env
- (FileWrite &, Widelands::MapObjectSaver &);
-
-private:
- std::unique_ptr<GameFactory> m_factory;
-};
-#endif // end of include guard: WL_SCRIPTING_SCRIPTING_H
+ lua_State* m_L;
+};
+
+#endif // end of include guard: WL_SCRIPTING_LUA_INTERFACE_H
=== modified file 'src/scripting/lua_map.cc'
--- src/scripting/lua_map.cc 2014-12-07 15:41:39 +0000
+++ src/scripting/lua_map.cc 2015-01-31 16:06:41 +0000
@@ -38,7 +38,8 @@
#include "logic/world/resource_description.h"
#include "logic/world/terrain_description.h"
#include "logic/world/world.h"
-#include "scripting/c_utils.h"
+#include "scripting/factory.h"
+#include "scripting/globals.h"
#include "scripting/lua_errors.h"
#include "scripting/lua_game.h"
#include "wui/mapviewpixelfunctions.h"
=== modified file 'src/scripting/lua_map.h'
--- src/scripting/lua_map.h 2014-12-02 20:21:07 +0000
+++ src/scripting/lua_map.h 2015-01-31 16:06:41 +0000
@@ -34,8 +34,8 @@
#include "logic/trainingsite.h"
#include "logic/warehouse.h"
#include "logic/worker.h"
+#include "scripting/lua.h"
#include "scripting/luna.h"
-#include "third_party/eris/lua.hpp"
namespace Widelands {
=== modified file 'src/scripting/lua_root.cc'
--- src/scripting/lua_root.cc 2014-09-20 09:37:47 +0000
+++ src/scripting/lua_root.cc 2015-01-31 16:06:41 +0000
@@ -29,6 +29,7 @@
#include "logic/immovable.h"
#include "logic/tribe.h"
#include "logic/world/world.h"
+#include "scripting/globals.h"
#include "scripting/lua_coroutine.h"
#include "scripting/lua_editor.h"
#include "scripting/lua_game.h"
=== modified file 'src/scripting/lua_root.h'
--- src/scripting/lua_root.h 2014-09-10 17:52:49 +0000
+++ src/scripting/lua_root.h 2015-01-31 16:06:41 +0000
@@ -20,9 +20,9 @@
#ifndef WL_SCRIPTING_LUA_ROOT_H
#define WL_SCRIPTING_LUA_ROOT_H
+#include "scripting/lua.h"
#include "scripting/lua_bases.h"
#include "scripting/luna.h"
-#include "third_party/eris/lua.hpp"
namespace LuaRoot {
=== modified file 'src/scripting/lua_table.cc'
--- src/scripting/lua_table.cc 2014-11-26 17:07:51 +0000
+++ src/scripting/lua_table.cc 2015-01-31 16:06:41 +0000
@@ -23,6 +23,8 @@
#include <boost/format.hpp>
+#include "base/log.h"
+
LuaTable::LuaTable(lua_State* L) : L_(L), warn_about_unaccessed_keys_(true) {
// S: <table>
lua_pushlightuserdata(L_, const_cast<LuaTable*>(this)); // S: this
=== modified file 'src/scripting/lua_table.h'
--- src/scripting/lua_table.h 2014-12-02 07:19:33 +0000
+++ src/scripting/lua_table.h 2015-01-31 16:06:41 +0000
@@ -23,13 +23,13 @@
#include <memory>
#include <set>
#include <string>
+#include <vector>
#include <boost/lexical_cast.hpp>
-#include "scripting/c_utils.h"
+#include "scripting/lua.h"
#include "scripting/lua_coroutine.h"
#include "scripting/lua_errors.h"
-#include "third_party/eris/lua.hpp"
class LuaTableKeyError : public LuaError {
public:
=== modified file 'src/scripting/lua_ui.cc'
--- src/scripting/lua_ui.cc 2014-09-30 06:25:04 +0000
+++ src/scripting/lua_ui.cc 2015-01-31 16:06:41 +0000
@@ -22,7 +22,7 @@
#include "base/macros.h"
#include "logic/game_controller.h"
#include "logic/player.h"
-#include "scripting/c_utils.h"
+#include "scripting/globals.h"
#include "scripting/lua_map.h"
#include "scripting/luna.h"
#include "wui/interactive_player.h"
@@ -520,8 +520,7 @@
{nullptr, nullptr, nullptr},
};
-LuaMapView::LuaMapView(lua_State * L) :
- LuaPanel(get_egbase(L).get_ibase()) {
+LuaMapView::LuaMapView(lua_State* L) : LuaPanel(get_egbase(L).get_ibase()) {
}
void LuaMapView::__unpersist(lua_State* L)
=== modified file 'src/scripting/lua_ui.h'
--- src/scripting/lua_ui.h 2014-09-10 17:52:49 +0000
+++ src/scripting/lua_ui.h 2015-01-31 16:06:41 +0000
@@ -20,8 +20,8 @@
#ifndef WL_SCRIPTING_LUA_UI_H
#define WL_SCRIPTING_LUA_UI_H
+#include "scripting/lua.h"
#include "scripting/luna.h"
-#include "third_party/eris/lua.hpp"
#include "ui_basic/button.h"
#include "ui_basic/tabpanel.h"
#include "ui_basic/window.h"
=== modified file 'src/scripting/luna.h'
--- src/scripting/luna.h 2014-07-05 16:41:51 +0000
+++ src/scripting/luna.h 2015-01-31 16:06:41 +0000
@@ -59,8 +59,8 @@
#define UNPERS_UINT32(name, value) _UNPERS_TYPE(name, value, uint32)
#define UNPERS_STRING(name, value) _UNPERS_TYPE(name, value, string)
+#include "scripting/lua.h"
#include "scripting/luna_impl.h"
-#include "third_party/eris/lua.hpp"
/**
* Base Class. All Luna class must derive from this
=== modified file 'src/scripting/luna_impl.cc'
--- src/scripting/luna_impl.cc 2014-06-26 05:24:04 +0000
+++ src/scripting/luna_impl.cc 2015-01-31 16:06:41 +0000
@@ -22,7 +22,6 @@
#include <string>
#include "base/log.h"
-#include "scripting/c_utils.h"
#include "scripting/luna.h"
=== modified file 'src/scripting/luna_impl.h'
--- src/scripting/luna_impl.h 2014-09-14 14:36:56 +0000
+++ src/scripting/luna_impl.h 2015-01-31 16:06:41 +0000
@@ -26,8 +26,10 @@
#ifndef WL_SCRIPTING_LUNA_IMPL_H
#define WL_SCRIPTING_LUNA_IMPL_H
-#include "scripting/c_utils.h"
-#include "third_party/eris/lua.hpp"
+#include <cassert>
+
+#include "scripting/lua.h"
+#include "scripting/report_error.h"
int luna_unpersisting_closure(lua_State * L);
@@ -240,7 +242,7 @@
template <class T>
int m_persist(lua_State * const L) {
- assert(lua_gettop(L) == 1); // S: lightuserdata
+ assert(lua_gettop(L) == 1); // S: lightuserdata
T * * const obj = get_user_class<T>(L, 1);
lua_newtable(L); // S: user_obj table
=== modified file 'src/scripting/persistence.cc'
--- src/scripting/persistence.cc 2014-10-08 04:56:08 +0000
+++ src/scripting/persistence.cc 2015-01-31 16:06:41 +0000
@@ -24,7 +24,6 @@
#include "base/log.h"
#include "io/fileread.h"
#include "io/filewrite.h"
-#include "scripting/c_utils.h"
#include "scripting/eris.h"
#include "scripting/luna_impl.h"
=== modified file 'src/scripting/persistence.h'
--- src/scripting/persistence.h 2014-09-10 08:55:04 +0000
+++ src/scripting/persistence.h 2015-01-31 16:06:41 +0000
@@ -22,7 +22,7 @@
#include <string>
-#include "third_party/eris/lua.hpp"
+#include "scripting/lua.h"
class FileRead;
class FileWrite;
=== renamed file 'src/scripting/c_utils.cc' => 'src/scripting/report_error.cc'
--- src/scripting/c_utils.cc 2014-09-10 08:55:04 +0000
+++ src/scripting/report_error.cc 2015-01-31 16:06:41 +0000
@@ -17,84 +17,11 @@
*
*/
-
-#include "scripting/c_utils.h"
-
-#include <cstdarg>
-#include <cstdio>
-#include <iostream>
-
-#include "scripting/factory.h"
-#include "scripting/scripting.h"
-
-Factory & get_factory(lua_State * const L) {
- lua_getfield(L, LUA_REGISTRYINDEX, "factory");
- Factory * fac = static_cast<Factory *>(lua_touserdata(L, -1));
- lua_pop(L, 1); // pop this userdata
-
- if (!fac)
- throw LuaError("\"factory\" field was nil, which should be impossible!");
-
- return *fac;
-}
-
-Widelands::Game & get_game(lua_State * const L) {
- lua_getfield(L, LUA_REGISTRYINDEX, "game");
- Widelands::Game * g = static_cast<Widelands::Game *>(lua_touserdata(L, -1));
- lua_pop(L, 1); // pop this userdata
-
- if (!g)
- throw LuaError
- ("\"game\" field was nil. get_game was not called in a game.");
-
- return *g;
-}
-
-Widelands::EditorGameBase & get_egbase(lua_State * const L) {
- lua_getfield(L, LUA_REGISTRYINDEX, "egbase");
- Widelands::EditorGameBase * g = static_cast<Widelands::EditorGameBase *>
- (lua_touserdata(L, -1));
- lua_pop(L, 1); // pop this userdata
-
- if (!g)
- throw LuaError
- ("\"egbase\" field was nil. This should be impossible.");
-
-
- return *g;
-}
-
-Widelands::MapObjectLoader * get_mol(lua_State * const L) {
- lua_pushstring(L, "mol");
- lua_gettable(L, LUA_REGISTRYINDEX);
-
- Widelands::MapObjectLoader * mol =
- static_cast<Widelands::MapObjectLoader *>(lua_touserdata(L, -1));
-
- lua_pop(L, 1); // pop this userdata
-
- if (!mol)
- throw LuaError
- ("\"mol\" field was nil. This should be impossible.");
-
- return mol;
-}
-
-Widelands::MapObjectSaver * get_mos(lua_State * const L) {
- lua_pushstring(L, "mos");
- lua_gettable(L, LUA_REGISTRYINDEX);
-
- Widelands::MapObjectSaver * mos =
- static_cast<Widelands::MapObjectSaver *>(lua_touserdata(L, -1));
-
- lua_pop(L, 1); // pop this userdata
-
- if (!mos)
- throw LuaError
- ("\"mos\" field was nil. This should be impossible.");
-
- return mos;
-}
+#include "scripting/report_error.h"
+
+#include <cassert>
+
+#include "scripting/lua_errors.h"
/*
* Returns an error to lua. This method never returns as lua_error long jumps.
=== renamed file 'src/scripting/c_utils.h' => 'src/scripting/report_error.h'
--- src/scripting/c_utils.h 2014-09-10 08:55:04 +0000
+++ src/scripting/report_error.h 2015-01-31 16:06:41 +0000
@@ -17,21 +17,10 @@
*
*/
-#ifndef WL_SCRIPTING_C_UTILS_H
-#define WL_SCRIPTING_C_UTILS_H
-
-
-#include "logic/game.h"
-#include "map_io/map_object_loader.h"
-#include "map_io/map_object_saver.h"
-#include "scripting/factory.h"
-#include "third_party/eris/lua.hpp"
-
-Factory & get_factory(lua_State *);
-Widelands::Game & get_game(lua_State *);
-Widelands::EditorGameBase & get_egbase(lua_State *);
-Widelands::MapObjectLoader * get_mol(lua_State *);
-Widelands::MapObjectSaver * get_mos(lua_State *);
+#ifndef WL_SCRIPTING_REPORT_ERROR_H
+#define WL_SCRIPTING_REPORT_ERROR_H
+
+#include "scripting/lua.h"
#ifdef __GNUC__
void report_error(lua_State*, const char*, ...)
@@ -40,26 +29,4 @@
[[noreturn]] void report_error(lua_State*, const char*, ...)
#endif
-#define luaL_checkint32(L, n) static_cast<int32_t>(luaL_checkinteger(L, (n)))
-#define luaL_checkuint32(L, n) static_cast<uint32_t>(luaL_checkinteger(L, (n)))
-
-#define lua_pushint32(L, n) (lua_pushinteger(L, static_cast<int32_t>(n)))
-#define lua_pushuint32(L, n) (lua_pushinteger(L, static_cast<uint32_t>(n)))
-
-inline void lua_pushstring (lua_State * L, const std::string & s) {
- lua_pushstring(L, s.c_str());
-}
-
-inline lua_State * luaL_checkthread(lua_State * L, int n) {
- luaL_checktype(L, n, LUA_TTHREAD);
- lua_State * thread = lua_tothread(L, n);
- return thread;
-}
-
-inline bool luaL_checkboolean(lua_State * L, int n) {
- if (lua_isboolean(L, n))
- return lua_toboolean(L, n);
- return luaL_checkinteger(L, n);
-}
-
-#endif // end of include guard: WL_SCRIPTING_C_UTILS_H
+#endif // end of include guard: WL_SCRIPTING_REPORT_ERROR_H
=== added file 'src/scripting/run_script.cc'
--- src/scripting/run_script.cc 1970-01-01 00:00:00 +0000
+++ src/scripting/run_script.cc 2015-01-31 16:06:41 +0000
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2006-2015 by the Widelands Development Team
+ *
+ * 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; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#include "scripting/run_script.h"
+
+#include <memory>
+
+#include "io/filesystem/filesystem.h"
+#include "scripting/lua_table.h"
+
+namespace {
+
+// Reads the 'filename' from the 'fs' and returns its content.
+std::string get_file_content(FileSystem* fs, const std::string& filename) {
+ if (!fs || !fs->file_exists(filename)) {
+ throw LuaScriptNotExistingError(filename);
+ }
+ size_t length;
+ void* input_data = fs->load(filename, length);
+ const std::string data(static_cast<char*>(input_data));
+ // make sure the input_data is freed
+ free(input_data);
+ return data;
+}
+
+// Runs the 'content' as a lua script identified by 'identifier' in 'L'.
+std::unique_ptr<LuaTable>
+run_string_as_script(lua_State* L, const std::string& identifier, const std::string& content) {
+ // Get the current value of __file__
+ std::string last_file;
+ lua_getglobal(L, "__file__");
+ if (!lua_isnil(L, -1)) {
+ last_file = luaL_checkstring(L, -1);
+ }
+ lua_pop(L, 1);
+
+ // Set __file__.
+ lua_pushstring(L, identifier);
+ lua_setglobal(L, "__file__");
+
+ check_return_value_for_errors(
+ L,
+ luaL_loadbuffer(L, content.c_str(), content.size(), identifier.c_str()) ||
+ lua_pcall(L, 0, 1, 0));
+
+ if (lua_isnil(L, -1)) {
+ lua_pop(L, 1); // No return value from script
+ lua_newtable(L); // Push an empty table
+ }
+ if (!lua_istable(L, -1))
+ throw LuaError("Script did not return a table!");
+
+ // Restore old value of __file__.
+ if (last_file.empty()) {
+ lua_pushnil(L);
+ } else {
+ lua_pushstring(L, last_file);
+ }
+ lua_setglobal(L, "__file__");
+
+ std::unique_ptr<LuaTable> return_value(new LuaTable(L));
+ lua_pop(L, 1);
+ return return_value;
+}
+
+} // namespace
+
+int check_return_value_for_errors(lua_State* L, int rv) {
+ if (rv) {
+ const std::string err = luaL_checkstring(L, -1);
+ lua_pop(L, 1);
+ throw LuaError(err);
+ }
+ return rv;
+}
+
+std::unique_ptr<LuaTable> run_script(lua_State* L, const std::string& path, FileSystem* fs) {
+ const std::string content = get_file_content(fs, path);
+ return run_string_as_script(L, path, content);
+}
=== added file 'src/scripting/run_script.h'
--- src/scripting/run_script.h 1970-01-01 00:00:00 +0000
+++ src/scripting/run_script.h 2015-01-31 16:06:41 +0000
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2006-2015 by the Widelands Development Team
+ *
+ * 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; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *
+ */
+
+#ifndef WL_SCRIPTING_RUN_SCRIPT_H
+#define WL_SCRIPTING_RUN_SCRIPT_H
+
+#include <memory>
+
+#include "io/filesystem/filesystem.h"
+#include "scripting/lua.h"
+#include "scripting/lua_table.h"
+
+// Checks the return value of a function all for nonzero state and throws the
+// string that the function hopefully pushed as an Error. Returns 'rv' if there
+// is no error.
+int check_return_value_for_errors(lua_State* L, int rv);
+
+// Runs the 'script' searched in the given 'fs'.
+std::unique_ptr<LuaTable> run_script(lua_State* L, const std::string& path, FileSystem* fs);
+
+#endif // end of include guard: WL_SCRIPTING_RUN_SCRIPT_H
=== modified file 'src/scripting/test/CMakeLists.txt'
--- src/scripting/test/CMakeLists.txt 2014-07-03 18:33:00 +0000
+++ src/scripting/test/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -1,8 +1,8 @@
wl_test(test_scripting
SRCS
- ./scripting_test_main.cc
- ./test_luna.cc
+ scripting_test_main.cc
+ test_luna.cc
DEPENDS
- third_party_eris
- scripting
+ scripting_base
+ scripting_luna
)
=== modified file 'src/scripting/test/test_luna.cc'
--- src/scripting/test/test_luna.cc 2014-09-20 09:37:47 +0000
+++ src/scripting/test/test_luna.cc 2015-01-31 16:06:41 +0000
@@ -22,9 +22,9 @@
#include <boost/test/unit_test.hpp>
+#include "scripting/lua.h"
#include "scripting/luna.h"
#include "scripting/luna_impl.h"
-#include "third_party/eris/lua.hpp"
#ifndef BEGIN_LUNA_PROPERTIES
#define BEGIN_LUNA_PROPERTIES(klass) \
=== modified file 'src/ui_basic/CMakeLists.txt'
--- src/ui_basic/CMakeLists.txt 2014-12-14 12:16:27 +0000
+++ src/ui_basic/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -64,7 +64,8 @@
# TODO(sirver): should not depend on logic
logic
profile
- scripting
+ scripting_lua_interface
+ scripting_lua_table
sound
widelands_ball_of_mud
)
=== modified file 'src/ui_basic/helpwindow.cc'
--- src/ui_basic/helpwindow.cc 2014-11-27 16:43:37 +0000
+++ src/ui_basic/helpwindow.cc 2015-01-31 16:06:41 +0000
@@ -24,6 +24,7 @@
#include <boost/format.hpp>
+#include "base/i18n.h"
#include "base/log.h"
#include "graphic/font.h"
#include "graphic/font_handler.h"
@@ -33,8 +34,8 @@
#include "graphic/text_constants.h"
#include "io/filesystem/layered_filesystem.h"
#include "logic/building.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
#include "ui_basic/button.h"
#include "ui_basic/window.h"
=== modified file 'src/ui_fsmenu/CMakeLists.txt'
--- src/ui_fsmenu/CMakeLists.txt 2015-01-08 21:16:24 +0000
+++ src/ui_fsmenu/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -56,7 +56,8 @@
map_io_map_loader
network
profile
- scripting
+ scripting_lua_interface
+ scripting_lua_table
sound
ui_basic
widelands_ball_of_mud
=== modified file 'src/ui_fsmenu/fileview.cc'
--- src/ui_fsmenu/fileview.cc 2014-12-06 12:22:35 +0000
+++ src/ui_fsmenu/fileview.cc 2015-01-31 16:06:41 +0000
@@ -23,14 +23,15 @@
#include <boost/algorithm/string/predicate.hpp>
+#include "base/i18n.h"
#include "graphic/font_handler1.h"
#include "graphic/graphic.h"
#include "graphic/text/font_set.h"
#include "graphic/text_constants.h"
#include "io/filesystem/filesystem.h"
#include "profile/profile.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
namespace {
bool read_text(const std::string& filename, std::string* title, std::string* content) {
=== modified file 'src/ui_fsmenu/launch_mpg.cc'
--- src/ui_fsmenu/launch_mpg.cc 2014-12-11 10:27:30 +0000
+++ src/ui_fsmenu/launch_mpg.cc 2015-01-31 16:06:41 +0000
@@ -37,8 +37,8 @@
#include "logic/player.h"
#include "map_io/map_loader.h"
#include "profile/profile.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
#include "ui_basic/messagebox.h"
#include "ui_fsmenu/loadgame.h"
#include "ui_fsmenu/mapselect.h"
=== modified file 'src/ui_fsmenu/launch_spg.cc'
--- src/ui_fsmenu/launch_spg.cc 2014-12-06 12:22:35 +0000
+++ src/ui_fsmenu/launch_spg.cc 2015-01-31 16:06:41 +0000
@@ -38,8 +38,8 @@
#include "logic/player.h"
#include "map_io/map_loader.h"
#include "profile/profile.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
#include "ui_fsmenu/loadgame.h"
#include "ui_fsmenu/mapselect.h"
#include "wui/playerdescrgroup.h"
=== modified file 'src/ui_fsmenu/options.cc'
--- src/ui_fsmenu/options.cc 2015-01-22 11:00:01 +0000
+++ src/ui_fsmenu/options.cc 2015-01-31 16:06:41 +0000
@@ -27,6 +27,7 @@
#include <boost/algorithm/string.hpp>
#include <boost/format.hpp>
+#include "base/i18n.h"
#include "base/log.h"
#include "base/wexception.h"
#include "graphic/default_resolution.h"
@@ -38,8 +39,8 @@
#include "io/filesystem/layered_filesystem.h"
#include "logic/save_handler.h"
#include "profile/profile.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
#include "sound/sound_handler.h"
#include "wlapplication.h"
=== modified file 'src/wui/CMakeLists.txt'
--- src/wui/CMakeLists.txt 2014-12-14 12:16:27 +0000
+++ src/wui/CMakeLists.txt 2015-01-31 16:06:41 +0000
@@ -162,7 +162,8 @@
network
notifications
profile
- scripting
+ scripting_lua_interface
+ scripting_lua_table
sound
ui_basic
ui_fsmenu
=== modified file 'src/wui/general_statistics_menu.cc'
--- src/wui/general_statistics_menu.cc 2014-11-30 18:49:38 +0000
+++ src/wui/general_statistics_menu.cc 2015-01-31 16:06:41 +0000
@@ -32,8 +32,8 @@
#include "logic/player.h"
#include "logic/tribe.h"
#include "logic/warelist.h"
+#include "scripting/lua_interface.h"
#include "scripting/lua_table.h"
-#include "scripting/scripting.h"
#include "ui_basic/button.h"
#include "ui_basic/checkbox.h"
#include "ui_basic/slider.h"
=== modified file 'src/wui/interactive_base.cc'
--- src/wui/interactive_base.cc 2014-12-06 12:22:35 +0000
+++ src/wui/interactive_base.cc 2015-01-31 16:06:41 +0000
@@ -44,7 +44,7 @@
#include "logic/player.h"
#include "logic/productionsite.h"
#include "profile/profile.h"
-#include "scripting/scripting.h"
+#include "scripting/lua_interface.h"
#include "wlapplication.h"
#include "wui/game_chat_menu.h"
#include "wui/game_debug_ui.h"
Follow ups