Exemplo n.º 1
0
 public void save(WriteArchive archive)
 {
     archive.save(type);
     archive.save(id_field);
     archive.save(id_value);
     archive.save(id_index);
 }
Exemplo n.º 2
0
        public override void OnLoad(ConfigNode node)
        {
            // setups data from structured config node is only available at part compilation
            // for this reason, we parse it and then re-serialize it as a string
            if (HighLogic.LoadedScene == GameScenes.LOADING)
            {
                // parse all setups from config node and generate details
                setups = new List <ConfigureSetup>();
                foreach (var setup_node in node.GetNodes("SETUP"))
                {
                    setups.Add(new ConfigureSetup(setup_node, this));
                }

                // serialize the setups to string data
                var archive = new WriteArchive();
                archive.save(setups.Count);
                foreach (var setup in setups)
                {
                    setup.save(archive);
                }
                data = archive.serialize();

                // serialize empty configuration to string data
                archive = new WriteArchive();
                archive.save(0);
                cfg = archive.serialize();

                // serialize empty previous configuration to string data
                archive = new WriteArchive();
                archive.save(0);
                prev_cfg = archive.serialize();
            }
        }
Exemplo n.º 3
0
        public void save(WriteArchive archive)
        {
            // save basic data
            archive.save(name);
            archive.save(desc);
            archive.save(tech);
            archive.save(cost);
            archive.save(mass);

            // save modules
            archive.save(modules.Count);
            foreach (ConfigureModule m in modules)
            {
                m.save(archive);
            }

            // save resources
            archive.save(resources.Count);
            foreach (ConfigureResource r in resources)
            {
                r.save(archive);
            }

            // save details
            archive.save(details.Count);
            foreach (Detail det in details)
            {
                archive.save(det.label);
                archive.save(det.value);
            }
        }
Exemplo n.º 4
0
        public void Save(WriteArchive archive)
        {
            // save basic data
            archive.Save(name);
            archive.Save(desc);
            archive.Save(tech);
            archive.Save(cost);
            archive.Save(mass);

            // save modules
            archive.Save(modules.Count);
            foreach (ConfigureModule m in modules)
            {
                m.Save(archive);
            }

            // save resources
            archive.Save(resources.Count);
            foreach (ConfigureResource r in resources)
            {
                r.Save(archive);
            }
        }
Exemplo n.º 5
0
 public void save(WriteArchive archive)
 {
     archive.save(name);
     archive.save(amount);
     archive.save(maxAmount);
 }
Exemplo n.º 6
0
        public void configure()
        {
            // shortcut to resource library
            var reslib = PartResourceLibrary.Instance.resourceDefinitions;

            // reset extra cost and mass
            extra_cost = 0.0;
            extra_mass = 0.0;

            // find modules unlocked by tech
            unlocked = new List <ConfigureSetup>();
            foreach (ConfigureSetup setup in setups)
            {
                // if unlocked
                if (setup.tech.Length == 0 || Lib.HasTech(setup.tech))
                {
                    // unlock
                    unlocked.Add(setup);
                }
            }

            // make sure configuration include all available slots
            // this also create default configuration
            // - we don it only in the editor
            // - we avoid corner case when cfg was never set up (because craft was never in VAB)
            if (Lib.IsEditor() || selected.Count == 0)
            {
                while (selected.Count < Math.Min(slots, (uint)unlocked.Count))
                {
                    selected.Add(unlocked.Find(k => selected.IndexOf(k.name) == -1).name);
                }
            }

            // for each setup
            foreach (ConfigureSetup setup in setups)
            {
                // detect if the setup is selected
                bool active = selected.Contains(setup.name);

                // detect if the setup was previously selected
                bool prev_active = prev_selected.Contains(setup.name);

                // for each module specification in the setup
                foreach (ConfigureModule cm in setup.modules)
                {
                    // try to find the module
                    PartModule m = find_module(cm);

                    // if the module exist
                    if (m != null)
                    {
                        // call configure/deconfigure functions on module if available
                        IConfigurable configurable_module = m as IConfigurable;
                        if (configurable_module != null)
                        {
                            configurable_module.Configure(active);
                        }

                        // enable/disable the module
                        m.isEnabled = active;
                        m.enabled   = active;
                    }
                }

                // for each resource specification in the setup
                foreach (ConfigureResource cr in setup.resources)
                {
                    // ignore non-existing resources
                    if (!reslib.Contains(cr.name))
                    {
                        continue;
                    }

                    // get resource unit cost
                    double unit_cost = reslib[cr.name].unitCost;

                    // parse resource amount and capacity
                    double amount   = Lib.Parse.ToDouble(cr.amount);
                    double capacity = Lib.Parse.ToDouble(cr.maxAmount);

                    // (de)install resource
                    if ((prev_active != (active && capacity > 0.0)) || (reconfigure_cs && initialized))
                    {
                        // if previously selected
                        if (prev_active)
                        {
                            // remove the resources
                            Lib.RemoveResource(part, cr.name, amount, capacity);
                        }

                        // if selected
                        if (active && capacity > 0.0)
                        {
                            // add the resources
                            // - in flight, do not add amount
                            Lib.AddResource(part, cr.name, Lib.IsFlight() ? 0.0 : amount, capacity);
                        }
                    }

                    // add resource cost
                    if (active)
                    {
                        extra_cost += amount * unit_cost;
                    }
                }

                // add setup extra cost and mass
                if (active)
                {
                    extra_cost += setup.cost;
                    extra_mass += setup.mass;
                }
            }

            // remember previously selected setups
            prev_selected.Clear();
            foreach (string s in selected)
            {
                prev_selected.Add(s);
            }

            // save configuration
            WriteArchive archive = new WriteArchive();

            archive.save(selected.Count);
            foreach (string s in selected)
            {
                archive.save(s);
            }
            cfg = archive.serialize();

            // save previous configuration
            archive = new WriteArchive();
            archive.save(prev_selected.Count);
            foreach (string s in prev_selected)
            {
                archive.save(s);
            }
            prev_cfg = archive.serialize();

            // in the editor
            if (Lib.IsEditor())
            {
                // for each part in the symmetry group (avoid infinite recursion)
                if (!avoid_inf_recursion)
                {
                    avoid_inf_recursion = true;
                    foreach (Part p in part.symmetryCounterparts)
                    {
                        // get the Configure module
                        Configure c = p.FindModulesImplementing <Configure>().Find(k => k.title == title);

                        // both modules will share configuration
                        c.selected = selected;

                        // re-configure the other module
                        c.configure();
                    }
                    avoid_inf_recursion = false;
                }
            }

            // refresh this part ui
            MonoUtilities.RefreshContextWindows(part);

            // refresh VAB ui
            if (Lib.IsEditor())
            {
                GameEvents.onEditorShipModified.Fire(EditorLogic.fetch.ship);
            }

            // this was configured at least once
            initialized = true;
        }
