Skip to content

Commit

Permalink
Move Lua unit wrapper to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
CelticMinstrel committed Aug 21, 2016
1 parent 797613f commit 1521f32
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 134 deletions.
16 changes: 8 additions & 8 deletions projectfiles/Xcode/Wesnoth.xcodeproj/project.pbxproj
Expand Up @@ -164,6 +164,8 @@
9193FC7B1D5AE5B2004F6C07 /* name_generator_factory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 913D26751D3C9697002FF3AB /* name_generator_factory.cpp */; };
9193FC7E1D5BB64F004F6C07 /* advancement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9193FC7C1D5BB64E004F6C07 /* advancement.cpp */; };
9193FC7F1D5BB64F004F6C07 /* advancement.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9193FC7C1D5BB64E004F6C07 /* advancement.cpp */; };
9193FC821D5C2CF8004F6C07 /* lua_unit.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9193FC801D5C2CF7004F6C07 /* lua_unit.cpp */; };
9193FC831D5C2D00004F6C07 /* lua_unit.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9193FC801D5C2CF7004F6C07 /* lua_unit.cpp */; };
919B37F81BAF789E00E0094C /* synced_user_choice.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 919B37F71BAF789D00E0094C /* synced_user_choice.cpp */; };
919B37FC1BAF7A9D00E0094C /* synced_choice_wait.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 919B37FA1BAF7A9D00E0094C /* synced_choice_wait.cpp */; };
91A214E51CAD666B00927AEA /* arrow.cpp in Sources */ = {isa = PBXBuildFile; fileRef = B52EE8A1121359A600CFBDAB /* arrow.cpp */; };
Expand Down Expand Up @@ -268,7 +270,6 @@
91A215601CAD6A8B00927AEA /* lzio.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC89A1281879D17D00A3B0B1 /* lzio.cpp */; };
91A215621CAD6DA400927AEA /* application_lua_kernel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC218E9A1A1064F4007C910C /* application_lua_kernel.cpp */; };
91A215631CAD6DA400927AEA /* game_lua_kernel.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC218EA21A106673007C910C /* game_lua_kernel.cpp */; };
91A215641CAD6DA400927AEA /* lua_api.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C02A0D182F1F64008525C6 /* lua_api.cpp */; };
91A215651CAD6DA400927AEA /* lua_common.cpp in Sources */ = {isa = PBXBuildFile; fileRef = EC218E9E1A106648007C910C /* lua_common.cpp */; };
91A215661CAD6DA400927AEA /* lua_cpp_function.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECD5D7BE1A22DC8600114175 /* lua_cpp_function.cpp */; };
91A215671CAD6DA400927AEA /* lua_fileops.cpp in Sources */ = {isa = PBXBuildFile; fileRef = ECA4A6781A1EC319006BCCF2 /* lua_fileops.cpp */; };
Expand Down Expand Up @@ -1259,7 +1260,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 */; };
F4C02A10182F1F64008525C6 /* lua_api.cpp in Sources */ = {isa = PBXBuildFile; fileRef = F4C02A0D182F1F64008525C6 /* lua_api.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 */; };
Expand Down Expand Up @@ -1594,6 +1594,8 @@
9193FC781D5AC2D8004F6C07 /* game_stats.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = game_stats.hpp; sourceTree = "<group>"; };
9193FC7C1D5BB64E004F6C07 /* advancement.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = advancement.cpp; sourceTree = "<group>"; };
9193FC7D1D5BB64E004F6C07 /* advancement.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = advancement.hpp; sourceTree = "<group>"; };
9193FC801D5C2CF7004F6C07 /* lua_unit.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_unit.cpp; sourceTree = "<group>"; };
9193FC811D5C2CF8004F6C07 /* lua_unit.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_unit.hpp; sourceTree = "<group>"; };
919B37F71BAF789D00E0094C /* synced_user_choice.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = synced_user_choice.cpp; sourceTree = "<group>"; };
919B37F91BAF78AB00E0094C /* synced_user_choice.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = synced_user_choice.hpp; sourceTree = "<group>"; };
919B37FA1BAF7A9D00E0094C /* synced_choice_wait.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = synced_choice_wait.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2245,7 +2247,6 @@
B5A5E3E012132C790047782D /* lundump.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lundump.h; sourceTree = "<group>"; };
B5A5E3E212132C790047782D /* lvm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lvm.h; sourceTree = "<group>"; };
B5A5E3E412132C790047782D /* lzio.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = lzio.h; sourceTree = "<group>"; };
B5A5E45012132DE30047782D /* lua_api.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_api.hpp; sourceTree = "<group>"; };
B5A9914A0ECA4064002BE442 /* fonts */ = {isa = PBXFileReference; lastKnownFileType = folder; name = fonts; path = ../../fonts; sourceTree = SOURCE_ROOT; };
B5A9B0780ECA4074002BE442 /* images */ = {isa = PBXFileReference; lastKnownFileType = folder; name = images; path = ../../images; sourceTree = SOURCE_ROOT; };
B5A9B2140ECA4074002BE442 /* sounds */ = {isa = PBXFileReference; lastKnownFileType = folder; name = sounds; path = ../../sounds; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -2656,7 +2657,6 @@
F480CD3F14035038007175D6 /* tag.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = tag.hpp; sourceTree = "<group>"; };
F49F134815BC627C00B64B0B /* edit_label.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = edit_label.cpp; sourceTree = "<group>"; };
F49F134915BC627C00B64B0B /* edit_label.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = edit_label.hpp; sourceTree = "<group>"; };
F4C02A0D182F1F64008525C6 /* lua_api.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_api.cpp; sourceTree = "<group>"; };
F4C02A0E182F1F64008525C6 /* lua_types.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = lua_types.cpp; sourceTree = "<group>"; };
F4C02A0F182F1F64008525C6 /* lua_types.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = lua_types.hpp; sourceTree = "<group>"; };
F4C5DD66158CFD5E0044F754 /* point.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = point.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -4125,8 +4125,6 @@
91B621E21B76BAF300B00E0F /* context.hpp */,
EC218EA21A106673007C910C /* game_lua_kernel.cpp */,
91B621E41B76BB0100B00E0F /* game_lua_kernel.hpp */,
F4C02A0D182F1F64008525C6 /* lua_api.cpp */,
B5A5E45012132DE30047782D /* lua_api.hpp */,
EC218E9E1A106648007C910C /* lua_common.cpp */,
91B621E51B76BB0700B00E0F /* lua_common.hpp */,
ECD5D7BE1A22DC8600114175 /* lua_cpp_function.cpp */,
Expand All @@ -4150,6 +4148,8 @@
91B621ED1B76BB2600B00E0F /* lua_team.hpp */,
F4C02A0E182F1F64008525C6 /* lua_types.cpp */,
F4C02A0F182F1F64008525C6 /* lua_types.hpp */,
9193FC801D5C2CF7004F6C07 /* lua_unit.cpp */,
9193FC811D5C2CF8004F6C07 /* lua_unit.hpp */,
EC5430221A4E6024006D206C /* lua_unit_type.cpp */,
91B621EE1B76BB2C00B00E0F /* lua_unit_type.hpp */,
EC59F2601A4529D2001910CB /* manager.cpp */,
Expand Down Expand Up @@ -4940,7 +4940,6 @@
EC89A1431879D17D00A3B0B1 /* ltable.cpp in Sources */,
EC89A1441879D17D00A3B0B1 /* ltablib.cpp in Sources */,
EC89A1451879D17D00A3B0B1 /* ltm.cpp in Sources */,
F4C02A10182F1F64008525C6 /* lua_api.cpp in Sources */,
EC218EA01A106648007C910C /* lua_common.cpp in Sources */,
ECD5D7BF1A22DC8600114175 /* lua_cpp_function.cpp in Sources */,
ECA4A67B1A1EC319006BCCF2 /* lua_fileops.cpp in Sources */,
Expand Down Expand Up @@ -5237,6 +5236,7 @@
9193FC791D5AC2D8004F6C07 /* game_stats.cpp in Sources */,
9193FC7E1D5BB64F004F6C07 /* advancement.cpp in Sources */,
917746C11D680C7C00E8689A /* walker_tree_node.cpp in Sources */,
9193FC821D5C2CF8004F6C07 /* lua_unit.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -5603,7 +5603,6 @@
91A215601CAD6A8B00927AEA /* lzio.cpp in Sources */,
91A215621CAD6DA400927AEA /* application_lua_kernel.cpp in Sources */,
91A215631CAD6DA400927AEA /* game_lua_kernel.cpp in Sources */,
91A215641CAD6DA400927AEA /* lua_api.cpp in Sources */,
91A215651CAD6DA400927AEA /* lua_common.cpp in Sources */,
91A215661CAD6DA400927AEA /* lua_cpp_function.cpp in Sources */,
91A215671CAD6DA400927AEA /* lua_fileops.cpp in Sources */,
Expand Down Expand Up @@ -5809,6 +5808,7 @@
9193FC7B1D5AE5B2004F6C07 /* name_generator_factory.cpp in Sources */,
9193FC7F1D5BB64F004F6C07 /* advancement.cpp in Sources */,
917746C21D680C7C00E8689A /* walker_tree_node.cpp in Sources */,
9193FC831D5C2D00004F6C07 /* lua_unit.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
2 changes: 1 addition & 1 deletion src/ai/default/aspect_attacks.cpp
Expand Up @@ -30,7 +30,7 @@
#include "units/unit.hpp"
#include "pathfind/pathfind.hpp"
#include "units/filter.hpp"
#include "scripting/lua_api.hpp"
#include "scripting/lua_unit.hpp"
#include "lua/lauxlib.h"

namespace ai {
Expand Down
2 changes: 1 addition & 1 deletion src/ai/lua/core.cpp
Expand Up @@ -24,7 +24,7 @@
#include "ai/lua/core.hpp"
#include "ai/composite/aspect.hpp"
#include "scripting/game_lua_kernel.hpp"
#include "scripting/lua_api.hpp"
#include "scripting/lua_unit.hpp"
#include "scripting/push_check.hpp"
#include "lua_object.hpp" // (Nephro)

Expand Down
1 change: 0 additions & 1 deletion src/ai/lua/lua_object.hpp
Expand Up @@ -25,7 +25,6 @@
#include "lua/lua.h"
#include "map/location.hpp"
#include "resources.hpp"
#include "scripting/lua_api.hpp"
#include "scripting/lua_common.hpp"
#include "terrain/filter.hpp"
#include "variable.hpp"
Expand Down
1 change: 0 additions & 1 deletion src/scripting/application_lua_kernel.cpp
Expand Up @@ -30,7 +30,6 @@
#include "config.hpp"
#include "game_errors.hpp"
#include "log.hpp"
#include "scripting/lua_api.hpp"
#include "scripting/lua_common.hpp"
#include "scripting/lua_cpp_function.hpp"
#include "scripting/lua_fileops.hpp"
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/game_lua_kernel.cpp
Expand Up @@ -72,7 +72,7 @@
#include "recall_list_manager.hpp" // for recall_list_manager
#include "replay.hpp" // for get_user_choice, etc
#include "reports.hpp" // for register_generator, etc
#include "scripting/lua_api.hpp" // for luaW_toboolean, etc
#include "scripting/lua_unit.hpp"
#include "scripting/lua_common.hpp"
#include "scripting/lua_cpp_function.hpp"
#include "scripting/lua_gui2.hpp" // for show_gamestate_inspector
Expand Down
90 changes: 89 additions & 1 deletion src/scripting/lua_common.cpp
Expand Up @@ -26,12 +26,15 @@
#include "global.hpp"

#include "config.hpp"
#include "scripting/lua_api.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"
#include "gettext.hpp"
#include "resources.hpp"
#include "lua_jailbreak_exception.hpp"
#include "game_display.hpp"

#include <cstring>
#include <iterator> // for distance, advance
Expand Down Expand Up @@ -885,3 +888,88 @@ bool luaW_checkvariable(lua_State *L, variable_access_create& v, int n)
return false;
}
}

void chat_message(std::string const &caption, std::string const &msg)
{
if (!resources::screen) return;
resources::screen->get_chat_manager().add_chat_message(time(nullptr), caption, 0, msg,
events::chat_handler::MESSAGE_PUBLIC, false);
}

// To silence "no prototype" warnings
void push_error_handler(lua_State *L);
int luaW_pcall_internal(lua_State *L, int nArgs, int nRets);

void push_error_handler(lua_State *L)
{
lua_pushlightuserdata(L, executeKey);
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_remove(L, -2);
lua_rawset(L, LUA_REGISTRYINDEX);
lua_pop(L, 1);
}

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_rawget(L, LUA_REGISTRYINDEX);
lua_insert(L, -2 - nArgs);

int error_handler_index = lua_gettop(L) - nArgs - 1;

// Call the function.
int errcode = lua_pcall(L, nArgs, nRets, -2 - nArgs);

// Remove the error handler.
lua_remove(L, error_handler_index);

tlua_jailbreak_exception::rethrow();

return errcode;
}

#ifdef _MSC_VER
#pragma warning (push)
#pragma warning (disable: 4706)
#endif
bool luaW_pcall(lua_State *L, int nArgs, int nRets, bool allow_wml_error)
{
int res = luaW_pcall_internal(L, nArgs, nRets);

if (res)
{
/*
* When an exception is thrown which doesn't derive from
* std::exception m will be nullptr pointer.
*/
char const *m = lua_tostring(L, -1);
if(m) {
if (allow_wml_error && strncmp(m, "~wml:", 5) == 0) {
m += 5;
char const *e = strstr(m, "stack traceback");
lg::wml_error() << std::string(m, e ? e - m : strlen(m));
} else if (allow_wml_error && strncmp(m, "~lua:", 5) == 0) {
m += 5;
char const *e = nullptr, *em = m;
while (em[0] && ((em = strstr(em + 1, "stack traceback"))))
#ifdef _MSC_VER
#pragma warning (pop)
#endif
e = em;
chat_message("Lua error", std::string(m, e ? e - m : strlen(m)));
} else {
ERR_LUA << m << '\n';
chat_message("Lua error", m);
}
} else {
chat_message("Lua caught unknown exception", "");
}
lua_pop(L, 2);
return false;
}

