示例#1
0
        private void RegisterAssemblyTypes(Assembly assembly)
        {
            var types = assembly.GetTypes().Where(x => x.GetCustomAttribute <LunarBindHideAttribute>() == null && x.GetCustomAttribute <LunarBindIgnoreAssemblyAddAttribute>() == null);

            foreach (var type in types)
            {
                if (type.IsEnum)
                {
                    var enumAttr = (LunarBindEnumAttribute)type.GetCustomAttribute(typeof(LunarBindEnumAttribute));
                    if (enumAttr != null)
                    {
                        BindingHelpers.CreateBindEnum(bindItems, enumAttr.Name ?? type.Name, type);
                    }
                }
                else
                {
                    var instantiable = (LunarBindInstanceAttribute)type.GetCustomAttribute(typeof(LunarBindInstanceAttribute));
                    if (instantiable != null)
                    {
                        var constructor = type.GetConstructor(new Type[] { });
                        if (constructor != null)
                        {
                            object instance = constructor.Invoke(new object[] { });
                            RegisterUserDataType(type);
                            var bindObj = BindingHelpers.CreateBindUserObject(bindItems, instantiable.Path, instance);
                            var doc     = type.GetCustomAttribute <LunarBindDocumentationAttribute>()?.Data ?? "";
                            var ex      = type.GetCustomAttribute <LunarBindExampleAttribute>()?.Data ?? "";
                            bindObj.Documentation = doc;
                            bindObj.Example       = ex;


                            //AddGlobalObject(instantiable.Path, instance);
                        }
                        else
                        {
                            //TODO: custom exception
                            throw new Exception($"LunarBind: No public empty constructor found on Type [{type.Name}] with LunarBindInstantiableAttribute");
                        }
                    }

                    var staticAttribute = (LunarBindStaticAttribute)type.GetCustomAttribute(typeof(LunarBindStaticAttribute));
                    if (staticAttribute != null)
                    {
                        RegisterUserDataType(type);
                        BindingHelpers.CreateBindType(bindItems, staticAttribute.Path ?? type.Name, type);
                    }

                    RegisterTypeFuncs(type);
                }
            }
        }
示例#2
0
 /// <summary>
 /// Adds all fields of an enum not marked with <see cref="MoonSharpHiddenAttribute"/> or <see cref="LunarBindHideAttribute"/> to a lua table with the path
 /// </summary>
 /// <typeparam name="T"></typeparam>
 public void AddEnum <T>(string path)
 {
     BindingHelpers.CreateBindEnum(bindItems, path, typeof(T));
 }
示例#3
0
 /// <summary>
 /// Adds an enum to a lua table named typeof(T).Name
 /// </summary>
 /// <typeparam name="T"></typeparam>
 public void AddEnum <T>() where T : Enum
 {
     BindingHelpers.CreateBindEnum(bindItems, typeof(T).Name, typeof(T));
 }