|
11 | 11 | #include "lua.h" |
12 | 12 | #include "lualib.h" |
13 | 13 |
|
| 14 | +#include "LuaBridge/LuaBridge.h" |
| 15 | +#include "LuaBridge/Vector.h" |
| 16 | + |
14 | 17 | namespace Script |
15 | 18 | { |
16 | 19 | class LuaScript |
17 | 20 | { |
18 | 21 | lua_State* mState; |
| 22 | + static std::shared_ptr<LuaScript> mInstance; |
19 | 23 |
|
20 | 24 | public: |
21 | | - LuaScript(); |
22 | 25 | ~LuaScript(); |
23 | 26 |
|
24 | | - template <typename T> void registerGlobalType(); |
| 27 | + template <typename T> void registerType(); |
25 | 28 |
|
26 | 29 | template <typename Func> void registerGlobalFunction(const std::string& functionName, Func&& func) |
27 | 30 | { |
28 | | - lua_pushcfunction(mState, func); |
29 | | - lua_setglobal(mState, functionName.c_str()); |
| 31 | + luabridge::getGlobalNamespace(mState).addFunction(functionName.c_str(), func); |
30 | 32 | } |
31 | 33 |
|
32 | | - template <typename T> void pushObject(T& obj, const char* varName) |
| 34 | + template <typename T> void pushGlobalObject(T& obj, const char* varName) |
33 | 35 | { |
34 | | - static bool registered = false; |
35 | | - if (!registered) |
36 | | - { |
37 | | - registerGlobalType<T>(); |
38 | | - registered = true; |
39 | | - } |
40 | | - |
41 | | - T** udata = reinterpret_cast<T**>(lua_newuserdata(mState, sizeof(T*))); |
42 | | - (*udata) = &obj; |
43 | | - const std::string typeName = typeid(T).name(); |
44 | | - luaL_setmetatable(mState, typeName.substr(1).c_str()); |
| 36 | + registerType<T>(); |
| 37 | + luabridge::push(mState, obj); |
45 | 38 | lua_setglobal(mState, varName); |
46 | 39 | } |
47 | 40 |
|
48 | 41 | template <typename T> T get(const std::string& variable) |
49 | 42 | { |
50 | | - release_assert(mState); |
51 | | - int level; |
52 | | - |
53 | | - T result; |
54 | | - if (luaGetToStack(variable, level)) |
55 | | - result = luaGet<T>(variable); |
56 | | - |
57 | | - else |
58 | | - result = luaGetDefault<T>(); |
59 | | - |
60 | | - lua_pop(mState, level + 1); |
61 | | - return result; |
62 | | - } |
63 | | - |
64 | | - template <typename T> std::vector<T> getVector(const std::string& variable) |
65 | | - { |
66 | | - std::vector<T> ret; |
67 | | - lua_getglobal(mState, variable.c_str()); |
68 | | - if (lua_isnil(mState, -1)) |
69 | | - { |
70 | | - return {}; |
71 | | - } |
72 | | - |
73 | | - lua_pushnil(mState); |
74 | | - |
75 | | - while (lua_next(mState, -2)) |
76 | | - { |
77 | | - T aux; |
78 | | - if constexpr (std::is_same<T, std::string>::value) |
79 | | - { |
80 | | - ret.push_back(lua_tostring(mState, -1)); |
81 | | - } |
82 | | - |
83 | | - else |
84 | | - { |
85 | | - ret.push_back(static_cast<T>(lua_tonumber(mState, -1))); |
86 | | - } |
87 | | - |
88 | | - lua_pop(mState, 1); |
89 | | - } |
90 | | - |
91 | | - clean(); |
92 | | - return ret; |
| 43 | + luabridge::LuaRef ret = luabridge::getGlobal(mState, variable.c_str()); |
| 44 | + return ret.cast<T>(); |
93 | 45 | } |
94 | 46 |
|
95 | 47 | void runScript(const std::string& path); |
96 | 48 | void eval(const char* script); |
97 | 49 |
|
98 | | - private: |
99 | | - bool luaGetToStack(const std::string& variable, int& level); |
100 | | - |
101 | | - void clean(); |
102 | | - |
103 | | - void printError(const std::string& variable, const std::string& reason); |
| 50 | + static std::shared_ptr<LuaScript> getInstance() |
| 51 | + { |
| 52 | + if (!mInstance) |
| 53 | + mInstance = std::shared_ptr<LuaScript>(new LuaScript()); |
104 | 54 |
|
105 | | - template <typename T> T luaGet(const std::string& variable = ""); |
| 55 | + return mInstance; |
| 56 | + } |
106 | 57 |
|
107 | | - template <typename T> T luaGetDefault() { return {}; } |
| 58 | + operator lua_State*() { return mState; } |
108 | 59 |
|
109 | | - template <typename Func> void pushFunction(Func&& f, const char* funcName) |
110 | | - { |
111 | | - lua_pushcfunction(mState, f); |
112 | | - lua_setfield(mState, -2, funcName); |
113 | | - } |
| 60 | + private: |
| 61 | + LuaScript(); |
114 | 62 | }; |
115 | 63 | } |
0 commit comments