Finish leftovers from previous commit

This commit is contained in:
JustArchi
2017-07-21 14:25:25 +02:00
parent f564cc5ebb
commit 15ec01f447
6 changed files with 92 additions and 67 deletions

View File

@@ -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);

View File

@@ -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;
}

View File

@@ -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<JsonConverter>(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<GlobalDatabase>(File.ReadAllText(filePath), CustomSerializerSettings);
globalDatabase = JsonConvert.DeserializeObject<GlobalDatabase>(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;

View File

@@ -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<string>());
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) {
IPAddress ip = (IPAddress) value;
writer.WriteValue(ip.ToString());
}
}
}

View File

@@ -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<ServerRecord> Servers = new ConcurrentHashSet<ServerRecord>();
private readonly ConcurrentHashSet<ServerRecordEndPoint> ServerRecords = new ConcurrentHashSet<ServerRecordEndPoint>();
public Task<IEnumerable<ServerRecord>> FetchServerListAsync() => Task.FromResult<IEnumerable<ServerRecord>>(Servers);
public Task<IEnumerable<ServerRecord>> FetchServerListAsync() => Task.FromResult(ServerRecords.Select(server => ServerRecord.CreateServer(server.Host, server.Port, server.ProtocolTypes)));
public Task UpdateServerListAsync(IEnumerable<ServerRecord> endpoints) {
if (endpoints == null) {
@@ -41,9 +42,9 @@ namespace ArchiSteamFarm {
return Task.CompletedTask;
}
HashSet<ServerRecord> newServers = new HashSet<ServerRecord>(endpoints);
HashSet<ServerRecordEndPoint> newServerRecords = new HashSet<ServerRecordEndPoint>(endpoints.Select(ep => new ServerRecordEndPoint(ep.GetHost(), (ushort) ep.GetPort(), ep.ProtocolTypes)));
if (!Servers.ReplaceIfNeededWith(newServers)) {
if (!ServerRecords.ReplaceIfNeededWith(newServerRecords)) {
return Task.CompletedTask;
}

View File

@@ -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);
}
}