Skip to content

Commit

Permalink
Reworked logic, refactoring, attempt to fix the "A task was canceled"…
Browse files Browse the repository at this point in the history
… exception
  • Loading branch information
SeRi0uS007 committed Sep 3, 2023
1 parent bf512de commit 3ff358e
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 112 deletions.
25 changes: 13 additions & 12 deletions SteamAccountDataFetcher/FSAgent/WriteJSONAgent.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using System.Collections;
using System.Text.Json;
using SteamAccountDataFetcher.SteamDataClient;

namespace SteamAccountDataFetcher.FSAgent;

internal class WriteJSONAgent : IList<SteamDataClient.SteamDataClient.ResponseData>
internal class WriteJSONAgent : IList<ResponseData>
{
private string FilePath { get; init; }

private List<SteamDataClient.SteamDataClient.ResponseData> _accountDataList;
private List<ResponseData> _accountDataList;
private static readonly JsonSerializerOptions _jsonSerializerOptions = new()
{
WriteIndented = true
Expand All @@ -34,7 +35,7 @@ internal WriteJSONAgent(string path)
{
using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
{
var cachedType = JsonSerializer.Deserialize<List<SteamDataClient.SteamDataClient.ResponseData>?>(file);
var cachedType = JsonSerializer.Deserialize<List<ResponseData>?>(file);
if (cachedType == null)
{
_accountDataList = new();
Expand All @@ -57,7 +58,7 @@ private void WriteCache()
JsonSerializer.Serialize(file, _accountDataList, _jsonSerializerOptions);
}

public SteamDataClient.SteamDataClient.ResponseData this[int index]
public ResponseData this[int index]
{
get => _accountDataList[index];
set => _accountDataList[index] = value;
Expand All @@ -66,11 +67,11 @@ public SteamDataClient.SteamDataClient.ResponseData this[int index]
public int Count => _accountDataList.Count;

public bool IsReadOnly =>
((IList<SteamDataClient.SteamDataClient.ResponseData>)_accountDataList).IsReadOnly;
((IList<ResponseData>)_accountDataList).IsReadOnly;

public int IndexOf(SteamDataClient.SteamDataClient.ResponseData item) => _accountDataList.IndexOf(item);
public int IndexOf(ResponseData item) => _accountDataList.IndexOf(item);

public void Insert(int index, SteamDataClient.SteamDataClient.ResponseData item)
public void Insert(int index, ResponseData item)
{
_accountDataList.Insert(index, item);
WriteCache();
Expand All @@ -82,7 +83,7 @@ public void RemoveAt(int index)
WriteCache();
}

public void Add(SteamDataClient.SteamDataClient.ResponseData item)
public void Add(ResponseData item)
{
_accountDataList.Add(item);
WriteCache();
Expand All @@ -94,11 +95,11 @@ public void Clear()
WriteCache();
}

public bool Contains(SteamDataClient.SteamDataClient.ResponseData item) => _accountDataList.Contains(item);
public bool Contains(ResponseData item) => _accountDataList.Contains(item);

public void CopyTo(SteamDataClient.SteamDataClient.ResponseData[] array, int arrayIndex) => _accountDataList.CopyTo(array, arrayIndex);
public void CopyTo(ResponseData[] array, int arrayIndex) => _accountDataList.CopyTo(array, arrayIndex);

public bool Remove(SteamDataClient.SteamDataClient.ResponseData item)
public bool Remove(ResponseData item)
{
var success = _accountDataList.Remove(item);
if (success)
Expand All @@ -107,7 +108,7 @@ public bool Remove(SteamDataClient.SteamDataClient.ResponseData item)
return success;
}

public IEnumerator<SteamDataClient.SteamDataClient.ResponseData> GetEnumerator() => _accountDataList.GetEnumerator();
public IEnumerator<ResponseData> GetEnumerator() => _accountDataList.GetEnumerator();

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}
4 changes: 2 additions & 2 deletions SteamAccountDataFetcher/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static async Task Main()
{
AccountLoginInfo account = csvAccounts.First();

SteamDataClient.SteamDataClient steamDataClient = new(account.Username, account.Password, account.SharedSecret);
SteamDataClient.SteamDataClient.ResponseData data = await steamDataClient.GetResponseDataAsync();
Client steamDataClient = new(account.Username, account.Password, account.SharedSecret);
ResponseData data = await steamDataClient.GetResponseDataAsync();

jsonAccounts.Add(data);
csvAccounts.Remove(account);
Expand Down
2 changes: 1 addition & 1 deletion SteamAccountDataFetcher/SteamAccountDataFetcher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Authors>Andrii Lavrenko</Authors>
<Description>Utility for collecting Steam account information</Description>
<Copyright>Andrii Lavrenko</Copyright>
<Version>1.0.0</Version>
<Version>1.1.0</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,23 @@

namespace SteamAccountDataFetcher.SteamDataClient;

internal class SteamDataClient
public class ResponseData
{
public class AppData
{
public uint AppId { get; set; } = 0;
public long RegistrationTime { get; set; } = 0;
public string AppName { get; set; } = string.Empty;
}
public string Username { get; set; } = string.Empty;
public ulong SteamId { get; set; } = 0;
public List<AppData> Apps { get; set; } = new();
public bool IsLimited { get; set; }
public bool IsBanned { get; set; }
public string ApiKey { get; set; } = string.Empty;
}

internal class Client
{
private SteamClient _steamClient;
private SteamTwoFactorGenerator _steamTwoFactorGenerator;
Expand All @@ -14,6 +30,7 @@ internal class SteamDataClient

private bool _isRunning = false;
private bool _isLicensesProcessed = false;
private bool _isWebAPIProcessed = false;

internal SteamConfiguration SteamConfiguration
{
Expand All @@ -29,29 +46,10 @@ internal SteamID? SteamID

private static uint _instance = 0;
private static DateTime _lastConnectionTime = DateTime.MinValue;

public class ResponseData
{
public class AppData
{
public uint AppId { get; set; } = 0;
public long RegistrationTime { get; set; } = 0;
public string AppName { get; set; } = string.Empty;
}
public bool Success
{
get => SteamId != 0 && !string.IsNullOrEmpty(ApiKey);
}
public string Username { get; set; } = string.Empty;
public ulong SteamId { get; set; } = 0;
public List<AppData> Apps { get; set; } = new();
public bool IsLimited { get; set; }
public bool IsBanned { get; set; }
public string ApiKey { get; set; } = string.Empty;
}

private ResponseData _responseData;

internal SteamDataClient(string username, string password, string sharedSecret)
internal Client(string username, string password, string sharedSecret)
{
++_instance;

Expand Down Expand Up @@ -86,8 +84,8 @@ internal SteamDataClient(string username, string password, string sharedSecret)
}
_steamApps = steamApps;

_callbackManager.Subscribe<SteamClient.ConnectedCallback>(OnConnectedAsync);
_callbackManager.Subscribe<SteamClient.DisconnectedCallback>(OnDisconnectedAsync);
_callbackManager.Subscribe<SteamKit2.SteamClient.ConnectedCallback>(OnConnectedAsync);
_callbackManager.Subscribe<SteamKit2.SteamClient.DisconnectedCallback>(OnDisconnectedAsync);
_callbackManager.Subscribe<SteamUser.LoggedOnCallback>(OnLoggedOnAsync);
_callbackManager.Subscribe<SteamUser.LoggedOffCallback>(OnLoggedOffAsync);
_callbackManager.Subscribe<SteamApps.LicenseListCallback>(OnLicenseListAsync);
Expand Down Expand Up @@ -121,7 +119,7 @@ internal async Task RunAsync()
{
_callbackManager.RunCallbacks();

if (_responseData.Success && _isLicensesProcessed)
if (_isLicensesProcessed && _isWebAPIProcessed)
_steamClient.Disconnect();
await Task.Delay(TimeSpan.FromSeconds(1));
}
Expand All @@ -138,7 +136,7 @@ private async Task LoginAsync()
});
}

private async void OnConnectedAsync(SteamClient.ConnectedCallback callback)
private async void OnConnectedAsync(SteamKit2.SteamClient.ConnectedCallback callback)
{
_lastConnectionTime = DateTime.Now;

Expand All @@ -161,21 +159,12 @@ private async void OnConnectedAsync(SteamClient.ConnectedCallback callback)
await LoginAsync();
}

private async void OnDisconnectedAsync(SteamClient.DisconnectedCallback callback)
private async void OnDisconnectedAsync(SteamKit2.SteamClient.DisconnectedCallback callback)
{
if (callback.UserInitiated)
{
Log("Disconnected from Steam Network by user.");

if (!_responseData.Success || !_isLicensesProcessed)
{
_responseData.SteamId = 0;
_responseData.Apps.Clear();
_responseData.IsLimited = false;
_responseData.IsBanned = false;
_responseData.ApiKey = string.Empty;
}

_isRunning = false;
return;
}
Expand Down Expand Up @@ -223,22 +212,10 @@ private async void OnLoggedOnAsync(SteamUser.LoggedOnCallback callback)
_steamClient.Disconnect();
return;
}
Log("Logged into Web Api. Retrieving Web API Key.");
await Task.Delay(Configuration.DefaultWebRequestTimeout);

(bool success, string webApiKey) = await _steamWebClient.GetWebApiKeyAsync();
if (!success)
{
Log("Unable to retrieve Web Api Key.", Logger.Level.Error);
_steamClient.Disconnect();
return;
}
Log("Retrieved Web API Key.");

_responseData.ApiKey = webApiKey;
Log("Logged into Web Api.");
}

private void OnIsLimitedAccount(DataFetcher.IsLimitedAccountCallback callback)
private async void OnIsLimitedAccount(DataFetcher.IsLimitedAccountCallback callback)
{
if (callback.Locked)
{
Expand All @@ -247,11 +224,35 @@ private void OnIsLimitedAccount(DataFetcher.IsLimitedAccountCallback callback)
return;
}

_responseData.IsBanned = callback.CommunityBanned;
_responseData.IsLimited = callback.Limited;

if (callback.CommunityBanned)
Log("Account is banned.", Logger.Level.Warning);

_responseData.IsBanned = callback.CommunityBanned;
_responseData.IsLimited = callback.Limited;
if (callback.Limited)
{
Log("Account is limited.", Logger.Level.Warning);

// Limited accounts is unable to retrieve Web API
_isWebAPIProcessed = true;
return;
}

Log("Retrieving Web API Key.");
await Task.Delay(Configuration.DefaultWebRequestTimeout);

(bool success, string webApiKey) = await _steamWebClient.GetWebApiKeyAsync();
if (!success)
{
Log("Unable to retrieve Web Api Key.", Logger.Level.Error);
_steamClient.Disconnect();
return;
}
Log("Retrieved Web API Key.");

_responseData.ApiKey = webApiKey;
_isWebAPIProcessed = true;
}

private async void OnLoggedOffAsync(SteamUser.LoggedOffCallback callback)
Expand Down Expand Up @@ -287,14 +288,30 @@ private async void OnLicenseListAsync(SteamApps.LicenseListCallback callback)
if (package.PackageID == 0)
continue;

Log($"Retrieving PICS info for package {package.PackageID}");
var packageUnixTime = (new DateTimeOffset(package.TimeCreated)).ToUnixTimeMilliseconds();

Log($"Retrieving PICS info for package {package.PackageID}.");

List<SteamApps.PICSRequest> packages = new()
{
new(package.PackageID, package.AccessToken)
};

var productInfo = await _steamApps.PICSGetProductInfo(new List<SteamApps.PICSRequest>(0), packages);
AsyncJobMultiple<SteamApps.PICSProductInfoCallback>.ResultSet productInfo;
while (true)
{
try
{
productInfo = await _steamApps.PICSGetProductInfo(new List<SteamApps.PICSRequest>(0), packages);
break;
}
catch (TaskCanceledException)
{
Log($"Failure to receive PICS info for package {package.PackageID}. Retrying...", Logger.Level.Warning);
await Task.Delay(1000);
continue;
}
}
if (productInfo == null || !productInfo.Complete || productInfo.Failed || productInfo.Results == null)
{
Log($"Unable to receive PICS info", Logger.Level.Error);
Expand All @@ -306,26 +323,37 @@ private async void OnLicenseListAsync(SteamApps.LicenseListCallback callback)
{
foreach (var packageResponse in product.Packages.Values)
{
List<uint> apps = GetAppsInKeyValues(packageResponse.KeyValues);
if (apps.Count == 0)
uint[] apps = GetAppsInKeyValues(packageResponse.KeyValues);
List<uint> newApps = new();

foreach (var app in apps)
{
var appData = _responseData.Apps.SingleOrDefault(x => x?.AppId == app, null);
if (appData == null)
newApps.Add(app);
else
if (appData.RegistrationTime > packageUnixTime)
appData.RegistrationTime = packageUnixTime;
}

if (newApps.Count == 0)
continue;

(var success, var appNames) = await _steamWebClient.GetAppNamesAsync(apps.ToArray());
(var success, var appNames) = await _steamWebClient.GetAppNamesAsync(newApps.ToArray());

if (!success || appNames == null)
{
Log($"Unable to receive Apps info", Logger.Level.Error);
Log($"Unable to receive Apps info.", Logger.Level.Error);
_steamClient.Disconnect();
return;
}

foreach (var app in appNames)
{
var offset = new DateTimeOffset(package.TimeCreated);
_responseData.Apps.Add(new()
{
AppId = app.Key,
RegistrationTime = offset.ToUnixTimeMilliseconds(),
RegistrationTime = packageUnixTime,
AppName = app.Value
});
}
Expand All @@ -346,20 +374,17 @@ private async void OnLicenseListAsync(SteamApps.LicenseListCallback callback)
_isLicensesProcessed = true;
}

private List<uint> GetAppsInKeyValues(KeyValue keyValue)
private uint[] GetAppsInKeyValues(KeyValue keyValue)
{
List<uint> apps = new();

foreach (KeyValue appKeyValue in keyValue["appids"].Children)
{
var app = appKeyValue.AsUnsignedInteger();
if (_responseData.Apps.Any(x => x.AppId == app))
continue;

apps.Add(app);
}

return apps;
return apps.ToArray();
}

internal void Log(string message, Logger.Level level = Logger.Level.Info, [CallerMemberName] string callerName = "") =>
Expand Down
Loading

0 comments on commit 3ff358e

Please sign in to comment.