Skip to content

Commit

Permalink
Allow negative numbers in FGD metadata dictionaries
Browse files Browse the repository at this point in the history
Fixes #38
  • Loading branch information
LogicAndTrick committed Aug 26, 2024
1 parent 1836036 commit 56d8478
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 5 deletions.
25 changes: 25 additions & 0 deletions Sledge.Formats.GameData.Tests/Fgd/TestFgdSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -444,4 +444,29 @@ input ExampleInput(void) { another_test = 1 }
Assert.AreEqual(1, in1.Metadata.Count);
Assert.AreEqual(1m, in1.Metadata["another_test"].Value);
}

[TestMethod]
public void TestMetadataDictionaryWithNegativeValue()
{
const string fgd = @"@PointClass metadata { view_attach_offset = [ -10.0, 0.0, 0.0 ] } = test : ""test"" []";
var format = new FgdFormat();
var def = format.Read(fgd);

Assert.AreEqual(1, def.Classes.Count);

var ent = def.Classes[0];
Assert.AreEqual("test", ent.Name);
Assert.AreEqual(1, ent.Dictionaries.Count);

var meta = ent.Dictionaries[0];
var vao = meta["view_attach_offset"];
Assert.AreEqual(GameDataDictionaryValueType.Array, vao.Type);
Assert.IsInstanceOfType<List<GameDataDictionaryValue>>(vao.Value);

var values = (List<GameDataDictionaryValue>)vao.Value;
Assert.AreEqual(3, values.Count);
Assert.AreEqual(-10m, values[0].Value);
Assert.AreEqual(0m, values[1].Value);
Assert.AreEqual(0m, values[2].Value);
}
}
2 changes: 1 addition & 1 deletion Sledge.Formats.GameData/FgdFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ private static GameDataDictionaryValue ParseGameDataDictionaryValue(IEnumerator<
else if (cur.Value == "false") return new GameDataDictionaryValue(false);
else throw new TokenParsingException(cur, $"Unknown dictionary value {cur.Value}");
}
else if (it.Current?.Is(TokenType.Number) == true)
else if (it.Current?.Is(TokenType.Number) == true || it.Current?.Is(TokenType.Symbol, Symbols.Minus) == true)
{
var metaValue = TokenParsing.ParseDecimal(it);
return new GameDataDictionaryValue(metaValue);
Expand Down
10 changes: 7 additions & 3 deletions Sledge.Formats.GameData/Objects/GameDataDictionary.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections.Generic;
using System.Linq;

namespace Sledge.Formats.GameData.Objects
{
Expand All @@ -12,5 +11,10 @@ public GameDataDictionary(string name)
{
Name = name;
}

public override string ToString()
{
return Name + " { " + string.Join(", ", this.Select(kv => kv.Key + " = " + kv.Value)) + " }";
}
}
}
18 changes: 17 additions & 1 deletion Sledge.Formats.GameData/Objects/GameDataDictionaryValue.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Sledge.Formats.GameData.Objects
Expand Down Expand Up @@ -42,5 +43,20 @@ public GameDataDictionaryValue(IEnumerable<GameDataDictionaryValue> values)
public static implicit operator GameDataDictionaryValue(decimal val) => new GameDataDictionaryValue(val);
public static implicit operator GameDataDictionaryValue(bool val) => new GameDataDictionaryValue(val);
public static implicit operator GameDataDictionaryValue(GameDataDictionary val) => new GameDataDictionaryValue(val);

public override string ToString()
{
switch (Value)
{
case null:
return "null";
case string s:
return '"' + s.Replace("\"", "\\\"") + '"';
case IList<GameDataDictionaryValue> list:
return "[ " + String.Join(",", list) + "]";
default:
return Value.ToString();
}
}
}
}

0 comments on commit 56d8478

Please sign in to comment.