From e78c136f52f4dbcbca3809fe9a7c0942c0dadc15 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 5 Nov 2016 16:34:45 -0400 Subject: document extensions --- src/StardewModdingAPI/Extensions.cs | 58 ++++++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Extensions.cs b/src/StardewModdingAPI/Extensions.cs index 5726e882..27278416 100644 --- a/src/StardewModdingAPI/Extensions.cs +++ b/src/StardewModdingAPI/Extensions.cs @@ -8,28 +8,47 @@ using Microsoft.Xna.Framework.Input; namespace StardewModdingAPI { + /// Provides general utility extensions. public static class Extensions { + /********* + ** Accessors + *********/ + /// A pseudo-random number generator. public static Random Random = new Random(); + + /********* + ** Public methods + *********/ + /// Get whether the given key is currently being pressed. + /// The key to check. public static bool IsKeyDown(this Keys key) { return Keyboard.GetState().IsKeyDown(key); } + /// Get a random color. public static Color RandomColour() { - return new Color(Random.Next(0, 255), Random.Next(0, 255), Random.Next(0, 255)); + return new Color(Extensions.Random.Next(0, 255), Extensions.Random.Next(0, 255), Extensions.Random.Next(0, 255)); } + /// Concatenate an enumeration into a delimiter-separated string. + /// The values to concatenate. + /// The value separator. [Obsolete("The usage of ToSingular has changed. Please update your call to use ToSingular")] public static string ToSingular(this IEnumerable ienum, string split = ", ") { - Log.AsyncR("The usage of ToSingular has changed. Please update your call to use ToSingular"); + Log.Error("The usage of ToSingular has changed. Please update your call to use ToSingular"); return ""; } - public static string ToSingular(this IEnumerable ienum, string split = ", ") // where T : class + /// Concatenate an enumeration into a delimiter-separated string. + /// The enumerated value type. + /// The values to concatenate. + /// The value separator. + public static string ToSingular(this IEnumerable ienum, string split = ", ") { //Apparently Keys[] won't split normally :l if (typeof(T) == typeof(Keys)) @@ -39,72 +58,97 @@ namespace StardewModdingAPI return string.Join(split, ienum); } + /// Get whether the value can be parsed as a number. + /// The value. public static bool IsInt32(this object o) { int i; return int.TryParse(o.ToString(), out i); } + /// Get the numeric representation of a value. + /// The value. public static int AsInt32(this object o) { return int.Parse(o.ToString()); } + /// Get whether the value can be parsed as a boolean. + /// The value. public static bool IsBool(this object o) { bool b; return bool.TryParse(o.ToString(), out b); } + /// Get the boolean representation of a value. + /// The value. public static bool AsBool(this object o) { return bool.Parse(o.ToString()); } + /// Get a list hash calculated from the hashes of the values it contains. + /// The values to hash. public static int GetHash(this IEnumerable enumerable) { var hash = 0; foreach (var v in enumerable) - { hash ^= v.GetHashCode(); - } return hash; } + /// Cast a value to the given type. This returns null if the value can't be cast. + /// The type to which to cast. + /// The value. public static T Cast(this object o) where T : class { return o as T; } + /// Get all private types on an object. + /// The object to scan. public static FieldInfo[] GetPrivateFields(this object o) { return o.GetType().GetFields(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); } + /// Get metadata for a private field. + /// The type to scan. + /// The name of the field to find. public static FieldInfo GetBaseFieldInfo(this Type t, string name) { return t.GetField(name, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Static); } + /// Get the value of a private field. + /// The type to scan. + /// The instance for which to get a value. + /// The name of the field to find. public static T GetBaseFieldValue(this Type t, object o, string name) where T : class { return t.GetBaseFieldInfo(name).GetValue(o) as T; } + /// Set the value of a private field. + /// The type to scan. + /// The instance for which to set a value. + /// The name of the field to find. + /// The value to set. public static void SetBaseFieldValue(this Type t, object o, string name, object newValue) where T : class { t.GetBaseFieldInfo(name).SetValue(o, newValue as T); } + /// Get a copy of the string with only alphanumeric characters. (Numbers are not removed, despite the name.) + /// The string to copy. 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; } -- cgit