diff --git a/playback/Playback/MessageReader.cs b/playback/Playback/MessageReader.cs index 9f983da..f4badfe 100644 --- a/playback/Playback/MessageReader.cs +++ b/playback/Playback/MessageReader.cs @@ -45,11 +45,6 @@ namespace Playback fs.Read(bt, 0, bt.Length); for (int i = 0; i < prefixLen; ++i) { - if (i == 2) - { - if (bt[i] == 0) // 为了做一个兼容,0和5均视为5 - continue; - } if (bt[i] != PlayBackConstant.Prefix[i]) throw new FileFormatNotLegalException(fileName); } diff --git a/playback/Playback/Playback.csproj b/playback/Playback/Playback.csproj index 132c02c..70e6d49 100644 --- a/playback/Playback/Playback.csproj +++ b/playback/Playback/Playback.csproj @@ -1,7 +1,7 @@ - net6.0 + net6.0;netstandard2.1 enable enable diff --git a/playback/Playback/PlaybackConstant.cs b/playback/Playback/PlaybackConstant.cs index 73de67f..fae4668 100644 --- a/playback/Playback/PlaybackConstant.cs +++ b/playback/Playback/PlaybackConstant.cs @@ -3,6 +3,6 @@ namespace Playback public static class PlayBackConstant { public static string ExtendedName = ".thuaipb"; - public static byte[] Prefix = { (byte)'P', (byte)'B', 0, 0 }; // 文件前缀,用于标识文件类型,版本号为5 + public static byte[] Prefix = { (byte)'P', (byte)'B', 5, 0 }; // 文件前缀,用于标识文件类型,版本号为6 } } diff --git a/playback/PlaybackForUnity/MessageReaderForUnity.cs b/playback/PlaybackForUnity/MessageReaderForUnity.cs deleted file mode 100644 index 75f96e8..0000000 --- a/playback/PlaybackForUnity/MessageReaderForUnity.cs +++ /dev/null @@ -1,137 +0,0 @@ -using Communication.Proto; -using Google.Protobuf; -using System; -using System.IO; -using System.IO.Compression; - -namespace Playback -{ - public class FileFormatNotLegalException : Exception - { - private readonly string fileName; - public FileFormatNotLegalException(string fileName) - { - this.fileName = fileName; - } - public override string Message => $"The file: " + this.fileName + " is not a legal playback file for THUAI6."; - } - - public class MessageReader : IDisposable - { - private FileStream fs; - private CodedInputStream cos; - private GZipStream gzs; - private byte[] buffer; - public bool Finished { get; private set; } = false; - - public readonly uint teamCount; - public readonly uint playerCount; - - const int bufferMaxSize = 1024 * 1024; // 1M - - public MessageReader(string fileName) - { - if (!fileName.EndsWith(PlaybackConstantForUnity.ExtendedName)) - { - fileName += PlaybackConstantForUnity.ExtendedName; - } - - fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); - - try - { - var prefixLen = PlaybackConstantForUnity.Prefix.Length; - byte[] bt = new byte[prefixLen + sizeof(UInt32) * 2]; - fs.Read(bt, 0, bt.Length); - for (int i = 0; i < prefixLen; ++i) - { - if (i == 2) - { - if (bt[i] == 0) - continue; - } - if (bt[i] != PlaybackConstantForUnity.Prefix[i]) throw new FileFormatNotLegalException(fileName); - } - - teamCount = BitConverter.ToUInt32(bt, prefixLen); - playerCount = BitConverter.ToUInt32(bt, prefixLen + sizeof(UInt32)); - } - catch - { - throw new FileFormatNotLegalException(fileName); - } - - gzs = new GZipStream(fs, CompressionMode.Decompress); - var tmpBuffer = new byte[bufferMaxSize]; - var bufferSize = gzs.Read(tmpBuffer, 0, tmpBuffer.Length); - if (bufferSize == 0) - { - buffer = tmpBuffer; - Finished = true; - } - else if (bufferSize != bufferMaxSize) // 不留空位,防止 CodedInputStream 获取信息错误 - { - if (bufferSize == 0) - { - Finished = true; - } - buffer = new byte[bufferSize]; - Array.Copy(tmpBuffer, buffer, bufferSize); - } - else - { - buffer = tmpBuffer; - } - cos = new CodedInputStream(buffer); - } - - public MessageToClient ReadOne() - { - beginRead: - if (Finished) return null; - var pos = cos.Position; - try - { - MessageToClient msg = new MessageToClient(); - cos.ReadMessage(msg); - return msg; - } - catch (InvalidProtocolBufferException) - { - var leftByte = buffer.Length - pos; // 上次读取剩余的字节 - for (int i = 0; i < leftByte; ++i) - { - buffer[i] = buffer[pos + i]; - } - var bufferSize = gzs.Read(buffer, (int)leftByte, (int)(buffer.Length - leftByte)) + leftByte; - if (bufferSize == leftByte) - { - Finished = true; - return null; - } - if (bufferSize != buffer.Length) // 不留空位,防止 CodedInputStream 获取信息错误 - { - var tmpBuffer = new byte[bufferSize]; - Array.Copy(buffer, tmpBuffer, bufferSize); - buffer = tmpBuffer; - } - cos = new CodedInputStream(buffer); - goto beginRead; - } - } - - public void Dispose() - { - Finished = true; - if (fs.CanRead) - { - fs.Close(); - } - } - - ~MessageReader() - { - Dispose(); - } - } -} diff --git a/playback/PlaybackForUnity/PlaybackConstantForUnity.cs b/playback/PlaybackForUnity/PlaybackConstantForUnity.cs deleted file mode 100644 index b4d1701..0000000 --- a/playback/PlaybackForUnity/PlaybackConstantForUnity.cs +++ /dev/null @@ -1,8 +0,0 @@ -namespace Playback -{ - public static class PlaybackConstantForUnity - { - public static string ExtendedName = ".thuaipb"; - public static byte[] Prefix = { (byte)'P', (byte)'B', 0, 0 }; // 文件前缀,用于标识文件类型 - } -} diff --git a/playback/PlaybackForUnity/PlaybackForUnity.csproj b/playback/PlaybackForUnity/PlaybackForUnity.csproj deleted file mode 100644 index 9581f42..0000000 --- a/playback/PlaybackForUnity/PlaybackForUnity.csproj +++ /dev/null @@ -1,8 +0,0 @@ - - - - netstandard2.0 - - - -