Skip to content

Commit fefa59c

Browse files
committed
Add support for unknown variable types
1 parent 35ff039 commit fefa59c

File tree

6 files changed

+41
-24
lines changed

6 files changed

+41
-24
lines changed

Sledge.Formats.GameData.Tests/Fgd/TestFgdBasic.cs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ public void TestProperties()
211211
2 : ""flag2"" : 1
212212
]
213213
nine(string) report readonly
214-
ten(string) readonly report
214+
ten(array:struct:light_style_event) readonly report
215+
eleven(unknown_type)
215216
]
216217
";
217218
var format = new FgdFormatter();
@@ -220,30 +221,33 @@ public void TestProperties()
220221
var cls = def.Classes[0];
221222
Assert.AreEqual("Test", cls.Name);
222223

223-
Assert.AreEqual(11, cls.Properties.Count);
224-
225-
AssertProperty(cls.Properties[0], "one", VariableType.Integer, "", "", "", false, false);
226-
AssertProperty(cls.Properties[1], "two", VariableType.String, "description", "", "", false, false);
227-
AssertProperty(cls.Properties[2], "three", VariableType.String, "desc", "default", "", false, false);
228-
AssertProperty(cls.Properties[3], "four", VariableType.Color255, "", "", "", true, false);
229-
AssertProperty(cls.Properties[4], "five", VariableType.String, "desc", "def", "", false, true);
230-
AssertProperty(cls.Properties[5], "six", VariableType.String, "d", "-123", "details", false, false);
231-
AssertProperty(cls.Properties[6], "seven", VariableType.String, "d", "", "details", false, false);
232-
AssertProperty(cls.Properties[7], "eight", VariableType.Choices, "eight", "0", "", false, false);
233-
AssertProperty(cls.Properties[8], "spawnflags", VariableType.Flags, "", "", "", false, false);
234-
AssertProperty(cls.Properties[9], "nine", VariableType.String, "", "", "", true, true);
235-
AssertProperty(cls.Properties[10], "ten", VariableType.String, "", "", "", true, true);
224+
Assert.AreEqual(12, cls.Properties.Count);
225+
226+
AssertProperty(cls.Properties[0], "one", VariableType.Integer, "", null, "", "", "", false, false);
227+
AssertProperty(cls.Properties[1], "two", VariableType.String, "", null, "description", "", "", false, false);
228+
AssertProperty(cls.Properties[2], "three", VariableType.String, "", null, "desc", "default", "", false, false);
229+
AssertProperty(cls.Properties[3], "four", VariableType.Color255, "", null, "", "", "", true, false);
230+
AssertProperty(cls.Properties[4], "five", VariableType.String, "", null, "desc", "def", "", false, true);
231+
AssertProperty(cls.Properties[5], "six", VariableType.String, "", null, "d", "-123", "details", false, false);
232+
AssertProperty(cls.Properties[6], "seven", VariableType.String, "", null, "d", "", "details", false, false);
233+
AssertProperty(cls.Properties[7], "eight", VariableType.Choices, "", null, "eight", "0", "", false, false);
234+
AssertProperty(cls.Properties[8], "spawnflags", VariableType.Flags, "", null, "", "", "", false, false);
235+
AssertProperty(cls.Properties[9], "nine", VariableType.String, "", null, "", "", "", true, true);
236+
AssertProperty(cls.Properties[10], "ten", VariableType.Array, "struct:light_style_event", null, "", "", "", true, true);
237+
AssertProperty(cls.Properties[11], "eleven", VariableType.Unknown, "", "unknown_type", "", "", "", false, false);
236238

237239
AssertOption(cls.Properties[7].Options[0], "0", "c1", false, "");
238240
AssertOption(cls.Properties[7].Options[1], "1", "c2", false, "details");
239241

240242
AssertOption(cls.Properties[8].Options[0], "1", "flag1", false, "");
241243
AssertOption(cls.Properties[8].Options[1], "2", "flag2", true, "");
242244

