Skip to content

Commit

Permalink
Moved harmony patches to separate dll
Browse files Browse the repository at this point in the history
Added code analysers and implemented recommended code changes
  • Loading branch information
mipen committed Apr 16, 2020
1 parent 03f6750 commit 6a0af2f
Show file tree
Hide file tree
Showing 36 changed files with 370 additions and 82 deletions.
34 changes: 34 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[*.{cs,vb}]

# IDE0060: Remove unused parameter
dotnet_code_quality_unused_parameters = all:none

# CA1303: Do not pass literals as localized parameters
dotnet_diagnostic.CA1303.severity = none

# CA1031: Do not catch general exception types
dotnet_diagnostic.CA1031.severity = none

# CA2227: Collection properties should be read only
dotnet_diagnostic.CA2227.severity = none

# CA1305: Specify IFormatProvider
dotnet_diagnostic.CA1305.severity = none

# CA1304: Specify CultureInfo
dotnet_diagnostic.CA1304.severity = none

#CA1801:
dotnet_diagnostic.CA1801.severity = none

#CA1822:
dotnet_diagnostic.CA1822.severity = none

# CA1307: Specify StringComparison
dotnet_diagnostic.CA1307.severity = none

# CA3075: Insecure DTD processing in XML
dotnet_diagnostic.CA3075.severity = none

