Пример #1
0
        static MASIVTOL()
        {
            wbiVtolManager_t = Utility.GetExportedType("KerbalActuators", "KerbalActuators.WBIVTOLManager");

            if (wbiVtolManager_t != null)
            {
                // Init
                MethodInfo FindControllers_t = wbiVtolManager_t.GetMethod("FindControllers", BindingFlags.Instance | BindingFlags.Public);
                if (FindControllers_t == null)
                {
                    Utility.LogStaticError("Didn't find FindControllers");
                    return;
                }
                wbiFindControllers = DynamicMethodFactory.CreateAction <object, Vessel>(FindControllers_t);

                // Capabilities
                MethodInfo AirParkControllerActive_t = wbiVtolManager_t.GetMethod("AirParkControllerActive", BindingFlags.Instance | BindingFlags.Public);
                if (AirParkControllerActive_t != null)
                {
                    wbiAirParkAvailable = DynamicMethodFactory.CreateFunc <object, bool>(AirParkControllerActive_t);
                }
                else
                {
                    Utility.LogStaticError("Didn't find AirParkControllerActive");
                    return;
                }
                MethodInfo HoverControllerActive_t = wbiVtolManager_t.GetMethod("HoverControllerActive", BindingFlags.Instance | BindingFlags.Public);
                if (HoverControllerActive_t != null)
                {
                    wbiHoverControllerAvailable = DynamicMethodFactory.CreateFunc <object, bool>(HoverControllerActive_t);
                }
                else
                {
                    Utility.LogStaticError("Didn't find HoverControllerActive");
                    return;
                }
                MethodInfo HasRotationControllers_t = wbiVtolManager_t.GetMethod("HasRotationControllers", BindingFlags.Instance | BindingFlags.Public);
                if (HasRotationControllers_t != null)
                {
                    wbiRotationControllerAvailable = DynamicMethodFactory.CreateFunc <object, bool>(HasRotationControllers_t);
                }
                else
                {
                    Utility.LogStaticError("Didn't find HasRotationControllers");
                    return;
                }
                MethodInfo ThrustVectorControllerActive_t = wbiVtolManager_t.GetMethod("ThrustVectorControllerActive", BindingFlags.Instance | BindingFlags.Public);
                if (ThrustVectorControllerActive_t != null)
                {
                    wbiThrustControllerAvailable = DynamicMethodFactory.CreateFunc <object, bool>(ThrustVectorControllerActive_t);
                }
                else
                {
                    Utility.LogStaticError("Didn't find ThrustVectorControllerActive");
                    return;
                }

                // Air Park
                MethodInfo IsParked_t = wbiVtolManager_t.GetMethod("IsParked", BindingFlags.Instance | BindingFlags.Public);
                if (IsParked_t == null)
                {
                    Utility.LogStaticError("Didn't find IsParked");
                    return;
                }
                wbiGetParkActive = DynamicMethodFactory.CreateFunc <object, bool>(IsParked_t);

                MethodInfo TogglePark_t = wbiVtolManager_t.GetMethod("TogglePark", BindingFlags.Instance | BindingFlags.Public);
                if (TogglePark_t == null)
                {
                    Utility.LogStaticError("Didn't find TogglePark");
                    return;
                }
                wbiTogglePark = DynamicMethodFactory.CreateAction <object>(TogglePark_t);

                // Hover
                MethodInfo EnginesAreActive_t = wbiVtolManager_t.GetMethod("EnginesAreActive", BindingFlags.Instance | BindingFlags.Public);
                if (EnginesAreActive_t == null)
                {
                    Utility.LogStaticError("Didn't find EnginesAreActive");
                    return;
                }
                wbiEnginesAreActive = DynamicMethodFactory.CreateFunc <object, bool>(EnginesAreActive_t);

                MethodInfo StartEngines_t = wbiVtolManager_t.GetMethod("StartEngines", BindingFlags.Instance | BindingFlags.Public);
                if (StartEngines_t == null)
                {
                    Utility.LogStaticError("Didn't find StartEngines");
                    return;
                }
                wbiStartEngines = DynamicMethodFactory.CreateAction <object>(StartEngines_t);

                MethodInfo StopEngines_t = wbiVtolManager_t.GetMethod("StopEngines", BindingFlags.Instance | BindingFlags.Public);
                if (StopEngines_t == null)
                {
                    Utility.LogStaticError("Didn't find StopEngines");
                    return;
                }
                wbiStopEngines = DynamicMethodFactory.CreateAction <object>(StopEngines_t);

                FieldInfo HoverActive_t = wbiVtolManager_t.GetField("hoverActive", BindingFlags.Instance | BindingFlags.Public);
                if (HoverActive_t == null)
                {
                    Utility.LogStaticError("Didn't find hoverActive");
                    return;
                }
                wbiGetHoverActive = DynamicMethodFactory.CreateGetField <object, bool>(HoverActive_t);

                MethodInfo ToggleHover_t = wbiVtolManager_t.GetMethod("ToggleHover", BindingFlags.Instance | BindingFlags.Public);
                if (ToggleHover_t == null)
                {
                    Utility.LogStaticError("Didn't find ToggleHover");
                    return;
                }
                wbiToggleHover = DynamicMethodFactory.CreateAction <object>(ToggleHover_t);

                // Rotation
                MethodInfo CanRotateMax_t = wbiVtolManager_t.GetMethod("CanRotateMax", BindingFlags.Instance | BindingFlags.Public);
                if (CanRotateMax_t == null)
                {
                    Utility.LogStaticError("Didn't find CanRotateMax");
                    return;
                }
                wbiCanRotateMax = DynamicMethodFactory.CreateFunc <object, bool>(CanRotateMax_t);

                MethodInfo CanRotateMin_t = wbiVtolManager_t.GetMethod("CanRotateMin", BindingFlags.Instance | BindingFlags.Public);
                if (CanRotateMin_t == null)
                {
                    Utility.LogStaticError("Didn't find CanRotateMin");
                    return;
                }
                wbiCanRotateMin = DynamicMethodFactory.CreateFunc <object, bool>(CanRotateMin_t);

                MethodInfo RotateToMax_t = wbiVtolManager_t.GetMethod("RotateToMax", BindingFlags.Instance | BindingFlags.Public);
                if (RotateToMax_t == null)
                {
                    Utility.LogStaticError("Didn't find RotateToMax");
                    return;
                }
                wbiRotateMax = DynamicMethodFactory.CreateAction <object>(RotateToMax_t);

                MethodInfo RotateToMin_t = wbiVtolManager_t.GetMethod("RotateToMin", BindingFlags.Instance | BindingFlags.Public);
                if (RotateToMin_t == null)
                {
                    Utility.LogStaticError("Didn't find RotateToMin");
                    return;
                }
                wbiRotateMin = DynamicMethodFactory.CreateAction <object>(RotateToMin_t);

                MethodInfo RotateToNeutral_t = wbiVtolManager_t.GetMethod("RotateToNeutral", BindingFlags.Instance | BindingFlags.Public);
                if (ToggleHover_t == null)
                {
                    Utility.LogStaticError("Didn't find RotateToNeutral");
                    return;
                }
                wbiRotateNeutral = DynamicMethodFactory.CreateAction <object>(RotateToNeutral_t);

                MethodInfo IncreaseRotationAngle_t = wbiVtolManager_t.GetMethod("IncreaseRotationAngle", BindingFlags.Instance | BindingFlags.Public);
                if (IncreaseRotationAngle_t == null)
                {
                    Utility.LogStaticError("Didn't find IncreaseRotationAngle");
                    return;
                }
                wbiIncreaseRotationAngle = DynamicMethodFactory.CreateAction <object, float>(IncreaseRotationAngle_t);

                MethodInfo DecreaseRotationAngle_t = wbiVtolManager_t.GetMethod("DecreaseRotationAngle", BindingFlags.Instance | BindingFlags.Public);
                if (DecreaseRotationAngle_t == null)
                {
                    Utility.LogStaticError("Didn't find DecreaseRotationAngle");
                    return;
                }
                wbiDecreaseRotationAngle = DynamicMethodFactory.CreateAction <object, float>(DecreaseRotationAngle_t);

                MethodInfo GetMaxRotation_t = wbiVtolManager_t.GetMethod("GetMaxRotation", BindingFlags.Instance | BindingFlags.Public);
                if (GetMaxRotation_t == null)
                {
                    Utility.LogStaticError("Didn't find GetMaxRotation");
                    return;
                }
                wbiGetMaxRotation = DynamicMethodFactory.CreateFunc <object, float>(GetMaxRotation_t);

                MethodInfo GetMinRotation_t = wbiVtolManager_t.GetMethod("GetMinRotation", BindingFlags.Instance | BindingFlags.Public);
                if (GetMinRotation_t == null)
                {
                    Utility.LogStaticError("Didn't find GetMinRotation");
                    return;
                }
                wbiGetMinRotation = DynamicMethodFactory.CreateFunc <object, float>(GetMinRotation_t);

                MethodInfo GetCurrentRotation_t = wbiVtolManager_t.GetMethod("GetCurrentRotation", BindingFlags.Instance | BindingFlags.Public);
                if (GetCurrentRotation_t == null)
                {
                    Utility.LogStaticError("Didn't find GetCurrentRotation");
                    return;
                }
                wbiGetCurrentRotation = DynamicMethodFactory.CreateFunc <object, float>(GetCurrentRotation_t);

                // VSpd
                MethodInfo KillVerticalSpeed_t = wbiVtolManager_t.GetMethod("KillVerticalSpeed", BindingFlags.Instance | BindingFlags.Public);
                if (KillVerticalSpeed_t == null)
                {
                    Utility.LogStaticError("Didn't find KillVerticalSpeed");
                    return;
                }
                wbiKillVerticalSpeed = DynamicMethodFactory.CreateAction <object>(KillVerticalSpeed_t);

                MethodInfo IncreaseVerticalSpeed_t = wbiVtolManager_t.GetMethod("IncreaseVerticalSpeed", BindingFlags.Instance | BindingFlags.Public);
                if (IncreaseVerticalSpeed_t == null)
                {
                    Utility.LogStaticError("Didn't find IncreaseVerticalSpeed");
                    return;
                }
                wbiIncreaseVerticalSpeed = DynamicMethodFactory.CreateAction <object, float>(IncreaseVerticalSpeed_t);

                MethodInfo DecreaseVerticalSpeed_t = wbiVtolManager_t.GetMethod("DecreaseVerticalSpeed", BindingFlags.Instance | BindingFlags.Public);
                if (DecreaseVerticalSpeed_t == null)
                {
                    Utility.LogStaticError("Didn't find DecreaseVerticalSpeed");
                    return;
                }
                wbiDecreaseVerticalSpeed = DynamicMethodFactory.CreateAction <object, float>(DecreaseVerticalSpeed_t);

                // Thrust mode
                FieldInfo ThrustMode_t = wbiVtolManager_t.GetField("thrustMode", BindingFlags.Instance | BindingFlags.Public);
                if (ThrustMode_t == null)
                {
                    Utility.LogStaticError("Didn't find thrustMode");
                    return;
                }
                wbiGetThrustMode = DynamicMethodFactory.CreateGetField <object, object>(ThrustMode_t);

                MethodInfo SetForwardThrust_t = wbiVtolManager_t.GetMethod("SetForwardThrust", BindingFlags.Instance | BindingFlags.Public);
                if (SetForwardThrust_t == null)
                {
                    Utility.LogStaticError("Didn't find SetForwardThrust");
                    return;
                }
                wbiSetForwardThrust = DynamicMethodFactory.CreateAction <object>(SetForwardThrust_t);

                MethodInfo SetReverseThrust_t = wbiVtolManager_t.GetMethod("SetReverseThrust", BindingFlags.Instance | BindingFlags.Public);
                if (SetReverseThrust_t == null)
                {
                    Utility.LogStaticError("Didn't find SetReverseThrust");
                    return;
                }
                wbiSetReverseThrust = DynamicMethodFactory.CreateAction <object>(SetReverseThrust_t);

                MethodInfo SetVTOLThrust_t = wbiVtolManager_t.GetMethod("SetVTOLThrust", BindingFlags.Instance | BindingFlags.Public);
                if (SetVTOLThrust_t == null)
                {
                    Utility.LogStaticError("Didn't find SetVTOLThrust");
                    return;
                }
                wbiSetVTOLThrust = DynamicMethodFactory.CreateAction <object>(SetVTOLThrust_t);

                FieldInfo VerticalSpeed_t = wbiVtolManager_t.GetField("verticalSpeed", BindingFlags.Instance | BindingFlags.Public);
                if (VerticalSpeed_t == null)
                {
                    Utility.LogStaticError("Didn't find verticalSpeed");
                    return;
                }
                wbiGetVerticalSpeed = DynamicMethodFactory.CreateGetField <object, float>(VerticalSpeed_t);

                wbiWBIGraviticEngine_t = Utility.GetExportedType("FlyingSaucers", "WildBlueIndustries.WBIGraviticEngine");
                if (wbiWBIGraviticEngine_t != null)
                {
                    MethodInfo IsCrazyModeUnlocked_t = wbiWBIGraviticEngine_t.GetMethod("IsCrazyModeUnlocked", BindingFlags.Instance | BindingFlags.Public);
                    if (IsCrazyModeUnlocked_t == null)
                    {
                        Utility.LogStaticError("Didn't find IsCrazyModeUnlocked");
                        return;
                    }
                    wbiCrazyModeUnlocked = DynamicMethodFactory.CreateFunc <object, bool>(IsCrazyModeUnlocked_t);

                    MethodInfo IsActive_t = wbiWBIGraviticEngine_t.GetMethod("IsActive", BindingFlags.Instance | BindingFlags.Public);
                    if (IsActive_t == null)
                    {
                        Utility.LogStaticError("Didn't find IsActive");
                        return;
                    }
                    wbiCrazyModeIsActive = DynamicMethodFactory.CreateFunc <object, bool>(IsActive_t);

                    MethodInfo GetWarpDirection_t = wbiWBIGraviticEngine_t.GetMethod("GetWarpDirection", BindingFlags.Instance | BindingFlags.Public);
                    if (GetWarpDirection_t == null)
                    {
                        Utility.LogStaticError("Didn't find GetWarpDirection");
                        return;
                    }
                    wbiGetWarpDirection = DynamicMethodFactory.CreateFunc <object, object>(GetWarpDirection_t);

                    MethodInfo SetWarpDirection_t = wbiWBIGraviticEngine_t.GetMethod("SetWarpDirection", BindingFlags.Instance | BindingFlags.Public);
                    if (SetWarpDirection_t == null)
                    {
                        Utility.LogStaticError("Didn't find SetWarpDirection");
                        return;
                    }
                    wbiSetWarpDirection = DynamicMethodFactory.CreateAction <object, int>(SetWarpDirection_t);
                }

                wbivtolInstalled = true;
            }
        }