return true;
}
13 changes: 13 additions & 0 deletions src/scripting/lua_common.hpp
Expand Up @@ -29,6 +29,7 @@ class vconfig;
#include "variable_info.hpp"
#include "map/location.hpp"
#include <vector>
#include <string>

namespace lua_common {
int intf_textdomain(lua_State *L);
Expand Down Expand Up @@ -159,6 +160,18 @@ bool luaW_pushvariable(lua_State *L, variable_access_const& v);

bool luaW_checkvariable(lua_State *L, variable_access_create& v, int n);

/**
* Displays a message in the chat window.
*/
void chat_message(std::string const &caption, std::string const &msg);

/**
* Calls a Lua function stored below its @a nArgs arguments at the top of the stack.
* @param nRets LUA_MULTRET for unbounded return values.
* @return true if the call was successful and @a nRets return values are available.
*/
bool luaW_pcall(lua_State *L, int nArgs, int nRets, bool allow_wml_error = false);


#define return_tstring_attrib(name, accessor) \
if (strcmp(m, name) == 0) { \
Expand Down
1 change: 0 additions & 1 deletion src/scripting/lua_fileops.cpp
Expand Up @@ -19,7 +19,6 @@
#include "game_config.hpp" //for game_config::debug_lua
#include "game_errors.hpp"
#include "log.hpp"
#include "scripting/lua_api.hpp" // for chat_message, luaW_pcall
#include "scripting/lua_common.hpp" // for chat_message, luaW_pcall

#include <exception>
Expand Down
2 changes: 1 addition & 1 deletion src/scripting/lua_formula_bridge.cpp
Expand Up @@ -16,7 +16,7 @@
#include "boost/variant/static_visitor.hpp"

#include "scripting/game_lua_kernel.hpp"
#include "scripting/lua_api.hpp"
#include "scripting/lua_unit.hpp"
#include "scripting/lua_common.hpp"
#include "lua/lauxlib.h"
#include "lua/lua.h"
Expand Down
1 change: 0 additions & 1 deletion src/scripting/lua_gui2.cpp
Expand Up @@ -41,7 +41,6 @@

#include "config.hpp"
#include "log.hpp"
#include "scripting/lua_api.hpp" // for luaW_toboolean, etc
#include "scripting/lua_common.hpp"
#include "scripting/lua_types.hpp" // for getunitKey, dlgclbkKey, etc
#include "serialization/string_utils.hpp"
Expand Down
1 change: 0 additions & 1 deletion src/scripting/lua_pathfind_cost_calculator.hpp
@@ -1,7 +1,6 @@
#pragma once
#include "lua/lua.h"
#include "map/location.hpp"
#include "scripting/lua_api.hpp"
#include "pathfind/pathfind.hpp"

/**
Expand Down

0 comments on commit 1521f32

Please sign in to comment.