Skip to content

Commit

Permalink
Update based on newly enabled code analysis
Browse files Browse the repository at this point in the history
Code analysis enabled and changes made to satisfy the enabled rules.
fix #1
  • Loading branch information
natsnudasoft committed Jan 25, 2019
1 parent 09d40c0 commit 62e431c
Show file tree
Hide file tree
Showing 46 changed files with 643 additions and 310 deletions.
1 change: 1 addition & 0 deletions CustomDictionary.xml
Expand Up @@ -3,6 +3,7 @@
<Unrecognized>
</Unrecognized>
<Recognized>
<Word>Drawable</Word>
<Word>Egami</Word>
<Word>Natsnudasoft</Word>
</Recognized>
Expand Down
7 changes: 7 additions & 0 deletions src/EgamiFlowScreensaver/AssemblyNLogConfiguration.cs
Expand Up @@ -51,8 +51,15 @@ public AssemblyNLogConfiguration(Assembly configAssembly)
/// containing the configuration.</param>
/// <returns>A <see cref="LoggingConfiguration"/> loaded from an embedded resource.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="configurationName"/> is
/// <see langword="null"/>.</exception>
/// <exception cref="ArgumentException"><paramref name="configurationName"/> is empty.
/// </exception>
public LoggingConfiguration LoadConfiguration(string configurationName)
{
ParameterValidation.IsNotNull(configurationName, nameof(configurationName));
ParameterValidation.IsNotEmpty(configurationName, nameof(configurationName));

using (var configStream = this.configAssembly
.GetManifestResourceStream(configurationName))
using (var configXmlReader = XmlReader.Create(configStream))
Expand Down
82 changes: 36 additions & 46 deletions src/EgamiFlowScreensaver/BackgroundDrawableManager.cs
Expand Up @@ -17,90 +17,75 @@
namespace Natsnudasoft.EgamiFlowScreensaver
{
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Natsnudasoft.EgamiFlowScreensaver.Config;
using Natsnudasoft.NatsnudaLibrary;
using SystemColor = System.Drawing.Color;

/// <summary>
/// Provides a class to manage a <see cref="BackgroundDrawable"/> based on a specified
/// <see cref="ScreensaverConfiguration"/>.
/// Provides a class to manage a <see cref="BackgroundDrawable"/> that will be created by a
/// specified <see cref="IBackgroundDrawableFactory"/>.
/// </summary>
/// <seealso cref="IBackgroundDrawableManager"/>
/// <seealso cref="IDisposable"/>
public sealed class BackgroundDrawableManager : IBackgroundDrawableManager, IDisposable
{
private readonly IServiceProvider serviceProvider;
private readonly IBackgroundDrawableFactory backgroundDrawableFactory;
private BackgroundDrawable backgroundDrawable;

/// <summary>
/// Initializes a new instance of the <see cref="BackgroundDrawableManager"/> class.
/// </summary>
/// <param name="serviceProvider">The service provider for the currently running
/// <see cref="Game"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="serviceProvider"/> is
/// <param name="backgroundDrawableFactory">The factory to use to provide instances of
/// <see cref="BackgroundDrawable"/>.</param>
/// <exception cref="ArgumentNullException"><paramref name="backgroundDrawableFactory"/> is
/// <see langword="null"/>.</exception>
public BackgroundDrawableManager(IServiceProvider serviceProvider)
public BackgroundDrawableManager(IBackgroundDrawableFactory backgroundDrawableFactory)
{
ParameterValidation.IsNotNull(serviceProvider, nameof(serviceProvider));
ParameterValidation.IsNotNull(
backgroundDrawableFactory,
nameof(backgroundDrawableFactory));

this.serviceProvider = serviceProvider;
this.backgroundDrawableFactory = backgroundDrawableFactory;
}

/// <inheritdoc/>
public void Initialize(
ScreensaverConfiguration screensaverConfiguration,
ScreensaverArea screensaverArea)
/// <exception cref="ArgumentNullException"><paramref name="screensaverArea"/> is
/// <see langword="null"/>.</exception>
public void Initialize(ScreensaverArea screensaverArea)
{
switch (screensaverConfiguration.BackgroundMode)
{
case BackgroundMode.Desktop:
var screenCaptureService =
this.serviceProvider.GetService<IScreenCaptureService>();
this.backgroundDrawable = new DesktopBackgroundDrawable(
screenCaptureService,
screensaverArea);
break;
case BackgroundMode.Image:
var textureConverterService =
this.serviceProvider.GetService<ITextureConverterService>();
var imageScaleService = this.serviceProvider.GetService<IImageScaleService>();
this.backgroundDrawable = new ImageBackgroundDrawbale(
screensaverConfiguration.BackgroundImage.ImageFilePath,
textureConverterService,
imageScaleService,
screensaverConfiguration.BackgroundImageScaleMode,
screensaverArea);
break;
case BackgroundMode.SolidColor:
this.backgroundDrawable = new SolidColorBackgroundDrawable(
screensaverConfiguration.BackgroundColor,
screensaverArea);
break;
default:
this.backgroundDrawable = new SolidColorBackgroundDrawable(
SystemColor.SteelBlue,
screensaverArea);
break;
}
ParameterValidation.IsNotNull(screensaverArea, nameof(screensaverArea));

this.backgroundDrawable = this.backgroundDrawableFactory.Create(screensaverArea);
}

/// <inheritdoc/>
/// <exception cref="ArgumentNullException"><paramref name="graphicsDevice"/> is
/// <see langword="null"/>.</exception>
public void LoadContent(GraphicsDevice graphicsDevice)
{
ParameterValidation.IsNotNull(graphicsDevice, nameof(graphicsDevice));

this.CheckInitialized();
this.backgroundDrawable.LoadContent(graphicsDevice);
}

/// <inheritdoc/>
/// <exception cref="ArgumentNullException"><paramref name="graphicsDevice"/> is
/// <see langword="null"/>.</exception>
public void BeforeDraw(GraphicsDevice graphicsDevice)
{
ParameterValidation.IsNotNull(graphicsDevice, nameof(graphicsDevice));

this.CheckInitialized();
this.backgroundDrawable.BeforeDraw(graphicsDevice);
}

/// <inheritdoc/>
/// <exception cref="ArgumentNullException"><paramref name="spriteBatch"/> is
/// <see langword="null"/>.</exception>
public void Draw(SpriteBatch spriteBatch)
{
ParameterValidation.IsNotNull(spriteBatch, nameof(spriteBatch));

this.CheckInitialized();
this.backgroundDrawable.Draw(spriteBatch);
}
Expand All @@ -112,6 +97,11 @@ public void Dispose()
GC.SuppressFinalize(this);
}

[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Microsoft.Naming",
"CA2204:Literals should be spelled correctly",
MessageId = nameof(BackgroundDrawableManager),
Justification = "Exception message describing name of class.")]
private void CheckInitialized()
{
if (this.backgroundDrawable == null)
Expand Down
@@ -1,4 +1,4 @@
// <copyright file="ConfigurationDirLayoutRenderer.cs" company="natsnudasoft">
// <copyright file="ConfigurationDirectoryLayoutRenderer.cs" company="natsnudasoft">
// Copyright (c) Adrian John Dunstan. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -27,35 +27,42 @@ namespace Natsnudasoft.EgamiFlowScreensaver.Config
/// </summary>
/// <seealso cref="LayoutRenderer" />
[LayoutRenderer("ConfigDir")]
public sealed class ConfigurationDirLayoutRenderer : LayoutRenderer
public sealed class ConfigurationDirectoryLayoutRenderer : LayoutRenderer
{
private readonly IConfigurationFileService configFileService;

/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationDirLayoutRenderer"/> class.
/// Initializes a new instance of the <see cref="ConfigurationDirectoryLayoutRenderer"/>
/// class.
/// </summary>
public ConfigurationDirLayoutRenderer()
public ConfigurationDirectoryLayoutRenderer()
: this(new ConfigurationFileService())
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ConfigurationDirLayoutRenderer"/> class.
/// Initializes a new instance of the <see cref="ConfigurationDirectoryLayoutRenderer"/>
/// class.
/// </summary>
/// <param name="configFileService">The configuration file service to use to retrieve the
/// configuration directory.</param>
/// <exception cref="ArgumentNullException"><paramref name="configFileService"/> is
/// <see langword="null"/>.</exception>
public ConfigurationDirLayoutRenderer(IConfigurationFileService configFileService)
public ConfigurationDirectoryLayoutRenderer(IConfigurationFileService configFileService)
{
ParameterValidation.IsNotNull(configFileService, nameof(configFileService));

this.configFileService = configFileService;
}

/// <inheritdoc/>
/// <exception cref="ArgumentNullException"><paramref name="builder"/>, or
/// <paramref name="logEvent"/> is <see langword="null"/>.</exception>
protected override void Append(StringBuilder builder, LogEventInfo logEvent)
{
ParameterValidation.IsNotNull(builder, nameof(builder));
ParameterValidation.IsNotNull(logEvent, nameof(logEvent));

builder.Append(this.configFileService.ConfigPath);
}
}
Expand Down
27 changes: 16 additions & 11 deletions src/EgamiFlowScreensaver/Config/ConfigurationFileService.cs
Expand Up @@ -24,8 +24,6 @@ namespace Natsnudasoft.EgamiFlowScreensaver.Config
using System.Security.AccessControl;
using System.Security.Principal;
using System.Threading;
using NLog;
using Properties;
using ProtoBuf;
using static System.FormattableString;

Expand All @@ -35,7 +33,13 @@ namespace Natsnudasoft.EgamiFlowScreensaver.Config
/// <seealso cref="IConfigurationFileService" />
public sealed class ConfigurationFileService : IConfigurationFileService
{
private static readonly ILogger Logger = LogManager.GetCurrentClassLogger();
private const string ConfigurationBackgroundImageFileName = "background";
private const string ConfigurationFileName = "settings.cfg";
#pragma warning disable CC0021 // Use nameof
private const string ConfigurationFolderName = "EgamiFlowScreensaver";
#pragma warning restore CC0021 // Use nameof
private const string ConfigurationImagesFolderName = "images";

private static readonly TimeSpan ConfigFileTimeout = TimeSpan.FromSeconds(3);
private static readonly Lazy<Mutex> LazyConfigFileMutex =
new Lazy<Mutex>(CreateConfigFileMutex);
Expand All @@ -47,13 +51,13 @@ public ConfigurationFileService()
{
this.ConfigPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
Resources.ConfigurationFolderName);
ConfigurationFolderName);
this.ConfigFilePath = Path.Combine(
this.ConfigPath,
Resources.ConfigurationFileName);
ConfigurationFileName);
this.ConfigImagesPath = Path.Combine(
this.ConfigPath,
Resources.ConfigurationImagesFolderName);
ConfigurationImagesFolderName);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -144,7 +148,7 @@ public void Save(ScreensaverConfiguration screensaverConfiguration)
Directory.CreateDirectory(this.ConfigPath);
this.DeleteOldBackgroundImagesInternal();
var newBackgroundImageFileName =
Resources.ConfigurationBackgroundImageFileName +
ConfigurationBackgroundImageFileName +
Path.GetExtension(backgroundImageItem.OriginalFileName);
var newBackgroundImageFilePath =
Path.Combine(this.ConfigPath, newBackgroundImageFileName);
Expand Down Expand Up @@ -218,6 +222,7 @@ private static void PerformMutuallyExclusiveConfigFileAction(Action action)

