示例#1
0
        static JSIPilotAssistant()
        {
            try
            {
                var loadedPAAssy = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.name == "PilotAssistant");
                if (loadedPAAssy == null)
                {
                    //JUtil.LogMessage(null, "Did not load PilotAssistant");
                    //var list = AssemblyLoader.loadedAssemblies;
                    //foreach(var a in list)
                    //{
                    //    JUtil.LogMessage(null, "-- {0}", a.name);
                    //}
                    return;
                }
                //else
                //{
                //    JUtil.LogMessage(null, "Did find PilotAssistant");
                //}

                Type paAsstVesselModule_t = loadedPAAssy.assembly.GetExportedTypes()
                                            .SingleOrDefault(t => t.FullName == "PilotAssistant.FlightModules.AsstVesselModule");
                if (paAsstVesselModule_t == null)
                {
                    throw new NotImplementedException("paAsstVesselModule_t");
                }

                vesselAsst_t = paAsstVesselModule_t.GetField("vesselAsst", BindingFlags.Instance | BindingFlags.Public);
                if (vesselAsst_t == null)
                {
                    throw new NotImplementedException("vesselAsst_t");
                }

                Type paPilotAssistant_t = loadedPAAssy.assembly.GetExportedTypes()
                                          .SingleOrDefault(t => t.FullName == "PilotAssistant.FlightModules.PilotAssistant");
                if (paPilotAssistant_t == null)
                {
                    throw new NotImplementedException("paPilotAssistant_t");
                }

                vertMode_t = paPilotAssistant_t.GetField("CurrentVertMode", BindingFlags.Instance | BindingFlags.Public);
                if (vertMode_t == null)
                {
                    throw new NotImplementedException("currentVertMode_t");
                }
                vertActive_t = paPilotAssistant_t.GetField("VertActive", BindingFlags.Instance | BindingFlags.Public);
                if (vertActive_t == null)
                {
                    throw new NotImplementedException("vertActive_t");
                }
                MethodInfo vertSetting_t = paPilotAssistant_t.GetMethod("GetCurrentVert", BindingFlags.Instance | BindingFlags.Public);
                if (vertSetting_t == null)
                {
                    throw new NotImplementedException("vertSetting_t");
                }
                GetCurrentVert = DynamicMethodDelegateFactory.CreateFuncDouble(vertSetting_t);
                MethodInfo setVert_t = paPilotAssistant_t.GetMethod("SetVert", BindingFlags.Instance | BindingFlags.Public);
                if (setVert_t == null)
                {
                    throw new NotImplementedException("setVert_t");
                }
                SetVert = DynamicMethodDelegateFactory.Create(setVert_t);

                horzMode_t = paPilotAssistant_t.GetField("CurrentHrztMode", BindingFlags.Instance | BindingFlags.Public);
                if (horzMode_t == null)
                {
                    throw new NotImplementedException("horzMode_t");
                }
                horzActive_t = paPilotAssistant_t.GetField("HrztActive", BindingFlags.Instance | BindingFlags.Public);
                if (horzActive_t == null)
                {
                    throw new NotImplementedException("horzActive_t");
                }
                MethodInfo horzSetting_t = paPilotAssistant_t.GetMethod("GetCurrentHrzt", BindingFlags.Instance | BindingFlags.Public);
                if (horzSetting_t == null)
                {
                    throw new NotImplementedException("horzSetting_t");
                }
                GetCurrentHorz = DynamicMethodDelegateFactory.CreateFuncDouble(horzSetting_t);
                MethodInfo setHorz_t = paPilotAssistant_t.GetMethod("SetHrzt", BindingFlags.Instance | BindingFlags.Public);
                if (setHorz_t == null)
                {
                    throw new NotImplementedException("setHorz_t");
                }
                SetHorz = DynamicMethodDelegateFactory.Create(setHorz_t);

                throttleMode_t = paPilotAssistant_t.GetField("CurrentThrottleMode", BindingFlags.Instance | BindingFlags.Public);
                if (throttleMode_t == null)
                {
                    throw new NotImplementedException("throttleMode_t");
                }
                throttleActive_t = paPilotAssistant_t.GetField("ThrtActive", BindingFlags.Instance | BindingFlags.Public);
                if (throttleActive_t == null)
                {
                    throw new NotImplementedException("throttleActive_t");
                }
                MethodInfo throttleSetting_t = paPilotAssistant_t.GetMethod("GetCurrentThrottle", BindingFlags.Instance | BindingFlags.Public);
                if (throttleSetting_t == null)
                {
                    throw new NotImplementedException("throttleSetting_t");
                }
                GetCurrentThrottle = DynamicMethodDelegateFactory.CreateFuncDouble(throttleSetting_t);
                MethodInfo setThrottle_t = paPilotAssistant_t.GetMethod("SetThrottle", BindingFlags.Instance | BindingFlags.Public);
                if (setThrottle_t == null)
                {
                    throw new NotImplementedException("setThrottle_t");
                }
                SetThrottle = DynamicMethodDelegateFactory.Create(setThrottle_t);

                speedRef_t = paPilotAssistant_t.GetField("speedRef", BindingFlags.Instance | BindingFlags.NonPublic);
                if (speedRef_t == null)
                {
                    throw new NotImplementedException("PA speedRef_t");
                }
                MethodInfo changeSpeedRef_t = paPilotAssistant_t.GetMethod("ChangeSpeedRef", BindingFlags.Instance | BindingFlags.Public);
                if (changeSpeedRef_t == null)
                {
                    throw new NotImplementedException("PA changeSpeedRef_t");
                }
                SetSpeedRef = DynamicMethodDelegateFactory.Create(changeSpeedRef_t);

                speedUnits_t = paPilotAssistant_t.GetField("units", BindingFlags.Instance | BindingFlags.NonPublic);
                if (speedUnits_t == null)
                {
                    throw new NotImplementedException("PA speedUnits_t");
                }
                MethodInfo changeSpeedUnits_t = paPilotAssistant_t.GetMethod("ChangeSpeedUnit", BindingFlags.Instance | BindingFlags.Public);
                if (changeSpeedUnits_t == null)
                {
                    throw new NotImplementedException("PA changeSpeedUnits_t");
                }
                SetSpeedUnits = DynamicMethodDelegateFactory.Create(changeSpeedUnits_t);

                pauseState_t = paPilotAssistant_t.GetField("bPause", BindingFlags.Instance | BindingFlags.Public);
                if (pauseState_t == null)
                {
                    throw new NotImplementedException("PA pauseState_t");
                }
                MethodInfo TogglePause_t = paPilotAssistant_t.GetMethod("TogglePauseCtrlState", BindingFlags.Instance | BindingFlags.Public);
                if (TogglePause_t == null)
                {
                    throw new NotImplementedException("PA TogglePause_t");
                }
                TogglePause = DynamicMethodDelegateFactory.CreateAction(TogglePause_t);
            }
            catch (Exception e)
            {
                JUtil.LogMessage(null, "Exception tripped: {0}", e);
                paFound = false;
                return;
            }

            paFound = true;
        }
