LuaRegistrationHelper.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. using System;
  2. using System.Diagnostics.CodeAnalysis;
  3. using System.Reflection;
  4. namespace LuaInterface
  5. {
  6. public static class LuaRegistrationHelper
  7. {
  8. #region Tagged instance methods
  9. /// <summary>
  10. /// Registers all public instance methods in an object tagged with <see cref="LuaGlobalAttribute"/> as Lua global functions
  11. /// </summary>
  12. /// <param name="lua">The Lua VM to add the methods to</param>
  13. /// <param name="o">The object to get the methods from</param>
  14. public static void TaggedInstanceMethods(LuaState lua, object o)
  15. {
  16. #region Sanity checks
  17. if (lua == null) throw new ArgumentNullException("lua");
  18. if (o == null) throw new ArgumentNullException("o");
  19. #endregion
  20. foreach (MethodInfo method in o.GetType().GetMethods(BindingFlags.Instance | BindingFlags.Public))
  21. {
  22. foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), true))
  23. {
  24. if (string.IsNullOrEmpty(attribute.Name))
  25. lua.RegisterFunction(method.Name, o, method); // CLR name
  26. else
  27. lua.RegisterFunction(attribute.Name, o, method); // Custom name
  28. }
  29. }
  30. }
  31. #endregion
  32. #region Tagged static methods
  33. /// <summary>
  34. /// Registers all public static methods in a class tagged with <see cref="LuaGlobalAttribute"/> as Lua global functions
  35. /// </summary>
  36. /// <param name="lua">The Lua VM to add the methods to</param>
  37. /// <param name="type">The class type to get the methods from</param>
  38. public static void TaggedStaticMethods(LuaState lua, Type type)
  39. {
  40. #region Sanity checks
  41. if (lua == null) throw new ArgumentNullException("lua");
  42. if (type == null) throw new ArgumentNullException("type");
  43. if (!type.IsClass) throw new ArgumentException("The type must be a class!", "type");
  44. #endregion
  45. foreach (MethodInfo method in type.GetMethods(BindingFlags.Static | BindingFlags.Public))
  46. {
  47. foreach (LuaGlobalAttribute attribute in method.GetCustomAttributes(typeof(LuaGlobalAttribute), false))
  48. {
  49. if (string.IsNullOrEmpty(attribute.Name))
  50. lua.RegisterFunction(method.Name, null, method); // CLR name
  51. else
  52. lua.RegisterFunction(attribute.Name, null, method); // Custom name
  53. }
  54. }
  55. }
  56. #endregion
  57. #region Enumeration
  58. /// <summary>
  59. /// Registers an enumeration's values for usage as a Lua variable table
  60. /// </summary>
  61. /// <typeparam name="T">The enum type to register</typeparam>
  62. /// <param name="lua">The Lua VM to add the enum to</param>
  63. [SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter", Justification = "The type parameter is used to select an enum type")]
  64. public static void Enumeration<T>(LuaState lua)
  65. {
  66. #region Sanity checks
  67. if (lua == null) throw new ArgumentNullException("lua");
  68. #endregion
  69. Type type = typeof(T);
  70. if (!type.IsEnum) throw new ArgumentException("The type must be an enumeration!");
  71. string[] names = Enum.GetNames(type);
  72. T[] values = (T[])Enum.GetValues(type);
  73. lua.NewTable(type.Name);
  74. for (int i = 0; i < names.Length; i++)
  75. {
  76. string path = type.Name + "." + names[i];
  77. lua[path] = values[i];
  78. }
  79. }
  80. #endregion
  81. }
  82. }