[1711] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Oliver Scheuss, (C) 2008 |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[1701] | 29 | #include "Gamestate.h" |
---|
[2371] | 30 | #include "../GamestateHandler.h" |
---|
| 31 | #include "../synchronisable/Synchronisable.h" |
---|
| 32 | #include "../TrafficControl.h" |
---|
[2485] | 33 | #include "core/Core.h" |
---|
[1763] | 34 | #include "core/CoreIncludes.h" |
---|
[1751] | 35 | #include "core/Iterator.h" |
---|
[1701] | 36 | |
---|
| 37 | #include <zlib.h> |
---|
[2575] | 38 | #include <cassert> |
---|
[1701] | 39 | |
---|
| 40 | |
---|
| 41 | |
---|
[2171] | 42 | namespace orxonox { |
---|
[1701] | 43 | |
---|
| 44 | namespace packet { |
---|
| 45 | |
---|
[2655] | 46 | #define GAMESTATE_START(data) (data + GamestateHeader::getSize()) |
---|
[1740] | 47 | |
---|
[1907] | 48 | #define PACKET_FLAG_GAMESTATE ENET_PACKET_FLAG_RELIABLE |
---|
[2087] | 49 | |
---|
[2655] | 50 | // Gamestate::Gamestate() |
---|
| 51 | // { |
---|
| 52 | // flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
| 53 | // } |
---|
| 54 | |
---|
[1701] | 55 | Gamestate::Gamestate() |
---|
| 56 | { |
---|
[1907] | 57 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
[2655] | 58 | header_ = 0; |
---|
[1701] | 59 | } |
---|
| 60 | |
---|
[1907] | 61 | Gamestate::Gamestate(uint8_t *data, unsigned int clientID): |
---|
[1711] | 62 | Packet(data, clientID) |
---|
[1701] | 63 | { |
---|
[1907] | 64 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
[2655] | 65 | header_ = new GamestateHeader(data_); |
---|
[1701] | 66 | } |
---|
| 67 | |
---|
[1907] | 68 | Gamestate::Gamestate(uint8_t *data) |
---|
| 69 | { |
---|
| 70 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
| 71 | data_=data; |
---|
[2655] | 72 | header_ = new GamestateHeader(data_); |
---|
[1907] | 73 | } |
---|
[1701] | 74 | |
---|
[1907] | 75 | |
---|
[1701] | 76 | Gamestate::~Gamestate() |
---|
| 77 | { |
---|
| 78 | } |
---|
| 79 | |
---|
[2171] | 80 | bool Gamestate::collectData(int id, uint8_t mode) |
---|
[1701] | 81 | { |
---|
[2655] | 82 | assert(this->header_==0); // make sure the header didn't exist before |
---|
[2371] | 83 | uint32_t tempsize=0, currentsize=0; |
---|
[1751] | 84 | assert(data_==0); |
---|
[2371] | 85 | uint32_t size = calcGamestateSize(id, mode); |
---|
[1740] | 86 | |
---|
[1701] | 87 | COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl; |
---|
| 88 | if(size==0) |
---|
| 89 | return false; |
---|
[2655] | 90 | data_ = new uint8_t[size + GamestateHeader::getSize()]; |
---|
[1701] | 91 | if(!data_){ |
---|
| 92 | COUT(2) << "GameStateManager: could not allocate memory" << std::endl; |
---|
| 93 | return false; |
---|
| 94 | } |
---|
[2655] | 95 | |
---|
| 96 | // create the header object |
---|
| 97 | header_ = new GamestateHeader(data_); |
---|
[2087] | 98 | |
---|
[1701] | 99 | //start collect data synchronisable by synchronisable |
---|
[1907] | 100 | uint8_t *mem=data_; |
---|
[2655] | 101 | mem += GamestateHeader::getSize(); |
---|
[2171] | 102 | ObjectList<Synchronisable>::iterator it; |
---|
| 103 | for(it = ObjectList<Synchronisable>::begin(); it; ++it){ |
---|
[2575] | 104 | |
---|
| 105 | #ifndef NDEBUG |
---|
[1907] | 106 | tempsize=it->getSize(id, mode); |
---|
[1701] | 107 | if(currentsize+tempsize > size){ |
---|
[2171] | 108 | assert(0); // if we don't use multithreading this part shouldn't be neccessary |
---|
[1701] | 109 | // start allocate additional memory |
---|
| 110 | COUT(3) << "G.St.Man: need additional memory" << std::endl; |
---|
[2171] | 111 | ObjectList<Synchronisable>::iterator temp = it; |
---|
[2490] | 112 | uint32_t addsize=tempsize; |
---|
[1701] | 113 | while(++temp) |
---|
[1907] | 114 | addsize+=temp->getSize(id, mode); |
---|
[2655] | 115 | data_ = (uint8_t *)realloc(data_, GamestateHeader::getSize() + currentsize + addsize); |
---|
[1701] | 116 | if(!data_) |
---|
| 117 | return false; |
---|
| 118 | size = currentsize+addsize; |
---|
| 119 | }// stop allocate additional memory |
---|
[2575] | 120 | #endif |
---|
[1701] | 121 | |
---|
[1907] | 122 | //if(it->doSelection(id)) |
---|
[2575] | 123 | if ( it->doSync( id, mode ) ) |
---|
[2371] | 124 | dataMap_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) ); |
---|
| 125 | // dataMap_[mem-data_]=(*it); // save the mem location of the synchronisable data |
---|
[1751] | 126 | if(!it->getData(mem, id, mode)) |
---|
[1701] | 127 | return false; // mem pointer gets automatically increased because of call by reference |
---|
| 128 | // increase size counter by size of current synchronisable |
---|
| 129 | currentsize+=tempsize; |
---|
| 130 | } |
---|
[1740] | 131 | |
---|
| 132 | |
---|
[1701] | 133 | //start write gamestate header |
---|
[2655] | 134 | header_->setDataSize( currentsize ); |
---|
| 135 | header_->setID( id ); |
---|
| 136 | header_->setDiffed( false ); |
---|
| 137 | header_->setComplete( true ); |
---|
| 138 | header_->setCompressed( false ); |
---|
[1701] | 139 | //stop write gamestate header |
---|
[1740] | 140 | |
---|
[1701] | 141 | COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl; |
---|
| 142 | COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl; |
---|
| 143 | return true; |
---|
| 144 | } |
---|
| 145 | |
---|
[2171] | 146 | bool Gamestate::spreadData(uint8_t mode) |
---|
[1701] | 147 | { |
---|
[1751] | 148 | assert(data_); |
---|
[2655] | 149 | assert(!header_->isCompressed()); |
---|
| 150 | assert(!header_->isDiffed()); |
---|
| 151 | uint8_t *mem=data_+GamestateHeader::getSize(); |
---|
[1701] | 152 | // get the start of the Synchronisable list |
---|
[2171] | 153 | //ObjectList<Synchronisable>::iterator it=ObjectList<Synchronisable>::begin(); |
---|
[1907] | 154 | Synchronisable *s; |
---|
[1740] | 155 | |
---|
[1907] | 156 | // update the data of the objects we received |
---|
[2655] | 157 | while(mem < data_+GamestateHeader::getSize()+header_->getDataSize()){ |
---|
| 158 | SynchronisableHeader objectheader(mem); |
---|
[1701] | 159 | |
---|
[2655] | 160 | s = Synchronisable::getSynchronisable( objectheader.getObjectID() ); |
---|
[1907] | 161 | if(!s) |
---|
[1701] | 162 | { |
---|
[2485] | 163 | if (!Core::isMaster()) |
---|
[2655] | 164 | { |
---|
[2485] | 165 | Synchronisable::fabricate(mem, mode); |
---|
[2655] | 166 | } |
---|
[2485] | 167 | else |
---|
[2655] | 168 | { |
---|
| 169 | mem += objectheader.getDataSize(); |
---|
| 170 | } |
---|
[2371] | 171 | // COUT(0) << "could not fabricate synchronisable: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl; |
---|
| 172 | // else |
---|
| 173 | // COUT(0) << "fabricated: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl; |
---|
[1701] | 174 | } |
---|
[1907] | 175 | else |
---|
| 176 | { |
---|
| 177 | bool b = s->updateData(mem, mode); |
---|
| 178 | assert(b); |
---|
| 179 | } |
---|
[1701] | 180 | } |
---|
| 181 | |
---|
[2464] | 182 | // In debug mode, check first, whether there are no duplicate objectIDs |
---|
| 183 | #ifndef NDEBUG |
---|
| 184 | ObjectList<Synchronisable>::iterator it; |
---|
| 185 | for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it) { |
---|
| 186 | if (it->getObjectID() == OBJECTID_UNKNOWN) { |
---|
| 187 | if (it->objectMode_ != 0x0) { |
---|
| 188 | COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl; |
---|
[2485] | 189 | COUT(0) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << std::endl; |
---|
[2501] | 190 | COUT(0) << "Objects class: " << it->getIdentifier()->getName() << std::endl; |
---|
[2464] | 191 | assert(false); |
---|
| 192 | } |
---|
| 193 | } |
---|
| 194 | else { |
---|
| 195 | ObjectList<Synchronisable>::iterator it2; |
---|
| 196 | for (it2 = ObjectList<Synchronisable>::begin(); it2 != ObjectList<Synchronisable>::end(); ++it2) { |
---|
| 197 | if (it->getObjectID() == it2->getObjectID() && *it != *it2) { |
---|
| 198 | COUT(0) << "Found duplicate objectIDs on the client!" << std::endl |
---|
| 199 | << "Are you sure you don't create a Sychnronisable objcect with 'new' \ |
---|
| 200 | that doesn't have objectMode = 0x0?" << std::endl; |
---|
| 201 | assert(false); |
---|
| 202 | } |
---|
| 203 | } |
---|
| 204 | } |
---|
| 205 | } |
---|
| 206 | #endif |
---|
| 207 | |
---|
[1701] | 208 | return true; |
---|
| 209 | } |
---|
| 210 | |
---|
[2371] | 211 | uint32_t Gamestate::getSize() const |
---|
[1701] | 212 | { |
---|
[1711] | 213 | assert(data_); |
---|
[2655] | 214 | if(header_->isCompressed()) |
---|
| 215 | return header_->getCompSize()+GamestateHeader::getSize(); |
---|
[1701] | 216 | else |
---|
| 217 | { |
---|
[2655] | 218 | return header_->getDataSize()+GamestateHeader::getSize(); |
---|
[1701] | 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
[1751] | 222 | bool Gamestate::operator==(packet::Gamestate gs){ |
---|
[2655] | 223 | uint8_t *d1 = data_+GamestateHeader::getSize(); |
---|
| 224 | uint8_t *d2 = gs.data_+GamestateHeader::getSize(); |
---|
[1751] | 225 | assert(!isCompressed()); |
---|
| 226 | assert(!gs.isCompressed()); |
---|
[2655] | 227 | while(d1<data_+header_->getDataSize()) |
---|
[1751] | 228 | { |
---|
| 229 | if(*d1!=*d2) |
---|
| 230 | return false; |
---|
| 231 | d1++; |
---|
| 232 | d2++; |
---|
| 233 | } |
---|
| 234 | return true; |
---|
| 235 | } |
---|
| 236 | |
---|
[1701] | 237 | bool Gamestate::process() |
---|
| 238 | { |
---|
[1705] | 239 | return GamestateHandler::addGamestate(this, getClientID()); |
---|
[1701] | 240 | } |
---|
| 241 | |
---|
[1907] | 242 | |
---|
| 243 | |
---|
[1701] | 244 | bool Gamestate::compressData() |
---|
| 245 | { |
---|
[2655] | 246 | assert(data_); |
---|
| 247 | assert(!header_->isCompressed()); |
---|
| 248 | uLongf buffer = (uLongf)(((header_->getDataSize() + 12)*1.01)+1); |
---|
[1701] | 249 | if(buffer==0) |
---|
| 250 | return false; |
---|
[1740] | 251 | |
---|
[2655] | 252 | uint8_t *ndata = new uint8_t[buffer+GamestateHeader::getSize()]; |
---|
| 253 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
[1751] | 254 | //unsigned char *dest = new unsigned char[buffer]; |
---|
[2655] | 255 | uint8_t *source = data_ + GamestateHeader::getSize(); |
---|
[1701] | 256 | int retval; |
---|
[2655] | 257 | retval = compress( dest, &buffer, source, (uLong)(header_->getDataSize()) ); |
---|
[1701] | 258 | switch ( retval ) { |
---|
| 259 | case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; |
---|
[1751] | 260 | case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false; |
---|
| 261 | case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false; |
---|
| 262 | case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false; |
---|
[1701] | 263 | } |
---|
| 264 | |
---|
| 265 | //copy and modify header |
---|
[2655] | 266 | GamestateHeader *temp = header_; |
---|
| 267 | header_ = new GamestateHeader(ndata, temp); |
---|
| 268 | delete temp; |
---|
[1701] | 269 | //delete old data |
---|
| 270 | delete[] data_; |
---|
| 271 | //save new data |
---|
| 272 | data_ = ndata; |
---|
[2655] | 273 | header_->setCompSize( buffer ); |
---|
| 274 | header_->setCompressed( true ); |
---|
| 275 | COUT(5) << "gamestate compress datasize: " << header_->getDataSize() << " compsize: " << header_->getCompSize() << std::endl; |
---|
[1701] | 276 | return true; |
---|
| 277 | } |
---|
| 278 | bool Gamestate::decompressData() |
---|
| 279 | { |
---|
[2655] | 280 | assert(data_); |
---|
| 281 | assert(header_->isCompressed()); |
---|
| 282 | COUT(4) << "GameStateClient: uncompressing gamestate. id: " << header_->getID() << ", baseid: " << header_->getBaseID() << ", datasize: " << header_->getDataSize() << ", compsize: " << header_->getCompSize() << std::endl; |
---|
| 283 | uint32_t datasize = header_->getDataSize(); |
---|
| 284 | uint32_t compsize = header_->getCompSize(); |
---|
[2371] | 285 | uint32_t bufsize; |
---|
[2087] | 286 | // assert(compsize<=datasize); |
---|
[1907] | 287 | bufsize = datasize; |
---|
[1751] | 288 | assert(bufsize!=0); |
---|
[2655] | 289 | uint8_t *ndata = new uint8_t[bufsize + GamestateHeader::getSize()]; |
---|
| 290 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
| 291 | uint8_t *source = data_ + GamestateHeader::getSize(); |
---|
[1701] | 292 | int retval; |
---|
[1751] | 293 | uLongf length=bufsize; |
---|
| 294 | retval = uncompress( dest, &length, source, (uLong)compsize ); |
---|
[1701] | 295 | switch ( retval ) { |
---|
| 296 | case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break; |
---|
| 297 | case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false; |
---|
| 298 | case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false; |
---|
| 299 | case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false; |
---|
| 300 | } |
---|
[1752] | 301 | |
---|
[1701] | 302 | //copy over the header |
---|
[2655] | 303 | GamestateHeader *temp = header_; |
---|
| 304 | header_ = new GamestateHeader( data_, header_ ); |
---|
| 305 | delete temp; |
---|
[2087] | 306 | |
---|
| 307 | if (this->bDataENetAllocated_){ |
---|
| 308 | // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will |
---|
| 309 | // deallocated it anyway. So data and packet stay together. |
---|
| 310 | this->bDataENetAllocated_ = false; |
---|
| 311 | } |
---|
| 312 | else{ |
---|
| 313 | // We allocated the memory in the first place (unlikely). So we destroy the old data |
---|
| 314 | // and overwrite it with the new decompressed data. |
---|
| 315 | delete[] this->data_; |
---|
| 316 | } |
---|
| 317 | |
---|
[1751] | 318 | //set new pointers |
---|
[1701] | 319 | data_ = ndata; |
---|
[2655] | 320 | header_->setCompressed( false ); |
---|
| 321 | assert(header_->getDataSize()==datasize); |
---|
| 322 | assert(header_->getCompSize()==compsize); |
---|
[1701] | 323 | return true; |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | Gamestate *Gamestate::diff(Gamestate *base) |
---|
| 327 | { |
---|
[2655] | 328 | assert(data_); |
---|
| 329 | assert(!header_->isCompressed()); |
---|
| 330 | assert(!header_->isDiffed()); |
---|
| 331 | GamestateHeader diffHeader(base->data_); |
---|
[1701] | 332 | //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; |
---|
[1907] | 333 | uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); |
---|
[2371] | 334 | uint32_t of=0; // pointers offset |
---|
| 335 | uint32_t dest_length=0; |
---|
[2655] | 336 | dest_length=header_->getDataSize(); |
---|
[1701] | 337 | if(dest_length==0) |
---|
| 338 | return NULL; |
---|
[2655] | 339 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
| 340 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
| 341 | while(of < diffHeader.getDataSize() && of < header_->getDataSize()){ |
---|
[1701] | 342 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
| 343 | ++of; |
---|
| 344 | } |
---|
[2655] | 345 | if(diffHeader.getDataSize()!=header_->getDataSize()){ |
---|
[1907] | 346 | uint8_t n=0; |
---|
[2655] | 347 | if(diffHeader.getDataSize() < header_->getDataSize()){ |
---|
[1701] | 348 | while(of<dest_length){ |
---|
| 349 | *(dest+of)=n^*(gs+of); |
---|
| 350 | of++; |
---|
| 351 | } |
---|
| 352 | } |
---|
| 353 | } |
---|
| 354 | |
---|
[1751] | 355 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
[2655] | 356 | *(g->header_) = *header_; |
---|
| 357 | g->header_->setDiffed( true ); |
---|
| 358 | g->header_->setBaseID( base->getID() ); |
---|
[1751] | 359 | g->flags_=flags_; |
---|
| 360 | g->packetDirection_ = packetDirection_; |
---|
[1701] | 361 | return g; |
---|
| 362 | } |
---|
| 363 | |
---|
[2371] | 364 | Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){ |
---|
[1907] | 365 | assert(data_); |
---|
[2371] | 366 | std::list<obj>::iterator it; |
---|
[2087] | 367 | |
---|
[1907] | 368 | // allocate memory for new data |
---|
[2655] | 369 | uint8_t *gdata = new uint8_t[header_->getDataSize()+GamestateHeader::getSize()]; |
---|
[1907] | 370 | // create a gamestate out of it |
---|
| 371 | Gamestate *gs = new Gamestate(gdata); |
---|
[2655] | 372 | uint8_t *newdata = gdata + GamestateHeader::getSize(); |
---|
[2417] | 373 | uint8_t *origdata = GAMESTATE_START(data_); |
---|
[2087] | 374 | |
---|
[1907] | 375 | //copy the GamestateHeader |
---|
[2655] | 376 | assert(gs->header_); |
---|
| 377 | *(gs->header_) = *header_; |
---|
[2087] | 378 | |
---|
[2371] | 379 | uint32_t objectOffset; |
---|
| 380 | unsigned int objectsize, destsize=0; |
---|
[2435] | 381 | // TODO: Why is this variable not used? |
---|
| 382 | //Synchronisable *object; |
---|
[2087] | 383 | |
---|
[2371] | 384 | //call TrafficControl |
---|
[2655] | 385 | TrafficControl::getInstance()->processObjectList( clientID, header_->getID(), &dataMap_ ); |
---|
[2485] | 386 | |
---|
[1907] | 387 | //copy in the zeros |
---|
[2419] | 388 | for(it=dataMap_.begin(); it!=dataMap_.end();){ |
---|
[2459] | 389 | // if((*it).objSize==0) |
---|
| 390 | // continue; |
---|
[2485] | 391 | // if(it->second->getSize(HEADER->id)==0) // merged from objecthierarchy2, doesn't work anymore; TODO: change this |
---|
| 392 | // continue; // merged from objecthierarchy2, doesn't work anymore; TODO: change this |
---|
[2655] | 393 | SynchronisableHeader oldobjectheader(origdata); |
---|
| 394 | SynchronisableHeader newobjectheader(newdata); |
---|
[2476] | 395 | if ( (*it).objSize == 0 ) |
---|
[2490] | 396 | { |
---|
| 397 | ++it; |
---|
[2476] | 398 | continue; |
---|
[2490] | 399 | } |
---|
[2417] | 400 | // object = Synchronisable::getSynchronisable( (*it).objID ); |
---|
| 401 | // assert(object->objectID == oldobjectheader->objectID); |
---|
[2655] | 402 | objectsize = oldobjectheader.getDataSize(); |
---|
| 403 | objectOffset=SynchronisableHeader::getSize(); //skip the size and the availableData variables in the objectheader |
---|
| 404 | if ( (*it).objID == oldobjectheader.getObjectID() ){ |
---|
[2417] | 405 | memcpy(newdata, origdata, objectsize); |
---|
[2655] | 406 | assert(newobjectheader.isDataAvailable()==true); |
---|
[2419] | 407 | ++it; |
---|
[1907] | 408 | }else{ |
---|
[2655] | 409 | newobjectheader = oldobjectheader; |
---|
| 410 | newobjectheader.setDataAvailable(false); |
---|
[2171] | 411 | memset(newdata+objectOffset, 0, objectsize-objectOffset); |
---|
[1907] | 412 | } |
---|
| 413 | newdata += objectsize; |
---|
[2417] | 414 | origdata += objectsize; |
---|
[2371] | 415 | destsize += objectsize; |
---|
[1907] | 416 | } |
---|
[2490] | 417 | #ifndef NDEBUG |
---|
| 418 | uint32_t origsize = destsize; |
---|
[2655] | 419 | while ( origsize < header_->getDataSize() ) |
---|
[2490] | 420 | { |
---|
[2655] | 421 | SynchronisableHeader oldobjectheader(origdata); |
---|
| 422 | objectsize = oldobjectheader.getDataSize(); |
---|
[2490] | 423 | origdata += objectsize; |
---|
| 424 | origsize += objectsize; |
---|
| 425 | } |
---|
[2655] | 426 | assert(origsize==header_->getDataSize()); |
---|
[2490] | 427 | assert(destsize!=0); |
---|
| 428 | #endif |
---|
[2655] | 429 | gs->header_->setDataSize( destsize ); |
---|
[1907] | 430 | return gs; |
---|
| 431 | } |
---|
| 432 | |
---|
| 433 | |
---|
[1701] | 434 | Gamestate *Gamestate::undiff(Gamestate *base) |
---|
| 435 | { |
---|
[2655] | 436 | assert(this && base);assert(data_); |
---|
| 437 | assert(header_->isDiffed()); |
---|
| 438 | assert(!header_->isCompressed() && !base->header_->isCompressed()); |
---|
[1701] | 439 | //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; |
---|
[1907] | 440 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
| 441 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
[2371] | 442 | uint32_t of=0; // pointers offset |
---|
| 443 | uint32_t dest_length=0; |
---|
[2655] | 444 | dest_length=header_->getDataSize(); |
---|
[1701] | 445 | if(dest_length==0) |
---|
| 446 | return NULL; |
---|
[2655] | 447 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+GamestateHeader::getSize()]; |
---|
| 448 | uint8_t *dest = ndata + GamestateHeader::getSize(); |
---|
| 449 | while(of < base->header_->getDataSize() && of < header_->getDataSize()){ |
---|
[1701] | 450 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
| 451 | ++of; |
---|
| 452 | } |
---|
[2655] | 453 | if(base->header_->getDataSize()!=header_->getDataSize()){ |
---|
[1907] | 454 | uint8_t n=0; |
---|
[2655] | 455 | if(base->header_->getDataSize() < header_->getDataSize()){ |
---|
[1701] | 456 | while(of < dest_length){ |
---|
| 457 | *(dest+of)=n^*(gs+of); |
---|
| 458 | of++; |
---|
| 459 | } |
---|
| 460 | } |
---|
| 461 | } |
---|
[1751] | 462 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
[2655] | 463 | assert(g->header_); |
---|
| 464 | *(g->header_) = *header_; |
---|
| 465 | g->header_->setDiffed( false ); |
---|
[1751] | 466 | g->flags_=flags_; |
---|
| 467 | g->packetDirection_ = packetDirection_; |
---|
| 468 | assert(!g->isDiffed()); |
---|
| 469 | assert(!g->isCompressed()); |
---|
[1701] | 470 | return g; |
---|
| 471 | } |
---|
| 472 | |
---|
| 473 | |
---|
[2371] | 474 | uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode) |
---|
[1701] | 475 | { |
---|
[2371] | 476 | uint32_t size=0; |
---|
[1701] | 477 | // get the start of the Synchronisable list |
---|
[2171] | 478 | ObjectList<Synchronisable>::iterator it; |
---|
[1701] | 479 | // get total size of gamestate |
---|
[2171] | 480 | for(it = ObjectList<Synchronisable>::begin(); it; ++it) |
---|
[1907] | 481 | size+=it->getSize(id, mode); // size of the actual data of the synchronisable |
---|
[1701] | 482 | return size; |
---|
| 483 | } |
---|
| 484 | |
---|
[2575] | 485 | } //namespace packet |
---|
| 486 | } //namespace orxonox |
---|