243-
void AssertProperty(Property actual, string name, VariableType type, string desc, string dfault, string details, bool ro, bool report)
245+
void AssertProperty(Property actual, string name, VariableType type, string subType, string unknownVariableTypeName, string desc, string dfault, string details, bool ro, bool report)
244246
{
245247
Assert.AreEqual(name, actual.Name);
246248
Assert.AreEqual(type, actual.VariableType);
249+
Assert.AreEqual(subType, actual.SubType);
250+
Assert.AreEqual(unknownVariableTypeName, actual.UnknownVariableTypeName);
247251
Assert.AreEqual(desc, actual.Description);
248252
Assert.AreEqual(dfault, actual.DefaultValue);
249253
Assert.AreEqual(details, actual.Details);

Sledge.Formats.GameData/FgdFormat.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ private void ParseClassMember(GameDataClass cls, IEnumerator<Token> it)
656656
var type = ParseVariableType(it);
657657
TokenParsing.Expect(it, TokenType.Symbol, Symbols.CloseParen);
658658

659-
var io = new IO(iotype, type.type, type.subType, name);
659+
var io = new IO(iotype, type.type, type.subType, name) { UnknownVariableTypeName = type.unknownVariableTypeName };
660660
cls.InOuts.Add(io);
661661

662662
/* Source 2 dictionary-based metadata:
@@ -682,7 +682,7 @@ output OnStartTouchAll(void) { is_activator_important = true } : "..."
682682
var type = ParseVariableType(it);
683683
TokenParsing.Expect(it, TokenType.Symbol, Symbols.CloseParen);
684684

685-
var prop = new Property(name, type.type, type.subType);
685+
var prop = new Property(name, type.type, type.subType) { UnknownVariableTypeName = type.unknownVariableTypeName };
686686
cls.Properties.Add(prop);
687687

688688
var next = it.Current;
@@ -869,7 +869,7 @@ private void ParsePropertyOption(Property prop, IEnumerator<Token> it)
869869
opt.Details = TokenParsing.ParseAppendedString(it);
870870
}
871871

872-
private (VariableType type, string subType) ParseVariableType(IEnumerator<Token> it)
872+
private static (VariableType type, string subType, string unknownVariableTypeName) ParseVariableType(IEnumerator<Token> it)
873873
{
874874
var token = it.Current;
875875
Debug.Assert(token != null, nameof(token) + " != null");
@@ -891,13 +891,13 @@ private void ParsePropertyOption(Property prop, IEnumerator<Token> it)
891891
}
892892
}
893893

894-
type = type.Replace("_", "").ToLower();
895-
if (Enum.TryParse(type, true, out VariableType vt))
894+
var typeRepl = type.Replace("_", "").ToLower();
895+
if (Enum.TryParse(typeRepl, true, out VariableType vt))
896896
{
897-
return (vt, subType);
897+
return (vt, subType, null);
898898
}
899899

900-
throw new TokenParsingException(token, $"Unknown variable type {type}");
900+
return (VariableType.Unknown, subType, type);
901901
}
902902
}
903903
}

Sledge.Formats.GameData/Objects/IO.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ public class IO
55
public IOType IOType { get; set; }
66
public VariableType VariableType { get; set; }
77
public string SubType { get; set; }
8+
/// <summary>
9+
/// If the <see cref="VariableType"/> is <see cref="Objects.VariableType.Unknown"/>, this will contain the specified type of the IO instance. Otherwise, it will be null.
10+
/// </summary>
11+
public string UnknownVariableTypeName { get; set; }
812
public string Description { get; set; }
913
public string Name { get; set; }
1014
public GameDataDictionary Metadata { get; set; }

Sledge.Formats.GameData/Objects/Property.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ public class Property
88
public string Name { get; set; }
99
public VariableType VariableType { get; set; }
1010
public string SubType { get; set; }
11+
/// <summary>
12+
/// If the <see cref="VariableType"/> is <see cref="Objects.VariableType.Unknown"/>, this will contain the specified type of the property. Otherwise, it will be null.
13+
/// </summary>
14+
public string UnknownVariableTypeName { get; set; }
1115
public string Description { get; set; }
1216
public string Details { get; set; }
1317
public string DefaultValue { get; set; }

Sledge.Formats.GameData/Objects/VariableType.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
{
33
public enum VariableType
44
{
5+
/// <summary>
6+
/// Generic for unrecognised variable types. Check UnknownVariableTypeName for the actual type name.
7+
/// </summary>
8+
Unknown,
9+
510
Axis,
611
Angle,
712
AngleNegativePitch,

Sledge.Formats.GameData/Sledge.Formats.GameData.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
55
<AssemblyVersion></AssemblyVersion>
6-
<Version>1.1.4</Version>
6+
<Version>1.1.5</Version>
77
<Authors>LogicAndTrick</Authors>
88
<Description>An FGD parser. Currently supports Worldcraft, JACK, and TrenchBroom FGD formats.</Description>
99
<Copyright>2021 LogicAndTrick</Copyright>
@@ -12,7 +12,7 @@
1212
<RepositoryUrl>https://github.com/LogicAndTrick/sledge-formats</RepositoryUrl>
1313
<RepositoryType>git</RepositoryType>
1414
<PackageTags>half-life worldcraft hammer valve fgd trenchbroom</PackageTags>
15-
<PackageReleaseNotes>Fixes for parsing issues in more recent Source 2 FGD files</PackageReleaseNotes>
15+
<PackageReleaseNotes>Add 'cpp' and 'collision_property' variable types, add support for unknown variable types</PackageReleaseNotes>
1616
</PropertyGroup>
1717

1818
<ItemGroup>

0 commit comments

Comments
 (0)