Skip to content

Commit e9adf5b

Browse files
committed
Remove redundant code
1 parent 239d314 commit e9adf5b

File tree

9 files changed

+106
-119
lines changed

9 files changed

+106
-119
lines changed

DepotDownloader/AccountSettingsStore.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
using System;
2+
using System.Collections.Concurrent;
23
using System.Collections.Generic;
3-
using ProtoBuf;
44
using System.IO;
55
using System.IO.Compression;
66
using System.IO.IsolatedStorage;
7-
using System.Linq;
8-
using SteamKit2;
9-
using SteamKit2.Discovery;
7+
using ProtoBuf;
108

119
namespace DepotDownloader
1210
{
@@ -17,17 +15,17 @@ [ ProtoMember( 1, IsRequired = false ) ]
1715
public Dictionary<string, byte[]> SentryData { get; private set; }
1816

1917
[ ProtoMember( 2, IsRequired = false ) ]
20-
public System.Collections.Concurrent.ConcurrentDictionary<string, int> ContentServerPenalty { get; private set; }
18+
public ConcurrentDictionary<string, int> ContentServerPenalty { get; private set; }
2119

2220
[ ProtoMember( 3, IsRequired = false ) ]
2321
public Dictionary<string, string> LoginKeys { get; private set; }
2422

25-
string FileName = null;
23+
string FileName;
2624

2725
AccountSettingsStore()
2826
{
2927
SentryData = new Dictionary<string, byte[]>();
30-
ContentServerPenalty = new System.Collections.Concurrent.ConcurrentDictionary<string, int>();
28+
ContentServerPenalty = new ConcurrentDictionary<string, int>();
3129
LoginKeys = new Dictionary<string, string>();
3230
}
3331

@@ -36,7 +34,7 @@ static bool Loaded
3634
get { return Instance != null; }
3735
}
3836

39-
public static AccountSettingsStore Instance = null;
37+
public static AccountSettingsStore Instance;
4038
static readonly IsolatedStorageFile IsolatedStorage = IsolatedStorageFile.GetUserStoreForAssembly();
4139

4240
public static void LoadFromFile( string filename )
@@ -51,7 +49,7 @@ public static void LoadFromFile( string filename )
5149
using ( var fs = IsolatedStorage.OpenFile( filename, FileMode.Open, FileAccess.Read ) )
5250
using ( DeflateStream ds = new DeflateStream( fs, CompressionMode.Decompress ) )
5351
{
54-
Instance = ProtoBuf.Serializer.Deserialize<AccountSettingsStore>( ds );
52+
Instance = Serializer.Deserialize<AccountSettingsStore>( ds );
5553
}
5654
}
5755
catch ( IOException ex )
@@ -78,7 +76,7 @@ public static void Save()
7876
using ( var fs = IsolatedStorage.OpenFile( Instance.FileName, FileMode.Create, FileAccess.Write ) )
7977
using ( DeflateStream ds = new DeflateStream( fs, CompressionMode.Compress ) )
8078
{
81-
ProtoBuf.Serializer.Serialize<AccountSettingsStore>( ds, Instance );
79+
Serializer.Serialize( ds, Instance );
8280
}
8381
}
8482
catch ( IOException ex )

DepotDownloader/CDNClientPool.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
using SteamKit2;
2-
using System;
1+
using System;
32
using System.Collections.Concurrent;
43
using System.Collections.Generic;
54
using System.Linq;
65
using System.Net;
76
using System.Threading;
87
using System.Threading.Tasks;
8+
using SteamKit2;
99

1010
namespace DepotDownloader
1111
{
@@ -165,10 +165,8 @@ public async Task<string> AuthenticateConnection( uint appId, uint depotId, CDNC
165165
var result = await authTokenCallbackPromise.Task;
166166
return result.Token;
167167
}
168-
else
169-
{
170-
throw new Exception( $"Failed to retrieve CDN token for server {server.Host} depot {depotId}" );
171-
}
168+
169+
throw new Exception( $"Failed to retrieve CDN token for server {server.Host} depot {depotId}" );
172170
}
173171

174172
public void ReturnConnection( CDNClient.Server server )

DepotDownloader/ContentDownloader.cs

Lines changed: 44 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
1-
using SteamKit2;
2-
using System;
1+
using System;
32
using System.Collections.Concurrent;
43
using System.Collections.Generic;
54
using System.IO;
65
using System.Linq;
76
using System.Net;
8-
using System.Net.Http;
9-
using System.Text;
107
using System.Text.RegularExpressions;
118
using System.Threading;
129
using System.Threading.Tasks;
10+
using SteamKit2;
1311

