Skip to content

Commit

Permalink
Changed library to be mostly CLS-Compliant
Browse files Browse the repository at this point in the history
  • Loading branch information
Emik03 committed Aug 24, 2021
1 parent 382e3f4 commit bc13247
Show file tree
Hide file tree
Showing 22 changed files with 155 additions and 44 deletions.
2 changes: 2 additions & 0 deletions Source/Abstracts/RoutineBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public abstract class RoutineBase : IEnumerable, IEnumerable<Coroutine>
/// <param name="index">The index to index into <see cref="Coroutines"/>.</param>
/// <exception cref="IndexOutOfRangeException">The parameter <paramref name="index"/> is out of range from <see cref="Coroutines"/>.</exception>
/// <returns>The coroutine from <see cref="Coroutines"/>.</returns>
[CLSCompliant(false)]
public Coroutine this[int index] => Coroutines[index];

/// <summary>
Expand Down Expand Up @@ -303,6 +304,7 @@ public abstract class RoutineBase : IEnumerable, IEnumerable<Coroutine>
/// </code>
/// </example>
/// <seealso cref="Routine"/>
[CLSCompliant(false)]
public List<Coroutine> Coroutines { get; private protected set; } = new List<Coroutine>();

/// <summary>
Expand Down
11 changes: 6 additions & 5 deletions Source/Abstracts/WorkBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Threading;
using UnityEngine;