Пример #2
0
        /// <summary>
        /// Rebuild the action array.
        /// </summary>
        /// <param name="parts"></param>
        internal void Rebuild(List <Part> parts)
        {
            List <MASAction> newAction = new List <MASAction>();

            //Utility.LogMessage(this, "Rebuild AG {0}", actionGroupId);
            //foreach (var aPart in parts)
            //{
            //    Utility.LogMessage(this, "... {0}", aPart.name);
            //}

            // I'm pretty sure this isn't the most efficient way to do this...
            int numTemplates = actionTemplate.Length;

            for (int templateIdx = 0; templateIdx < numTemplates; ++templateIdx)
            {
                // This is kind-of lame.  The part name for the root part has " (vesselName)" tacked to the end of it
                // (ie, "SomePart (Untitled Craft)"), so I can't do a simple string comparison.
                string      templateName  = actionTemplate[templateIdx].partName;
                List <Part> relevantParts = parts.FindAll((Part x) =>
                {
                    int idxSpace = x.name.IndexOf(' ');
                    if (idxSpace > 0)
                    {
                        return(x.name.Substring(0, idxSpace) == templateName);
                    }
                    else
                    {
                        return(x.name == templateName);
                    }
                });
                //Utility.LogMessage(this, "Found {0} parts of name {1}", relevantParts.Count, actionTemplate[templateIdx].partName);
                if (relevantParts.Count > 0)
                {
                    bool     warned          = false;
                    string[] reflectedAction = actionTemplate[templateIdx].module.Split('.');
                    foreach (Part p in relevantParts)
                    {
                        switch (actionTemplate[templateIdx].module)
                        {
                        case "ModuleAnimateGeneric":
                            List <ModuleAnimateGeneric> mag = p.FindModulesImplementing <ModuleAnimateGeneric>();
                            //Utility.LogMessage(this, "Found {0} ModuleAnimateGeneric", mag.Count);
                            foreach (ModuleAnimateGeneric magx in mag)
                            {
                                newAction.Add(new MASActionModuleAnimateGeneric(magx, actionTemplate[templateIdx].action));
                            }
                            break;

                        case "ModuleDecouple":
                            List <ModuleDecouple> md = p.FindModulesImplementing <ModuleDecouple>();
                            //Utility.LogMessage(this, "Found {0} ModuleDecouple", md.Count);
                            foreach (ModuleDecouple mdx in md)
                            {
                                newAction.Add(new MASActionModuleDecouple(mdx, actionTemplate[templateIdx].action));
                            }
                            break;

                        case "ModuleEngines":
                            List <ModuleEngines> me = p.FindModulesImplementing <ModuleEngines>();
                            //Utility.LogMessage(this, "Found {0} ModuleEngines", me.Count);
                            foreach (ModuleEngines mex in me)
                            {
                                newAction.Add(new MASActionModuleEngines(mex, actionTemplate[templateIdx].action));
                            }
                            break;

                        case "ModuleLight":
                            List <ModuleLight> ml = p.FindModulesImplementing <ModuleLight>();
                            //Utility.LogMessage(this, "Found {0} ModuleLight", ml.Count);
                            foreach (ModuleLight mlx in ml)
                            {
                                newAction.Add(new MASActionModuleLight(mlx, actionTemplate[templateIdx].action));
                            }
                            break;

                        case "ModuleRCS":
                            List <ModuleRCS> mr = p.FindModulesImplementing <ModuleRCS>();
                            //Utility.LogMessage(this, "Found {0} ModuleRCS", mr.Count);
                            foreach (ModuleRCS mrx in mr)
                            {
                                newAction.Add(new MASActionModuleRCS(mrx, actionTemplate[templateIdx].action));
                            }
                            break;

                        default:
                            if (reflectedAction.Length != 2)
                            {
                                if (!warned)
                                {
                                    Utility.LogWarning(this, "Found unrecognized module \"{0}\" for MAS_ACTION_GROUP {1}", actionTemplate[templateIdx].module, name);
                                    warned = true;
                                }
                            }
                            else if (p.Modules.Contains(reflectedAction[0]))
                            {
                                PartModule mod = p.Modules[reflectedAction[0]];
                                Type       t   = mod.GetType();
                                MethodInfo mi  = t.GetMethod(reflectedAction[1]);
                                if (mi == null || mi.GetParameters().Length > 0)
                                {
                                    if (!warned)
                                    {
                                        Utility.LogWarning(this, "Did not find method \"{3}\" with 0 parameters in module \"{0}\" in part \"{2}\" for MAS_ACTION_GROUP {1}",
                                                           reflectedAction[0], name, p.name, reflectedAction[1]);
                                        warned = true;
                                    }
                                }
                                else
                                {
                                    Action <object> theAction = DynamicMethodFactory.CreateAction <object>(mi);
                                    newAction.Add(new MASActionGeneric(mod, theAction, actionTemplate[templateIdx].action));
                                }
                            }
                            else
                            {
                                if (!warned)
                                {
                                    Utility.LogWarning(this, "Did not find module \"{0}\" in part \"{2}\" for MAS_ACTION_GROUP {1}",
                                                       reflectedAction[0], name, p.name);
                                    warned = true;
                                }
                            }
                            break;
                        }
                    }
                }
            }

            action = newAction.ToArray();
            //Utility.LogMessage(this, "Ended up with {0} entries in action.", action.Length);
        }