Exemplo n.º 7
0
        void configure()
        {
            // store list of modules to remove
            var module_to_remove = new List <PartModule>();

            // store list of resources to remove
            var resource_to_remove = new List <PartResource>();

            // shortcut to resource library
            var reslib = PartResourceLibrary.Instance.resourceDefinitions;

            // reset extra cost and mass
            extra_cost = 0.0;
            extra_mass = 0.0;

            // find modules unlocked by tech
            unlocked = new List <ConfigureSetup>();
            foreach (ConfigureSetup setup in setups)
            {
                // if unlocked
                if (setup.tech.Length == 0 || Lib.HasTech(setup.tech))
                {
                    // unlock
                    unlocked.Add(setup);
                }
            }

            // make sure configuration include all available slots
            // this also create default configuration
            if (HighLogic.LoadedSceneIsEditor)
            {
                while (selected.Count < Math.Min(slots, (uint)unlocked.Count))
                {
                    selected.Add(unlocked.Find(k => selected.IndexOf(k.name) == -1).name);
                }
            }

            // for each setup
            foreach (ConfigureSetup setup in setups)
            {
                // detect if the setup is selected
                bool active = selected.Contains(setup.name);

                // for each module specification in the setup
                foreach (ConfigureModule cm in setup.modules)
                {
                    // try to find the module
                    PartModule m = find_module(cm);

                    // if the module exist
                    if (m != null)
                    {
                        // in the editor, enable/disable it
                        if (HighLogic.LoadedSceneIsEditor)
                        {
                            m.isEnabled = active;
                        }
                        // in flight, add to list of modules to remove
                        else if (Lib.SceneIsGame())
                        {
                            if (!active)
                            {
                                module_to_remove.Add(m);
                            }
                        }
                    }
                }

                // only in the editor
                if (HighLogic.LoadedSceneIsEditor)
                {
                    // for each resource specification in the setup
                    foreach (ConfigureResource cr in setup.resources)
                    {
                        // ignore non-existing resources
                        if (!reslib.Contains(cr.name))
                        {
                            continue;
                        }

                        // get resource unit cost
                        double unit_cost = reslib[cr.name].unitCost;

                        // parse resource amount and capacity
                        double amount   = Lib.Parse.ToDouble(cr.amount);
                        double capacity = Lib.Parse.ToDouble(cr.maxAmount);

                        // get resources to remove first
                        resource_to_remove.Clear();
                        foreach (PartResource res in part.Resources)
                        {
                            if (res.resourceName == cr.name)
                            {
                                resource_to_remove.Add(res);
                            }
                        }

                        // remove resources
                        foreach (PartResource r in resource_to_remove)
                        {
                            if (part.Resources.list.Contains(r))
                            {
                                part.Resources.list.Remove(r);
                                Destroy(r);
                            }
                        }

                        // if selected
                        if (active && capacity > 0.0)
                        {
                            // add the resource
                            ConfigNode res = new ConfigNode("RESOURCE");
                            res.AddValue("name", cr.name);
                            res.AddValue("amount", cr.amount);
                            res.AddValue("maxAmount", cr.maxAmount);
                            res.AddValue("isVisible", true);
                            res.AddValue("isTweakable", true);
                            part.Resources.Add(res);

                            // add extra cost
                            extra_cost += amount * unit_cost;
                        }
                    }
                }

                // add setup extra cost and mass
                if (active)
                {
                    extra_cost += setup.cost;
                    extra_mass += setup.mass;
                }
            }

            // remove non-selected modules
            foreach (PartModule m in module_to_remove)
            {
                part.RemoveModule(m);
            }

            // save configuration
            WriteArchive archive = new WriteArchive();

            archive.save(selected.Count);
            foreach (string s in selected)
            {
                archive.save(s);
            }
            cfg = archive.serialize();
        }