private static T PerformMutuallyExclusiveConfigFileAction<T>(Func<T> action)
{
var result = default(T);
if (action != null)
{
var configFileMutex = LazyConfigFileMutex.Value;
Expand All @@ -238,7 +243,7 @@ private static T PerformMutuallyExclusiveConfigFileAction<T>(Func<T> action)
hasHandle = true;
}

return action();
result = action();
}
finally
{
Expand All @@ -249,7 +254,7 @@ private static T PerformMutuallyExclusiveConfigFileAction<T>(Func<T> action)
}
}

return default(T);
return result;
}

private static Mutex CreateConfigFileMutex()
Expand All @@ -270,7 +275,7 @@ private static Mutex CreateConfigFileMutex()
int screensaverImageIndex)
{
var newImageFileName =
$"{screensaverImageIndex:D3}" +
Invariant($"{screensaverImageIndex:D3}") +
Path.GetExtension(screensaverImageItem.OriginalFileName);
var newScreensaverImagePath = Path.Combine(this.ConfigImagesPath, newImageFileName);
File.Copy(screensaverImageItem.ImageFilePath, newScreensaverImagePath, true);
Expand All @@ -283,7 +288,7 @@ private void DeleteOldBackgroundImagesInternal()
{
var configDirectoryInfo = new DirectoryInfo(this.ConfigPath);
var backgroundImageFiles = configDirectoryInfo.EnumerateFiles(
Resources.ConfigurationBackgroundImageFileName + ".*");
ConfigurationBackgroundImageFileName + ".*");
foreach (var backgroundImageFile in backgroundImageFiles)
{
backgroundImageFile.Delete();
Expand Down
22 changes: 9 additions & 13 deletions src/EgamiFlowScreensaver/Config/ConfigurationFilesTempCache.cs
Expand Up @@ -86,12 +86,6 @@ public ConfigurationFilesTempCache()
}

/// <inheritdoc/>
/// <exception cref="IOException">The path to the file is not known, or an error occurred
/// while opening the file.</exception>
/// <exception cref="UnauthorizedAccessException">The caller does not have the required
/// permissions to access the file.</exception>
/// <exception cref="DirectoryNotFoundException">The specified path is invalid (for example,
/// it is on an unmapped drive).</exception>
public void RemoveScreensaverImage(string screensaverImageFilePath)
{
if (TryRemoveTempFilePath(screensaverImageFilePath))
Expand All @@ -105,12 +99,6 @@ public void RemoveScreensaverImage(string screensaverImageFilePath)
}

/// <inheritdoc/>
/// <exception cref="IOException">The path to the file is not known, or an error occurred
/// while opening the file.</exception>
/// <exception cref="UnauthorizedAccessException">The caller does not have the required
/// permissions to access the file.</exception>
/// <exception cref="DirectoryNotFoundException">The specified path is invalid (for example,
/// it is on an unmapped drive).</exception>
public void Clear()
{
this.tempPathsToDelete.RemoveWhere(TryRemoveTempFilePath);
Expand All @@ -137,7 +125,15 @@ private static bool TryRemoveTempFilePath(string tempFilePath)
File.Delete(tempFilePath);
result = true;
}
catch (Exception ex)
catch (DirectoryNotFoundException)
{
result = true;
}
catch (IOException ex)
{
Logger.Warn(ex, Invariant($"Could not delete temp path at '{tempFilePath}'."));
}
catch (UnauthorizedAccessException ex)
{
Logger.Warn(ex, Invariant($"Could not delete temp path at '{tempFilePath}'."));
}
Expand Down
4 changes: 4 additions & 0 deletions src/EgamiFlowScreensaver/Config/ConfigurationForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 62e431c

Please sign in to comment.