Skip to content

Commit

Permalink
Support placement type
Browse files Browse the repository at this point in the history
  • Loading branch information
sk-zk committed Jan 29, 2024
1 parent 3213cc6 commit 383907b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
27 changes: 27 additions & 0 deletions TruckLib/Sii/Placement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;

namespace TruckLib.Sii
{
/// <summary>
/// Represents the placement type used in SII files.
/// </summary>
/// <remarks>I don't know what this does, beyond that it's apparently being used for
/// Winter Wonderland portals, so the property names are a complete guess.</remarks>
public struct Placement
{
public Vector3 Position { get; set; }

public Quaternion Rotation { get; set; }

public Placement(Vector3 position, Quaternion rotation)
{
Position = position;
Rotation = rotation;
}
}
}
23 changes: 23 additions & 0 deletions TruckLib/Sii/SiiParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
Expand Down Expand Up @@ -261,6 +262,10 @@ private dynamic ParseAttributeValue(string valueStr)
if (valueStr.StartsWith(doubleQuote) && valueStr.EndsWith(doubleQuote))
return valueStr[1..^1];

// placement type
if (Regex.IsMatch(valueStr, @"\(.+, *.+, *.+\) *\(.+; *.+, *.+, *.+\)"))
return ParsePlacement(valueStr);

// expicit quaternion - undocumented but used in a few files
if (Regex.IsMatch(valueStr, @"\(.+;.+,.+,.+\)"))
return ParseQuaternion(valueStr);
Expand Down Expand Up @@ -307,6 +312,24 @@ private dynamic ParseQuaternion(string valueStr)
);
}

private dynamic ParsePlacement(string valueStr)
{
var matches = Regex.Matches(valueStr, @"\((.+), *(.+), *(.+)\) *\((.+); *(.+), *(.+), *(.+)\)");
return new Placement(
new Vector3(
ParseNumber(matches[0].Groups[1].Value),
ParseNumber(matches[0].Groups[2].Value),
ParseNumber(matches[0].Groups[3].Value)
),
new Quaternion(
ParseNumber(matches[0].Groups[5].Value),
ParseNumber(matches[0].Groups[6].Value),
ParseNumber(matches[0].Groups[7].Value),
ParseNumber(matches[0].Groups[4].Value)
)
);
}

private dynamic ParseTuple(string valueStr)
{
var tupleVals = valueStr[1..^1].Split(TupleSeperator);
Expand Down
25 changes: 24 additions & 1 deletion TruckLibTests/TruckLib/Sii/SiiParserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,30 @@ public void ParseBooleans()
}

[Fact]
public void Includes()
public void ParsePlacement()
{
var unit = @"
foo : .bar {
a: (1, 2, 3) (4; 5, 6, 7)
b: (&c6b5d1a7, &41e27800, &c48e31db) (&3f29a17a; 0, &3f3fbb90, 0)
}
";
var parser = new SiiParser();
var file = parser.DeserializeFromString(unit);

Assert.Equal(new Vector3(1, 2, 3),
file.Units[0].Attributes["a"].Position);
Assert.Equal(new Quaternion(5, 6, 7, 4),
file.Units[0].Attributes["a"].Rotation);

Assert.Equal(new Vector3(-23272.826f, 28.308594f, -1137.558f),
file.Units[0].Attributes["b"].Position);
Assert.Equal(new Quaternion(0, 0.74895573f, 0, 0.66262019f),
file.Units[0].Attributes["b"].Rotation);
}

[Fact]
public void DontInsertIncludes()
{
var unit = @"
@include ""global_include.sui""
Expand Down

0 comments on commit 383907b

Please sign in to comment.