Esempio n. 1
0
        public IEnumerable <StructuralError> TryImportConstants(ModuleContext context)
        {
            if (alias != null)
            {
                return(Enumerable.Empty <StructuralError>());
            }
            IKontrolModule module = context.FindModule(fromModule);

            if (module == null)
            {
                return(new StructuralError(
                           StructuralError.ErrorType.NoSuchModule,
                           $"Module '{fromModule}' not found",
                           Start,
                           End
                           ).Yield());
            }
            foreach (string name in names ?? module.AllConstantNames)
            {
                IKontrolConstant constant = module.FindConstant(name);

                if (constant != null)
                {
                    context.mappedConstants.Add(name, constant);
                }
            }

            return(Enumerable.Empty <StructuralError>());
        }
Esempio n. 2
0
        public IEnumerable <StructuralError> TryImportTypes(ModuleContext context)
        {
            IKontrolModule module = context.FindModule(fromModule);

            if (module == null)
            {
                return(new StructuralError(
                           StructuralError.ErrorType.NoSuchModule,
                           $"Module '{fromModule}' not found",
                           Start,
                           End
                           ).Yield());
            }
            if (alias != null)
            {
                context.moduleAliases.Add(alias, fromModule);
            }
            else
            {
                foreach (string name in (names ?? module.AllTypeNames))
                {
                    TO2Type type = module.FindType(name);

                    if (type != null)
                    {
                        context.mappedTypes.Add(name, type);
                    }
                }
            }

            return(Enumerable.Empty <StructuralError>());
        }
        private static Entrypoint GetEntrypoint(IKontrolModule module, string name, IKSPContext context)
        {
            try {
                IKontrolFunction function = module.FindFunction(name);
                if (function == null || !function.IsAsync)
                {
                    return(null);
                }

                if (function.Parameters.Count == 0)
                {
                    return(_ => (IAnyFuture)function.Invoke(context));
                }

                if (function.Parameters.Count == 1 && function.Parameters[0].type.Name == "ksp::vessel::Vessel")
                {
                    return(vessel =>
                           (IAnyFuture)function.Invoke(context, new KSPVesselModule.VesselAdapter(context, vessel)));
                }

                context.Logger.Error($"GetEntrypoint {name} failed: Invalid parameters {function.Parameters}");
                return(null);
            } catch (Exception e) {
                context.Logger.Error($"GetEntrypoint {name} failed: {e}");
                return(null);
            }
        }
        private static bool HasEntrypoint(IKontrolModule module, string name, bool allowVessel)
        {
            IKontrolFunction function = module.FindFunction(name);

            return(function != null && function.IsAsync &&
                   (function.Parameters.Count == 0 || allowVessel && function.Parameters.Count == 1 &&
                    function.Parameters[0].type.Name == "ksp::vessel::Vessel"));
        }
Esempio n. 5
0
        public static TestResult RunTest(IKontrolModule module, IKontrolFunction testFunction,
                                         TestContextFactory contextFactory)
        {
            TestRunnerContext testContext = contextFactory();

            try {
                testContext.ResetTimeout();
                object testReturn = testFunction.Invoke(testContext);

                switch (testReturn)
                {
                case bool booleanResult when !booleanResult:
                    return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount,
                                          "Returned false", testContext.Messages));

                case IAnyOption option when !option.Defined:
                    return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount,
                                          "Returned None", testContext.Messages));

                case IAnyResult result when !result.Success:
                    return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount,
                                          $"Returned Err({result.ErrorString})", testContext.Messages));

                case IAnyFuture future:
                    ContextHolder.CurrentContext.Value = testContext;
                    for (int i = 0; i < 100; i++)
                    {
                        testContext.IncrYield();
                        testContext.ResetTimeout();
                        IAnyFutureResult result = future.Poll();
                        if (result.IsReady)
                        {
                            return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount,
                                                  testContext.Messages));
                        }
                    }

                    return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount,
                                          "Future did not become ready", testContext.Messages));

                default:
                    return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount,
                                          testContext.Messages));
                }
            } catch (AssertException e) {
                return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount, e.Message,
                                      testContext.Messages));
            } catch (Exception e) {
                Console.Error.WriteLine(e);
                return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount, e,
                                      testContext.Messages));
            } finally {
                ContextHolder.CurrentContext.Value = null;
            }
        }
 public void RegisterModule(IKontrolModule kontrolModule)
 {
     if (modules.ContainsKey(kontrolModule.Name))
     {
         if (!(modules[kontrolModule.Name] is DeclaredKontrolModule))
         {
             throw new ArgumentException($"Module {kontrolModule.Name} is defined twice");
         }
         modules[kontrolModule.Name] = kontrolModule;
     }
     else
     {
         modules.Add(kontrolModule.Name, kontrolModule);
     }
 }
