public override Object ReadGameData(Boolean forSpotter) { lock (this) { pCars2APIStruct _pcarsapistruct = new pCars2APIStruct(); if (!initialised) { if (!InitialiseInternal()) { throw new GameDataReadException("Failed to initialise shared memory"); } } try { int retries = -1; do { retries++; using (var sharedMemoryStreamView = memoryMappedFile.CreateViewStream()) { BinaryReader _SharedMemoryStream = new BinaryReader(sharedMemoryStreamView); sharedMemoryReadBuffer = _SharedMemoryStream.ReadBytes(sharedmemorysize); GCHandle handle = GCHandle.Alloc(sharedMemoryReadBuffer, GCHandleType.Pinned); try { _pcarsapistruct = (pCars2APIStruct)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(pCars2APIStruct)); } finally { handle.Free(); } } } while (_pcarsapistruct.mSequenceNumber % 2 != 0); tornFramesCount += retries; PCars2StructWrapper structWrapper = new PCars2StructWrapper(); structWrapper.ticksWhenRead = DateTime.UtcNow.Ticks; structWrapper.data = _pcarsapistruct; if (!forSpotter && dumpToFile && dataToDump != null && _pcarsapistruct.mTrackLocation != null && _pcarsapistruct.mTrackLocation.Length > 0) { dataToDump.Add(structWrapper); } return(structWrapper); } catch (Exception ex) { throw new GameDataReadException(ex.Message, ex); } } }
public override Object ReadGameDataFromFile(String filename, int pauseBeforeStart) { if (dataReadFromFile == null || filename != lastReadFileName) { dataReadFromFileIndex = 0; var filePathResolved = Utilities.ResolveDataFile(this.dataFilesPath, filename); dataReadFromFile = DeSerializeObject <PCars2StructWrapper[]>(filePathResolved); lastReadFileName = filename; Thread.Sleep(pauseBeforeStart); } if (dataReadFromFile != null && dataReadFromFile.Length > dataReadFromFileIndex) { PCars2StructWrapper structWrapperData = dataReadFromFile[dataReadFromFileIndex]; dataReadFromFileIndex++; return(structWrapperData); } else { return(null); } }