Code cleanup

This commit is contained in:
JustArchi
2017-02-06 15:46:42 +01:00
parent b4483057b9
commit d04e17a046
3 changed files with 61 additions and 40 deletions

View File

@@ -28,23 +28,36 @@ using Newtonsoft.Json;
namespace ArchiSteamFarm.JSON { namespace ArchiSteamFarm.JSON {
internal static class GitHub { internal static class GitHub {
[SuppressMessage("ReSharper", "ClassCannotBeInstantiated")]
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
internal sealed class ReleaseResponse { internal sealed class ReleaseResponse {
#pragma warning disable 649 #pragma warning disable 649
internal sealed class Asset {
[JsonProperty(PropertyName = "browser_download_url", Required = Required.Always)]
internal readonly string DownloadURL;
[JsonProperty(PropertyName = "name", Required = Required.Always)]
internal readonly string Name;
}
[JsonProperty(PropertyName = "tag_name", Required = Required.Always)]
internal readonly string Tag;
[JsonProperty(PropertyName = "assets", Required = Required.Always)] [JsonProperty(PropertyName = "assets", Required = Required.Always)]
internal readonly List<Asset> Assets; internal readonly List<Asset> Assets;
#pragma warning restore 649 #pragma warning restore 649
#pragma warning disable 649
[JsonProperty(PropertyName = "tag_name", Required = Required.Always)]
internal readonly string Tag;
#pragma warning restore 649
// Deserialized from JSON
private ReleaseResponse() { }
internal sealed class Asset {
#pragma warning disable 649
[JsonProperty(PropertyName = "browser_download_url", Required = Required.Always)]
internal readonly string DownloadURL;
#pragma warning restore 649
#pragma warning disable 649
[JsonProperty(PropertyName = "name", Required = Required.Always)]
internal readonly string Name;
#pragma warning restore 649
// Deserialized from JSON
private Asset() { }
}
} }
} }
} }

View File

