Skip to content

Commit

Permalink
fix sonarcloud issues
Browse files Browse the repository at this point in the history
  • Loading branch information
snixtho committed Dec 11, 2023
1 parent 8d5da75 commit f993a11
Show file tree
Hide file tree
Showing 22 changed files with 91 additions and 164 deletions.
7 changes: 5 additions & 2 deletions src/EvoSC.Common/Config/Stores/TomlConfigStore.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using Config.Net;
using EvoSC.Common.Themes;
Expand Down Expand Up @@ -71,7 +72,9 @@ private TomlDocument BuildSubDocument(TomlDocument document, Type type, string n

var tomlValue = optionAttr?.DefaultValue ?? property.PropertyType.GetDefaultTypeValue();
if (property.PropertyType == typeof(TextColor))
{
tomlValue = new TextColor(tomlValue.ToString());
}

// get property value
var value = TomletMain.ValueFrom(property.PropertyType,
Expand Down Expand Up @@ -103,13 +106,13 @@ public void Dispose()
if (lastDotIndex > 0 && key.Length > lastDotIndex + 1 && !char.IsAsciiLetterOrDigit(key[lastDotIndex + 1]))
{
var value = _document.GetValue(key[..lastDotIndex]) as TomlArray;
return value.Count.ToString();
return value.Count.ToString(CultureInfo.InvariantCulture);
}

if (key.EndsWith("]", StringComparison.Ordinal))
{
var indexStart = key.IndexOf("[", StringComparison.Ordinal);
var index = int.Parse(key[(indexStart + 1)..^1]);
var index = int.Parse(key[(indexStart + 1)..^1], CultureInfo.InvariantCulture);
var value = _document.GetValue(key[..indexStart]) as TomlArray;

return value?.Skip(index)?.FirstOrDefault()?.StringValue;
Expand Down
4 changes: 2 additions & 2 deletions src/EvoSC.Common/Interfaces/Themes/IThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ public Task AddThemeAsync<TTheme>(Guid moduleId) where TTheme : Theme<TTheme>
/// </summary>
/// <param name="name">Name of the theme to remove.</param>
/// <returns></returns>
public Task RemoveTheme(string name);
public Task RemoveThemeAsync(string name);

/// <summary>
/// Remove all themes from a module.
/// </summary>
/// <param name="moduleId">ID of the module to remove themes from.</param>
/// <returns></returns>
public Task RemoveThemesForModule(Guid moduleId);
public Task RemoveThemesForModuleAsync(Guid moduleId);

/// <summary>
/// Activate a theme. This will replace existing themes which this theme will
Expand Down
20 changes: 6 additions & 14 deletions src/EvoSC.Common/Services/ServiceContainerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,20 +183,12 @@ private void ResolveCoreService(UnregisteredTypeEventArgs e, Guid containerId)
}
}

try
{
_logger.LogTrace(
"Dependencies does not have service '{Service}' for {Container}. Will try core services",
e.UnregisteredServiceType,
containerId);

return _app.Services.GetInstance(e.UnregisteredServiceType);
}
catch (ActivationException ex)
{
// _logger.LogError(ex, "Failed to get EvoSC core service");
throw;
}
_logger.LogTrace(
"Dependencies does not have service '{Service}' for {Container}. Will try core services",
e.UnregisteredServiceType,
containerId);

return _app.Services.GetInstance(e.UnregisteredServiceType);
});
}
catch (Exception ex)
Expand Down
8 changes: 4 additions & 4 deletions src/EvoSC.Common/Themes/DynamicThemeOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public object this[string key]

IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

public void Add(KeyValuePair<string, object> item) => throw new NotImplementedException();
public void Add(KeyValuePair<string, object> item) => throw new NotSupportedException();

public void Clear() => _options.Clear();

public bool Contains(KeyValuePair<string, object> item) => throw new NotImplementedException();
public bool Contains(KeyValuePair<string, object> item) => throw new NotSupportedException();

public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) => throw new NotImplementedException();
public void CopyTo(KeyValuePair<string, object>[] array, int arrayIndex) => throw new NotSupportedException();

public bool Remove(KeyValuePair<string, object> item) => throw new NotImplementedException();
public bool Remove(KeyValuePair<string, object> item) => throw new NotSupportedException();

