Example #1
0
        private static void ProcessApiModule(Vessel v, ProtoPartSnapshot p, ProtoPartModuleSnapshot m,
                                             Part part_prefab, PartModule module_prefab, VesselResources resources, Dictionary <string, double> availableResources, List <KeyValuePair <string, double> > resourceChangeRequests, double elapsed_s)
        {
            resourceChangeRequests.Clear();

            try
            {
                string title = BackgroundDelegate.Instance(module_prefab).invoke(v, p, m, module_prefab, part_prefab, availableResources, resourceChangeRequests, elapsed_s);

                foreach (var cr in resourceChangeRequests)
                {
                    if (cr.Value > 0)
                    {
                        resources.Produce(v, cr.Key, cr.Value * elapsed_s, ResourceBroker.GetOrCreate(title));
                    }
                    else if (cr.Value < 0)
                    {
                        resources.Consume(v, cr.Key, -cr.Value * elapsed_s, ResourceBroker.GetOrCreate(title));
                    }
                }
            }
            catch (Exception ex)
            {
                Lib.Log("BackgroundUpdate in PartModule " + module_prefab.moduleName + " excepted: " + ex.Message + "\n" + ex.ToString());
            }
        }
Example #2
0
            public static BackgroundDelegate Instance(PartModule module_prefab)
            {
                BackgroundDelegate result = null;

                var type = module_prefab.GetType();

                supportedModules.TryGetValue(type, out result);
                if (result != null)
                {
                    return(result);
                }

                if (unsupportedModules.Contains(type))
                {
                    return(null);
                }

                MethodInfo methodInfo = type.GetMethod("BackgroundUpdate", signature);

                if (methodInfo == null)
                {
                    unsupportedModules.Add(type);
                    return(null);
                }

                result = new BackgroundDelegate(methodInfo);
                supportedModules[type] = result;
                return(result);
            }
Example #3
0
        private static List <BackgroundPM> Background_PMs(Vessel v)
        {
            var result = Cache.VesselObjectsCache <List <BackgroundPM> >(v, "background");

            if (result != null)
            {
                return(result);
            }

            result = new List <BackgroundPM>();

            // store data required to support multiple modules of same type in a part
            var PD = new Dictionary <string, Lib.Module_prefab_data>();

            // for each part
            foreach (ProtoPartSnapshot p in v.protoVessel.protoPartSnapshots)
            {
                // get part prefab (required for module properties)
                Part part_prefab = PartLoader.getPartInfoByName(p.partName).partPrefab;

                // get all module prefabs
                var module_prefabs = part_prefab.FindModulesImplementing <PartModule>();

                // clear module indexes
                PD.Clear();

                // for each module
                foreach (ProtoPartModuleSnapshot m in p.modules)
                {
                    // TODO : this is to migrate pre-3.1 saves using WarpFixer to the new SolarPanelFixer. At some point in the future we can remove this code.
                    if (m.moduleName == "WarpFixer")
                    {
                        MigrateWarpFixer(v, part_prefab, p, m);
                    }

                    // get the module prefab
                    // if the prefab doesn't contain this module, skip it
                    PartModule module_prefab = Lib.ModulePrefab(module_prefabs, m.moduleName, PD);
                    if (!module_prefab)
                    {
                        continue;
                    }

                    // if the module is disabled, skip it
                    // note: this must be done after ModulePrefab is called, so that indexes are right
                    if (!Lib.Proto.GetBool(m, "isEnabled"))
                    {
                        continue;
                    }

                    // get module type
                    // if the type is unknown, skip it
                    Module_type type = ModuleType(m.moduleName);
                    if (type == Module_type.Unknown)
                    {
                        var backgroundDelegate = BackgroundDelegate.Instance(module_prefab);
                        if (backgroundDelegate != null)
                        {
                            type = Module_type.APIModule;
                        }
                        else
                        {
                            continue;
                        }
                    }

                    var entry = new BackgroundPM();
                    entry.p             = p;
                    entry.m             = m;
                    entry.module_prefab = module_prefab;
                    entry.part_prefab   = part_prefab;
                    entry.type          = type;
                    result.Add(entry);
                }
            }

            Cache.SetVesselObjectsCache(v, "background", result);
            return(result);
        }