* Start working on nullable checks

help me

* Update GlobalConfig.cs

* Finish initial fixup round

* nullability code review
This commit is contained in:
Łukasz Domeradzki
2020-08-22 21:41:01 +02:00
committed by GitHub
parent e5f64ec9dd
commit 9fc1ea65a5
91 changed files with 1996 additions and 2808 deletions

View File

@@ -31,7 +31,7 @@ namespace ArchiSteamFarm.NLog {
public sealed class ArchiLogger {
private readonly Logger Logger;
public ArchiLogger([NotNull] string name) {
public ArchiLogger(string name) {
if (string.IsNullOrEmpty(name)) {
throw new ArgumentNullException(nameof(name));
}
@@ -40,22 +40,18 @@ namespace ArchiSteamFarm.NLog {
}
[PublicAPI]
public void LogGenericDebug(string message, [CallerMemberName] string previousMethodName = null) {
public void LogGenericDebug(string message, [CallerMemberName] string? previousMethodName = null) {
if (string.IsNullOrEmpty(message)) {
LogNullError(nameof(message));
return;
throw new ArgumentNullException(nameof(message));
}
Logger.Debug($"{previousMethodName}() {message}");
}
[PublicAPI]
public void LogGenericDebuggingException(Exception exception, [CallerMemberName] string previousMethodName = null) {
public void LogGenericDebuggingException(Exception exception, [CallerMemberName] string? previousMethodName = null) {
if (exception == null) {
LogNullError(nameof(exception));
return;
throw new ArgumentNullException(nameof(exception));
}
if (!Debugging.IsUserDebugging) {
@@ -66,85 +62,71 @@ namespace ArchiSteamFarm.NLog {
}
[PublicAPI]
public void LogGenericError(string message, [CallerMemberName] string previousMethodName = null) {
public void LogGenericError(string message, [CallerMemberName] string? previousMethodName = null) {
if (string.IsNullOrEmpty(message)) {
LogNullError(nameof(message));
return;
throw new ArgumentNullException(nameof(message));
}
Logger.Error($"{previousMethodName}() {message}");
}
[PublicAPI]
public void LogGenericException(Exception exception, [CallerMemberName] string previousMethodName = null) {
public void LogGenericException(Exception exception, [CallerMemberName] string? previousMethodName = null) {
if (exception == null) {
LogNullError(nameof(exception));
return;
throw new ArgumentNullException(nameof(exception));
}
Logger.Error(exception, $"{previousMethodName}()");
}
[PublicAPI]
public void LogGenericInfo(string message, [CallerMemberName] string previousMethodName = null) {
public void LogGenericInfo(string message, [CallerMemberName] string? previousMethodName = null) {
if (string.IsNullOrEmpty(message)) {
LogNullError(nameof(message));
return;
throw new ArgumentNullException(nameof(message));
}
Logger.Info($"{previousMethodName}() {message}");
}
[PublicAPI]
public void LogGenericTrace(string message, [CallerMemberName] string previousMethodName = null) {
public void LogGenericTrace(string message, [CallerMemberName] string? previousMethodName = null) {
if (string.IsNullOrEmpty(message)) {
LogNullError(nameof(message));
return;
throw new ArgumentNullException(nameof(message));
}
Logger.Trace($"{previousMethodName}() {message}");
}
[PublicAPI]
public void LogGenericWarning(string message, [CallerMemberName] string previousMethodName = null) {
public void LogGenericWarning(string message, [CallerMemberName] string? previousMethodName = null) {
if (string.IsNullOrEmpty(message)) {
LogNullError(nameof(message));
return;
throw new ArgumentNullException(nameof(message));
}
Logger.Warn($"{previousMethodName}() {message}");
}
[PublicAPI]
public void LogGenericWarningException(Exception exception, [CallerMemberName] string previousMethodName = null) {
public void LogGenericWarningException(Exception exception, [CallerMemberName] string? previousMethodName = null) {
if (exception == null) {
LogNullError(nameof(exception));
return;
throw new ArgumentNullException(nameof(exception));
}
Logger.Warn(exception, $"{previousMethodName}()");
}
[PublicAPI]
public void LogNullError(string nullObjectName, [CallerMemberName] string previousMethodName = null) {
public void LogNullError(string nullObjectName, [CallerMemberName] string? previousMethodName = null) {
if (string.IsNullOrEmpty(nullObjectName)) {
return;
throw new ArgumentNullException(nameof(nullObjectName));
}
LogGenericError(string.Format(Strings.ErrorObjectIsNull, nullObjectName), previousMethodName);
}
internal void LogChatMessage(bool echo, string message, ulong chatGroupID = 0, ulong chatID = 0, ulong steamID = 0, [CallerMemberName] string previousMethodName = null) {
internal void LogChatMessage(bool echo, string message, ulong chatGroupID = 0, ulong chatID = 0, ulong steamID = 0, [CallerMemberName] string? previousMethodName = null) {
if (string.IsNullOrEmpty(message) || (((chatGroupID == 0) || (chatID == 0)) && (steamID == 0))) {
LogNullError(nameof(message) + " || " + "((" + nameof(chatGroupID) + " || " + nameof(chatID) + ") && " + nameof(steamID) + ")");
return;
throw new ArgumentNullException(nameof(message) + " || " + "((" + nameof(chatGroupID) + " || " + nameof(chatID) + ") && " + nameof(steamID) + ")");
}
StringBuilder loggedMessage = new StringBuilder(previousMethodName + "() " + message + " " + (echo ? "->" : "<-") + " ");
@@ -169,11 +151,9 @@ namespace ArchiSteamFarm.NLog {
Logger.Log(logEventInfo);
}
internal async Task LogFatalException(Exception exception, [CallerMemberName] string previousMethodName = null) {
internal async Task LogFatalException(Exception exception, [CallerMemberName] string? previousMethodName = null) {
if (exception == null) {
LogNullError(nameof(exception));
return;
throw new ArgumentNullException(nameof(exception));
}
Logger.Fatal(exception, $"{previousMethodName}()");

View File

@@ -75,12 +75,12 @@ namespace ArchiSteamFarm.NLog {
NewHistoryEntry?.Invoke(this, new NewHistoryEntryArgs(message));
}
internal event EventHandler<NewHistoryEntryArgs> NewHistoryEntry;
internal event EventHandler<NewHistoryEntryArgs>? NewHistoryEntry;
internal sealed class NewHistoryEntryArgs : EventArgs {
internal readonly string Message;
internal NewHistoryEntryArgs([NotNull] string message) => Message = message ?? throw new ArgumentNullException(nameof(message));
internal NewHistoryEntryArgs(string message) => Message = message ?? throw new ArgumentNullException(nameof(message));
}
}
}

View File

@@ -28,7 +28,6 @@ using System.Threading.Tasks;
using ArchiSteamFarm.Collections;
using ArchiSteamFarm.IPC;
using ArchiSteamFarm.Localization;
using JetBrains.Annotations;
using NLog;
using NLog.Config;
using NLog.Targets;
@@ -64,14 +63,12 @@ namespace ArchiSteamFarm.NLog {
}
}
internal static async Task<string> GetUserInput(ASF.EUserInputType userInputType, string botName = SharedInfo.ASF) {
internal static async Task<string?> GetUserInput(ASF.EUserInputType userInputType, string botName = SharedInfo.ASF) {
if ((userInputType == ASF.EUserInputType.None) || !Enum.IsDefined(typeof(ASF.EUserInputType), userInputType) || string.IsNullOrEmpty(botName)) {
ASF.ArchiLogger.LogNullError(nameof(userInputType) + " || " + nameof(botName));
return null;
throw new ArgumentNullException(nameof(userInputType) + " || " + nameof(botName));
}
if (ASF.GlobalConfig.Headless) {
if (ASF.GlobalConfig?.Headless ?? GlobalConfig.DefaultHeadless) {
ASF.ArchiLogger.LogGenericWarning(Strings.ErrorUserInputRunningInHeadlessMode);
return null;
@@ -196,7 +193,7 @@ namespace ArchiSteamFarm.NLog {
return;
}
HistoryTarget historyTarget = LogManager.Configuration.AllTargets.OfType<HistoryTarget>().FirstOrDefault();
HistoryTarget? historyTarget = LogManager.Configuration.AllTargets.OfType<HistoryTarget>().FirstOrDefault();
if ((historyTarget == null) && !IsUsingCustomConfiguration) {
historyTarget = new HistoryTarget("History") {
@@ -214,7 +211,7 @@ namespace ArchiSteamFarm.NLog {
}
internal static void StartInteractiveConsole() {
if (ASF.GlobalConfig.SteamOwnerID == 0) {
if ((ASF.GlobalConfig?.SteamOwnerID ?? GlobalConfig.DefaultSteamOwnerID) == 0) {
ASF.ArchiLogger.LogGenericWarning(string.Format(Strings.InteractiveConsoleNotAvailable, nameof(ASF.GlobalConfig.SteamOwnerID)));
return;
@@ -230,7 +227,6 @@ namespace ArchiSteamFarm.NLog {
return Console.ReadLine();
}
[NotNull]
private static string ConsoleReadLineMasked(char mask = '*') {
StringBuilder result = new StringBuilder();
@@ -287,15 +283,17 @@ namespace ArchiSteamFarm.NLog {
continue;
}
if (!string.IsNullOrEmpty(ASF.GlobalConfig.CommandPrefix) && command.StartsWith(ASF.GlobalConfig.CommandPrefix, StringComparison.Ordinal)) {
command = command.Substring(ASF.GlobalConfig.CommandPrefix.Length);
string commandPrefix = ASF.GlobalConfig?.CommandPrefix ?? GlobalConfig.DefaultCommandPrefix;
if (!string.IsNullOrEmpty(commandPrefix) && command.StartsWith(commandPrefix, StringComparison.Ordinal)) {
command = command.Substring(commandPrefix.Length);
if (string.IsNullOrEmpty(command)) {
continue;
}
}
Bot targetBot = Bot.Bots.OrderBy(bot => bot.Key, Bot.BotsComparer).Select(bot => bot.Value).FirstOrDefault();
Bot? targetBot = Bot.Bots.OrderBy(bot => bot.Key, Bot.BotsComparer).Select(bot => bot.Value).FirstOrDefault();
if (targetBot == null) {
Console.WriteLine(@"<< " + Strings.ErrorNoBotsDefined);
@@ -305,7 +303,9 @@ namespace ArchiSteamFarm.NLog {
Console.WriteLine(@"<> " + Strings.Executing);
string response = await targetBot.Commands.Response(ASF.GlobalConfig.SteamOwnerID, command).ConfigureAwait(false);
ulong steamOwnerID = ASF.GlobalConfig?.SteamOwnerID ?? GlobalConfig.DefaultSteamOwnerID;
string? response = await targetBot.Commands.Response(steamOwnerID, command).ConfigureAwait(false);
if (string.IsNullOrEmpty(response)) {
ASF.ArchiLogger.LogNullError(nameof(response));
@@ -339,9 +339,9 @@ namespace ArchiSteamFarm.NLog {
}
}
private static void OnConfigurationChanged(object sender, LoggingConfigurationChangedEventArgs e) {
if ((sender == null) || (e == null)) {
ASF.ArchiLogger.LogNullError(nameof(sender) + " || " + nameof(e));
private static void OnConfigurationChanged(object? sender, LoggingConfigurationChangedEventArgs e) {
if (e == null) {
ASF.ArchiLogger.LogNullError(nameof(e));
return;
}
@@ -352,7 +352,7 @@ namespace ArchiSteamFarm.NLog {
OnUserInputStart();
}
HistoryTarget historyTarget = LogManager.Configuration.AllTargets.OfType<HistoryTarget>().FirstOrDefault();
HistoryTarget? historyTarget = LogManager.Configuration.AllTargets.OfType<HistoryTarget>().FirstOrDefault();
ArchiKestrel.OnNewHistoryTarget(historyTarget);
}

View File

@@ -19,6 +19,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
@@ -36,7 +37,7 @@ namespace ArchiSteamFarm.NLog {
// This is NLog config property, it must have public get() and set() capabilities
[PublicAPI]
public string BotName { get; set; }
public string? BotName { get; set; }
// This is NLog config property, it must have public get() and set() capabilities
[PublicAPI]
@@ -71,10 +72,10 @@ namespace ArchiSteamFarm.NLog {
return;
}
Bot bot = null;
Bot? bot = null;
if (!string.IsNullOrEmpty(BotName)) {
bot = Bot.GetBot(BotName);
bot = Bot.GetBot(BotName!);
if (bot?.IsConnectedAndLoggedOn != true) {
return;
@@ -88,15 +89,13 @@ namespace ArchiSteamFarm.NLog {
}
}
private async Task SendGroupMessage(string message, Bot bot = null) {
private async Task SendGroupMessage(string message, Bot? bot = null) {
if (string.IsNullOrEmpty(message)) {
ASF.ArchiLogger.LogNullError(nameof(message));
return;
throw new ArgumentNullException(nameof(message));
}
if (bot == null) {
bot = Bot.Bots.Values.FirstOrDefault(targetBot => targetBot.IsConnectedAndLoggedOn);
bot = Bot.Bots?.Values.FirstOrDefault(targetBot => targetBot.IsConnectedAndLoggedOn);
if (bot == null) {
return;
@@ -108,15 +107,13 @@ namespace ArchiSteamFarm.NLog {
}
}
private async Task SendPrivateMessage(string message, Bot bot = null) {
private async Task SendPrivateMessage(string message, Bot? bot = null) {
if (string.IsNullOrEmpty(message)) {
ASF.ArchiLogger.LogNullError(nameof(message));
return;
throw new ArgumentNullException(nameof(message));
}
if (bot == null) {
bot = Bot.Bots.Values.FirstOrDefault(targetBot => targetBot.IsConnectedAndLoggedOn && (targetBot.SteamID != SteamID));
bot = Bot.Bots?.Values.FirstOrDefault(targetBot => targetBot.IsConnectedAndLoggedOn && (targetBot.SteamID != SteamID));
if (bot == null) {
return;