Expand All @@ -9,11 +10,11 @@ namespace KeepCoding.Internal
/// </summary>
public abstract class WorkBase
{
private readonly uint _maximumThreadsActive;
private readonly int _maximumThreadsActive;

internal WorkBase() : this(true, 1) { }
private protected WorkBase() : this(true, 1) { }

internal WorkBase(in bool allowSimultaneousActive, in uint maximumThreadsActive)
private protected WorkBase(in bool allowSimultaneousActive, in int maximumThreadsActive)
{
AllowSimultaneousActive = allowSimultaneousActive;
_maximumThreadsActive = maximumThreadsActive;
Expand All @@ -32,7 +33,7 @@ internal WorkBase(in bool allowSimultaneousActive, in uint maximumThreadsActive)
/// <summary>
/// Counts the number of threads currently active within this class, and all of its overloads.
/// </summary>
public static uint ThreadsActive { get; private protected set; }
public static int ThreadsActive { get; private protected set; }

/// <summary>
/// The thread of the class.
Expand Down
2 changes: 1 addition & 1 deletion Source/Editors/ModuleScriptEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace KeepCoding.Internal
/// <summary>
/// Custom inspector for <see cref="ModuleScript"/>.
/// </summary>
[CanEditMultipleObjects, CustomEditor(typeof(ModuleScript), true)]
[CanEditMultipleObjects, CLSCompliant(false), CustomEditor(typeof(ModuleScript), true)]
public sealed class ModuleScriptEditor : Editor
{
private static string SelfPath => UnescapeDataString(new UriBuilder(GetExecutingAssembly().CodeBase).Path);
Expand Down
5 changes: 3 additions & 2 deletions Source/Editors/TPScriptEditor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Linq;
using UnityEditor;
using static UnityEngine.GUILayout;
Expand All @@ -9,7 +10,7 @@ namespace KeepCoding.Internal
/// <summary>
/// Custom inspector for <see cref="TPScript{TModule}"/>.
/// </summary>
[CanEditMultipleObjects, CustomEditor(typeof(TPScript<>), true)]
[CanEditMultipleObjects, CLSCompliant(false), CustomEditor(typeof(TPScript<>), true)]
public sealed class TPScriptEditor : Editor
{
/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions Source/Global.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using System;

[assembly: CLSCompliant(true)]
8 changes: 8 additions & 0 deletions Source/Helpers/EventHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static class EventHelper
/// <param name="bombInfo">The bomb info instance to assign events to.</param>
/// <param name="onBombExploded">Called when the bomb explodes.</param>
/// <param name="onBombSolved">Called when the bomb is defused.</param>
[CLSCompliant(false)]
public static KMBombInfo Assign(this KMBombInfo bombInfo, Action onBombExploded = null, Action onBombSolved = null)
{
AssertDefault(bombInfo);
Expand All @@ -44,6 +45,7 @@ public static KMBombInfo Assign(this KMBombInfo bombInfo, Action onBombExploded
/// <param name="onActivate">Called when the lights turn on.</param>
/// <param name="onPass">Called when the module is solved.</param>
/// <param name="onStrike">Called when the module strikes.</param>
[CLSCompliant(false)]
public static KMBombModule Assign(this KMBombModule bombModule, Action onActivate = null, Action onPass = null, Action onStrike = null)
{
AssertDefault(bombModule);
Expand All @@ -66,6 +68,7 @@ public static KMBombModule Assign(this KMBombModule bombModule, Action onActivat
/// <param name="onStateChange">Called when the state of the game changes.</param>
/// <param name="onAlarmClockChange">Called when the alarm clock changes state, and passes in whether it's on or off.</param>
/// <param name="onLightsChange">Called when the lights change state, and passes in whether it's on or off.</param>
[CLSCompliant(false)]
public static KMGameInfo Assign(this KMGameInfo gameInfo, Action<State> onStateChange = null, Action<bool> onAlarmClockChange = null, Action<bool> onLightsChange = null)
{
AssertDefault(gameInfo);
Expand All @@ -90,6 +93,7 @@ public static KMGameInfo Assign(this KMGameInfo gameInfo, Action<State> onStateC
/// <param name="onPass">Called when the needy is solved.</param>
/// <param name="onStrike">Called when the needy strikes.</param>
/// <param name="onTimerExpired">Called when the timer runs out of time.</param>
[CLSCompliant(false)]
public static KMNeedyModule Assign(this KMNeedyModule needyModule, Action onActivate = null, Action onNeedyActivation = null, Action onNeedyDeactivation = null, Action onPass = null, Action onStrike = null, Action onTimerExpired = null)
{
AssertDefault(needyModule);
Expand Down Expand Up @@ -126,6 +130,7 @@ public static KMNeedyModule Assign(this KMNeedyModule needyModule, Action onActi
/// <param name="onRight">Called when the right controller stick is pulled while selected.</param>
/// <param name="onSelect">Called whenever the selectable becomes the current selectable.</param>
/// <param name="onUpdateChildren">Called when the selectable updates its children.</param>
[CLSCompliant(false)]
public static KMSelectable Assign(this KMSelectable selectable, bool? overrideReturn = null, Action onCancel = null, Action onDefocus = null, Action onDeselect = null, Action onFocus = null, Action onHighlight = null, Action onHighlightEnded = null, Action onInteract = null, Action onInteractEnded = null, Action<float> onInteractionPunch = null, Action onLeft = null, Action onRight = null, Action onSelect = null, Action<KMSelectable> onUpdateChildren = null)
{
AssertDefault(selectable);
Expand Down Expand Up @@ -173,6 +178,7 @@ public static KMSelectable Assign(this KMSelectable selectable, bool? overrideRe
/// <param name="onRight">Called when the right controller stick is pulled while selected.</param>
/// <param name="onSelect">Called whenever the selectable becomes the current selectable.</param>
/// <param name="onUpdateChildren">Called when the selectable updates its children.</param>
[CLSCompliant(false)]
public static KMSelectable[] Assign(this KMSelectable[] selectable, bool? overrideReturn = null, Action<int> onCancel = null, Action<int> onDefocus = null, Action<int> onDeselect = null, Action<int> onFocus = null, Action<int> onHighlight = null, Action<int> onHighlightEnded = null, Action<int> onInteract = null, Action<int> onInteractEnded = null, Action<int, float> onInteractionPunch = null, Action<int> onLeft = null, Action<int> onRight = null, Action<int> onSelect = null, Action<int, KMSelectable> onUpdateChildren = null) =>
selectable.NullOrEmptyCheck("The array is not populated. Please check your public fields in Unity.").ToArray().Call((s, i) => s.Assign(
overrideReturn,
Expand Down Expand Up @@ -214,6 +220,7 @@ public static KMSelectable[] Assign(this KMSelectable[] selectable, bool? overri
/// <param name="onRight">Called when the right controller stick is pulled while selected.</param>
/// <param name="onSelect">Called whenever the selectable becomes the current selectable.</param>
/// <param name="onUpdateChildren">Called when the selectable updates its children.</param>
[CLSCompliant(false)]
public static KMSelectable[] Assign(this KMSelectable[] selectable, bool? overrideReturn = null, Action<KMSelectable> onCancel = null, Action<KMSelectable> onDefocus = null, Action<KMSelectable> onDeselect = null, Action<KMSelectable> onFocus = null, Action<KMSelectable> onHighlight = null, Action<KMSelectable> onHighlightEnded = null, Action<KMSelectable> onInteract = null, Action<KMSelectable> onInteractEnded = null, Action<KMSelectable, float> onInteractionPunch = null, Action<KMSelectable> onLeft = null, Action<KMSelectable> onRight = null, Action<KMSelectable> onSelect = null, Action<KMSelectable, KMSelectable> onUpdateChildren = null) =>
selectable.NullOrEmptyCheck("The array is not populated. Please check your public fields in Unity.").ToArray().ForEach(s => s.Assign(
overrideReturn,
Expand All @@ -235,6 +242,7 @@ public static KMSelectable[] Assign(this KMSelectable[] selectable, bool? overri
/// Stops all sounds for the entire <see cref="KMAudioRef"/> <see cref="Array"/>.
/// </summary>
/// <param name="audioRefs">The <see cref="KMAudioRef"/> <see cref="Array"/> to mute all sounds from, using <see cref="KMAudioRef.StopSound"/>.</param>
[CLSCompliant(false)]
public static KMAudioRef[] StopSound(this KMAudioRef[] audioRefs) => audioRefs.ForEach(a => a.StopSound());

/// <summary>
Expand Down
25 changes: 21 additions & 4 deletions Source/Helpers/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static class Helper
/// <param name="obj">The game object to search with.</param>
/// <param name="component">The variable to store the component in.</param>
/// <returns>True if a component has been found of type <typeparamref name="T"/> from <paramref name="obj"/>.</returns>
[CLSCompliant(false)]
public static bool HasComponent<T>(this GameObject obj, out T component) where T : Component => (component = obj.GetComponent<T>()) is T;

/// <summary>
Expand Down Expand Up @@ -186,6 +187,7 @@ public static class Helper
/// <param name="min">The minimum value required to return <see langword="true"/>.</param>
/// <param name="max">The maximum value required to return <see langword="true"/>.</param>
/// <returns>True if <paramref name="comparison"/> is more than or equal <paramref name="min"/> and less than or equal <paramref name="max"/>.</returns>
[CLSCompliant(false)]
public static bool IsBetween(this uint comparison, uint min, uint max) => comparison >= min && comparison <= max;

/// <summary>
Expand All @@ -194,6 +196,7 @@ public static class Helper
/// <param name="comparison">The number to use as comparison.</param>
/// <param name="range">The minimum and maximum value required to return <see langword="true"/>.</param>
/// <returns>True if <paramref name="comparison"/> is more than or equal <see cref="Tuple{T}.Item1"/> and less than or equal <see cref="Tuple{T1, T2}.Item2"/>.</returns>
[CLSCompliant(false)]
public static bool IsBetween(this uint comparison, Tuple<uint, uint> range) => comparison >= range.Item1 && comparison <= range.Item2;

/// <summary>
Expand All @@ -203,6 +206,7 @@ public static class Helper
/// <param name="min">The minimum value required to return <see langword="true"/>.</param>
/// <param name="max">The maximum value required to return <see langword="true"/>.</param>
/// <returns>True if <paramref name="comparison"/> is more than or equal <paramref name="min"/> and less than or equal <paramref name="max"/>.</returns>
[CLSCompliant(false)]
public static bool IsBetween(this ulong comparison, ulong min, ulong max) => comparison >= min && comparison <= max;

/// <summary>
Expand All @@ -211,6 +215,7 @@ public static class Helper
/// <param name="comparison">The number to use as comparison.</param>
/// <param name="range">The minimum and maximum value required to return <see langword="true"/>.</param>
/// <returns>True if <paramref name="comparison"/> is more than or equal <see cref="Tuple{T}.Item1"/> and less than or equal <see cref="Tuple{T1, T2}.Item2"/>.</returns>
[CLSCompliant(false)]
public static bool IsBetween(this ulong comparison, Tuple<ulong, ulong> range) => comparison >= range.Item1 && comparison <= range.Item2;

/// <summary>
Expand Down Expand Up @@ -254,6 +259,7 @@ public static class Helper
/// </summary>
/// <param name="kmSelectable">This is required to check the children field.</param>
/// <returns>True if <see cref="KMSelectable.Children"/> is empty.</returns>
[CLSCompliant(false)]
public static bool IsParent(this KMSelectable kmSelectable) => !kmSelectable.Children.IsNullOrEmpty();

/// <summary>
Expand Down Expand Up @@ -332,8 +338,8 @@ public static class Helper
/// Counts the number of members in an enum.
/// </summary>
/// <typeparam name="T">The enum to check the length for.</typeparam>
/// <returns>The number of members in <typeparamref name="T"/>.</returns>
public static int MemberCount<T>() where T : struct, Enum, IConvertible => Enum.GetNames(typeof(T)).Length;
/// <returns>The number of members in <typeparamref name="T"/>.</returns>
public static int MemberCount<T>() where T : struct, Enum => Enum.GetNames(typeof(T)).Length;

/// <summary>
/// Calculates the rem-euclid modulo, which allows negative numbers to be properly calculated.
Expand Down Expand Up @@ -521,12 +527,17 @@ public static long BaseToLong(this string value, string baseChars = Binary)
/// <remarks>
/// This can be useful to prevent a <see cref="TextMesh"/> from going outside its boundaries. A monospaced font is recommended in this case.
/// </remarks>
/// <exception cref="FormatException"></exception>
/// <exception cref="NegativeNumberException"></exception>
/// <param name="condition">The string to insert line breaks with.</param>
/// <param name="maxLineLength">The maximum number of characters in one line.</param>
/// <returns><paramref name="condition"/> with a line break every <paramref name="maxLineLength"/> or less characters.</returns>
public static string InsertNewlines(this string condition, ushort maxLineLength)
public static string InsertNewlines(this string condition, int maxLineLength)
{
if (maxLineLength == 0)
if (maxLineLength < 0)
throw new NegativeNumberException($"{nameof(maxLineLength)} must be a positive integer.");

if (maxLineLength is 0)
throw new FormatException($"{nameof(maxLineLength)} cannot be 0 because that would insert infinite linebreaks for each character.");

condition = condition.NullCheck("Line breaks cannot be inserted in a null string.").Replace('\n', ' ');
Expand Down Expand Up @@ -689,6 +700,7 @@ static string Recursion(IEnumerable enumerable, string join, StringifyFormat for
/// </summary>
/// <param name="logType">The type of method to get.</param>
/// <returns>The log method representing the enum <paramref name="logType"/>.</returns>
[CLSCompliant(false)]
public static Action<object> Method(this LogType logType) => logType switch
{
LogType.Error => LogError,
Expand All @@ -705,6 +717,7 @@ static string Recursion(IEnumerable enumerable, string join, StringifyFormat for
/// <param name="monoBehaviour">The <see cref="MonoBehaviour"/> instance needed to stop coroutines.</param>
/// <param name="coroutines">The <see cref="Coroutine"/>s to stop.</param>
/// <returns>The array of <see cref="Coroutine"/>s given.</returns>
[CLSCompliant(false)]
public static Coroutine[] Stop(this MonoBehaviour monoBehaviour, params Coroutine[] coroutines) => coroutines?.ForEach(c =>
{
if (c is { })
Expand Down Expand Up @@ -964,6 +977,7 @@ public static IEnumerator Flatten(this IEnumerator source, Predicate<IEnumerator
/// </summary>
/// <param name="bombInfo">The instance of <see cref="KMBombInfo"/> needed to get the modules.</param>
/// <returns>A list of unsolved module names.</returns>
[CLSCompliant(false)]
public static List<string> GetUnsolvedModuleIDs(this KMBombInfo bombInfo)
{
List<string> modules = bombInfo.GetSolvableModuleIDs();
Expand All @@ -976,6 +990,7 @@ public static List<string> GetUnsolvedModuleIDs(this KMBombInfo bombInfo)
/// </summary>
/// <param name="bombInfo">The instance of <see cref="KMBombInfo"/> needed to get the modules.</param>
/// <returns>A list of unsolved modules.</returns>
[CLSCompliant(false)]
public static List<string> GetUnsolvedModuleNames(this KMBombInfo bombInfo)
{
List<string> modules = bombInfo.GetSolvableModuleNames();
Expand Down Expand Up @@ -1044,6 +1059,7 @@ public static Tuple<IEnumerable<T>, IEnumerable<T>> SplitBy<T>(this IEnumerable<
/// <param name="obj">The <see cref="Object"/> to do a null check on.</param>
/// <param name="message">The message of the exception.</param>
/// <returns>The component <paramref name="obj"/>.</returns>
[CLSCompliant(false)]
public static T Assert<T>(this T obj, string message = "While asserting for null, the variable ended up null.") where T : Object => obj ? obj : throw new MissingComponentException(message);

/// <summary>
Expand All @@ -1069,6 +1085,7 @@ public static T Call<T>(this T item, Action<T> action)
/// <param name="item">The item to log</param>
/// <param name="logType">The type of logging.</param>
/// <returns>The item <paramref name="item"/>.</returns>
[CLSCompliant(false)]
public static T Call<T>(this T item, LogType logType = LogType.Log) => item.Call(t => logType.Method()(t.Stringify()));

/// <summary>
Expand Down
Loading

0 comments on commit bc13247

Please sign in to comment.