Esempio n. 1
0
        private void CalculateTotalFeatureCost(MsiFeature parentFeature)
        {
            long totalCost = parentFeature.FeatureCost;

            if (parentFeature.ChildFeatures != null)
            {
                foreach (MsiFeature feature in parentFeature.ChildFeatures)
                {
                    CalculateTotalFeatureCost(feature);

                    totalCost += feature.FeatureCost;
                }
            }

            parentFeature.SetTotalCost(totalCost);
        }
Esempio n. 2
0
 internal void SetParent(MsiFeature parent)
 {
     this.parentFeature = parent;
 }
Esempio n. 3
0
        private void LoadFeatures()
        {
            using (MsiConnection conn = new MsiConnection(msiFile)) {
                conn.Open();

                // GET ALL FEATURES
                features.Clear();
                Dictionary <string, MsiFeature> dicFeatures = new Dictionary <string, MsiFeature>();
                using (MsiCommand cmd = new MsiCommand("SELECT Feature, Feature_Parent, Title, Description, Display, Directory_ FROM Feature", conn)) {
                    using (MsiDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            MsiFeature feature = new MsiFeature();
                            feature.SetId(reader.GetString(0));
                            feature.ParentId = reader.GetString(1);
                            feature.SetTitle(reader.GetString(2));
                            feature.SetDescription(reader.GetString(3));
                            feature.SetDisplay(reader.GetInteger(4));
                            feature.SetDirectory(reader.GetString(5));

                            dicFeatures.Add(feature.Id, feature);
                            features.Add(feature);
                        }
                    }
                }

                // CREATE FEATURE TREE
                // 1. Set all feature parent and child nodes
                foreach (MsiFeature feature in features)
                {
                    if (String.IsNullOrEmpty(feature.ParentId) == false)
                    {
                        feature.SetParent(dicFeatures[feature.ParentId]);
                        if (feature.ParentFeature.ChildFeatures == null)
                        {
                            feature.ParentFeature.SetChildFeature(new List <MsiFeature>());
                        }

                        feature.ParentFeature.ChildFeatures.Add(feature);
                    }
                }
                // 2. Get only root nodes
                featureTree.Clear();
                foreach (MsiFeature feature in features)
                {
                    if (feature.ParentFeature == null)
                    {
                        featureTree.Add(feature);
                    }
                }

                // SET INSTALLDIR
                using (MsiCommand cmd = new MsiCommand("SELECT Value FROM Property WHERE Property='INSTALLDIR'", conn)) {
                    using (MsiDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            installDir = reader.GetString(0);
                        }
                    }
                }

                // CALCULATE FEATURE COST
                // 1. Get all components
                Dictionary <string, MsiComponent> files = new Dictionary <string, MsiComponent>();
                using (MsiCommand cmd = new MsiCommand("SELECT Component_, FileSize FROM File", conn)) {
                    using (MsiDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            MsiComponent component = new MsiComponent();
                            component.Id       = reader.GetString(0);
                            component.FileSize = reader.GetInteger(1);

                            files.Add(component.Id, component);
                        }
                    }
                }

                // 2. Get feature component mapping
                Dictionary <string, List <string> > featureComponents = new Dictionary <string, List <string> >();
                using (MsiCommand cmd = new MsiCommand("SELECT Feature_, Component_ FROM FeatureComponents", conn)) {
                    using (MsiDataReader reader = cmd.ExecuteReader()) {
                        while (reader.Read())
                        {
                            string feature = reader.GetString(0);
                            if (featureComponents.ContainsKey(feature) == false)
                            {
                                featureComponents.Add(feature, new List <string>());
                            }
                            featureComponents[feature].Add(reader.GetString(1));
                        }
                    }
                }

                // 3. calculate feature cost
                foreach (string feature in featureComponents.Keys)
                {
                    int cost = 0;
                    foreach (string component in featureComponents[feature])
                    {
                        if (files.ContainsKey(component))
                        {
                            cost += files[component].FileSize;
                        }
                    }

                    dicFeatures[feature].SetFeatureCost(cost);
                }

                // 4. Get total feature cost
                foreach (MsiFeature feature in featureTree)
                {
                    CalculateTotalFeatureCost(feature);
                }
            }
        }