public override void OnInitialize()
 {
     if (type.Equals("ModuleEnginesFX"))
         ActiveEngine = new EngineWrapper((ModuleEnginesFX)part.Modules[type]);
     else if (type.Equals("ModuleEngines"))
         ActiveEngine = new EngineWrapper((ModuleEngines)part.Modules[type]);
     else
         print("*RF* trying to start " + part.name + " but is neither ME nor MEFX! (type = " + type + ")");
     SetConfiguration(configuration);
 }
        public override void OnStart (StartState state)
        {
            if(configs.Count == 0 && part.partInfo != null
               && part.partInfo.partPrefab.Modules.Contains ("ModuleHybridEngine")) {
                ModuleHybridEngine prefab = (ModuleHybridEngine) part.partInfo.partPrefab.Modules["ModuleHybridEngine"];
                configs = new List<ConfigNode>();
                foreach (ConfigNode subNode in prefab.configs)
                {
                    ConfigNode newNode = new ConfigNode("CONFIG");
                    subNode.CopyTo(newNode);
                    configs.Add(newNode);
                }
            }
            if (type.Equals("ModuleEnginesFX"))
                ActiveEngine = new EngineWrapper((ModuleEnginesFX)part.Modules[type]);
            else if (type.Equals("ModuleEngines"))
                ActiveEngine = new EngineWrapper((ModuleEngines)part.Modules[type]);
            else
                print("*RF* trying to start " + part.name + " but is neither ME nor MEFX! (type = " + type + ")");

            SetConfiguration(configuration);
            if (part.Modules.Contains("ModuleEngineIgnitor"))
                part.Modules["ModuleEngineIgnitor"].OnStart(state);
        }
        override public void SetConfiguration(string newConfiguration = null)
        {
            if (newConfiguration == null)
                newConfiguration = configuration;
            ConfigNode newConfig = configs.Find (c => c.GetValue ("name").Equals (newConfiguration));
            pModule = part.Modules[type];
            if (newConfig == null || pModule == null)
                return;

            // fix for HotRockets etc.
            if (type.Equals("ModuleEngines") && part.Modules.Contains("ModuleEnginesFX") && !part.Modules.Contains("ModuleEngines"))
                type = "ModuleEnginesFX";

            if (type.Equals("ModuleEnginesFX"))
                ActiveEngine = new EngineWrapper((ModuleEnginesFX)part.Modules[type]);
            else if (type.Equals("ModuleEngines"))
                ActiveEngine = new EngineWrapper((ModuleEngines)part.Modules[type]);
            else
                print("*RF* trying to start " + part.name + " but is neither ME nor MEFX! (type = " + type + ")");

            ActiveEngine.g = 9.80665f;

            Fields ["configuration"].guiActive = true;
            Fields ["configuration"].guiName = "Current Mode";

            configuration = newConfiguration;
            config = new ConfigNode ("MODULE");
            newConfig.CopyTo (config);
            config.name = "MODULE";
            config.SetValue ("name", type);

            // clear all relevant FloatCurves
            Type mType = pModule.GetType();
            foreach (FieldInfo field in mType.GetFields())
            {
                if (field.FieldType == typeof(FloatCurve) && (field.Name.Equals("atmosphereCurve") || field.Name.Equals("velocityCurve")))
                {
                    //print("*MFS* resetting curve " + field.Name);
                    field.SetValue(pModule, new FloatCurve());
                }
            }
            // clear propellant gauges Squad made
            foreach (FieldInfo field in mType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
            {
                if (field.FieldType == typeof(Dictionary<Propellant, VInfoBox>))
                {
                    Dictionary<Propellant, VInfoBox> boxes = (Dictionary<Propellant, VInfoBox>)(field.GetValue(pModule));
                    if (boxes == null)
                        continue;
                    foreach (VInfoBox v in boxes.Values)
                    {
                        if (v == null) //just in case...
                            continue;
                        try
                        {
                            part.stackIcon.RemoveInfo(v);
                        }
                        catch (Exception e)
                        {
                            print("*MFS* Trying to remove info box: " + e.Message);
                        }
                    }
                    boxes.Clear();
                }
            }
            

            bool engineActive = ActiveEngine.getIgnitionState;
            ActiveEngine.EngineIgnited = false;

            //  remove all fuel gauges we made
            ClearMeters ();
            propellants.Clear ();

            if (type.Equals("ModuleEngines"))
            {
                ModuleEngines mE = (ModuleEngines)pModule;
                if (mE != null)
                {
                    configMaxThrust = mE.maxThrust;
                    configMinThrust = mE.minThrust;
                    fastEngines = mE;
                    fastType = ModuleType.MODULEENGINES;
                }
                if (config.HasValue("maxThrust"))
                {
                    float thr;
                    if (float.TryParse(config.GetValue("maxThrust"), out thr))
                        configMaxThrust = thr;
                }
                if (config.HasValue("minThrust"))
                {
                    float thr;
                    if (float.TryParse(config.GetValue("minThrust"), out thr))
                        configMinThrust = thr;
                }
            }
            else if (type.Equals("ModuleEnginesFX"))
            {
                ModuleEnginesFX mE = (ModuleEnginesFX)pModule;
                if (mE != null)
                {
                    configMaxThrust = mE.maxThrust;
                    configMinThrust = mE.minThrust;
                    fastEnginesFX = mE;
                    fastType = ModuleType.MODULEENGINESFX;
                }
                if (config.HasValue("maxThrust"))
                {
                    float thr;
                    if (float.TryParse(config.GetValue("maxThrust"), out thr))
                        configMaxThrust = thr;
                }
                if (config.HasValue("minThrust"))
                {
                    float thr;
                    if (float.TryParse(config.GetValue("minThrust"), out thr))
                        configMinThrust = thr;
                }
            }

            DoConfig(config); // from MEC

            //  load the new engine state
            pModule.Load(config);

            // I'd think the load, above, would do this already. So maybe unnecessary?
            if (config.HasValue ("useVelocityCurve") && (config.GetValue ("useVelocityCurve").ToLowerInvariant () == "true")) {
                ActiveEngine.velocityCurve.Load (config.GetNode ("velocityCurve"));
            } else {
                ActiveEngine.useVelocityCurve = false;
            }

            //  set up propellants
            foreach (Propellant propellant in ActiveEngine.propellants) {
                if(propellant.drawStackGauge) { // we need to handle fuel gauges ourselves
                    propellant.drawStackGauge = false;
                    propellants.Add (propellant);
                }
            }
            ActiveEngine.SetupPropellant ();

            if (engineActive)
                ActiveEngine.Actions ["ActivateAction"].Invoke (new KSPActionParam (KSPActionGroup.None, KSPActionType.Activate));

            UpdateTweakableMenu();
        }