Skip to content

Commit

Permalink
Initial upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Grub4K committed Jan 26, 2021
0 parents commit b4fae37
Show file tree
Hide file tree
Showing 12 changed files with 725 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Auto detect text files and perform LF normalization
* text=auto
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Grub4K

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 1 addition & 0 deletions build.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@csc @build.rsp
12 changes: 12 additions & 0 deletions build.rsp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# VDFparse Build File
-nologo
-out:bin\VDFparse.exe

# output optimized
-O

# include all cs files
-recurse:src\*.cs

# provide manifest
-nowin32manifest
88 changes: 88 additions & 0 deletions src/VDFFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.IO;
using System.Collections.Generic;



namespace VDFparse
{
public enum EUniverse
{
Invalid = 0,
Public = 1,
Beta = 2,
Internal = 3,
Dev = 4,
Max = 5,
}

public interface VDFFileReader
{
public uint Magic { get; }
public List<Dataset> Read(BinaryReader reader);
}

public class VDFFile
{
private static List<VDFFileReader> Readers = new List<VDFFileReader>(){
new PackageInfoReaderOld(),
new PackageInfoReader(),
new AppInfoReader(),
};

public List<Dataset> Datasets { get; private set; } = new List<Dataset>();

public EUniverse Universe { get; private set; }

public void Read(string filename)
{
using (var fs = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
Read(fs);
}
}

public void Read(Stream stream)
{
using (var reader = new BinaryReader(stream))
{
var magic = reader.ReadUInt32();
Universe = (EUniverse)reader.ReadUInt32();
foreach (var reader_sub in Readers)
{
if (magic == reader_sub.Magic)
{
Datasets = reader_sub.Read(reader);
return;
}
}
throw new InvalidDataException($"Unknown header: {magic.ToString("X")}");
}
}

public Dataset FindByID(uint id)
{
foreach (var dataset in Datasets)
{
if (dataset.ID == id)
{
return dataset;
}
}
return null;
}
}

public class Dataset
{
public uint ID { get; set; }

public ReadOnlyCollection<byte> Hash { get; set; }

public ulong Token { get; set; }

public uint ChangeNumber { get; set; }

public KVObject Data { get; set; }
}
}
130 changes: 130 additions & 0 deletions src/ValveKV/KVObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
using System;
using System.Text;
using System.Linq;
using System.Drawing;
using System.Globalization;
using System.Collections.Generic;
using System.IO;



namespace VDFparse
{
public class KVObject : Dictionary<string, dynamic>, IFormattable {
public override string ToString()
{
return this.ToString("0");
}

public string ToString(string format)
{
return ToString(format, CultureInfo.CurrentCulture);
}

public string ToString(string format, IFormatProvider provider)
{
uint indent = 0;
if (String.IsNullOrEmpty(format))
{
indent = 0;
}
else
{
bool success = UInt32.TryParse(format, out indent);
if (!success)
{
throw new FormatException(String.Format("The '{0}' format string is not supported.", format));
}
}

return ToJSON((int)indent);
}

public string ToJSON(int indent)
{
var builder = new StringBuilder();
ToJSON(builder, indent);
return builder.ToString();
}

private void ToJSON(StringBuilder builder, int indent, int depth=0)
{
bool indented = indent != 0;
if (Count == 0)
{
builder.Append("{}");
return;
}
builder.Append('{');
if (indented)
builder.Append('\n');
using var enumerator = this.GetEnumerator();
var notLast = enumerator.MoveNext();
KeyValuePair<string, dynamic> keyValue;
while (notLast)
{
keyValue = enumerator.Current;
if (indented)
builder.Append(new String(' ', indent * (depth+1)));
builder.Append('"').Append(keyValue.Key).Append("\":");
if (indented)
builder.Append(' ');
Type type = keyValue.Value.GetType();
if (type == typeof(String))
builder.Append('"').Append(keyValue.Value).Append('"');
else if (type == typeof(KVObject))
keyValue.Value.ToJSON(builder, indent, depth+1);
else if (type == typeof(Color))
builder.Append(keyValue.Value.ToArgb());
else
builder.Append(keyValue.Value);
notLast = enumerator.MoveNext();
if (notLast)
{
builder.Append(',');
if (indent != 0)
builder.Append('\n');
}
}
if (indented)
builder
.Append('\n')
.Append(new String(' ', indent * depth));
builder.Append('}');
}

public IEnumerable<dynamic> Search(string fullquery, bool sub=false)
{
var query = fullquery.Split(new []{'.'}, 2);
foreach (var keyValue in this)
{
var isKVObject = keyValue.Value.GetType() == typeof(KVObject);
if (query[0] == "*" || keyValue.Key == query[0])
{
if (query.Length == 1)
{
yield return keyValue.Value;
}
else
{
if (isKVObject)
{
foreach (var subobj in keyValue.Value.Search(query[1], sub: true))
{
yield return subobj;
}
}
}
}
if (!sub && isKVObject)
{
foreach (var subobj in keyValue.Value.Search(fullquery))
{
yield return subobj;
}
}
}
}
}
}

