reverted unsafe readfstring

This commit is contained in:
iAmAsval 2020-10-11 23:48:41 +02:00
parent aeced01cce
commit e754e14a03

View File

@ -9,12 +9,10 @@ namespace PakReader
{
public static string ReadFString(this BinaryReader reader)
{
var SaveNum = reader.ReadInt32();
if (SaveNum == 0) return null;
// > 0 for ANSICHAR, < 0 for UCS2CHAR serialization
if (SaveNum < 0)
var SaveNum = reader.ReadInt32();
bool LoadUCS2Char = SaveNum < 0;
if (LoadUCS2Char)
{
// If SaveNum cannot be negated due to integer overflow, Ar is corrupted.
if (SaveNum == int.MinValue)
@ -23,14 +21,27 @@ namespace PakReader
}
SaveNum = -SaveNum;
}
var dataBytes = reader.ReadBytes(SaveNum * sizeof(char));
return Encoding.Unicode.GetString(dataBytes, 0, dataBytes.Length - 1); // 1 byte is removed because of null terminator (\0)
if (SaveNum == 0) return null;
// 1 byte is removed because of null terminator (\0)
if (LoadUCS2Char)
{
ushort[] data = new ushort[SaveNum];
for (int i = 0; i < SaveNum; i++)
{
data[i] = reader.ReadUInt16();
}
unsafe
{
fixed (ushort* dataPtr = &data[0])
return new string((char*)dataPtr, 0, data.Length - 1);
}
}
else
{
var dataBytes = reader.ReadBytes(SaveNum);
return Encoding.UTF8.GetString(dataBytes, 0, dataBytes.Length - 1); // 1 byte is removed because of null terminator (\0)
return Encoding.UTF8.GetString(reader.ReadBytes(SaveNum).AsSpan(..^1));
}
}