This commit is contained in:
2021-10-20 22:22:19 +02:00
parent 78a7310a3b
commit 0863b2fd71
12 changed files with 1206 additions and 508 deletions

View File

@@ -1,6 +1,7 @@
using AnotherReplayReader.Utils;
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Text;
@@ -23,6 +24,8 @@ namespace AnotherReplayReader.ReplayFile
{ ReplayType.Lan, "局域网录像" },
{ ReplayType.Online, "官网录像" },
};
public const double FrameRate = 15.0;
public const string PostCommentator = "post Commentator";
private readonly byte _replaySaverIndex;
private readonly byte[]? _rawHeader;
@@ -31,7 +34,7 @@ namespace AnotherReplayReader.ReplayFile
public DateTime Date { get; }
public string MapName { get; }
public string MapPath { get; }
public IReadOnlyList<Player> Players { get; }
public ImmutableArray<Player> Players { get; }
public int NumberOfPlayingPlayers { get; }
public Mod Mod { get; }
public ReplayType Type { get; }
@@ -39,7 +42,7 @@ namespace AnotherReplayReader.ReplayFile
public long Size { get; }
public ReplayFooter? Footer { get; }
public IReadOnlyList<ReplayChunk>? Body { get; }
public ImmutableArray<ReplayChunk>? Body { get; }
public string FileName => System.IO.Path.GetFileNameWithoutExtension(Path);
public Player ReplaySaver => Players[_replaySaverIndex];
@@ -119,7 +122,7 @@ namespace AnotherReplayReader.ReplayFile
Players = entries["S"].Split(':')
.TakeWhile(x => !string.IsNullOrWhiteSpace(x) && x[0] != 'X')
.Select(x => new Player(x.Split(',')))
.ToList();
.ToImmutableArray();
}
catch (Exception exception)
{
@@ -219,40 +222,7 @@ namespace AnotherReplayReader.ReplayFile
}
_rawHeader = rawHeader;
Body = body;
}
public Dictionary<byte, int[]> GetCommandCounts()
{
if (Body is null)
{
throw new InvalidOperationException("Replay body must be parsed before retrieving command chunks");
}
var playerCommands = new Dictionary<byte, int[]>();
foreach (var chunk in Body)
{
if (chunk.Type != 1)
{
continue;
}
foreach (var command in CommandChunk.Parse(chunk))
{
var commandCount = playerCommands.TryGetValue(command.CommandId, out var current) ? current : new int[Players.Count];
if (command.PlayerIndex >= commandCount.Length) // unknown or unparsable command?
{
commandCount = commandCount
.Concat(new int[command.PlayerIndex - commandCount.Length + 1])
.ToArray();
}
commandCount[command.PlayerIndex] = commandCount[command.PlayerIndex] + 1;
playerCommands[command.CommandId] = commandCount;
}
}
return playerCommands;
Body = body.ToImmutableArray();
}
public Replay CloneHeader()
@@ -283,7 +253,7 @@ namespace AnotherReplayReader.ReplayFile
string size = GetSizeString(Size);
string length = Length?.ToString() ?? "录像已损坏,请先修复录像";
var replaySaver = _replaySaverIndex < Players.Count
var replaySaver = _replaySaverIndex < Players.Length
? ReplaySaver.PlayerName
: "[无法获取保存录像的玩家]";
@@ -298,7 +268,7 @@ namespace AnotherReplayReader.ReplayFile
writer.WriteLine("玩家列表:");
foreach (var player in Players)
{
if (player == Players.Last() && player.PlayerName.Equals("post Commentator"))
if (player == Players.Last() && player.PlayerName.Equals(PostCommentator))
{
break;
}