Skip to content

Commit

Permalink
Changed object to generic
Browse files Browse the repository at this point in the history
  • Loading branch information
Emik03 committed Apr 24, 2021
1 parent a50c264 commit 28379be
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Source/Helpers/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ internal const string
/// </summary>
/// <param name="item">The item to check the type for.</param>
/// <returns><paramref name="item"/> is either <see cref="string"/>, <see cref="IEnumerable"/>, or <see cref="IEnumerator"/>.</returns>
public static bool IsIterator(this object item) => item is string or IEnumerable or IEnumerator;
public static bool IsIterator<T>(this T item) => item is string or IEnumerable or IEnumerator;

/// <summary>
/// Determines if the string is null or empty.
Expand Down Expand Up @@ -329,7 +329,7 @@ public static long BaseToLong(this string value, string baseChars = Binary)
/// </summary>
/// <param name="e">The <see cref="Expression"/> which returns the object you want the name of.</param>
/// <returns>The name of the variable, or if it cannot find it, <see cref="Unknown"/>.</returns>
public static string NameOfVariable(this Expression<Func<object>> e)
public static string NameOfVariable<T>(this Expression<Func<T>> e)
{
try
{
Expand Down Expand Up @@ -405,7 +405,7 @@ public static string LongToBase(this long value, string baseChars = Alphanumeric
/// <param name="getVariables">Whether it should search recursively inside the variable and yield return the elements inside <paramref name="item"/>.</param>
/// <param name="delimiter">The characters in-between each element.</param>
/// <returns>A string consisting of all values from <paramref name="item"/>.</returns>
public static string UnwrapToString(this object item, bool getVariables = false, string delimiter = ", ") => string.Join(delimiter, Unwrap(item, getVariables).Select(o => o.ToString()).ToArray());
public static string UnwrapToString<T>(this T item, bool getVariables = false, string delimiter = ", ") => string.Join(delimiter, Unwrap(item, getVariables).Select(o => o.ToString()).ToArray());

/// <summary>
/// Unwraps any object, whether it be a class, list, tuple, or any other data.
Expand Down Expand Up @@ -465,7 +465,7 @@ public static IEnumerable AsEnumerable(this IEnumerator source)
/// </summary>
/// <param name="source">The item to get all fields and properties.</param>
/// <returns>All fields and properties of <paramref name="source"/>.</returns>
public static IEnumerable<string> GetAllValues(this object source) => source?.GetType()?.GetFields(Helper.Flags).Select(f => $"\n{f} (Field): {f?.GetValue(source).UnwrapToString()}").Concat(source?.GetType()?.GetProperties(Helper.Flags).Select(p => $"\n{p} (Property): {p?.GetValue(source, null).UnwrapToString()}"));
public static IEnumerable<string> GetAllValues<T>(this T source) => source?.GetType()?.GetFields(Flags).Select(f => $"\n{f} (Field): {f?.GetValue(source).UnwrapToString()}").Concat(source?.GetType()?.GetProperties(Flags).Select(p => $"\n{p} (Property): {p?.GetValue(source, null).UnwrapToString()}"));

/// <summary>
/// Unwraps any <see cref="IEnumerable"/> of type <see cref="object"/>, which ends up flattening it as a <see cref="Array"/> of type <see cref="object"/>.
Expand Down
6 changes: 3 additions & 3 deletions Source/Instances/BigInteger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public override int GetHashCode()
/// <param name="min">The minimum value accepted.</param>
/// <param name="max">The maximum value accepted.</param>
/// <returns>True if <paramref name="min"/> is smaller or equal itself and <paramref name="max"/> is greater or equal itself.</returns>
public bool IsInRange(object min, object max) => this >= min && this <= max;
public bool IsInRange<T>(T min, T max) => this >= min && this <= max;

/// <summary>
/// Converts the BigInteger to a <see cref="int"/>.
Expand Down Expand Up @@ -493,7 +493,7 @@ public override int GetHashCode()
/// <exception cref="NegativeNumberException"></exception>
/// <param name="obj">The right-hand side operator.</param>
/// <returns>Itself mod <paramref name="obj"/>.</returns>
public BigInteger Modulo(object obj) => ((this % obj) + obj) % obj;
public BigInteger Modulo<T>(T obj) => ((this % obj) + obj) % obj;

private enum Operator { Add, Subtract, Multiply, Divide, Modulo }

Expand Down Expand Up @@ -691,7 +691,7 @@ private static sbyte[] Multiplication(sbyte[] left, sbyte[] right)

private static sbyte[] InvertConditional(in sbyte[] vs, bool b) => b ? vs.Select(s => (sbyte)(-1 * s)).ToArray() : vs;

private sbyte[] ObjectToBytes(in object obj)
private sbyte[] ObjectToBytes<T>(in T obj)
{
obj.NullCheck("You cannot construct a BigInteger out of null.");

Expand Down
9 changes: 4 additions & 5 deletions Source/MonoBehaviours/ModuleScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ protected virtual void OnTimerTick() { }
/// <exception cref="NullIteratorException"></exception>
protected void Awake()
{
Debug.Log("test" + ModBundleName);
_setActive = () =>
{
if (Get<KMBombInfo>(allowNull: true) is KMBombInfo bombInfo)
Expand All @@ -115,7 +114,7 @@ protected void Awake()
};

_components = new Dictionary<Type, Component[]>() { { typeof(ModuleScript), new[] { this } } };

ModBundleName.NullOrEmptyCheck("The public field \"ModBundleName\" is empty! This means that when compiled it won't be able to run! Please set this field to your Mod ID located at Keep Talking ModKit -> Configure Mod. Refer to this link for more details: https://github.com/Emik03/KeepCoding/wiki/Chapter-2.1:-ModuleScript#version-string");

Module = new ModuleContainer(Get<KMBombModule>(), Get<KMNeedyModule>());
Expand Down Expand Up @@ -174,7 +173,7 @@ public void Dump(bool getVariables = false)
{
int index = 0;

string Format(string name, object value) => Helper.VariableTemplate.Form(index++, name, value?.GetType().ToString() ?? Helper.Null, string.Join(", ", value.Unwrap(getVariables).Select(o => o.ToString()).ToArray()));
string Format<T>(string name, T value) => Helper.VariableTemplate.Form(index++, name, value?.GetType().ToString() ?? Helper.Null, string.Join(", ", value.Unwrap(getVariables).Select(o => o.ToString()).ToArray()));

var type = GetType();
var values = new List<object>();
Expand Down Expand Up @@ -275,15 +274,15 @@ public T[] Cache<T>(Func<T[]> func, bool allowNull = false) where T : Component
/// <exception cref="UnrecognizedValueException"></exception>
/// <param name="message">The message to log.</param>
/// <param name="logType">The type of logging. Different logging types have different icons within the editor.</param>
public void Log(object message, LogType logType = LogType.Log) => GetLogMethod(logType)($"[{Module.ModuleDisplayName} #{ModuleId}] {message.UnwrapToString()}");
public void Log<T>(T message, LogType logType = LogType.Log) => GetLogMethod(logType)($"[{Module.ModuleDisplayName} #{ModuleId}] {message.UnwrapToString()}");

/// <summary>
/// Logs multiple entries, but formats it to be compliant with the Logfile Analyzer.
/// </summary>
/// <exception cref="UnrecognizedValueException"></exception>
/// <param name="message">The message to log.</param>
/// <param name="args">All of the arguments to embed into <paramref name="message"/>.</param>
public void Log(object message, params object[] args) => Log(message.UnwrapToString().Form(args));
public void Log<T>(T message, params object[] args) => Log(message.UnwrapToString().Form(args));

/// <summary>
/// Plays a sound. Requires <see cref="KMAudio"/> to be assigned.
Expand Down
3 changes: 2 additions & 1 deletion Source/MonoBehaviours/TPScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,12 @@ protected IEnumerator OnInteractSequence(KMSelectable[] selectables, float wait,
/// <remarks>
/// You can yield return this to send error messages or interactions by first checking for the condition.
/// </remarks>
/// <typeparam name="TThen">The type of then condition.</typeparam>
/// <param name="condition">The boolean to check.</param>
/// <param name="then">The output to return if <paramref name="condition"/> is true.</param>
/// <param name="otherwise">The output to return if <paramref name="condition"/> is false.</param>
/// <returns><paramref name="then"/> or <paramref name="otherwise"/>, depending on <paramref name="condition"/>.</returns>
protected static object Evaluate(bool condition, object then, object otherwise = null) => condition ? then : otherwise;
protected static object Evaluate<TThen>(bool condition, TThen then, object otherwise = null) => condition ? then : otherwise;

/// <summary>
/// Yield return this to allow you to tell the user why they got a strike if it isn't clear.
Expand Down
2 changes: 1 addition & 1 deletion Source/Statics/PathManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static void LoadLibrary(string bundleFileName, string libraryFileName)
if (IsCached(in current))
return;

SetCache<object>(current, null);
SetCache<string>(current, null);

string path = GetPath(FileFormat.Form(bundleFileName, FileExtensionWindows));

Expand Down

0 comments on commit 28379be

Please sign in to comment.