ScriptTarget.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. #include "Base.h"
  2. #include "ScriptController.h"
  3. #include "ScriptTarget.h"
  4. namespace gameplay
  5. {
  6. // TODO: Handle reloading scripts and then case where EventRegistries have events added AFTER a script has been loaded (i.e. need to reload callbacks for the script).
  7. extern void splitURL(const std::string& url, std::string* file, std::string* id);
  8. const char* ScriptTarget::Event::getName() const
  9. {
  10. return name.c_str();
  11. }
  12. const char* ScriptTarget::Event::getArgs() const
  13. {
  14. return args.c_str();
  15. }
  16. ScriptTarget::EventRegistry::EventRegistry()
  17. {
  18. }
  19. ScriptTarget::EventRegistry::~EventRegistry()
  20. {
  21. for (size_t i = 0, count = _events.size(); i < count; ++i)
  22. {
  23. SAFE_DELETE(_events[i]);
  24. }
  25. }
  26. const ScriptTarget::Event* ScriptTarget::EventRegistry::addEvent(const char* name, const char* args)
  27. {
  28. GP_ASSERT(name);
  29. Event* evt = new Event;
  30. evt->name = name;
  31. evt->args = args ? args : "";
  32. _events.push_back(evt);
  33. return evt;
  34. }
  35. unsigned int ScriptTarget::EventRegistry::getEventCount() const
  36. {
  37. return _events.size();
  38. }
  39. const ScriptTarget::Event* ScriptTarget::EventRegistry::getEvent(unsigned int index) const
  40. {
  41. GP_ASSERT(index < _events.size());
  42. return _events[index];
  43. }
  44. const ScriptTarget::Event* ScriptTarget::EventRegistry::getEvent(const char* name) const
  45. {
  46. GP_ASSERT(name);
  47. for (size_t i = 0, count = _events.size(); i < count; ++i)
  48. {
  49. if (_events[i]->name == name)
  50. return _events[i];
  51. }
  52. return NULL;
  53. }
  54. ScriptTarget::ScriptTarget() : _scriptRegistries(NULL), _scripts(NULL), _scriptCallbacks(NULL)
  55. {
  56. }
  57. ScriptTarget::~ScriptTarget()
  58. {
  59. // Free callbacks
  60. SAFE_DELETE(_scriptCallbacks);
  61. // Free scripts
  62. ScriptEntry* se = _scripts;
  63. while (se)
  64. {
  65. ScriptEntry* tmp = se;
  66. se = se->next;
  67. SAFE_RELEASE(tmp->script);
  68. SAFE_DELETE(tmp);
  69. }
  70. // Free registry entries
  71. RegistryEntry* re = _scriptRegistries;
  72. while (re)
  73. {
  74. RegistryEntry* tmp = re;
  75. re = re->next;
  76. // Don't delete the actual EventRegistry, since it's shared by all
  77. // ScriptTargets of the same type
  78. SAFE_DELETE(tmp);
  79. }
  80. }
  81. void ScriptTarget::registerEvents(EventRegistry* registry)
  82. {
  83. GP_ASSERT(registry);
  84. // Attach the registry
  85. RegistryEntry* re = new RegistryEntry(registry);
  86. if (_scriptRegistries)
  87. {
  88. RegistryEntry* last = _scriptRegistries;
  89. while (last->next)
  90. last = last->next;
  91. last->next = re;
  92. re->prev = last;
  93. }
  94. else
  95. {
  96. _scriptRegistries = re;
  97. }
  98. }
  99. Script* ScriptTarget::addScript(const char* path)
  100. {
  101. ScriptController* sc = Game::getInstance()->getScriptController();
  102. // Load the script
  103. Script* script = sc->loadScript(path, Script::PROTECTED);
  104. if (!script)
  105. return NULL;
  106. // Attach the script
  107. ScriptEntry* se = new ScriptEntry(script);
  108. if (_scripts)
  109. {
  110. ScriptEntry* last = _scripts;
  111. while (last->next)
  112. last = last->next;
  113. last->next = se;
  114. se->prev = last;
  115. }
  116. else
  117. {
  118. _scripts = se;
  119. }
  120. // Inspect the loaded script for event functions that are supported by this ScriptTarget.
  121. // TODO: We'll need to re-load eventCallbacks when EventRegistries change for this ScriptObject.
  122. RegistryEntry* re = _scriptRegistries;
  123. while (re)
  124. {
  125. std::vector<Event*>& events = re->registry->_events;
  126. for (size_t i = 0, count = events.size(); i < count; ++i)
  127. {
  128. const Event* event = events[i];
  129. if (sc->functionExists(event->name.c_str(), script))
  130. {
  131. if (!_scriptCallbacks)
  132. _scriptCallbacks = new std::map<const Event*, std::vector<CallbackFunction>>();
  133. (*_scriptCallbacks)[event].push_back(CallbackFunction(script, event->name.c_str()));
  134. }
  135. }
  136. re = re->next;
  137. }
  138. // Automatically call the 'attached' event if it is defined within the script
  139. if (sc->functionExists("attached", script))
  140. {
  141. char args[256];
  142. sprintf(args, "<%s>", getTypeName());
  143. sc->executeFunction<void>(script, "attached", args, dynamic_cast<void*>(this));
  144. }
  145. return script;
  146. }
  147. bool ScriptTarget::removeScript(const char* path)
  148. {
  149. GP_ASSERT(path);
  150. ScriptEntry* se = _scripts;
  151. while (se)
  152. {
  153. if (strcmp(se->script->getPath(), path) == 0 && se->script->getScope() == Script::PROTECTED)
  154. {
  155. removeScript(se);
  156. return true;
  157. }
  158. se = se->next;
  159. }
  160. return false;
  161. }
  162. void ScriptTarget::removeScript(ScriptEntry* se)
  163. {
  164. GP_ASSERT(se);
  165. // Link out this ScriptEntry
  166. if (se->prev)
  167. se->prev->next = se->next;
  168. if (se->next)
  169. se->next->prev = se->prev;
  170. if (_scripts == se)
  171. _scripts = se->next;
  172. Script* script = se->script;
  173. // Delete the ScriptEntry
  174. SAFE_DELETE(se);
  175. // Erase any callback functions registered for this script
  176. if (_scriptCallbacks)
  177. {
  178. std::map<const Event*, std::vector<CallbackFunction>>::iterator itr = _scriptCallbacks->begin();
  179. for ( ; itr != _scriptCallbacks->end(); ++itr)
  180. {
  181. std::vector<CallbackFunction>& callbacks = itr->second;
  182. std::vector<CallbackFunction>::iterator itr2 = callbacks.begin();
  183. while (itr2 != callbacks.end())
  184. {
  185. if (itr2->script == script)
  186. itr2 = callbacks.erase(itr2);
  187. else
  188. ++itr2;
  189. }
  190. }
  191. }
  192. // Free the script
  193. SAFE_RELEASE(script);
  194. }
  195. void ScriptTarget::addScriptCallback(const Event* event, const char* function)
  196. {
  197. GP_ASSERT(event);
  198. GP_ASSERT(function);
  199. // Parse the script name (if it exists) and function out
  200. std::string scriptPath, func;
  201. splitURL(function, &scriptPath, &func);
  202. if (func.length() == 0)
  203. {
  204. // The url doesn't reference a script, only a function
  205. func = scriptPath;
  206. scriptPath = "";
  207. }
  208. // Have we already loaded this global script?
  209. bool loaded = true;
  210. Script* script = NULL;
  211. if (!scriptPath.empty())
  212. {
  213. loaded = false;
  214. ScriptEntry* se = _scripts;
  215. while (se)
  216. {
  217. if (scriptPath == se->script->getPath() && se->script->getScope() == Script::GLOBAL)
  218. {
  219. // Script is already loaded
  220. script = se->script;
  221. loaded = true;
  222. break;
  223. }
  224. se = se->next;
  225. }
  226. }
  227. if (!loaded)
  228. {
  229. // The specified global script is not yet loaded, so do so
  230. script = Game::getInstance()->getScriptController()->loadScript(scriptPath.c_str(), Script::GLOBAL);
  231. if (script)
  232. {
  233. loaded = true;
  234. ScriptEntry* se = new ScriptEntry(script);
  235. if (_scripts)
  236. {
  237. ScriptEntry* last = _scripts;
  238. while (last->next)
  239. last = last->next;
  240. last->next = se;
  241. se->prev = last;
  242. }
  243. else
  244. {
  245. _scripts = se;
  246. }
  247. }
  248. else
  249. {
  250. GP_WARN("Failed to load script '%s' for script target while registering for function: %s", scriptPath.c_str(), function);
  251. }
  252. }
  253. if (loaded)
  254. {
  255. // Store the callback
  256. if (!_scriptCallbacks)
  257. _scriptCallbacks = new std::map<const Event*, std::vector<CallbackFunction>>();
  258. (*_scriptCallbacks)[event].push_back(CallbackFunction(script, func.c_str()));
  259. }
  260. }
  261. void ScriptTarget::removeScriptCallback(const Event* event, const char* function)
  262. {
  263. // Parse the script name (if it exists) and function out
  264. std::string scriptPath, func;
  265. splitURL(function, &scriptPath, &func);
  266. if (func.length() == 0)
  267. {
  268. // The url doesn't reference a script, only a function
  269. func = scriptPath;
  270. scriptPath = "";
  271. }
  272. // Find the script entry for this callback
  273. ScriptEntry* scriptEntry = NULL;
  274. if (!scriptPath.empty())
  275. {
  276. ScriptEntry* se = _scripts;
  277. while (se)
  278. {
  279. if (scriptPath == se->script->getPath() && se->script->getScope() == Script::GLOBAL)
  280. {
  281. scriptEntry = se;
  282. break;
  283. }
  284. se = se->next;
  285. }
  286. }
  287. Script* script = scriptEntry ? scriptEntry->script : NULL;
  288. // Remove any registered callback functions that match the specified one
  289. int removedCallbacks = 0;
  290. int totalCallbacks = 0;
  291. if (_scriptCallbacks)
  292. {
  293. std::map<const Event*, std::vector<CallbackFunction>>::iterator itr = _scriptCallbacks->begin();
  294. for (; itr != _scriptCallbacks->end(); ++itr)
  295. {
  296. // Erase matching callback functions for this event
  297. bool forEvent = itr->first == event;
  298. std::vector<CallbackFunction>& callbacks = itr->second;
  299. std::vector<CallbackFunction>::iterator itr2 = callbacks.begin();
  300. while (itr2 != callbacks.end())
  301. {
  302. if (itr2->script == script)
  303. {
  304. ++totalCallbacks; // sum total number of callbacks found for this script
  305. if (forEvent && itr2->function == func)
  306. {
  307. itr2 = callbacks.erase(itr2);
  308. ++removedCallbacks; // sum number of callbacks removed
  309. }
  310. else
  311. ++itr2;
  312. }
  313. }
  314. }
  315. }
  316. // Cleanup the script if there are no remaining callbacks for it
  317. if (scriptEntry && (totalCallbacks - removedCallbacks) <= 0)
  318. {
  319. removeScript(scriptEntry);
  320. }
  321. }
  322. void ScriptTarget::clearScripts()
  323. {
  324. while (_scripts)
  325. {
  326. removeScript(_scripts);
  327. }
  328. }
  329. bool ScriptTarget::hasScriptListener(const char* eventName) const
  330. {
  331. const Event* event = getScriptEvent(eventName);
  332. return event ? hasScriptListener(event) : false;
  333. }
  334. const ScriptTarget::Event* ScriptTarget::getScriptEvent(const char* eventName) const
  335. {
  336. GP_ASSERT(eventName);
  337. // Lookup the event for this name
  338. const Event* event = NULL;
  339. RegistryEntry* re = _scriptRegistries;
  340. while (re)
  341. {
  342. if ((event = re->registry->getEvent(eventName)) != NULL)
  343. break;
  344. re = re->next;
  345. }
  346. return event;
  347. }
  348. bool ScriptTarget::hasScriptListener(const Event* event) const
  349. {
  350. GP_ASSERT(event);
  351. if (_scriptCallbacks)
  352. {
  353. std::map<const Event*, std::vector<CallbackFunction>>::iterator itr = _scriptCallbacks->find(event);
  354. if (itr != _scriptCallbacks->end())
  355. {
  356. return !itr->second.empty();
  357. }
  358. }
  359. return false;
  360. }
  361. template<> void ScriptTarget::fireScriptEvent<void>(const Event* event, ...)
  362. {
  363. GP_ASSERT(event);
  364. if (!_scriptCallbacks)
  365. return; // no registered callbacks
  366. va_list list;
  367. va_start(list, event);
  368. // Lookup registered callbacks for this event and fire them
  369. std::map<const Event*, std::vector<CallbackFunction>>::iterator itr = _scriptCallbacks->find(event);
  370. if (itr != _scriptCallbacks->end())
  371. {
  372. ScriptController* sc = Game::getInstance()->getScriptController();
  373. std::vector<CallbackFunction>& callbacks = itr->second;
  374. for (size_t i = 0, count = callbacks.size(); i < count; ++i)
  375. {
  376. CallbackFunction& cb = callbacks[i];
  377. sc->executeFunction<void>(cb.script, cb.function.c_str(), event->args.c_str(), &list);
  378. }
  379. }
  380. va_end(list);
  381. }
  382. template<> bool ScriptTarget::fireScriptEvent<bool>(const Event* event, ...)
  383. {
  384. GP_ASSERT(event);
  385. if (!_scriptCallbacks)
  386. return false; // no registered callbacks
  387. va_list list;
  388. va_start(list, event);
  389. // Lookup registered callbacks for this event and fire them
  390. std::map<const Event*, std::vector<CallbackFunction>>::iterator itr = _scriptCallbacks->find(event);
  391. if (itr != _scriptCallbacks->end())
  392. {
  393. ScriptController* sc = Game::getInstance()->getScriptController();
  394. std::vector<CallbackFunction>& callbacks = itr->second;
  395. for (size_t i = 0, count = callbacks.size(); i < count; ++i)
  396. {
  397. CallbackFunction& cb = callbacks[i];
  398. if (sc->executeFunction<bool>(cb.script, cb.function.c_str(), event->args.c_str(), &list))
  399. {
  400. va_end(list);
  401. return true;
  402. }
  403. }
  404. }
  405. va_end(list);
  406. return false;
  407. }
  408. }