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/LogMessageBuilder.cs | 25 ++++---- .../Framework/LogParsing/LogParseException.cs | 2 - src/SMAPI.Web/Framework/LogParsing/LogParser.cs | 54 ++++++++-------- .../Framework/LogParsing/Models/LogMessage.cs | 38 ++++++++++-- .../Framework/LogParsing/Models/LogModInfo.cs | 71 ++++++++++++++++++---- .../Framework/LogParsing/Models/ParsedLog.cs | 20 +++--- 6 files changed, 138 insertions(+), 72 deletions(-) (limited to 'src/SMAPI.Web/Framework/LogParsing') diff --git a/src/SMAPI.Web/Framework/LogParsing/LogMessageBuilder.cs b/src/SMAPI.Web/Framework/LogParsing/LogMessageBuilder.cs index 1b692e63..a1384b8f 100644 --- a/src/SMAPI.Web/Framework/LogParsing/LogMessageBuilder.cs +++ b/src/SMAPI.Web/Framework/LogParsing/LogMessageBuilder.cs @@ -1,6 +1,5 @@ -#nullable disable - using System; +using System.Diagnostics.CodeAnalysis; using System.Text; using StardewModdingAPI.Web.Framework.LogParsing.Models; @@ -13,7 +12,7 @@ namespace StardewModdingAPI.Web.Framework.LogParsing ** Fields *********/ /// The local time when the next log was posted. - public string Time { get; set; } + public string? Time { get; set; } /// The log level for the next log message. public LogLevel Level { get; set; } @@ -22,7 +21,7 @@ namespace StardewModdingAPI.Web.Framework.LogParsing public int ScreenId { get; set; } /// The mod name for the next log message. - public string Mod { get; set; } + public string? Mod { get; set; } /// The text for the next log message. private readonly StringBuilder Text = new(); @@ -32,6 +31,7 @@ namespace StardewModdingAPI.Web.Framework.LogParsing ** Accessors *********/ /// Whether the next log message has been started. + [MemberNotNullWhen(true, nameof(LogMessageBuilder.Time), nameof(LogMessageBuilder.Mod))] public bool Started { get; private set; } @@ -72,19 +72,18 @@ namespace StardewModdingAPI.Web.Framework.LogParsing } /// Get a log message for the accumulated values. - public LogMessage Build() + public LogMessage? Build() { if (!this.Started) return null; - return new LogMessage - { - Time = this.Time, - Level = this.Level, - ScreenId = this.ScreenId, - Mod = this.Mod, - Text = this.Text.ToString() - }; + return new LogMessage( + time: this.Time, + level: this.Level, + screenId: this.ScreenId, + mod: this.Mod, + text: this.Text.ToString() + ); } /// Reset to start a new log message. diff --git a/src/SMAPI.Web/Framework/LogParsing/LogParseException.cs b/src/SMAPI.Web/Framework/LogParsing/LogParseException.cs index 4ee58433..3f815e3e 100644 --- a/src/SMAPI.Web/Framework/LogParsing/LogParseException.cs +++ b/src/SMAPI.Web/Framework/LogParsing/LogParseException.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; namespace StardewModdingAPI.Web.Framework.LogParsing diff --git a/src/SMAPI.Web/Framework/LogParsing/LogParser.cs b/src/SMAPI.Web/Framework/LogParsing/LogParser.cs index 4e61ac95..55272b23 100644 --- a/src/SMAPI.Web/Framework/LogParsing/LogParser.cs +++ b/src/SMAPI.Web/Framework/LogParsing/LogParser.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.IO; @@ -55,7 +53,7 @@ namespace StardewModdingAPI.Web.Framework.LogParsing *********/ /// Parse SMAPI log text. /// The SMAPI log text. - public ParsedLog Parse(string logText) + public ParsedLog Parse(string? logText) { try { @@ -79,8 +77,8 @@ namespace StardewModdingAPI.Web.Framework.LogParsing }; // parse log messages - LogModInfo smapiMod = new() { Name = "SMAPI", Author = "Pathoschild", Description = "", Loaded = true }; - LogModInfo gameMod = new() { Name = "game", Author = "", Description = "", Loaded = true }; + LogModInfo smapiMod = new(name: "SMAPI", author: "Pathoschild", version: "", description: "", loaded: true); + LogModInfo gameMod = new(name: "game", author: "", version: "", description: "", loaded: true); IDictionary> mods = new Dictionary>(); bool inModList = false; bool inContentPackList = false; @@ -103,7 +101,7 @@ namespace StardewModdingAPI.Web.Framework.LogParsing default: if (mods.TryGetValue(message.Mod, out var entries)) { - foreach (var entry in entries) + foreach (LogModInfo entry in entries) entry.Errors++; } break; @@ -133,9 +131,9 @@ namespace StardewModdingAPI.Web.Framework.LogParsing string author = match.Groups["author"].Value; string description = match.Groups["description"].Value; - if (!mods.TryGetValue(name, out List entries)) + if (!mods.TryGetValue(name, out List? entries)) mods[name] = entries = new List(); - entries.Add(new LogModInfo { Name = name, Author = author, Version = version, Description = description, Loaded = true }); + entries.Add(new LogModInfo(name: name, author: author, version: version, description: description, loaded: true)); message.Section = LogSection.ModsList; } @@ -156,9 +154,9 @@ namespace StardewModdingAPI.Web.Framework.LogParsing string description = match.Groups["description"].Value; string forMod = match.Groups["for"].Value; - if (!mods.TryGetValue(name, out List entries)) + if (!mods.TryGetValue(name, out List? entries)) mods[name] = entries = new List(); - entries.Add(new LogModInfo { Name = name, Author = author, Version = version, Description = description, ContentPackFor = forMod, Loaded = true }); + entries.Add(new LogModInfo(name: name, author: author, version: version, description: description, contentPackFor: forMod, loaded: true)); message.Section = LogSection.ContentPackList; } @@ -179,23 +177,19 @@ namespace StardewModdingAPI.Web.Framework.LogParsing if (mods.TryGetValue(name, out var entries)) { - foreach (var entry in entries) - { - entry.UpdateLink = link; - entry.UpdateVersion = version; - } + foreach (LogModInfo entry in entries) + entry.SetUpdate(version, link); } message.Section = LogSection.ModUpdateList; } - else if (message.Level == LogLevel.Alert && this.SmapiUpdatePattern.IsMatch(message.Text)) { Match match = this.SmapiUpdatePattern.Match(message.Text); string version = match.Groups["version"].Value; string link = match.Groups["link"].Value; - smapiMod.UpdateVersion = version; - smapiMod.UpdateLink = link; + + smapiMod.SetUpdate(version, link); } // platform info line @@ -205,7 +199,7 @@ namespace StardewModdingAPI.Web.Framework.LogParsing log.ApiVersion = match.Groups["apiVersion"].Value; log.GameVersion = match.Groups["gameVersion"].Value; log.OperatingSystem = match.Groups["os"].Value; - smapiMod.Version = log.ApiVersion; + smapiMod.OverrideVersion(log.ApiVersion); } // mod path line @@ -215,7 +209,7 @@ namespace StardewModdingAPI.Web.Framework.LogParsing log.ModPath = match.Groups["path"].Value; int lastDelimiterPos = log.ModPath.LastIndexOfAny(new[] { '/', '\\' }); log.GamePath = lastDelimiterPos >= 0 - ? log.ModPath.Substring(0, lastDelimiterPos) + ? log.ModPath[..lastDelimiterPos] : log.ModPath; } @@ -229,7 +223,8 @@ namespace StardewModdingAPI.Web.Framework.LogParsing } // finalize log - gameMod.Version = log.GameVersion; + if (log.GameVersion != null) + gameMod.OverrideVersion(log.GameVersion); log.Mods = new[] { gameMod, smapiMod }.Concat(mods.Values.SelectMany(p => p).OrderBy(p => p.Name)).ToArray(); return log; } @@ -261,7 +256,8 @@ namespace StardewModdingAPI.Web.Framework.LogParsing /// The messages to filter. private IEnumerable CollapseRepeats(IEnumerable messages) { - LogMessage next = null; + LogMessage? next = null; + foreach (LogMessage message in messages) { // new message @@ -282,7 +278,9 @@ namespace StardewModdingAPI.Web.Framework.LogParsing yield return next; next = message; } - yield return next; + + if (next != null) + yield return next; } /// Split a SMAPI log into individual log messages. @@ -295,7 +293,7 @@ namespace StardewModdingAPI.Web.Framework.LogParsing while (true) { // read line - string line = reader.ReadLine(); + string? line = reader.ReadLine(); if (line == null) break; @@ -308,17 +306,17 @@ namespace StardewModdingAPI.Web.Framework.LogParsing { if (builder.Started) { - yield return builder.Build(); + yield return builder.Build()!; builder.Clear(); } - var screenGroup = header.Groups["screen"]; + Group screenGroup = header.Groups["screen"]; builder.Start( time: header.Groups["time"].Value, level: Enum.Parse(header.Groups["level"].Value, ignoreCase: true), screenId: screenGroup.Success ? int.Parse(screenGroup.Value) : 0, // main player is always screen ID 0 mod: header.Groups["modName"].Value, - text: line.Substring(header.Length) + text: line[header.Length..] ); } else @@ -332,7 +330,7 @@ namespace StardewModdingAPI.Web.Framework.LogParsing // end last message if (builder.Started) - yield return builder.Build(); + yield return builder.Build()!; } } } 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