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