Skip to content

Commit 94cc4fc

Browse files
committed
Garbage implementation of Bool and BoolArray for VariableDef
(updates #5)
1 parent 4163881 commit 94cc4fc

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/Core/VariableDefs/VariableDef.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ public void Write(BfevWriter writer)
3232
writer.ReserveBlockWriter("VariableDefData", () => {
3333
ptr();
3434

35-
if (IntArray is not null) {
35+
if (BoolArray is not null) {
36+
foreach (bool boolean in BoolArray) {
37+
writer.Write(boolean ? 1 : 0);
38+
}
39+
}
40+
else if (IntArray is not null) {
3641
foreach (int s32 in IntArray) {
3742
writer.Write((long)s32);
3843
}

src/Core/VariableDefs/VariableDefValue.cs

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ namespace BfevLibrary.Core;
44

55
public class VariableDefValue
66
{
7+
public bool? Bool { get; set; }
8+
79
public int? Int { get; set; }
810

911
public float? Float { get; set; }
1012

13+
public bool[]? BoolArray { get; set; }
14+
1115
public int[]? IntArray { get; set; }
1216

1317
public float[]? FloatArray { get; set; }
@@ -20,12 +24,19 @@ protected void Read(BfevReader reader, long value, ushort count, ContainerDataTy
2024
{
2125
// ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
2226
switch (type) {
27+
case ContainerDataType.Bool:
28+
Bool = (int)value != 0;
29+
break;
2330
case ContainerDataType.Int:
2431
Int = (int)value;
2532
break;
2633
case ContainerDataType.Float:
2734
Float = (float)value;
2835
break;
36+
case ContainerDataType.BoolArray: {
37+
BoolArray = reader.ReadObjectsPtr(new bool[count], () => reader.ReadInt32() != 0, value);
38+
break;
39+
}
2940
case ContainerDataType.IntArray: {
3041
IntArray = reader.ReadObjectsPtr(new int[count], reader.ReadInt32, value);
3142
break;
@@ -34,11 +45,18 @@ protected void Read(BfevReader reader, long value, ushort count, ContainerDataTy
3445
FloatArray = reader.ReadObjectsPtr(new float[count], reader.ReadSingle, value);
3546
break;
3647
}
48+
default:
49+
throw new NotSupportedException($"Unsupported variable type: {type}");
3750
}
3851
}
3952

4053
protected Action? WriteData(BfevWriter writer)
4154
{
55+
if (Bool != null) {
56+
writer.Write(Bool.Value ? 1L : 0L);
57+
return null;
58+
}
59+
4260
if (Int != null) {
4361
writer.Write((ulong)Int);
4462
return null;
@@ -49,7 +67,7 @@ protected void Read(BfevReader reader, long value, ushort count, ContainerDataTy
4967
return null;
5068
}
5169

52-
if (IntArray != null || FloatArray != null) {
70+
if (BoolArray != null || IntArray != null || FloatArray != null) {
5371
return writer.ReservePtr();
5472
}
5573

@@ -60,8 +78,10 @@ protected int GetCount(ContainerDataType type)
6078
{
6179
// ReSharper disable once SwitchExpressionHandlesSomeKnownEnumValuesWithExceptionInDefault
6280
return type switch {
81+
ContainerDataType.Bool => 1,
6382
ContainerDataType.Int => 1,
6483
ContainerDataType.Float => 1,
84+
ContainerDataType.BoolArray => BoolArray!.Length,
6585
ContainerDataType.IntArray => IntArray!.Length,
6686
ContainerDataType.FloatArray => FloatArray!.Length,
6787
_ => throw new NotSupportedException($"Unsupported variable type: {type}")
@@ -70,6 +90,10 @@ protected int GetCount(ContainerDataType type)
7090

7191
public ContainerDataType GetDataType()
7292
{
93+
if (Bool != null) {
94+
return ContainerDataType.Bool;
95+
}
96+
7397
if (Int != null) {
7498
return ContainerDataType.Int;
7599
}
@@ -78,6 +102,10 @@ public ContainerDataType GetDataType()
78102
return ContainerDataType.Float;
79103
}
80104

105+
if (BoolArray != null) {
106+
return ContainerDataType.BoolArray;
107+
}
108+
81109
if (IntArray != null) {
82110
return ContainerDataType.IntArray;
83111
}

0 commit comments

Comments
 (0)