1412
namespace DepotDownloader
1513
{
16-
public class ContentDownloaderException : System.Exception
14+
public class ContentDownloaderException : Exception
1715
{
1816
public ContentDownloaderException( String value ) : base( value ) { }
1917
}
@@ -58,7 +56,7 @@ static bool CreateDirectories( uint depotId, uint depotVersion, out string insta
5856
installDir = null;
5957
try
6058
{
61-
if ( string.IsNullOrWhiteSpace( ContentDownloader.Config.InstallDirectory ) )
59+
if ( string.IsNullOrWhiteSpace( Config.InstallDirectory ) )
6260
{
6361
Directory.CreateDirectory( DEFAULT_DOWNLOAD_DIR );
6462

@@ -73,9 +71,9 @@ static bool CreateDirectories( uint depotId, uint depotVersion, out string insta
7371
}
7472
else
7573
{
76-
Directory.CreateDirectory( ContentDownloader.Config.InstallDirectory );
74+
Directory.CreateDirectory( Config.InstallDirectory );
7775

78-
installDir = ContentDownloader.Config.InstallDirectory;
76+
installDir = Config.InstallDirectory;
7977

8078
Directory.CreateDirectory( Path.Combine( installDir, CONFIG_DIR ) );
8179
Directory.CreateDirectory( Path.Combine( installDir, STAGING_DIR ) );
@@ -120,7 +118,7 @@ static bool AccountHasAccess( uint depotId )
120118
IEnumerable<uint> licenseQuery;
121119
if ( steam3.steamUser.SteamID.AccountType == EAccountType.AnonUser )
122120
{
123-
licenseQuery = new List<uint>() { 17906 };
121+
licenseQuery = new List<uint> { 17906 };
124122
}
125123
else
126124
{
@@ -189,7 +187,7 @@ static uint GetSteam3AppBuildNumber( uint appId, string branch )
189187
return 0;
190188

191189

192-
KeyValue depots = ContentDownloader.GetSteam3AppSection( appId, EAppInfoSection.Depots );
190+
KeyValue depots = GetSteam3AppSection( appId, EAppInfoSection.Depots );
193191
KeyValue branches = depots[ "branches" ];
194192
KeyValue node = branches[ branch ];
195193

@@ -267,7 +265,8 @@ static ulong GetSteam3DepotManifest( uint depotId, uint appId, string branch )
267265

268266
return BitConverter.ToUInt64( manifest_bytes, 0 );
269267
}
270-
else if ( encrypted_v2 != KeyValue.Invalid )
268+
269+
if ( encrypted_v2 != KeyValue.Invalid )
271270
{
272271
// Submit the password to Steam now to get encryption keys
273272
steam3.CheckAppBetaPassword( appId, Config.BetaPassword );
@@ -292,11 +291,9 @@ static ulong GetSteam3DepotManifest( uint depotId, uint appId, string branch )
292291

293292
return BitConverter.ToUInt64( manifest_bytes, 0 );
294293
}
295-
else
296-
{
297-
Console.WriteLine( "Unhandled depot encryption for depotId {0}", depotId );
298-
return INVALID_MANIFEST_ID;
299-
}
294+
295+
Console.WriteLine( "Unhandled depot encryption for depotId {0}", depotId );
296+
return INVALID_MANIFEST_ID;
300297
}
301298

302299
return INVALID_MANIFEST_ID;
@@ -319,20 +316,18 @@ static string GetAppOrDepotName( uint depotId, uint appId )
319316

320317
return info[ "name" ].AsString();
321318
}
322-
else
323-
{
324-
KeyValue depots = GetSteam3AppSection( appId, EAppInfoSection.Depots );
325319

326-
if ( depots == null )
327-
return String.Empty;
320+
KeyValue depots = GetSteam3AppSection( appId, EAppInfoSection.Depots );
328321

329-
KeyValue depotChild = depots[ depotId.ToString() ];
322+
if ( depots == null )
323+
return String.Empty;
330324

331-
if ( depotChild == null )
332-
return String.Empty;
325+
KeyValue depotChild = depots[ depotId.ToString() ];
333326

334-
return depotChild[ "name" ].AsString();
335-
}
327+
if ( depotChild == null )
328+
return String.Empty;
329+
330+
return depotChild[ "name" ].AsString();
336331
}
337332

338333
public static bool InitializeSteam3( string username, string password )
@@ -345,7 +340,7 @@ public static bool InitializeSteam3( string username, string password )
345340
}
346341

347342
steam3 = new Steam3Session(
348-
new SteamUser.LogOnDetails()
343+
new SteamUser.LogOnDetails
349344
{
350345
Username = username,
351346
Password = loginKey == null ? password : null,
@@ -391,7 +386,7 @@ public static async Task DownloadPubfileAsync( uint appId, ulong publishedFileId
391386
}
392387
else if ( details?.hcontent_file > 0 )
393388
{
394-
await DownloadAppAsync( appId, new List<(uint, ulong)>() { ( appId, details.hcontent_file ) }, DEFAULT_BRANCH, null, null, null, false, true );
389+
await DownloadAppAsync( appId, new List<(uint, ulong)> { ( appId, details.hcontent_file ) }, DEFAULT_BRANCH, null, null, null, false, true );
395390
}
396391
else
397392
{
@@ -418,7 +413,7 @@ public static async Task DownloadUGCAsync( uint appId, ulong ugcId )
418413
}
419414
else
420415
{
421-
await DownloadAppAsync( appId, new List<(uint, ulong)>() { ( appId, ugcId ) }, DEFAULT_BRANCH, null, null, null, false, true );
416+
await DownloadAppAsync( appId, new List<(uint, ulong)> { ( appId, ugcId ) }, DEFAULT_BRANCH, null, null, null, false, true );
422417
}
423418
}
424419

@@ -459,7 +454,7 @@ public static async Task DownloadAppAsync( uint appId, List<(uint depotId, ulong
459454
cdnPool = new CDNClientPool( steam3, appId );
460455

461456
// Load our configuration data containing the depots currently installed
462-
string configPath = ContentDownloader.Config.InstallDirectory;
457+
string configPath = Config.InstallDirectory;
463458
if ( string.IsNullOrWhiteSpace( configPath ) )
464459
{
465460
configPath = DEFAULT_DOWNLOAD_DIR;
@@ -562,15 +557,16 @@ public static async Task DownloadAppAsync( uint appId, List<(uint depotId, ulong
562557
depotIdsFound.Add( id );
563558

564559
if ( !hasSpecificDepots )
565-
depotManifestIds.Add( ( id, ContentDownloader.INVALID_MANIFEST_ID ) );
560+
depotManifestIds.Add( ( id, INVALID_MANIFEST_ID ) );
566561
}
567562
}
568563

569564
if ( depotManifestIds.Count == 0 && !hasSpecificDepots )
570565
{
571566
throw new ContentDownloaderException( String.Format( "Couldn't find any depots to download for app {0}", appId ) );
572567
}
573-
else if ( depotIdsFound.Count < depotIdsExpected.Count )
568+
569+
if ( depotIdsFound.Count < depotIdsExpected.Count )
574570
{
575571
var remainingDepotIds = depotIdsExpected.Except( depotIdsFound );
576572
throw new ContentDownloaderException( String.Format( "Depot {0} not listed for app {1}", string.Join( ", ", remainingDepotIds ), appId ) );
@@ -602,7 +598,7 @@ public static async Task DownloadAppAsync( uint appId, List<(uint depotId, ulong
602598
static DepotDownloadInfo GetDepotInfo( uint depotId, uint appId, ulong manifestId, string branch )
603599
{
604600
if ( steam3 != null && appId != INVALID_APP_ID )
605-
steam3.RequestAppInfo( ( uint )appId );
601+
steam3.RequestAppInfo( appId );
606602

607603
string contentName = GetAppOrDepotName( depotId, appId );
608604

@@ -722,7 +718,7 @@ private static async Task DownloadSteam3Async( uint appId, List<DepotDownloadInf
722718

723719
// If we're about to write all the files to the same directory, we will need to first de-duplicate any files by path
724720
// This is in last-depot-wins order, from Steam or the list of depots supplied by the user
725-
if ( !string.IsNullOrWhiteSpace( ContentDownloader.Config.InstallDirectory ) && depotsToDownload.Count > 0 )
721+
if ( !string.IsNullOrWhiteSpace( Config.InstallDirectory ) && depotsToDownload.Count > 0 )
726722
{
727723
var claimedFileNames = new HashSet<String>();
728724

@@ -863,15 +859,14 @@ private static async Task<DepotFilesData> ProcessDepotManifestAndFiles( Cancella
863859
Console.WriteLine( "Encountered 401 for depot manifest {0} {1}. Aborting.", depot.id, depot.manifestId );
864860
break;
865861
}
866-
else if ( e.StatusCode == HttpStatusCode.NotFound )
862+
863+
if ( e.StatusCode == HttpStatusCode.NotFound )
867864
{
868865
Console.WriteLine( "Encountered 404 for depot manifest {0} {1}. Aborting.", depot.id, depot.manifestId );
869866
break;
870867
}
871-
else
872-
{
873-
Console.WriteLine( "Encountered error downloading depot manifest {0} {1}: {2}", depot.id, depot.manifestId, e.StatusCode );
874-
}
868+
869+
Console.WriteLine( "Encountered error downloading depot manifest {0} {1}: {2}", depot.id, depot.manifestId, e.StatusCode );
875870
}
876871
catch ( OperationCanceledException )
877872
{
@@ -983,7 +978,7 @@ await Task.Run( () => DownloadSteam3AsyncDepotFileChunk( cts, appId, downloadCou
983978
var previousFilteredFiles = depotFilesData.previousManifest.Files.AsParallel().Where( f => TestIsFileIncluded( f.FileName ) ).Select( f => f.FileName ).ToHashSet();
984979

985980
// Check if we are writing to a single output directory. If not, each depot folder is managed independently
986-
if ( string.IsNullOrWhiteSpace( ContentDownloader.Config.InstallDirectory ) )
981+
if ( string.IsNullOrWhiteSpace( Config.InstallDirectory ) )
987982
{
988983
// Of the list of files in the previous manifest, remove any file names that exist in the current set of all file names
989984
previousFilteredFiles.ExceptWith( depotFilesData.allFileNames );
@@ -1173,21 +1168,19 @@ private static void DownloadSteam3AsyncDepotFile(
11731168
{
11741169
lock ( depotDownloadCounter )
11751170
{
1176-
depotDownloadCounter.SizeDownloaded += ( ulong )file.TotalSize;
1177-
Console.WriteLine( "{0,6:#00.00}% {1}", ( ( float )depotDownloadCounter.SizeDownloaded / ( float )depotDownloadCounter.CompleteDownloadSize ) * 100.0f, fileFinalPath );
1171+
depotDownloadCounter.SizeDownloaded += file.TotalSize;
1172+
Console.WriteLine( "{0,6:#00.00}% {1}", ( depotDownloadCounter.SizeDownloaded / ( float )depotDownloadCounter.CompleteDownloadSize ) * 100.0f, fileFinalPath );
11781173
}
11791174

11801175
if ( fs != null )
11811176
fs.Dispose();
11821177
return;
11831178
}
1184-
else
1179+
1180+
var sizeOnDisk = ( file.TotalSize - ( ulong )neededChunks.Select( x => ( long )x.UncompressedLength ).Sum() );
1181+
lock ( depotDownloadCounter )
11851182
{
1186-
var sizeOnDisk = ( file.TotalSize - ( ulong )neededChunks.Select( x => ( long )x.UncompressedLength ).Sum() );
1187-
lock ( depotDownloadCounter )
1188-
{
1189-
depotDownloadCounter.SizeDownloaded += sizeOnDisk;
1190-
}
1183+
depotDownloadCounter.SizeDownloaded += sizeOnDisk;
11911184
}
11921185
}
11931186

@@ -1260,10 +1253,8 @@ private static async Task DownloadSteam3AsyncDepotFileChunk(
12601253
Console.WriteLine( "Encountered 401 for chunk {0}. Aborting.", chunkID );
12611254
break;
12621255
}
1263-
else
1264-
{
1265-
Console.WriteLine( "Encountered error downloading chunk {0}: {1}", chunkID, e.StatusCode );
1266-
}
1256+
1257+
Console.WriteLine( "Encountered error downloading chunk {0}: {1}", chunkID, e.StatusCode );
12671258
}
12681259
catch ( OperationCanceledException )
12691260
{
@@ -1322,7 +1313,7 @@ private static async Task DownloadSteam3AsyncDepotFileChunk(
13221313
if ( remainingChunks == 0 )
13231314
{
13241315
var fileFinalPath = Path.Combine( depot.installDir, file.FileName );
1325-
Console.WriteLine( "{0,6:#00.00}% {1}", ( ( float )sizeDownloaded / ( float )depotDownloadCounter.CompleteDownloadSize ) * 100.0f, fileFinalPath );
1316+
Console.WriteLine( "{0,6:#00.00}% {1}", ( sizeDownloaded / ( float )depotDownloadCounter.CompleteDownloadSize ) * 100.0f, fileFinalPath );
13261317
}
13271318
}
13281319

0 commit comments

Comments
 (0)