mirror of
https://github.com/SteamRE/DepotDownloader.git
synced 2026-10-20 01:46:17 +00:00
Refactored DepotDownloader to use CallbackManager
This commit is contained in:
1 parent
6c00c19300
commit
5bc21f183a
2 files changed
+167
-148
No files matched your search
@@ -294,7 +294,7 @@ namespace DepotDownloader
|
||||
|
||||
steam3Credentials = steam3.WaitForCredentials();
|
||||
|
||||
if (!steam3Credentials.HasSessionToken)
|
||||
if (!steam3Credentials.IsValid)
|
||||
{
|
||||
Console.WriteLine("Unable to get steam3 credentials.");
|
||||
return;
|
||||
@@ -311,7 +311,7 @@ namespace DepotDownloader
|
||||
|
||||
private static ContentServerClient.Credentials GetSteam2Credentials(uint appId)
|
||||
{
|
||||
if (steam3 == null || !steam3Credentials.HasSessionToken)
|
||||
if (steam3 == null || !steam3Credentials.IsValid)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
@@ -443,7 +443,7 @@ namespace DepotDownloader
|
||||
List<IPEndPoint> serverList = steam3.steamClient.GetServersOfType(EServerType.CS);
|
||||
|
||||
List<CDNClient.ClientEndPoint> cdnServers = null;
|
||||
int tries = 0, counterDeferred = 0, counterCDN = 0;
|
||||
int tries = 0, counterDeferred = 0;
|
||||
|
||||
for(int i = 0; ; i++ )
|
||||
{
|
||||
@@ -452,9 +452,8 @@ namespace DepotDownloader
|
||||
cdnServers = CDNClient.FetchServerList(new CDNClient.ClientEndPoint(endpoint.Address.ToString(), endpoint.Port), Config.CellID);
|
||||
|
||||
if (cdnServers == null) counterDeferred++;
|
||||
else if (cdnServers.Count == 0) counterCDN++;
|
||||
|
||||
if (cdnServers != null && cdnServers.Count > 0)
|
||||
if (cdnServers != null && cdnServers.Count((ep) => { return ep.Type == "CS"; }) > 0)
|
||||
break;
|
||||
|
||||
if (((i+1) % serverList.Count) == 0)
|
||||
@@ -465,9 +464,8 @@ namespace DepotDownloader
|
||||
return;
|
||||
}
|
||||
|
||||
Console.Write("\nSearching for content servers... (deferred: {0}, CDN: {1})", counterDeferred, counterCDN);
|
||||
Console.Write("\nSearching for content servers... (deferred: {0})", counterDeferred);
|
||||
counterDeferred = 0;
|
||||
counterCDN = 0;
|
||||
Thread.Sleep(1000);
|
||||
}
|
||||
}
|
||||
@@ -481,11 +479,13 @@ namespace DepotDownloader
|
||||
Console.WriteLine(" Done!");
|
||||
Console.Write("Downloading depot manifest...");
|
||||
|
||||
List<CDNClient.ClientEndPoint> cdnEndpoints = cdnServers.Where((ep) => { return ep.Type == "CDN"; }).ToList();
|
||||
List<CDNClient.ClientEndPoint> csEndpoints = cdnServers.Where((ep) => { return ep.Type == "CS"; }).ToList();
|
||||
CDNClient cdnClient = null;
|
||||
|
||||
foreach (var server in cdnServers)
|
||||
foreach (var server in csEndpoints)
|
||||
{
|
||||
CDNClient client = new CDNClient(cdnServers[0], steam3.AppTickets[(uint)depotId]);
|
||||
CDNClient client = new CDNClient(server, steam3.AppTickets[(uint)depotId]);
|
||||
|
||||
if (client.Connect())
|
||||
{
|
||||
|
||||
+158
-139
@@ -13,9 +13,13 @@ namespace DepotDownloader
|
||||
{
|
||||
public class Credentials
|
||||
{
|
||||
public bool HasSessionToken { get; set; }
|
||||
public ulong SessionToken { get; set; }
|
||||
public byte[] Steam2Ticket { get; set; }
|
||||
|
||||
public bool IsValid
|
||||
{
|
||||
get { return SessionToken > 0 && Steam2Ticket != null; }
|
||||
}
|
||||
}
|
||||
|
||||
public ReadOnlyCollection<SteamApps.LicenseListCallback.License> Licenses
|
||||
@@ -34,6 +38,8 @@ namespace DepotDownloader
|
||||
SteamUser steamUser;
|
||||
SteamApps steamApps;
|
||||
|
||||
CallbackManager callbacks;
|
||||
|
||||
bool bConnected;
|
||||
bool bAborted;
|
||||
DateTime connectTime;
|
||||
@@ -65,6 +71,13 @@ namespace DepotDownloader
|
||||
this.steamUser = this.steamClient.GetHandler<SteamUser>();
|
||||
this.steamApps = this.steamClient.GetHandler<SteamApps>();
|
||||
|
||||
this.callbacks = new CallbackManager(this.steamClient);
|
||||
|
||||
this.callbacks.Register(new Callback<SteamClient.ConnectedCallback>(ConnectedCallback));
|
||||
this.callbacks.Register(new Callback<SteamUser.LoggedOnCallback>(LogOnCallback));
|
||||
this.callbacks.Register(new Callback<SteamUser.SessionTokenCallback>(SessionTokenCallback));
|
||||
this.callbacks.Register(new Callback<SteamApps.LicenseListCallback>(LicenseListCallback));
|
||||
|
||||
Console.Write( "Connecting to Steam3..." );
|
||||
|
||||
Connect();
|
||||
@@ -72,55 +85,106 @@ namespace DepotDownloader
|
||||
|
||||
public Credentials WaitForCredentials()
|
||||
{
|
||||
if (credentials.IsValid || bAborted)
|
||||
return credentials;
|
||||
|
||||
do
|
||||
{
|
||||
HandleCallbacks();
|
||||
WaitForCallbacks();
|
||||
}
|
||||
while (!bAborted && (credentials.SessionToken == 0 || credentials.Steam2Ticket == null));
|
||||
while (!bAborted && !credentials.IsValid);
|
||||
|
||||
return credentials;
|
||||
}
|
||||
|
||||
public void RequestAppInfo(uint appId)
|
||||
{
|
||||
if (bAborted || AppInfo.ContainsKey(appId))
|
||||
if (AppInfo.ContainsKey(appId) || bAborted)
|
||||
return;
|
||||
|
||||
steamApps.GetAppInfo( new uint[] { appId } );
|
||||
|
||||
do
|
||||
Action<SteamApps.AppInfoCallback> cbMethod = appInfo =>
|
||||
{
|
||||
HandleCallbacks();
|
||||
foreach (var app in appInfo.Apps)
|
||||
{
|
||||
Console.WriteLine("Got AppInfo for {0}: {1}", app.AppID, app.Status);
|
||||
AppInfo.Add(app.AppID, app);
|
||||
|
||||
KeyValue depots;
|
||||
if (app.Sections.TryGetValue(EAppInfoSection.Depots, out depots))
|
||||
{
|
||||
if (depots[app.AppID.ToString()]["OverridesCDDB"].AsBoolean(false))
|
||||
{
|
||||
AppInfoOverridesCDR[app.AppID] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
using (JobCallback<SteamApps.AppInfoCallback> appInfoCallback = new JobCallback<SteamApps.AppInfoCallback>(steamApps.GetAppInfo(appId), cbMethod, callbacks))
|
||||
{
|
||||
do
|
||||
{
|
||||
WaitForCallbacks();
|
||||
}
|
||||
while (!appInfoCallback.Completed && !bAborted);
|
||||
}
|
||||
while (!bAborted && !AppInfo.ContainsKey(appId));
|
||||
}
|
||||
|
||||
public void RequestAppTicket(uint appId)
|
||||
{
|
||||
if (bAborted || AppTickets.ContainsKey(appId))
|
||||
if (AppTickets.ContainsKey(appId) || bAborted)
|
||||
return;
|
||||
|
||||
steamApps.GetAppOwnershipTicket(appId);
|
||||
|
||||
do
|
||||
Action<SteamApps.AppOwnershipTicketCallback> cbMethod = appTicket =>
|
||||
{
|
||||
HandleCallbacks();
|
||||
if (appTicket.Result != EResult.OK)
|
||||
{
|
||||
Console.WriteLine("Unable to get appticket for {0}: {1}", appTicket.AppID, appTicket.Result);
|
||||
Abort();
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Got appticket for {0}!", appTicket.AppID);
|
||||
AppTickets[appTicket.AppID] = appTicket.Ticket;
|
||||
}
|
||||
};
|
||||
|
||||
using (JobCallback<SteamApps.AppOwnershipTicketCallback> appTicketCallback = new JobCallback<SteamApps.AppOwnershipTicketCallback>(steamApps.GetAppOwnershipTicket(appId), cbMethod, callbacks))
|
||||
{
|
||||
do
|
||||
{
|
||||
WaitForCallbacks();
|
||||
}
|
||||
while (!appTicketCallback.Completed && !bAborted);
|
||||
}
|
||||
while (!bAborted && !AppTickets.ContainsKey(appId));
|
||||
}
|
||||
|
||||
public void RequestDepotKey(uint depotId)
|
||||
{
|
||||
if (bAborted || DepotKeys.ContainsKey(depotId))
|
||||
if (DepotKeys.ContainsKey(depotId) || bAborted)
|
||||
return;
|
||||
|
||||
steamApps.GetDepotDecryptionKey(depotId);
|
||||
|
||||
do
|
||||
Action<SteamApps.DepotKeyCallback> cbMethod = depotKey =>
|
||||
{
|
||||
HandleCallbacks();
|
||||
Console.WriteLine("Got depot key for {0} result: {1}", depotKey.DepotID, depotKey.Result);
|
||||
|
||||
if (depotKey.Result != EResult.OK)
|
||||
{
|
||||
Abort();
|
||||
return;
|
||||
}
|
||||
|
||||
DepotKeys[depotKey.DepotID] = depotKey.DepotKey;
|
||||
};
|
||||
|
||||
using (JobCallback<SteamApps.DepotKeyCallback> depotKeyCallback = new JobCallback<SteamApps.DepotKeyCallback>(steamApps.GetDepotDecryptionKey(depotId), cbMethod, callbacks))
|
||||
{
|
||||
do
|
||||
{
|
||||
WaitForCallbacks();
|
||||
}
|
||||
while (!depotKeyCallback.Completed && !bAborted);
|
||||
}
|
||||
while (!bAborted && !DepotKeys.ContainsKey(depotId));
|
||||
}
|
||||
|
||||
void Connect()
|
||||
@@ -142,129 +206,84 @@ namespace DepotDownloader
|
||||
bConnected = false;
|
||||
}
|
||||
|
||||
void HandleCallbacks()
|
||||
|
||||
private void WaitForCallbacks()
|
||||
{
|
||||
while ( true )
|
||||
callbacks.RunWaitCallbacks( TimeSpan.FromSeconds(1) );
|
||||
|
||||
TimeSpan diff = DateTime.Now - connectTime;
|
||||
|
||||
if (diff > STEAM3_TIMEOUT && !bConnected)
|
||||
{
|
||||
var callback = steamClient.WaitForCallback( true, TimeSpan.FromSeconds( 1 ) );
|
||||
Console.WriteLine("Timeout connecting to Steam3.");
|
||||
Abort();
|
||||
|
||||
TimeSpan diff = DateTime.Now - connectTime;
|
||||
|
||||
if (diff > STEAM3_TIMEOUT && !bConnected)
|
||||
{
|
||||
Abort();
|
||||
break;
|
||||
}
|
||||
|
||||
if ( callback == null )
|
||||
break;
|
||||
|
||||
if ( callback.IsType<SteamClient.ConnectedCallback>() )
|
||||
{
|
||||
Console.WriteLine( " Done!" );
|
||||
bConnected = true;
|
||||
steamUser.LogOn( logonDetails );
|
||||
|
||||
Console.Write( "Logging '{0}' into Steam3...", logonDetails.Username );
|
||||
}
|
||||
else if ( callback.IsType<SteamUser.LoggedOnCallback>() )
|
||||
{
|
||||
var msg = callback as SteamUser.LoggedOnCallback;
|
||||
|
||||
if ( msg.Result == EResult.AccountLogonDenied )
|
||||
{
|
||||
Console.WriteLine( "This account is protected by Steam Guard. Please enter the authentication code sent to your email address." );
|
||||
Console.Write( "Auth Code: " );
|
||||
|
||||
logonDetails.AuthCode = Console.ReadLine();
|
||||
|
||||
Console.Write( "Retrying Steam3 connection..." );
|
||||
Connect();
|
||||
continue;
|
||||
}
|
||||
else if ( msg.Result != EResult.OK )
|
||||
{
|
||||
Console.WriteLine( "Unable to login to Steam3: {0}", msg.Result );
|
||||
Abort();
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine( " Done!" );
|
||||
|
||||
Console.WriteLine( "Got Steam2 Ticket!" );
|
||||
credentials.Steam2Ticket = msg.Steam2Ticket;
|
||||
|
||||
if (ContentDownloader.Config.CellID == 0)
|
||||
{
|
||||
Console.WriteLine( "Using Steam3 suggest CellID: " + msg.CellID );
|
||||
ContentDownloader.Config.CellID = (int)msg.CellID;
|
||||
}
|
||||
}
|
||||
else if (callback.IsType<SteamApps.AppOwnershipTicketCallback>())
|
||||
{
|
||||
var msg = callback as SteamApps.AppOwnershipTicketCallback;
|
||||
|
||||
if ( msg.Result != EResult.OK )
|
||||
{
|
||||
Console.WriteLine( "Unable to get appticket for {0}: {1}", msg.AppID, msg.Result );
|
||||
Abort();
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine( "Got appticket for {0}!", msg.AppID );
|
||||
AppTickets[msg.AppID] = msg.Ticket;
|
||||
|
||||
}
|
||||
else if (callback.IsType<SteamUser.SessionTokenCallback>())
|
||||
{
|
||||
var msg = callback as SteamUser.SessionTokenCallback;
|
||||
|
||||
Console.WriteLine( "Got session token!" );
|
||||
credentials.SessionToken = msg.SessionToken;
|
||||
credentials.HasSessionToken = true;
|
||||
}
|
||||
else if (callback.IsType<SteamApps.LicenseListCallback>())
|
||||
{
|
||||
var msg = callback as SteamApps.LicenseListCallback;
|
||||
|
||||
if ( msg.Result != EResult.OK )
|
||||
{
|
||||
Console.WriteLine( "Unable to get license list: {0} ", msg.Result );
|
||||
Abort();
|
||||
break;
|
||||
}
|
||||
|
||||
Console.WriteLine( "Got {0} licenses for account!", msg.LicenseList.Count );
|
||||
Licenses = msg.LicenseList;
|
||||
}
|
||||
else if (callback.IsType<SteamClient.JobCallback<SteamApps.AppInfoCallback>>())
|
||||
{
|
||||
var msg = callback as SteamClient.JobCallback<SteamApps.AppInfoCallback>;
|
||||
|
||||
foreach (var app in msg.Callback.Apps)
|
||||
{
|
||||
Console.WriteLine("Got AppInfo for {0}: {1}", app.AppID, app.Status);
|
||||
AppInfo.Add(app.AppID, app);
|
||||
|
||||
KeyValue depots;
|
||||
if ( app.Sections.TryGetValue( EAppInfoSection.Depots, out depots ) )
|
||||
{
|
||||
if ( depots[ app.AppID.ToString() ][ "OverridesCDDB" ].AsBoolean( false ) )
|
||||
{
|
||||
AppInfoOverridesCDR[ app.AppID ] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (callback.IsType<SteamApps.DepotKeyCallback>())
|
||||
{
|
||||
var msg = callback as SteamApps.DepotKeyCallback;
|
||||
|
||||
Console.WriteLine("Got depot key for {0} result: {1}", msg.DepotID, msg.Result);
|
||||
DepotKeys[msg.DepotID] = msg.DepotKey;
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private void ConnectedCallback(SteamClient.ConnectedCallback connected)
|
||||
{
|
||||
Console.WriteLine(" Done!");
|
||||
bConnected = true;
|
||||
steamUser.LogOn(logonDetails);
|
||||
|
||||
Console.Write("Logging '{0}' into Steam3...", logonDetails.Username);
|
||||
}
|
||||
|
||||
private void LogOnCallback(SteamUser.LoggedOnCallback loggedOn)
|
||||
{
|
||||
if (loggedOn.Result == EResult.AccountLogonDenied)
|
||||
{
|
||||
Console.WriteLine("This account is protected by Steam Guard. Please enter the authentication code sent to your email address.");
|
||||
Console.Write("Auth Code: ");
|
||||
|
||||
logonDetails.AuthCode = Console.ReadLine();
|
||||
|
||||
Console.Write("Retrying Steam3 connection...");
|
||||
Connect();
|
||||
|
||||
return;
|
||||
}
|
||||
else if (loggedOn.Result != EResult.OK)
|
||||
{
|
||||
Console.WriteLine("Unable to login to Steam3: {0}", loggedOn.Result);
|
||||
Abort();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine(" Done!");
|
||||
|
||||
Console.WriteLine("Got Steam2 Ticket!");
|
||||
credentials.Steam2Ticket = loggedOn.Steam2Ticket;
|
||||
|
||||
if (ContentDownloader.Config.CellID == 0)
|
||||
{
|
||||
Console.WriteLine("Using Steam3 suggest CellID: " + loggedOn.CellID);
|
||||
ContentDownloader.Config.CellID = (int)loggedOn.CellID;
|
||||
}
|
||||
}
|
||||
|
||||
private void SessionTokenCallback(SteamUser.SessionTokenCallback sessionToken)
|
||||
{
|
||||
Console.WriteLine("Got session token!");
|
||||
credentials.SessionToken = sessionToken.SessionToken;
|
||||
}
|
||||
|
||||
private void LicenseListCallback(SteamApps.LicenseListCallback licenseList)
|
||||
{
|
||||
if (licenseList.Result != EResult.OK)
|
||||
{
|
||||
Console.WriteLine("Unable to get license list: {0} ", licenseList.Result);
|
||||
Abort();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Console.WriteLine("Got {0} licenses for account!", licenseList.LicenseList.Count);
|
||||
Licenses = licenseList.LicenseList;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user