コード例 #1
0
        /// <summary>
        /// Adds tanks accodring to the configuration.
        /// </summary>
        /// <returns><c>true</c>, if configuration was added, <c>false</c> otherwise.</returns>
        /// <param name="cfg">Predefined configuration of tanks.</param>
        /// <param name="volume">Total volume of the configuration.</param>
        /// <param name="update_counterparts">If counterparts are to be updated.</param>
        public bool AddConfiguration(VolumeConfiguration cfg, float volume, bool update_counterparts = true)
        {
            if (!AddRemoveEnabled || !cfg.Valid)
            {
                return(false);
            }
            var V = cfg.TotalVolume;

            foreach (var v in cfg.Volumes)
            {
                var t = v as TankVolume;
                if (t != null)
                {
                    AddTank(t.TankType, volume * v.Volume / V, t.CurrentResource, t.InitialAmount, update_counterparts);
                    continue;
                }
                var c = v as VolumeConfiguration;
                if (c != null)
                {
                    AddConfiguration(c, volume * v.Volume / V, update_counterparts);
                    continue;
                }
            }
            return(true);
        }
コード例 #2
0
        protected override void init_from_part()
        {
            if (ModuleSave == null)
            {
                ModuleSave = new ConfigNode("MODULE");
            }
            var volume = VolumeConfiguration.FromResources(part.Resources);

            if (volume == null)
            {
                Utils.Message("TankManager module is added to a part with unknown resource!\n" +
                              "This is an error in MM patch.\n" +
                              "TankManager module is disabled.");
                this.EnableModule(false);
                part.Modules.Remove(this);
            }
            volume.name = ModuleSave.GetValue("name");
            ModuleSave.RemoveValue("Volume");
            ModuleSave.RemoveNodes(TankVolume.NODE_NAME);
            volume.Save(ModuleSave);
            Volume      = volume.Volume;
            DoCostPatch = false;
            DoMassPatch = true;
//            this.Log("ModuleSave was initialized from part in flight: {}", ModuleSave);//debug
        }
コード例 #3
0
        static void add_unique(VolumeConfiguration cfg, IDictionary <string, VolumeConfiguration> db)
        {
            int index    = 1;
            var basename = cfg.name;

            while (db.ContainsKey(cfg.name))
            {
                cfg.name = string.Concat(basename, " ", index++);
            }
            db.Add(cfg.name, cfg);
        }
コード例 #4
0
 public static void AddOrSave(VolumeConfiguration cfg)
 {
     if (UserConfigs.ContainsKey(cfg.name))
     {
         UserConfigs[cfg.name] = cfg;
     }
     else
     {
         UserConfigs.Add(cfg.name, cfg);
     }
     save_user_configs();
 }
        void volume_configs_gui()
        {
            if (!AddRemoveEnabled)
            {
                return;
            }
            GUILayout.BeginHorizontal();
            VolumeConfiguration cfg = null;

            GUILayout.Label("Configuration Name:", GUILayout.ExpandWidth(false));
            config_name = GUILayout.TextField(config_name, GUILayout.ExpandWidth(true), GUILayout.MinWidth(50));
            if (GUILayout.Button(VolumeConfigsLibrary.HaveUserConfig(config_name)? "Save" : "Add",
                                 Styles.add_button, GUILayout.ExpandWidth(false)) &&
                !string.IsNullOrEmpty(config_name))
            {
                //add new config
                var node = new ConfigNode();
                Save(node);
                cfg = ConfigNodeObject.FromConfig <VolumeConfiguration>(node);
                if (cfg.Valid)
                {
                    cfg.name   = config_name;
                    cfg.Volume = TotalVolume;
                    VolumeConfigsLibrary.AddOrSave(cfg);
                    init_supported_types();
                }
                else
                {
                    Utils.Log("Configuration is invalid:\n{}\nThis should never happen!", node);
                }
            }
            config_name = Utils.LeftRightChooser(config_name, VolumeConfigsLibrary.UserConfigs,
                                                 "Select tank configuration to edit", 200);
            if (config_name == null)
            {
                config_name = "";
            }
            if (GUILayout.Button("Delete", Styles.danger_button, GUILayout.ExpandWidth(false)) &&
                !string.IsNullOrEmpty(config_name))
            {
                //remove config
                if (VolumeConfigsLibrary.RemoveConfig(config_name))
                {
                    init_supported_types();
                }
                config_name = "";
            }
            GUILayout.EndHorizontal();
        }
コード例 #6
0
        public static VolumeConfiguration FromResources(IEnumerable <PartResource> resources)
        {
            var volume = new VolumeConfiguration();

            foreach (var res in resources)
            {
                var tank = TankVolume.FromResource(res);
                if (tank == null)
                {
                    return(null);
                }
                volume.Volumes.Add(tank);
            }
            volume.Volume = volume.TotalVolume;
            return(volume);
        }
コード例 #7
0
 public static void AddConfig(VolumeConfiguration cfg)
 {
     add_unique(cfg, UserConfigs);
     save_user_configs();
 }