Commit graph

52 commits

Author SHA1 Message Date
Elvish_Hunter
9b7b1751fd Removed trailing tabs and whitespaces from C++ source
I used this command line: find <source directory> -name \*.\[ch\]pp -print0 | xargs -0 sed -i 's/[[:blank:]]*$//'
2015-12-21 20:39:46 +01:00
Celtic Minstrel
376020097f Port [object] ActionWML tag from C++ to Lua
Lua API additions:
- wesnoth.show_popup_dialog()
- optional write_to_mods parameter to wesnoth.add_modification
2015-09-19 12:13:25 -04:00
Celtic Minstrel
91ac90db11 Lua API: Add wesnoth.show_message_dialog() 2015-09-18 00:23:54 -04:00
Ignacio R. Morelle
b4de73f83b Add wesnoth.set_dialog_visible function
Sets a given widget's visibility property.
2015-08-22 23:07:03 -03:00
Ignacio R. Morelle
ba6257129a Add wesnoth.set_dialog_focus function
Gives keyboard focus to the given widget.
2015-08-22 22:12:02 -03:00
gfgtdf
90ee0a2e9b add wesnoth.remove_dialog_item
a function to remove items from gui2 lists
2015-05-26 03:28:19 +02:00
Chris Beck
bce5fb22e5 fixup HEAD^
use tab characters between lua print arguments, and no trailing tab
2015-04-29 17:18:03 -04:00
Chris Beck
c095029d68 lua print: put a space between items 2015-04-29 13:35:11 -04:00
Chris Beck
1bd5cfb756 fixup lua dofile to use a protected call
thanks to gfgtdf for pointing this out earlier, it can cause a
lua kernel panic if an error occurs which is not what we want.
2015-04-11 22:55:30 -04:00
Chris Beck
9812e14acd change member function registration to use templates and extraspace
This is used instead of the boost::bind and boost::function method
introduced before. It's a bit simpler than the boost::function code
in lua_cpp_function, and a bit faster / less can go wrong when we
don't use boost::fucntion.

It also fixes a potential future use-case -- if a lua state is
copied, but many of the functions in the environment are made with
boost bind, they become bad pointers since they point to the old
lua state, and there is no easy way to retrieve them. Using lua
extra space instead of bind ties the lua_State more tightly to our
C++ representation of it.
2015-04-10 15:02:12 -04:00
gfgtdf
7dc59fcab5 Enable tree_view gui2 widget for lua
wesnoth.add_dialog_tree_node to add nodes to a tree_value
wesnoth.set_dialog_callback support for tree_views (triggers when the
selected node was changed)
wesnoth.get_dialog_value support for tree_views returns an array of
integers that describe the path to the currebntly selected node.
2015-04-08 16:35:10 +02:00
Chris Beck
7a870f016a fixup lua print with null strings 2015-04-05 12:31:34 -04:00
gfgtdf
38ed3a3c97 Use given seed in lua random map generator
Thew seed can be passed in the editor in the generate map dialog, just
like for the other generators.
2015-03-30 00:58:15 +02:00
gfgtdf
6655b186b1 LUAI_EXTRASPACE
We use LUAI_EXTRASPACE to add a pointer to the lua_kernel_base in the lua_State
stucture, this makes is very easy to get a reference to the lua_kernel in
functions that take a lua_State* as an argument.
2015-03-30 00:58:14 +02:00
gfgtdf
b892e16b94 include lua/... files after others
headers from lua/..  contain macros that can break other headers in
wesnoth, boost or other libraries.

In this case it was a macro
#define cast(t, exp) ((t)(exp))
defined in lua/llimits.h that broke a boost header.
2015-03-27 00:54:22 +01:00
Elvish_Hunter
6c47130adf Lua: enabled support for the bit32 (bitwise operations) library 2015-01-14 12:22:44 +01:00
Ignacio R. Morelle
57ae45387a New Year copyright update 2015-01-01 19:07:35 -03:00
Chris Beck
0f9bd0a929 lua: move gettext metatable registration to lua_common 2014-12-25 14:11:12 -05:00
Chris Beck
93fe16a832 lua: move metatable registration for lua_common
this gives better encapsulation
2014-12-25 13:42:54 -05:00
Chris Beck
06ca78fec4 bugfix: intf_require would segfault if a thread called it
The problem was that intf_require would assume use the wrong
lua_State* since it finds it using the kernel's this pointer. This
commit gives a version of "protected_call" which allows to use the
callback's lua_State*.
2014-12-19 19:47:20 -05:00
Chris Beck
3e73ec2f68 fixups: don't construct string from NULL, avoid complicated API
In the error handling routines for compiling and running a script
at runtime, don't construct a string directly from lua_tostring,
because if it is null that is a runtime error. Instead check for
this scenario.

