nodeListManager.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all 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
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "environment/nodeListManager.h"
  23. #include "core/module.h"
  24. #include "core/stream/bitStream.h"
  25. MODULE_BEGIN( NodeListManager )
  26. MODULE_INIT
  27. {
  28. AssertFatal(gClientNodeListManager == NULL && gServerNodeListManager == NULL, "Error, already initialized the node list manager!");
  29. gClientNodeListManager = new NodeListManager(false);
  30. gServerNodeListManager = new NodeListManager(true);
  31. }
  32. MODULE_SHUTDOWN
  33. {
  34. AssertFatal(gClientNodeListManager != NULL && gServerNodeListManager != NULL, "Error, node list manager not initialized!");
  35. delete gClientNodeListManager;
  36. gClientNodeListManager = NULL;
  37. delete gServerNodeListManager;
  38. gServerNodeListManager = NULL;
  39. }
  40. MODULE_END;
  41. //-----------------------------------------------------------------------------
  42. // NodeListEvent Class
  43. //-----------------------------------------------------------------------------
  44. NodeListEvent::~NodeListEvent()
  45. {
  46. if (mNodeList)
  47. {
  48. // This means the node list wasn't processed
  49. delete mNodeList;
  50. }
  51. }
  52. void NodeListEvent::pack(NetConnection* conn, BitStream* stream)
  53. {
  54. stream->write(mId);
  55. stream->write(mTotalNodes);
  56. stream->write(mLocalListStart);
  57. // NOTE: Child class needs to transmit the nodes
  58. }
  59. void NodeListEvent::write(NetConnection* conn, BitStream *stream)
  60. {
  61. pack(conn, stream);
  62. }
  63. void NodeListEvent::unpack(NetConnection* conn, BitStream* stream)
  64. {
  65. stream->read(&mId);
  66. stream->read(&mTotalNodes);
  67. stream->read(&mLocalListStart);
  68. mNodeList->mId = mId;
  69. // NOTE: Child class needs to populate the local node list
  70. }
  71. void NodeListEvent::process(NetConnection* conn)
  72. {
  73. if (mNodeList)
  74. {
  75. NodeListManager::NodeList* oldList = NULL;
  76. gClientNodeListManager->findListById(mNodeList->mId, &oldList, false);
  77. mergeLists(oldList);
  78. gClientNodeListManager->addNodeList(mNodeList);
  79. mNodeList = NULL;
  80. }
  81. }
  82. void NodeListEvent::mergeLists(NodeListManager::NodeList* oldList)
  83. {
  84. if (oldList)
  85. {
  86. if ( !mNodeList->mListComplete)
  87. {
  88. copyIntoList( oldList );
  89. // Is the node list now complete?
  90. oldList->mTotalValidNodes += mNodeList->mTotalValidNodes;
  91. if (oldList->mTotalValidNodes >= mTotalNodes)
  92. {
  93. oldList->mListComplete = true;
  94. }
  95. delete mNodeList;
  96. mNodeList = oldList;
  97. }
  98. }
  99. else
  100. {
  101. padListToSize();
  102. }
  103. }
  104. IMPLEMENT_CO_NETEVENT_V1(NodeListEvent);
  105. ConsoleDocClass( NodeListEvent,
  106. "@brief Base class for events used by node editors, like River\n\n"
  107. "Editor use only.\n\n"
  108. "@internal"
  109. );
  110. //-----------------------------------------------------------------------------
  111. // NodeListManager Class
  112. //-----------------------------------------------------------------------------
  113. NodeListManager* gClientNodeListManager = NULL;
  114. NodeListManager* gServerNodeListManager = NULL;
  115. U32 NodeListManager::smMaximumNodesPerEvent = 20;
  116. //-----------------------------------------------------------------------------
  117. NodeListManager::NodeListManager(const bool isServer)
  118. {
  119. mIsServer = isServer;
  120. mNextListId = 0;
  121. }
  122. NodeListManager::~NodeListManager()
  123. {
  124. clearAllNotifications();
  125. clearNodeLists();
  126. }
  127. void NodeListManager::clearNodeLists()
  128. {
  129. for ( U32 i=0; i<mNodeLists.size(); ++i )
  130. {
  131. delete mNodeLists[i];
  132. }
  133. mNodeLists.clear();
  134. }
  135. U32 NodeListManager::nextListId()
  136. {
  137. U32 id = mNextListId;
  138. ++mNextListId;
  139. return id;
  140. }
  141. void NodeListManager::addNodeList( NodeList* list )
  142. {
  143. // Before we store the node list, we should check if anyone has registered
  144. // an interest in it, but only if the list is complete.
  145. if (list->mListComplete)
  146. {
  147. if (dispatchNotification( list ))
  148. {
  149. delete list;
  150. return;
  151. }
  152. }
  153. // Nothing is registered or the list is not complete, so store the list for later
  154. mNodeLists.push_back( list );
  155. }
  156. bool NodeListManager::findListById(U32 id, NodeList** list, bool completeOnly)
  157. {
  158. *list = NULL;
  159. for (U32 i=0; i<mNodeLists.size(); ++i)
  160. {
  161. if (mNodeLists[i]->mId == id)
  162. {
  163. if (completeOnly && !mNodeLists[i]->mListComplete)
  164. {
  165. // Found the list, but it is not complete.
  166. return false;
  167. }
  168. // Return the node list (complete or not) and remove
  169. // it from our list of lists
  170. *list = mNodeLists[i];
  171. mNodeLists.erase(i);
  172. return true;
  173. }
  174. }
  175. return false;
  176. }
  177. void NodeListManager::clearNotification( U32 listId )
  178. {
  179. for (U32 i=0; i<mNotifyList.size(); ++i)
  180. {
  181. if (mNotifyList[i]->getListId() == listId)
  182. {
  183. delete mNotifyList[i];
  184. mNotifyList.erase(i);
  185. return;
  186. }
  187. }
  188. }
  189. void NodeListManager::clearAllNotifications()
  190. {
  191. for (U32 i=0; i<mNotifyList.size(); ++i)
  192. {
  193. delete mNotifyList[i];
  194. }
  195. mNotifyList.clear();
  196. }
  197. void NodeListManager::registerNotification( NodeListNotify* notify )
  198. {
  199. mNotifyList.push_back( notify );
  200. }
  201. bool NodeListManager::dispatchNotification( U32 listId )
  202. {
  203. // Find the matching list
  204. NodeList* list = NULL;
  205. for (U32 i=0; i<mNodeLists.size(); ++i)
  206. {
  207. if (mNodeLists[i]->mId == listId)
  208. {
  209. list = mNodeLists[i];
  210. break;
  211. }
  212. }
  213. if (list)
  214. return dispatchNotification( list );
  215. return false;
  216. }
  217. bool NodeListManager::dispatchNotification( NodeList* list )
  218. {
  219. for (U32 i=0; i<mNotifyList.size(); ++i)
  220. {
  221. if (mNotifyList[i]->getListId() == list->mId)
  222. {
  223. mNotifyList[i]->sendNotification( list );
  224. delete mNotifyList[i];
  225. mNotifyList.erase(i);
  226. return true;
  227. }
  228. }
  229. return false;
  230. }