Skip to content

Commit

Permalink
Added new general extension methods and utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
ManlyMarco committed Apr 18, 2021
1 parent 0ebe67f commit 3df605a
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/Shared.Core/Utilities/CoroutineUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,31 @@ public static IEnumerator StripYields(this IEnumerator coroutine, bool onlyStrip
public static void RunImmediately(this IEnumerator coroutine)
{
coroutine = FlattenCo(coroutine);
while (coroutine.MoveNext()) ;
while (coroutine.MoveNext()) { }
}

/// <summary>
/// Prevent a coroutine from getting stopped by exceptions. Exceptions are caught and logged.
/// Code after the exception is thrown doesn't run up until the next yield. The coroutine continues after the yield then.
/// </summary>
public static IEnumerator PreventFromCrashing(this IEnumerator coroutine)
{
if (coroutine == null)
throw new ArgumentNullException(nameof(coroutine));
while (true)
{
try
{
if (!coroutine.MoveNext())
break;
}
catch (Exception ex)
{
UnityEngine.Debug.LogException(ex);
break;
}
yield return coroutine.Current;
}
}

/// <summary>
Expand Down
70 changes: 70 additions & 0 deletions src/Shared.Core/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,75 @@ public static AssignedAnotherWeights GetAaWeightsBody(this ChaControl ctrl)
return Traverse.Create(ctrl).Field<AssignedAnotherWeights>("aaWeightsBody").Value;
}
#endif

/// <summary>
/// Get value of a property through reflection
/// </summary>
/// <param name="self">Object that has the property</param>
/// <param name="name">Name of the property</param>
/// <param name="value">Value returned by the property</param>
/// <returns>True if the property exists, flase if it doesn't</returns>
public static bool GetPropertyValue(this object self, string name, out object value)
{
var property = Traverse.Create(self).Property(name);
if (property.PropertyExists())
{
value = property.GetValue();
return true;
}
else
{
UnityEngine.Debug.LogWarning($"Property {name} doesn't exist on {self?.GetType().FullName}");
value = null;
return false;
}
}

/// <summary>
/// Set value of a property through reflection
/// </summary>
/// <param name="self">Object that has the property</param>
/// <param name="name">Name of the property</param>
/// <param name="value">Value to be set to the property</param>
/// <returns>True if the property exists, flase if it doesn't</returns>
public static bool SetPropertyValue(this object self, string name, object value)
{
return Traverse.Create(self).Property(name).SetValue(value).PropertyExists();
}

/// <summary>
/// Get value of a field through reflection
/// </summary>
/// <param name="self">Object that has the field</param>
/// <param name="name">Name of the field</param>
/// <param name="value">Value returned by the field</param>
/// <returns>True if the field exists, flase if it doesn't</returns>
public static bool GetFieldValue(this object self, string name, out object value)
{
var field = Traverse.Create(self).Field(name);
if (field.FieldExists())
{
value = field.GetValue();
return true;
}
else
{
UnityEngine.Debug.LogWarning($"Field {name} doesn't exist on {self?.GetType().FullName}");
value = null;
return false;
}
}

/// <summary>
/// Set value of a field through reflection
/// </summary>
/// <param name="self">Object that has the field</param>
/// <param name="name">Name of the property</param>
/// <param name="value">Value to be set to the field</param>
/// <returns>True if the field exists, flase if it doesn't</returns>
public static bool SetFieldValue(this object self, string name, object value)
{
return Traverse.Create(self).Field(name).SetValue(value).FieldExists();
}
}
}
29 changes: 29 additions & 0 deletions src/Shared.Core/Utilities/IgnoreCaseEqualityComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;

namespace KKAPI.MainGame
{
/// <summary>
/// An equality comparer that uses StringComparison.OrdinalIgnoreCase rule
/// </summary>
public class IgnoreCaseEqualityComparer : IEqualityComparer<string>
{
/// <inheritdoc />
public bool Equals(string x, string y)
{
if (x == null || y == null) return false;
return x.Equals(y, StringComparison.OrdinalIgnoreCase);
}

/// <inheritdoc />
public int GetHashCode(string obj)
{
return obj.GetHashCode();
}

/// <summary>
/// Instance of the comparer for use in linq and such
/// </summary>
public static readonly IgnoreCaseEqualityComparer Instance = new IgnoreCaseEqualityComparer();
}
}

0 comments on commit 3df605a

Please sign in to comment.