Exemplo n.º 1
0
        /* Latest versions of FAR emulate different chemical composition of
         * atmospheres on various planets, which causes changes in density
         * and speed of sound compared to stock. Thus, for full compatibility
         * it is necessary to try calling the FAR functions if it is loaded.
         */
        public static void InitFAR()
        {
            if (checkedForFAR)
            {
                return;
            }

            checkedForFAR = true;
            try
            {
                if (SteamGauges.debug)
                {
                    Debug.Log("(S)G Initializing FAR interaction...");
                }
                var loadedAssembly = AssemblyLoader.loadedAssemblies.FirstOrDefault(a => a.assembly.GetName().Name == "FerramAerospaceResearch");
                if (SteamGauges.debug)
                {
                    Debug.Log("(SG) FAR Assembly loaded.");
                }
                if (loadedAssembly == null)
                {
                    return;
                }
                if (SteamGauges.debug)
                {
                    Debug.Log("(SG) Loading FAR utilities...");
                }
                var type = loadedAssembly.assembly.GetTypes().FirstOrDefault(t => t.FullName.Equals("ferram4.FARAeroUtil"));
                if (type == null)
                {
                    return;
                }
                if (SteamGauges.debug)
                {
                    Debug.Log("(SG) Loading FAR controls...");
                }
                Type type_api = loadedAssembly.assembly.GetTypes().FirstOrDefault(t => t.FullName.Equals("ferram4.FARAPI"));
                Type type_cs  = loadedAssembly.assembly.GetTypes().FirstOrDefault(t => t.FullName.Equals("ferram4.FARControlSys"));

                if (SteamGauges.debug)
                {
                    Debug.Log("(SG) Loading FAR functions...");
                }
                far_GetMachNumber     = (FAR_GetMachNumber)Delegate.CreateDelegate(typeof(FAR_GetMachNumber), type, "GetMachNumber", false, false);
                far_GetCurrentDensity = (FAR_GetCurrentDensity)Delegate.CreateDelegate(typeof(FAR_GetCurrentDensity), type, "GetCurrentDensity", false, false);

                if (far_GetCurrentDensity != null)
                {
                    foreach (var body in FlightGlobals.Bodies)
                    {
                        if (body.name == "Kerbin")
                        {
                            invKerbinSLDensity = 1 / far_GetCurrentDensity(body, 0);
                        }
                    }
                }

                if (type_api != null)
                {
                    far_GetTermVel = (FAR_GetTermVel)Delegate.CreateDelegate(typeof(FAR_GetTermVel), type_api, "GetActiveControlSys_TermVel", false, false);
                }

                if (type_cs != null && far_GetTermVel == null)
                {
                    var tvel = type_cs.GetField("termVel", BindingFlags.Static | BindingFlags.Public | BindingFlags.NonPublic);
                    if (tvel != null && tvel.GetType() == typeof(double))
                    {
                        far_GetTermVel = () => (double)tvel.GetValue(null);
                    }
                }

                if (SteamGauges.debug)
                {
                    Debug.Log("(SG) AirGauge: Successfully attached to FAR");
                }
            }
            catch (Exception e)
            {
                if (SteamGauges.debug)
                {
                    Debug.Log("(SG) AirGauge: Error retrieving FAR method references: " + e);
                }
            }
        }
Exemplo n.º 2
0
        public static bool RegisterWithFAR()
        {
            try
            {
                Type FARWind      = null;
                Type WindFunction = null;

                foreach (var assembly in AssemblyLoader.loadedAssemblies)
                {
                    if (assembly.name == "FerramAerospaceResearch")
                    {
                        var types = assembly.assembly.GetExportedTypes();

                        foreach (Type t in types)
                        {
                            if (t.FullName.Equals("FerramAerospaceResearch.FARWind"))
                            {
                                FARWind = t;
                            }
                            if (t.FullName.Equals("FerramAerospaceResearch.FARWind+WindFunction"))
                            {
                                WindFunction = t;
                            }
                            if (t.FullName.Equals("FerramAerospaceResearch.FARAeroUtil"))
                            {
                                far_GetMachNumber     = (FAR_GetMachNumber)Delegate.CreateDelegate(typeof(FAR_GetMachNumber), t, "GetMachNumber", false, false);
                                far_GetCurrentDensity = (FAR_GetCurrentDensity)Delegate.CreateDelegate(typeof(FAR_GetCurrentDensity), t, "GetCurrentDensity", false, false);
                            }
                        }
                    }
                }
                if (FARWind == null)
                {
                    return(false);
                }
                if (WindFunction == null)
                {
                    return(false);
                }
                MethodInfo SetWindFunction = FARWind.GetMethod("SetWindFunction");
                if (SetWindFunction == null)
                {
                    return(false);
                }
                if (Util.useCLIM())
                {
                    var del = Delegate.CreateDelegate(WindFunction, new KerbalWxClimo(), typeof(KerbalWxClimo).GetMethod("GetTheWind"), true);
                    SetWindFunction.Invoke(null, new object[] { del });
                }
                else
                {
                    var del = Delegate.CreateDelegate(WindFunction, new KerbalWxPoint(), typeof(KerbalWxClimo).GetMethod("GetTheWind"), true);
                    SetWindFunction.Invoke(null, new object[] { del });
                }
                return(true); // jump out
            }
            catch (Exception e)
            {
                Debug.LogError("KerbalWeatherProject: unable to register with FerramAerospaceResearch. Exception thrown: " + e.ToString());
            }
            return(false);
        }