+ if (msg->cursize + !*sectionstarted + 2 + 1 + 2 > msg->maxsize)
+ return;
+ val2 = PRVM_EDICTFIELDVALUE((&prog->edicts[number]), prog->fieldoffsets.Version);
+ version = (int)val2->_float;
+ // LordHavoc: do some validity checks on self.Version
+ // if self.Version reaches 255, it will soon exceed the byte used to
+ // store an entity version in the client struct, so we need to reset
+ // all the version to 1 and force all the existing clients' version of
+ // it to 255 (which we're not allowing to actually occur)
+ if (version < 0)
+ val2->_float = 0;
+ if (version >= 255)
+ {
+ int i;
+ val2->_float = version = 1;
+ // since we just reset the Version field to 1, it may accidentally
+ // end up being equal to an existing client version now or in the
+ // future, so to fix this situation we have to loop over all
+ // clients and change their versions for this entity to be -1
+ // which never matches, thus causing them to receive the update
+ // soon, as they should
+ for (i = 0;i < svs.maxclients;i++)
+ if (svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[number])
+ svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[number] = 255;
+ }
+ }
+ // if the version already matches, we don't need to do anything as the
+ // latest version has already been sent.
+ if (svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[number] == version)
+ return;
+ if (version)
+ {
+ // if there's no SendEntity function, treat it as a remove
+ val = PRVM_EDICTFIELDVALUE((&prog->edicts[number]), prog->fieldoffsets.SendEntity);
+ if (val->function)
+ {
+ // there is a function to call, save the cursize value incase we
+ // have to do a rollback due to overflow
+ int oldcursize = msg->cursize;
+ if(!*sectionstarted)
+ MSG_WriteByte(msg, svc_csqcentities);
+ MSG_WriteShort(msg, number);
+ ((int *)prog->globals.generic)[OFS_PARM0] = sv.writeentitiestoclient_cliententitynumber;
+ prog->globals.server->self = number;
+ PRVM_ExecuteProgram(val->function, "Null SendEntity\n");
+ if(prog->globals.generic[OFS_RETURN])
+ {
+ if (msg->cursize + 2 > msg->maxsize)
+ {
+ // if the packet no longer has enough room to write the
+ // final index code that ends the message, rollback to the
+ // state before we tried to write anything and then return
+ msg->cursize = oldcursize;
+ msg->overflowed = false;
+ return;
+ }
+ // an update has been successfully written, update the version
+ svs.clients[sv.writeentitiestoclient_clientnumber].csqcentityversion[number] = version;
+ // and take note that we have begun the svc_csqcentities
+ // section of the packet
+ *sectionstarted = 1;
+ return;
+ }
+ else
+ {
+ // rollback the buffer to its state before the writes
+ msg->cursize = oldcursize;
+ // if the function returned FALSE, simply write a remove
+ // this is done by falling through to the remove code below
+ version = 0;
+ }
+ }