From 650af7ef1ac1957919ab19ec3d06af97792c54f8 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 9 Apr 2022 13:59:56 -0400 Subject: enable nullable annotations in log parser (#837) --- .../Framework/LogParsing/Models/LogMessage.cs | 38 ++++++++++-- .../Framework/LogParsing/Models/LogModInfo.cs | 71 ++++++++++++++++++---- .../Framework/LogParsing/Models/ParsedLog.cs | 20 +++--- 3 files changed, 100 insertions(+), 29 deletions(-) (limited to 'src/SMAPI.Web/Framework/LogParsing/Models') diff --git a/src/SMAPI.Web/Framework/LogParsing/Models/LogMessage.cs b/src/SMAPI.Web/Framework/LogParsing/Models/LogMessage.cs index 57d28755..7a5f32e0 100644 --- a/src/SMAPI.Web/Framework/LogParsing/Models/LogMessage.cs +++ b/src/SMAPI.Web/Framework/LogParsing/Models/LogMessage.cs @@ -1,4 +1,4 @@ -#nullable disable +using System.Diagnostics.CodeAnalysis; namespace StardewModdingAPI.Web.Framework.LogParsing.Models { @@ -9,19 +9,19 @@ namespace StardewModdingAPI.Web.Framework.LogParsing.Models ** Accessors *********/ /// The local time when the log was posted. - public string Time { get; set; } + public string Time { get; } /// The log level. - public LogLevel Level { get; set; } + public LogLevel Level { get; } /// The screen ID in split-screen mode. - public int ScreenId { get; set; } + public int ScreenId { get; } /// The mod name. - public string Mod { get; set; } + public string Mod { get; } /// The log text. - public string Text { get; set; } + public string Text { get; } /// The number of times this message was repeated consecutively. public int Repeated { get; set; } @@ -30,6 +30,32 @@ namespace StardewModdingAPI.Web.Framework.LogParsing.Models public LogSection? Section { get; set; } /// Whether this message is the first one of its section. + [MemberNotNullWhen(true, nameof(LogMessage.Section))] public bool IsStartOfSection { get; set; } + + + /********* + ** Public methods + *********/ + /// Construct an instance/ + /// The local time when the log was posted. + /// The log level. + /// The screen ID in split-screen mode. + /// The mod name. + /// The log text. + /// The number of times this message was repeated consecutively. + /// The section that this log message belongs to. + /// Whether this message is the first one of its section. + public LogMessage(string time, LogLevel level, int screenId, string mod, string text, int repeated = 0, LogSection? section = null, bool isStartOfSection = false) + { + this.Time = time; + this.Level = level; + this.ScreenId = screenId; + this.Mod = mod; + this.Text = text; + this.Repeated = repeated; + this.Section = section; + this.IsStartOfSection = isStartOfSection; + } } } diff --git a/src/SMAPI.Web/Framework/LogParsing/Models/LogModInfo.cs b/src/SMAPI.Web/Framework/LogParsing/Models/LogModInfo.cs index 349312df..a6b9165c 100644 --- a/src/SMAPI.Web/Framework/LogParsing/Models/LogModInfo.cs +++ b/src/SMAPI.Web/Framework/LogParsing/Models/LogModInfo.cs @@ -1,4 +1,4 @@ -#nullable disable +using System.Diagnostics.CodeAnalysis; namespace StardewModdingAPI.Web.Framework.LogParsing.Models { @@ -9,36 +9,81 @@ namespace StardewModdingAPI.Web.Framework.LogParsing.Models ** Accessors *********/ /// The mod name. - public string Name { get; set; } + public string Name { get; } /// The mod author. - public string Author { get; set; } - - /// The update version. - public string UpdateVersion { get; set; } - - /// The update link. - public string UpdateLink { get; set; } + public string Author { get; } /// The mod version. - public string Version { get; set; } + public string Version { get; private set; } /// The mod description. - public string Description { get; set; } + public string Description { get; } + + /// The update version. + public string? UpdateVersion { get; private set; } + + /// The update link. + public string? UpdateLink { get; private set; } /// The name of the mod for which this is a content pack (if applicable). - public string ContentPackFor { get; set; } + public string? ContentPackFor { get; } /// The number of errors logged by this mod. public int Errors { get; set; } /// Whether the mod was loaded into the game. - public bool Loaded { get; set; } + public bool Loaded { get; } /// Whether the mod has an update available. + [MemberNotNullWhen(true, nameof(LogModInfo.UpdateVersion), nameof(LogModInfo.UpdateLink))] public bool HasUpdate => this.UpdateVersion != null && this.Version != this.UpdateVersion; /// Whether the mod is a content pack for another mod. + [MemberNotNullWhen(true, nameof(LogModInfo.ContentPackFor))] public bool IsContentPack => !string.IsNullOrWhiteSpace(this.ContentPackFor); + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// The mod name. + /// The mod author. + /// The mod version. + /// The mod description. + /// The update version. + /// The update link. + /// The name of the mod for which this is a content pack (if applicable). + /// The number of errors logged by this mod. + /// Whether the mod was loaded into the game. + public LogModInfo(string name, string author, string version, string description, string? updateVersion = null, string? updateLink = null, string? contentPackFor = null, int errors = 0, bool loaded = true) + { + this.Name = name; + this.Author = author; + this.Version = version; + this.Description = description; + this.UpdateVersion = updateVersion; + this.UpdateLink = updateLink; + this.ContentPackFor = contentPackFor; + this.Errors = errors; + this.Loaded = loaded; + } + + /// Add an update alert for this mod. + /// The update version. + /// The update link. + public void SetUpdate(string updateVersion, string updateLink) + { + this.UpdateVersion = updateVersion; + this.UpdateLink = updateLink; + } + + /// Override the version number, for cases like SMAPI itself where the version is only known later during parsing. + /// The new mod version. + public void OverrideVersion(string version) + { + this.Version = version; + } } } diff --git a/src/SMAPI.Web/Framework/LogParsing/Models/ParsedLog.cs b/src/SMAPI.Web/Framework/LogParsing/Models/ParsedLog.cs index dae91d84..6951e434 100644 --- a/src/SMAPI.Web/Framework/LogParsing/Models/ParsedLog.cs +++ b/src/SMAPI.Web/Framework/LogParsing/Models/ParsedLog.cs @@ -1,6 +1,5 @@ -#nullable disable - using System; +using System.Diagnostics.CodeAnalysis; namespace StardewModdingAPI.Web.Framework.LogParsing.Models { @@ -14,31 +13,32 @@ namespace StardewModdingAPI.Web.Framework.LogParsing.Models ** Metadata ****/ /// Whether the log file was successfully parsed. + [MemberNotNullWhen(true, nameof(ParsedLog.RawText))] public bool IsValid { get; set; } /// An error message indicating why the log file is invalid. - public string Error { get; set; } + public string? Error { get; set; } /// The raw log text. - public string RawText { get; set; } + public string? RawText { get; set; } /**** ** Log data ****/ /// The SMAPI version. - public string ApiVersion { get; set; } + public string? ApiVersion { get; set; } /// The game version. - public string GameVersion { get; set; } + public string? GameVersion { get; set; } /// The player's operating system. - public string OperatingSystem { get; set; } + public string? OperatingSystem { get; set; } /// The game install path. - public string GamePath { get; set; } + public string? GamePath { get; set; } /// The mod folder path. - public string ModPath { get; set; } + public string? ModPath { get; set; } /// The ISO 8601 timestamp when the log was started. public DateTimeOffset Timestamp { get; set; } @@ -47,6 +47,6 @@ namespace StardewModdingAPI.Web.Framework.LogParsing.Models public LogModInfo[] Mods { get; set; } = Array.Empty(); /// The log messages. - public LogMessage[] Messages { get; set; } + public LogMessage[] Messages { get; set; } = Array.Empty(); } } -- cgit