//log.cpp //log [network] physics packets in order to replay the games #include #include #include "networkphysics.h" #include "gamemem.h" #include "log.h" #include "gameinitexit.h" #include "gameframe.h" #include "network.h" typedef struct{ int offset; short size; short type; } tLogEntry; #define kLogSize (20*1024*1024) #define kGhostLogSize (256*1024) #define kIndexSize (256*1024) char* gPacketLog; char *gGhostLog; tLogEntry *gPacketLogIndex; tLogEntry *gGhostIndex; int gLogPos=0; int gIndexPos=0; int gGhostLogPos=0; int gGhostIndexPos=0; void LogInit() { gPacketLog=(char*)malloc(kLogSize); gGhostLog=(char*)malloc(kGhostLogSize); gPacketLogIndex=(tLogEntry*)malloc(kIndexSize*sizeof(tLogEntry)); gGhostIndex=(tLogEntry*)malloc(kIndexSize*sizeof(tLogEntry)); } void GetLogs(tFileRef *logs,int *logCount) { *logCount=0; for(int i=0;ikGhostLogSize!!!! MemoryMove(gGhostLog,gPacketLog,gLogPos>kGhostLogSize?kGhostLogSize:gLogPos); MemoryMove(gGhostIndex,gPacketLogIndex,gIndexPos*sizeof(tLogEntry)); gGhostLogPos=gLogPos; gGhostIndexPos=gIndexPos; } int LogCompare(const void *a,const void *b) { int aFrame=((tPhysicsMessage*)(gPacketLog+*(int*)a))->frame; int bFrame=((tPhysicsMessage*)(gPacketLog+*(int*)b))->frame; S32Swap(aFrame); S32Swap(bFrame); return aFrame-bFrame; } void LogSort() { qsort(gPacketLogIndex,gIndexPos,sizeof(tLogEntry),LogCompare); } void LogReset() { gLogPos=0; gIndexPos=0; } void GhostLogReset() { gGhostLogPos=0; gGhostIndexPos=0; } int LogGetPacket(int index,int *type,int *size,int log,void *buffer) { if(log==kLogReplayLog||gGhostLogPos==0){ if(indexnumLaps==-1) { int w=gGhostIndexPos; S32Swap(w); fwrite(&w,sizeof(int),1,f); w=gGhostLogPos; S32Swap(w); fwrite(&w,sizeof(int),1,f); w=gReplayOldFrameCount; S32Swap(w); fwrite(&w,sizeof(int),1,f); for(int i=0;iframe-=(gBestLapTime!=0?gBestLapStart+5:gCurrentLapStart+5); } } } fwrite(gGhostLog,sizeof(char),gGhostLogPos,f); for(int i=0;iframe+=(gBestLapTime!=0?gBestLapStart+5:gCurrentLapStart+5); } } } } else { int w=gIndexPos; S32Swap(w); fwrite(&w,sizeof(int),1,f); w=gLogPos; S32Swap(w); fwrite(&w,sizeof(int),1,f); w=gReplayOldFrameCount; S32Swap(w); fwrite(&w,sizeof(int),1,f); for(int i=0;inumPlayers;i++) if(gInfo->playerCars[i]==-1) return false; if(gInfo->map==-1) return false; if(gInfo->environment==-1) return false; gIndexPos=*(int*)ch; S32Swap(gIndexPos); gGhostIndexPos=gIndexPos; ch+=sizeof(int); gLogPos=*(int*)ch; S32Swap(gLogPos); ch+=sizeof(int); gReplayOldFrameCount=*(int*)ch; S32Swap(gReplayOldFrameCount); ch+=sizeof(int); MemoryMove(gPacketLogIndex,ch,sizeof(tLogEntry)*gIndexPos); for(int i=0;i