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

initial VariableDef implementation #3

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
15 changes: 13 additions & 2 deletions src/Core/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ namespace BfevLibrary.Core;

public class EntryPoint : IBfevDataBlock
{
public RadixTree<VariableDef>? Parameters { get; set; } = new();
public List<short> SubFlowEventIndices { get; set; }
public short EventIndex { get; set; }

Expand All @@ -17,11 +18,21 @@ public EntryPoint(BfevReader reader)
public IBfevDataBlock Read(BfevReader reader)
{
long subFlowEventIndicesPtr = reader.ReadInt64();
reader.BaseStream.Position += 8 + 8; // unused (in botw) VariableDef pointers (ulong, ulong)
long entrypointVariableDefNamesPtr = reader.ReadInt64();
long entrypointVariableDefPtr = reader.ReadInt64();
ushort subFlowEventIndicesCount = reader.ReadUInt16();
reader.BaseStream.Position += 2; // unused (in botw) VariableDef count (ushort)
ushort varDefCount= reader.ReadUInt16();
EventIndex = reader.ReadInt16();

reader.BaseStream.Position += 2; // padding

if (varDefCount > 0)
{
var variableDefinitions = new VariableDef[varDefCount];
reader.ReadObjectsPtr(variableDefinitions, () => new VariableDef(reader, false), entrypointVariableDefPtr);
reader.TemporarySeek(entrypointVariableDefNamesPtr, SeekOrigin.Begin, () => Parameters = new RadixTree<VariableDef>(reader, variableDefinitions));
}

SubFlowEventIndices = reader.ReadObjectsPtr(new short[subFlowEventIndicesCount], reader.ReadInt16, subFlowEventIndicesPtr).ToList();
return this;
}
Expand Down
108 changes: 108 additions & 0 deletions src/Core/VariableDef.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using BfevLibrary.Common;
using BfevLibrary.Parsers;
using System.Buffers.Binary;

namespace BfevLibrary.Core;
public class VariableDef : IBfevDataBlock
{
public enum VariableDefType : ushort
{
Int = 2,
Bool = 3,
Float = 4
}

public VariableDefType GetVariableType()
{
if (Int != null)
{
return VariableDefType.Int;
}
else if (Bool != null)
{
return VariableDefType.Bool;
}
else if (Float != null)
{
return VariableDefType.Float;
}
else
{
throw new NotImplementedException();
}
}

/// <summary>
/// Creates a new Variable Definition
/// </summary>
/// <param name="reader"></param>
/// <param name="isImplicit"><inheritdoc cref="IsImplicit" path="/summary"/></param>
public VariableDef(BfevReader reader, bool isImplicit)
{
Read(reader);
IsImplicit = isImplicit;
}

public VariableDefType Type { get; set; }
public int? Int { get; set; }
public bool? Bool { get; set; }
public float? Float { get; set; }

/// <summary>
/// Whether or not the variable is being implicitly used in some <see cref="ActionEvent"/>. <b>False</b> means it has been loaded by an <seealso cref="EntryPoint"/>.
/// </summary>
public bool IsImplicit { get; set; } = false;

public IBfevDataBlock Read(BfevReader reader)
{
byte[] initialValueRaw = reader.ReadBytes(8);
var parametersCount = reader.ReadUInt16();

// No arrays is expected, may change in the future
if (parametersCount == 1)
{
Type = (VariableDefType) reader.ReadUInt16();

switch (Type)
{
case VariableDefType.Int:
Int = BinaryPrimitives.ReadInt32LittleEndian(initialValueRaw.AsSpan(0, 4));
break;

case VariableDefType.Bool:
Bool = initialValueRaw[0] != 0;
break;

case VariableDefType.Float:
Float = BinaryPrimitives.ReadSingleLittleEndian(initialValueRaw.AsSpan(0, 4));
break;

default:
throw new InvalidDataException($"Unsupported VarDef type: {Type}");
}

reader.BaseStream.Position += 4; // padding
}
return this;
}


public void Write(BfevWriter writer)
{
byte[] initialValueRaw = new byte[8];

if (Int.HasValue)
BinaryPrimitives.WriteInt32LittleEndian(initialValueRaw.AsSpan(0, 4), Int.Value);
else if (Bool.HasValue)
initialValueRaw[0] = (byte)(Bool.Value ? 1 : 0);

else if (Float.HasValue)
BinaryPrimitives.WriteSingleLittleEndian(initialValueRaw.AsSpan(0, 4), Float.Value);


writer.Write(initialValueRaw);
writer.Write((ushort) 1);
writer.Write((ushort) GetVariableType());
writer.Write(0); // padding
}
}