From 3b008d30d96db0f4a89f3fdcd4716c6583f4f8dd Mon Sep 17 00:00:00 2001 From: Brett Caswell Date: Mon, 17 Feb 2020 20:12:40 -0600 Subject: [PATCH] add GetSByte and GetSBytes method to ArkArchive; modify sbyte to byte in ArkByteValue Type and ArkArrayByte ; implement BlockCopy for byte to sbyte conversion and remove both commented out code and unnecessary casts in ArkByteValue.cs; handle some sbyte[] scenarios that should be byte[] and ensure Int8 uses SByte. --- ArkSavegameToolkitNet/ArkArchive.cs | 84 +++++++++---------- ArkSavegameToolkitNet/ArkLocalProfile.cs | 2 +- ArkSavegameToolkitNet/Arrays/ArkArrayBool.cs | 7 +- ArkSavegameToolkitNet/Arrays/ArkArrayByte.cs | 9 +- ArkSavegameToolkitNet/Arrays/ArkArrayInt8.cs | 5 +- ArkSavegameToolkitNet/Data/ExtraDataBlob.cs | 2 +- .../Property/PropertyInt8.cs | 2 +- .../Property/PropertyText.cs | 2 +- ArkSavegameToolkitNet/Structs/StructColor.cs | 20 ++--- ArkSavegameToolkitNet/Types/ArkByteValue.cs | 37 ++++---- ArkSavegameToolkitNet/Types/EmbeddedData.cs | 6 +- 11 files changed, 79 insertions(+), 97 deletions(-) diff --git a/ArkSavegameToolkitNet/ArkArchive.cs b/ArkSavegameToolkitNet/ArkArchive.cs index 4932b05..f911ee2 100644 --- a/ArkSavegameToolkitNet/ArkArchive.cs +++ b/ArkSavegameToolkitNet/ArkArchive.cs @@ -254,11 +254,7 @@ public void SkipString() public int GetInt(long position) { const long size = 4; - if (position + size > _size) - { - _logger.Error($"Trying to read {size} bytes at {position:X} with just {_size - position} bytes left"); - throw new OverflowException(); - } + CheckForOverflow(size); var value = _va.ReadInt32(position); @@ -268,27 +264,40 @@ public int GetInt(long position) public int GetInt() { const long size = 4; - if (_position + size > _size) - { - _logger.Error($"Trying to read {size} bytes at {_position:X} with just {_size - _position} bytes left"); - throw new OverflowException(); - } + CheckForOverflow(size); var value = _va.ReadInt32(_position); _position += size; return value; } + + public byte[] GetBytes(int length) + { + var buffer = new byte[length]; + CheckForOverflow(length); + + var value = _va.ReadArray(_position, buffer, 0, length); + _position += length; + + return buffer; + } + + public byte GetByte() + { + const long size = 1; + CheckForOverflow(size); + + var value = _va.ReadByte(_position); + _position += size; + + return value; + } - public sbyte[] GetBytes(int length) + public sbyte[] GetSBytes(int length) { var buffer = new sbyte[length]; - - if (_position + length > _size) - { - _logger.Error($"Trying to read {length} bytes at {_position:X} with just {_size - _position} bytes left"); - throw new OverflowException(); - } + CheckForOverflow(length); var value = _va.ReadArray(_position, buffer, 0, length); _position += length; @@ -296,14 +305,10 @@ public sbyte[] GetBytes(int length) return buffer; } - public sbyte GetByte() + public sbyte GetSByte() { const long size = 1; - if (_position + size > _size) - { - _logger.Error($"Trying to read {size} bytes at {_position:X} with just {_size - _position} bytes left"); - throw new OverflowException(); - } + CheckForOverflow(size); var value = _va.ReadSByte(_position); _position += size; @@ -314,11 +319,7 @@ public sbyte GetByte() public long GetLong() { const long size = 8; - if (_position + size > _size) - { - _logger.Error($"Trying to read {size} bytes at {_position:X} with just {_size - _position} bytes left"); - throw new OverflowException(); - } + CheckForOverflow(size); var value = _va.ReadInt64(_position); _position += size; @@ -329,11 +330,7 @@ public long GetLong() public short GetShort() { const long size = 2; - if (_position + size > _size) - { - _logger.Error($"Trying to read {size} bytes at {_position:X} with just {_size - _position} bytes left"); - throw new OverflowException(); - } + CheckForOverflow(size); var value = _va.ReadInt16(_position); _position += size; @@ -344,11 +341,7 @@ public short GetShort() public double GetDouble() { const long size = 8; - if (_position + size > _size) - { - _logger.Error($"Trying to read {size} bytes at {_position:X} with just {_size - _position} bytes left"); - throw new OverflowException(); - } + CheckForOverflow(size); var value = _va.ReadDouble(_position); _position += size; @@ -359,11 +352,7 @@ public double GetDouble() public float GetFloat() { const long size = 4; - if (_position + size > _size) - { - _logger.Error($"Trying to read {size} bytes at {_position:X} with just {_size - _position} bytes left"); - throw new OverflowException(); - } + CheckForOverflow(size); var value = _va.ReadSingle(_position); _position += size; @@ -378,5 +367,14 @@ public bool GetBoolean() return val != 0; } + + private void CheckForOverflow(long size) + { + if (_position + size > _size) + { + _logger.Error($"Trying to read {size} bytes at {_position:X} with just {_size - _position} bytes left"); + throw new OverflowException(); + } + } } } diff --git a/ArkSavegameToolkitNet/ArkLocalProfile.cs b/ArkSavegameToolkitNet/ArkLocalProfile.cs index f6baa07..6ee5c07 100644 --- a/ArkSavegameToolkitNet/ArkLocalProfile.cs +++ b/ArkSavegameToolkitNet/ArkLocalProfile.cs @@ -47,7 +47,7 @@ public GameObject LocalProfile } } private GameObject _localprofile; - private sbyte[] unknownData; + private byte[] unknownData; private ArkNameCache _arkNameCache; private ArkNameTree _exclusivePropertyNameTree; private string _fileName; diff --git a/ArkSavegameToolkitNet/Arrays/ArkArrayBool.cs b/ArkSavegameToolkitNet/Arrays/ArkArrayBool.cs index 8479e43..ef89d00 100644 --- a/ArkSavegameToolkitNet/Arrays/ArkArrayBool.cs +++ b/ArkSavegameToolkitNet/Arrays/ArkArrayBool.cs @@ -17,10 +17,9 @@ public ArkArrayBool(ArkArchive archive, int dataSize) var size = archive.GetInt(); Capacity = size; - for (int n = 0; n < size; n++) - { - Add(archive.GetByte() != 0); - } + AddRange(archive.GetBytes(size).Select(IsByteValueNotZero).Cast().ToArray()); + + bool IsByteValueNotZero(byte b) => b != 0; } public Type ValueClass => typeof(bool?); diff --git a/ArkSavegameToolkitNet/Arrays/ArkArrayByte.cs b/ArkSavegameToolkitNet/Arrays/ArkArrayByte.cs index df43f00..9971fcd 100644 --- a/ArkSavegameToolkitNet/Arrays/ArkArrayByte.cs +++ b/ArkSavegameToolkitNet/Arrays/ArkArrayByte.cs @@ -7,7 +7,7 @@ namespace ArkSavegameToolkitNet.Arrays { - public class ArkArrayByte : List, IArkArray + public class ArkArrayByte : List, IArkArray { public ArkArrayByte() { @@ -23,13 +23,10 @@ public ArkArrayByte(ArkArchive archive, int dataSize) throw new UnreadablePropertyException(); } - for (int n = 0; n < size; n++) - { - Add(archive.GetByte()); - } + AddRange(archive.GetBytes(size).Cast().ToArray()); } - public Type ValueClass => typeof(sbyte?); + public Type ValueClass => typeof(byte?); //public int calculateSize(bool nameTable) //{ diff --git a/ArkSavegameToolkitNet/Arrays/ArkArrayInt8.cs b/ArkSavegameToolkitNet/Arrays/ArkArrayInt8.cs index f433260..27d61c7 100644 --- a/ArkSavegameToolkitNet/Arrays/ArkArrayInt8.cs +++ b/ArkSavegameToolkitNet/Arrays/ArkArrayInt8.cs @@ -17,10 +17,7 @@ public ArkArrayInt8(ArkArchive archive, int dataSize) var size = archive.GetInt(); Capacity = size; - for (int n = 0; n < size; n++) - { - this.Add(archive.GetByte()); - } + AddRange(archive.GetSBytes(size).Cast().ToArray()); } public Type ValueClass => typeof(sbyte?); diff --git a/ArkSavegameToolkitNet/Data/ExtraDataBlob.cs b/ArkSavegameToolkitNet/Data/ExtraDataBlob.cs index 25c1de9..352e6d5 100644 --- a/ArkSavegameToolkitNet/Data/ExtraDataBlob.cs +++ b/ArkSavegameToolkitNet/Data/ExtraDataBlob.cs @@ -11,7 +11,7 @@ namespace ArkSavegameToolkitNet.Data public class ExtraDataBlob : IExtraData { [JsonProperty] - public sbyte[] Data { get; set; } + public byte[] Data { get; set; } //public int calculateSize(bool nameTable) //{ diff --git a/ArkSavegameToolkitNet/Property/PropertyInt8.cs b/ArkSavegameToolkitNet/Property/PropertyInt8.cs index ea00f72..b830248 100644 --- a/ArkSavegameToolkitNet/Property/PropertyInt8.cs +++ b/ArkSavegameToolkitNet/Property/PropertyInt8.cs @@ -24,7 +24,7 @@ public PropertyInt8(ArkArchive archive, PropertyArgs args, bool propertyIsExclud return; } - _value = archive.GetByte(); + _value = archive.GetSByte(); } //public override Type ValueClass => typeof(sbyte?); diff --git a/ArkSavegameToolkitNet/Property/PropertyText.cs b/ArkSavegameToolkitNet/Property/PropertyText.cs index 40ececf..a3489b6 100644 --- a/ArkSavegameToolkitNet/Property/PropertyText.cs +++ b/ArkSavegameToolkitNet/Property/PropertyText.cs @@ -24,7 +24,7 @@ public PropertyText(ArkArchive archive, PropertyArgs args, bool propertyIsExclud return; } - _value = System.Convert.ToBase64String((byte[])(Array)archive.GetBytes(DataSize)); + _value = Convert.ToBase64String(archive.GetBytes(DataSize)); } //public override Type ValueClass => typeof(string); diff --git a/ArkSavegameToolkitNet/Structs/StructColor.cs b/ArkSavegameToolkitNet/Structs/StructColor.cs index befcc5f..29e563e 100644 --- a/ArkSavegameToolkitNet/Structs/StructColor.cs +++ b/ArkSavegameToolkitNet/Structs/StructColor.cs @@ -12,19 +12,19 @@ namespace ArkSavegameToolkitNet.Structs public class StructColor : StructBase { [JsonProperty] - public sbyte B { get; set; } + public byte B { get; set; } [JsonProperty] - public sbyte G { get; set; } + public byte G { get; set; } [JsonProperty] - public sbyte R { get; set; } + public byte R { get; set; } [JsonProperty] - public sbyte A { get; set; } + public byte A { get; set; } public StructColor(ArkName structType) : base(structType) { } - public StructColor(ArkName structType, sbyte b, sbyte g, sbyte r, sbyte a) : base(structType) + public StructColor(ArkName structType, byte b, byte g, byte r, byte a) : base(structType) { B = b; G = g; @@ -34,11 +34,11 @@ public StructColor(ArkName structType, sbyte b, sbyte g, sbyte r, sbyte a) : bas public StructColor(ArkArchive archive, ArkName structType) : base(structType) { - - B = archive.GetByte(); - G = archive.GetByte(); - R = archive.GetByte(); - A = archive.GetByte(); + var BGRA = archive.GetBytes(4); + B = BGRA[0]; + G = BGRA[1]; + R = BGRA[2]; + A = BGRA[3]; } //public override int getSize(bool nameTable) diff --git a/ArkSavegameToolkitNet/Types/ArkByteValue.cs b/ArkSavegameToolkitNet/Types/ArkByteValue.cs index 43f0fff..c449e83 100644 --- a/ArkSavegameToolkitNet/Types/ArkByteValue.cs +++ b/ArkSavegameToolkitNet/Types/ArkByteValue.cs @@ -12,7 +12,7 @@ public class ArkByteValue : INameContainer, IConvertible { private bool _fromEnum; - private sbyte _byteValue; + private byte _byteValue; private ArkName _enumName; @@ -20,7 +20,7 @@ public class ArkByteValue : INameContainer, IConvertible public ArkByteValue() { } - public ArkByteValue(sbyte byteValue) + public ArkByteValue(byte byteValue) { _fromEnum = false; _enumName = ArkName.NONE_NAME; @@ -47,7 +47,7 @@ public ArkByteValue(ArkArchive archive, ArkName enumName, bool propertyIsExclude public bool FromEnum => _fromEnum; [JsonProperty] - public sbyte ByteValue + public byte ByteValue { get { @@ -67,12 +67,7 @@ public void SetEnumValue(ArkName enumName, ArkName nameValue) _enumName = enumName; _nameValue = nameValue; } - - //public int getSize(bool nameTable) - //{ - // return _fromEnum ? ArkArchive.GetNameLength(_nameValue, nameTable) : 1; - //} - + public void read(ArkArchive archive, ArkName enumName, bool propertyIsExcluded = false) { _enumName = enumName; @@ -89,12 +84,6 @@ public void read(ArkArchive archive, ArkName enumName, bool propertyIsExcluded = } } - //public void CollectNames(ISet nameTable) - //{ - // nameTable.Add(_enumName.Name); - // if (_fromEnum) nameTable.Add(_nameValue.Name); - //} - public TypeCode GetTypeCode() { throw new NotImplementedException(); @@ -112,42 +101,44 @@ public char ToChar(IFormatProvider provider) public sbyte ToSByte(IFormatProvider provider) { - return ByteValue; + var sbyteArray = new sbyte[1]; + Buffer.BlockCopy(new[] { ByteValue }, 0, sbyteArray, 0, 1); + return sbyteArray[0]; } public byte ToByte(IFormatProvider provider) { - return (byte)ByteValue; + return ByteValue; } public short ToInt16(IFormatProvider provider) { - return (short)ByteValue; + return ByteValue; } public ushort ToUInt16(IFormatProvider provider) { - return (ushort)ByteValue; + return ByteValue; } public int ToInt32(IFormatProvider provider) { - return (int)ByteValue; + return ByteValue; } public uint ToUInt32(IFormatProvider provider) { - return (uint)ByteValue; + return ByteValue; } public long ToInt64(IFormatProvider provider) { - return (long)ByteValue; + return ByteValue; } public ulong ToUInt64(IFormatProvider provider) { - return (ulong)ByteValue; + return ByteValue; } public float ToSingle(IFormatProvider provider) diff --git a/ArkSavegameToolkitNet/Types/EmbeddedData.cs b/ArkSavegameToolkitNet/Types/EmbeddedData.cs index a82123e..1010978 100644 --- a/ArkSavegameToolkitNet/Types/EmbeddedData.cs +++ b/ArkSavegameToolkitNet/Types/EmbeddedData.cs @@ -11,7 +11,7 @@ namespace ArkSavegameToolkitNet.Types public class EmbeddedData { [JsonProperty] - public virtual sbyte[][][] Data { get; set; } + public virtual byte[][][] Data { get; set; } [JsonProperty] public string Path { get; set; } @@ -54,11 +54,11 @@ public virtual void read(ArkArchive archive) var partCount = archive.GetInt(); - Data = new sbyte[partCount][][]; + Data = new byte[partCount][][]; for (var part = 0; part < partCount; part++) { var blobCount = archive.GetInt(); - var partData = new sbyte[blobCount][]; + var partData = new byte[blobCount][]; for (var blob = 0; blob < blobCount; blob++) {