In intf_require, when getting the "wesnoth" global, luaW_getglobal
is unnecessary and lua_getglobal should be prefered.
2014-12-19 19:47:20 -05:00
Chris Beck
96ad22069f add error reporting to try to understand wesnoth.require error 2014-12-19 19:47:19 -05:00
Chris Beck
6032372e4a add plugins context, manager, create lua thread object
This collection of classes provides a framework whereby the engine
may provide scripting capabilities in any "context". A C++
function may declare a plugin context by providing a list of names
function objects, then may call "play_slice()" on this context to
transfer control to lua. A lua script may generate some arguments
and request to call some of these, then yield to the engine. C++
will run them until one of them signals an error, or that the
context is now invalid (using a boolean return value).

The intended semantics is that lua may only make requests
corresponding to the current context. As soon as a new context
is used (play_slice() is called again) the old context is invalid
from lua's point of view -- calling those functions is a lua
error but not undefined behavior. This should be the case even if
the old context has not been destroyed yet in C++, for instance
if a lua request resulted in a new context being created. There is
a system of locks in place to ensure that when old contexts are
invalidated, stale requests are discarded.

We also add the coroutine library to our lua base. This because
threads are run as coroutines, and writing them requires the use
of coroutine yield from the standard library.
2014-12-19 19:47:17 -05:00
Chris Beck
bae61e944f fixup wesnoth.require after commit HEAD^
It was implicitly assuming that loadfile takes arguments from the
bottom of the stack.
2014-12-05 15:26:15 -05:00
anonymissimus
5ad0e47eab fix warning about unreferenced variable 2014-11-30 23:45:57 +01:00
Chris Beck
84f5677155 add wesnoth.show_lua_console, as a debugging aide
This allows some sort of "drop into debug" mode for lua if your
script detects an error.

We could consider to actually add an "option to attach debugger"
as an error handling mechanism, but it might be complicated
because of wesnoth exceptions which must go through lua and be
treated as lua errors, and we cannot handle two exceptions at once
obviously, so the lua interpreter is probably not safe to run that
way in general. Anyways this commit does not do that.
2014-11-28 18:11:32 -05:00
Chris Beck
99678ef29a refactor lua file ops: use fileops::load, but use kernel pcall 2014-11-24 23:13:36 -05:00
Chris Beck
9eaba64651 refactor game_lua_kernel not to use resources:: for gamestate
This commit changes the majority of the wesnoth callback functions
for the game lua kernel to be private methods of the game lua
kernel. The game lua kernel is given direct access, via it's ctor,
to references of many of the things provided by resources. The
call back functions are modified to access these private reference
variables instead of resources, and they are added to the
scripting environment using the lua cpp function mechanism.

This greatly improves encapsulation and will eventually lead to
eliminating the resources pointers from the game lua kernel
entirely.
2014-11-24 17:13:03 -05:00
Chris Beck
48a14ddadf move lua fcn 'wesnoth.tovconfig' to lua kernel base / lua common
there was no reason for it to only be in the game kernel, most
other vconfig functions were already in lua common.
2014-11-24 01:12:25 -05:00
Chris Beck
3b1c52f3c8 move lua boost::function wrapper to scripting/lua_cpp_function.?pp 2014-11-23 20:24:47 -05:00
Chris Beck
93becab0ac clean up some comments, improve type-safety in lua boost function 2014-11-23 19:11:41 -05:00
Chris Beck
f0fac7f367 add a "__tostring" method for boost functions
It makes it easier to realize that they are functions when in the
console.
2014-11-23 19:02:07 -05:00
Chris Beck
8adfd3a38c fixup lua <-> boost function binding system
The solution in place was working but it was flawed in that unlike
with normal lua callbacks, the callbacks would find the userdata
with a pointer to themselves as the first item on the stack. This
breaks the normal lua stack discipline for callback functions.

This commit makes it work normally, removing the stack entry for
the function before it is called, and also making a local copy
of the function pointer in case the lua garbage collector should
deallocate and destroy it when it is removed from the stack.
2014-11-23 18:27:05 -05:00
Chris Beck
a65d168317 enable "strict mode" in wesnoth lua
Enables an ilua feature called "strict mode" in all of our lua
environments. This change causes lua to report an error if a global
variable is used before it is assigned. The benefits of this are:
- Improved maintainability of lua-based add-ons, since you get
better error reporting in case of a typo.
- Improved behavior of the lua interpreter console, since mistyped
or gibberish lines resolve to an error rather than "nil", which
the console treats as a legal return value.

