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 | uint32_t 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 | Synchronisable::fabricate(mem, mode); |
---|
156 | else |
---|
157 | mem += objectheader->size; |
---|
158 | // COUT(0) << "could not fabricate synchronisable: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl; |
---|
159 | // else |
---|
160 | // COUT(0) << "fabricated: " << objectheader->objectID << " classid: " << objectheader->classID << " creator: " << objectheader->creatorID << endl; |
---|
161 | } |
---|
162 | else |
---|
163 | { |
---|
164 | bool b = s->updateData(mem, mode); |
---|
165 | assert(b); |
---|
166 | } |
---|
167 | } |
---|
168 | |
---|
169 | // In debug mode, check first, whether there are no duplicate objectIDs |
---|
170 | #ifndef NDEBUG |
---|
171 | ObjectList<Synchronisable>::iterator it; |
---|
172 | for (it = ObjectList<Synchronisable>::begin(); it != ObjectList<Synchronisable>::end(); ++it) { |
---|
173 | if (it->getObjectID() == OBJECTID_UNKNOWN) { |
---|
174 | if (it->objectMode_ != 0x0) { |
---|
175 | COUT(0) << "Found object with OBJECTID_UNKNOWN on the client with objectMode != 0x0!" << std::endl; |
---|
176 | COUT(0) << "Possible reason for this error: Client created a synchronized object without the Server's approval." << std::endl; |
---|
177 | assert(false); |
---|
178 | } |
---|
179 | } |
---|
180 | else { |
---|
181 | ObjectList<Synchronisable>::iterator it2; |
---|
182 | for (it2 = ObjectList<Synchronisable>::begin(); it2 != ObjectList<Synchronisable>::end(); ++it2) { |
---|
183 | if (it->getObjectID() == it2->getObjectID() && *it != *it2) { |
---|
184 | COUT(0) << "Found duplicate objectIDs on the client!" << std::endl |
---|
185 | << "Are you sure you don't create a Sychnronisable objcect with 'new' \ |
---|
186 | that doesn't have objectMode = 0x0?" << std::endl; |
---|
187 | assert(false); |
---|
188 | } |
---|
189 | } |
---|
190 | } |
---|
191 | } |
---|
192 | #endif |
---|
193 | |
---|
194 | return true; |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | int Gamestate::getID(){ |
---|
200 | return HEADER->id; |
---|
201 | } |
---|
202 | |
---|
203 | uint32_t Gamestate::getSize() const |
---|
204 | { |
---|
205 | assert(data_); |
---|
206 | if(HEADER->compressed) |
---|
207 | return HEADER->compsize+sizeof(GamestateHeader); |
---|
208 | else |
---|
209 | { |
---|
210 | return HEADER->datasize+sizeof(GamestateHeader); |
---|
211 | } |
---|
212 | } |
---|
213 | |
---|
214 | bool Gamestate::operator==(packet::Gamestate gs){ |
---|
215 | uint8_t *d1 = data_+sizeof(GamestateHeader); |
---|
216 | uint8_t *d2 = gs.data_+sizeof(GamestateHeader); |
---|
217 | assert(!isCompressed()); |
---|
218 | assert(!gs.isCompressed()); |
---|
219 | while(d1<data_+HEADER->datasize) |
---|
220 | { |
---|
221 | if(*d1!=*d2) |
---|
222 | return false; |
---|
223 | d1++; |
---|
224 | d2++; |
---|
225 | } |
---|
226 | return true; |
---|
227 | } |
---|
228 | |
---|
229 | bool Gamestate::process() |
---|
230 | { |
---|
231 | return GamestateHandler::addGamestate(this, getClientID()); |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | bool Gamestate::compressData() |
---|
237 | { |
---|
238 | assert(HEADER); |
---|
239 | assert(!HEADER->compressed); |
---|
240 | uLongf buffer = (uLongf)(((HEADER->datasize + 12)*1.01)+1); |
---|
241 | if(buffer==0) |
---|
242 | return false; |
---|
243 | |
---|
244 | uint8_t *ndata = new uint8_t[buffer+sizeof(GamestateHeader)]; |
---|
245 | uint8_t *dest = GAMESTATE_START(ndata); |
---|
246 | //unsigned char *dest = new unsigned char[buffer]; |
---|
247 | uint8_t *source = GAMESTATE_START(data_); |
---|
248 | int retval; |
---|
249 | retval = compress( dest, &buffer, source, (uLong)(HEADER->datasize) ); |
---|
250 | switch ( retval ) { |
---|
251 | case Z_OK: COUT(5) << "G.St.Man: compress: successfully compressed" << std::endl; break; |
---|
252 | case Z_MEM_ERROR: COUT(1) << "G.St.Man: compress: not enough memory available in gamestate.compress" << std::endl; return false; |
---|
253 | case Z_BUF_ERROR: COUT(2) << "G.St.Man: compress: not enough memory available in the buffer in gamestate.compress" << std::endl; return false; |
---|
254 | case Z_DATA_ERROR: COUT(2) << "G.St.Man: compress: data corrupted in gamestate.compress" << std::endl; return false; |
---|
255 | } |
---|
256 | #ifndef NDEBUG |
---|
257 | //decompress and compare the start and the decompressed data |
---|
258 | uint8_t *rdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)]; |
---|
259 | uint8_t *d2 = GAMESTATE_START(rdata); |
---|
260 | uLongf length2 = HEADER->datasize; |
---|
261 | uncompress(d2, &length2, dest, buffer); |
---|
262 | for(unsigned int i=0; i<HEADER->datasize; i++){ |
---|
263 | assert(*(source+i)==*(d2+i)); |
---|
264 | } |
---|
265 | delete[] rdata; |
---|
266 | #endif |
---|
267 | |
---|
268 | //copy and modify header |
---|
269 | #ifndef NDEBUG |
---|
270 | HEADER->crc32 = calcCRC(data_+sizeof(GamestateHeader), HEADER->datasize); |
---|
271 | #endif |
---|
272 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
273 | //delete old data |
---|
274 | delete[] data_; |
---|
275 | //save new data |
---|
276 | data_ = ndata; |
---|
277 | HEADER->compsize = buffer; |
---|
278 | HEADER->compressed = true; |
---|
279 | assert(HEADER->compressed); |
---|
280 | COUT(4) << "gamestate compress datasize: " << HEADER->datasize << " compsize: " << HEADER->compsize << std::endl; |
---|
281 | return true; |
---|
282 | } |
---|
283 | bool Gamestate::decompressData() |
---|
284 | { |
---|
285 | assert(HEADER); |
---|
286 | assert(HEADER->compressed); |
---|
287 | COUT(4) << "GameStateClient: uncompressing gamestate. id: " << HEADER->id << ", baseid: " << HEADER->base_id << ", datasize: " << HEADER->datasize << ", compsize: " << HEADER->compsize << std::endl; |
---|
288 | uint32_t datasize = HEADER->datasize; |
---|
289 | uint32_t compsize = HEADER->compsize; |
---|
290 | uint32_t bufsize; |
---|
291 | // assert(compsize<=datasize); |
---|
292 | bufsize = datasize; |
---|
293 | assert(bufsize!=0); |
---|
294 | uint8_t *ndata = new uint8_t[bufsize + sizeof(GamestateHeader)]; |
---|
295 | uint8_t *dest = ndata + sizeof(GamestateHeader); |
---|
296 | uint8_t *source = data_ + sizeof(GamestateHeader); |
---|
297 | int retval; |
---|
298 | uLongf length=bufsize; |
---|
299 | retval = uncompress( dest, &length, source, (uLong)compsize ); |
---|
300 | switch ( retval ) { |
---|
301 | case Z_OK: COUT(5) << "successfully decompressed" << std::endl; break; |
---|
302 | case Z_MEM_ERROR: COUT(1) << "not enough memory available" << std::endl; return false; |
---|
303 | case Z_BUF_ERROR: COUT(2) << "not enough memory available in the buffer" << std::endl; return false; |
---|
304 | case Z_DATA_ERROR: COUT(2) << "data corrupted (zlib)" << std::endl; return false; |
---|
305 | } |
---|
306 | #ifndef NDEBUG |
---|
307 | assert(HEADER->crc32==calcCRC(ndata+sizeof(GamestateHeader), HEADER->datasize)); |
---|
308 | #endif |
---|
309 | |
---|
310 | //copy over the header |
---|
311 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
312 | |
---|
313 | if (this->bDataENetAllocated_){ |
---|
314 | // Memory was allocated by ENet. --> We let it be since enet_packet_destroy will |
---|
315 | // deallocated it anyway. So data and packet stay together. |
---|
316 | this->bDataENetAllocated_ = false; |
---|
317 | } |
---|
318 | else{ |
---|
319 | // We allocated the memory in the first place (unlikely). So we destroy the old data |
---|
320 | // and overwrite it with the new decompressed data. |
---|
321 | delete[] this->data_; |
---|
322 | } |
---|
323 | |
---|
324 | //set new pointers |
---|
325 | data_ = ndata; |
---|
326 | HEADER->compressed = false; |
---|
327 | assert(HEADER->datasize==datasize); |
---|
328 | assert(HEADER->compsize==compsize); |
---|
329 | return true; |
---|
330 | } |
---|
331 | |
---|
332 | Gamestate *Gamestate::diff(Gamestate *base) |
---|
333 | { |
---|
334 | assert(HEADER); |
---|
335 | assert(!HEADER->compressed); |
---|
336 | assert(!HEADER->diffed); |
---|
337 | //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; |
---|
338 | uint8_t *basep = GAMESTATE_START(base->data_), *gs = GAMESTATE_START(this->data_); |
---|
339 | uint32_t of=0; // pointers offset |
---|
340 | uint32_t dest_length=0; |
---|
341 | dest_length=HEADER->datasize; |
---|
342 | if(dest_length==0) |
---|
343 | return NULL; |
---|
344 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)]; |
---|
345 | uint8_t *dest = ndata + sizeof(GamestateHeader); |
---|
346 | while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){ |
---|
347 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
348 | ++of; |
---|
349 | } |
---|
350 | if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){ |
---|
351 | uint8_t n=0; |
---|
352 | if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){ |
---|
353 | while(of<dest_length){ |
---|
354 | *(dest+of)=n^*(gs+of); |
---|
355 | of++; |
---|
356 | } |
---|
357 | } |
---|
358 | } |
---|
359 | |
---|
360 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
361 | GAMESTATE_HEADER(ndata)->diffed = true; |
---|
362 | GAMESTATE_HEADER(ndata)->base_id = base->getID(); |
---|
363 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
364 | g->flags_=flags_; |
---|
365 | g->packetDirection_ = packetDirection_; |
---|
366 | return g; |
---|
367 | } |
---|
368 | |
---|
369 | Gamestate* Gamestate::doSelection(unsigned int clientID, unsigned int targetSize){ |
---|
370 | assert(data_); |
---|
371 | std::list<obj>::iterator it; |
---|
372 | |
---|
373 | // allocate memory for new data |
---|
374 | uint8_t *gdata = new uint8_t[HEADER->datasize+sizeof(GamestateHeader)]; |
---|
375 | // create a gamestate out of it |
---|
376 | Gamestate *gs = new Gamestate(gdata); |
---|
377 | uint8_t *newdata = gdata + sizeof(GamestateHeader); |
---|
378 | uint8_t *origdata = GAMESTATE_START(data_); |
---|
379 | |
---|
380 | //copy the GamestateHeader |
---|
381 | *(GamestateHeader*)gdata = *HEADER; |
---|
382 | |
---|
383 | synchronisableHeader *oldobjectheader, *newobjectheader; |
---|
384 | uint32_t objectOffset; |
---|
385 | unsigned int objectsize, destsize=0; |
---|
386 | // TODO: Why is this variable not used? |
---|
387 | //Synchronisable *object; |
---|
388 | |
---|
389 | //call TrafficControl |
---|
390 | TrafficControl::getInstance()->processObjectList( clientID, HEADER->id, &dataMap_ ); |
---|
391 | |
---|
392 | //copy in the zeros |
---|
393 | for(it=dataMap_.begin(); it!=dataMap_.end();){ |
---|
394 | // if((*it).objSize==0) |
---|
395 | // continue; |
---|
396 | // if(it->second->getSize(HEADER->id)==0) // merged from objecthierarchy2, doesn't work anymore; TODO: change this |
---|
397 | // continue; // merged from objecthierarchy2, doesn't work anymore; TODO: change this |
---|
398 | oldobjectheader = (synchronisableHeader*)origdata; |
---|
399 | newobjectheader = (synchronisableHeader*)newdata; |
---|
400 | if ( (*it).objSize == 0 ) |
---|
401 | { |
---|
402 | ++it; |
---|
403 | continue; |
---|
404 | } |
---|
405 | // object = Synchronisable::getSynchronisable( (*it).objID ); |
---|
406 | // assert(object->objectID == oldobjectheader->objectID); |
---|
407 | objectsize = oldobjectheader->size; |
---|
408 | objectOffset=sizeof(synchronisableHeader); //skip the size and the availableData variables in the objectheader |
---|
409 | if ( (*it).objID == oldobjectheader->objectID ){ |
---|
410 | memcpy(newdata, origdata, objectsize); |
---|
411 | assert(newobjectheader->dataAvailable==true); |
---|
412 | ++it; |
---|
413 | }else{ |
---|
414 | *newobjectheader = *oldobjectheader; |
---|
415 | newobjectheader->dataAvailable=false; |
---|
416 | memset(newdata+objectOffset, 0, objectsize-objectOffset); |
---|
417 | } |
---|
418 | newdata += objectsize; |
---|
419 | origdata += objectsize; |
---|
420 | destsize += objectsize; |
---|
421 | // origdata += objectsize; |
---|
422 | } |
---|
423 | #ifndef NDEBUG |
---|
424 | uint32_t origsize = destsize; |
---|
425 | while ( origsize < HEADER->datasize ) |
---|
426 | { |
---|
427 | oldobjectheader = (synchronisableHeader*)origdata; |
---|
428 | objectsize = oldobjectheader->size; |
---|
429 | origdata += objectsize; |
---|
430 | origsize += objectsize; |
---|
431 | } |
---|
432 | assert(origsize==HEADER->datasize); |
---|
433 | assert(destsize!=0); |
---|
434 | #endif |
---|
435 | ((GamestateHeader*)gdata)->datasize = destsize; |
---|
436 | return gs; |
---|
437 | } |
---|
438 | |
---|
439 | |
---|
440 | Gamestate* Gamestate::intelligentDiff(Gamestate *base, unsigned int clientID){ |
---|
441 | // asserts |
---|
442 | assert(data_); |
---|
443 | assert(base->data_); |
---|
444 | assert(!GAMESTATE_HEADER(base->data_)->diffed); |
---|
445 | assert(!GAMESTATE_HEADER(base->data_)->compressed); |
---|
446 | assert(!HEADER->compressed); |
---|
447 | assert(!HEADER->diffed); |
---|
448 | |
---|
449 | //preparations |
---|
450 | std::list<obj>::iterator it; |
---|
451 | uint8_t *origdata, *basedata, *destdata, *ndata; |
---|
452 | uint32_t objectOffset, streamOffset=0; //data offset |
---|
453 | uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; |
---|
454 | synchronisableHeader *origheader; |
---|
455 | synchronisableHeader *destheader; |
---|
456 | Synchronisable *object; |
---|
457 | |
---|
458 | origdata = GAMESTATE_START(this->data_); |
---|
459 | basedata = GAMESTATE_START(base->data_); |
---|
460 | ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)]; |
---|
461 | destdata = ndata + sizeof(GamestateHeader); |
---|
462 | |
---|
463 | // do the diff |
---|
464 | for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ |
---|
465 | assert(streamOffset<HEADER->datasize); |
---|
466 | origheader = (synchronisableHeader *)(origdata+streamOffset); |
---|
467 | destheader = (synchronisableHeader *)(destdata+streamOffset); |
---|
468 | object = Synchronisable::getSynchronisable(origheader->objectID); |
---|
469 | bool sendData = object->doSelection(HEADER->id); |
---|
470 | |
---|
471 | //copy and partially diff the object header |
---|
472 | assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool)); |
---|
473 | *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff) |
---|
474 | *(bool*)(destdata+sizeof(uint32_t)) = sendData; |
---|
475 | if(sendData){ |
---|
476 | *(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) |
---|
477 | *(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) |
---|
478 | }else{ |
---|
479 | *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
480 | *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
481 | } |
---|
482 | objectOffset=sizeof(synchronisableHeader); |
---|
483 | streamOffset+=sizeof(synchronisableHeader); |
---|
484 | |
---|
485 | //now handle the object data or fill with zeros |
---|
486 | while(objectOffset<origheader->size ){ |
---|
487 | |
---|
488 | if(sendData && streamOffset<minsize) |
---|
489 | *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor |
---|
490 | else if(sendData) |
---|
491 | *(destdata+objectOffset)=((uint8_t)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short) |
---|
492 | else |
---|
493 | *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered |
---|
494 | |
---|
495 | objectOffset++; |
---|
496 | streamOffset++; |
---|
497 | } |
---|
498 | destdata+=objectOffset; |
---|
499 | origdata+=objectOffset; |
---|
500 | basedata+=objectOffset; |
---|
501 | } |
---|
502 | |
---|
503 | //copy over the gamestate header and set the diffed flag |
---|
504 | *(GamestateHeader *)ndata = *HEADER; //copy over the header |
---|
505 | Gamestate *gs = new Gamestate(ndata); |
---|
506 | GAMESTATE_HEADER(ndata)->diffed=true; |
---|
507 | return gs; |
---|
508 | } |
---|
509 | |
---|
510 | Gamestate* Gamestate::intelligentUnDiff(Gamestate *base){ |
---|
511 | // asserts |
---|
512 | assert(data_); |
---|
513 | assert(base->data_); |
---|
514 | assert(!GAMESTATE_HEADER(base->data_)->diffed); |
---|
515 | assert(!GAMESTATE_HEADER(base->data_)->compressed); |
---|
516 | assert(!HEADER->compressed); |
---|
517 | assert(HEADER->diffed); |
---|
518 | |
---|
519 | //preparations |
---|
520 | std::list<obj>::iterator it; |
---|
521 | uint8_t *origdata, *basedata, *destdata, *ndata; |
---|
522 | uint32_t objectOffset, streamOffset=0; //data offset |
---|
523 | uint32_t minsize = (HEADER->datasize < GAMESTATE_HEADER(base->data_)->datasize) ? HEADER->datasize : GAMESTATE_HEADER(base->data_)->datasize; |
---|
524 | synchronisableHeader *origheader; |
---|
525 | synchronisableHeader *destheader; |
---|
526 | Synchronisable *object; |
---|
527 | |
---|
528 | origdata = GAMESTATE_START(this->data_); |
---|
529 | basedata = GAMESTATE_START(base->data_); |
---|
530 | ndata = new uint8_t[HEADER->datasize + sizeof(GamestateHeader)]; |
---|
531 | destdata = ndata + sizeof(GamestateHeader); |
---|
532 | |
---|
533 | // do the undiff |
---|
534 | for(it=dataMap_.begin(); it!=dataMap_.end(); it++){ |
---|
535 | assert(streamOffset<HEADER->datasize); |
---|
536 | origheader = (synchronisableHeader *)(origdata+streamOffset); |
---|
537 | destheader = (synchronisableHeader *)(destdata+streamOffset); |
---|
538 | object = Synchronisable::getSynchronisable( origheader->objectID ); |
---|
539 | bool sendData; |
---|
540 | |
---|
541 | //copy and partially diff the object header |
---|
542 | assert(sizeof(synchronisableHeader)==3*sizeof(uint32_t)+sizeof(bool)); |
---|
543 | *(uint32_t*)destdata = *(uint32_t*)origdata; //size (do not diff) |
---|
544 | *(bool*)(destdata+sizeof(uint32_t)) = *(bool*)(origdata+sizeof(uint32_t)); |
---|
545 | sendData = *(bool*)(origdata+sizeof(uint32_t)); |
---|
546 | if(sendData){ |
---|
547 | *(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) |
---|
548 | *(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) |
---|
549 | }else{ |
---|
550 | *(uint32_t*)(destdata+sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
551 | *(uint32_t*)(destdata+2*sizeof(uint32_t)+sizeof(bool)) = 0; |
---|
552 | } |
---|
553 | objectOffset=sizeof(synchronisableHeader); |
---|
554 | streamOffset+=sizeof(synchronisableHeader); |
---|
555 | |
---|
556 | //now handle the object data or fill with zeros |
---|
557 | while(objectOffset<origheader->size ){ |
---|
558 | |
---|
559 | if(sendData && streamOffset<minsize) |
---|
560 | *(destdata+objectOffset)=*(basedata+objectOffset)^*(origdata+objectOffset); // do the xor |
---|
561 | else if(sendData) |
---|
562 | *(destdata+objectOffset)=((unsigned char)0)^*(origdata+objectOffset); // xor with 0 (basestream is too short) |
---|
563 | else |
---|
564 | *(destdata+objectOffset)=0; // set to 0 because this object should not be transfered |
---|
565 | |
---|
566 | objectOffset++; |
---|
567 | streamOffset++; |
---|
568 | } |
---|
569 | destdata+=objectOffset; |
---|
570 | origdata+=objectOffset; |
---|
571 | basedata+=objectOffset; |
---|
572 | } |
---|
573 | |
---|
574 | //copy over the gamestate header and set the diffed flag |
---|
575 | *(GamestateHeader *)ndata = *HEADER; //copy over the header |
---|
576 | Gamestate *gs = new Gamestate(ndata); |
---|
577 | GAMESTATE_HEADER(ndata)->diffed=false; |
---|
578 | return gs; |
---|
579 | } |
---|
580 | |
---|
581 | Gamestate *Gamestate::undiff(Gamestate *base) |
---|
582 | { |
---|
583 | assert(this && base);assert(HEADER); |
---|
584 | assert(HEADER->diffed); |
---|
585 | assert(!HEADER->compressed && !GAMESTATE_HEADER(base->data_)->compressed); |
---|
586 | //unsigned char *basep = base->getGs()/*, *gs = getGs()*/; |
---|
587 | uint8_t *basep = GAMESTATE_START(base->data_); |
---|
588 | uint8_t *gs = GAMESTATE_START(this->data_); |
---|
589 | uint32_t of=0; // pointers offset |
---|
590 | uint32_t dest_length=0; |
---|
591 | dest_length=HEADER->datasize; |
---|
592 | if(dest_length==0) |
---|
593 | return NULL; |
---|
594 | uint8_t *ndata = new uint8_t[dest_length*sizeof(uint8_t)+sizeof(GamestateHeader)]; |
---|
595 | uint8_t *dest = ndata + sizeof(GamestateHeader); |
---|
596 | while(of < GAMESTATE_HEADER(base->data_)->datasize && of < HEADER->datasize){ |
---|
597 | *(dest+of)=*(basep+of)^*(gs+of); // do the xor |
---|
598 | ++of; |
---|
599 | } |
---|
600 | if(GAMESTATE_HEADER(base->data_)->datasize!=HEADER->datasize){ |
---|
601 | uint8_t n=0; |
---|
602 | if(GAMESTATE_HEADER(base->data_)->datasize < HEADER->datasize){ |
---|
603 | while(of < dest_length){ |
---|
604 | *(dest+of)=n^*(gs+of); |
---|
605 | of++; |
---|
606 | } |
---|
607 | } |
---|
608 | } |
---|
609 | *GAMESTATE_HEADER(ndata) = *HEADER; |
---|
610 | GAMESTATE_HEADER(ndata)->diffed = false; |
---|
611 | Gamestate *g = new Gamestate(ndata, getClientID()); |
---|
612 | g->flags_=flags_; |
---|
613 | g->packetDirection_ = packetDirection_; |
---|
614 | assert(!g->isDiffed()); |
---|
615 | assert(!g->isCompressed()); |
---|
616 | return g; |
---|
617 | } |
---|
618 | |
---|
619 | |
---|
620 | uint32_t Gamestate::calcGamestateSize(int32_t id, uint8_t mode) |
---|
621 | { |
---|
622 | uint32_t size=0; |
---|
623 | // get the start of the Synchronisable list |
---|
624 | ObjectList<Synchronisable>::iterator it; |
---|
625 | // get total size of gamestate |
---|
626 | for(it = ObjectList<Synchronisable>::begin(); it; ++it) |
---|
627 | size+=it->getSize(id, mode); // size of the actual data of the synchronisable |
---|
628 | // size+=sizeof(GamestateHeader); |
---|
629 | return size; |
---|
630 | } |
---|
631 | |
---|
632 | /** |
---|
633 | * This function removes a Synchronisable out of the universe |
---|
634 | * @param it iterator of the list pointing to the object |
---|
635 | * @return iterator pointing to the next object in the list |
---|
636 | */ |
---|
637 | void Gamestate::removeObject(ObjectList<Synchronisable>::iterator &it) { |
---|
638 | ObjectList<Synchronisable>::iterator temp=it; |
---|
639 | ++it; |
---|
640 | delete *temp; |
---|
641 | } |
---|
642 | |
---|
643 | bool Gamestate::isDiffed(){ |
---|
644 | return HEADER->diffed; |
---|
645 | } |
---|
646 | |
---|
647 | bool Gamestate::isCompressed(){ |
---|
648 | return HEADER->compressed; |
---|
649 | } |
---|
650 | |
---|
651 | int Gamestate::getBaseID(){ |
---|
652 | return HEADER->base_id; |
---|
653 | } |
---|
654 | } |
---|
655 | |
---|
656 | } |
---|