Browse Source

debug

tags/0.1.0
ysqTHU 3 years ago
parent
commit
4e9579743b
6 changed files with 2 additions and 160 deletions
  1. +0
    -5
      playback/Playback/MessageReader.cs
  2. +1
    -1
      playback/Playback/Playback.csproj
  3. +1
    -1
      playback/Playback/PlaybackConstant.cs
  4. +0
    -137
      playback/PlaybackForUnity/MessageReaderForUnity.cs
  5. +0
    -8
      playback/PlaybackForUnity/PlaybackConstantForUnity.cs
  6. +0
    -8
      playback/PlaybackForUnity/PlaybackForUnity.csproj

+ 0
- 5
playback/Playback/MessageReader.cs View File

@@ -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);
}



+ 1
- 1
playback/Playback/Playback.csproj View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;netstandard2.1</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>


+ 1
- 1
playback/Playback/PlaybackConstant.cs View File

@@ -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
}
}

+ 0
- 137
playback/PlaybackForUnity/MessageReaderForUnity.cs View File

@@ -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();
}
}
}

+ 0
- 8
playback/PlaybackForUnity/PlaybackConstantForUnity.cs View File

@@ -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 }; // 文件前缀,用于标识文件类型
}
}

+ 0
- 8
playback/PlaybackForUnity/PlaybackForUnity.csproj View File

@@ -1,8 +0,0 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>


</Project>

Loading…
Cancel
Save