public void Start(Replay replay, Stream stream) { if (disposedValue) { throw new ObjectDisposedException(nameof(ReplayWriter)); } if (Replay != null) { throw new InvalidOperationException("Already started."); } _startTime = DateTime.Now - _lobbyTime; Replay = replay ?? throw new ArgumentNullException(nameof(replay)); _binaryWriter = new BinaryWriter(_stream = stream ?? throw new ArgumentNullException(nameof(stream))); // header count _binaryWriter.Write(4); _binaryWriter.Write(Replay.Name); _binaryWriter.Write(Replay.GameId.ToString()); _binaryWriter.Write(Replay.User); _binaryWriter.Write(_lobbyTime.Ticks); // Game start time, for fastforwarding while (_prestartQueue.Count > 0) { var item = _prestartQueue.Dequeue(); ReplayEvent.Write(item, _binaryWriter); } _binaryWriter.Flush(); }
public void WriteEvent(ReplayEvent replayEvent) { if (disposedValue) { throw new ObjectDisposedException(nameof(ReplayWriter)); } if (replayEvent.Type == ReplayEventType.Start) { if (_wroteStartEvent) { throw new InvalidOperationException($"Already wrote Start event"); } _wroteStartEvent = true; } if (Replay == null) { replayEvent.Time = _lobbyTime; _lobbyTime += TimeSpan.FromMilliseconds(10); _prestartQueue.Enqueue(replayEvent); } else { replayEvent.Time = DateTime.Now - _startTime; ReplayEvent.Write(replayEvent, _binaryWriter); _binaryWriter.Flush(); } }
public static void Write(ReplayEvent eve, BinaryWriter writer) { writer.Write((byte)eve.Type); writer.Write(eve.Time.Ticks); writer.Write(eve.PlayerId); writer.Write(eve.Action.Length); writer.Write(eve.Action); }
public static ReplayReader FromStream(Stream stream) { var binaryReader = new BinaryReader(stream ?? throw new ArgumentNullException(nameof(stream))); var headerCount = binaryReader.ReadInt32(); var name = binaryReader.ReadString(); var gameIdString = binaryReader.ReadString(); var username = binaryReader.ReadString(); var gameStartTimeTicks = binaryReader.ReadInt64(); var gameStartTime = TimeSpan.FromTicks(gameStartTimeTicks); var gameId = Guid.Parse(gameIdString); var replay = new Replay { Name = name, GameId = gameId, User = username, GameStartTime = gameStartTime }; var pos = binaryReader.BaseStream.Position; var replayList = new List <ReplayEvent>(); long lengthTicks = 0; var nextId = 0; while (true) { try { var eve = ReplayEvent.Read(binaryReader); eve.Id = nextId; nextId++; lengthTicks = eve.Time.Ticks; replayList.Add(eve); } catch (EndOfStreamException) { break; } } binaryReader.BaseStream.Position = pos; replay.GameLength = TimeSpan.FromTicks(lengthTicks); return(new ReplayReader(replayList, replay)); }