Skip to content

Commit 56d8478

Browse files
committed
Allow negative numbers in FGD metadata dictionaries
Fixes #38
1 parent 1836036 commit 56d8478

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,29 @@ input ExampleInput(void) { another_test = 1 }
444444
Assert.AreEqual(1, in1.Metadata.Count);
445445
Assert.AreEqual(1m, in1.Metadata["another_test"].Value);
446446
}
447+
448+
[TestMethod]
449+
public void TestMetadataDictionaryWithNegativeValue()
450+
{
451+
const string fgd = @"@PointClass metadata { view_attach_offset = [ -10.0, 0.0, 0.0 ] } = test : ""test"" []";
452+
var format = new FgdFormat();
453+
var def = format.Read(fgd);
454+
455+
Assert.AreEqual(1, def.Classes.Count);
456+
457+
var ent = def.Classes[0];
458+
Assert.AreEqual("test", ent.Name);
459+
Assert.AreEqual(1, ent.Dictionaries.Count);
460+
461+
var meta = ent.Dictionaries[0];
462+
var vao = meta["view_attach_offset"];
463+
Assert.AreEqual(GameDataDictionaryValueType.Array, vao.Type);
464+
Assert.IsInstanceOfType<List<GameDataDictionaryValue>>(vao.Value);
465+
466+
var values = (List<GameDataDictionaryValue>)vao.Value;
467+
Assert.AreEqual(3, values.Count);
468+
Assert.AreEqual(-10m, values[0].Value);
469+
Assert.AreEqual(0m, values[1].Value);
470+
Assert.AreEqual(0m, values[2].Value);
471+
}
447472
}

Sledge.Formats.GameData/FgdFormat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ private static GameDataDictionaryValue ParseGameDataDictionaryValue(IEnumerator<
592592
else if (cur.Value == "false") return new GameDataDictionaryValue(false);
593593
else throw new TokenParsingException(cur, $"Unknown dictionary value {cur.Value}");
594594
}
595-
else if (it.Current?.Is(TokenType.Number) == true)
595+
else if (it.Current?.Is(TokenType.Number) == true || it.Current?.Is(TokenType.Symbol, Symbols.Minus) == true)
596596
{
597597
var metaValue = TokenParsing.ParseDecimal(it);
598598
return new GameDataDictionaryValue(metaValue);

Sledge.Formats.GameData/Objects/GameDataDictionary.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Text;
1+
using System.Collections.Generic;
2+
using System.Linq;
43

54
namespace Sledge.Formats.GameData.Objects
65
{
@@ -12,5 +11,10 @@ public GameDataDictionary(string name)
1211
{
1312
Name = name;
1413
}
14+
15+
public override string ToString()
16+
{
17+
return Name + " { " + string.Join(", ", this.Select(kv => kv.Key + " = " + kv.Value)) + " }";
18+
}
1519
}
1620
}

Sledge.Formats.GameData/Objects/GameDataDictionaryValue.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34

45
namespace Sledge.Formats.GameData.Objects
@@ -42,5 +43,20 @@ public GameDataDictionaryValue(IEnumerable<GameDataDictionaryValue> values)
4243
public static implicit operator GameDataDictionaryValue(decimal val) => new GameDataDictionaryValue(val);
4344
public static implicit operator GameDataDictionaryValue(bool val) => new GameDataDictionaryValue(val);
4445
public static implicit operator GameDataDictionaryValue(GameDataDictionary val) => new GameDataDictionaryValue(val);
46+
47+
public override string ToString()
48+
{
49+
switch (Value)
50+
{
51+
case null:
52+
return "null";
53+
case string s:
54+
return '"' + s.Replace("\"", "\\\"") + '"';
55+
case IList<GameDataDictionaryValue> list:
56+
return "[ " + String.Join(",", list) + "]";
57+
default:
58+
return Value.ToString();
59+
}
60+
}
4561
}
4662
}

0 commit comments

Comments
 (0)