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/CoreIncludes.h" |
---|
34 | #include "core/Iterator.h" |
---|
35 | |
---|
36 | #include <zlib.h> |
---|
37 | #include <assert.h> |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | namespace orxonox { |
---|
42 | |
---|
43 | namespace packet { |
---|
44 | |
---|
45 | #define GAMESTATE_START(data) (data + sizeof(GamestateHeader)) |
---|
46 | #define GAMESTATE_HEADER(data) ((GamestateHeader *)data) |
---|
47 | #define HEADER GAMESTATE_HEADER(data_) |
---|
48 | |
---|
49 | |
---|
50 | #define PACKET_FLAG_GAMESTATE ENET_PACKET_FLAG_RELIABLE |
---|
51 | |
---|
52 | Gamestate::Gamestate() |
---|
53 | { |
---|
54 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
55 | } |
---|
56 | |
---|
57 | Gamestate::Gamestate(uint8_t *data, unsigned int clientID): |
---|
58 | Packet(data, clientID) |
---|
59 | { |
---|
60 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
61 | } |
---|
62 | |
---|
63 | Gamestate::Gamestate(uint8_t *data) |
---|
64 | { |
---|
65 | flags_ = flags_ | PACKET_FLAG_GAMESTATE; |
---|
66 | data_=data; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | Gamestate::~Gamestate() |
---|
71 | { |
---|
72 | } |
---|
73 | |
---|
74 | bool Gamestate::collectData(int id, uint8_t mode) |
---|
75 | { |
---|
76 | uint32_t tempsize=0, currentsize=0; |
---|
77 | assert(data_==0); |
---|
78 | uint32_t size = calcGamestateSize(id, mode); |
---|
79 | |
---|
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 | } |
---|
88 | |
---|
89 | //start collect data synchronisable by synchronisable |
---|
90 | uint8_t *mem=data_; |
---|
91 | mem+=sizeof(GamestateHeader); |
---|
92 | ObjectList<Synchronisable>::iterator it; |
---|
93 | for(it = ObjectList<Synchronisable>::begin(); it; ++it){ |
---|
94 | tempsize=it->getSize(id, mode); |
---|
95 | |
---|
96 | if(currentsize+tempsize > size){ |
---|
97 | assert(0); // if we don't use multithreading this part shouldn't be neccessary |
---|
98 | // start allocate additional memory |
---|
99 | COUT(3) << "G.St.Man: need additional memory" << std::endl; |
---|
100 | ObjectList<Synchronisable>::iterator temp = it; |
---|
101 | int addsize=tempsize; |
---|
102 | while(++temp) |
---|
103 | addsize+=temp->getSize(id, mode); |
---|
104 | data_ = (uint8_t *)realloc(data_, sizeof(GamestateHeader) + currentsize + addsize); |
---|
105 | if(!data_) |
---|
106 | return false; |
---|
107 | size = currentsize+addsize; |
---|
108 | }// stop allocate additional memory |
---|
109 | |
---|
110 | |
---|
111 | //if(it->doSelection(id)) |
---|
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 |
---|
115 | if(!it->getData(mem, id, mode)) |
---|
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 | } |
---|
120 | |
---|
121 | |
---|
122 | //start write gamestate header |
---|
123 | HEADER->packetType = ENUM::Gamestate; |
---|
124 | HEADER->datasize = currentsize; |
---|
125 | HEADER->id = id; |
---|
126 | HEADER->diffed = false; |
---|
127 | HEADER->complete = true; |
---|
128 | HEADER->compressed = false; |
---|
129 | //stop write gamestate header |
---|
130 | |
---|
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 | |
---|
136 | bool Gamestate::spreadData(uint8_t mode) |
---|
137 | { |
---|
138 | assert(data_); |
---|
139 | assert(!HEADER->compressed); |
---|
140 | assert(!HEADER->diffed); |
---|
141 | uint8_t *mem=data_+sizeof(GamestateHeader); |
---|
142 | // get the start of the Synchronisable list |
---|
143 | //ObjectList<Synchronisable>::iterator it=ObjectList<Synchronisable>::begin(); |
---|
144 | Synchronisable *s; |
---|
145 | |
---|
146 | // update the data of the objects we received |
---|
147 | while(mem < data_+sizeof(GamestateHeader)+HEADER->datasize){ |
---|
148 | synchronisableHeader *objectheader = (synchronisableHeader*)mem; |
---|
149 | |
---|
150 | s = Synchronisable::getSynchronisable( objectheader->objectID ); |
---|
151 | if(!s) |
---|
152 | { |
---|
153 | s = Synchronisable::fabricate(mem, mode); |
---|
154 | if(s==0) |
---|
155 | assert(0); |
---|
156 | // COUT(0) << "could not fabricate synchronisable: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl; |
---|
157 | // else |
---|
158 | // COUT(0) << "fabricated: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl; |
---|
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 | Synchronisable *object; |
---|
360 | |
---|
361 | //call TrafficControl |
---|
362 | TrafficControl::getInstance()->processObjectList( clientID, HEADER->id, &dataMap_ ); |
---|
363 | |
---|
364 | //copy in the zeros |
---|
365 | for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ |
---|
366 | oldobjectheader = (synchronisableHeader*)(data_ + (*it).objDataOffset); |
---|
367 | newobjectheader = (synchronisableHeader*)newdata; |
---|
368 | object = Synchronisable::getSynchronisable( (*it).objID ); |
---|
369 | assert(object->objectID == oldobjectheader->objectID); |
---|
370 | objectsize = oldobjectheader->size; |
---|
371 | *newobjectheader = *oldobjectheader; |
---|
372 | objectOffset=sizeof(synchronisableHeader); //skip the size and the availableData variables in the objectheader |
---|
373 | if ( /*object->doSelection(HEADER->id)*/true ){ |
---|
374 | assert(newobjectheader->dataAvailable==true); |
---|
375 | memcpy(newdata+objectOffset, data_ + (*it).objDataOffset + objectOffset, objectsize-objectOffset); |
---|
376 | }else{ |
---|
377 | newobjectheader->dataAvailable=false; |
---|
378 | memset(newdata+objectOffset, 0, objectsize-objectOffset); |
---|
379 | assert(objectOffset==objectsize); |
---|
380 | } |
---|
381 | newdata += objectsize; |
---|
382 | destsize += objectsize; |
---|
383 | // origdata += objectsize; |
---|
384 | } |
---|
385 | ((GamestateHeader*)gdata)->datasize = destsize; |
---|
386 | assert(destsize!=0); |
---|
387 | return gs; |
---|
388 | } |
---|
389 | |
---|
390 | |
---|
391 | Gamestate* Gamestate::intelligentDiff(Gamestate *base, unsigned int clientID){ |
---|
392 | // asserts |
---|
393 | assert(data_); |
---|
394 | assert(base->data_); |
---|
395 | assert(!GAMESTATE_HEADER(base->data_)->diffed); |
---|
396 | assert(!GAMESTATE_HEADER(base->data_)->compressed); |
---|
397 | assert(!HEADER->compressed); |
---|
398 | assert(!HEADER->diffed); |
---|
399 | |
---|
400 | //preparations |
---|
401 | std::list<obj>::iterator it; |
---|
402 | uint8_t *origdata, *basedata, *destdata, *ndata; |
---|
403 | uint32_t objectOffset, streamOffset=0; //data offset |
---|
404 | uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; |
---|
405 | synchronisableHeader *origheader; |
---|
406 | synchronisableHeader *destheader; |
---|
407 | Synchronisable *object; |
---|
408 | |
---|
409 | origdata = GAMESTATE_START(this->data_); |
---|
410 | basedata = GAMESTATE_START(base->data_); |
---|
411 | ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)]; |
---|
412 | destdata = ndata + sizeof(GamestateHeader); |
---|
413 | |
---|
414 | // do the diff |
---|
415 | for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ |
---|
416 | assert(streamOffset<HEADER->datasize); |
---|
417 | origheader = (synchronisableHeader *)(origdata+streamOffset); |
---|
418 | destheader = (synchronisableHeader *)(destdata+streamOffset); |
---|
419 | object = Synchronisable::getSynchronisable(origheader->objectID); |
---|
420 | bool sendData = object->doSelection(HEADER->id); |
---|
421 | |
---|
422 | //copy and partially diff the object header |
---|
423 | assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool)); |
---|
424 | *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff) |
---|
425 | *(bool*)(destdata+sizeof(uint32_t)) = sendData; |
---|
426 | if(sendData){ |
---|
427 | *(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) |
---|
428 | *(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) |
---|
429 | }else{ |
---|
430 | *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
431 | *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
432 | } |
---|
433 | objectOffset=sizeof(synchronisableHeader); |
---|
434 | streamOffset+=sizeof(synchronisableHeader); |
---|
435 | |
---|
436 | //now handle the object data or fill with zeros |
---|
437 | while(objectOffset<origheader->size ){ |
---|
438 | |
---|
439 | if(sendData && streamOffset<minsize) |
---|
440 | *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor |
---|
441 | else if(sendData) |
---|
442 | *(destdata+objectOffset)=((uint8_t)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short) |
---|
443 | else |
---|
444 | *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered |
---|
445 | |
---|
446 | objectOffset++; |
---|
447 | streamOffset++; |
---|
448 | } |
---|
449 | destdata+=objectOffset; |
---|
450 | origdata+=objectOffset; |
---|
451 | basedata+=objectOffset; |
---|
452 | } |
---|
453 | |
---|
454 | //copy over the gamestate header and set the diffed flag |
---|
455 | *(GamestateHeader *)ndata = *HEADER; //copy over the header |
---|
456 | Gamestate *gs = new Gamestate(ndata); |
---|
457 | GAMESTATE_HEADER(ndata)->diffed=true; |
---|
458 | return gs; |
---|
459 | } |
---|
460 | |
---|
461 | Gamestate* Gamestate::intelligentUnDiff(Gamestate *base){ |
---|
462 | // asserts |
---|
463 | assert(data_); |
---|
464 | assert(base->data_); |
---|
465 | assert(!GAMESTATE_HEADER(base->data_)->diffed); |
---|
466 | assert(!GAMESTATE_HEADER(base->data_)->compressed); |
---|
467 | assert(!HEADER->compressed); |
---|
468 | assert(HEADER->diffed); |
---|
469 | |
---|
470 | //preparations |
---|
471 | std::list<obj>::iterator it; |
---|
472 | uint8_t *origdata, *basedata, *destdata, *ndata; |
---|
473 | uint32_t objectOffset, streamOffset=0; //data offset |
---|
474 | uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; |
---|
475 | synchronisableHeader *origheader; |
---|
476 | synchronisableHeader *destheader; |
---|
477 | Synchronisable *object; |
---|
478 | |
---|
479 | origdata = GAMESTATE_START(this->data_); |
---|
480 | basedata = GAMESTATE_START(base->data_); |
---|
481 | ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)]; |
---|
482 | destdata = ndata + sizeof(GamestateHeader); |
---|
483 | |
---|
484 | // do the undiff |
---|
485 | for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ |
---|
486 | assert(streamOffset<HEADER->datasize); |
---|
487 | origheader = (synchronisableHeader *)(origdata+streamOffset); |
---|
488 | destheader = (synchronisableHeader *)(destdata+streamOffset); |
---|
489 | object = Synchronisable::getSynchronisable( origheader->objectID ); |
---|
490 | bool sendData; |
---|
491 | |
---|
492 | //copy and partially diff the object header |
---|
493 | assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool)); |
---|
494 | *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff) |
---|
495 | *(bool*)(destdata+sizeof(uint32_t)) = *(bool*)(origdata+sizeof(uint32_t)); |
---|
496 | sendData = *(bool*)(origdata+sizeof(uint32_t)); |
---|
497 | if(sendData){ |
---|
498 | *(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) |
---|
499 | *(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) |
---|
500 | }else{ |
---|
501 | *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
502 | *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
503 | } |
---|
504 | objectOffset=sizeof(synchronisableHeader); |
---|
505 | streamOffset+=sizeof(synchronisableHeader); |
---|
506 | |
---|
507 | //now handle the object data or fill with zeros |
---|
508 | while(objectOffset<origheader->size ){ |
---|
509 | |
---|
510 | if(sendData && streamOffset<minsize) |
---|
511 | *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor |
---|
512 | else if(sendData) |
---|
513 | *(destdata+objectOffset)=((unsigned char)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short) |
---|
514 | else |
---|
515 | *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered |
---|
516 | |
---|
517 | objectOffset++; |
---|
518 | streamOffset++; |
---|
519 | } |
---|
520 | destdata+=objectOffset; |
---|
521 | origdata+=objectOffset; |
---|
522 | basedata+=objectOffset; |
---|
523 | } |
---|
524 | |
---|
525 | //copy over the gamestate header and set the diffed flag |
---|
526 | *(GamestateHeader *)ndata = *HEADER; //copy over the header |
---|
527 | Gamestate *gs = new Gamestate(ndata); |
---|
528 | GAMESTATE_HEADER(ndata)->diffed=false; |
---|
529 | return gs; |
---|
530 | } |
---|
531 | |
---|
532 | Gamestate *Gamestate::undiff(Gamestate *base) |
---|
533 | { |
---|
534 | assert(this && base);assert(HEADER); |
---|
535 | assert(HEADER->diffed); |
---|
536 | assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed); |
---|
537 | //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; |
---|
538 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
539 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
540 | uint32_t of=0; // pointers offset |
---|
541 | uint32_t dest_length=0; |
---|
542 | dest_length=HEADER->datasize; |
---|
543 | if(dest_length==0) |
---|
544 | return NULL; |
---|
545 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)]; |
---|
546 | uint8_t *dest = ndata + sizeof(GamestateHeader); |
---|
547 | while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){ |
---|
548 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
549 | ++of; |
---|
550 | } |
---|
551 | if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){ |
---|
552 | uint8_t n=0; |
---|
553 | if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){ |
---|
554 | while(of < dest_length){ |
---|
555 | *(dest+of)=n^*(gs+of); |
---|
556 | of++; |
---|
557 | } |
---|
558 | } |
---|
559 | } |
---|
560 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
561 | GAMESTATE_HEADER(ndata)->diffed = false; |
---|
562 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
563 | g->flags_=flags_; |
---|
564 | g->packetDirection_ = packetDirection_; |
---|
565 | assert(!g->isDiffed()); |
---|
566 | assert(!g->isCompressed()); |
---|
567 | return g; |
---|
568 | } |
---|
569 | |
---|
570 | |
---|
571 | uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode) |
---|
572 | { |
---|
573 | uint32_t size=0; |
---|
574 | // get the start of the Synchronisable list |
---|
575 | ObjectList<Synchronisable>::iterator it; |
---|
576 | // get total size of gamestate |
---|
577 | for(it = ObjectList<Synchronisable>::begin(); it; ++it) |
---|
578 | size+=it->getSize(id, mode); // size of the actual data of the synchronisable |
---|
579 | // size+=sizeof(GamestateHeader); |
---|
580 | return size; |
---|
581 | } |
---|
582 | |
---|
583 | /** |
---|
584 | * This function removes a Synchronisable out of the universe |
---|
585 | * @param it iterator of the list pointing to the object |
---|
586 | * @return iterator pointing to the next object in the list |
---|
587 | */ |
---|
588 | void Gamestate::removeObject(ObjectList<Synchronisable>::iterator &it) { |
---|
589 | ObjectList<Synchronisable>::iterator temp=it; |
---|
590 | ++it; |
---|
591 | delete *temp; |
---|
592 | } |
---|
593 | |
---|
594 | bool Gamestate::isDiffed(){ |
---|
595 | return HEADER->diffed; |
---|
596 | } |
---|
597 | |
---|
598 | bool Gamestate::isCompressed(){ |
---|
599 | return HEADER->compressed; |
---|
600 | } |
---|
601 | |
---|
602 | int Gamestate::getBaseID(){ |
---|
603 | return HEADER->base_id; |
---|
604 | } |
---|
605 | } |
---|
606 | |
---|
607 | } |
---|