It is possible to disable this or work around it in individual
scripts. For more info see release notes.
2014-11-22 15:19:19 -05:00
Chris Beck
4a2eb88607 allow lua console to handle expressions
Implemented using ilua, and "experimental compilation" technique.
2014-11-21 23:01:10 -05:00
Chris Beck
ba4276ea31 add "ilua" helper functions for lua interpreter 2014-11-21 22:22:43 -05:00
Chris Beck
e89383d1aa move lua Rng wrapper to the lua kernel base 2014-11-20 16:58:12 -05:00
Chris Beck
d7770f5a92 move lua map_location operations to their own file 2014-11-20 15:53:26 -05:00
Chris Beck
fccddbb226 move lua fileops to their own file 2014-11-20 15:37:22 -05:00
Chris Beck
29aaa7e05c fix a bug in lua console implementation
I think that without this, if tab completion is pressed many times
it might cause the lua stack to overflow in some cases.
2014-11-20 15:12:06 -05:00
Chris Beck
e7e0bb57dc keep the old lua print function (to cout) around, as "std_print"
name should change maybe
2014-11-20 15:07:35 -05:00
Chris Beck
dffbd96068 add bindings for map_location functions to the lua kernel base
I mainly want to use this for random map generators, it would be
silly to rewrite this stuff in lua after we already have it in C++
(and unit tested there).
2014-11-19 23:09:37 -05:00
Chris Beck
caeaa6ba67 refactor lua show_dialog
The lua show dialog used to get the video object from resources,
which is not ideal. In this version, lua kernel base internalizes
a pointer to the video object, and show_dialog is bound to a
method of the lua kernel base.

From now on, lua kernels which want to show dialogs need to be
constructed with a pointer to a CVideo, if it is null the lua will
fail with a runtime error.

The application lua kernel is constructed with the video for
game_launcher, and the game lua kernel is modified so that it is
owned by the play controller and not the game events manager, and
held in a boost scoped pointer rather than manually deleted. This
is because the game events manager doesn't have a video object,
also it conceptually seems independent of the lua kernel anyways.

The map generator lua kernels are constructed with NULL, but if a
user configuration dialog is provided (new feature) then when they
try to run it they will update with the mp_create's video pointer.
2014-11-17 17:03:00 -05:00
Chris Beck
59507f3d09 fixup lua error formatting 2014-11-13 20:29:07 -05:00
Chris Beck
2021a42d5b Add lua console
- Add an internal command logger to the lua kernel base, and expose
this.
- Redirect the internal lua `print` to the command logger.
- Add an external logger registration system to lua kernel, in
addition to the internal one.
- Add a lua console dialog which binds to a lua kernel base and
permits to review the contents of the logger and issue new commands,
and report errors.
- Add a hotkey binding to launch the lua console "`"
- Adds tab completion support to the lua console
2014-11-13 11:38:58 -05:00
Chris Beck
aa19f6eecd move gui2 dialog lua functions to the lua kernel base 2014-11-10 21:13:22 -05:00
Chris Beck
1967d52a5d fixup stack discipline with error handling
When a C function terminates, it should not leave bogus elements
on the stack, or the stack can eventually overflow.

This is not as critical for C functions which are called by lua,
because they return an int explaining how many values on the stack
are important. But for C++ facing functions it is very important.
2014-11-10 17:12:55 -05:00
Chris Beck
4134ef3a80 remove comment from dev concerned about a nonissue
read here: http://www.lua.org/manual/5.1/manual.html#lua_CFunction
When the C function returns with "1", all but the top entry are
discarded from the stack. If this function could be called again
and again without ever returning and without ever dumping the stack,
then there could be a stackoverflow, but in this case it's
not possible.
2014-11-10 15:34:14 -05:00
Chris Beck
63399b47e9 move some callbacks (gettext, file ops) to lua kernel base 2014-11-10 15:34:13 -05:00
Chris Beck
76141a0de1 new code for lua protected calls in lua_kernel_base
In initial commit of the new lua kernels, I introduced a problem
by trying to use luaW_pcall and lua_pcall interchangeably via
polymorphism. This doesn't work because their return types don't
match, and it's alot of work to change luaW_pcall syntax. Besides
this there's no reason we can't use the custom error handler
everywhere.

This commits adds protected_call and load_string functions to lua
kernel base. These are an intended replacement for luaW_pcall,
and replace the "run" function. They do better error reporting
and allow to specify an error handler.

The error reporting is very flexible -- by default we select a
an error reporting function associated polymorphically to the lua
kernel, so the in-game lua kernel can send chat messages, and
others can do something else. However an arbitrary handler may
be specified, and exceptions instead of logging may also be
requested.
2014-11-10 15:32:19 -05:00