Skip to content

Commit

Permalink
Ensured formatting is set to tabs, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PlatinumLucario committed Nov 10, 2022
1 parent 2e9659d commit a1c71b0
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 97 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ class MyBasicObj
[BinaryBooleanSize(BooleanSize.U32)]
public bool Bool32 { get; set; }

// 24-bit signed integer value
[BinaryInt24]
public int S24 { get; set; }
// 24-bit unsigned integer value
[BinaryInt24]
public uint U24 { get; set; }

// String encoded in ASCII
// Reads chars until the stream encounters a '\0'
// Writing will append a '\0' at the end of the string
Expand Down Expand Up @@ -101,6 +108,9 @@ And assume these are our input bytes (in little endian):
0x00, 0x00, 0x00, 0x00, // (bool32)false
0xD7, 0xDC, 0xFF, // (int24)-9001
0x00, 0x00, 0x80, // (uint24)0x800000
0x45, 0x6E, 0x64, 0x69, 0x61, 0x6E, 0x42, 0x69, 0x6E, 0x61, 0x72, 0x79, 0x49, 0x4F, 0x00, // (ASCII)"EndianBinaryIO\0"
0x4B, 0x00, 0x65, 0x00, 0x72, 0x00, 0x6D, 0x00, 0x61, 0x00, 0x6C, 0x00, 0x69, 0x00, 0x73, 0x00, 0x00, 0x00, 0x00, 0x00, // (UTF16-LE)"Kermalis\0\0"
Expand All @@ -121,6 +131,9 @@ reader.ReadUInt32s(obj.ArrayWith16Elements); // Reads 16 'uint's (4 bytes each)
obj.Bool32 = reader.ReadBoolean(); // Reads a 'bool' (4 bytes in this case, since the reader's current bool state is BooleanSize.U32)
obj.S24 = reader.ReadInt24(); // Reads a 24-bit signed integer (3 bytes)
obj.U24 = reader.ReadUInt24(); // Reads a 24-bit unsigned integer (3 bytes)
reader.ASCII = true; // Set the reader's ASCII state to true
obj.NullTerminatedASCIIString = reader.ReadString_NullTerminated(); // Reads ASCII chars until a '\0' is read, then returns a 'string'
Expand Down Expand Up @@ -151,6 +164,9 @@ var obj = new MyBasicObj

Bool32 = false,

S24 = -9001,
U24 = 0x800000,

NullTerminatedASCIIString = "EndianBinaryIO",
UTF16String = "Kermalis",
};
Expand All @@ -161,6 +177,8 @@ writer.WriteInt16(obj.Version); // Writes a 'short' (2 bytes)
writer.WriteDateTime(obj.Date); // Writes a 'DateTime' (8 bytes)
writer.WriteUInt32s(obj.ArrayWith16Elements); // Writes 16 'uint's (4 bytes each)
writer.WriteBoolean(obj.Bool32); // Writes a 'bool' (4 bytes in this case, since the reader's current bool state is BooleanSize.U32)
writer.WriteInt24(obj.S24); // Writes a 24-bit signed integer (3 bytes)
writer.WriteUInt24(obj.U24); // Writes a 24-bit unsigned integer (3 bytes)
writer.ASCII = true; // Set the reader's ASCII state to true
writer.WriteChars_NullTerminated(obj.NullTerminatedASCIIString); // Writes the chars in the 'string' as ASCII and appends a '\0' at the end
Expand All @@ -185,6 +203,9 @@ var obj = new MyBasicObj

Bool32 = false,

S24 = -9001,
U24 = 0x800000,

NullTerminatedASCIIString = "EndianBinaryIO",
UTF16String = "Kermalis",
};
Expand Down
45 changes: 39 additions & 6 deletions Source/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,39 @@ public BinaryIgnoreAttribute(bool ignore = true)
}
}
[AttributeUsage(AttributeTargets.Property)]
public sealed class BinaryInt24Attribute : Attribute, IBinaryAttribute<bool>
{
public bool Value { get; }
public BinaryInt24Attribute(bool isInt24 = true)
{
Value= isInt24;
}
}
public sealed class BinaryInt40Attribute : Attribute, IBinaryAttribute<bool>
{
public bool Value { get; }
public BinaryInt40Attribute(bool isInt40 = true)
{
Value= isInt40;
}
}
public sealed class BinaryInt48Attribute : Attribute, IBinaryAttribute<bool>
{
public bool Value { get; }
public BinaryInt48Attribute(bool isInt48 = true)
{
Value= isInt48;
}
}
public sealed class BinaryInt56Attribute : Attribute, IBinaryAttribute<bool>
{
public bool Value { get; }
public BinaryInt56Attribute(bool isInt56 = true)
{
Value= isInt56;
}
}
[AttributeUsage(AttributeTargets.Property)]
public sealed class BinaryBooleanSizeAttribute : Attribute, IBinaryAttribute<BooleanSize>
{
public BooleanSize Value { get; }
Expand All @@ -36,19 +69,19 @@ public sealed class BinaryASCIIAttribute : Attribute, IBinaryAttribute<bool>
{
public bool Value { get; }

public BinaryASCIIAttribute(bool ascii = true)
public BinaryASCIIAttribute(bool isAscii = true)
{
Value = ascii;
Value = isAscii;
}
}
[AttributeUsage(AttributeTargets.Property)]
public sealed class BinaryStringNullTerminatedAttribute : Attribute, IBinaryAttribute<bool>
{
public bool Value { get; }

public BinaryStringNullTerminatedAttribute(bool nullTerminated = true)
public BinaryStringNullTerminatedAttribute(bool isNullTerminated = true)
{
Value = nullTerminated;
Value = isNullTerminated;
}
}
[AttributeUsage(AttributeTargets.Property)]
Expand Down Expand Up @@ -104,8 +137,8 @@ public sealed class BinaryStringTrimNullTerminatorsAttribute : Attribute, IBinar
{
public bool Value { get; }

public BinaryStringTrimNullTerminatorsAttribute(bool trim = true)
public BinaryStringTrimNullTerminatorsAttribute(bool shouldTrim = true)
{
Value = trim;
Value = shouldTrim;
}
}
Loading

0 comments on commit a1c71b0

Please sign in to comment.