diff --git a/ArchiSteamFarm/ASF.cs b/ArchiSteamFarm/ASF.cs index 7779ba1bc..66b076f7f 100644 --- a/ArchiSteamFarm/ASF.cs +++ b/ArchiSteamFarm/ASF.cs @@ -220,7 +220,7 @@ namespace ArchiSteamFarm { } // Before attempting to connect, initialize our configuration - await Bot.InitializeSteamConfiguration(Program.GlobalConfig.SteamProtocols, Program.GlobalDatabase.CellID, Program.GlobalDatabase.ServerList).ConfigureAwait(false); + await Bot.InitializeSteamConfiguration(Program.GlobalConfig.SteamProtocols, Program.GlobalDatabase.CellID, Program.GlobalDatabase.ServerListProvider).ConfigureAwait(false); foreach (string botName in Directory.EnumerateFiles(SharedInfo.ConfigDirectory, "*.json").Select(Path.GetFileNameWithoutExtension).Where(botName => !string.IsNullOrEmpty(botName) && IsValidBotName(botName)).OrderBy(botName => botName)) { Bot.RegisterBot(botName); diff --git a/ArchiSteamFarm/CardsFarmer.cs b/ArchiSteamFarm/CardsFarmer.cs index 5734699a6..7900d012e 100755 --- a/ArchiSteamFarm/CardsFarmer.cs +++ b/ArchiSteamFarm/CardsFarmer.cs @@ -854,7 +854,8 @@ namespace ArchiSteamFarm { return; } - GamesToFarm.ReplaceWith(gamesToFarm.ToList()); // We must call ToList() here as we can't enumerate during replacing + // We must call ToList() here as we can't enumerate during replacing + GamesToFarm.ReplaceWith(gamesToFarm.ToList()); } internal sealed class Game { @@ -870,8 +871,6 @@ namespace ArchiSteamFarm { [JsonProperty] internal float HoursPlayed { get; set; } - //internal string HeaderURL => "https://steamcdn-a.akamaihd.net/steam/apps/" + AppID + "/header.jpg"; - internal Game(uint appID, string gameName, float hoursPlayed, ushort cardsRemaining) { if ((appID == 0) || string.IsNullOrEmpty(gameName) || (hoursPlayed < 0) || (cardsRemaining == 0)) { throw new ArgumentOutOfRangeException(nameof(appID) + " || " + nameof(gameName) + " || " + nameof(hoursPlayed) + " || " + nameof(cardsRemaining)); @@ -884,11 +883,11 @@ namespace ArchiSteamFarm { } public override bool Equals(object obj) { - if (obj == null) { + if (ReferenceEquals(null, obj)) { return false; } - if (obj == this) { + if (ReferenceEquals(this, obj)) { return true; } diff --git a/ArchiSteamFarm/GlobalDatabase.cs b/ArchiSteamFarm/GlobalDatabase.cs index ff89c7139..ab43977f7 100644 --- a/ArchiSteamFarm/GlobalDatabase.cs +++ b/ArchiSteamFarm/GlobalDatabase.cs @@ -23,24 +23,16 @@ */ using System; -using System.Collections.Generic; using System.IO; using Newtonsoft.Json; namespace ArchiSteamFarm { internal sealed class GlobalDatabase : IDisposable { - private static readonly JsonSerializerSettings CustomSerializerSettings = new JsonSerializerSettings { - Converters = new List(1) { - new IPAddressConverter() - } - }; - [JsonProperty(Required = Required.DisallowNull)] internal readonly Guid Guid = Guid.NewGuid(); [JsonProperty(Required = Required.DisallowNull)] - [JsonIgnore] // TODO: Remove me once https://github.com/SteamRE/SteamKit/issues/416 is solved - internal readonly InMemoryServerListProvider ServerList = new InMemoryServerListProvider(); + internal readonly InMemoryServerListProvider ServerListProvider = new InMemoryServerListProvider(); private readonly object FileLock = new object(); @@ -72,9 +64,9 @@ namespace ArchiSteamFarm { } // This constructor is used only by deserializer - private GlobalDatabase() => ServerList.ServerListUpdated += OnServerListUpdated; + private GlobalDatabase() => ServerListProvider.ServerListUpdated += OnServerListUpdated; - public void Dispose() => ServerList.ServerListUpdated -= OnServerListUpdated; + public void Dispose() => ServerListProvider.ServerListUpdated -= OnServerListUpdated; internal static GlobalDatabase Load(string filePath) { if (string.IsNullOrEmpty(filePath)) { @@ -89,7 +81,7 @@ namespace ArchiSteamFarm { GlobalDatabase globalDatabase; try { - globalDatabase = JsonConvert.DeserializeObject(File.ReadAllText(filePath), CustomSerializerSettings); + globalDatabase = JsonConvert.DeserializeObject(File.ReadAllText(filePath)); } catch (Exception e) { ASF.ArchiLogger.LogGenericException(e); return null; @@ -107,7 +99,7 @@ namespace ArchiSteamFarm { private void OnServerListUpdated(object sender, EventArgs e) => Save(); private void Save() { - string json = JsonConvert.SerializeObject(this, CustomSerializerSettings); + string json = JsonConvert.SerializeObject(this); if (string.IsNullOrEmpty(json)) { ASF.ArchiLogger.LogNullError(nameof(json)); return; diff --git a/ArchiSteamFarm/IPAddressConverter.cs b/ArchiSteamFarm/IPAddressConverter.cs deleted file mode 100644 index f6ab83fe0..000000000 --- a/ArchiSteamFarm/IPAddressConverter.cs +++ /dev/null @@ -1,44 +0,0 @@ -/* - _ _ _ ____ _ _____ - / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ - / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ - / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | -/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| - - Copyright 2015-2017 Łukasz "JustArchi" Domeradzki - Contact: JustArchi@JustArchi.net - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. - -*/ - -using System; -using System.Net; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; - -namespace ArchiSteamFarm { - internal sealed class IPAddressConverter : JsonConverter { - public override bool CanConvert(Type objectType) => objectType == typeof(IPAddress); - - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { - JToken token = JToken.Load(reader); - return IPAddress.Parse(token.Value()); - } - - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { - IPAddress ip = (IPAddress) value; - writer.WriteValue(ip.ToString()); - } - } -} \ No newline at end of file diff --git a/ArchiSteamFarm/InMemoryServerListProvider.cs b/ArchiSteamFarm/InMemoryServerListProvider.cs index 9d9eca5f3..06e96a1ce 100644 --- a/ArchiSteamFarm/InMemoryServerListProvider.cs +++ b/ArchiSteamFarm/InMemoryServerListProvider.cs @@ -24,6 +24,7 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Threading.Tasks; using Newtonsoft.Json; using SteamKit2.Discovery; @@ -31,9 +32,9 @@ using SteamKit2.Discovery; namespace ArchiSteamFarm { internal sealed class InMemoryServerListProvider : IServerListProvider { [JsonProperty(Required = Required.DisallowNull)] - private readonly ConcurrentHashSet Servers = new ConcurrentHashSet(); + private readonly ConcurrentHashSet ServerRecords = new ConcurrentHashSet(); - public Task> FetchServerListAsync() => Task.FromResult>(Servers); + public Task> FetchServerListAsync() => Task.FromResult(ServerRecords.Select(server => ServerRecord.CreateServer(server.Host, server.Port, server.ProtocolTypes))); public Task UpdateServerListAsync(IEnumerable endpoints) { if (endpoints == null) { @@ -41,9 +42,9 @@ namespace ArchiSteamFarm { return Task.CompletedTask; } - HashSet newServers = new HashSet(endpoints); + HashSet newServerRecords = new HashSet(endpoints.Select(ep => new ServerRecordEndPoint(ep.GetHost(), (ushort) ep.GetPort(), ep.ProtocolTypes))); - if (!Servers.ReplaceIfNeededWith(newServers)) { + if (!ServerRecords.ReplaceIfNeededWith(newServerRecords)) { return Task.CompletedTask; } diff --git a/ArchiSteamFarm/ServerRecordEndPoint.cs b/ArchiSteamFarm/ServerRecordEndPoint.cs new file mode 100644 index 000000000..ec8bddfcf --- /dev/null +++ b/ArchiSteamFarm/ServerRecordEndPoint.cs @@ -0,0 +1,77 @@ +/* + _ _ _ ____ _ _____ + / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___ + / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \ + / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | | +/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_| + + Copyright 2015-2017 Łukasz "JustArchi" Domeradzki + Contact: JustArchi@JustArchi.net + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +*/ + +using System; +using Newtonsoft.Json; +using SteamKit2; + +namespace ArchiSteamFarm { + internal sealed class ServerRecordEndPoint { + [JsonProperty(Required = Required.Always)] + internal readonly string Host; + + [JsonProperty(Required = Required.Always)] + internal readonly ushort Port; + + [JsonProperty(Required = Required.Always)] + internal readonly ProtocolTypes ProtocolTypes; + + internal ServerRecordEndPoint(string host, ushort port, ProtocolTypes protocolTypes) { + if (string.IsNullOrEmpty(host)) { + throw new ArgumentNullException(nameof(host)); + } + + if (port == 0) { + throw new ArgumentNullException(nameof(port)); + } + + if (protocolTypes == 0) { + throw new ArgumentNullException(nameof(protocolTypes)); + } + + Host = host; + Port = port; + ProtocolTypes = protocolTypes; + } + + private ServerRecordEndPoint() { } + + public override bool Equals(object obj) { + if (ReferenceEquals(null, obj)) { + return false; + } + + if (ReferenceEquals(this, obj)) { + return true; + } + + ServerRecordEndPoint serverRecord = obj as ServerRecordEndPoint; + return (serverRecord != null) && Equals(serverRecord); + } + + public override int GetHashCode() => (Host, Port, ProtocolTypes).GetHashCode(); + + private bool Equals(ServerRecordEndPoint other) => string.Equals(Host, other.Host) && (Port == other.Port) && (ProtocolTypes == other.ProtocolTypes); + } +} \ No newline at end of file