Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce GetSByte and GetSBytes to ArkArchive; handle sbyte and byte scenarios #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 41 additions & 43 deletions ArkSavegameToolkitNet/ArkArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -268,42 +264,51 @@ 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;

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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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();
}
}
}
}
2 changes: 1 addition & 1 deletion ArkSavegameToolkitNet/ArkLocalProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions ArkSavegameToolkitNet/Arrays/ArkArrayBool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool?>().ToArray());

bool IsByteValueNotZero(byte b) => b != 0;
}

public Type ValueClass => typeof(bool?);
Expand Down
9 changes: 3 additions & 6 deletions ArkSavegameToolkitNet/Arrays/ArkArrayByte.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace ArkSavegameToolkitNet.Arrays
{
public class ArkArrayByte : List<sbyte?>, IArkArray<sbyte?>
public class ArkArrayByte : List<byte?>, IArkArray<byte?>
{
public ArkArrayByte()
{
Expand All @@ -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<byte?>().ToArray());
}

public Type ValueClass => typeof(sbyte?);
public Type ValueClass => typeof(byte?);

//public int calculateSize(bool nameTable)
//{
Expand Down
5 changes: 1 addition & 4 deletions ArkSavegameToolkitNet/Arrays/ArkArrayInt8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<sbyte?>().ToArray());
}

public Type ValueClass => typeof(sbyte?);
Expand Down
2 changes: 1 addition & 1 deletion ArkSavegameToolkitNet/Data/ExtraDataBlob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
//{
Expand Down
2 changes: 1 addition & 1 deletion ArkSavegameToolkitNet/Property/PropertyInt8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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?);
Expand Down
2 changes: 1 addition & 1 deletion ArkSavegameToolkitNet/Property/PropertyText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 10 additions & 10 deletions ArkSavegameToolkitNet/Structs/StructColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
37 changes: 14 additions & 23 deletions ArkSavegameToolkitNet/Types/ArkByteValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ public class ArkByteValue : INameContainer, IConvertible
{
private bool _fromEnum;

private sbyte _byteValue;
private byte _byteValue;

private ArkName _enumName;

private ArkName _nameValue;

public ArkByteValue() { }

public ArkByteValue(sbyte byteValue)
public ArkByteValue(byte byteValue)
{
_fromEnum = false;
_enumName = ArkName.NONE_NAME;
Expand All @@ -47,7 +47,7 @@ public ArkByteValue(ArkArchive archive, ArkName enumName, bool propertyIsExclude
public bool FromEnum => _fromEnum;

[JsonProperty]
public sbyte ByteValue
public byte ByteValue
{
get
{
Expand All @@ -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;
Expand All @@ -89,12 +84,6 @@ public void read(ArkArchive archive, ArkName enumName, bool propertyIsExcluded =
}
}

//public void CollectNames(ISet<string> nameTable)
//{
// nameTable.Add(_enumName.Name);
// if (_fromEnum) nameTable.Add(_nameValue.Name);
//}

public TypeCode GetTypeCode()
{
throw new NotImplementedException();
Expand All @@ -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)
Expand Down
Loading