ScriptController.inl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include "ScriptController.h"
  2. namespace gameplay
  3. {
  4. template <typename T>
  5. ScriptUtil::LuaArray<T>::LuaArray(T* param)
  6. {
  7. _data = new ScriptUtil::LuaArray<T>::Data();
  8. _data->value = param;
  9. // Initial ref count of zero means no memory management
  10. _data->refCount = 0;
  11. }
  12. template <typename T>
  13. ScriptUtil::LuaArray<T>::LuaArray(int count)
  14. {
  15. _data = new ScriptUtil::LuaArray<T>::Data();
  16. // Allocate a chunk of memory to store 'count' number of T.
  17. // Use new instead of malloc since we track memory allocations
  18. // int DebugMem configurations.
  19. _data->value = (T*)new unsigned char[sizeof(T) * count];
  20. // Positive ref count means we automatically cleanup memory
  21. _data->refCount = 1;
  22. }
  23. template <typename T>
  24. ScriptUtil::LuaArray<T>::LuaArray(const ScriptUtil::LuaArray<T>& copy)
  25. {
  26. _data = copy._data;
  27. ++_data->refCount;
  28. }
  29. template <typename T>
  30. ScriptUtil::LuaArray<T>::~LuaArray()
  31. {
  32. if ((--_data->refCount) <= 0)
  33. {
  34. // Non managed arrays/pointers start with ref count zero, so only delete data if
  35. // the decremented ref count == 0 (otherwise it will be -1).
  36. if (_data->refCount == 0)
  37. {
  38. unsigned char* value = (unsigned char*)_data->value;
  39. SAFE_DELETE_ARRAY(value);
  40. }
  41. SAFE_DELETE(_data);
  42. }
  43. }
  44. template <typename T>
  45. ScriptUtil::LuaArray<T>& ScriptUtil::LuaArray<T>::operator = (const ScriptUtil::LuaArray<T>& p)
  46. {
  47. _data = p._data;
  48. ++_data->refCount;
  49. }
  50. template <typename T>
  51. void ScriptUtil::LuaArray<T>::set(unsigned int index, const T* itemPtr)
  52. {
  53. // WARNING: The following code will only work properly for arrays of pointers
  54. // to objects (i.e. T**) or for simple structs that are being passed
  55. // in as read-only. Since the memory is directly copied, any member data that
  56. // is modified with the object that is copied, will not modify the original object.
  57. // What is even scarier is that if an array of objects that contain virtual functions
  58. // is copied here, then the vtables are copied directly, meaning the new object
  59. // contains a copy of a vtable that points to functions in the old object. Calling
  60. // virtual fucntions on the new object would then call the functions on the old object.
  61. // If the old object is deleted, the vtable on the new object would point to addressess
  62. // for functions that no longer exist.
  63. if (itemPtr)
  64. memcpy((void*)&_data->value[index], (void*)itemPtr, sizeof(T));
  65. else
  66. memset((void*)&_data->value[index], 0, sizeof(T));
  67. }
  68. template <typename T>
  69. ScriptUtil::LuaArray<T>::operator T* () const
  70. {
  71. return _data->value;
  72. }
  73. template <typename T>
  74. T& ScriptUtil::LuaArray<T>::operator[] (int index)
  75. {
  76. return _data->value[index];
  77. }
  78. template<typename T>
  79. ScriptUtil::LuaArray<T> ScriptUtil::getObjectPointer(int index, const char* type, bool nonNull, bool* success)
  80. {
  81. *success = false;
  82. ScriptController* sc = Game::getInstance()->getScriptController();
  83. // Was 'nil' passed?
  84. if (lua_type(sc->_lua, index) == LUA_TNIL)
  85. {
  86. if (nonNull)
  87. {
  88. GP_WARN("Attempting to pass NULL for required non-NULL parameter at index %d (likely a reference or by-value parameter).", index);
  89. }
  90. else
  91. {
  92. // NULL values allowed
  93. *success = true;
  94. }
  95. return LuaArray<T>((T*)NULL);
  96. }
  97. // Was a Lua table passed?
  98. if (lua_type(sc->_lua, index) == LUA_TTABLE)
  99. {
  100. // Array type - assume success unless we encounter a problem below.
  101. *success = true;
  102. // Get the size of the array.
  103. lua_len(sc->_lua, index);
  104. int size = luaL_checkint(sc->_lua, -1);
  105. if (size <= 0)
  106. {
  107. // Array of size zero
  108. return LuaArray<T>((T*)NULL);
  109. }
  110. LuaArray<T> arr(size);
  111. // Loop through the lua table/array.
  112. int i = 0;
  113. lua_pushnil(sc->_lua);
  114. for (; lua_next(sc->_lua, index) != 0 && i < size; i++)
  115. {
  116. // Process each item in the array - make sure they are of valid types.
  117. void* p = lua_touserdata(sc->_lua, -1);
  118. if (p == NULL)
  119. {
  120. arr.set(i, (T*)NULL);
  121. }
  122. else
  123. {
  124. bool foundMatch = false;
  125. // Push array element metatable.
  126. if (lua_getmetatable(sc->_lua, -1))
  127. {
  128. // Push param type metatable.
  129. luaL_getmetatable(sc->_lua, type);
  130. if (lua_rawequal(sc->_lua, -1, -2))
  131. {
  132. // Pop param type metatable.
  133. lua_pop(sc->_lua, 1);
  134. // Matched the declared parameter type.
  135. arr.set(i, (T*)((ScriptUtil::LuaObject*)p)->instance);
  136. foundMatch = true;
  137. }
  138. else
  139. {
  140. // Pop param type metatable.
  141. lua_pop(sc->_lua, 1);
  142. // Check if it matches any of the derived types' metatables.
  143. const std::vector<std::string>& types = sc->_hierarchy[type];
  144. for (size_t k = 0, count = types.size(); k < count; k++)
  145. {
  146. // Push dervied type metatable.
  147. luaL_getmetatable(sc->_lua, types[k].c_str());
  148. if (lua_rawequal(sc->_lua, -1, -2))
  149. {
  150. // Pop derived type metatable.
  151. lua_pop(sc->_lua, 1);
  152. // Matched a derived type.
  153. arr.set(i, (T*)((ScriptUtil::LuaObject*)p)->instance);
  154. foundMatch = true;
  155. break;
  156. }
  157. // Pop derived type metatable
  158. lua_pop(sc->_lua, 1);
  159. }
  160. }
  161. // Pop array element metatable.
  162. lua_pop(sc->_lua, 1);
  163. }
  164. if (!foundMatch)
  165. {
  166. GP_WARN("Invalid type passed for an array element for parameter index %d.", index);
  167. arr.set(i, (T*)NULL);
  168. *success = false;
  169. }
  170. }
  171. // Pop 'value' and key 'key' for lua_next.
  172. lua_pop(sc->_lua, 1);
  173. }
  174. return arr;
  175. }
  176. // Type is not nil and not a table, so it should be USERDATA.
  177. void* p = lua_touserdata(sc->_lua, index);
  178. if (p != NULL)
  179. {
  180. // Push object metatable.
  181. if (lua_getmetatable(sc->_lua, index))
  182. {
  183. // Push param type metatable.
  184. luaL_getmetatable(sc->_lua, type);
  185. if (lua_rawequal(sc->_lua, -1, -2))
  186. {
  187. // Pop both metatables.
  188. lua_pop(sc->_lua, 2);
  189. T* ptr = (T*)((ScriptUtil::LuaObject*)p)->instance;
  190. if (ptr == NULL && nonNull)
  191. {
  192. GP_WARN("Attempting to pass NULL for required non-NULL parameter at index %d (likely a reference or by-value parameter).", index);
  193. return LuaArray<T>((T*)NULL);
  194. }
  195. // Type is valid.
  196. *success = true;
  197. return LuaArray<T>(ptr);
  198. }
  199. // Pop param type metable.
  200. lua_pop(sc->_lua, 1);
  201. // Check if it matches any of the derived types' metatables.
  202. const std::vector<std::string>& types = sc->_hierarchy[type];
  203. for (size_t i = 0, count = types.size(); i < count; i++)
  204. {
  205. // Push derived type metatable.
  206. luaL_getmetatable(sc->_lua, types[i].c_str());
  207. if (lua_rawequal(sc->_lua, -1, -2))
  208. {
  209. // Pop both metatables.
  210. lua_pop(sc->_lua, 2);
  211. T* ptr = (T*)((ScriptUtil::LuaObject*)p)->instance;
  212. if (ptr == NULL && nonNull)
  213. {
  214. GP_WARN("Attempting to pass NULL for required non-NULL parameter at index %d (likely a reference or by-value parameter).", index);
  215. return LuaArray<T>((T*)NULL);
  216. }
  217. // Type is valid (matches a derived type).
  218. *success = true;
  219. return LuaArray<T>(ptr);
  220. }
  221. // Pop derived type metatable.
  222. lua_pop(sc->_lua, 1);
  223. }
  224. // Pop object metatable.
  225. lua_pop(sc->_lua, 1);
  226. }
  227. }
  228. // If we made it here, type was not nil, and it could not be mapped to a valid object pointer.
  229. //GP_WARN("Failed to retrieve a valid object pointer of type '%s' for parameter %d.", type, index);
  230. return LuaArray<T>((T*)NULL);
  231. }
  232. template<typename T> T ScriptController::executeFunction(const char* func)
  233. {
  234. executeFunctionHelper(1, func, NULL, NULL);
  235. T value = (T)((ScriptUtil::LuaObject*)lua_touserdata(_lua, -1))->instance;
  236. lua_pop(_lua, -1);
  237. return value;
  238. }
  239. template<typename T> T ScriptController::executeFunction(const char* func, const char* args, ...)
  240. {
  241. va_list list;
  242. va_start(list, args);
  243. executeFunctionHelper(1, func, args, &list);
  244. T value = (T)((ScriptUtil::LuaObject*)lua_touserdata(_lua, -1))->instance;
  245. lua_pop(_lua, -1);
  246. va_end(list);
  247. return value;
  248. }
  249. template<typename T> T ScriptController::executeFunction(const char* func, const char* args, va_list* list)
  250. {
  251. executeFunctionHelper(1, func, args, list);
  252. T value = (T)((ScriptUtil::LuaObject*)lua_touserdata(_lua, -1))->instance;
  253. lua_pop(_lua, -1);
  254. return value;
  255. }
  256. template<typename T>T* ScriptController::getObjectPointer(const char* type, const char* name)
  257. {
  258. lua_getglobal(_lua, name);
  259. void* userdata = luaL_checkudata(_lua, -1, type);
  260. std::string msg = std::string("'") + std::string(type) + std::string("' expected.");
  261. luaL_argcheck(_lua, userdata != NULL, 1, msg.c_str());
  262. return (T*)((ScriptUtil::LuaObject*)userdata)->instance;
  263. }
  264. template<typename T>void ScriptController::setObjectPointer(const char* type, const char* name, T* v)
  265. {
  266. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(_lua, sizeof(ScriptUtil::LuaObject));
  267. object->instance = (void*)v;
  268. object->owns = false;
  269. luaL_getmetatable(_lua, type);
  270. lua_setmetatable(_lua, -2);
  271. lua_setglobal(_lua, name);
  272. }
  273. }