[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" |
---|
[1763] | 33 | #include "core/CoreIncludes.h" |
---|
[1751] | 34 | #include "core/Iterator.h" |
---|
[1701] | 35 | |
---|
| 36 | #include <zlib.h> |
---|
| 37 | #include <assert.h> |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | |
---|
[2171] | 41 | namespace orxonox { |
---|
[1701] | 42 | |
---|
| 43 | namespace packet { |
---|
| 44 | |
---|
[1751] | 45 | #define GAMESTATE_START(data) (data + sizeof(GamestateHeader)) |
---|
[1701] | 46 | #define GAMESTATE_HEADER(data) ((GamestateHeader *)data) |
---|
| 47 | #define HEADER GAMESTATE_HEADER(data_) |
---|
[1740] | 48 | |
---|
[2087] | 49 | |
---|
[1907] | 50 | #define PACKET_FLAG_GAMESTATE ENET_PACKET_FLAG_RELIABLE |
---|
[2087] | 51 | |
---|
[1701] | 52 | Gamestate::Gamestate() |
---|
| 53 | { |
---|
[1907] | 54 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
[1701] | 55 | } |
---|
| 56 | |
---|
[1907] | 57 | Gamestate::Gamestate(uint8_t *data, unsigned int clientID): |
---|
[1711] | 58 | Packet(data, clientID) |
---|
[1701] | 59 | { |
---|
[1907] | 60 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
[1701] | 61 | } |
---|
| 62 | |
---|
[1907] | 63 | Gamestate::Gamestate(uint8_t *data) |
---|
| 64 | { |
---|
| 65 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
| 66 | data_=data; |
---|
| 67 | } |
---|
[1701] | 68 | |
---|
[1907] | 69 | |
---|
[1701] | 70 | Gamestate::~Gamestate() |
---|
| 71 | { |
---|
| 72 | } |
---|
| 73 | |
---|
[2171] | 74 | bool Gamestate::collectData(int id, uint8_t mode) |
---|
[1701] | 75 | { |
---|
[2371] | 76 | uint32_t tempsize=0, currentsize=0; |
---|
[1751] | 77 | assert(data_==0); |
---|
[2371] | 78 | uint32_t size = calcGamestateSize(id, mode); |
---|
[1740] | 79 | |
---|
[1701] | 80 | COUT(4) << "G.ST.Man: producing gamestate with id: " << id << std::endl; |
---|
| 81 | if(size==0) |
---|
| 82 | return false; |
---|
| 83 | data_ = new unsigned char[size + sizeof(GamestateHeader)]; |
---|
| 84 | if(!data_){ |
---|
| 85 | COUT(2) << "GameStateManager: could not allocate memory" << std::endl; |
---|
| 86 | return false; |
---|
| 87 | } |
---|
[2087] | 88 | |
---|
[1701] | 89 | //start collect data synchronisable by synchronisable |
---|
[1907] | 90 | uint8_t *mem=data_; |
---|
[1701] | 91 | mem+=sizeof(GamestateHeader); |
---|
[2171] | 92 | ObjectList<Synchronisable>::iterator it; |
---|
| 93 | for(it = ObjectList<Synchronisable>::begin(); it; ++it){ |
---|
[1907] | 94 | tempsize=it->getSize(id, mode); |
---|
[1740] | 95 | |
---|
[1701] | 96 | if(currentsize+tempsize > size){ |
---|
[2171] | 97 | assert(0); // if we don't use multithreading this part shouldn't be neccessary |
---|
[1701] | 98 | // start allocate additional memory |
---|
| 99 | COUT(3) << "G.St.Man: need additional memory" << std::endl; |
---|
[2171] | 100 | ObjectList<Synchronisable>::iterator temp = it; |
---|
[1701] | 101 | int addsize=tempsize; |
---|
| 102 | while(++temp) |
---|
[1907] | 103 | addsize+=temp->getSize(id, mode); |
---|
| 104 | data_ = (uint8_t *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize); |
---|
[1701] | 105 | if(!data_) |
---|
| 106 | return false; |
---|
| 107 | size = currentsize+addsize; |
---|
| 108 | }// stop allocate additional memory |
---|
| 109 | |
---|
[2087] | 110 | |
---|
[1907] | 111 | //if(it->doSelection(id)) |
---|
[2371] | 112 | if(tempsize!=0) |
---|
| 113 | dataMap_.push_back( obj(it->getObjectID(), it->getCreatorID(), tempsize, mem-data_) ); |
---|
| 114 | // dataMap_[mem-data_]=(*it); // save the mem location of the synchronisable data |
---|
[1751] | 115 | if(!it->getData(mem, id, mode)) |
---|
[1701] | 116 | return false; // mem pointer gets automatically increased because of call by reference |
---|
| 117 | // increase size counter by size of current synchronisable |
---|
| 118 | currentsize+=tempsize; |
---|
| 119 | } |
---|
[1740] | 120 | |
---|
| 121 | |
---|
[1701] | 122 | //start write gamestate header |
---|
[1710] | 123 | HEADER->packetType = ENUM::Gamestate; |
---|
[1907] | 124 | HEADER->datasize = currentsize; |
---|
[1701] | 125 | HEADER->id = id; |
---|
| 126 | HEADER->diffed = false; |
---|
| 127 | HEADER->complete = true; |
---|
[1715] | 128 | HEADER->compressed = false; |
---|
[1701] | 129 | //stop write gamestate header |
---|
[1740] | 130 | |
---|
[1701] | 131 | COUT(5) << "G.ST.Man: Gamestate size: " << currentsize << std::endl; |
---|
| 132 | COUT(5) << "G.ST.Man: 'estimated' (and corrected) Gamestate size: " << size << std::endl; |
---|
| 133 | return true; |
---|
| 134 | } |
---|
| 135 | |
---|
[2171] | 136 | bool Gamestate::spreadData(uint8_t mode) |
---|
[1701] | 137 | { |
---|
[1751] | 138 | assert(data_); |
---|
| 139 | assert(!HEADER->compressed); |
---|
| 140 | assert(!HEADER->diffed); |
---|
[1907] | 141 | uint8_t *mem=data_+sizeof(GamestateHeader); |
---|
[1701] | 142 | // get the start of the Synchronisable list |
---|
[2171] | 143 | //ObjectList<Synchronisable>::iterator it=ObjectList<Synchronisable>::begin(); |
---|
[1907] | 144 | Synchronisable *s; |
---|
[1740] | 145 | |
---|
[1907] | 146 | // update the data of the objects we received |
---|
| 147 | while(mem < data_+sizeof(GamestateHeader)+HEADER->datasize){ |
---|
| 148 | synchronisableHeader *objectheader = (synchronisableHeader*)mem; |
---|
[1701] | 149 | |
---|
[1907] | 150 | s = Synchronisable::getSynchronisable( objectheader->objectID ); |
---|
| 151 | if(!s) |
---|
[1701] | 152 | { |
---|
[2417] | 153 | Synchronisable::fabricate(mem, mode); |
---|
[2371] | 154 | // COUT(0) << "could not fabricate synchronisable: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl; |
---|
| 155 | // else |
---|
| 156 | // COUT(0) << "fabricated: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl; |
---|
[1701] | 157 | } |
---|
[1907] | 158 | else |
---|
| 159 | { |
---|
| 160 | bool b = s->updateData(mem, mode); |
---|
| 161 | assert(b); |
---|
| 162 | } |
---|
[1701] | 163 | } |
---|
| 164 | |
---|
| 165 | return true; |
---|
| 166 | } |
---|
| 167 | |
---|
[1907] | 168 | |
---|
| 169 | |
---|
[1705] | 170 | int Gamestate::getID(){ |
---|
| 171 | return HEADER->id; |
---|
| 172 | } |
---|
| 173 | |
---|
[2371] | 174 | uint32_t Gamestate::getSize() const |
---|
[1701] | 175 | { |
---|
[1711] | 176 | assert(data_); |
---|
[1715] | 177 | if(HEADER->compressed) |
---|
[1701] | 178 | return HEADER->compsize+sizeof(GamestateHeader); |
---|
| 179 | else |
---|
| 180 | { |
---|
[1907] | 181 | return HEADER->datasize+sizeof(GamestateHeader); |
---|
[1701] | 182 | } |
---|
| 183 | } |
---|
| 184 | |
---|
[1751] | 185 | bool Gamestate::operator==(packet::Gamestate gs){ |
---|
[1907] | 186 | uint8_t *d1 = data_+sizeof(GamestateHeader); |
---|
| 187 | uint8_t *d2 = gs.data_+sizeof(GamestateHeader); |
---|
[1751] | 188 | assert(!isCompressed()); |
---|
| 189 | assert(!gs.isCompressed()); |
---|
[1907] | 190 | while(d1<data_+HEADER->datasize) |
---|
[1751] | 191 | { |
---|
| 192 | if(*d1!=*d2) |
---|
| 193 | return false; |
---|
| 194 | d1++; |
---|
| 195 | d2++; |
---|
| 196 | } |
---|
| 197 | return true; |
---|
| 198 | } |
---|
| 199 | |
---|
[1701] | 200 | bool Gamestate::process() |
---|
| 201 | { |
---|
[1705] | 202 | return GamestateHandler::addGamestate(this, getClientID()); |
---|
[1701] | 203 | } |
---|
| 204 | |
---|
[1907] | 205 | |
---|
| 206 | |
---|
[1701] | 207 | bool Gamestate::compressData() |
---|
| 208 | { |
---|
| 209 | assert(HEADER); |
---|
[1751] | 210 | assert(!HEADER->compressed); |
---|
[1907] | 211 | uLongf buffer = (uLongf)(((HEADER->datasize + 12)*1.01)+1); |
---|
[1701] | 212 | if(buffer==0) |
---|
| 213 | return false; |
---|
[1740] | 214 | |
---|
[1907] | 215 | uint8_t *ndata = new uint8_t[buffer+sizeof(GamestateHeader)]; |
---|
| 216 | uint8_t *dest = GAMESTATE_START(ndata); |
---|
[1751] | 217 | //unsigned char *dest = new unsigned char[buffer]; |
---|
[1907] | 218 | uint8_t *source = GAMESTATE_START(data_); |
---|
[1701] | 219 | int retval; |
---|
[1907] | 220 | retval = compress( dest, &buffer, source, (uLong)(HEADER->datasize) ); |
---|
[1701] | 221 | switch ( retval ) { |
---|
| 222 | case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; |
---|
[1751] | 223 | case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false; |
---|
| 224 | case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false; |
---|
| 225 | case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false; |
---|
[1701] | 226 | } |
---|
[1751] | 227 | #ifndef NDEBUG |
---|
| 228 | //decompress and compare the start and the decompressed data |
---|
[1907] | 229 | uint8_t *rdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)]; |
---|
| 230 | uint8_t *d2 = GAMESTATE_START(rdata); |
---|
| 231 | uLongf length2 = HEADER->datasize; |
---|
[1751] | 232 | uncompress(d2, &length2, dest, buffer); |
---|
[1907] | 233 | for(unsigned int i=0; i<HEADER->datasize; i++){ |
---|
[1751] | 234 | assert(*(source+i)==*(d2+i)); |
---|
| 235 | } |
---|
| 236 | delete[] rdata; |
---|
| 237 | #endif |
---|
[1701] | 238 | |
---|
| 239 | //copy and modify header |
---|
[1751] | 240 | #ifndef NDEBUG |
---|
[1907] | 241 | HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->datasize); |
---|
[1751] | 242 | #endif |
---|
[1701] | 243 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
| 244 | //delete old data |
---|
| 245 | delete[] data_; |
---|
| 246 | //save new data |
---|
| 247 | data_ = ndata; |
---|
[1751] | 248 | HEADER->compsize = buffer; |
---|
| 249 | HEADER->compressed = true; |
---|
[1730] | 250 | assert(HEADER->compressed); |
---|
[2087] | 251 | COUT(4) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl; |
---|
[1701] | 252 | return true; |
---|
| 253 | } |
---|
| 254 | bool Gamestate::decompressData() |
---|
| 255 | { |
---|
[1751] | 256 | assert(HEADER); |
---|
[1715] | 257 | assert(HEADER->compressed); |
---|
[2087] | 258 | COUT(4) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl; |
---|
[2371] | 259 | uint32_t datasize = HEADER->datasize; |
---|
| 260 | uint32_t compsize = HEADER->compsize; |
---|
| 261 | uint32_t bufsize; |
---|
[2087] | 262 | // assert(compsize<=datasize); |
---|
[1907] | 263 | bufsize = datasize; |
---|
[1751] | 264 | assert(bufsize!=0); |
---|
[1907] | 265 | uint8_t *ndata = new uint8_t[bufsize + sizeof(GamestateHeader)]; |
---|
| 266 | uint8_t *dest = ndata + sizeof(GamestateHeader); |
---|
| 267 | uint8_t *source = data_ + sizeof(GamestateHeader); |
---|
[1701] | 268 | int retval; |
---|
[1751] | 269 | uLongf length=bufsize; |
---|
| 270 | retval = uncompress( dest, &length, source, (uLong)compsize ); |
---|
[1701] | 271 | switch ( retval ) { |
---|
| 272 | case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break; |
---|
| 273 | case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false; |
---|
| 274 | case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false; |
---|
| 275 | case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false; |
---|
| 276 | } |
---|
[1751] | 277 | #ifndef NDEBUG |
---|
[1907] | 278 | assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->datasize)); |
---|
[1751] | 279 | #endif |
---|
[1752] | 280 | |
---|
[1701] | 281 | //copy over the header |
---|
| 282 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
[2087] | 283 | |
---|
| 284 | if (this->bDataENetAllocated_){ |
---|
| 285 | // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will |
---|
| 286 | // deallocated it anyway. So data and packet stay together. |
---|
| 287 | this->bDataENetAllocated_ = false; |
---|
| 288 | } |
---|
| 289 | else{ |
---|
| 290 | // We allocated the memory in the first place (unlikely). So we destroy the old data |
---|
| 291 | // and overwrite it with the new decompressed data. |
---|
| 292 | delete[] this->data_; |
---|
| 293 | } |
---|
| 294 | |
---|
[1751] | 295 | //set new pointers |
---|
[1701] | 296 | data_ = ndata; |
---|
[1751] | 297 | HEADER->compressed = false; |
---|
[1907] | 298 | assert(HEADER->datasize==datasize); |
---|
[1751] | 299 | assert(HEADER->compsize==compsize); |
---|
[1701] | 300 | return true; |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | Gamestate *Gamestate::diff(Gamestate *base) |
---|
| 304 | { |
---|
[1751] | 305 | assert(HEADER); |
---|
| 306 | assert(!HEADER->compressed); |
---|
| 307 | assert(!HEADER->diffed); |
---|
[1701] | 308 | //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; |
---|
[1907] | 309 | uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); |
---|
[2371] | 310 | uint32_t of=0; // pointers offset |
---|
| 311 | uint32_t dest_length=0; |
---|
[1907] | 312 | dest_length=HEADER->datasize; |
---|
[1701] | 313 | if(dest_length==0) |
---|
| 314 | return NULL; |
---|
[1907] | 315 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)]; |
---|
| 316 | uint8_t *dest = ndata + sizeof(GamestateHeader); |
---|
| 317 | while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){ |
---|
[1701] | 318 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
| 319 | ++of; |
---|
| 320 | } |
---|
[1907] | 321 | if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){ |
---|
| 322 | uint8_t n=0; |
---|
| 323 | if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){ |
---|
[1701] | 324 | while(of<dest_length){ |
---|
| 325 | *(dest+of)=n^*(gs+of); |
---|
| 326 | of++; |
---|
| 327 | } |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | |
---|
[1715] | 331 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
| 332 | GAMESTATE_HEADER(ndata)->diffed = true; |
---|
[1751] | 333 | GAMESTATE_HEADER(ndata)->base_id = base->getID(); |
---|
| 334 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
| 335 | g->flags_=flags_; |
---|
| 336 | g->packetDirection_ = packetDirection_; |
---|
[1701] | 337 | return g; |
---|
| 338 | } |
---|
| 339 | |
---|
[2371] | 340 | Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){ |
---|
[1907] | 341 | assert(data_); |
---|
[2371] | 342 | std::list<obj>::iterator it; |
---|
[2087] | 343 | |
---|
[1907] | 344 | // allocate memory for new data |
---|
| 345 | uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)]; |
---|
| 346 | // create a gamestate out of it |
---|
| 347 | Gamestate *gs = new Gamestate(gdata); |
---|
| 348 | uint8_t *newdata = gdata + sizeof(GamestateHeader); |
---|
[2417] | 349 | uint8_t *origdata = GAMESTATE_START(data_); |
---|
[2087] | 350 | |
---|
[1907] | 351 | //copy the GamestateHeader |
---|
| 352 | *(GamestateHeader*)gdata = *HEADER; |
---|
[2087] | 353 | |
---|
[1907] | 354 | synchronisableHeader *oldobjectheader, *newobjectheader; |
---|
[2371] | 355 | uint32_t objectOffset; |
---|
| 356 | unsigned int objectsize, destsize=0; |
---|
[2435] | 357 | // TODO: Why is this variable not used? |
---|
| 358 | //Synchronisable *object; |
---|
[2087] | 359 | |
---|
[2371] | 360 | //call TrafficControl |
---|
[2382] | 361 | TrafficControl::getInstance()->processObjectList( clientID, HEADER->id, &dataMap_ ); |
---|
[2371] | 362 | |
---|
[1907] | 363 | //copy in the zeros |
---|
[2419] | 364 | for(it=dataMap_.begin(); it!=dataMap_.end();){ |
---|
[2457] | 365 | // if((*it).objSize==0) |
---|
| 366 | // continue; |
---|
[2417] | 367 | oldobjectheader = (synchronisableHeader*)origdata; |
---|
[1907] | 368 | newobjectheader = (synchronisableHeader*)newdata; |
---|
[2417] | 369 | // object = Synchronisable::getSynchronisable( (*it).objID ); |
---|
| 370 | // assert(object->objectID == oldobjectheader->objectID); |
---|
[2371] | 371 | objectsize = oldobjectheader->size; |
---|
[2171] | 372 | objectOffset=sizeof(synchronisableHeader); //skip the size and the availableData variables in the objectheader |
---|
[2417] | 373 | if ( (*it).objID == oldobjectheader->objectID ){ |
---|
| 374 | memcpy(newdata, origdata, objectsize); |
---|
[2171] | 375 | assert(newobjectheader->dataAvailable==true); |
---|
[2419] | 376 | ++it; |
---|
[1907] | 377 | }else{ |
---|
[2417] | 378 | *newobjectheader = *oldobjectheader; |
---|
[1907] | 379 | newobjectheader->dataAvailable=false; |
---|
[2171] | 380 | memset(newdata+objectOffset, 0, objectsize-objectOffset); |
---|
[1907] | 381 | } |
---|
| 382 | newdata += objectsize; |
---|
[2417] | 383 | origdata += objectsize; |
---|
[2371] | 384 | destsize += objectsize; |
---|
| 385 | // origdata += objectsize; |
---|
[1907] | 386 | } |
---|
[2371] | 387 | ((GamestateHeader*)gdata)->datasize = destsize; |
---|
[2419] | 388 | assert(destsize==HEADER->datasize); |
---|
[2371] | 389 | assert(destsize!=0); |
---|
[1907] | 390 | return gs; |
---|
| 391 | } |
---|
| 392 | |
---|
| 393 | |
---|
| 394 | Gamestate* Gamestate::intelligentDiff(Gamestate *base, unsigned int clientID){ |
---|
| 395 | // asserts |
---|
| 396 | assert(data_); |
---|
| 397 | assert(base->data_); |
---|
| 398 | assert(!GAMESTATE_HEADER(base->data_)->diffed); |
---|
| 399 | assert(!GAMESTATE_HEADER(base->data_)->compressed); |
---|
| 400 | assert(!HEADER->compressed); |
---|
| 401 | assert(!HEADER->diffed); |
---|
[2087] | 402 | |
---|
[1907] | 403 | //preparations |
---|
[2371] | 404 | std::list<obj>::iterator it; |
---|
[1907] | 405 | uint8_t *origdata, *basedata, *destdata, *ndata; |
---|
[2371] | 406 | uint32_t objectOffset, streamOffset=0; //data offset |
---|
| 407 | uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; |
---|
[1907] | 408 | synchronisableHeader *origheader; |
---|
| 409 | synchronisableHeader *destheader; |
---|
[2371] | 410 | Synchronisable *object; |
---|
[2087] | 411 | |
---|
[1907] | 412 | origdata = GAMESTATE_START(this->data_); |
---|
| 413 | basedata = GAMESTATE_START(base->data_); |
---|
| 414 | ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)]; |
---|
| 415 | destdata = ndata + sizeof(GamestateHeader); |
---|
[2087] | 416 | |
---|
[1907] | 417 | // do the diff |
---|
| 418 | for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ |
---|
| 419 | assert(streamOffset<HEADER->datasize); |
---|
| 420 | origheader = (synchronisableHeader *)(origdata+streamOffset); |
---|
| 421 | destheader = (synchronisableHeader *)(destdata+streamOffset); |
---|
[2371] | 422 | object = Synchronisable::getSynchronisable(origheader->objectID); |
---|
| 423 | bool sendData = object->doSelection(HEADER->id); |
---|
[2087] | 424 | |
---|
[1907] | 425 | //copy and partially diff the object header |
---|
[2371] | 426 | assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool)); |
---|
[1907] | 427 | *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff) |
---|
| 428 | *(bool*)(destdata+sizeof(uint32_t)) = sendData; |
---|
| 429 | if(sendData){ |
---|
| 430 | *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+sizeof(uint32_t)+sizeof(bool)); //objectid (diff it) |
---|
| 431 | *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+2*sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+2*sizeof(uint32_t)+sizeof(bool)); //classid (diff it) |
---|
| 432 | }else{ |
---|
| 433 | *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
[2087] | 434 | *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
[1907] | 435 | } |
---|
| 436 | objectOffset=sizeof(synchronisableHeader); |
---|
| 437 | streamOffset+=sizeof(synchronisableHeader); |
---|
[2087] | 438 | |
---|
[1907] | 439 | //now handle the object data or fill with zeros |
---|
| 440 | while(objectOffset<origheader->size ){ |
---|
[2087] | 441 | |
---|
[1907] | 442 | if(sendData && streamOffset<minsize) |
---|
| 443 | *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor |
---|
| 444 | else if(sendData) |
---|
| 445 | *(destdata+objectOffset)=((uint8_t)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short) |
---|
| 446 | else |
---|
| 447 | *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered |
---|
[2087] | 448 | |
---|
[1907] | 449 | objectOffset++; |
---|
| 450 | streamOffset++; |
---|
| 451 | } |
---|
| 452 | destdata+=objectOffset; |
---|
| 453 | origdata+=objectOffset; |
---|
| 454 | basedata+=objectOffset; |
---|
| 455 | } |
---|
[2087] | 456 | |
---|
[1907] | 457 | //copy over the gamestate header and set the diffed flag |
---|
| 458 | *(GamestateHeader *)ndata = *HEADER; //copy over the header |
---|
| 459 | Gamestate *gs = new Gamestate(ndata); |
---|
| 460 | GAMESTATE_HEADER(ndata)->diffed=true; |
---|
| 461 | return gs; |
---|
| 462 | } |
---|
| 463 | |
---|
| 464 | Gamestate* Gamestate::intelligentUnDiff(Gamestate *base){ |
---|
| 465 | // asserts |
---|
| 466 | assert(data_); |
---|
| 467 | assert(base->data_); |
---|
| 468 | assert(!GAMESTATE_HEADER(base->data_)->diffed); |
---|
| 469 | assert(!GAMESTATE_HEADER(base->data_)->compressed); |
---|
| 470 | assert(!HEADER->compressed); |
---|
| 471 | assert(HEADER->diffed); |
---|
[2087] | 472 | |
---|
[1907] | 473 | //preparations |
---|
[2371] | 474 | std::list<obj>::iterator it; |
---|
[1907] | 475 | uint8_t *origdata, *basedata, *destdata, *ndata; |
---|
[2371] | 476 | uint32_t objectOffset, streamOffset=0; //data offset |
---|
| 477 | uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; |
---|
[1907] | 478 | synchronisableHeader *origheader; |
---|
| 479 | synchronisableHeader *destheader; |
---|
[2371] | 480 | Synchronisable *object; |
---|
[2087] | 481 | |
---|
[1907] | 482 | origdata = GAMESTATE_START(this->data_); |
---|
| 483 | basedata = GAMESTATE_START(base->data_); |
---|
| 484 | ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)]; |
---|
| 485 | destdata = ndata + sizeof(GamestateHeader); |
---|
[2087] | 486 | |
---|
[1907] | 487 | // do the undiff |
---|
| 488 | for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ |
---|
| 489 | assert(streamOffset<HEADER->datasize); |
---|
| 490 | origheader = (synchronisableHeader *)(origdata+streamOffset); |
---|
| 491 | destheader = (synchronisableHeader *)(destdata+streamOffset); |
---|
[2371] | 492 | object = Synchronisable::getSynchronisable( origheader->objectID ); |
---|
[1907] | 493 | bool sendData; |
---|
[2087] | 494 | |
---|
[1907] | 495 | //copy and partially diff the object header |
---|
[2371] | 496 | assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool)); |
---|
| 497 | *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff) |
---|
| 498 | *(bool*)(destdata+sizeof(uint32_t)) = *(bool*)(origdata+sizeof(uint32_t)); |
---|
| 499 | sendData = *(bool*)(origdata+sizeof(uint32_t)); |
---|
[1907] | 500 | if(sendData){ |
---|
[2371] | 501 | *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+sizeof(uint32_t)+sizeof(bool)); //objectid (diff it) |
---|
| 502 | *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = *(uint32_t*)(basedata+2*sizeof(uint32_t)+sizeof(bool)) ^ *(uint32_t*)(origdata+2*sizeof(uint32_t)+sizeof(bool)); //classid (diff it) |
---|
[1907] | 503 | }else{ |
---|
[2371] | 504 | *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
| 505 | *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
[1907] | 506 | } |
---|
| 507 | objectOffset=sizeof(synchronisableHeader); |
---|
| 508 | streamOffset+=sizeof(synchronisableHeader); |
---|
[2087] | 509 | |
---|
[1907] | 510 | //now handle the object data or fill with zeros |
---|
| 511 | while(objectOffset<origheader->size ){ |
---|
[2087] | 512 | |
---|
[1907] | 513 | if(sendData && streamOffset<minsize) |
---|
| 514 | *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor |
---|
| 515 | else if(sendData) |
---|
| 516 | *(destdata+objectOffset)=((unsigned char)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short) |
---|
| 517 | else |
---|
| 518 | *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered |
---|
[2087] | 519 | |
---|
[1907] | 520 | objectOffset++; |
---|
| 521 | streamOffset++; |
---|
| 522 | } |
---|
| 523 | destdata+=objectOffset; |
---|
| 524 | origdata+=objectOffset; |
---|
| 525 | basedata+=objectOffset; |
---|
| 526 | } |
---|
[2087] | 527 | |
---|
[1907] | 528 | //copy over the gamestate header and set the diffed flag |
---|
| 529 | *(GamestateHeader *)ndata = *HEADER; //copy over the header |
---|
| 530 | Gamestate *gs = new Gamestate(ndata); |
---|
| 531 | GAMESTATE_HEADER(ndata)->diffed=false; |
---|
| 532 | return gs; |
---|
| 533 | } |
---|
| 534 | |
---|
[1701] | 535 | Gamestate *Gamestate::undiff(Gamestate *base) |
---|
| 536 | { |
---|
[1751] | 537 | assert(this && base);assert(HEADER); |
---|
| 538 | assert(HEADER->diffed); |
---|
[1715] | 539 | assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed); |
---|
[1701] | 540 | //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; |
---|
[1907] | 541 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
| 542 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
[2371] | 543 | uint32_t of=0; // pointers offset |
---|
| 544 | uint32_t dest_length=0; |
---|
[1907] | 545 | dest_length=HEADER->datasize; |
---|
[1701] | 546 | if(dest_length==0) |
---|
| 547 | return NULL; |
---|
[1907] | 548 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)]; |
---|
| 549 | uint8_t *dest = ndata + sizeof(GamestateHeader); |
---|
| 550 | while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){ |
---|
[1701] | 551 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
| 552 | ++of; |
---|
| 553 | } |
---|
[1907] | 554 | if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){ |
---|
| 555 | uint8_t n=0; |
---|
| 556 | if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){ |
---|
[1701] | 557 | while(of < dest_length){ |
---|
| 558 | *(dest+of)=n^*(gs+of); |
---|
| 559 | of++; |
---|
| 560 | } |
---|
| 561 | } |
---|
| 562 | } |
---|
[1715] | 563 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
| 564 | GAMESTATE_HEADER(ndata)->diffed = false; |
---|
[1751] | 565 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
| 566 | g->flags_=flags_; |
---|
| 567 | g->packetDirection_ = packetDirection_; |
---|
| 568 | assert(!g->isDiffed()); |
---|
| 569 | assert(!g->isCompressed()); |
---|
[1701] | 570 | return g; |
---|
| 571 | } |
---|
| 572 | |
---|
| 573 | |
---|
[2371] | 574 | uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode) |
---|
[1701] | 575 | { |
---|
[2371] | 576 | uint32_t size=0; |
---|
[1701] | 577 | // get the start of the Synchronisable list |
---|
[2171] | 578 | ObjectList<Synchronisable>::iterator it; |
---|
[1701] | 579 | // get total size of gamestate |
---|
[2171] | 580 | for(it = ObjectList<Synchronisable>::begin(); it; ++it) |
---|
[1907] | 581 | size+=it->getSize(id, mode); // size of the actual data of the synchronisable |
---|
[1701] | 582 | // size+=sizeof(GamestateHeader); |
---|
| 583 | return size; |
---|
| 584 | } |
---|
| 585 | |
---|
| 586 | /** |
---|
| 587 | * This function removes a Synchronisable out of the universe |
---|
| 588 | * @param it iterator of the list pointing to the object |
---|
| 589 | * @return iterator pointing to the next object in the list |
---|
| 590 | */ |
---|
[2171] | 591 | void Gamestate::removeObject(ObjectList<Synchronisable>::iterator &it) { |
---|
| 592 | ObjectList<Synchronisable>::iterator temp=it; |
---|
[1701] | 593 | ++it; |
---|
| 594 | delete *temp; |
---|
| 595 | } |
---|
| 596 | |
---|
[1712] | 597 | bool Gamestate::isDiffed(){ |
---|
| 598 | return HEADER->diffed; |
---|
| 599 | } |
---|
[1740] | 600 | |
---|
[1751] | 601 | bool Gamestate::isCompressed(){ |
---|
| 602 | return HEADER->compressed; |
---|
| 603 | } |
---|
[1752] | 604 | |
---|
[1712] | 605 | int Gamestate::getBaseID(){ |
---|
| 606 | return HEADER->base_id; |
---|
| 607 | } |
---|
[1701] | 608 | } |
---|
| 609 | |
---|
| 610 | } |
---|