Lib
QOLを高める
script.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "util.h"
4 #include "framework.h"
5 #include <lua.h>
6 #include <lualib.h>
7 #include <lauxlib.h>
8 #include "script_debugger.h"
9 #include "script_export.h"
10 
11 namespace yappy {
13 namespace lua {
14 
15 class LuaError : public std::runtime_error {
16 public:
17  LuaError(const std::string &msg, lua_State *L);
18  const char *what() const override
19  { return m_what.c_str(); }
20 private:
21  std::string m_what;
22 };
23 
33 class Lua : private util::noncopyable {
34 public:
42  Lua(bool debugEnable, size_t maxHeapSize, size_t initHeapSize = 1024 * 1024,
43  int instLimit = 10 * 10000);
46  ~Lua();
47 
51  lua_State *getLuaState() const;
52 
53  void loadTraceLib();
54  void loadSysLib();
55  void loadRandLib();
56  void loadResourceLib(framework::Application *app);
57  void loadGraphLib(framework::Application *app);
58  void loadSoundLib(framework::Application *app);
59 
65  void loadFile(const wchar_t *fileName, bool autoBreak, bool prot = true);
66 
67  struct doNothing {
68  void operator ()(lua_State *L) {}
69  };
70 
91  template <class ParamFunc = doNothing, class RetFunc = doNothing>
92  void callGlobal(const char *funcName, bool autoBreak,
93  ParamFunc pushArgFunc = doNothing(), int narg = 0,
94  RetFunc getRetFunc = doNothing(), int nret = 0)
95  {
96  lua_State *L = m_lua.get();
97  lua_getglobal(L, funcName);
98  // push args
99  pushArgFunc(L);
100  // pcall
101  pcallInternal(narg, nret, autoBreak);
102  // get results
103  getRetFunc(L);
104  // clear stack
105  lua_settop(L, 0);
106  }
107 
108 private:
109  static const DWORD HeapOption = HEAP_NO_SERIALIZE;
110 
111  struct LuaDeleter {
112  void operator()(lua_State *L);
113  };
114 
115  bool m_debugEnable;
116  util::HeapPtr m_heap;
117  std::unique_ptr<lua_State, LuaDeleter> m_lua;
118  std::unique_ptr<debugger::LuaDebugger> m_dbg;
119 
120  // custom allocator
121  static void *luaAlloc(void *ud, void *ptr, size_t osize, size_t nsize);
122 
123  void pcallInternal(int narg, int nret, bool autoBreak);
124 };
125 
126 std::vector<std::string> luaValueToStrList(
127  lua_State *L, int ind, int maxDepth);
128 
129 } // namespace lua
130 } // namespace yappy
Noncopyable class.
Definition: util.h:24
LuaError(const std::string &msg, lua_State *L)
Definition: script.cpp:12
void callGlobal(const char *funcName, bool autoBreak, ParamFunc pushArgFunc=doNothing(), int narg=0, RetFunc getRetFunc=doNothing(), int nret=0)
Calls global function.
Definition: script.h:92
Game application main framework classes.
std::vector< uint8_t > loadFile(const wchar_t *fileName)
Load file from abstract file system.
Definition: file.cpp:103
const char * what() const override
Definition: script.h:18
Luaへエクスポートする関数群。
std::unique_ptr< HANDLE, heapDeleter > HeapPtr
unique_ptr of HANDLE with heapDeleter.
Definition: util.h:77
Definition: config.cpp:6
Utilities.
char msg[LINE_DATA_SIZE-sizeof(LARGE_INTEGER)-sizeof(uint32_t)]
Definition: debug.cpp:159
Lua state manager.
Definition: script.h:33
User application base, which manages a window and DirectX objects.
Definition: framework.h:300
std::vector< std::string > luaValueToStrList(lua_State *L, int ind, int maxDepth)
Definition: script.cpp:346