public int Count => _options.Count;
public bool IsReadOnly => false;
Expand Down
18 changes: 10 additions & 8 deletions src/EvoSC.Common/Themes/ThemeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ public class ThemeManager : IThemeManager
private readonly IEventManager _events;
private readonly IEvoScBaseConfig _evoscConfig;

private dynamic? _themeOptionsCache = null;
private Dictionary<string, string>? _componentReplacementsCache = null;
private dynamic? _themeOptionsCache;
private Dictionary<string, string>? _componentReplacementsCache;

private Dictionary<string, IThemeInfo> _availableThemes = new();
private Dictionary<Type, ITheme> _activeThemes = new();
private readonly Dictionary<string, IThemeInfo> _availableThemes = new();
private readonly Dictionary<Type, ITheme> _activeThemes = new();

public IEnumerable<IThemeInfo> AvailableThemes => _availableThemes.Values;
public dynamic Theme => _themeOptionsCache ?? GetCurrentThemeOptions();
Expand Down Expand Up @@ -71,7 +71,7 @@ public async Task AddThemeAsync(Type themeType, Guid moduleId)

public Task AddThemeAsync(Type themeType) => AddThemeAsync(themeType, Guid.Empty);

public async Task RemoveTheme(string name)
public async Task RemoveThemeAsync(string name)
{
ThrowIfNotExists(name);

Expand All @@ -86,13 +86,13 @@ public async Task RemoveTheme(string name)
}
}

public async Task RemoveThemesForModule(Guid moduleId)
public async Task RemoveThemesForModuleAsync(Guid moduleId)
{
foreach (var (name, theme) in _availableThemes)
{
if (theme.ModuleId.Equals(moduleId))
{
await RemoveTheme(name);
await RemoveThemeAsync(name);
}
}
}
Expand Down Expand Up @@ -160,7 +160,9 @@ private Dictionary<string, object> GetConfigOverrideOptions()

