Skip to content

Commit d409c40

Browse files
committed
to files
1 parent 5f4d6c0 commit d409c40

23 files changed

+1316
-1218
lines changed

src/AET.SteamAbstraction/AET.SteamAbstraction.Test/Vdf/VObjectFacts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace AET.SteamAbstraction.Test.Vdf;
55

6+
// Taken from https://github.com/shravan2x/Gameloop.Vdf
7+
68
public class VObjectFacts
79
{
810
[Fact]

src/AET.SteamAbstraction/AET.SteamAbstraction.Test/Vdf/VPropertyFacts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace AET.SteamAbstraction.Test.Vdf;
55

6+
// Taken from https://github.com/shravan2x/Gameloop.Vdf
7+
68
public class VPropertyFacts
79
{
810
[Fact]

src/AET.SteamAbstraction/AET.SteamAbstraction.Test/Vdf/VValueFacts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
namespace AET.SteamAbstraction.Test.Vdf;
55

6+
// Taken from https://github.com/shravan2x/Gameloop.Vdf
7+
68
public class VValueFacts
79
{
810
[Fact]

src/AET.SteamAbstraction/AET.SteamAbstraction.Test/Vdf/VdfConvertFacts.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
namespace AET.SteamAbstraction.Test.Vdf;
77

8+
// Taken from https://github.com/shravan2x/Gameloop.Vdf
9+
810
public class VdfConvertFacts
911
{
1012
[Fact]

src/AET.SteamAbstraction/AET.SteamAbstraction/SteamVdfReader.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using AET.SteamAbstraction.Library;
66
using AET.SteamAbstraction.Vdf;
77
using AET.SteamAbstraction.Vdf.Linq;
8+
using AET.SteamAbstraction.Vdf.Utilities;
89
using AnakinRaW.CommonUtilities.FileSystem;
910
using Microsoft.Extensions.DependencyInjection;
1011

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System.Collections.Generic;
2+
3+
namespace AET.SteamAbstraction.Vdf.Linq;
4+
5+
// Taken from https://github.com/shravan2x/Gameloop.Vdf
6+
7+
internal interface IVEnumerable<out T> : IEnumerable<T> where T : VToken
8+
{
9+
IVEnumerable<VToken> this[object key] { get; }
10+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace AET.SteamAbstraction.Vdf.Linq;
6+
7+
// Taken from https://github.com/shravan2x/Gameloop.Vdf
8+
9+
internal class VConditional : VToken
10+
{
11+
private readonly List<Token> _tokens = new();
12+
13+
public override VTokenType Type => VTokenType.Conditional;
14+
15+
public override VToken DeepClone()
16+
{
17+
var newCond = new VConditional();
18+
foreach (var token in _tokens)
19+
newCond.Add(token.DeepClone());
20+
21+
return newCond;
22+
}
23+
24+
public override void WriteTo(VdfWriter writer)
25+
{
26+
writer.WriteConditional(_tokens);
27+
}
28+
29+
protected override bool DeepEquals(VToken token)
30+
{
31+
if (token is not VConditional otherCond)
32+
return false;
33+
34+
return _tokens.Count == otherCond._tokens.Count && Enumerable.Range(0, _tokens.Count).All(x => Token.DeepEquals(_tokens[x], otherCond._tokens[x]));
35+
}
36+
37+
public void Add(Token token)
38+
{
39+
_tokens.Add(token);
40+
}
41+
42+
public bool Evaluate(IReadOnlyList<string> definedConditionals)
43+
{
44+
var index = 0;
45+
46+
bool EvaluateToken()
47+
{
48+
if (_tokens[index].TokenType != TokenType.Not && _tokens[index].TokenType != TokenType.Constant)
49+
throw new Exception($"Unexpected conditional token type ({_tokens[index].TokenType}).");
50+
51+
var isNot = false;
52+
if (_tokens[index].TokenType == TokenType.Not)
53+
{
54+
isNot = true;
55+
index++;
56+
}
57+
58+
if (_tokens[index].TokenType != TokenType.Constant)
59+
throw new Exception($"Unexpected conditional token type ({_tokens[index].TokenType}).");
60+
61+
return isNot ^ definedConditionals.Contains(_tokens[index++].Name!);
62+
}
63+
64+
var runningResult = EvaluateToken();
65+
while (index < _tokens.Count)
66+
{
67+
var tokenType = _tokens[index++].TokenType;
68+
69+
if (tokenType == TokenType.Or)
70+
runningResult |= EvaluateToken();
71+
else if (tokenType == TokenType.And)
72+
runningResult &= EvaluateToken();
73+
else
74+
throw new Exception($"Unexpected conditional token type ({tokenType}).");
75+
}
76+
77+
return runningResult;
78+
}
79+
80+
public readonly struct Token(TokenType tokenType, string? name = null)
81+
{
82+
public TokenType TokenType { get; } = tokenType;
83+
public string? Name { get; } = name;
84+
85+
public Token DeepClone()
86+
{
87+
return new Token(TokenType, Name);
88+
}
89+
90+
public static bool DeepEquals(Token t1, Token t2)
91+
{
92+
return t1.TokenType == t2.TokenType && t1.Name == t2.Name;
93+
}
94+
}
95+
96+
public enum TokenType
97+
{
98+
Constant = 0,
99+
Not = 1,
100+
Or = 2,
101+
And = 3
102+
}
103+
}

0 commit comments

Comments
 (0)