mirror of
https://github.com/SabreTools/SabreTools.IO.git
synced 2026-02-04 05:36:05 +00:00
Fix writing issues
This commit is contained in:
@@ -449,14 +449,21 @@ namespace SabreTools.IO.Test.Extensions
|
||||
[Fact]
|
||||
public void WriteTypeExplicitTest()
|
||||
{
|
||||
byte[] bytesWithString =
|
||||
[
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x41, 0x42, 0x43, 0x00,
|
||||
];
|
||||
|
||||
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
|
||||
var bw = new BinaryWriter(stream);
|
||||
var obj = new TestStructExplicit
|
||||
{
|
||||
FirstValue = TestEnum.RecognizedTestValue,
|
||||
SecondValue = 0x07060504,
|
||||
FifthValue = "ABC",
|
||||
};
|
||||
byte[] expected = _bytes.Take(8).ToArray();
|
||||
byte[] expected = bytesWithString.Take(12).ToArray();
|
||||
bool write = bw.WriteType(obj);
|
||||
Assert.True(write);
|
||||
ValidateBytes(expected, stream.GetBuffer());
|
||||
@@ -465,6 +472,12 @@ namespace SabreTools.IO.Test.Extensions
|
||||
[Fact]
|
||||
public void WriteTypeSequentialTest()
|
||||
{
|
||||
byte[] bytesWithString =
|
||||
[
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0A, 0x0B, 0x41, 0x42, 0x43, 0x00,
|
||||
];
|
||||
|
||||
var stream = new MemoryStream(new byte[24], 0, count: 24, true, true);
|
||||
var bw = new BinaryWriter(stream);
|
||||
var obj = new TestStructSequential
|
||||
@@ -473,8 +486,9 @@ namespace SabreTools.IO.Test.Extensions
|
||||
SecondValue = 0x07060504,
|
||||
ThirdValue = 0x0908,
|
||||
FourthValue = 0x0B0A,
|
||||
FifthValue = "ABC",
|
||||
};
|
||||
byte[] expected = _bytes.Take(12).ToArray();
|
||||
byte[] expected = bytesWithString.Take(16).ToArray();
|
||||
bool write = bw.WriteType(obj);
|
||||
Assert.True(write);
|
||||
ValidateBytes(expected, stream.GetBuffer());
|
||||
|
||||
@@ -432,14 +432,21 @@ namespace SabreTools.IO.Test.Extensions
|
||||
[Fact]
|
||||
public void WriteTypeExplicitTest()
|
||||
{
|
||||
byte[] bytesWithString =
|
||||
[
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x41, 0x42, 0x43, 0x00,
|
||||
];
|
||||
|
||||
byte[] buffer = new byte[16];
|
||||
int offset = 0;
|
||||
var obj = new TestStructExplicit
|
||||
{
|
||||
FirstValue = TestEnum.RecognizedTestValue,
|
||||
SecondValue = 0x07060504,
|
||||
FifthValue = "ABC",
|
||||
};
|
||||
byte[] expected = _bytes.Take(8).ToArray();
|
||||
byte[] expected = bytesWithString.Take(12).ToArray();
|
||||
bool write = buffer.WriteType(ref offset, obj);
|
||||
Assert.True(write);
|
||||
ValidateBytes(expected, buffer);
|
||||
@@ -448,6 +455,12 @@ namespace SabreTools.IO.Test.Extensions
|
||||
[Fact]
|
||||
public void WriteTypeSequentialTest()
|
||||
{
|
||||
byte[] bytesWithString =
|
||||
[
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0A, 0x0B, 0x41, 0x42, 0x43, 0x00,
|
||||
];
|
||||
|
||||
byte[] buffer = new byte[24];
|
||||
int offset = 0;
|
||||
var obj = new TestStructSequential
|
||||
@@ -456,8 +469,9 @@ namespace SabreTools.IO.Test.Extensions
|
||||
SecondValue = 0x07060504,
|
||||
ThirdValue = 0x0908,
|
||||
FourthValue = 0x0B0A,
|
||||
FifthValue = "ABC",
|
||||
};
|
||||
byte[] expected = _bytes.Take(12).ToArray();
|
||||
byte[] expected = bytesWithString.Take(16).ToArray();
|
||||
bool write = buffer.WriteType(ref offset, obj);
|
||||
Assert.True(write);
|
||||
ValidateBytes(expected, buffer);
|
||||
|
||||
@@ -397,13 +397,20 @@ namespace SabreTools.IO.Test.Extensions
|
||||
[Fact]
|
||||
public void WriteTypeExplicitTest()
|
||||
{
|
||||
byte[] bytesWithString =
|
||||
[
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x41, 0x42, 0x43, 0x00,
|
||||
];
|
||||
|
||||
var stream = new MemoryStream(new byte[16], 0, 16, true, true);
|
||||
var obj = new TestStructExplicit
|
||||
{
|
||||
FirstValue = TestEnum.RecognizedTestValue,
|
||||
SecondValue = 0x07060504,
|
||||
FifthValue = "ABC",
|
||||
};
|
||||
byte[] expected = _bytes.Take(8).ToArray();
|
||||
byte[] expected = bytesWithString.Take(12).ToArray();
|
||||
bool write = stream.WriteType(obj);
|
||||
Assert.True(write);
|
||||
ValidateBytes(expected, stream.GetBuffer());
|
||||
@@ -412,6 +419,12 @@ namespace SabreTools.IO.Test.Extensions
|
||||
[Fact]
|
||||
public void WriteTypeSequentialTest()
|
||||
{
|
||||
byte[] bytesWithString =
|
||||
[
|
||||
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
||||
0x08, 0x09, 0x0A, 0x0B, 0x41, 0x42, 0x43, 0x00,
|
||||
];
|
||||
|
||||
var stream = new MemoryStream(new byte[24], 0, 24, true, true);
|
||||
var obj = new TestStructSequential
|
||||
{
|
||||
@@ -419,8 +432,9 @@ namespace SabreTools.IO.Test.Extensions
|
||||
SecondValue = 0x07060504,
|
||||
ThirdValue = 0x0908,
|
||||
FourthValue = 0x0B0A,
|
||||
FifthValue = "ABC",
|
||||
};
|
||||
byte[] expected = _bytes.Take(12).ToArray();
|
||||
byte[] expected = bytesWithString.Take(16).ToArray();
|
||||
bool write = stream.WriteType(obj);
|
||||
Assert.True(write);
|
||||
ValidateBytes(expected, stream.GetBuffer());
|
||||
|
||||
@@ -465,9 +465,11 @@ namespace SabreTools.IO.Extensions
|
||||
{
|
||||
// Null values cannot be written
|
||||
if (value == null)
|
||||
return false;
|
||||
return true;
|
||||
|
||||
int typeSize = Marshal.SizeOf(type);
|
||||
if (value.GetType() != type)
|
||||
value = Convert.ChangeType(value, type);
|
||||
|
||||
var buffer = new byte[typeSize];
|
||||
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
|
||||
@@ -491,7 +493,7 @@ namespace SabreTools.IO.Extensions
|
||||
{
|
||||
// Null values cannot be written
|
||||
if (value == null)
|
||||
return false;
|
||||
return true;
|
||||
|
||||
// Get the layout information
|
||||
var layoutAttr = MarshalHelpers.GetAttribute<StructLayoutAttribute>(type);
|
||||
@@ -587,7 +589,7 @@ namespace SabreTools.IO.Extensions
|
||||
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
|
||||
string? fieldValue = fi.GetValue(instance) as string;
|
||||
if (fieldValue == null)
|
||||
return false;
|
||||
return true;
|
||||
|
||||
switch (marshalAsAttr?.Value)
|
||||
{
|
||||
|
||||
@@ -618,9 +618,11 @@ namespace SabreTools.IO.Extensions
|
||||
{
|
||||
// Null values cannot be written
|
||||
if (value == null)
|
||||
return false;
|
||||
return true;
|
||||
|
||||
int typeSize = Marshal.SizeOf(type);
|
||||
if (value.GetType() != type)
|
||||
value = Convert.ChangeType(value, type);
|
||||
|
||||
var buffer = new byte[typeSize];
|
||||
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
|
||||
@@ -644,7 +646,7 @@ namespace SabreTools.IO.Extensions
|
||||
{
|
||||
// Null values cannot be written
|
||||
if (value == null)
|
||||
return false;
|
||||
return true;
|
||||
|
||||
// Get the layout information
|
||||
var layoutAttr = MarshalHelpers.GetAttribute<StructLayoutAttribute>(type);
|
||||
@@ -740,7 +742,7 @@ namespace SabreTools.IO.Extensions
|
||||
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
|
||||
string? fieldValue = fi.GetValue(instance) as string;
|
||||
if (fieldValue == null)
|
||||
return false;
|
||||
return true;
|
||||
|
||||
switch (marshalAsAttr?.Value)
|
||||
{
|
||||
|
||||
@@ -619,9 +619,11 @@ namespace SabreTools.IO.Extensions
|
||||
{
|
||||
// Null values cannot be written
|
||||
if (value == null)
|
||||
return false;
|
||||
return true;
|
||||
|
||||
int typeSize = Marshal.SizeOf(type);
|
||||
if (value.GetType() != type)
|
||||
value = Convert.ChangeType(value, type);
|
||||
|
||||
var buffer = new byte[typeSize];
|
||||
var handle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
|
||||
@@ -645,7 +647,7 @@ namespace SabreTools.IO.Extensions
|
||||
{
|
||||
// Null values cannot be written
|
||||
if (value == null)
|
||||
return false;
|
||||
return true;
|
||||
|
||||
// Get the layout information
|
||||
var layoutAttr = MarshalHelpers.GetAttribute<StructLayoutAttribute>(type);
|
||||
@@ -742,7 +744,7 @@ namespace SabreTools.IO.Extensions
|
||||
var marshalAsAttr = MarshalHelpers.GetAttribute<MarshalAsAttribute>(fi);
|
||||
string? fieldValue = fi.GetValue(instance) as string;
|
||||
if (fieldValue == null)
|
||||
return false;
|
||||
return true;
|
||||
|
||||
switch (marshalAsAttr?.Value)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user