using System; using System.Diagnostics.CodeAnalysis; using System.Reflection; namespace LuaInterface { public static class LuaRegistrationHelper { #region Tagged instance methods /// /// Registers all public instance methods in an object tagged with as Lua global functions /// /// The Lua VM to add the methods to /// The object to get the methods from public static void TaggedInstanceMethods(LuaState lua, object o) { #region Sanity checks if (lua == null) throw new ArgumentNullException("lua"); if (o == null) throw new ArgumentNullException("o"); #endregion foreach (MethodInfo method in o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public)) { foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), true)) { if (string.IsNullOrEmpty(attribute.Name)) lua.RegisterFunction(method.Name, o, method); // CLR name else lua.RegisterFunction(attribute.Name, o, method); // Custom name } } } #endregion #region Tagged static methods /// /// Registers all public static methods in a class tagged with as Lua global functions /// /// The Lua VM to add the methods to /// The class type to get the methods from public static void TaggedStaticMethods(LuaState lua, Type type) { #region Sanity checks if (lua == null) throw new ArgumentNullException("lua"); if (type == null) throw new ArgumentNullException("type"); if (!type.IsClass) throw new ArgumentException("The type must be a class!", "type"); #endregion foreach (MethodInfo method in type.GetMethods(BindingFlags.Static | BindingFlags.Public)) { foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), false)) { if (string.IsNullOrEmpty(attribute.Name)) lua.RegisterFunction(method.Name, null, method); // CLR name else lua.RegisterFunction(attribute.Name, null, method); // Custom name } } } #endregion #region Enumeration /// /// Registers an enumeration's values for usage as a Lua variable table /// /// The enum type to register /// The Lua VM to add the enum to [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to select an enum type")] public static void Enumeration(LuaState lua) { #region Sanity checks if (lua == null) throw new ArgumentNullException("lua"); #endregion Type type = typeof(T); if (!type.IsEnum) throw new ArgumentException("The type must be an enumeration!"); string[] names = Enum.GetNames(type); T[] values = (T[])Enum.GetValues(type); lua.NewTable(type.Name); for (int i = 0; i < names.Length; i++) { string path = type.Name + "." + names[i]; lua[path] = values[i]; } } #endregion } }