diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-05 16:22:11 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2016-11-05 16:22:11 -0400 |
commit | 4b52245ad11361c7dd1689a3104dab156fe58565 (patch) | |
tree | f720e140d594ffbfe2da1182c491b2ec49473ddd /src/StardewModdingAPI | |
parent | 8d8b640779061a5cd001890c15f6f00a602df463 (diff) | |
download | SMAPI-4b52245ad11361c7dd1689a3104dab156fe58565.tar.gz SMAPI-4b52245ad11361c7dd1689a3104dab156fe58565.tar.bz2 SMAPI-4b52245ad11361c7dd1689a3104dab156fe58565.zip |
restore removed extensions for backwards compatibility
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r-- | src/StardewModdingAPI/Extensions.cs | 112 | ||||
-rw-r--r-- | src/StardewModdingAPI/StardewModdingAPI.csproj | 1 |
2 files changed, 113 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Extensions.cs b/src/StardewModdingAPI/Extensions.cs new file mode 100644 index 00000000..5726e882 --- /dev/null +++ b/src/StardewModdingAPI/Extensions.cs @@ -0,0 +1,112 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Input; + +namespace StardewModdingAPI +{ + public static class Extensions + { + public static Random Random = new Random(); + + public static bool IsKeyDown(this Keys key) + { + return Keyboard.GetState().IsKeyDown(key); + } + + public static Color RandomColour() + { + return new Color(Random.Next(0, 255), Random.Next(0, 255), Random.Next(0, 255)); + } + + [Obsolete("The usage of ToSingular has changed. Please update your call to use ToSingular<T>")] + public static string ToSingular(this IEnumerable ienum, string split = ", ") + { + Log.AsyncR("The usage of ToSingular has changed. Please update your call to use ToSingular<T>"); + return ""; + } + + public static string ToSingular<T>(this IEnumerable<T> ienum, string split = ", ") // where T : class + { + //Apparently Keys[] won't split normally :l + if (typeof(T) == typeof(Keys)) + { + return string.Join(split, ienum.ToArray()); + } + return string.Join(split, ienum); + } + + public static bool IsInt32(this object o) + { + int i; + return int.TryParse(o.ToString(), out i); + } + + public static int AsInt32(this object o) + { + return int.Parse(o.ToString()); + } + + public static bool IsBool(this object o) + { + bool b; + return bool.TryParse(o.ToString(), out b); + } + + public static bool AsBool(this object o) + { + return bool.Parse(o.ToString()); + } + + public static int GetHash(this IEnumerable enumerable) + { + var hash = 0; + foreach (var v in enumerable) + { + hash ^= v.GetHashCode(); + } + return hash; + } + + public static T Cast<T>(this object o) where T : class + { + return o as T; + } + + public static FieldInfo[] GetPrivateFields(this object o) + { + return o.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); + } + + public static FieldInfo GetBaseFieldInfo(this Type t, string name) + { + return t.GetField(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); + } + + public static T GetBaseFieldValue<T>(this Type t, object o, string name) where T : class + { + return t.GetBaseFieldInfo(name).GetValue(o) as T; + } + + public static void SetBaseFieldValue<T>(this Type t, object o, string name, object newValue) where T : class + { + t.GetBaseFieldInfo(name).SetValue(o, newValue as T); + } + + public static string RemoveNumerics(this string st) + { + var s = st; + foreach (var c in s) + { + if (!char.IsLetterOrDigit(c)) + { + s = s.Replace(c.ToString(), ""); + } + } + return s; + } + } +}
\ No newline at end of file diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 0b55a925..5a901e4b 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -195,6 +195,7 @@ <Compile Include="Events\MineEvents.cs" /> <Compile Include="Events\PlayerEvents.cs" /> <Compile Include="Events\TimeEvents.cs" /> + <Compile Include="Extensions.cs" /> <Compile Include="Framework\DeprecationManager.cs" /> <Compile Include="Framework\UpdateHelper.cs" /> <Compile Include="Framework\GitRelease.cs" /> |