Пример #1
0
        public static CompiledKontrolFunction BindFunction(Type type, string methodName, string description,
                                                           params Type[] parameterTypes)
        {
            string     name       = BindingGenerator.ToSnakeCase(methodName);
            MethodInfo methodInfo = type.GetMethod(methodName, parameterTypes) ??
                                    throw new ArgumentException($"Method {methodName} not found in {type}");
            List <RealizedParameter> parameters = methodInfo.GetParameters().Select(p =>
                                                                                    new RealizedParameter(p.Name, BindingGenerator.MapNativeType(p.ParameterType),
                                                                                                          BoundDefaultValue.DefaultValueFor(p))).ToList();

            if (methodInfo.ReturnType.IsGenericType &&
                methodInfo.ReturnType.GetGenericTypeDefinition() == typeof(Future <>))
            {
                RealizedType returnType =
                    BindingGenerator.MapNativeType(methodInfo.ReturnType.GetGenericArguments()[0]);

                return(new CompiledKontrolFunction(name, description, true, parameters, returnType, methodInfo));
            }
            else
            {
                RealizedType returnType = BindingGenerator.MapNativeType(methodInfo.ReturnType);

                return(new CompiledKontrolFunction(name, description, false, parameters, returnType, methodInfo));
            }
        }
Пример #2
0
        public static CompiledKontrolConstant BindConstant(Type type, string fieldName, string description)
        {
            string    name      = BindingGenerator.ToSnakeCase(fieldName).ToUpperInvariant();
            FieldInfo fieldInfo = type.GetField(fieldName);
            TO2Type   to2Type   = BindingGenerator.MapNativeType(fieldInfo.FieldType);

            return(new CompiledKontrolConstant(name, description, to2Type, fieldInfo));
        }
Пример #3
0
        public static CompiledKontrolModule BindModule(Type moduleType)
        {
            lock (BoundModules) {
                if (BoundModules.ContainsKey(moduleType))
                {
                    return(BoundModules[moduleType]);
                }

                KSModule ksModule = moduleType.GetCustomAttribute <KSModule>();

                if (ksModule == null)
                {
                    throw new ArgumentException($"Type {moduleType} must have a kSClass attribute");
                }

                Type runtimeType = moduleType;
                List <CompiledKontrolFunction> functions = new List <CompiledKontrolFunction>();
                List <BoundType> types = new List <BoundType>();
                List <CompiledKontrolConstant> constants = new List <CompiledKontrolConstant>();

                while (runtimeType != null && runtimeType != typeof(object))
                {
                    foreach (Type nested in runtimeType.GetNestedTypes(BindingFlags.Public))
                    {
                        if (nested.GetCustomAttribute <KSClass>() != null)
                        {
                            types.Add(BindType(ksModule.Name, nested));
                        }
                    }

                    foreach (BoundType type in types)
                    {
                        LinkType(type);
                    }

                    foreach (FieldInfo field in runtimeType.GetFields(BindingFlags.Public | BindingFlags.Static))
                    {
                        KSConstant ksConstant = field.GetCustomAttribute <KSConstant>();
                        if (ksConstant == null)
                        {
                            continue;
                        }

                        TO2Type to2Type = BindingGenerator.MapNativeType(field.FieldType);

                        constants.Add(new CompiledKontrolConstant(
                                          ksConstant.Name ?? ToSnakeCase(field.Name).ToUpperInvariant(),
                                          NormalizeDescription(ksConstant.Description), to2Type, field));
                    }

                    foreach (MethodInfo method in runtimeType.GetMethods(BindingFlags.Public | BindingFlags.Static))
                    {
                        KSFunction ksFunction = method.GetCustomAttribute <KSFunction>();
                        if (ksFunction == null)
                        {
                            continue;
                        }

                        List <RealizedParameter> parameters = method.GetParameters().Select(p =>
                                                                                            new RealizedParameter(p.Name, MapNativeType(p.ParameterType),
                                                                                                                  BoundDefaultValue.DefaultValueFor(p))).ToList();
                        if (method.ReturnType.IsGenericType &&
                            method.ReturnType.GetGenericTypeDefinition() == typeof(Future <>))
                        {
                            Type         typeArg    = method.ReturnType.GetGenericArguments()[0];
                            RealizedType resultType =
                                typeArg == typeof(object) ? BuiltinType.Unit : MapNativeType(typeArg);
                            functions.Add(new CompiledKontrolFunction(ksFunction.Name ?? ToSnakeCase(method.Name),
                                                                      NormalizeDescription(ksFunction.Description), true, parameters, resultType, method));
                        }
                        else
                        {
                            RealizedType resultType = MapNativeType(method.ReturnType);
                            functions.Add(new CompiledKontrolFunction(ksFunction.Name ?? ToSnakeCase(method.Name),
                                                                      NormalizeDescription(ksFunction.Description), false, parameters, resultType, method));
                        }
                    }

                    runtimeType = runtimeType.BaseType;
                }

                CompiledKontrolModule module = new CompiledKontrolModule(ksModule.Name,
                                                                         NormalizeDescription(ksModule.Description), types.Select(t => (t.localName, t as RealizedType)),
                                                                         constants, functions, new List <CompiledKontrolFunction>());
                BoundModules.Add(moduleType, module);
                return(module);
            }
        }