diff options
| author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-04-30 18:55:16 -0400 |
|---|---|---|
| committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-04-30 18:55:16 -0400 |
| commit | 482a91962ac02cf83c2647fd7e5ba8627bd0bb0b (patch) | |
| tree | deb6d10f11185beba973d07e9510f790588071dc | |
| parent | 22806ab900721a61b142937bc58dd33727d377f9 (diff) | |
| parent | d4f172fef160d277d5161d96a26d5174e6fc14ca (diff) | |
| download | SMAPI-482a91962ac02cf83c2647fd7e5ba8627bd0bb0b.tar.gz SMAPI-482a91962ac02cf83c2647fd7e5ba8627bd0bb0b.tar.bz2 SMAPI-482a91962ac02cf83c2647fd7e5ba8627bd0bb0b.zip | |
Merge branch 'develop' into stable
24 files changed, 644 insertions, 135 deletions
diff --git a/release-notes.md b/release-notes.md index d600db57..56b41214 100644 --- a/release-notes.md +++ b/release-notes.md @@ -10,6 +10,21 @@ For mod developers: images). --> +## 1.11 +See [log](https://github.com/Pathoschild/SMAPI/compare/1.10...1.11). + +For players: +* SMAPI now detects issues in `ObjectInformation.xnb` files caused by outdated XNB mods. +* Errors when loading a save are now shown in the SMAPI console. +* Improved console logging performance. +* Fixed errors during game update causing the game to hang. +* Fixed errors due to mod events triggering during game save in Stardew Valley 1.2. + +For mod developers: +* Added a content API which loads custom textures/maps/data from the mod's folder (`.xnb` or `.png` format) or game content. +* `Console.Out` messages are now written to the log file. +* `Monitor.ExitGameImmediately` now aborts SMAPI initialisation and events more quickly. + ## 1.10 See [log](https://github.com/Pathoschild/SMAPI/compare/1.9...1.10). diff --git a/src/.editorconfig b/src/.editorconfig new file mode 100644 index 00000000..3037884e --- /dev/null +++ b/src/.editorconfig @@ -0,0 +1,58 @@ +# topmost editorconfig +root: true + +########## +## General formatting +## documentation: http://editorconfig.org +########## +[*] +indent_style = space +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + + +########## +## C# formatting +## documentation: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference +## some undocumented settings from: https://github.com/dotnet/roslyn/blob/master/.editorconfig +########## +[*.cs] + +#sort 'system' usings first +dotnet_sort_system_directives_first = true + +# use 'this.' qualifier +dotnet_style_qualification_for_field = true:error +dotnet_style_qualification_for_property = true:error +dotnet_style_qualification_for_method = true:error +dotnet_style_qualification_for_event = true:error + +# use language keywords (like int) instead of type (like Int32) +dotnet_style_predefined_type_for_locals_parameters_members = true:error +dotnet_style_predefined_type_for_member_access = true:error + +# don't use 'var' for language keywords +csharp_style_var_for_built_in_types = false:error + +# suggest modern C# features where simpler +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_explicit_tuple_names = true:suggestion +csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion +csharp_style_pattern_matching_over_as_with_null_check = true:suggestion +csharp_style_conditional_delegate_call = true:suggestion + +# prefer method block bodies +csharp_style_expression_bodied_methods = false:suggestion +csharp_style_expression_bodied_constructors = false:suggestion + +# prefer property expression bodies +csharp_style_expression_bodied_properties = true:suggestion +csharp_style_expression_bodied_indexers = true:suggestion +csharp_style_expression_bodied_accessors = true:suggestion + +# prefer inline out variables +csharp_style_inlined_variable_declaration = true:warning diff --git a/src/GlobalAssemblyInfo.cs b/src/GlobalAssemblyInfo.cs index b591153a..3de78da4 100644 --- a/src/GlobalAssemblyInfo.cs +++ b/src/GlobalAssemblyInfo.cs @@ -2,5 +2,5 @@ using System.Runtime.InteropServices; [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.8.0.0")] -[assembly: AssemblyFileVersion("1.8.0.0")]
\ No newline at end of file +[assembly: AssemblyVersion("1.11.0.0")] +[assembly: AssemblyFileVersion("1.11.0.0")]
\ No newline at end of file diff --git a/src/StardewModdingAPI.sln b/src/StardewModdingAPI.sln index 441b51a9..57f94648 100644 --- a/src/StardewModdingAPI.sln +++ b/src/StardewModdingAPI.sln @@ -1,7 +1,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26228.4 +VisualStudioVersion = 15.0.26403.7 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrainerMod", "TrainerMod\TrainerMod.csproj", "{28480467-1A48-46A7-99F8-236D95225359}" EndProject @@ -9,6 +9,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StardewModdingAPI", "Starde EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "metadata", "metadata", "{86C452BE-D2D8-45B4-B63F-E329EB06CEDA}" ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig ..\.gitattributes = ..\.gitattributes ..\.gitignore = ..\.gitignore crossplatform.targets = crossplatform.targets diff --git a/src/StardewModdingAPI.sln.DotSettings b/src/StardewModdingAPI.sln.DotSettings index 81b52fd4..06cc66ef 100644 --- a/src/StardewModdingAPI.sln.DotSettings +++ b/src/StardewModdingAPI.sln.DotSettings @@ -1,5 +1,7 @@ <wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> + <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBeMadeStatic_002ELocal/@EntryIndexedValue">DO_NOT_SHOW</s:String> <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=RedundantNameQualifier/@EntryIndexedValue">HINT</s:String> + <s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=RedundantTypeArgumentsOfMethod/@EntryIndexedValue">HINT</s:String> <s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/StaticQualifier/STATIC_MEMBERS_QUALIFY_MEMBERS/@EntryValue">Field, Property, Event, Method</s:String> <s:String x:Key="/Default/CodeStyle/CodeFormatting/CSharpCodeStyle/ThisQualifier/INSTANCE_MEMBERS_QUALIFY_MEMBERS/@EntryValue">Field, Property, Event, Method</s:String> <s:Boolean x:Key="/Default/CodeStyle/CodeFormatting/CSharpFormat/LINE_FEED_AT_FILE_END/@EntryValue">True</s:Boolean> diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 6ba16935..fec634e0 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -20,10 +20,10 @@ namespace StardewModdingAPI ** Properties *********/ /// <summary>The directory path containing the current save's data (if a save is loaded).</summary> - private static string RawSavePath => Constants.IsSaveLoaded ? Path.Combine(Constants.SavesPath, Constants.GetSaveFolderName()) : null; + private static string RawSavePath => Context.IsSaveLoaded ? Path.Combine(Constants.SavesPath, Constants.GetSaveFolderName()) : null; /// <summary>Whether the directory containing the current save's data exists on disk.</summary> - private static bool SavePathReady => Constants.IsSaveLoaded && Directory.Exists(Constants.RawSavePath); + private static bool SavePathReady => Context.IsSaveLoaded && Directory.Exists(Constants.RawSavePath); /********* @@ -33,10 +33,10 @@ namespace StardewModdingAPI ** Public ****/ /// <summary>SMAPI's current semantic version.</summary> - public static ISemanticVersion ApiVersion { get; } = new SemanticVersion(1, 10, 0); + public static ISemanticVersion ApiVersion { get; } = new SemanticVersion(1, 11, 0); /// <summary>The minimum supported version of Stardew Valley.</summary> - public static ISemanticVersion MinimumGameVersion { get; } = new SemanticVersion("1.2.15"); + public static ISemanticVersion MinimumGameVersion { get; } = new SemanticVersion("1.2.26"); /// <summary>The maximum supported version of Stardew Valley.</summary> public static ISemanticVersion MaximumGameVersion { get; } = null; @@ -54,7 +54,7 @@ namespace StardewModdingAPI public static string SavesPath { get; } = Path.Combine(Constants.DataPath, "Saves"); /// <summary>The directory name containing the current save's data (if a save is loaded and the directory exists).</summary> - public static string SaveFolderName => Constants.IsSaveLoaded ? Constants.GetSaveFolderName() : ""; + public static string SaveFolderName => Context.IsSaveLoaded ? Constants.GetSaveFolderName() : ""; /// <summary>The directory path containing the current save's data (if a save is loaded and the directory exists).</summary> public static string CurrentSavePath => Constants.SavePathReady ? Path.Combine(Constants.SavesPath, Constants.GetSaveFolderName()) : ""; @@ -74,9 +74,6 @@ namespace StardewModdingAPI /// <summary>The full path to the folder containing mods.</summary> internal static string ModPath { get; } = Path.Combine(Constants.ExecutionPath, "Mods"); - /// <summary>Whether a player save has been loaded.</summary> - internal static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.name); - /// <summary>The game's current semantic version.</summary> internal static ISemanticVersion GameVersion { get; } = Constants.GetGameVersion(); diff --git a/src/StardewModdingAPI/ContentSource.cs b/src/StardewModdingAPI/ContentSource.cs new file mode 100644 index 00000000..35c8bc21 --- /dev/null +++ b/src/StardewModdingAPI/ContentSource.cs @@ -0,0 +1,12 @@ +namespace StardewModdingAPI +{ + /// <summary>Specifies a source containing content that can be loaded.</summary> + public enum ContentSource + { + /// <summary>Assets in the game's content manager (i.e. XNBs in the game's content folder).</summary> + GameContent, + + /// <summary>XNB files in the current mod's folder.</summary> + ModFolder + } +} diff --git a/src/StardewModdingAPI/Context.cs b/src/StardewModdingAPI/Context.cs new file mode 100644 index 00000000..415b4aac --- /dev/null +++ b/src/StardewModdingAPI/Context.cs @@ -0,0 +1,20 @@ +using StardewValley; + +namespace StardewModdingAPI +{ + /// <summary>Provides information about the current game state.</summary> + internal static class Context + { + /********* + ** Accessors + *********/ + /// <summary>Whether a player save has been loaded.</summary> + public static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.name); + + /// <summary>Whether the game is currently writing to the save file.</summary> + public static bool IsSaving => SaveGame.IsProcessing; + + /// <summary>Whether the game is currently running the draw loop.</summary> + public static bool IsInDrawLoop { get; set; } + } +} diff --git a/src/StardewModdingAPI/Framework/ContentHelper.cs b/src/StardewModdingAPI/Framework/ContentHelper.cs new file mode 100644 index 00000000..762b7e35 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ContentHelper.cs @@ -0,0 +1,233 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.IO; +using System.Linq; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using StardewValley; + +namespace StardewModdingAPI.Framework +{ + /// <summary>Provides an API for loading content assets.</summary> + internal class ContentHelper : IContentHelper + { + /********* + ** Properties + *********/ + /// <summary>SMAPI's underlying content manager.</summary> + private readonly SContentManager ContentManager; + + /// <summary>The absolute path to the mod folder.</summary> + private readonly string ModFolderPath; + + /// <summary>The path to the mod's folder, relative to the game's content folder (e.g. "../Mods/ModName").</summary> + private readonly string ModFolderPathFromContent; + + /// <summary>The friendly mod name for use in errors.</summary> + private readonly string ModName; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="contentManager">SMAPI's underlying content manager.</param> + /// <param name="modFolderPath">The absolute path to the mod folder.</param> + /// <param name="modName">The friendly mod name for use in errors.</param> + public ContentHelper(SContentManager contentManager, string modFolderPath, string modName) + { + this.ContentManager = contentManager; + this.ModFolderPath = modFolderPath; + this.ModName = modName; + this.ModFolderPathFromContent = this.GetRelativePath(contentManager.FullRootDirectory, modFolderPath); + } + + /// <summary>Load content from the game folder or mod folder (if not already cached), and return it. When loading a <c>.png</c> file, this must be called outside the game's draw loop.</summary> + /// <typeparam name="T">The expected data type. The main supported types are <see cref="Texture2D"/> and dictionaries; other types may be supported by the game's content pipeline.</typeparam> + /// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to an XNB file relative to the mod folder.</param> + /// <param name="source">Where to search for a matching content asset.</param> + /// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception> + /// <exception cref="ContentLoadException">The content asset couldn't be loaded (e.g. because it doesn't exist).</exception> + public T Load<T>(string key, ContentSource source) + { + this.AssertValidAssetKeyFormat(key); + try + { + switch (source) + { + case ContentSource.GameContent: + return this.ContentManager.Load<T>(this.StripXnbExtension(key)); + + case ContentSource.ModFolder: + // find content file + key = this.ContentManager.NormalisePathSeparators(key); + FileInfo file = new FileInfo(Path.Combine(this.ModFolderPath, key)); + if (!file.Exists && file.Extension == "") + file = new FileInfo(Path.Combine(this.ModFolderPath, key + ".xnb")); + if (!file.Exists) + throw new ContentLoadException($"There is no file at path '{file.FullName}'."); + + // get underlying asset key + string actualKey = this.GetActualAssetKey(key, source); + + // load content + switch (file.Extension.ToLower()) + { + case ".xnb": + return this.ContentManager.Load<T>(actualKey); + + case ".png": + // validate + if (typeof(T) != typeof(Texture2D)) + throw new ContentLoadException($"Can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'."); + + // try cache + if (this.ContentManager.IsLoaded(actualKey)) + return this.ContentManager.Load<T>(actualKey); + + // fetch & cache + using (FileStream stream = File.OpenRead(file.FullName)) + { + Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream); + texture = this.PremultiplyTransparency(texture); + this.ContentManager.Inject(actualKey, texture); + return (T)(object)texture; + } + + default: + throw new ContentLoadException($"Unknown file extension '{file.Extension}'; must be '.xnb' or '.png'."); + } + + default: + throw new NotSupportedException($"Unknown content source '{source}'."); + } + } + catch (Exception ex) + { + throw new ContentLoadException($"{this.ModName} failed loading content asset '{key}' from {source}.", ex); + } + } + + /// <summary>Get the underlying key in the game's content cache for an asset. This can be used to load custom map tilesheets, but should be avoided when you can use the content API instead. This does not validate whether the asset exists.</summary> + /// <param name="key">The asset key to fetch (if the <paramref name="source"/> is <see cref="ContentSource.GameContent"/>), or the local path to an XNB file relative to the mod folder.</param> + /// <param name="source">Where to search for a matching content asset.</param> + /// <exception cref="ArgumentException">The <paramref name="key"/> is empty or contains invalid characters.</exception> + public string GetActualAssetKey(string key, ContentSource source) + { + switch (source) + { + case ContentSource.GameContent: + return this.ContentManager.NormaliseAssetName(this.StripXnbExtension(key)); + + case ContentSource.ModFolder: + string contentPath = Path.Combine(this.ModFolderPathFromContent, key); + return this.ContentManager.NormaliseAssetName(this.StripXnbExtension(contentPath)); + + default: + throw new NotSupportedException($"Unknown content source '{source}'."); + } + } + + + /********* + ** Private methods + *********/ + /// <summary>Assert that the given key has a valid format.</summary> + /// <param name="key">The asset key to check.</param> + /// <exception cref="ArgumentException">The asset key is empty or contains invalid characters.</exception> + [SuppressMessage("ReSharper", "UnusedParameter.Local", Justification = "Parameter is only used for assertion checks by design.")] + private void AssertValidAssetKeyFormat(string key) + { + if (string.IsNullOrWhiteSpace(key)) + throw new ArgumentException("The asset key or local path is empty."); + if (key.Intersect(Path.GetInvalidPathChars()).Any()) + throw new ArgumentException("The asset key or local path contains invalid characters."); + } + + /// <summary>Strip the .xnb extension from an asset key, since it's assumed by the underlying content manager.</summary> + /// <param name="key">The asset key.</param> + private string StripXnbExtension(string key) + { + if (key.EndsWith(".xnb", StringComparison.InvariantCultureIgnoreCase)) + return key.Substring(0, key.Length - 4); + return key; + } + + /// <summary>Get a directory path relative to a given root.</summary> + /// <param name="rootPath">The root path from which the path should be relative.</param> + /// <param name="targetPath">The target file path.</param> + private string GetRelativePath(string rootPath, string targetPath) + { + // convert to URIs + Uri from = new Uri(rootPath + "/"); + Uri to = new Uri(targetPath + "/"); + if (from.Scheme != to.Scheme) + throw new InvalidOperationException($"Can't get path for '{targetPath}' relative to '{rootPath}'."); + + // get relative path + return Uri.UnescapeDataString(from.MakeRelativeUri(to).ToString()) + .Replace(Path.DirectorySeparatorChar == '/' ? '\\' : '/', Path.DirectorySeparatorChar); // use correct separator for platform + } + + /// <summary>Premultiply a texture's alpha values to avoid transparency issues in the game. This is only possible if the game isn't currently drawing.</summary> + /// <param name="texture">The texture to premultiply.</param> + /// <returns>Returns a premultiplied texture.</returns> + /// <remarks>Based on <a href="https://gist.github.com/Layoric/6255384">code by Layoric</a>.</remarks> + private Texture2D PremultiplyTransparency(Texture2D texture) + { + // validate + if (Context.IsInDrawLoop) + throw new NotSupportedException("Can't load a PNG file while the game is drawing to the screen. Make sure you load content outside the draw loop."); + + // process texture + using (RenderTarget2D renderTarget = new RenderTarget2D(Game1.graphics.GraphicsDevice, texture.Width, texture.Height)) + using (SpriteBatch spriteBatch = new SpriteBatch(Game1.graphics.GraphicsDevice)) + { + //Viewport originalViewport = Game1.graphics.GraphicsDevice.Viewport; + + // create blank slate in render target + Game1.graphics.GraphicsDevice.SetRenderTarget(renderTarget); + Game1.graphics.GraphicsDevice.Clear(Color.Black); + + // multiply each color by the source alpha, and write just the color values into the final texture + spriteBatch.Begin(SpriteSortMode.Immediate, new BlendState + { + ColorDestinationBlend = Blend.Zero, + ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue, + AlphaDestinationBlend = Blend.Zero, + AlphaSourceBlend = Blend.SourceAlpha, + ColorSourceBlend = Blend.SourceAlpha + }); + spriteBatch.Draw(texture, texture.Bounds, Color.White); + spriteBatch.End(); + + // copy the alpha values from the source texture into the final one without multiplying them + spriteBatch.Begin(SpriteSortMode.Immediate, new BlendState + { + ColorWriteChannels = ColorWriteChannels.Alpha, + AlphaDestinationBlend = Blend.Zero, + ColorDestinationBlend = Blend.Zero, + AlphaSourceBlend = Blend.One, + ColorSourceBlend = Blend.One + }); + spriteBatch.Draw(texture, texture.Bounds, Color.White); + spriteBatch.End(); + + // release the GPU + Game1.graphics.GraphicsDevice.SetRenderTarget(null); + //Game1.graphics.GraphicsDevice.Viewport = originalViewport; + + // store data from render target because the RenderTarget2D is volatile + Color[] data = new Color[texture.Width * texture.Height]; + renderTarget.GetData(data); + + // unset texture from graphic device and set modified data back to it + Game1.graphics.GraphicsDevice.Textures[0] = null; + texture.SetData(data); + } + + return texture; + } + } +} diff --git a/src/StardewModdingAPI/Framework/DeprecationManager.cs b/src/StardewModdingAPI/Framework/DeprecationManager.cs index e44cd369..6b95960b 100644 --- a/src/StardewModdingAPI/Framework/DeprecationManager.cs +++ b/src/StardewModdingAPI/Framework/DeprecationManager.cs @@ -78,7 +78,7 @@ namespace StardewModdingAPI.Framework break; default: - throw new NotImplementedException($"Unknown deprecation level '{severity}'"); + throw new NotSupportedException($"Unknown deprecation level '{severity}'"); } } diff --git a/src/StardewModdingAPI/Framework/InternalExtensions.cs b/src/StardewModdingAPI/Framework/InternalExtensions.cs index a2d589ff..5199c72d 100644 --- a/src/StardewModdingAPI/Framework/InternalExtensions.cs +++ b/src/StardewModdingAPI/Framework/InternalExtensions.cs @@ -41,6 +41,14 @@ namespace StardewModdingAPI.Framework foreach (EventHandler handler in handlers.Cast<EventHandler>()) { + // handle SMAPI exiting + if (monitor.IsExiting) + { + monitor.Log($"SMAPI shutting down: aborting {name} event.", LogLevel.Warn); + return; + } + + // raise event try { handler.Invoke(sender, args ?? EventArgs.Empty); @@ -84,21 +92,20 @@ namespace StardewModdingAPI.Framework /// <param name="exception">The error to summarise.</param> public static string GetLogSummary(this Exception exception) { - // type load exception - if (exception is TypeLoadException typeLoadEx) - return $"Failed loading type: {typeLoadEx.TypeName}: {exception}"; - - // reflection type load exception - if (exception is ReflectionTypeLoadException reflectionTypeLoadEx) + switch (exception) { - string summary = exception.ToString(); - foreach (Exception childEx in reflectionTypeLoadEx.LoaderExceptions) - summary += $"\n\n{childEx.GetLogSummary()}"; - return summary; - } + case TypeLoadException ex: + return $"Failed loading type '{ex.TypeName}': {exception}"; + + case ReflectionTypeLoadException ex: + string summary = exception.ToString(); + foreach (Exception childEx in ex.LoaderExceptions) + summary += $"\n\n{childEx.GetLogSummary()}"; + return summary; - // anything else - return exception.ToString(); + default: + return exception.ToString(); + } } /**** diff --git a/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs b/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs index d84671ee..b8f2c34e 100644 --- a/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs +++ b/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs @@ -18,8 +18,8 @@ namespace StardewModdingAPI.Framework.Logging /// <summary>Whether the current console supports color formatting.</summary> public bool SupportsColor { get; } - /// <summary>The event raised when something writes a line to the console directly.</summary> - public event Action<string> OnLineIntercepted; + /// <summary>The event raised when a message is written to the console directly.</summary> + public event Action<string> OnMessageIntercepted; /********* @@ -30,7 +30,7 @@ namespace StardewModdingAPI.Framework.Logging { // redirect output through interceptor this.Output = new InterceptingTextWriter(Console.Out); - this.Output.OnLineIntercepted += line => this.OnLineIntercepted?.Invoke(line); + this.Output.OnMessageIntercepted += line => this.OnMessageIntercepted?.Invoke(line); Console.SetOut(this.Output); // test color support diff --git a/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs b/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs index 14789109..9ca61b59 100644 --- a/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs +++ b/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using System.Text; @@ -9,13 +8,6 @@ namespace StardewModdingAPI.Framework.Logging internal class InterceptingTextWriter : TextWriter { /********* - ** Properties - *********/ - /// <summary>The current line being intercepted.</summary> - private readonly List<char> Line = new List<char>(); - - - /********* ** Accessors *********/ /// <summary>The underlying console output.</summary> @@ -27,8 +19,8 @@ namespace StardewModdingAPI.Framework.Logging /// <summary>Whether to intercept console output.</summary> public bool ShouldIntercept { get; set; } - /// <summary>The event raised when a line of text is intercepted.</summary> - public event Action<string> OnLineIntercepted; + /// <summary>The event raised when a message is written to the console directly.</summary> + public event Action<string> OnMessageIntercepted; /********* @@ -41,39 +33,31 @@ namespace StardewModdingAPI.Framework.Logging this.Out = output; } + /// <summary>Writes a subarray of characters to the text string or stream.</summary> + /// <param name="buffer">The character array to write data from.</param> + /// <param name="index">The character position in the buffer at which to start retrieving data.</param> + /// <param name="count">The number of characters to write.</param> + public override void Write(char[] buffer, int index, int count) + { + if (this.ShouldIntercept) + this.OnMessageIntercepted?.Invoke(new string(buffer, index, count).TrimEnd('\r', '\n')); + else + this.Out.Write(buffer, index, count); + } + /// <summary>Writes a character to the text string or stream.</summary> /// <param name="ch">The character to write to the text stream.</param> + /// <remarks>Console log messages from the game should be caught by <see cref="Write(char[],int,int)"/>. This method passes through anything that bypasses that method for some reason, since it's better to show it to users than hide it from everyone.</remarks> public override void Write(char ch) { - // intercept - if (this.ShouldIntercept) - { - switch (ch) - { - case '\r': - return; - - case '\n': - this.OnLineIntercepted?.Invoke(new string(this.Line.ToArray())); - this.Line.Clear(); - break; - - default: - this.Line.Add(ch); - break; - } - } - - // pass through - else - this.Out.Write(ch); + this.Out.Write(ch); } /// <summary>Releases the unmanaged resources used by the <see cref="T:System.IO.TextWriter" /> and optionally releases the managed resources.</summary> /// <param name="disposing">true to release both managed and unmanaged resources; false to release only unmanaged resources.</param> protected override void Dispose(bool disposing) { - this.OnLineIntercepte |
