The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
lua_kernel_base.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Chris Beck <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef SCRIPTING_LUA_KERNEL_BASE_HPP
16 #define SCRIPTING_LUA_KERNEL_BASE_HPP
17 
18 #include <sstream>
19 #include <string>
20 #include <vector>
21 #include "utils/functional.hpp"
22 #include <boost/cstdint.hpp>
23 
24 struct lua_State;
25 class CVideo;
26 class config;
27 
29 public:
30  lua_kernel_base(CVideo * ptr);
31  virtual ~lua_kernel_base();
32 
33  /** Runs a [lua] tag. Doesn't throw lua_error.*/
34  void run_lua_tag(const config& cfg);
35 
36  /** Runs a plain script. Doesn't throw lua_error.*/
37  void run(char const *prog, int nArgs = 0);
38 
39  /** Runs a plain script, but reports errors by throwing lua_error.*/
40  void throwing_run(char const * prog, int nArgs);
41 
42  /** Tests if a program resolves to an expression, and pretty prints it if it is, otherwise it runs it normally. Throws exceptions.*/
43  void interactive_run(char const * prog);
44 
45  void load_package();
46 
47  std::vector<std::string> get_global_var_names();
48  std::vector<std::string> get_attribute_names(const std::string & var_path);
49 
50  virtual std::string my_name() { return "Basic Lua Kernel"; }
51 
52  const std::stringstream & get_log() { cmd_log_.log_ << std::flush; return cmd_log_.log_; }
53  void clear_log() { cmd_log_.log_.str(""); cmd_log_.log_.clear(); }
54  void set_external_log( std::ostream * lg ) { cmd_log_.external_log_ = lg; }
55 
56  virtual void log_error(char const* msg, char const* context = "Lua error");
57  virtual void throw_exception(char const* msg, char const* context = "Lua error"); //throws game::lua_error
58 
59  typedef std::function<void(char const*, char const*)> error_handler;
60 
61  void set_video(CVideo * ptr) { video_ = ptr; }
62 
63  template<typename T>
64  static T& get_lua_kernel(lua_State *L)
65  {
66  return *static_cast<T*>(get_lua_kernel_base_ptr(L));
67  }
68 
70  lua_State * get_state() { return mState; }
71 protected:
73 
75 
76  struct command_log {
77  std::stringstream log_;
78  std::ostream * external_log_;
79 
81  : log_()
82  , external_log_(nullptr)
83  {}
84 
85  inline command_log & operator<< (const std::string & str) {
86  log_ << str;
87  if (external_log_) {
88  (*external_log_) << str;
89  }
90  return *this;
91  }
92 
93  inline command_log & operator<< (char const* str) {
94  if (str != nullptr) {
95  log_ << str;
96  if (external_log_) {
97  (*external_log_) << str;
98  }
99  }
100  return *this;
101  }
102  };
103 
105 
106  // Print text to the command log for this lua kernel. Used as a replacement impl for lua print.
107  int intf_print(lua_State * L);
108 
109  // Show a dialog to the currently connected video object (if available)
110  int intf_show_dialog(lua_State * L);
111 
112  // Show a message dialog, possibly with options
114 
115  // Show a transient popup message
117 
118  // Show the interactive lua console (for debugging purposes)
120 
121  // Execute a protected call. Error handler is called in case of an error, using syntax for log_error and throw_exception above. Returns true if successful.
122  bool protected_call(int nArgs, int nRets, error_handler);
123  // Execute a protected call, taking a lua_State as argument. For functions pushed into the lua environment, this version should be used, or the function cannot be used by coroutines without segfaulting (since they have a different lua_State pointer). This version is called by the above version.
124  static bool protected_call(lua_State * L, int nArgs, int nRets, error_handler);
125  // Load a string onto the stack as a function. Returns true if successful, error handler is called if not.
126  bool load_string(char const * prog, error_handler);
127 
128  virtual bool protected_call(int nArgs, int nRets); // select default error handler polymorphically
129  virtual bool load_string(char const * prog); // select default error handler polymorphically
130 
131  // dofile (using lua_fileops)
132  int intf_dofile(lua_State * L);
133 
134  // require (using lua_fileops, protected_call)
135  int intf_require(lua_State * L);
136 private:
138 };
139 
140 #endif
void interactive_run(char const *prog)
Tests if a program resolves to an expression, and pretty prints it if it is, otherwise it runs it nor...
std::function< void(char const *, char const *)> error_handler
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
Definition: video.hpp:58
command_log & operator<<(const std::string &str)
virtual ~lua_kernel_base()
int intf_show_dialog(lua_State *L)
lua_kernel_base(CVideo *ptr)
static T & get_lua_kernel(lua_State *L)
void throwing_run(char const *prog, int nArgs)
Runs a plain script, but reports errors by throwing lua_error.
int intf_show_lua_console(lua_State *L)
void load_package()
Loads the "package" package into the Lua environment.
bool protected_call(int nArgs, int nRets, error_handler)
bool load_string(char const *prog, error_handler)
void run(char const *prog, int nArgs=0)
Runs a plain script.
Definition: pump.hpp:42
int intf_print(lua_State *L)
Replacement print function – instead of printing to std::cout, print to the command log...
void set_video(CVideo *ptr)
static std::string flush(std::ostringstream &s)
Definition: reports.cpp:86
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:112
virtual void log_error(char const *msg, char const *context="Lua error")
void set_external_log(std::ostream *lg)
virtual boost::uint32_t get_random_seed()
std::vector< std::string > get_global_var_names()
Gets all the global variable names in the Lua environment.
command_log cmd_log_
std::vector< std::string > get_attribute_names(const std::string &var_path)
Gets all attribute names of an extended variable name.
const std::stringstream & get_log()
virtual void throw_exception(char const *msg, char const *context="Lua error")
lua_State * get_state()
virtual std::string my_name()
int intf_show_message_dialog(lua_State *L)
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
int intf_dofile(lua_State *L)
Loads and executes a Lua file.
lua_State * mState
GLsizei const GLcharARB ** string
Definition: glew.h:4503
int intf_require(lua_State *L)
Loads and executes a Lua file, if there is no corresponding entry in wesnoth.package.
void run_lua_tag(const config &cfg)
Runs a [lua] tag.
static lua_kernel_base *& get_lua_kernel_base_ptr(lua_State *L)
int intf_show_popup_dialog(lua_State *L)