示例#2
0
        static JSIMechJeb()
        {
            try
            {
                var loadedMechJebAssy = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.name == "MechJeb2");

                if (loadedMechJebAssy == null)
                {
                    mjFound = false;
                    //JUtil.LogMessage(this, "A supported version of MechJeb is {0}", (mjFound) ? "present" : "not available");

                    return;
                }

                //--- Process all the reflection info
                // MechJebCore
                mjMechJebCore_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.MechJebCore");
                if (mjMechJebCore_t == null)
                {
                    return;
                }
                MethodInfo mjGetComputerModule = mjMechJebCore_t.GetMethod("GetComputerModule", new Type[] { typeof(string) });
                if (mjGetComputerModule == null)
                {
                    throw new NotImplementedException("mjGetComputerModule");
                }
                getComputerModule = DynamicMethodDelegateFactory.Create(mjGetComputerModule);
                mjCoreTarget = mjMechJebCore_t.GetField("target", BindingFlags.Instance | BindingFlags.Public);
                if (mjCoreTarget == null)
                {
                    throw new NotImplementedException("mjCoreTarget");
                }
                mjCoreNode = mjMechJebCore_t.GetField("node", BindingFlags.Instance | BindingFlags.Public);
                if (mjCoreNode == null)
                {
                    throw new NotImplementedException("mjCoreNode");
                }
                mjCoreAttitude = mjMechJebCore_t.GetField("attitude", BindingFlags.Instance | BindingFlags.Public);
                if (mjCoreAttitude == null)
                {
                    throw new NotImplementedException("mjCoreAttitude");
                }
                mjCoreVesselState = mjMechJebCore_t.GetField("vesselState", BindingFlags.Instance | BindingFlags.Public);
                if (mjCoreVesselState == null)
                {
                    throw new NotImplementedException("mjCoreVesselState");
                }

                // VesselExtensions
                Type mjVesselExtensions_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.VesselExtensions");
                if (mjVesselExtensions_t == null)
                {
                    throw new NotImplementedException("mjVesselExtensions_t");
                }
                MethodInfo mjGetMasterMechJeb = mjVesselExtensions_t.GetMethod("GetMasterMechJeb", BindingFlags.Static | BindingFlags.Public);
                if (mjGetMasterMechJeb == null)
                {
                    throw new NotImplementedException("mjGetMasterMechJeb");
                }
                getMasterMechJeb = DynamicMethodDelegateFactory.Create(mjGetMasterMechJeb);
                MethodInfo mjPlaceManeuverNode = mjVesselExtensions_t.GetMethod("PlaceManeuverNode", BindingFlags.Static | BindingFlags.Public);
                if (mjPlaceManeuverNode == null)
                {
                    throw new NotImplementedException("mjPlaceManeuverNode");
                }
                placeManeuverNode = DynamicMethodDelegateFactory.Create(mjPlaceManeuverNode);

                // VesselState
                Type mjVesselState_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.VesselState");
                if (mjVesselState_t == null)
                {
                    throw new NotImplementedException("mjVesselState_t");
                }
                MethodInfo mjTerminalVelocity = mjVesselState_t.GetMethod("TerminalVelocity", BindingFlags.Instance | BindingFlags.Public);
                if (mjTerminalVelocity == null)
                {
                    throw new NotImplementedException("mjTerminalVelocity");
                }
                terminalVelocity = DynamicMethodDelegateFactory.CreateFuncDouble(mjTerminalVelocity);

                // SmartASS
                Type mjSmartass_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.MechJebModuleSmartASS");
                mjSmartassTarget = mjSmartass_t.GetField("target", BindingFlags.Instance | BindingFlags.Public);
                if (mjSmartassTarget == null)
                {
                    throw new NotImplementedException("mjSmartassTarget");
                }
                MethodInfo mjSmartassEngage = mjSmartass_t.GetMethod("Engage", BindingFlags.Instance | BindingFlags.Public);
                if (mjSmartassEngage == null)
                {
                    throw new NotImplementedException("mjSmartassEngage");
                }
                engageSmartass = DynamicMethodDelegateFactory.Create(mjSmartassEngage);
                mjSmartassForceRol = mjSmartass_t.GetField("forceRol", BindingFlags.Instance | BindingFlags.Public);
                if (mjSmartassForceRol == null)
                {
                    throw new NotImplementedException("mjSmartassForceRol");
                }
                mjSmartassRol = mjSmartass_t.GetField("rol", BindingFlags.Instance | BindingFlags.Public);
                if (mjSmartassRol == null)
                {
                    throw new NotImplementedException("mjSmartassRol");
                }
                FieldInfo TargetTextsInfo = mjSmartass_t.GetField("TargetTexts", BindingFlags.Static | BindingFlags.Public);
                TargetTexts = (string[])TargetTextsInfo.GetValue(null);
                if (TargetTexts == null)
                {
                    throw new NotImplementedException("TargetTexts");
                }
                FieldInfo ModeTextsInfo = mjSmartass_t.GetField("ModeTexts", BindingFlags.Static | BindingFlags.Public);
                ModeTexts = (string[])ModeTextsInfo.GetValue(null);
                if (ModeTexts == null)
                {
                    throw new NotImplementedException("ModeTexts");
                }

                // Landing Predictions
                Type mjModuleLandingPredictions_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.MechJebModuleLandingPredictions");
                if (mjModuleLandingPredictions_t == null)
                {
                    throw new NotImplementedException("mjModuleLandingPredictions_t");
                }
                MethodInfo mjPredictionsGetResult = mjModuleLandingPredictions_t.GetMethod("GetResult", BindingFlags.Instance | BindingFlags.Public);
                if (mjPredictionsGetResult == null)
                {
                    throw new NotImplementedException("mjPredictionsGetResult");
                }
                getPredictionsResult = DynamicMethodDelegateFactory.Create(mjPredictionsGetResult);

                // AbsoluteVector
                Type mjAbsoluteVector_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.AbsoluteVector");
                if (mjAbsoluteVector_t == null)
                {
                    throw new NotImplementedException("mjAbsoluteVector_t");
                }
                mjAbsoluteVectorLat = mjAbsoluteVector_t.GetField("latitude", BindingFlags.Instance | BindingFlags.Public);
                if (mjAbsoluteVectorLat == null)
                {
                    throw new NotImplementedException("mjAbsoluteVectorLat");
                }
                mjAbsoluteVectorLon = mjAbsoluteVector_t.GetField("longitude", BindingFlags.Instance | BindingFlags.Public);
                if (mjAbsoluteVectorLon == null)
                {
                    throw new NotImplementedException("mjAbsoluteVectorLon");
                }

                // MechJebModuleAscentAutopilot
                Type mjMechJebModuleAscentAutopilot_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.MechJebModuleAscentAutopilot");
                if (mjMechJebModuleAscentAutopilot_t == null)
                {
                    throw new NotImplementedException("mjMechJebModuleAscentAutopilot_t");
                }
                launchOrbitAltitude = mjMechJebModuleAscentAutopilot_t.GetField("desiredOrbitAltitude");
                if (launchOrbitAltitude == null)
                {
                    throw new NotImplementedException("launchOrbitAltitude");
                }

                Type mjEditableDoubleMult_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.EditableDoubleMult");
                if (mjEditableDoubleMult_t == null)
                {
                    throw new NotImplementedException("mjEditableDoubleMult_t");
                }
                getEditableDoubleMultMultiplier = mjEditableDoubleMult_t.GetField("multiplier");
                if (getEditableDoubleMultMultiplier == null)
                {
                    throw new NotImplementedException("getEditableDoubleMultMultiplier");
                }
                PropertyInfo edmVal = mjEditableDoubleMult_t.GetProperty("val");
                if (edmVal == null)
                {
                    throw new NotImplementedException("edmVal");
                }
                // getEditableDoubleMult
                MethodInfo mjGetEDM = edmVal.GetGetMethod();
                if (mjGetEDM != null)
                {
                    getEditableDoubleMult = DynamicMethodDelegateFactory.CreateFuncDouble(mjGetEDM);
                }
                // setEditableDoubleMult
                MethodInfo mjSetEDM = edmVal.GetSetMethod();
                if (mjSetEDM != null)
                {
                    setEditableDoubleMult = DynamicMethodDelegateFactory.Create(mjSetEDM);
                }

                // EditableAngle
                Type mjEditableAngle_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.EditableAngle");
                if (mjEditableAngle_t == null)
                {
                    throw new NotImplementedException("mjEditableAngle_t");
                }
                MethodInfo mjAbsoluteVectorToDouble = null;
                foreach (MethodInfo method in mjEditableAngle_t.GetMethods(BindingFlags.Static | BindingFlags.Public))
                {
                    // The method name reports as "op_Implicit", but there are two
                    if (method.ReturnType == typeof(System.Double))
                    {
                        mjAbsoluteVectorToDouble = method;
                        break;
                    }
                }
                if (mjAbsoluteVectorToDouble == null)
                {
                    throw new NotImplementedException("mjAbsoluteVectorToDouble");
                }
                absoluteVectorToDouble = DynamicMethodDelegateFactory.Create(mjAbsoluteVectorToDouble);

                // MechJebModuleTargetController
                Type mjModuleTargetController_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.MechJebModuleTargetController");
                if (mjModuleTargetController_t == null)
                {
                    throw new NotImplementedException("mjModuleTargetController_t");
                }
                mjTargetLongitude = mjModuleTargetController_t.GetField("targetLongitude", BindingFlags.Instance | BindingFlags.Public);
                if (mjTargetLongitude == null)
                {
                    throw new NotImplementedException("mjTargetLongitude");
                }
                mjTargetLatitude = mjModuleTargetController_t.GetField("targetLatitude", BindingFlags.Instance | BindingFlags.Public);
                if (mjTargetLatitude == null)
                {
                    throw new NotImplementedException("mjTargetLatitude");
                }
                PropertyInfo mjPositionTargetExists = mjModuleTargetController_t.GetProperty("PositionTargetExists", BindingFlags.Instance | BindingFlags.Public);
                MethodInfo mjGetPositionTargetExists = null;
                if (mjPositionTargetExists != null)
                {
                    mjGetPositionTargetExists = mjPositionTargetExists.GetGetMethod();
                }
                if (mjGetPositionTargetExists == null)
                {
                    throw new NotImplementedException("mjGetPositionTargetExists");
                }
                getPositionTargetExists = DynamicMethodDelegateFactory.CreateFuncBool(mjGetPositionTargetExists);
                PropertyInfo mjNormalTargetExists = mjModuleTargetController_t.GetProperty("NormalTargetExists", BindingFlags.Instance | BindingFlags.Public);
                MethodInfo mjGetNormalTargetExists = null;
                if (mjNormalTargetExists != null)
                {
                    mjGetNormalTargetExists = mjNormalTargetExists.GetGetMethod();
                }
                if (mjGetNormalTargetExists == null)
                {
                    throw new NotImplementedException("mjGetNormalTargetExists");
                }
                getNormalTargetExists = DynamicMethodDelegateFactory.CreateFuncBool(mjGetNormalTargetExists);
                PropertyInfo mjTargetOrbit = mjModuleTargetController_t.GetProperty("TargetOrbit", BindingFlags.Instance | BindingFlags.Public); ;
                MethodInfo mjGetTargetOrbit = null;
                if (mjTargetOrbit != null)
                {
                    mjGetTargetOrbit = mjTargetOrbit.GetGetMethod();
                }
                if (mjGetTargetOrbit == null)
                {
                    throw new NotImplementedException("mjGetTargetOrbit");
                }
                getTargetOrbit = DynamicMethodDelegateFactory.CreateFuncObject(mjGetTargetOrbit);

                // MuMech.FuelFlowSimulation
                Type mjFuelFlowSimulation_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.FuelFlowSimulation");
                if (mjFuelFlowSimulation_t == null)
                {
                    throw new NotImplementedException("mjFuelFlowSimulation_t");
                }
                // MuMech.FuelFlowSimulation.Stats
                Type mjFuelFlowSimulationStats_t = mjFuelFlowSimulation_t.GetNestedType("Stats");
                if (mjFuelFlowSimulationStats_t == null)
                {
                    throw new NotImplementedException("mjFuelFlowSimulationStats_t");
                }
                mjStageDv = mjFuelFlowSimulationStats_t.GetField("deltaV", BindingFlags.Instance | BindingFlags.Public);
                if (mjStageDv == null)
                {
                    throw new NotImplementedException("mjStageDv");
                }

                // MuMech.ReentrySimulation.Result
                Type mjReentrySim_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.ReentrySimulation");
                if (mjReentrySim_t == null)
                {
                    throw new NotImplementedException("mjReentrySim_t");
                }
                Type mjReentryResult_t = mjReentrySim_t.GetNestedType("Result");
                if (mjReentryResult_t == null)
                {
                    throw new NotImplementedException("mjReentryResult_t");
                }
                mjReentryOutcome = mjReentryResult_t.GetField("outcome", BindingFlags.Instance | BindingFlags.Public);
                if (mjReentryOutcome == null)
                {
                    throw new NotImplementedException("mjReentryOutcome");
                }
                mjReentryEndPosition = mjReentryResult_t.GetField("endPosition", BindingFlags.Instance | BindingFlags.Public);
                if (mjReentryEndPosition == null)
                {
                    throw new NotImplementedException("mjReentryEndPosition");
                }
                mjReentryTime = mjReentryResult_t.GetField("endUT", BindingFlags.Instance | BindingFlags.Public);
                if (mjReentryTime == null)
                {
                    throw new NotImplementedException("mjReentryTime");
                }

                // UserPool
                Type mjUserPool_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.UserPool");
                MethodInfo mjAddUser = mjUserPool_t.GetMethod("Add", BindingFlags.Instance | BindingFlags.Public);
                if (mjAddUser == null)
                {
                    throw new NotImplementedException("mjAddUser");
                }
                addUser = DynamicMethodDelegateFactory.Create(mjAddUser);
                MethodInfo mjRemoveUser = mjUserPool_t.GetMethod("Remove", BindingFlags.Instance | BindingFlags.Public);
                if (mjRemoveUser == null)
                {
                    throw new NotImplementedException("mjRemoveUser");
                }
                removeUser = DynamicMethodDelegateFactory.Create(mjRemoveUser);
                MethodInfo mjContainsUser = mjUserPool_t.GetMethod("Contains", BindingFlags.Instance | BindingFlags.Public);
                if (mjContainsUser == null)
                {
                    throw new NotImplementedException("mjContainsUser");
                }
                containsUser = DynamicMethodDelegateFactory.Create(mjContainsUser);

                // MechJebModuleLandingAutopilot
                Type mjLandingAutopilot_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.MechJebModuleLandingAutopilot");
                MethodInfo mjLandAtPositionTarget = mjLandingAutopilot_t.GetMethod("LandAtPositionTarget", BindingFlags.Instance | BindingFlags.Public);
                if (mjLandAtPositionTarget == null)
                {
                    throw new NotImplementedException("mjLandAtPositionTarget");
                }
                landAtPositionTarget = DynamicMethodDelegateFactory.Create(mjLandAtPositionTarget);
                MethodInfo mjLandUntargeted = mjLandingAutopilot_t.GetMethod("LandUntargeted", BindingFlags.Instance | BindingFlags.Public);
                if (mjLandUntargeted == null)
                {
                    throw new NotImplementedException("mjLandUntargeted");
                }
                landUntargeted = DynamicMethodDelegateFactory.Create(mjLandUntargeted);
                MethodInfo mjStopLanding = mjLandingAutopilot_t.GetMethod("StopLanding", BindingFlags.Instance | BindingFlags.Public);
                if (mjStopLanding == null)
                {
                    throw new NotImplementedException("mjStopLanding");
                }
                stopLanding = DynamicMethodDelegateFactory.CreateAction(mjStopLanding);

                Type mjSpaceplaneAutopilot_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.MechJebModuleSpaceplaneAutopilot");
                MethodInfo mjSPAutoland = mjSpaceplaneAutopilot_t.GetMethod("Autoland", BindingFlags.Instance | BindingFlags.Public);
                if (mjSPAutoland == null)
                {
                    throw new NotImplementedException("mjSPAutoland");
                }
                spaceplaneAutoland = DynamicMethodDelegateFactory.Create(mjSPAutoland);
                MethodInfo mjSPHoldHeadingAndAltitude = mjSpaceplaneAutopilot_t.GetMethod("HoldHeadingAndAltitude", BindingFlags.Instance | BindingFlags.Public);
                if (mjSPHoldHeadingAndAltitude == null)
                {
                    throw new NotImplementedException("mjSPHoldHeadingAndAltitude");
                }
                spaceplaneHoldHeading = DynamicMethodDelegateFactory.Create(mjSPHoldHeadingAndAltitude);
                MethodInfo mjSPAutopilotOff = mjSpaceplaneAutopilot_t.GetMethod("AutopilotOff", BindingFlags.Instance | BindingFlags.Public);
                if (mjSPAutopilotOff == null)
                {
                    throw new NotImplementedException("mjSPAutopilotOff");
                }
                spaceplaneAPOff = DynamicMethodDelegateFactory.CreateAction(mjSPAutopilotOff);
                spaceplaneAPMode = mjSpaceplaneAutopilot_t.GetField("mode", BindingFlags.Instance | BindingFlags.Public);
                if (spaceplaneAPMode == null)
                {
                    throw new NotImplementedException("spaceplaneAPMode");
                }
                spaceplaneAltitude = mjSpaceplaneAutopilot_t.GetField("targetAltitude", BindingFlags.Instance | BindingFlags.Public);
                if (spaceplaneAltitude == null)
                {
                    throw new NotImplementedException("spaceplaneAltitude");
                }
                spaceplaneHeading = mjSpaceplaneAutopilot_t.GetField("targetHeading", BindingFlags.Instance | BindingFlags.Public);
                if (spaceplaneHeading == null)
                {
                    throw new NotImplementedException("spaceplaneHeading");
                }
                spaceplaneGlideslope = mjSpaceplaneAutopilot_t.GetField("glideslope", BindingFlags.Instance | BindingFlags.Public);
                if (spaceplaneGlideslope == null)
                {
                    throw new NotImplementedException("spaceplaneGlideslope");
                }

                // EditableDouble
                Type mjEditableDouble_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.EditableDouble");
                PropertyInfo mjEditableDoubleVal = mjEditableDouble_t.GetProperty("val", BindingFlags.Instance | BindingFlags.Public);
                MethodInfo mjGetEditableDouble = null, mjSetEditableDouble = null;
                if (mjEditableDoubleVal != null)
                {
                    mjGetEditableDouble = mjEditableDoubleVal.GetGetMethod();
                    mjSetEditableDouble = mjEditableDoubleVal.GetSetMethod();
                }
                if (mjGetEditableDouble == null)
                {
                    throw new NotImplementedException("mjGetEditableDouble");
                }
                getEditableDouble = DynamicMethodDelegateFactory.CreateFuncDouble(mjGetEditableDouble);
                if (mjSetEditableDouble == null)
                {
                    throw new NotImplementedException("mjSetEditableDouble");
                }
                setEditableDouble = DynamicMethodDelegateFactory.Create(mjSetEditableDouble);

                // OrbitalManeuverCalculator
                Type mjOrbitalManeuverCalculator_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.OrbitalManeuverCalculator");
                mjDeltaVAndTimeForHohmannTransfer = mjOrbitalManeuverCalculator_t.GetMethod("DeltaVAndTimeForHohmannTransfer", BindingFlags.Static | BindingFlags.Public);
                if (mjDeltaVAndTimeForHohmannTransfer == null)
                {
                    throw new NotImplementedException("mjDeltaVAndTimeForHohmannTransfer");
                }
                //deltaVAndTimeForHohmannTransfer = DynamicMethodDelegateFactory.Create(mjDeltaVAndTimeForHohmannTransfer);
                mjDeltaVAndTimeForInterplanetaryTransferEjection = mjOrbitalManeuverCalculator_t.GetMethod("DeltaVAndTimeForInterplanetaryTransferEjection", BindingFlags.Static | BindingFlags.Public);
                if (mjDeltaVAndTimeForInterplanetaryTransferEjection == null)
                {
                    throw new NotImplementedException("mjDeltaVAndTimeForInterplanetaryTransferEjection");
                }
                //deltaVAndTimeForInterplanetaryTransferEjection = DynamicMethodDelegateFactory.Create(mjDeltaVAndTimeForInterplanetaryTransferEjection);
                MethodInfo mjDeltaVToCircularize = mjOrbitalManeuverCalculator_t.GetMethod("DeltaVToCircularize", BindingFlags.Static | BindingFlags.Public);
                if (mjDeltaVToCircularize == null)
                {
                    throw new NotImplementedException("mjDeltaVToCircularize");
                }
                deltaVToCircularize = DynamicMethodDelegateFactory.Create(mjDeltaVToCircularize);

                // MechJebModuleStageStats
                Type mjModuleStageStats_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.MechJebModuleStageStats");
                if (mjModuleStageStats_t == null)
                {
                    throw new NotImplementedException("mjModuleStageStats_t");
                }
                MethodInfo mjRequestUpdate = mjModuleStageStats_t.GetMethod("RequestUpdate", BindingFlags.Instance | BindingFlags.Public);
                if (mjRequestUpdate == null)
                {
                    throw new NotImplementedException("mjRequestUpdate");
                }
                requestUpdate = DynamicMethodDelegateFactory.Create(mjRequestUpdate);
                mjVacStageStats = mjModuleStageStats_t.GetField("vacStats", BindingFlags.Instance | BindingFlags.Public);
                if (mjVacStageStats == null)
                {
                    throw new NotImplementedException("mjVacStageStats");
                }

                // Updated MechJeb (post 2.5.1) switched from using KER back to
                // its internal FuelFlowSimulation.  This sim uses an array of
                // structs, which entails a couple of extra hoops to jump through
                // when reading via reflection.
                mjAtmStageStats = mjModuleStageStats_t.GetField("atmoStats", BindingFlags.Instance | BindingFlags.Public);
                if (mjAtmStageStats == null)
                {
                    throw new NotImplementedException("mjAtmStageStats");
                }

                PropertyInfo mjStageStatsLength = mjVacStageStats.FieldType.GetProperty("Length");
                if (mjStageStatsLength == null)
                {
                    throw new NotImplementedException("mjStageStatsLength");
                }
                MethodInfo mjStageStatsGetLength = mjStageStatsLength.GetGetMethod();
                if (mjStageStatsGetLength == null)
                {
                    throw new NotImplementedException("mjStageStatsGetLength");
                }
                stageStatsGetLength = DynamicMethodDelegateFactory.CreateFuncInt(mjStageStatsGetLength);
                MethodInfo mjStageStatsGetIndex = mjVacStageStats.FieldType.GetMethod("Get");
                if (mjStageStatsGetIndex == null)
                {
                    throw new NotImplementedException("mjStageStatsGetIndex");
                }
                stageStatsGetIndex = DynamicMethodDelegateFactory.Create(mjStageStatsGetIndex);

                // MechJebModuleNodeExecutor
                Type mjNodeExecutor_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.MechJebModuleNodeExecutor");
                MethodInfo mjExecuteOneNode = mjNodeExecutor_t.GetMethod("ExecuteOneNode", BindingFlags.Instance | BindingFlags.Public);
                if (mjExecuteOneNode == null)
                {
                    throw new NotImplementedException("mjExecuteOneNode");
                }
                executeOneNode = DynamicMethodDelegateFactory.Create(mjExecuteOneNode);
                MethodInfo mjAbortNode = mjNodeExecutor_t.GetMethod("Abort", BindingFlags.Instance | BindingFlags.Public);
                if (mjAbortNode == null)
                {
                    throw new NotImplementedException("mjAbortNode");
                }
                abortNode = DynamicMethodDelegateFactory.CreateAction(mjAbortNode);

                // Computer Module
                Type mjComputerModule_t = loadedMechJebAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "MuMech.ComputerModule");
                PropertyInfo mjModuleEnabledProperty = mjComputerModule_t.GetProperty("enabled", BindingFlags.Instance | BindingFlags.Public);
                MethodInfo mjModuleEnabled = null;
                if (mjModuleEnabledProperty != null)
                {
                    mjModuleEnabled = mjModuleEnabledProperty.GetGetMethod();
                }
                if (mjModuleEnabled == null)
                {
                    throw new NotImplementedException("mjModuleEnabled");
                }
                moduleEnabled = DynamicMethodDelegateFactory.CreateFuncBool(mjModuleEnabled);
                mjModuleUsers = mjComputerModule_t.GetField("users", BindingFlags.Instance | BindingFlags.Public);
                if (mjModuleUsers == null)
                {
                    throw new NotImplementedException("mjModuleUsers");
                }
            }
            catch (Exception e)
            {
                mjMechJebCore_t = null;
                JUtil.LogMessage(null, "Exception initializing JSIMechJeb: {0}", e);
            }

            if (mjMechJebCore_t != null && getMasterMechJeb != null)
            {
                mjFound = true;
            }
            else
            {
                mjFound = false;
            }
        }
        static JSIPilotAssistant()
        {
            try
            {
                var loadedPAAssy = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.name == "PilotAssistant");
                if (loadedPAAssy == null)
                {
                    //JUtil.LogMessage(null, "Did not load PilotAssistant");
                    //var list = AssemblyLoader.loadedAssemblies;
                    //foreach(var a in list)
                    //{
                    //    JUtil.LogMessage(null, "-- {0}", a.name);
                    //}
                    return;
                }
                //else
                //{
                //    JUtil.LogMessage(null, "Did find PilotAssistant");
                //}

                Type paAsstVesselModule_t = loadedPAAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "PilotAssistant.FlightModules.AsstVesselModule");
                if (paAsstVesselModule_t == null)
                {
                    throw new NotImplementedException("paAsstVesselModule_t");
                }

                vesselAsst_t = paAsstVesselModule_t.GetField("vesselAsst", BindingFlags.Instance | BindingFlags.Public);
                if (vesselAsst_t == null)
                {
                    throw new NotImplementedException("vesselAsst_t");
                }

                Type paPilotAssistant_t = loadedPAAssy.assembly.GetExportedTypes()
                    .SingleOrDefault(t => t.FullName == "PilotAssistant.FlightModules.PilotAssistant");
                if (paPilotAssistant_t == null)
                {
                    throw new NotImplementedException("paPilotAssistant_t");
                }

                vertMode_t = paPilotAssistant_t.GetField("CurrentVertMode", BindingFlags.Instance | BindingFlags.Public);
                if (vertMode_t == null)
                {
                    throw new NotImplementedException("currentVertMode_t");
                }
                vertActive_t = paPilotAssistant_t.GetField("VertActive", BindingFlags.Instance | BindingFlags.Public);
                if (vertActive_t == null)
                {
                    throw new NotImplementedException("vertActive_t");
                }
                MethodInfo vertSetting_t = paPilotAssistant_t.GetMethod("GetCurrentVert", BindingFlags.Instance | BindingFlags.Public);
                if (vertSetting_t == null)
                {
                    throw new NotImplementedException("vertSetting_t");
                }
                GetCurrentVert = DynamicMethodDelegateFactory.CreateFuncDouble(vertSetting_t);
                MethodInfo setVert_t = paPilotAssistant_t.GetMethod("SetVert", BindingFlags.Instance | BindingFlags.Public);
                if (setVert_t == null)
                {
                    throw new NotImplementedException("setVert_t");
                }
                SetVert = DynamicMethodDelegateFactory.Create(setVert_t);

                horzMode_t = paPilotAssistant_t.GetField("CurrentHrztMode", BindingFlags.Instance | BindingFlags.Public);
                if (horzMode_t == null)
                {
                    throw new NotImplementedException("horzMode_t");
                }
                horzActive_t = paPilotAssistant_t.GetField("HrztActive", BindingFlags.Instance | BindingFlags.Public);
                if (horzActive_t == null)
                {
                    throw new NotImplementedException("horzActive_t");
                }
                MethodInfo horzSetting_t = paPilotAssistant_t.GetMethod("GetCurrentHrzt", BindingFlags.Instance | BindingFlags.Public);
                if (horzSetting_t == null)
                {
                    throw new NotImplementedException("horzSetting_t");
                }
                GetCurrentHorz = DynamicMethodDelegateFactory.CreateFuncDouble(horzSetting_t);
                MethodInfo setHorz_t = paPilotAssistant_t.GetMethod("SetHrzt", BindingFlags.Instance | BindingFlags.Public);
                if (setHorz_t == null)
                {
                    throw new NotImplementedException("setHorz_t");
                }
                SetHorz = DynamicMethodDelegateFactory.Create(setHorz_t);

                throttleMode_t = paPilotAssistant_t.GetField("CurrentThrottleMode", BindingFlags.Instance | BindingFlags.Public);
                if (throttleMode_t == null)
                {
                    throw new NotImplementedException("throttleMode_t");
                }
                throttleActive_t = paPilotAssistant_t.GetField("ThrtActive", BindingFlags.Instance | BindingFlags.Public);
                if (throttleActive_t == null)
                {
                    throw new NotImplementedException("throttleActive_t");
                }
                MethodInfo throttleSetting_t = paPilotAssistant_t.GetMethod("GetCurrentThrottle", BindingFlags.Instance | BindingFlags.Public);
                if (throttleSetting_t == null)
                {
                    throw new NotImplementedException("throttleSetting_t");
                }
                GetCurrentThrottle = DynamicMethodDelegateFactory.CreateFuncDouble(throttleSetting_t);
                MethodInfo setThrottle_t = paPilotAssistant_t.GetMethod("SetThrottle", BindingFlags.Instance | BindingFlags.Public);
                if (setThrottle_t == null)
                {
                    throw new NotImplementedException("setThrottle_t");
                }
                SetThrottle = DynamicMethodDelegateFactory.Create(setThrottle_t);

                speedRef_t = paPilotAssistant_t.GetField("speedRef", BindingFlags.Instance | BindingFlags.NonPublic);
                if (speedRef_t == null)
                {
                    throw new NotImplementedException("PA speedRef_t");
                }
                MethodInfo changeSpeedRef_t = paPilotAssistant_t.GetMethod("ChangeSpeedRef", BindingFlags.Instance | BindingFlags.Public);
                if (changeSpeedRef_t == null)
                {
                    throw new NotImplementedException("PA changeSpeedRef_t");
                }
                SetSpeedRef = DynamicMethodDelegateFactory.Create(changeSpeedRef_t);

                speedUnits_t = paPilotAssistant_t.GetField("units", BindingFlags.Instance | BindingFlags.NonPublic);
                if (speedUnits_t == null)
                {
                    throw new NotImplementedException("PA speedUnits_t");
                }
                MethodInfo changeSpeedUnits_t = paPilotAssistant_t.GetMethod("ChangeSpeedUnit", BindingFlags.Instance | BindingFlags.Public);
                if (changeSpeedUnits_t == null)
                {
                    throw new NotImplementedException("PA changeSpeedUnits_t");
                }
                SetSpeedUnits = DynamicMethodDelegateFactory.Create(changeSpeedUnits_t);

                pauseState_t = paPilotAssistant_t.GetField("bPause", BindingFlags.Instance | BindingFlags.Public);
                if (pauseState_t == null)
                {
                    throw new NotImplementedException("PA pauseState_t");
                }
                MethodInfo TogglePause_t = paPilotAssistant_t.GetMethod("TogglePauseCtrlState", BindingFlags.Instance | BindingFlags.Public);
                if (TogglePause_t == null)
                {
                    throw new NotImplementedException("PA TogglePause_t");
                }
                TogglePause = DynamicMethodDelegateFactory.CreateAction(TogglePause_t);
            }
            catch (Exception e)
            {
                JUtil.LogMessage(null, "Exception tripped: {0}", e);
                paFound = false;
                return;
            }

            paFound = true;
        }