nodeListManager.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. if (mNodeList)
  69. mNodeList->mId = mId;
  70. // NOTE: Child class needs to populate the local node list
  71. }
  72. void NodeListEvent::process(NetConnection* conn)
  73. {
  74. if (mNodeList)
  75. {
  76. NodeListManager::NodeList* oldList = NULL;
  77. gClientNodeListManager->findListById(mNodeList->mId, &oldList, false);
  78. mergeLists(oldList);
  79. gClientNodeListManager->addNodeList(mNodeList);
  80. mNodeList = NULL;
  81. }
  82. }
  83. void NodeListEvent::mergeLists(NodeListManager::NodeList* oldList)
  84. {
  85. if (oldList)
  86. {
  87. if ( !mNodeList->mListComplete)
  88. {
  89. copyIntoList( oldList );
  90. // Is the node list now complete?
  91. oldList->mTotalValidNodes += mNodeList->mTotalValidNodes;
  92. if (oldList->mTotalValidNodes >= mTotalNodes)
  93. {
  94. oldList->mListComplete = true;
  95. }
  96. delete mNodeList;
  97. mNodeList = oldList;
  98. }
  99. }
  100. else
  101. {
  102. padListToSize();
  103. }
  104. }
  105. IMPLEMENT_CO_NETEVENT_V1(NodeListEvent);
  106. ConsoleDocClass( NodeListEvent,
  107. "@brief Base class for events used by node editors, like River\n\n"
  108. "Editor use only.\n\n"
  109. "@internal"
  110. );
  111. //-----------------------------------------------------------------------------
  112. // NodeListManager Class
  113. //-----------------------------------------------------------------------------
  114. NodeListManager* gClientNodeListManager = NULL;
  115. NodeListManager* gServerNodeListManager = NULL;
  116. U32 NodeListManager::smMaximumNodesPerEvent = 20;
  117. //-----------------------------------------------------------------------------
  118. NodeListManager::NodeListManager(const bool isServer)
  119. {
  120. mIsServer = isServer;
  121. mNextListId = 0;
  122. }
  123. NodeListManager::~NodeListManager()
  124. {
  125. clearAllNotifications();
  126. clearNodeLists();
  127. }
  128. void NodeListManager::clearNodeLists()
  129. {
  130. for ( U32 i=0; i<mNodeLists.size(); ++i )
  131. {
  132. delete mNodeLists[i];
  133. }
  134. mNodeLists.clear();
  135. }
  136. U32 NodeListManager::nextListId()
  137. {
  138. U32 id = mNextListId;
  139. ++mNextListId;
  140. return id;
  141. }
  142. void NodeListManager::addNodeList( NodeList* list )
  143. {
  144. // Before we store the node list, we should check if anyone has registered
  145. // an interest in it, but only if the list is complete.
  146. if (list->mListComplete)
  147. {
  148. if (dispatchNotification( list ))
  149. {
  150. delete list;
  151. return;
  152. }
  153. }
  154. // Nothing is registered or the list is not complete, so store the list for later
  155. mNodeLists.push_back( list );
  156. }
  157. bool NodeListManager::findListById(U32 id, NodeList** list, bool completeOnly)
  158. {
  159. *list = NULL;
  160. for (U32 i=0; i<mNodeLists.size(); ++i)
  161. {
  162. if (mNodeLists[i]->mId == id)
  163. {
  164. if (completeOnly && !mNodeLists[i]->mListComplete)
  165. {
  166. // Found the list, but it is not complete.
  167. return false;
  168. }
  169. // Return the node list (complete or not) and remove
  170. // it from our list of lists
  171. *list = mNodeLists[i];
  172. mNodeLists.erase(i);
  173. return true;
  174. }
  175. }
  176. return false;
  177. }
  178. void NodeListManager::clearNotification( U32 listId )
  179. {
  180. for (U32 i=0; i<mNotifyList.size(); ++i)
  181. {
  182. if (mNotifyList[i]->getListId() == listId)
  183. {
  184. delete mNotifyList[i];
  185. mNotifyList.erase(i);
  186. return;
  187. }
  188. }
  189. }
  190. void NodeListManager::clearAllNotifications()
  191. {
  192. for (U32 i=0; i<mNotifyList.size(); ++i)
  193. {
  194. delete mNotifyList[i];
  195. }
  196. mNotifyList.clear();
  197. }
  198. void NodeListManager::registerNotification( NodeListNotify* notify )
  199. {
  200. mNotifyList.push_back( notify );
  201. }
  202. bool NodeListManager::dispatchNotification( U32 listId )
  203. {
  204. // Find the matching list
  205. NodeList* list = NULL;
  206. for (U32 i=0; i<mNodeLists.size(); ++i)
  207. {
  208. if (mNodeLists[i]->mId == listId)
  209. {
  210. list = mNodeLists[i];
  211. break;
  212. }
  213. }
  214. if (list)
  215. return dispatchNotification( list );
  216. return false;
  217. }
  218. bool NodeListManager::dispatchNotification( NodeList* list )
  219. {
  220. for (U32 i=0; i<mNotifyList.size(); ++i)
  221. {
  222. if (mNotifyList[i]->getListId() == list->mId)
  223. {
  224. mNotifyList[i]->sendNotification( list );
  225. delete mNotifyList[i];
  226. mNotifyList.erase(i);
  227. return true;
  228. }
  229. }
  230. return false;
  231. }