Skip to content

Commit

Permalink
update utility
Browse files Browse the repository at this point in the history
  • Loading branch information
gmhevinci committed Dec 4, 2022
1 parent 2687466 commit 5bf1d29
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 222 deletions.
22 changes: 0 additions & 22 deletions Assets/YooAsset/Runtime/Utility/BufferDefine.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Assets/YooAsset/Runtime/Utility/BufferDefine.cs.meta

This file was deleted.

130 changes: 40 additions & 90 deletions Assets/YooAsset/Runtime/Utility/BufferReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using UnityEngine;

namespace YooAsset
{
Expand Down Expand Up @@ -38,72 +37,73 @@ public byte ReadByte()
CheckReaderIndex(1);
return _buffer[_index++];
}
public sbyte ReadSbyte()
{
return (sbyte)ReadByte();
}

public bool ReadBool()
{
CheckReaderIndex(1);
return _buffer[_index++] == 1;
}
public short ReadInt16()
{
return (short)ReadUInt16();
}
public ushort ReadUInt16()
{
CheckReaderIndex(2);
ushort value = 0;
for (int i = 0; i < 2; i++)
if (BitConverter.IsLittleEndian)
{
value += (ushort)(_buffer[_index++] << (i * 8));
short value = (short)((_buffer[_index]) | (_buffer[_index + 1] << 8));
_index += 2;
return value;
}
else
{
short value = (short)((_buffer[_index] << 8) | (_buffer[_index + 1]));
_index += 2;
return value;
}
return value;
}
public int ReadInt32()
public ushort ReadUInt16()
{
return (int)ReadUInt32();
return (ushort)ReadInt16();
}
public uint ReadUInt32()
public int ReadInt32()
{
CheckReaderIndex(4);
uint value = 0;
for (int i = 0; i < 4; i++)
if (BitConverter.IsLittleEndian)
{
value += (uint)(_buffer[_index++] << (i * 8));
int value = (_buffer[_index]) | (_buffer[_index + 1] << 8) | (_buffer[_index + 2] << 16) | (_buffer[_index + 3] << 24);
_index += 4;
return value;
}
else
{
int value = (_buffer[_index] << 24) | (_buffer[_index + 1] << 16) | (_buffer[_index + 2] << 8) | (_buffer[_index + 3]);
_index += 4;
return value;
}
return value;
}
public long ReadInt64()
public uint ReadUInt32()
{
return (long)ReadUInt64();
return (uint)ReadInt32();
}
public ulong ReadUInt64()
public long ReadInt64()
{
CheckReaderIndex(8);
ulong value = 0;
for (int i = 0; i < 8; i++)
if (BitConverter.IsLittleEndian)
{
value += (ulong)(_buffer[_index++] << (i * 8));
int i1 = (_buffer[_index]) | (_buffer[_index + 1] << 8) | (_buffer[_index + 2] << 16) | (_buffer[_index + 3] << 24);
int i2 = (_buffer[_index + 4]) | (_buffer[_index + 5] << 8) | (_buffer[_index + 6] << 16) | (_buffer[_index + 7] << 24);
_index += 8;
return (uint)i1 | ((long)i2 << 32);
}
else
{
int i1 = (_buffer[_index] << 24) | (_buffer[_index + 1] << 16) | (_buffer[_index + 2] << 8) | (_buffer[_index + 3]);
int i2 = (_buffer[_index + 4] << 24) | (_buffer[_index + 5] << 16) | (_buffer[_index + 6] << 8) | (_buffer[_index + 7]);
_index += 8;
return (uint)i2 | ((long)i1 << 32);
}
return value;
}

public float ReadSingle()
{
CheckReaderIndex(4);
FloatContent content = new FloatContent();
content.uintValue = ReadUInt32();
return content.floatValue;
}
public double ReadDouble()
public ulong ReadUInt64()
{
CheckReaderIndex(8);
DoubleContent content = new DoubleContent();
content.ulongValue = ReadUInt64();
return content.doubleValue;
return (ulong)ReadInt64();
}

public string ReadUTF8()
Expand Down Expand Up @@ -137,26 +137,6 @@ public long[] ReadInt64Array()
}
return values;
}
public float[] ReadFloatArray()
{
ushort count = ReadUInt16();
float[] values = new float[count];
for (int i = 0; i < count; i++)
{
values[i] = ReadSingle();
}
return values;
}
public double[] ReadDoubleArray()
{
ushort count = ReadUInt16();
double[] values = new double[count];
for (int i = 0; i < count; i++)
{
values[i] = ReadDouble();
}
return values;
}
public string[] ReadUTF8Array()
{
ushort count = ReadUInt16();
Expand All @@ -168,36 +148,6 @@ public string[] ReadUTF8Array()
return values;
}

public Vector2 ReadVector2()
{
float x = ReadSingle();
float y = ReadSingle();
return new Vector2(x, y);
}
public Vector3 ReadVector3()
{
float x = ReadSingle();
float y = ReadSingle();
float z = ReadSingle();
return new Vector3(x, y, z);
}
public Vector4 ReadVector4()
{
float x = ReadSingle();
float y = ReadSingle();
float z = ReadSingle();
float w = ReadSingle();
return new Vector4(x, y, z, w);
}
public Quaternion ReadQuaternion()
{
float x = ReadSingle();
float y = ReadSingle();
float z = ReadSingle();
float w = ReadSingle();
return new Quaternion(x, y, z, w);
}