@@ -26,7 +26,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.Linq; using System.Linq;
using System.Net;
using HtmlAgilityPack; using HtmlAgilityPack;
using Newtonsoft.Json; using Newtonsoft.Json;
using SteamKit2; using SteamKit2;
@@ -40,6 +39,7 @@ namespace ArchiSteamFarm.JSON {
[JsonProperty(PropertyName = "success", Required = Required.Always)] [JsonProperty(PropertyName = "success", Required = Required.Always)]
internal readonly bool Success; internal readonly bool Success;
#pragma warning restore 649 #pragma warning restore 649
internal ulong OtherSteamID64 { internal ulong OtherSteamID64 {
get { get {
if (_OtherSteamID64 != 0) { if (_OtherSteamID64 != 0) {
@@ -103,6 +103,7 @@ namespace ArchiSteamFarm.JSON {
[JsonProperty(PropertyName = "html", Required = Required.DisallowNull)] [JsonProperty(PropertyName = "html", Required = Required.DisallowNull)]
private readonly string HTML; private readonly string HTML;
#pragma warning restore 649 #pragma warning restore 649
private HtmlDocument HtmlDocument { private HtmlDocument HtmlDocument {
get { get {
if (_HtmlDocument != null) { if (_HtmlDocument != null) {
@@ -113,8 +114,7 @@ namespace ArchiSteamFarm.JSON {
return null; return null;
} }
_HtmlDocument = new HtmlDocument(); _HtmlDocument = WebBrowser.StringToHtmlDocument(HTML);
_HtmlDocument.LoadHtml(WebUtility.HtmlDecode(HTML));
return _HtmlDocument; return _HtmlDocument;
} }
} }
@@ -196,7 +196,9 @@ namespace ArchiSteamFarm.JSON {
private ulong _OtherSteamID64; private ulong _OtherSteamID64;
private ulong _TradeOfferID; private ulong _TradeOfferID;
private EType _Type; private EType _Type;
private ConfirmationDetails() { } // Deserialized from JSON
// Deserialized from JSON
private ConfirmationDetails() { }
internal enum EType : byte { internal enum EType : byte {
Unknown, Unknown,
@@ -208,16 +210,18 @@ namespace ArchiSteamFarm.JSON {
[SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")]
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
internal sealed class ConfirmationResponse { // Deserialized from JSON internal sealed class ConfirmationResponse {
#pragma warning disable 649 #pragma warning disable 649
[JsonProperty(PropertyName = "success", Required = Required.Always)] [JsonProperty(PropertyName = "success", Required = Required.Always)]
internal readonly bool Success; internal readonly bool Success;
#pragma warning restore 649 #pragma warning restore 649
// Deserialized from JSON
private ConfirmationResponse() { } private ConfirmationResponse() { }
} }
internal sealed class Item { // REF: https://developer.valvesoftware.com/wiki/Steam_Web_API/IEconService#CEcon_Asset | Deserialized from JSON (SteamCommunity) and constructed from code // REF: https://developer.valvesoftware.com/wiki/Steam_Web_API/IEconService#CEcon_Asset
internal sealed class Item {
internal const ushort SteamAppID = 753; internal const ushort SteamAppID = 753;
internal const byte SteamCommunityContextID = 6; internal const byte SteamCommunityContextID = 6;
@@ -340,8 +344,8 @@ namespace ArchiSteamFarm.JSON {
set { AssetIDString = value; } set { AssetIDString = value; }
} }
// This constructor is used for constructing items in trades being received // Constructed from trades being received
internal Item(uint appID, ulong contextID, ulong classID, uint amount, uint realAppID, EType type) { internal Item(uint appID, ulong contextID, ulong classID, uint amount, uint realAppID, EType type = EType.Unknown) {
if ((appID == 0) || (contextID == 0) || (classID == 0) || (amount == 0) || (realAppID == 0)) { if ((appID == 0) || (contextID == 0) || (classID == 0) || (amount == 0) || (realAppID == 0)) {
throw new ArgumentNullException(nameof(classID) + " || " + nameof(contextID) + " || " + nameof(classID) + " || " + nameof(amount) + " || " + nameof(realAppID)); throw new ArgumentNullException(nameof(classID) + " || " + nameof(contextID) + " || " + nameof(classID) + " || " + nameof(amount) + " || " + nameof(realAppID));
} }
@@ -354,6 +358,7 @@ namespace ArchiSteamFarm.JSON {
Type = type; Type = type;
} }
// Deserialized from JSON
[SuppressMessage("ReSharper", "UnusedMember.Local")] [SuppressMessage("ReSharper", "UnusedMember.Local")]
private Item() { } private Item() { }
@@ -371,27 +376,29 @@ namespace ArchiSteamFarm.JSON {
/* /*
[SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")]
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
internal sealed class NewDiscoveryQueueResponse { // Deserialized from JSON internal sealed class NewDiscoveryQueueResponse {
#pragma warning disable 649
[JsonProperty(PropertyName = "queue", Required = Required.Always)] [JsonProperty(PropertyName = "queue", Required = Required.Always)]
internal readonly HashSet<uint> Queue; internal readonly HashSet<uint> Queue;
#pragma warning restore 649
// Deserialized from JSON
private NewDiscoveryQueueResponse() { } private NewDiscoveryQueueResponse() { }
} }
*/ */
[SuppressMessage("ReSharper", "ClassCannotBeInstantiated")] [SuppressMessage("ReSharper", "ClassCannotBeInstantiated")]
[SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")] [SuppressMessage("ReSharper", "ClassNeverInstantiated.Global")]
internal sealed class RedeemWalletResponse { // Deserialized from JSON internal sealed class RedeemWalletResponse {
#pragma warning disable 649 #pragma warning disable 649
[JsonProperty(PropertyName = "detail", Required = Required.Always)] [JsonProperty(PropertyName = "detail", Required = Required.Always)]
internal readonly ArchiHandler.PurchaseResponseCallback.EPurchaseResult PurchaseResult; internal readonly ArchiHandler.PurchaseResponseCallback.EPurchaseResult PurchaseResult;
#pragma warning restore 649 #pragma warning restore 649
// Deserialized from JSON
private RedeemWalletResponse() { } private RedeemWalletResponse() { }
} }
// REF: https://developer.valvesoftware.com/wiki/Steam_Web_API/IEconService#CEcon_TradeOffer
internal sealed class TradeOffer { internal sealed class TradeOffer {
internal readonly HashSet<Item> ItemsToGive = new HashSet<Item>(); internal readonly HashSet<Item> ItemsToGive = new HashSet<Item>();
internal readonly HashSet<Item> ItemsToReceive = new HashSet<Item>(); internal readonly HashSet<Item> ItemsToReceive = new HashSet<Item>();
@@ -418,6 +425,7 @@ namespace ArchiSteamFarm.JSON {
private ulong _OtherSteamID64; private ulong _OtherSteamID64;
// Constructed from trades being received
internal TradeOffer(ulong tradeOfferID, uint otherSteamID3, ETradeOfferState state) { internal TradeOffer(ulong tradeOfferID, uint otherSteamID3, ETradeOfferState state) {
if ((tradeOfferID == 0) || (otherSteamID3 == 0) || (state == ETradeOfferState.Unknown)) { if ((tradeOfferID == 0) || (otherSteamID3 == 0) || (state == ETradeOfferState.Unknown)) {
throw new ArgumentNullException(nameof(tradeOfferID) + " || " + nameof(otherSteamID3) + " || " + nameof(state)); throw new ArgumentNullException(nameof(tradeOfferID) + " || " + nameof(otherSteamID3) + " || " + nameof(state));
@@ -486,7 +494,7 @@ namespace ArchiSteamFarm.JSON {
return true; return true;
} }
internal bool IsSteamCardsRequest() => ItemsToGive.All(item => (item.AppID == Item.SteamAppID) && (item.ContextID == Item.SteamCommunityContextID) && (item.Type == Item.EType.TradingCard)); // REF: https://developer.valvesoftware.com/wiki/Steam_Web_API/IEconService#CEcon_TradeOffer | Constructed from code internal bool IsSteamCardsRequest() => ItemsToGive.All(item => (item.AppID == Item.SteamAppID) && (item.ContextID == Item.SteamCommunityContextID) && (item.Type == Item.EType.TradingCard));
[SuppressMessage("ReSharper", "UnusedMember.Global")] [SuppressMessage("ReSharper", "UnusedMember.Global")]
internal enum ETradeOfferState : byte { internal enum ETradeOfferState : byte {
@@ -511,7 +519,7 @@ namespace ArchiSteamFarm.JSON {
internal readonly ItemList ItemsToGive = new ItemList(); internal readonly ItemList ItemsToGive = new ItemList();
[JsonProperty(PropertyName = "them", Required = Required.Always)] [JsonProperty(PropertyName = "them", Required = Required.Always)]
internal readonly ItemList ItemsToReceive = new ItemList(); // Constructed from code internal readonly ItemList ItemsToReceive = new ItemList();
internal sealed class ItemList { internal sealed class ItemList {
[JsonProperty(PropertyName = "assets", Required = Required.Always)] [JsonProperty(PropertyName = "assets", Required = Required.Always)]

View File

@@ -87,6 +87,18 @@ namespace ArchiSteamFarm {
#endif #endif
} }
internal static HtmlDocument StringToHtmlDocument(string html) {
if (string.IsNullOrEmpty(html)) {
ASF.ArchiLogger.LogNullError(nameof(html));
return null;
}
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(html);
return htmlDocument;
}
internal async Task<byte[]> UrlGetToBytesRetry(string request, string referer = null) { internal async Task<byte[]> UrlGetToBytesRetry(string request, string referer = null) {
if (string.IsNullOrEmpty(request)) { if (string.IsNullOrEmpty(request)) {
ArchiLogger.LogNullError(nameof(request)); ArchiLogger.LogNullError(nameof(request));
@@ -264,13 +276,7 @@ namespace ArchiSteamFarm {
} }
string content = await UrlPostToContentRetry(request, data, referer).ConfigureAwait(false); string content = await UrlPostToContentRetry(request, data, referer).ConfigureAwait(false);
if (string.IsNullOrEmpty(content)) { return !string.IsNullOrEmpty(content) ? StringToHtmlDocument(content) : null;
return null;
}
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(content);
return htmlDocument;
} }
internal async Task<T> UrlPostToJsonResultRetry<T>(string request, ICollection<KeyValuePair<string, string>> data = null, string referer = null) { internal async Task<T> UrlPostToJsonResultRetry<T>(string request, ICollection<KeyValuePair<string, string>> data = null, string referer = null) {
@@ -353,13 +359,7 @@ namespace ArchiSteamFarm {
} }
string content = await UrlGetToContent(request, referer).ConfigureAwait(false); string content = await UrlGetToContent(request, referer).ConfigureAwait(false);
if (string.IsNullOrEmpty(content)) { return !string.IsNullOrEmpty(content) ? StringToHtmlDocument(content) : null;
return null;
}
HtmlDocument htmlDocument = new HtmlDocument();
htmlDocument.LoadHtml(content);
return htmlDocument;
} }
private async Task<JObject> UrlGetToJObject(string request, string referer = null) { private async Task<JObject> UrlGetToJObject(string request, string referer = null) {