Esempio n. 7
0
        public static void RunTests(IKontrolModule module, ITestReporter reporter, TestContextFactory contextFactory)
        {
            if (!module.TestFunctions.Any())
            {
                return;
            }

            reporter.BeginModule(module.Name);
            foreach (IKontrolFunction testFunction in module.TestFunctions)
            {
                reporter.Report(RunTest(module, testFunction, contextFactory));
            }

            reporter.EndModule(module.Name);
        }
Esempio n. 8
0
        public IEnumerable <StructuralError> TryImportFunctions(ModuleContext context)
        {
            if (alias != null)
            {
                return(Enumerable.Empty <StructuralError>());
            }
            IKontrolModule module = context.FindModule(fromModule);

            if (module == null)
            {
                return(new StructuralError(
                           StructuralError.ErrorType.NoSuchModule,
                           $"Module '{fromModule}' not found",
                           Start,
                           End
                           ).Yield());
            }

            List <StructuralError> errors = new List <StructuralError>();

            foreach (string name in names ?? module.AllFunctionNames)
            {
                if (context.mappedConstants.ContainsKey(name))
                {
                    continue;
                }

                IKontrolFunction function = module.FindFunction(name);

                if (function != null)
                {
                    context.mappedFunctions.Add(name, function);
                }
                else if (!context.mappedTypes.ContainsKey(name))
                {
                    errors.Add(new StructuralError(
                                   StructuralError.ErrorType.InvalidImport,
                                   $"Module '{fromModule}' does not have public member '{name}''",
                                   Start,
                                   End
                                   ));
                }
            }

            return(errors);
        }
Esempio n. 9
0
        KSPMathModule()
        {
            List <RealizedType> types = new List <RealizedType> {
                Vector2Binding.Vector2Type,
                Vector3Binding.Vector3Type,
                DirectionBinding.DirectionType,
                Matrix2x2Binding.Matrix2x2Type
            };

            BindingGenerator.RegisterTypeMapping(typeof(Vector2d), Vector2Binding.Vector2Type);
            BindingGenerator.RegisterTypeMapping(typeof(Vector3d), Vector3Binding.Vector3Type);
            BindingGenerator.RegisterTypeMapping(typeof(Direction), DirectionBinding.DirectionType);
            BindingGenerator.RegisterTypeMapping(typeof(Matrix2x2), Matrix2x2Binding.Matrix2x2Type);

            List <CompiledKontrolConstant> constants = new List <CompiledKontrolConstant>();

            List <CompiledKontrolFunction> functions = new List <CompiledKontrolFunction> {
                Direct.BindFunction(typeof(Vector2Binding), "Vec2", "Create a new 2-dimensional vector", typeof(double),
                                    typeof(double)),
                Direct.BindFunction(typeof(Vector3Binding), "Vec3", "Create a new 3-dimensional vector", typeof(double),
                                    typeof(double), typeof(double)),
                Direct.BindFunction(typeof(DirectionBinding), "Euler", "Create a Direction from euler angles in degree",
                                    typeof(double), typeof(double), typeof(double)),
                Direct.BindFunction(typeof(DirectionBinding), "AngleAxis",
                                    "Create a Direction from a given axis with rotation angle in degree", typeof(double),
                                    typeof(Vector3d)),
                Direct.BindFunction(typeof(DirectionBinding), "FromVectorToVector",
                                    "Create a Direction to rotate from one vector to another", typeof(Vector3d), typeof(Vector3d)),
                Direct.BindFunction(typeof(DirectionBinding), "LookDirUp",
                                    "Create a Direction from a fore-vector and an up-vector", typeof(Vector3d), typeof(Vector3d)),
                Direct.BindFunction(typeof(ExtraMath), "AngleDelta",
                                    "Calculate the difference between to angles in degree (-180 .. 180)", typeof(double),
                                    typeof(double)),
                Direct.BindFunction(typeof(Matrix2x2Binding), "Matrix2x2", "Create a new 2-dimensional matrix",
                                    typeof(double), typeof(double), typeof(double), typeof(double))
            };

            module = Direct.BindModule(ModuleName, "Collection of KSP/Unity related mathematical functions.", types,
                                       constants, functions);
        }
 public static bool HasKSCEntrypoint(this IKontrolModule module) => HasEntrypoint(module, MainKsc, false);
 public static bool IsBootFlightEntrypointFor(this IKontrolModule module, Vessel vessel) =>
 vessel != null && module.Name.ToLowerInvariant() == "boot::vessels::" + vessel.vesselName.ToLowerInvariant().Replace(' ', '_') &&
 HasEntrypoint(module, MainFlight, true);
