From 62149aedc8541ef8debdb0b119919b8dfd3792b4 Mon Sep 17 00:00:00 2001 From: Daniel Brunner <0xFEEDC0DE64@gmail.com> Date: Sat, 4 Feb 2017 14:52:20 +0100 Subject: [PATCH] Implemented parsing of steam files --- SteamMover/Form1.cs | 64 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/SteamMover/Form1.cs b/SteamMover/Form1.cs index 9948a10..d87159c 100644 --- a/SteamMover/Form1.cs +++ b/SteamMover/Form1.cs @@ -3,8 +3,10 @@ using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; +using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Windows.Forms; @@ -12,9 +14,71 @@ namespace SteamMover { public partial class Form1 : Form { + private static readonly Regex regexObject = new Regex("^(?: |\t)*\"([^\"]*)\"$"); + private static readonly Regex regexObjectBegin = new Regex("^(?: |\t)*\\{$"); + private static readonly Regex regexObjectEnd = new Regex("^(?: |\t)*\\}$"); + private static readonly Regex regexVariable = new Regex("^(?: |\t)*\"([^\"]*)\"(?: |\t)*\"([^\"]*)\"$"); + public Form1() { InitializeComponent(); + + foreach (var path in Directory.GetFiles(@"C:\Program Files (x86)\Steam\steamapps\", "*.acf", SearchOption.AllDirectories)) + ReadConfigObject(File.OpenText(path)); + foreach (var path in Directory.GetFiles(@"D:\SteamLibrary\steamapps", "*.acf", SearchOption.AllDirectories)) + ReadConfigObject(File.OpenText(path)); + foreach (var path in Directory.GetFiles(@"F:\SteamLibrary\steamapps", "*.acf", SearchOption.AllDirectories)) + ReadConfigObject(File.OpenText(path)); + } + + private Dictionary ReadConfigObject(StreamReader streamReader, bool nestedObject = false) + { + var dictionary = new Dictionary(); + + while (true) + { + var line = streamReader.ReadLine(); + + if (line == null) + { + if (nestedObject) + throw new Exception("Unexpected end of file"); + else + break; + } + + Match match; + if ((match = regexObject.Match(line)).Success) + { + var key = match.Groups[1].Value; + + line = streamReader.ReadLine(); + if (!regexObjectBegin.IsMatch(line)) + throw new Exception("Object doesnt start"); + + var value = ReadConfigObject(streamReader, true); + + dictionary.Add(key, value); + } + else if ((match = regexVariable.Match(line)).Success) + { + var key = match.Groups[1].Value; + var value = match.Groups[2].Value; + + dictionary.Add(key, value); + } + else if (regexObjectEnd.IsMatch(line)) + { + if (nestedObject) + break; + else + throw new Exception("Unexpected object closing"); + } + else + throw new Exception("Unexpected line"); + } + + return dictionary; } } }