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