foreach (var defaultOption in _evoscConfig.Theme)
{
var key = defaultOption.Key.StartsWith("Theme.") ? defaultOption.Key[6..] : defaultOption.Key;
var key = defaultOption.Key.StartsWith("Theme.", StringComparison.Ordinal)
? defaultOption.Key[6..]
: defaultOption.Key;

themeOptions[key] = defaultOption.Value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/EvoSC.Common/Util/ColorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public static string Lighten(TextColor color, float amount) =>
public static string GrayScale(string hexColor)
{
var luma = Luma(hexColor);
return new Rgb() { R = luma, G = luma, B = luma }
return new Rgb { R = luma, G = luma, B = luma }
.To<Hex>()
.ToString()
.Substring(1);
Expand All @@ -149,7 +149,7 @@ public static string GrayScale(string hexColor)
public static string GrayScale(TextColor color)
{
var luma = Luma(color);
return new Rgb() { R = luma, G = luma, B = luma }
return new Rgb { R = luma, G = luma, B = luma }
.To<Hex>()
.ToString()
.Substring(1);
Expand Down
5 changes: 3 additions & 2 deletions src/EvoSC.Common/Util/TextFormatting/TextColor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Drawing;
using System.Globalization;

namespace EvoSC.Common.Util.TextFormatting;

Expand Down Expand Up @@ -46,7 +47,7 @@ public TextColor(byte r, byte g, byte b)
{
if (r is < 0 or > 0xf || g is < 0 or > 0xf || b is < 0 or > 0xf)
{
throw new ArgumentOutOfRangeException("Invalid RGB colors, must be between 0 and 15.");
throw new InvalidOperationException("Invalid RGB colors, must be between 0 and 15.");
}

_r = r;
Expand All @@ -61,7 +62,7 @@ public TextColor(byte r, byte g, byte b)
/// <returns></returns>
private static string ToHex(byte n) => n switch
{
>= 0 and <= 9 => n.ToString(),
>= 0 and <= 9 => n.ToString(CultureInfo.InvariantCulture),
10 => "a",
11 => "b",
12 => "c",
Expand Down
4 changes: 2 additions & 2 deletions src/EvoSC.Manialinks/ManialinkController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ private void AddModelValidationResult(ValidationResult? validationResult)
{
Name = validationResult.MemberNames.FirstOrDefault() ?? "Invalid Value.",
IsInvalid = false,
Message = validationResult?.ErrorMessage ?? ""
Message = validationResult.ErrorMessage ?? ""
});
}
else
Expand All @@ -185,7 +185,7 @@ private void AddModelValidationResult(ValidationResult? validationResult)
{
Name = validationResult.MemberNames.FirstOrDefault() ?? "",
IsInvalid = true,
Message = validationResult?.ErrorMessage ?? "Invalid Value."
Message = validationResult.ErrorMessage ?? "Invalid Value."
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/EvoSC.Manialinks/ManialinkManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ public async Task PreprocessAllAsync()
}

public void AddGlobalVariable<T>(string name, T value) =>
_engine.GlobalVariables.AddOrUpdate(name, value, (s, o) => value);
_engine.GlobalVariables.AddOrUpdate(name, value, (_, _) => value);

public void RemoveGlobalVariable(string name)
{
Expand Down
20 changes: 10 additions & 10 deletions src/EvoSC.Manialinks/Util/FontManialinkHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public FontManialinkHelper(IThemeManager theme)
/// <exception cref="InvalidOperationException"></exception>
public string ToRegular(string font) => font switch
{
_ when font.StartsWith("GameFont") => "GameFontSemiBold",
_ when font.StartsWith("Rajdhani") => "RajdhaniMono",
_ when font.StartsWith("Oswald") => "Oswald",
_ when font.StartsWith("Roboto") => "RobotoCondensed",
_ when font.StartsWith("GameFont", StringComparison.InvariantCulture) => "GameFontSemiBold",
_ when font.StartsWith("Rajdhani", StringComparison.InvariantCulture) => "RajdhaniMono",
_ when font.StartsWith("Oswald", StringComparison.InvariantCulture) => "Oswald",
_ when font.StartsWith("Roboto", StringComparison.InvariantCulture) => "RobotoCondensed",
_ => throw new InvalidOperationException("Invalid font.")
};

Expand All @@ -59,7 +59,7 @@ _ when font.StartsWith("Roboto") => "RobotoCondensed",
/// <exception cref="InvalidOperationException"></exception>
public string ToThin(string font) => font switch
{
_ when font.StartsWith("GameFont") => "GameFontSemiRegular",
_ when font.StartsWith("GameFont", StringComparison.InvariantCulture) => "GameFontSemiRegular",
_ => ToRegular(font)
};

Expand All @@ -71,8 +71,8 @@ _ when font.StartsWith("GameFont") => "GameFontSemiRegular",
/// <exception cref="InvalidOperationException"></exception>
public string ToBold(string font) => font switch
{
_ when font.StartsWith("GameFont") => "GameFontExtraBold",
_ when font.StartsWith("Roboto") => "RobotoCondensedBold",
_ when font.StartsWith("GameFont", StringComparison.InvariantCulture) => "GameFontExtraBold",
_ when font.StartsWith("Roboto", StringComparison.InvariantCulture) => "RobotoCondensedBold",
_ => ToRegular(font)
};

Expand All @@ -84,7 +84,7 @@ _ when font.StartsWith("Roboto") => "RobotoCondensedBold",
/// <exception cref="InvalidOperationException"></exception>
public string ToExtraBold(string font) => font switch
{
_ when font.StartsWith("GameFont") => "GameFontBlack",
_ when font.StartsWith("GameFont", StringComparison.InvariantCulture) => "GameFontBlack",
_ => ToBold(font)
};

Expand All @@ -96,8 +96,8 @@ _ when font.StartsWith("GameFont") => "GameFontBlack",
/// <exception cref="InvalidOperationException"></exception>
public string ToMono(string font) => font switch
{
_ when font.StartsWith("Rajdhani") => "RajdhaniMono",
_ when font.StartsWith("Oswald") => "OswaldMono",
_ when font.StartsWith("Rajdhani", StringComparison.InvariantCulture) => "RajdhaniMono",
_ when font.StartsWith("Oswald", StringComparison.InvariantCulture) => "OswaldMono",
_ => ToRegular(font)
};
}
9 changes: 5 additions & 4 deletions src/EvoSC.Manialinks/Util/GlobalManialinkUtils.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using EvoSC.Common.Interfaces.Themes;
using System.Globalization;
using EvoSC.Common.Interfaces.Themes;

namespace EvoSC.Manialinks.Util;

public class GlobalManialinkUtils
{
private readonly dynamic _theme;
private readonly GameIcons _icons = new GameIcons();
private readonly GameIcons _icons = new();

public GlobalManialinkUtils(IThemeManager themeManager)
{
Expand All @@ -17,7 +18,7 @@ public GlobalManialinkUtils(IThemeManager themeManager)
/// </summary>
/// <param name="type">Name of the status.</param>
/// <returns></returns>
public string TypeToColorBg(string type) => type.ToLower() switch
public string TypeToColorBg(string type) => type.ToLower(CultureInfo.InvariantCulture) switch
{
"info" => _theme.Teal,
"success" => _theme.Green,
Expand All @@ -33,7 +34,7 @@ public GlobalManialinkUtils(IThemeManager themeManager)
/// </summary>
/// <param name="type">Name of the status.</param>
/// <returns></returns>
public string TypeToIcon(string type) => type.ToLower() switch
public string TypeToIcon(string type) => type.ToLower(CultureInfo.InvariantCulture) switch
{
"info" => _icons.InfoCircle,
"success" => _icons.CheckCircle,
Expand Down
2 changes: 1 addition & 1 deletion src/EvoSC.Modules/ModuleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,7 @@ public async Task DisableAsync(Guid loadId)

private async Task DisableThemesAsync(IModuleLoadContext moduleContext)
{
await _themeManager.RemoveThemesForModule(moduleContext.LoadId);
await _themeManager.RemoveThemesForModuleAsync(moduleContext.LoadId);
}

private async Task StopBackgroundServicesAsync(IModuleLoadContext moduleContext)
Expand Down
5 changes: 3 additions & 2 deletions src/EvoSC/CliCommands/ConfigGeneratorCommand.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Text;
using Config.Net;
Expand Down Expand Up @@ -30,7 +31,7 @@ public async Task ExecuteAsync(
string fileName
)
{
if (formatType.ToUpper() != "ENV")
if (formatType.ToUpper(CultureInfo.InvariantCulture) != "ENV")
{
Console.Error.WriteLine("Invalid output format, supported types: ENV");
return;
Expand Down Expand Up @@ -129,7 +130,7 @@ private void GeneratePropertiesRecursiveEnv(StringBuilder sb, Type type, string?
sb.AppendLine($"# {descAttr.Description}");
}

sb.Append($"EVOSC_{keyName.ToUpper()}");
sb.Append($"EVOSC_{keyName.ToUpper(CultureInfo.InvariantCulture)}");
sb.Append("=");
sb.AppendLine(optionAttr?.DefaultValue?.ToString() ?? "");
sb.AppendLine();
Expand Down
10 changes: 2 additions & 8 deletions src/Modules/CurrentMapModule/Services/CurrentMapService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,16 @@ public class CurrentMapService : ICurrentMapService
private readonly IManialinkManager _manialinkManager;
private readonly IMapRepository _mapRepository;
private readonly IServerClient _client;
private readonly IEvoScBaseConfig _config;
private readonly IWorldRecordService _worldRecordService;
private readonly IThemeManager _themes;

public CurrentMapService(IManialinkManager manialinkManager,
ILogger<CurrentMapService> logger,
IMapRepository mapRepository, IServerClient client, IEvoScBaseConfig config,
IWorldRecordService worldRecordService, IThemeManager themes)
public CurrentMapService(IManialinkManager manialinkManager, ILogger<CurrentMapService> logger,
IMapRepository mapRepository, IServerClient client, IWorldRecordService worldRecordService)
{
_logger = logger;
_manialinkManager = manialinkManager;
_mapRepository = mapRepository;
_client = client;
_config = config;
_worldRecordService = worldRecordService;
_themes = themes;
}

[ExcludeFromCodeCoverage(Justification = "GBXRemoteClient cannot be mocked.")]
Expand Down
Loading

0 comments on commit f993a11

Please sign in to comment.