Esempio n. 12
0
 public static bool IsModuleEmpty(IKontrolModule module) => !module.AllConstantNames.Any() &&
 !module.AllFunctionNames.Any() &&
 !module.AllTypeNames.Any();
 public static bool HasFlightEntrypoint(this IKontrolModule module) => HasEntrypoint(module, MainFlight, true);
 public static Entrypoint GetFlightEntrypoint(this IKontrolModule module, IKSPContext context) =>
 GetEntrypoint(module, MainFlight, context);
 public static bool HasTrackingEntrypoint(this IKontrolModule module) =>
 HasEntrypoint(module, MainTracking, false);
 public static Entrypoint GetTrackingEntrypoint(this IKontrolModule module, IKSPContext context) =>
 GetEntrypoint(module, MainTracking, context);
 public static Entrypoint GetEditorEntrypoint(this IKontrolModule module, IKSPContext context) =>
 GetEntrypoint(module, MainEditor, context);
 public static bool HasEditorEntrypoint(this IKontrolModule module) => HasEntrypoint(module, MainEditor, false);
 public static Entrypoint GetKSCEntrypoint(this IKontrolModule module, IKSPContext context) =>
 GetEntrypoint(module, MainKsc, context);
 public KontrolSystemProcess(IKontrolModule module)
 {
     this.module = module;
     state       = KontrolSystemProcessState.Available;
     id          = Guid.NewGuid();
 }
Esempio n. 21
0
        public static void GenerateDocs(ModuleContext moduleContext, IKontrolModule module, TextWriter output)
        {
            output.WriteLine("---");
            output.WriteLine($"title: \"{module.Name}\"");
            output.WriteLine("---");
            output.WriteLine();
            output.WriteLine(module.Description);

            if (module.AllTypeNames.Any())
            {
                output.WriteLine();
                output.WriteLine("# Types");
                output.WriteLine();

                foreach (string typeName in module.AllTypeNames.OrderBy(name => name))
                {
                    RealizedType type = module.FindType(typeName)?.UnderlyingType(moduleContext);

                    output.WriteLine();
                    output.WriteLine($"## {typeName}");
                    output.WriteLine();
                    output.WriteLine(type.Description);

                    if (type.DeclaredFields.Count > 0)
                    {
                        output.WriteLine();
                        output.WriteLine("### Fields");
                        output.WriteLine();

                        output.WriteLine("Name | Type | Description");
                        output.WriteLine("--- | --- | ---");

                        foreach (var kv in type.DeclaredFields.OrderBy(kv => kv.Key))
                        {
                            output.WriteLine(
                                $"{kv.Key} | {kv.Value.DeclaredType} | {kv.Value.Description?.Replace("\n", " ")}");
                        }
                    }

                    if (type.DeclaredMethods.Count > 0)
                    {
                        output.WriteLine();
                        output.WriteLine("### Methods");

                        foreach (var kv in type.DeclaredMethods.OrderBy(kv => kv.Key))
                        {
                            output.WriteLine();
                            output.WriteLine($"#### {kv.Key}");
                            output.WriteLine();
                            output.WriteLine("```rust");
                            output.WriteLine(MethodSignature(type.LocalName, kv.Key, kv.Value));
                            output.WriteLine("```");
                            output.WriteLine();
                            output.WriteLine(kv.Value.Description);
                        }
                    }
                }
            }

            if (module.AllConstantNames.Any())
            {
                output.WriteLine();
                output.WriteLine("# Constants");
                output.WriteLine();

                output.WriteLine("Name | Type | Description");
                output.WriteLine("--- | --- | ---");
                foreach (string constantName in module.AllConstantNames.OrderBy(name => name))
                {
                    IKontrolConstant constant = module.FindConstant(constantName);

                    output.WriteLine($"{constantName} | {constant.Type} | {constant.Description?.Replace("\n", " ")}");
                }

                output.WriteLine();
            }

            if (module.AllFunctionNames.Any())
            {
                output.WriteLine();
                output.WriteLine("# Functions");
                output.WriteLine();

                foreach (string functionName in module.AllFunctionNames.OrderBy(name => name))
                {
                    IKontrolFunction function = module.FindFunction(functionName);

                    output.WriteLine();
                    output.WriteLine($"## {functionName}");
                    output.WriteLine();
                    output.WriteLine("```rust");
                    output.WriteLine(FunctionSignature(function));
                    output.WriteLine("```");
                    output.WriteLine();
                    output.WriteLine(function.Description);
                }
            }
        }