104 changes: 104 additions & 0 deletions src/ValveKV/KVParser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
using System;
using System.Text;
using System.Linq;
using System.Drawing;
using System.Globalization;
using System.Collections.Generic;
using System.IO;



namespace VDFparse
{
public enum DataType
{
START = 0,
STRING,
INT,
FLOAT,
PTR,
WSTRING,
COLOR,
UINT64,
END,
};

public static class KVParser
{
public static KVObject Parse(Stream stream)
{
using (var reader = new BinaryReader(stream))
{
return Parse(reader);
}
}

public static KVObject Parse(BinaryReader reader)
{
KVObject root = new KVObject();
var dataStack = new List<KVObject>(){ root };
while (true)
{
var type = reader.ReadByte();
switch ((DataType)type)
{
case DataType.START:
var newObj = new KVObject();
dataStack.Last()[ReadString(reader)] = newObj;
dataStack.Add(newObj);
break;
case DataType.END:
dataStack.RemoveAt(dataStack.Count - 1);
if (dataStack.Count == 0)
return root;
break;
case DataType.STRING:
dataStack.Last()[ReadString(reader)] = ReadString(reader);
break;
case DataType.INT:
dataStack.Last()[ReadString(reader)] = reader.ReadInt32();
break;
case DataType.FLOAT:
dataStack.Last()[ReadString(reader)] = reader.ReadSingle();
break;
case DataType.PTR:
dataStack.Last()[ReadString(reader)] = reader.ReadUInt32();
break;
case DataType.WSTRING:
dataStack.Last()[ReadString(reader)] = ReadString(reader, width: 2);
break;
case DataType.COLOR:
dataStack.Last()[ReadString(reader)] = Color.FromArgb(reader.ReadInt32());
break;
case DataType.UINT64:
dataStack.Last()[ReadString(reader)] = reader.ReadUInt64();
break;
}
}
}

// TODO: Correct this to produce correct wide string results
private static string ReadString(BinaryReader reader, byte width=1)
{
StringBuilder builder = new StringBuilder();
byte[] current = new byte[width];

while (true)
{
for (int i = 0; i < width ; ++i)
{
current[i] = reader.ReadByte();
}
if (current.All(val => val == 0))
{
break;
}
foreach (var c in current)
{
builder.Append((char)c);
}
}
return builder.ToString();
}
}
}
11 changes: 11 additions & 0 deletions src/cli/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Reflection;



[assembly: AssemblyProduct("VDFparse")]
[assembly: AssemblyDescription("Parses and displays info about Steam .vdf files")]
[assembly: AssemblyCopyright("© Grub4K 2021")]

[assembly: AssemblyVersion("2.1")]
[assembly: AssemblyInformationalVersion("2.1")]
[assembly: AssemblyFileVersion("2.1.3.0")]
Loading

0 comments on commit b4fae37

Please sign in to comment.