| 1 |
/* |
| 2 |
Copyright (c) 2010 Peter "Corsix" Cawley |
| 3 |
|
| 4 |
Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 |
this software and associated documentation files (the "Software"), to deal in |
| 6 |
the Software without restriction, including without limitation the rights to |
| 7 |
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 8 |
of the Software, and to permit persons to whom the Software is furnished to do |
| 9 |
so, subject to the following conditions: |
| 10 |
|
| 11 |
The above copyright notice and this permission notice shall be included in all |
| 12 |
copies or substantial portions of the Software. |
| 13 |
|
| 14 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 |
SOFTWARE. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifndef CORSIX_TH_TH_LUA_INTERNAL_H_ |
| 24 |
#define CORSIX_TH_TH_LUA_INTERNAL_H_ |
| 25 |
#include "config.h" |
| 26 |
|
| 27 |
#include <string> |
| 28 |
|
| 29 |
#include "th_lua.h" |
| 30 |
|
| 31 |
enum class lua_metatable { |
| 32 |
map, |
| 33 |
palette, |
| 34 |
sheet, |
| 35 |
font, |
| 36 |
bitmap_font, |
| 37 |
freetype_font, |
| 38 |
layers, |
| 39 |
anims, |
| 40 |
anim, |
| 41 |
pathfinder, |
| 42 |
surface, |
| 43 |
bitmap, |
| 44 |
cursor, |
| 45 |
lfs_ext, |
| 46 |
sound_archive, |
| 47 |
sound_fx, |
| 48 |
movie, |
| 49 |
string, |
| 50 |
window_base, |
| 51 |
sprite_list, |
| 52 |
string_proxy, |
| 53 |
line, |
| 54 |
iso_fs, |
| 55 |
midi_player, |
| 56 |
|
| 57 |
count |
| 58 |
}; |
| 59 |
|
| 60 |
struct lua_register_state { |
| 61 |
lua_State* L; |
| 62 |
int metatables[static_cast<size_t>(lua_metatable::count)]; |
| 63 |
int main_table; |
| 64 |
int top; |
| 65 |
}; |
| 66 |
|
| 67 |
void luaT_setclosure(const lua_register_state* pState, lua_CFunction fn, |
| 68 |
int iUps); |
| 69 |
|
| 70 |
template <typename... Args> |
| 71 |
void luaT_setclosure(const lua_register_state* pState, lua_CFunction fn, |
| 72 |
int iUps, lua_metatable eMetatable1, Args... args) { |
| 73 |
lua_pushvalue(pState->L, |
| 74 |
pState->metatables[static_cast<size_t>(eMetatable1)]); |
| 75 |
luaT_setclosure(pState, fn, iUps + 1, args...); |
| 76 |
} |
| 77 |
|
| 78 |
template <typename... Args> |
| 79 |
void luaT_setclosure(const lua_register_state* pState, lua_CFunction fn, |
| 80 |
int iUps, const char* str, Args... args) { |
| 81 |
lua_pushstring(pState->L, str); |
| 82 |
luaT_setclosure(pState, fn, iUps + 1, args...); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Add a c++ function to the lua state. |
| 87 |
* |
| 88 |
* @param pState Lua state for the game |
| 89 |
* @param fn The C/C++ function to register |
| 90 |
* @param name The name to use for the function in lua |
| 91 |
* @param args The upvalues to associate with the function in lua |
| 92 |
*/ |
| 93 |
template <typename... Args> |
| 94 |
void add_lua_function(const lua_register_state* pState, lua_CFunction fn, |
| 95 |
const char* name, Args... args) { |
| 96 |
luaT_setclosure(pState, fn, 0, args...); |
| 97 |
lua_setfield(pState->L, -2, name); |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Create a lua 'class' bound to a C++ class. |
| 102 |
* |
| 103 |
* This class should be immediately destructed after adding all of it's |
| 104 |
* functions, metamethods and constants to complete the creation of the bind. |
| 105 |
*/ |
| 106 |
template <typename T> |
| 107 |
class lua_class_binding final { |
| 108 |
public: |
| 109 |
lua_class_binding() = delete; |
| 110 |
lua_class_binding(const lua_class_binding&) = delete; |
| 111 |
lua_class_binding(lua_class_binding&&) = delete; |
| 112 |
lua_class_binding& operator=(const lua_class_binding&) = delete; |
| 113 |
lua_class_binding operator=(lua_class_binding&&) = delete; |
| 114 |
|
| 115 |
/** |
| 116 |
* Initiate class bindings for lua. |
| 117 |
* |
| 118 |
* @param pState The lua environment to bind to. |
| 119 |
* @param name The name to give this lua 'class'. |
| 120 |
* @param new_fn The function to call when a new class is created. |
| 121 |
* @param mt The metatable id for the class |
| 122 |
*/ |
| 123 |
lua_class_binding(const lua_register_state* pState, const char* name, |
| 124 |
lua_CFunction new_fn, lua_metatable mt) |
| 125 |
: pState(pState), |
| 126 |
class_name(name), |
| 127 |
class_metatable(pState->metatables[static_cast<size_t>(mt)]) { |
| 128 |
lua_settop(pState->L, pState->top); |
| 129 |
/* Make metatable the environment for registered functions */ |
| 130 |
lua_pushvalue(pState->L, class_metatable); |
| 131 |
lua_replace(pState->L, luaT_environindex); |
| 132 |
/* Set the __gc metamethod to C++ destructor */ |
| 133 |
luaT_pushcclosure(pState->L, luaT_stdgc<T, luaT_environindex>, 0); |
| 134 |
lua_setfield(pState->L, class_metatable, "__gc"); |
| 135 |
/* Set the depersist size */ |
| 136 |
lua_pushinteger(pState->L, sizeof(T)); |
| 137 |
lua_setfield(pState->L, class_metatable, "__depersist_size"); |
| 138 |
/* Create the methods table; call it -> new instance */ |
| 139 |
luaT_pushcclosuretable(pState->L, new_fn, 0); |
| 140 |
/* Set __class_name on the methods metatable */ |
| 141 |
lua_getmetatable(pState->L, -1); |
| 142 |
lua_pushstring(pState->L, class_name); |
| 143 |
lua_setfield(pState->L, -2, "__class_name"); |
| 144 |
lua_pop(pState->L, 1); |
| 145 |
/* Set __index to the methods table */ |
| 146 |
lua_pushvalue(pState->L, -1); |
| 147 |
lua_setfield(pState->L, class_metatable, "__index"); |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Set another class as the superclass of this class. |
| 152 |
* |
| 153 |
* @param super_mt The metatable id of the super class. |
| 154 |
*/ |
| 155 |
void set_superclass(lua_metatable super_mt) { |
| 156 |
lua_getmetatable(pState->L, -1); |
| 157 |
lua_getfield(pState->L, pState->metatables[static_cast<size_t>(super_mt)], |
| 158 |
"__index"); |
| 159 |
lua_setfield(pState->L, -2, "__index"); |
| 160 |
lua_pop(pState->L, 1); |
| 161 |
/* Set metatable[1] to super_mt */ |
| 162 |
lua_pushvalue(pState->L, pState->metatables[static_cast<size_t>(super_mt)]); |
| 163 |
lua_rawseti(pState->L, class_metatable, 1); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* Add a named constant to the lua interface. |
| 168 |
* |
| 169 |
* @param name (string literal) Name of the constant. |
| 170 |
* @param value (tested with int) Value of the constant. |
| 171 |
*/ |
| 172 |
template <typename V> |
| 173 |
void add_constant(const char* name, V value) { |
| 174 |
luaT_push(pState->L, value); |
| 175 |
lua_setfield(pState->L, -2, name); |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* Add a C++ metamethod to the lua class. |
| 180 |
* |
| 181 |
* @param fn The C++ function to call. |
| 182 |
* @param name The name of the metamethod (without the __ prefix). |
| 183 |
* @param args The upvalues for the function. |
| 184 |
*/ |
| 185 |
template <typename... Args> |
| 186 |
void add_metamethod(lua_CFunction fn, const char* name, Args... args) { |
| 187 |
luaT_setclosure(pState, fn, 0, args...); |
| 188 |
lua_setfield(pState->L, class_metatable, |
| 189 |
std::string("__").append(name).c_str()); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Add a C++ function to the lua class. |
| 194 |
* |
| 195 |
* @param fn The C++ function. |
| 196 |
* @param name The name of the function in lua. |
| 197 |
* @param args The upvalues for the function |
| 198 |
*/ |
| 199 |
template <typename... Args> |
| 200 |
void add_function(lua_CFunction fn, const char* name, Args... args) { |
| 201 |
add_lua_function(pState, fn, name, args...); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* Destructor which finalizes the lua binding |
| 206 |
*/ |
| 207 |
~lua_class_binding() { |
| 208 |
lua_setfield(pState->L, pState->main_table, class_name); |
| 209 |
} |
| 210 |
|
| 211 |
private: |
| 212 |
const lua_register_state* pState; |
| 213 |
const char* class_name; |
| 214 |
int class_metatable; |
| 215 |
}; |
| 216 |
|
| 217 |
#endif |