mirror of
https://github.com/SteamAutoCracks/DepotDownloaderMod.git
synced 2026-02-13 13:20:56 +01:00
Cleaned up the CDNCache threading even more.
--HG-- extra : convert_revision : svn%3A946a0da7-ebce-4904-9acb-2f1e67aed693%40143
This commit is contained in:
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Windows.Forms;
|
||||
using System.Threading;
|
||||
|
||||
namespace Vapor
|
||||
{
|
||||
@@ -24,6 +25,8 @@ namespace Vapor
|
||||
{
|
||||
Steam3.Update();
|
||||
Application.DoEvents();
|
||||
|
||||
Thread.Sleep( 1 ); // sue me, AzuiSleet.
|
||||
}
|
||||
|
||||
Steam3.Shutdown();
|
||||
|
||||
@@ -26,22 +26,21 @@ namespace Vapor
|
||||
|
||||
static Semaphore dlPool;
|
||||
|
||||
static object mapLock = new object();
|
||||
static Dictionary<SteamID, byte[]> avatarMap;
|
||||
|
||||
|
||||
static CDNCache()
|
||||
{
|
||||
dlPool = new Semaphore( 0, 3 );
|
||||
dlPool.Release( 3 );
|
||||
dlPool = new Semaphore( 0, 6 );
|
||||
dlPool.Release( 6 );
|
||||
|
||||
avatarMap = new Dictionary<SteamID, byte[]>();
|
||||
}
|
||||
|
||||
struct AvatarData
|
||||
{
|
||||
public SteamID friend;
|
||||
public byte[] hash;
|
||||
public string hashstr;
|
||||
public string localFile;
|
||||
public Action<AvatarDownloadDetails> callback;
|
||||
}
|
||||
|
||||
@@ -55,19 +54,7 @@ namespace Vapor
|
||||
|
||||
public static void DownloadAvatar( SteamID steamId, byte[] avatarHash, Action<AvatarDownloadDetails> callBack )
|
||||
{
|
||||
ThreadPool.QueueUserWorkItem( DoDownload, new AvatarData() { friend = steamId, hash = avatarHash, callback = callBack } );
|
||||
}
|
||||
|
||||
static void DoDownload( object state )
|
||||
{
|
||||
AvatarData data = ( AvatarData )state;
|
||||
|
||||
SteamID steamId = data.friend;
|
||||
Action<AvatarDownloadDetails> callBack = data.callback;
|
||||
byte[] avatarHash = data.hash;
|
||||
|
||||
lock ( mapLock )
|
||||
avatarMap[ steamId ] = avatarHash;
|
||||
avatarMap[ steamId ] = avatarHash;
|
||||
|
||||
string hashStr = BitConverter.ToString( avatarHash ).Replace( "-", "" ).ToLower();
|
||||
string hashPrefix = hashStr.Substring( 0, 2 );
|
||||
@@ -80,8 +67,9 @@ namespace Vapor
|
||||
{
|
||||
Directory.CreateDirectory( localPath ); // try making the cache directory
|
||||
}
|
||||
catch
|
||||
catch ( Exception ex )
|
||||
{
|
||||
DebugLog.WriteLine( "CDNCache", "Unableto create cache directory.\n{0}", ex.ToString() );
|
||||
callBack( new AvatarDownloadDetails() { Success = false, } );
|
||||
return;
|
||||
}
|
||||
@@ -94,9 +82,23 @@ namespace Vapor
|
||||
return;
|
||||
}
|
||||
|
||||
ThreadPool.QueueUserWorkItem( DoDownload, new AvatarData() { callback = callBack, hashstr = hashStr, localFile = localFile } );
|
||||
}
|
||||
|
||||
static void DoDownload( object state )
|
||||
{
|
||||
AvatarData data = ( AvatarData )state;
|
||||
|
||||
string hashStr = data.hashstr;
|
||||
string hashPrefix = hashStr.Substring( 0, 2 );
|
||||
string localPath = Path.Combine( Application.StartupPath, "cache" );
|
||||
var callBack = data.callback;
|
||||
|
||||
|
||||
dlPool.WaitOne();
|
||||
|
||||
string downloadUri = string.Format( AvatarRoot + AvatarSmall, hashPrefix, hashStr );
|
||||
string localFile = Path.Combine( localPath, hashStr + ".jpg" );
|
||||
|
||||
using ( WebClient client = new WebClient() )
|
||||
{
|
||||
@@ -106,13 +108,15 @@ namespace Vapor
|
||||
}
|
||||
catch
|
||||
{
|
||||
dlPool.Release();
|
||||
callBack( new AvatarDownloadDetails() { Success = false } );
|
||||
return;
|
||||
}
|
||||
finally
|
||||
{
|
||||
dlPool.Release();
|
||||
}
|
||||
}
|
||||
|
||||
dlPool.Release();
|
||||
callBack( new AvatarDownloadDetails() { Success = true, Filename = localFile } );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@
|
||||
<Compile Include="UI\ChatDialog.Designer.cs">
|
||||
<DependentUpon>ChatDialog.cs</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="ChatManager.cs" />
|
||||
<Compile Include="Steam\ChatManager.cs" />
|
||||
<Compile Include="UI\ChatTextBox.cs">
|
||||
<SubType>Component</SubType>
|
||||
</Compile>
|
||||
|
||||
Reference in New Issue
Block a user