ScriptController.inl 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. lua_pop(sc->_lua, 1);
  106. if (size <= 0)
  107. {
  108. // Array of size zero
  109. return LuaArray<T>((T*)NULL);
  110. }
  111. LuaArray<T> arr(size);
  112. // Loop through the lua table/array.
  113. int i = 0;
  114. lua_pushnil(sc->_lua);
  115. for (; lua_next(sc->_lua, index) != 0 && i < size; i++)
  116. {
  117. // Process each item in the array - make sure they are of valid types.
  118. void* p = lua_touserdata(sc->_lua, -1);
  119. if (p == NULL)
  120. {
  121. arr.set(i, (T*)NULL);
  122. }
  123. else
  124. {
  125. bool foundMatch = false;
  126. // Push array element metatable.
  127. if (lua_getmetatable(sc->_lua, -1))
  128. {
  129. // Push param type metatable.
  130. luaL_getmetatable(sc->_lua, type);
  131. if (lua_rawequal(sc->_lua, -1, -2))
  132. {
  133. // Pop param type metatable.
  134. lua_pop(sc->_lua, 1);
  135. // Matched the declared parameter type.
  136. arr.set(i, (T*)((ScriptUtil::LuaObject*)p)->instance);
  137. foundMatch = true;
  138. }
  139. else
  140. {
  141. // Pop param type metatable.
  142. lua_pop(sc->_lua, 1);
  143. // Check if it matches any of the derived types' metatables.
  144. const std::vector<std::string>& types = sc->_hierarchy[type];
  145. for (size_t k = 0, count = types.size(); k < count; k++)
  146. {
  147. // Push dervied type metatable.
  148. luaL_getmetatable(sc->_lua, types[k].c_str());
  149. if (lua_rawequal(sc->_lua, -1, -2))
  150. {
  151. // Pop derived type metatable.
  152. lua_pop(sc->_lua, 1);
  153. // Matched a derived type.
  154. arr.set(i, (T*)((ScriptUtil::LuaObject*)p)->instance);
  155. foundMatch = true;
  156. break;
  157. }
  158. // Pop derived type metatable
  159. lua_pop(sc->_lua, 1);
  160. }
  161. }
  162. // Pop array element metatable.
  163. lua_pop(sc->_lua, 1);
  164. }
  165. if (!foundMatch)
  166. {
  167. GP_WARN("Invalid type passed for an array element for parameter index %d.", index);
  168. arr.set(i, (T*)NULL);
  169. *success = false;
  170. }
  171. }
  172. // Pop 'value' and key 'key' for lua_next.
  173. lua_pop(sc->_lua, 1);
  174. }
  175. return arr;
  176. }
  177. // Type is not nil and not a table, so it should be USERDATA.
  178. void* p = lua_touserdata(sc->_lua, index);
  179. if (p != NULL)
  180. {
  181. // Push object metatable.
  182. if (lua_getmetatable(sc->_lua, index))
  183. {
  184. // Push param type metatable.
  185. luaL_getmetatable(sc->_lua, type);
  186. if (lua_rawequal(sc->_lua, -1, -2))
  187. {
  188. // Pop both metatables.
  189. lua_pop(sc->_lua, 2);
  190. T* ptr = (T*)((ScriptUtil::LuaObject*)p)->instance;
  191. if (ptr == NULL && nonNull)
  192. {
  193. GP_WARN("Attempting to pass NULL for required non-NULL parameter at index %d (likely a reference or by-value parameter).", index);
  194. return LuaArray<T>((T*)NULL);
  195. }
  196. // Type is valid.
  197. *success = true;
  198. return LuaArray<T>(ptr);
  199. }
  200. // Pop param type metable.
  201. lua_pop(sc->_lua, 1);
  202. // Check if it matches any of the derived types' metatables.
  203. const std::vector<std::string>& types = sc->_hierarchy[type];
  204. for (size_t i = 0, count = types.size(); i < count; i++)
  205. {
  206. // Push derived type metatable.
  207. luaL_getmetatable(sc->_lua, types[i].c_str());
  208. if (lua_rawequal(sc->_lua, -1, -2))
  209. {
  210. // Pop both metatables.
  211. lua_pop(sc->_lua, 2);
  212. T* ptr = (T*)((ScriptUtil::LuaObject*)p)->instance;
  213. if (ptr == NULL && nonNull)
  214. {
  215. GP_WARN("Attempting to pass NULL for required non-NULL parameter at index %d (likely a reference or by-value parameter).", index);
  216. return LuaArray<T>((T*)NULL);
  217. }
  218. // Type is valid (matches a derived type).
  219. *success = true;
  220. return LuaArray<T>(ptr);
  221. }
  222. // Pop derived type metatable.
  223. lua_pop(sc->_lua, 1);
  224. }
  225. // Pop object metatable.
  226. lua_pop(sc->_lua, 1);
  227. }
  228. }
  229. // If we made it here, type was not nil, and it could not be mapped to a valid object pointer.
  230. //GP_WARN("Failed to retrieve a valid object pointer of type '%s' for parameter %d.", type, index);
  231. return LuaArray<T>((T*)NULL);
  232. }
  233. template<typename T> T ScriptController::executeFunction(const char* func)
  234. {
  235. executeFunctionHelper(1, func, NULL, NULL);
  236. T value = (T)((ScriptUtil::LuaObject*)lua_touserdata(_lua, -1))->instance;
  237. lua_pop(_lua, -1);
  238. return value;
  239. }
  240. template<typename T> T ScriptController::executeFunction(const char* func, const char* args, ...)
  241. {
  242. va_list list;
  243. va_start(list, args);
  244. executeFunctionHelper(1, func, args, &list);
  245. T value = (T)((ScriptUtil::LuaObject*)lua_touserdata(_lua, -1))->instance;
  246. lua_pop(_lua, -1);
  247. va_end(list);
  248. return value;
  249. }
  250. template<typename T> T ScriptController::executeFunction(const char* func, const char* args, va_list* list)
  251. {
  252. executeFunctionHelper(1, func, args, list);
  253. T value = (T)((ScriptUtil::LuaObject*)lua_touserdata(_lua, -1))->instance;
  254. lua_pop(_lua, -1);
  255. return value;
  256. }
  257. template<typename T>T* ScriptController::getObjectPointer(const char* type, const char* name)
  258. {
  259. lua_getglobal(_lua, name);
  260. void* userdata = luaL_checkudata(_lua, -1, type);
  261. std::string msg = std::string("'") + std::string(type) + std::string("' expected.");
  262. luaL_argcheck(_lua, userdata != NULL, 1, msg.c_str());
  263. return (T*)((ScriptUtil::LuaObject*)userdata)->instance;
  264. }
  265. template<typename T>void ScriptController::setObjectPointer(const char* type, const char* name, T* v)
  266. {
  267. ScriptUtil::LuaObject* object = (ScriptUtil::LuaObject*)lua_newuserdata(_lua, sizeof(ScriptUtil::LuaObject));
  268. object->instance = (void*)v;
  269. object->owns = false;
  270. luaL_getmetatable(_lua, type);
  271. lua_setmetatable(_lua, -2);
  272. lua_setglobal(_lua, name);
  273. }
  274. }