mirror of
https://github.com/Alex-Rachel/TEngine.git
synced 2025-08-07 16:45:10 +00:00
Create BinaryExtension
Create BinaryExtension
This commit is contained in:
190
Assets/TEngine/Scripts/Runtime/Core/Utility/BinaryExtension.cs
Normal file
190
Assets/TEngine/Scripts/Runtime/Core/Utility/BinaryExtension.cs
Normal file
@@ -0,0 +1,190 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using TEngine.Runtime;
|
||||
|
||||
/// <summary>
|
||||
/// 对 BinaryReader 和 BinaryWriter 的扩展方法。
|
||||
/// </summary>
|
||||
public static class BinaryExtension
|
||||
{
|
||||
private static readonly byte[] s_CachedBytes = new byte[byte.MaxValue + 1];
|
||||
|
||||
/// <summary>
|
||||
/// 从二进制流读取编码后的 32 位有符号整数。
|
||||
/// </summary>
|
||||
/// <param name="binaryReader">要读取的二进制流。</param>
|
||||
/// <returns>读取的 32 位有符号整数。</returns>
|
||||
public static int Read7BitEncodedInt32(this BinaryReader binaryReader)
|
||||
{
|
||||
int value = 0;
|
||||
int shift = 0;
|
||||
byte b;
|
||||
do
|
||||
{
|
||||
if (shift >= 35)
|
||||
{
|
||||
throw new Exception("7 bit encoded int is invalid.");
|
||||
}
|
||||
|
||||
b = binaryReader.ReadByte();
|
||||
value |= (b & 0x7f) << shift;
|
||||
shift += 7;
|
||||
} while ((b & 0x80) != 0);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向二进制流写入编码后的 32 位有符号整数。
|
||||
/// </summary>
|
||||
/// <param name="binaryWriter">要写入的二进制流。</param>
|
||||
/// <param name="value">要写入的 32 位有符号整数。</param>
|
||||
public static void Write7BitEncodedInt32(this BinaryWriter binaryWriter, int value)
|
||||
{
|
||||
uint num = (uint)value;
|
||||
while (num >= 0x80)
|
||||
{
|
||||
binaryWriter.Write((byte)(num | 0x80));
|
||||
num >>= 7;
|
||||
}
|
||||
|
||||
binaryWriter.Write((byte)num);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从二进制流读取编码后的 32 位无符号整数。
|
||||
/// </summary>
|
||||
/// <param name="binaryReader">要读取的二进制流。</param>
|
||||
/// <returns>读取的 32 位无符号整数。</returns>
|
||||
public static uint Read7BitEncodedUInt32(this BinaryReader binaryReader)
|
||||
{
|
||||
return (uint)Read7BitEncodedInt32(binaryReader);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向二进制流写入编码后的 32 位无符号整数。
|
||||
/// </summary>
|
||||
/// <param name="binaryWriter">要写入的二进制流。</param>
|
||||
/// <param name="value">要写入的 32 位无符号整数。</param>
|
||||
public static void Write7BitEncodedUInt32(this BinaryWriter binaryWriter, uint value)
|
||||
{
|
||||
Write7BitEncodedInt32(binaryWriter, (int)value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从二进制流读取编码后的 64 位有符号整数。
|
||||
/// </summary>
|
||||
/// <param name="binaryReader">要读取的二进制流。</param>
|
||||
/// <returns>读取的 64 位有符号整数。</returns>
|
||||
public static long Read7BitEncodedInt64(this BinaryReader binaryReader)
|
||||
{
|
||||
long value = 0L;
|
||||
int shift = 0;
|
||||
byte b;
|
||||
do
|
||||
{
|
||||
if (shift >= 70)
|
||||
{
|
||||
throw new Exception("7 bit encoded int is invalid.");
|
||||
}
|
||||
|
||||
b = binaryReader.ReadByte();
|
||||
value |= (b & 0x7fL) << shift;
|
||||
shift += 7;
|
||||
} while ((b & 0x80) != 0);
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向二进制流写入编码后的 64 位有符号整数。
|
||||
/// </summary>
|
||||
/// <param name="binaryWriter">要写入的二进制流。</param>
|
||||
/// <param name="value">要写入的 64 位有符号整数。</param>
|
||||
public static void Write7BitEncodedInt64(this BinaryWriter binaryWriter, long value)
|
||||
{
|
||||
ulong num = (ulong)value;
|
||||
while (num >= 0x80)
|
||||
{
|
||||
binaryWriter.Write((byte)(num | 0x80));
|
||||
num >>= 7;
|
||||
}
|
||||
|
||||
binaryWriter.Write((byte)num);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从二进制流读取编码后的 64 位无符号整数。
|
||||
/// </summary>
|
||||
/// <param name="binaryReader">要读取的二进制流。</param>
|
||||
/// <returns>读取的 64 位无符号整数。</returns>
|
||||
public static ulong Read7BitEncodedUInt64(this BinaryReader binaryReader)
|
||||
{
|
||||
return (ulong)Read7BitEncodedInt64(binaryReader);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向二进制流写入编码后的 64 位无符号整数。
|
||||
/// </summary>
|
||||
/// <param name="binaryWriter">要写入的二进制流。</param>
|
||||
/// <param name="value">要写入的 64 位无符号整数。</param>
|
||||
public static void Write7BitEncodedUInt64(this BinaryWriter binaryWriter, ulong value)
|
||||
{
|
||||
Write7BitEncodedInt64(binaryWriter, (long)value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从二进制流读取加密字符串。
|
||||
/// </summary>
|
||||
/// <param name="binaryReader">要读取的二进制流。</param>
|
||||
/// <param name="encryptBytes">密钥数组。</param>
|
||||
/// <returns>读取的字符串。</returns>
|
||||
public static string ReadEncryptedString(this BinaryReader binaryReader, byte[] encryptBytes)
|
||||
{
|
||||
byte length = binaryReader.ReadByte();
|
||||
if (length <= 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
if (length > byte.MaxValue)
|
||||
{
|
||||
throw new Exception("String is too long.");
|
||||
}
|
||||
|
||||
for (byte i = 0; i < length; i++)
|
||||
{
|
||||
s_CachedBytes[i] = binaryReader.ReadByte();
|
||||
}
|
||||
|
||||
Utility.Encryption.GetSelfXorBytes(s_CachedBytes, 0, length, encryptBytes);
|
||||
string value = Utility.Converter.GetString(s_CachedBytes, 0, length);
|
||||
Array.Clear(s_CachedBytes, 0, length);
|
||||
return value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 向二进制流写入加密字符串。
|
||||
/// </summary>
|
||||
/// <param name="binaryWriter">要写入的二进制流。</param>
|
||||
/// <param name="value">要写入的字符串。</param>
|
||||
/// <param name="encryptBytes">密钥数组。</param>
|
||||
public static void WriteEncryptedString(this BinaryWriter binaryWriter, string value, byte[] encryptBytes)
|
||||
{
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
binaryWriter.Write((byte)0);
|
||||
return;
|
||||
}
|
||||
|
||||
int length = Utility.Converter.GetBytes(value, s_CachedBytes);
|
||||
if (length > byte.MaxValue)
|
||||
{
|
||||
throw new Exception(Utility.Text.Format("String '{0}' is too long.", value));
|
||||
}
|
||||
|
||||
Utility.Encryption.GetSelfXorBytes(s_CachedBytes, encryptBytes);
|
||||
binaryWriter.Write((byte)length);
|
||||
binaryWriter.Write(s_CachedBytes, 0, length);
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 29ffc5cfe9f6445d93c94ae60e25d607
|
||||
timeCreated: 1664441351
|
Reference in New Issue
Block a user