[Conditional("DEBUG")]
private void CheckReaderIndex(int length)
{
Expand Down
118 changes: 19 additions & 99 deletions Assets/YooAsset/Runtime/Utility/BufferWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
using System.Text;
using System.Diagnostics;
using System.IO;
using UnityEngine;

namespace YooAsset
{
/// <summary>
/// 数据存储以小端字节序为标准
/// </summary>
internal class BufferWriter
{
private readonly byte[] _buffer;
Expand Down Expand Up @@ -36,23 +38,16 @@ public void WriteToStream(FileStream fileStream)

public void WriteBytes(byte[] data)
{
WriteBytes(data, 0, data.Length);
}
public void WriteBytes(byte[] data, int offset, int count)
{
int count = data.Length;
CheckWriterIndex(count);
Buffer.BlockCopy(data, offset, _buffer, _index, count);
Buffer.BlockCopy(data, 0, _buffer, _index, count);
_index += count;
}
public void WriteByte(byte value)
{
CheckWriterIndex(1);
_buffer[_index++] = value;
}
public void WriteSbyte(sbyte value)
{
WriteByte((byte)value);
}

public void WriteBool(bool value)
{
Expand All @@ -65,10 +60,8 @@ public void WriteInt16(short value)
public void WriteUInt16(ushort value)
{
CheckWriterIndex(2);
for (int i = 0; i < 2; i++)
{
_buffer[_index++] = (byte)(value >> (i * 8));
}
_buffer[_index++] = (byte)value;
_buffer[_index++] = (byte)(value >> 8);
}
public void WriteInt32(int value)
{
Expand All @@ -77,10 +70,10 @@ public void WriteInt32(int value)
public void WriteUInt32(uint value)
{
CheckWriterIndex(4);
for (int i = 0; i < 4; i++)
{
_buffer[_index++] = (byte)(value >> (i * 8));
}
_buffer[_index++] = (byte)value;
_buffer[_index++] = (byte)(value >> 8);
_buffer[_index++] = (byte)(value >> 16);
_buffer[_index++] = (byte)(value >> 24);
}
public void WriteInt64(long value)
{
Expand All @@ -89,23 +82,14 @@ public void WriteInt64(long value)
public void WriteUInt64(ulong value)
{
CheckWriterIndex(8);
for (int i = 0; i < 8; i++)
{
_buffer[_index++] = (byte)(value >> (i * 8));
}
}

public void WriteSingle(float value)
{
FloatContent content = new FloatContent();
content.floatValue = value;
WriteUInt32(content.uintValue);
}
public void WriteDouble(double value)
{
DoubleContent content = new DoubleContent();
content.doubleValue = value;
WriteUInt64(content.ulongValue);
_buffer[_index++] = (byte)value;
_buffer[_index++] = (byte)(value >> 8);
_buffer[_index++] = (byte)(value >> 16);
_buffer[_index++] = (byte)(value >> 24);
_buffer[_index++] = (byte)(value >> 32);
_buffer[_index++] = (byte)(value >> 40);
_buffer[_index++] = (byte)(value >> 48);
_buffer[_index++] = (byte)(value >> 56);
}

public void WriteUTF8(string value)
Expand Down Expand Up @@ -163,44 +147,6 @@ public void WriteInt64Array(long[] values)
}
}
}
public void WriteSingleArray(float[] values)
{
if (values == null)
{
WriteUInt16(0);
}
else
{
int count = values.Length;
if (count > ushort.MaxValue)
throw new FormatException($"Write array length cannot be greater than {ushort.MaxValue} !");

WriteUInt16(Convert.ToUInt16(count));
for (int i = 0; i < count; i++)
{
WriteSingle(values[i]);
}
}
}
public void WriteDoubleArray(double[] values)
{
if (values == null)
{
WriteUInt16(0);
}
else
{
int count = values.Length;
if (count > ushort.MaxValue)
throw new FormatException($"Write array length cannot be greater than {ushort.MaxValue} !");

WriteUInt16(Convert.ToUInt16(count));
for (int i = 0; i < count; i++)
{
WriteDouble(values[i]);
}
}
}
public void WriteUTF8Array(string[] values)
{
if (values == null)
Expand All @@ -221,32 +167,6 @@ public void WriteUTF8Array(string[] values)
}
}

public void WriteVector2(Vector2 value)
{
WriteSingle(value.x);
WriteSingle(value.y);
}
public void WriteVector3(Vector3 value)
{
WriteSingle(value.x);
WriteSingle(value.y);
WriteSingle(value.z);
}
public void WriteVector4(Vector4 value)
{
WriteSingle(value.x);
WriteSingle(value.y);
WriteSingle(value.z);
WriteSingle(value.w);
}
public void WriteQuaternion(Quaternion value)
{
WriteSingle(value.x);
WriteSingle(value.y);
WriteSingle(value.z);
WriteSingle(value.w);
}

[Conditional("DEBUG")]
private void CheckWriterIndex(int length)
{
Expand Down

0 comments on commit 5bf1d29

Please sign in to comment.