# CA1720: Identifier contains type name
dotnet_diagnostic.CA1720.severity = none
2 changes: 1 addition & 1 deletion Debugging/DebugVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
public static class DebugVars
{
public static bool ShowDebug = false;
public static bool ShowDebug { get; } = false;
}
}
2 changes: 1 addition & 1 deletion Debugging/ModDebug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace ModLib.Debugging
{
public static class ModDebug
{
public static void ShowError(string message, string title="", Exception exception = null)
public static void ShowError(string message, string title = "", Exception exception = null)
{
if (string.IsNullOrWhiteSpace(title))
title = "";
Expand Down
5 changes: 4 additions & 1 deletion ExtensionMethods/ExceptionExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ public static class ExceptionExtensionMethods
{
public static string ToStringFull(this Exception ex)
{
return GetString(ex);
if (ex != null)
return GetString(ex);
else
return "";
}

private static string GetString(Exception ex)
Expand Down
11 changes: 8 additions & 3 deletions ExtensionMethods/IEnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@ public static class IEnumerableExtensions
/// <param name="action"></param>
public static IEnumerable<T> Do<T>(this IEnumerable<T> enumerable, Action<T> action)
{
foreach (var item in enumerable)
action?.Invoke(item);
return enumerable;
if (enumerable != null)
{
foreach (var item in enumerable)
action?.Invoke(item);
return enumerable;
}
else
return null;
}
}
}
5 changes: 4 additions & 1 deletion ExtensionMethods/MBBindingListExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using TaleWorlds.Library;

namespace ModLib
Expand All @@ -7,6 +8,8 @@ public static class MBBindingListExtensions
{
public static void AddRange<T>(this MBBindingList<T> bindingList, List<T> listToAdd)
{
if (listToAdd == null) throw new ArgumentNullException(nameof(listToAdd));
if (bindingList == null) throw new ArgumentNullException(nameof(bindingList));
if (listToAdd.Count == 1)
bindingList.Add(listToAdd[0]);
else if (listToAdd.Count > 0)
Expand Down
7 changes: 5 additions & 2 deletions ExtensionMethods/StackExtension.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ModLib
Expand All @@ -7,11 +8,13 @@ public static class StackExtension
{
public static void AppendToTop<T>(this Stack<T> baseStack, Stack<T> toAppend)
{
if (toAppend == null) throw new ArgumentNullException(nameof(toAppend));
if (baseStack == null) throw new ArgumentNullException(nameof(baseStack));
if (toAppend.Count == 0)
return;

T[] array = toAppend.ToArray();
for (int i = array.Count() - 1; i >= 0; i--)
for (int i = array.Length - 1; i >= 0; i--)
{
baseStack.Push(array[i]);
}
Expand Down
20 changes: 13 additions & 7 deletions ExtensionMethods/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,28 @@ public static class StringExtensions
public static int Count(this string str, char c)
{
int count = 0;
foreach (var ch in str)
if (!string.IsNullOrEmpty(str))
{
if (ch == c) count++;
foreach (var ch in str)
{
if (ch == c) count++;
}
}
return count;
}

public static string Last(this string str)
{
if (str.Length > 0)
if (!string.IsNullOrEmpty(str))
{
int index = str.Length - 1;
return str[index].ToString();
if (str.Length > 0)
{
int index = str.Length - 1;
return str[index].ToString();
}
}
else
return string.Empty;

return string.Empty;
}
}
}
12 changes: 7 additions & 5 deletions FileDatabase/FileDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace ModLib
{
public static class FileDatabase
{
private static readonly string LoadablesFolderName = "Loadables";
private const string LoadablesFolderName = "Loadables";
public static Dictionary<Type, Dictionary<string, ISerialisableFile>> Data { get; } = new Dictionary<Type, Dictionary<string, ISerialisableFile>>();

/// <summary>
Expand Down Expand Up @@ -61,6 +61,7 @@ public static bool SaveToFile(string moduleFolderName, ISerialisableFile sf, Loc
{
try
{
if (sf == null) throw new ArgumentNullException(nameof(sf));
if (string.IsNullOrWhiteSpace(sf.ID))
throw new Exception($"FileDatabase tried to save an object of type {sf.GetType().FullName} but the ID value was null.");
if (string.IsNullOrWhiteSpace(moduleFolderName))
Expand Down Expand Up @@ -104,7 +105,7 @@ public static bool SaveToFile(string moduleFolderName, ISerialisableFile sf, Loc
}
catch (Exception ex)
{
ModDebug.ShowError($"Cannot create the file for type {sf.GetType().FullName} with ID {sf.ID} for module {moduleFolderName}:", "Error saving to file", ex);
ModDebug.ShowError($"Cannot create the file for type {sf?.GetType().FullName} with ID {sf?.ID} for module {moduleFolderName}:", "Error saving to file", ex);
return false;
}
}
Expand Down Expand Up @@ -151,7 +152,7 @@ public static bool DeleteFile(string moduleFolderName, ISerialisableFile sf, Loc
private static void Add(ISerialisableFile loadable)
{
if (loadable == null)
throw new ArgumentNullException("Tried to add something to the FileDatabase Data dictionary that was null");
throw new ArgumentNullException(nameof(loadable), "Tried to add something to the FileDatabase Data dictionary that was null");
if (string.IsNullOrWhiteSpace(loadable.ID))
throw new ArgumentNullException($"Loadable of type {loadable.GetType().ToString()} has missing ID field");

Expand Down Expand Up @@ -233,7 +234,7 @@ private static void LoadAllFiles(string moduleName)

//Loop through any subfolders and load the files in them
string[] subDirs = Directory.GetDirectories(moduleLoadablesPath);
if (subDirs.Count() > 0)
if (subDirs.Any())
{
foreach (var subDir in subDirs)
{
Expand Down Expand Up @@ -275,7 +276,7 @@ private static void LoadAllFiles(string moduleName)
}
}
string[] subfolders = Directory.GetDirectories(modConfigsPath);
if (subfolders.Count() > 0)
if (subfolders.Any())
{
foreach (var subFolder in subfolders)
{
Expand Down Expand Up @@ -305,6 +306,7 @@ private static void LoadAllFiles(string moduleName)
/// <returns>Returns the file name of the given ISerialisableFile, including the file extension.</returns>
public static string GetFileNameFor(ISerialisableFile sf)
{
if (sf == null) throw new ArgumentNullException(nameof(sf));
return $"{sf.GetType().Name}.{sf.ID}.xml";
}

Expand Down
15 changes: 6 additions & 9 deletions FileDatabase/SettingsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using TaleWorlds.Library;

namespace ModLib
{
Expand Down Expand Up @@ -43,7 +42,7 @@ internal List<SettingPropertyGroup> GetSettingPropertyGroups()
let propAttr = p.GetCustomAttribute<SettingPropertyAttribute>(true)
let groupAttr = p.GetCustomAttribute<SettingPropertyGroupAttribute>(true)
where propAttr != null
let groupAttrToAdd = groupAttr == null ? SettingPropertyGroupAttribute.Default : groupAttr
let groupAttrToAdd = groupAttr ?? SettingPropertyGroupAttribute.Default
select new SettingProperty(propAttr, groupAttrToAdd, p, this)).ToList();

//Loop through each property
Expand Down Expand Up @@ -85,8 +84,7 @@ private SettingPropertyGroup GetGroupFor(SettingProperty sp, ICollection<Setting
if (sp.GroupAttribute.GroupName.Contains(SubGroupDelimiter))
{
//Intended group is a sub group. Must find it. First get the top group.
string truncatedGroupName;
string topGroupName = GetTopGroupName(sp.GroupAttribute.GroupName, out truncatedGroupName);
string topGroupName = GetTopGroupName(sp.GroupAttribute.GroupName, out string truncatedGroupName);
SettingPropertyGroup topGroup = groupsList.GetGroup(topGroupName);
if (topGroup == null)
{
Expand All @@ -109,7 +107,7 @@ private SettingPropertyGroup GetGroupFor(SettingProperty sp, ICollection<Setting
return group;
}

private SettingPropertyGroup GetGroupFor(string groupName, ICollection<SettingPropertyGroup> groupsList)
private static SettingPropertyGroup GetGroupFor(string groupName, ICollection<SettingPropertyGroup> groupsList)
{
return groupsList.GetGroup(groupName);
}
Expand All @@ -119,8 +117,7 @@ private SettingPropertyGroup GetGroupForRecursive(string groupName, ICollection<
if (groupName.Contains(SubGroupDelimiter))
{
//Need to go deeper
string truncatedGroupName;
string topGroupName = GetTopGroupName(groupName, out truncatedGroupName);
string topGroupName = GetTopGroupName(groupName, out string truncatedGroupName);
SettingPropertyGroup topGroup = GetGroupFor(topGroupName, groupsList);
if (topGroup == null)
{
Expand All @@ -142,7 +139,7 @@ private SettingPropertyGroup GetGroupForRecursive(string groupName, ICollection<
}
}

private string GetTopGroupName(string groupName, out string truncatedGroupName)
private static string GetTopGroupName(string groupName, out string truncatedGroupName)
{
int index = groupName.IndexOf(SubGroupDelimiter);
string topGroupName = groupName.Substring(0, index);
Expand All @@ -151,7 +148,7 @@ private string GetTopGroupName(string groupName, out string truncatedGroupName)
return topGroupName;
}

private void CheckIsValid(SettingProperty prop)
private static void CheckIsValid(SettingProperty prop)
{
if (!prop.Property.CanRead)
throw new Exception($"Property {prop.Property.Name} in {prop.SettingsInstance.GetType().FullName} must have a getter.");
Expand Down
3 changes: 3 additions & 0 deletions FileDatabase/SettingsDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static List<ModSettingsVM> ModSettingsVMs
/// <returns>Returns true if successful. Returns false if the object's ID key is already present in the SettingsDatabase.</returns>
public static bool RegisterSettings(SettingsBase settings)
{
if (settings == null) throw new ArgumentNullException(nameof(settings));
if (!AllSettingsDict.ContainsKey(settings.ID))
{
AllSettingsDict.Add(settings.ID, settings);
Expand Down Expand Up @@ -68,6 +69,7 @@ public static ISerialisableFile GetSettings(string uniqueID)
/// <returns>Return true if the settings object was saved successfully. Returns false if it failed to save.</returns>
public static bool SaveSettings(SettingsBase settingsInstance)
{
if (settingsInstance == null) throw new ArgumentNullException(nameof(settingsInstance));
return FileDatabase.SaveToFile(settingsInstance.ModuleFolderName, settingsInstance, FileDatabase.Location.Configs);
}

Expand All @@ -78,6 +80,7 @@ public static bool SaveSettings(SettingsBase settingsInstance)
/// <returns>Returns the instance of the new object with default values.</returns>
public static SettingsBase ResetSettingsInstance(SettingsBase settingsInstance)
{
if (settingsInstance == null) throw new ArgumentNullException(nameof(settingsInstance));
string id = settingsInstance.ID;
SettingsBase newObj = (SettingsBase)Activator.CreateInstance(settingsInstance.GetType());
newObj.ID = id;
Expand Down
2 changes: 1 addition & 1 deletion GUI/ViewModels/SettingProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public bool SatisfiesSearch
{
get
{
if (ScreenVM == null || ScreenVM.SearchText == "")
if (ScreenVM == null || string.IsNullOrWhiteSpace(ScreenVM.SearchText))
return true;

return Name.ToLower().Contains(ScreenVM.SearchText.ToLower());
Expand Down
6 changes: 4 additions & 2 deletions GUI/ViewModels/SettingPropertyGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ public bool SatisfiesSearch
{
get
{
if (ScreenVM == null || ScreenVM.SearchText == "")
if (ScreenVM == null || string.IsNullOrWhiteSpace(ScreenVM.SearchText))
return true;
return GroupName.ToLower().Contains(ScreenVM.SearchText.ToLower()) || AnyChildSettingSatisfiesSearch;
else
return GroupName.ToLower().Contains(ScreenVM.SearchText.ToLower()) || AnyChildSettingSatisfiesSearch;
}
}
public bool AnyChildSettingSatisfiesSearch
Expand Down Expand Up @@ -156,6 +157,7 @@ public override void RefreshValues()

public void Add(SettingProperty sp)
{
if (sp == null) throw new ArgumentNullException(nameof(sp));
SettingProperties.Add(sp);
sp.Group = this;

Expand Down
23 changes: 13 additions & 10 deletions GUI/Views/EditValueTextWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public override void HandleInput(IReadOnlyList<int> lastKeysPressed)
{
if (Input.IsKeyDown(InputKey.LeftControl) && Input.IsKeyPressed(InputKey.V))
return;
if (lastKeysPressed == null) return;

if (lastKeysPressed.Count > 0)
{
Expand Down Expand Up @@ -57,17 +58,19 @@ public override void HandleInput(IReadOnlyList<int> lastKeysPressed)
}
base.HandleInput(lastKeysPressed);
float value;
float.TryParse(RealText, out value);
float newVal = value;
if (value > MaxValue)
newVal = MaxValue;
else if (value < MinValue)
newVal = MinValue;
if (newVal != value)
if (float.TryParse(RealText, out value))
{
string format = SettingType == SettingType.Int ? "0" : "0.00";
RealText = newVal.ToString(format);
editableText.SetCursorPosition(0, true);
float newVal = value;
if (value > MaxValue)
newVal = MaxValue;
else if (value < MinValue)
newVal = MinValue;
if (newVal != value)
{
string format = SettingType == SettingType.Int ? "0" : "0.00";
RealText = newVal.ToString(format);
editableText.SetCursorPosition(0, true);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions GUI/Views/ModLibSliderWidget.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public float FinalisedFloatValue
set
{
_finalisedFloatValue = value;
OnPropertyChanged(value, "FinalisedFloatValue");
OnPropertyChanged(value, nameof(FinalisedFloatValue));
}
}
public int FinalisedIntValue
Expand All @@ -30,7 +30,7 @@ public int FinalisedIntValue
set
{
_finalisedIntValue = value;
OnPropertyChanged(value, "FinalisedIntValue");
OnPropertyChanged(value, nameof(FinalisedIntValue));
}
}

Expand Down
6 changes: 6 additions & 0 deletions GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This file is used by Code Analysis to maintain SuppressMessage
// attributes that are applied to this project.
// Project-level suppressions either have no target or are given
// a specific target and scoped to a namespace, type, member, etc.

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Security", "CA2119:Seal methods that satisfy private interfaces", Justification = "<Pending>", Scope = "member", Target = "~P:ModLib.SettingsBase.SubFolder")]
Loading

0 comments on commit 6a0af2f

Please sign in to comment.