From e96e8bae516a95f346269d545ec0719236f50766 Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Wed, 18 Sep 2013 22:28:16 -0400 Subject: [PATCH] Use console instance when possible. (now caches user sentry data and steam id to config.vdf). --- DepotDownloader/Steam3Session.cs | 140 ++++++++++++++++++++++++++++--- 1 file changed, 130 insertions(+), 10 deletions(-) diff --git a/DepotDownloader/Steam3Session.cs b/DepotDownloader/Steam3Session.cs index ea4abae7..e87979cc 100644 --- a/DepotDownloader/Steam3Session.cs +++ b/DepotDownloader/Steam3Session.cs @@ -23,6 +23,116 @@ namespace DepotDownloader } } + private class Config + { + private const string filename = "config.vdf"; + public void Init(string username) + { + this.username = username; + + if (File.Exists(filename)) + { + var existingConf = new KeyValue("DepotDownloader"); + existingConf.ReadFileAsText("config.vdf"); + + if (existingConf[username] != KeyValue.Invalid) + { + userConf = existingConf[username]; + } + } + + if (userConf == null) + { + userConf = new KeyValue(username); + } + } + + public byte[] SentryData + { + get + { + if (userConf["sentry"] != KeyValue.Invalid) + { + return Util.SHAHash(Util.DecodeHexString(userConf["sentry"].AsString())); + } + else + { + FileInfo fi = new FileInfo(String.Format("{0}.sentryFile", username)); + if (fi.Exists && fi.Length > 0) + { + // Support old sentryFile files if existing + var sentryData = File.ReadAllBytes(fi.FullName); + var sentryKey = userConf["sentry"]; + if (sentryKey == KeyValue.Invalid) + { + sentryKey = new KeyValue("sentry"); + userConf.Children.Add(sentryKey); + } + sentryKey.Value = Util.EncodeHexString(sentryData); + return Util.SHAHash(sentryData); + } + } + + return null; + } + set + { + var sentryKey = userConf["sentry"]; + if (sentryKey == KeyValue.Invalid) + { + sentryKey = new KeyValue("sentry"); + userConf.Children.Add(sentryKey); + } + + sentryKey.Value = Util.EncodeHexString(value); + } + } + + public SteamID SID + { + get + { + if (userConf["steamid"] != KeyValue.Invalid) + { + return new SteamID(ulong.Parse(userConf["steamid"].AsString())).AccountID; + } + + return new SteamID(); + } + set + { + var steamIdKey = userConf["steamid"]; + if (steamIdKey == KeyValue.Invalid) + { + steamIdKey = new KeyValue("steamid"); + userConf.Children.Add(steamIdKey); + } + + steamIdKey.Value = value.ConvertToUInt64().ToString(); + } + } + + public void Save() + { + KeyValue conf = new KeyValue("DepotDownloader"); + if (File.Exists(filename)) + { + conf.ReadFileAsText(filename); + } + + conf.Children.RemoveAll(c => c.Name == username); + + conf.Children.Add(userConf); + + conf.SaveToFile(filename, false); + } + + KeyValue userConf = null; + string username; + } + + Config config = new Config(); + public ReadOnlyCollection Licenses { get; @@ -54,7 +164,6 @@ namespace DepotDownloader static readonly TimeSpan STEAM3_TIMEOUT = TimeSpan.FromSeconds( 30 ); - public Steam3Session( SteamUser.LogOnDetails details ) { this.logonDetails = details; @@ -86,13 +195,11 @@ namespace DepotDownloader Console.Write( "Connecting to Steam3..." ); - if ( authenticatedUser ) + config.Init(logonDetails.Username); + + if ( !string.IsNullOrWhiteSpace( logonDetails.Username ) ) { - FileInfo fi = new FileInfo(String.Format("{0}.sentryFile", logonDetails.Username)); - if (fi.Exists && fi.Length > 0) - { - logonDetails.SentryFileHash = Util.SHAHash(File.ReadAllBytes(fi.FullName)); - } + logonDetails.SentryFileHash = config.SentryData; } Connect(); @@ -294,6 +401,8 @@ namespace DepotDownloader steamClient.Disconnect(); bConnected = false; bAborted = true; + + config.Save(); } @@ -323,6 +432,13 @@ namespace DepotDownloader } else { + var sid = config.SID; + if (sid.IsValid) + { + logonDetails.AccountInstance = SteamID.ConsoleInstance; + logonDetails.AccountID = sid.AccountID; + + } Console.Write( "Logging '{0}' into Steam3...", logonDetails.Username ); steamUser.LogOn( logonDetails ); } @@ -387,6 +503,11 @@ namespace DepotDownloader credentials.LoggedOn = true; + if (!string.IsNullOrWhiteSpace(logonDetails.Username)) + { + config.SID = loggedOn.ClientSteamID; + } + if (ContentDownloader.Config.CellID == 0) { Console.WriteLine("Using Steam3 suggested CellID: " + loggedOn.CellID); @@ -426,7 +547,8 @@ namespace DepotDownloader byte[] hash = Util.SHAHash(machineAuth.Data); Console.WriteLine("Got Machine Auth: {0} {1} {2} {3}", machineAuth.FileName, machineAuth.Offset, machineAuth.BytesToWrite, machineAuth.Data.Length, hash); - File.WriteAllBytes( String.Format("{0}.sentryFile", logonDetails.Username), machineAuth.Data ); + config.SentryData = machineAuth.Data; + var authResponse = new SteamUser.MachineAuthDetails { BytesWritten = machineAuth.BytesToWrite, @@ -447,7 +569,5 @@ namespace DepotDownloader // send off our response steamUser.SendMachineAuthResponse( authResponse ); } - - } }