gameConnectionEvents.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  23. // Arcane-FX for MIT Licensed Open Source version of Torque 3D from GarageGames
  24. // Copyright (C) 2015 Faust Logic, Inc.
  25. //~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~//~~~~~~~~~~~~~~~~~~~~~//
  26. #include "platform/platform.h"
  27. #include "core/dnet.h"
  28. #include "core/stream/bitStream.h"
  29. #include "console/consoleTypes.h"
  30. #include "console/simBase.h"
  31. #include "scene/pathManager.h"
  32. #include "scene/sceneManager.h"
  33. #include "sfx/sfxSystem.h"
  34. #include "sfx/sfxDescription.h"
  35. #include "app/game.h"
  36. #include "T3D/gameBase/gameConnection.h"
  37. #include "T3D/gameBase/gameConnectionEvents.h"
  38. #include "console/engineAPI.h"
  39. #define DebugChecksum 0xF00DBAAD
  40. //#define DEBUG_SPEW
  41. //--------------------------------------------------------------------------
  42. IMPLEMENT_CO_CLIENTEVENT_V1(SimDataBlockEvent);
  43. IMPLEMENT_CO_CLIENTEVENT_V1(Sim2DAudioEvent);
  44. IMPLEMENT_CO_CLIENTEVENT_V1(Sim3DAudioEvent);
  45. IMPLEMENT_CO_CLIENTEVENT_V1(SetMissionCRCEvent);
  46. ConsoleDocClass( SimDataBlockEvent,
  47. "@brief Use by GameConnection to process incoming datablocks.\n\n"
  48. "Not intended for game development, internal use only, but does expose onDataBlockObjectReceived.\n\n "
  49. "@internal");
  50. ConsoleDocClass( Sim2DAudioEvent,
  51. "@brief Use by GameConnection to send a 2D sound event over the network.\n\n"
  52. "Not intended for game development, internal use only, but does expose GameConnection::play2D.\n\n "
  53. "@internal");
  54. ConsoleDocClass( Sim3DAudioEvent,
  55. "@brief Use by GameConnection to send a 3D sound event over the network.\n\n"
  56. "Not intended for game development, internal use only, but does expose GameConnection::play3D.\n\n "
  57. "@internal");
  58. ConsoleDocClass( SetMissionCRCEvent,
  59. "@brief Use by GameConnection to send a 3D sound event over the network.\n\n"
  60. "Not intended for game development, internal use only, but does expose GameConnection::setMissionCRC.\n\n "
  61. "@internal");
  62. //----------------------------------------------------------------------------
  63. SimDataBlockEvent::SimDataBlockEvent(SimDataBlock* obj, U32 index, U32 total, U32 missionSequence)
  64. {
  65. mObj = NULL;
  66. mIndex = index;
  67. mTotal = total;
  68. mMissionSequence = missionSequence;
  69. mProcess = false;
  70. if(obj)
  71. {
  72. id = obj->getId();
  73. AssertFatal(id >= DataBlockObjectIdFirst && id <= DataBlockObjectIdLast,
  74. "Out of range event data block id... check simBase.h");
  75. #ifdef DEBUG_SPEW
  76. Con::printf("queuing data block: %d", mIndex);
  77. #endif
  78. }
  79. }
  80. SimDataBlockEvent::~SimDataBlockEvent()
  81. {
  82. if( mObj )
  83. delete mObj;
  84. }
  85. #ifdef TORQUE_DEBUG_NET
  86. const char *SimDataBlockEvent::getDebugName()
  87. {
  88. SimObject *obj = Sim::findObject(id);
  89. static char buffer[256];
  90. dSprintf(buffer, sizeof(buffer), "%s [%s - %s]",
  91. getClassName(),
  92. obj ? obj->getName() : "",
  93. obj ? obj->getClassName() : "NONE");
  94. return buffer;
  95. }
  96. #endif // TORQUE_DEBUG_NET
  97. void SimDataBlockEvent::notifyDelivered(NetConnection *conn, bool )
  98. {
  99. // if the modified key for this event is not the current one,
  100. // we've already resorted and resent some blocks, so fall out.
  101. if(conn->isRemoved())
  102. return;
  103. GameConnection *gc = (GameConnection *) conn;
  104. if(gc->getDataBlockSequence() != mMissionSequence)
  105. return;
  106. U32 nextIndex = mIndex + DataBlockQueueCount;
  107. SimDataBlockGroup *g = Sim::getDataBlockGroup();
  108. if(mIndex == g->size() - 1)
  109. {
  110. gc->setDataBlockModifiedKey(gc->getMaxDataBlockModifiedKey());
  111. gc->sendConnectionMessage(GameConnection::DataBlocksDone, mMissionSequence);
  112. }
  113. if(g->size() <= nextIndex)
  114. return;
  115. SimDataBlock *blk = (SimDataBlock *) (*g)[nextIndex];
  116. gc->postNetEvent(new SimDataBlockEvent(blk, nextIndex, g->size(), mMissionSequence));
  117. }
  118. void SimDataBlockEvent::pack(NetConnection *conn, BitStream *bstream)
  119. {
  120. #ifdef AFX_CAP_DATABLOCK_CACHE
  121. ((GameConnection *)conn)->tempDisableStringBuffering(bstream);
  122. #endif
  123. SimDataBlock* obj;
  124. Sim::findObject(id,obj);
  125. GameConnection *gc = (GameConnection *) conn;
  126. if(bstream->writeFlag(gc->getDataBlockModifiedKey() < obj->getModifiedKey()))
  127. {
  128. if(obj->getModifiedKey() > gc->getMaxDataBlockModifiedKey())
  129. gc->setMaxDataBlockModifiedKey(obj->getModifiedKey());
  130. AssertFatal(obj,
  131. "SimDataBlockEvent:: Data blocks cannot be deleted");
  132. bstream->writeInt(id - DataBlockObjectIdFirst,DataBlockObjectIdBitSize);
  133. S32 classId = obj->getClassId(conn->getNetClassGroup());
  134. bstream->writeClassId(classId, NetClassTypeDataBlock, conn->getNetClassGroup());
  135. bstream->writeInt(mIndex, DataBlockObjectIdBitSize);
  136. bstream->writeInt(mTotal, DataBlockObjectIdBitSize + 1);
  137. obj->packData(bstream);
  138. #ifdef TORQUE_DEBUG_NET
  139. bstream->writeInt(classId ^ DebugChecksum, 32);
  140. #endif
  141. }
  142. #ifdef AFX_CAP_DATABLOCK_CACHE
  143. ((GameConnection *)conn)->restoreStringBuffering(bstream);
  144. #endif
  145. }
  146. void SimDataBlockEvent::unpack(NetConnection *cptr, BitStream *bstream)
  147. {
  148. #ifdef AFX_CAP_DATABLOCK_CACHE
  149. // stash the stream position prior to unpacking
  150. S32 start_pos = bstream->getCurPos();
  151. ((GameConnection *)cptr)->tempDisableStringBuffering(bstream);
  152. #endif
  153. if(bstream->readFlag())
  154. {
  155. mProcess = true;
  156. id = bstream->readInt(DataBlockObjectIdBitSize) + DataBlockObjectIdFirst;
  157. S32 classId = bstream->readClassId(NetClassTypeDataBlock, cptr->getNetClassGroup());
  158. mIndex = bstream->readInt(DataBlockObjectIdBitSize);
  159. mTotal = bstream->readInt(DataBlockObjectIdBitSize + 1);
  160. SimObject* ptr;
  161. if( Sim::findObject( id, ptr ) )
  162. {
  163. // An object with the given ID already exists. Make sure it has the right class.
  164. AbstractClassRep* classRep = AbstractClassRep::findClassRep( cptr->getNetClassGroup(), NetClassTypeDataBlock, classId );
  165. if( classRep && String::compare( classRep->getClassName(), ptr->getClassName() ) != 0 )
  166. {
  167. Con::warnf( "A '%s' datablock with id: %d already existed. "
  168. "Clobbering it with new '%s' datablock from server.",
  169. ptr->getClassName(), id, classRep->getClassName() );
  170. ptr->deleteObject();
  171. ptr = NULL;
  172. }
  173. }
  174. if( !ptr )
  175. ptr = ( SimObject* ) ConsoleObject::create( cptr->getNetClassGroup(), NetClassTypeDataBlock, classId );
  176. mObj = dynamic_cast< SimDataBlock* >( ptr );
  177. if( mObj != NULL )
  178. {
  179. #ifdef DEBUG_SPEW
  180. Con::printf(" - SimDataBlockEvent: unpacking event of type: %s", mObj->getClassName());
  181. #endif
  182. mObj->unpackData( bstream );
  183. }
  184. else
  185. {
  186. #ifdef DEBUG_SPEW
  187. Con::printf(" - SimDataBlockEvent: INVALID PACKET! Could not create class with classID: %d", classId);
  188. #endif
  189. delete ptr;
  190. cptr->setLastError("Invalid packet in SimDataBlockEvent::unpack()");
  191. }
  192. #ifdef TORQUE_DEBUG_NET
  193. U32 checksum = bstream->readInt(32);
  194. AssertISV( (checksum ^ DebugChecksum) == (U32)classId,
  195. avar("unpack did not match pack for event of class %s.",
  196. mObj->getClassName()) );
  197. #endif
  198. }
  199. #ifdef AFX_CAP_DATABLOCK_CACHE
  200. // rewind to stream position and then process raw bytes for caching
  201. ((GameConnection *)cptr)->repackClientDatablock(bstream, start_pos);
  202. ((GameConnection *)cptr)->restoreStringBuffering(bstream);
  203. #endif
  204. }
  205. void SimDataBlockEvent::write(NetConnection *cptr, BitStream *bstream)
  206. {
  207. if(bstream->writeFlag(mProcess))
  208. {
  209. bstream->writeInt(id - DataBlockObjectIdFirst,DataBlockObjectIdBitSize);
  210. S32 classId = mObj->getClassId(cptr->getNetClassGroup());
  211. bstream->writeClassId(classId, NetClassTypeDataBlock, cptr->getNetClassGroup());
  212. bstream->writeInt(mIndex, DataBlockObjectIdBitSize);
  213. bstream->writeInt(mTotal, DataBlockObjectIdBitSize + 1);
  214. mObj->packData(bstream);
  215. }
  216. }
  217. void SimDataBlockEvent::process(NetConnection *cptr)
  218. {
  219. if(mProcess)
  220. {
  221. //call the console function to set the number of blocks to be sent
  222. Con::executef("onDataBlockObjectReceived", mIndex, mTotal);
  223. String &errorBuffer = NetConnection::getErrorBuffer();
  224. // Register the datablock object if this is a new DB
  225. // and not for a modified datablock event.
  226. if( !mObj->isProperlyAdded() )
  227. {
  228. // This is a fresh datablock object.
  229. // Perform preload on datablock and register
  230. // the object.
  231. GameConnection* conn = dynamic_cast< GameConnection* >( cptr );
  232. if( conn )
  233. conn->preloadDataBlock( mObj );
  234. if( mObj->registerObject(id) )
  235. {
  236. cptr->addObject( mObj );
  237. mObj = NULL;
  238. }
  239. }
  240. else
  241. {
  242. // This is an update to an existing datablock. Preload
  243. // to finish this.
  244. mObj->preload( false, errorBuffer );
  245. mObj = NULL;
  246. }
  247. }
  248. }
  249. //----------------------------------------------------------------------------
  250. Sim2DAudioEvent::Sim2DAudioEvent(SFXProfile *profile)
  251. {
  252. mProfile = profile;
  253. }
  254. void Sim2DAudioEvent::pack(NetConnection *, BitStream *bstream)
  255. {
  256. bstream->writeInt( mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize);
  257. }
  258. void Sim2DAudioEvent::write(NetConnection *, BitStream *bstream)
  259. {
  260. bstream->writeInt( mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize);
  261. }
  262. void Sim2DAudioEvent::unpack(NetConnection *, BitStream *bstream)
  263. {
  264. SimObjectId id = bstream->readInt(DataBlockObjectIdBitSize) + DataBlockObjectIdFirst;
  265. Sim::findObject(id, mProfile);
  266. }
  267. void Sim2DAudioEvent::process(NetConnection *)
  268. {
  269. if (mProfile)
  270. SFX->playOnce( mProfile );
  271. }
  272. //----------------------------------------------------------------------------
  273. static F32 SoundPosAccuracy = 0.5;
  274. static S32 SoundRotBits = 8;
  275. Sim3DAudioEvent::Sim3DAudioEvent(SFXProfile *profile,const MatrixF* mat)
  276. {
  277. mProfile = profile;
  278. if (mat)
  279. mTransform = *mat;
  280. }
  281. void Sim3DAudioEvent::pack(NetConnection *con, BitStream *bstream)
  282. {
  283. bstream->writeInt(mProfile->getId() - DataBlockObjectIdFirst, DataBlockObjectIdBitSize);
  284. // If the sound has cone parameters, the orientation is
  285. // transmitted as well.
  286. SFXDescription* ad = mProfile->getDescription();
  287. if ( bstream->writeFlag( ad->mConeInsideAngle || ad->mConeOutsideAngle ) )
  288. {
  289. QuatF q(mTransform);
  290. q.normalize();
  291. // LH - we can get a valid quat that's very slightly over 1 in and so
  292. // this fails (barely) check against zero. So use some error-
  293. AssertFatal((1.0 - ((q.x * q.x) + (q.y * q.y) + (q.z * q.z))) >= (0.0 - 0.001),
  294. "QuatF::normalize() is broken in Sim3DAudioEvent");
  295. bstream->writeSignedFloat(q.x,SoundRotBits);
  296. bstream->writeSignedFloat(q.y,SoundRotBits);
  297. bstream->writeSignedFloat(q.z,SoundRotBits);
  298. bstream->writeFlag(q.w < 0.0);
  299. }
  300. Point3F pos;
  301. mTransform.getColumn(3,&pos);
  302. bstream->writeCompressedPoint(pos,SoundPosAccuracy);
  303. }
  304. void Sim3DAudioEvent::write(NetConnection *con, BitStream *bstream)
  305. {
  306. // Just do the normal pack...
  307. pack(con,bstream);
  308. }
  309. void Sim3DAudioEvent::unpack(NetConnection *con, BitStream *bstream)
  310. {
  311. SimObjectId id = bstream->readInt(DataBlockObjectIdBitSize) + DataBlockObjectIdFirst;
  312. Sim::findObject(id, mProfile);
  313. if (bstream->readFlag()) {
  314. QuatF q;
  315. q.x = bstream->readSignedFloat(SoundRotBits);
  316. q.y = bstream->readSignedFloat(SoundRotBits);
  317. q.z = bstream->readSignedFloat(SoundRotBits);
  318. F32 value = ((q.x * q.x) + (q.y * q.y) + (q.z * q.z));
  319. // #ifdef __linux
  320. // Hmm, this should never happen, but it does...
  321. if ( value > 1.f )
  322. value = 1.f;
  323. // #endif
  324. q.w = mSqrt(1.f - value);
  325. if (bstream->readFlag())
  326. q.w = -q.w;
  327. q.setMatrix(&mTransform);
  328. }
  329. else
  330. mTransform.identity();
  331. Point3F pos;
  332. bstream->readCompressedPoint(&pos,SoundPosAccuracy);
  333. mTransform.setColumn(3, pos);
  334. }
  335. void Sim3DAudioEvent::process(NetConnection *)
  336. {
  337. if (mProfile)
  338. SFX->playOnce( mProfile, &mTransform );
  339. }