Beispiel #1
0
 public ParameterDelegate(string title, Func <T, bool> filterFunc, bool trivial, ParameterDelegateMatchType matchType = ParameterDelegateMatchType.FILTER)
     : base()
 {
     this.title           = title;
     this.filterFunc      = filterFunc;
     this.matchType       = matchType;
     this.trivial         = trivial;
     disableOnStateChange = false;
 }
Beispiel #2
0
        public static string Prefix(this ParameterDelegateMatchType type)
        {
            switch (type)
            {
            case ParameterDelegateMatchType.FILTER:
                return("With ");

            case ParameterDelegateMatchType.VALIDATE_ALL:
                return("All have ");

            case ParameterDelegateMatchType.NONE:
                return("None have ");
            }
            return(null);
        }
        public static string Prefix(this ParameterDelegateMatchType type)
        {
            switch (type)
            {
            case ParameterDelegateMatchType.FILTER:
            case ParameterDelegateMatchType.VALIDATE:
                return(Localizer.GetStringByTag("#cc.param.ParameterDelegate.MatchExtension.validate"));

            case ParameterDelegateMatchType.VALIDATE_ALL:
                return(Localizer.GetStringByTag("#cc.param.ParameterDelegate.MatchExtension.all"));

            case ParameterDelegateMatchType.NONE:
                return(Localizer.GetStringByTag("#cc.param.ParameterDelegate.MatchExtension.none"));
            }
            return(null);
        }
Beispiel #4
0
 public ParameterDelegate(string title, Func <T, bool> filterFunc, ParameterDelegateMatchType matchType = ParameterDelegateMatchType.FILTER, bool trivial = false)
     : this(title, filterFunc, trivial, matchType)
 {
 }
 public Filter(ParameterDelegateMatchType type)
 {
     this.type = type;
 }
 public AllParameterDelegate(string title, ParameterDelegateMatchType matchType = ParameterDelegateMatchType.FILTER)
     : base(title, null, matchType)
 {
     filterFunc = (t => this.AllChildParametersComplete());
 }
 public Filter(ParameterDelegateMatchType type)
 {
     this.type = type;
 }
Beispiel #8
0
        protected void CreateDelegates()
        {
            ParameterDelegateMatchType matchType = ParameterDelegateMatchType.FILTER;

            if (maxCount == 0)
            {
                matchType = ParameterDelegateMatchType.NONE;
            }

            // Filter by type
            if (antennaType == AntennaType.Dish)
            {
                AddParameter(new ParameterDelegate <IAntenna>("Type: " + antennaType,
                                                              a => a.CanTarget, matchType));
            }
            else if (antennaType == AntennaType.Omni)
            {
                AddParameter(new ParameterDelegate <IAntenna>("Type: " + antennaType,
                                                              a => !a.CanTarget, matchType));
            }

            // Filter for active vessel
            if (activeVessel)
            {
                AddParameter(new ParameterDelegate <IAntenna>("Target: Active vessel",
                                                              a => a.Target == NetworkManager.ActiveVesselGuid || a.Omni > 0.0, matchType));
            }
            // Filter for celestial bodies
            else if (targetBody != null)
            {
                AddParameter(new ParameterDelegate <IAntenna>("Target: " + targetBody.CleanDisplayName(),
                                                              a => a.Target == targetBody.Guid(), matchType));
            }

            // Activated and powered
            AddParameter(new ParameterDelegate <IAntenna>("Activated", a => a.Activated, matchType, true));
            AddParameter(new ParameterDelegate <IAntenna>("Powered", a => a.Powered, matchType, true));

            // Filter for range
            if (minRange != 0.0 || maxRange != double.MaxValue)
            {
                string output = "Range: ";
                if (maxRange == double.MaxValue)
                {
                    output += "At least " + RemoteTechAssistant.RangeString(minRange);
                }
                else if (minRange == 0)
                {
                    output += "At most " + RemoteTechAssistant.RangeString(maxRange);
                }
                else
                {
                    output += "Between " + RemoteTechAssistant.RangeString(minRange) + " and " + RemoteTechAssistant.RangeString(maxRange);
                }

                AddParameter(new ParameterDelegate <IAntenna>(output,
                                                              a => Math.Max(a.Omni, a.Dish) >= minRange && Math.Max(a.Omni, a.Dish) <= maxRange, matchType));
            }

            // Extra filter for celestial bodies
            if (!activeVessel && targetBody != null)
            {
                double distance = (Planetarium.fetch.Home.position - targetBody.position).magnitude;
                AddParameter(new ParameterDelegate <IAntenna>("Range: In range of " + targetBody.CleanDisplayName(true),
                                                              a => Math.Max(a.Omni, a.Dish) >= distance, matchType, true));
            }

            // Validate count
            if (minCount != 0 || maxCount != int.MaxValue && !(minCount == maxCount && maxCount == 0))
            {
                AddParameter(new CountParameterDelegate <IAntenna>(minCount, maxCount));
            }
        }
        protected void CreateDelegates()
        {
            ParameterDelegateMatchType matchType = ParameterDelegateMatchType.FILTER;

            if (maxCount == 0)
            {
                matchType = ParameterDelegateMatchType.NONE;
            }

            // Filter by type
            if (antennaType == AntennaType.Dish)
            {
                AddParameter(new ParameterDelegate <IAntenna>(Localizer.Format("#cc.remotetech.param.HasAntenna.type", antennaType.displayDescription()),
                                                              a => a.CanTarget, matchType));
            }
            else if (antennaType == AntennaType.Omni)
            {
                AddParameter(new ParameterDelegate <IAntenna>(Localizer.Format("#cc.remotetech.param.HasAntenna.type", antennaType.displayDescription()),
                                                              a => !a.CanTarget, matchType));
            }

            // Filter for active vessel
            if (activeVessel)
            {
                AddParameter(new ParameterDelegate <IAntenna>(Localizer.Format("#cc.remotetech.param.HasAntenna.target", Localizer.GetStringByTag("#RT_ModuleUI_ActiveVessel")),
                                                              a => a.Target == NetworkManager.ActiveVesselGuid || a.Omni > 0.0, matchType));
            }
            // Filter for celestial bodies
            else if (targetBody != null)
            {
                AddParameter(new ParameterDelegate <IAntenna>(Localizer.Format("#cc.remotetech.param.HasAntenna.target", targetBody.displayName),
                                                              a => a.Target == targetBody.Guid(), matchType));
            }

            // Activated and powered
            AddParameter(new ParameterDelegate <IAntenna>(Localizer.GetStringByTag("#cc.remotetech.param.HasAntenna.activated"), a => a.Activated, matchType, true));
            AddParameter(new ParameterDelegate <IAntenna>(Localizer.GetStringByTag("#cc.remotetech.param.HasAntenna.powered"), a => a.Powered, matchType, true));

            // Filter for range
            if (minRange != 0.0 || maxRange != double.MaxValue)
            {
                string countStr;
                if (maxRange == double.MaxValue)
                {
                    countStr = Localizer.Format("#cc.param.count.atLeast", RemoteTechAssistant.RangeString(minRange));
                }
                else if (minRange == 0)
                {
                    countStr = Localizer.Format("#cc.param.count.atMost", RemoteTechAssistant.RangeString(maxRange));
                }
                else
                {
                    countStr = Localizer.Format("#cc.param.count.between", RemoteTechAssistant.RangeString(minRange), RemoteTechAssistant.RangeString(maxRange));
                }

                AddParameter(new ParameterDelegate <IAntenna>(Localizer.Format("#cc.remotetech.param.HasAntenna.range", countStr),
                                                              a => Math.Max(a.Omni, a.Dish) >= minRange && Math.Max(a.Omni, a.Dish) <= maxRange, matchType));
            }

            // Extra filter for celestial bodies
            if (!activeVessel && targetBody != null)
            {
                double distance = (Planetarium.fetch.Home.position - targetBody.position).magnitude;
                AddParameter(new ParameterDelegate <IAntenna>(Localizer.Format("#cc.remotetech.param.HasAntenna.range.body", targetBody.displayName),
                                                              a => Math.Max(a.Omni, a.Dish) >= distance, matchType, true));
            }

            // Validate count
            if (minCount != 0 || maxCount != int.MaxValue && !(minCount == maxCount && maxCount == 0))
            {
                AddParameter(new CountParameterDelegate <IAntenna>(minCount, maxCount));
            }
        }
        public override bool Load(ConfigNode configNode)
        {
            // Load base class
            bool valid = base.Load(configNode);

            // Read min/max first
            valid &= ConfigNodeUtil.ParseValue <int?>(configNode, "minCount", x => minCount = x, this,
                                                      configNode.HasNode("VALIDATE") || configNode.HasNode("VALIDATE_ALL") || configNode.HasNode("NONE") ? (int?)null : 1, x => x == null || Validation.GE(x.Value, 0));
            valid &= ConfigNodeUtil.ParseValue <int>(configNode, "maxCount", x => maxCount = x, this, minCount != null && minCount.Value == 0 ? 0 : int.MaxValue, x => Validation.GE(x, 0));

            // Set the default match type
            ParameterDelegateMatchType defaultMatch = ParameterDelegateMatchType.FILTER;

            if (maxCount == 0)
            {
                defaultMatch = ParameterDelegateMatchType.NONE;
            }

            // Standard definition
            if (configNode.HasValue("part") || configNode.HasValue("partModule") || configNode.HasValue("partModuleType") || configNode.HasValue("category") || configNode.HasValue("manufacturer"))
            {
                PartValidation.Filter filter = new PartValidation.Filter(defaultMatch);
                valid &= ConfigNodeUtil.ParseValue <List <AvailablePart> >(configNode, "part", x => filter.parts = x, this, new List <AvailablePart>());
                valid &= ConfigNodeUtil.ParseValue <List <string> >(configNode, "partModule", x => filter.partModules = x, this, new List <string>(), x => x.All(Validation.ValidatePartModule));
                valid &= ConfigNodeUtil.ParseValue <List <string> >(configNode, "partModuleType", x => filter.partModuleTypes = x, this, new List <string>(), x => x.All(Validation.ValidatePartModuleType));
                valid &= ConfigNodeUtil.ParseValue <PartCategories?>(configNode, "category", x => filter.category = x, this, (PartCategories?)null);
                valid &= ConfigNodeUtil.ParseValue <string>(configNode, "manufacturer", x => filter.manufacturer = x, this, (string)null);

                // Add modules
                foreach (ConfigNode moduleNode in configNode.GetNodes("MODULE"))
                {
                    ConfigNode.ValueList tmp = new ConfigNode.ValueList();
                    foreach (ConfigNode.Value v in moduleNode.values)
                    {
                        tmp.Add(new ConfigNode.Value(v.name, v.value));
                    }
                    filter.partModuleExtended.Add(tmp);
                }

                filters.Add(filter);
            }

            // Extended definition
            foreach (ConfigNode child in ConfigNodeUtil.GetChildNodes(configNode))
            {
                ParameterDelegateMatchType matchType;
                if (child.name == "FILTER")
                {
                    matchType = ParameterDelegateMatchType.FILTER;
                }
                else if (child.name == "VALIDATE")
                {
                    matchType = ParameterDelegateMatchType.VALIDATE;
                }
                else if (child.name == "VALIDATE_ALL")
                {
                    matchType = ParameterDelegateMatchType.VALIDATE_ALL;
                }
                else if (child.name == "NONE")
                {
                    matchType = ParameterDelegateMatchType.NONE;
                }
                else
                {
                    LoggingUtil.LogError(this, ErrorPrefix() + ": unexpected node '" + child.name + "'.");
                    valid = false;
                    continue;
                }

                if (defaultMatch == ParameterDelegateMatchType.NONE)
                {
                    matchType = ParameterDelegateMatchType.NONE;
                }

                PartValidation.Filter filter = new PartValidation.Filter(matchType);
                valid &= ConfigNodeUtil.ParseValue <List <AvailablePart> >(child, "part", x => filter.parts = x, this, new List <AvailablePart>());
                valid &= ConfigNodeUtil.ParseValue <List <string> >(child, "partModule", x => filter.partModules = x, this, new List <string>(), x => x.All(Validation.ValidatePartModule));
                valid &= ConfigNodeUtil.ParseValue <List <string> >(child, "partModuleType", x => filter.partModuleTypes = x, this, new List <string>());
                valid &= ConfigNodeUtil.ParseValue <PartCategories?>(child, "category", x => filter.category = x, this, (PartCategories?)null);
                valid &= ConfigNodeUtil.ParseValue <string>(child, "manufacturer", x => filter.manufacturer = x, this, (string)null);

                foreach (ConfigNode moduleNode in child.GetNodes("MODULE"))
                {
                    ConfigNode.ValueList tmp = new ConfigNode.ValueList();
                    foreach (ConfigNode.Value v in moduleNode.values)
                    {
                        tmp.Add(new ConfigNode.Value(v.name, v.value));
                    }
                    filter.partModuleExtended.Add(tmp);
                }

                if (matchType == ParameterDelegateMatchType.VALIDATE)
                {
                    valid &= ConfigNodeUtil.ParseValue <int>(child, "minCount", x => filter.minCount = x, this, 1, x => Validation.GE(x, 0));
                    valid &= ConfigNodeUtil.ParseValue <int>(child, "maxCount", x => filter.maxCount = x, this, int.MaxValue, x => Validation.GE(x, 0));
                }

                filters.Add(filter);
            }

            return(valid);
        }
        protected void CreateDelegates()
        {
            foreach (Filter filter in filters)
            {
                if (filter.type == ParameterDelegateMatchType.VALIDATE)
                {
                    foreach (AvailablePart part in filter.parts)
                    {
                        AddParameter(new CountParameterDelegate <Part>(filter.minCount, filter.maxCount, p => p.partInfo.name == part.name,
                                                                       part.title, false));
                    }

                    // Filter by part modules
                    foreach (string partModule in filter.partModules)
                    {
                        AddParameter(new CountParameterDelegate <Part>(filter.minCount, filter.maxCount, p => PartHasModule(p, partModule),
                                                                       "with module: " + ModuleName(partModule), false));
                    }

                    // Filter by part module types
                    foreach (string partModuleType in filter.partModuleTypes)
                    {
                        AddParameter(new CountParameterDelegate <Part>(filter.minCount, filter.maxCount, p => PartHasObjective(p, partModuleType),
                                                                       "with module type: " + partModuleType, false));
                    }
                }
                else
                {
                    // Filter by part
                    if (filter.parts.Any())
                    {
                        AddParameter(new ParameterDelegate <Part>(filter.type.Prefix() + "type: " +
                                                                  filter.parts.Select(p => p.title).Aggregate((sum, s) => sum + " or " + s),
                                                                  p => filter.parts.Any(pp => p.partInfo.name == pp.name), filter.type));
                    }

                    // Filter by part modules
                    foreach (string partModule in filter.partModules)
                    {
                        AddParameter(new ParameterDelegate <Part>(filter.type.Prefix() + "module: " + ModuleName(partModule), p => PartHasModule(p, partModule), filter.type));
                    }

                    // Filter by part modules
                    foreach (string partModuleType in filter.partModuleTypes)
                    {
                        AddParameter(new ParameterDelegate <Part>(filter.type.Prefix() + "module type: " + partModuleType, p => PartHasObjective(p, partModuleType), filter.type));
                    }

                    // Filter by part modules - extended mode
                    foreach (ConfigNode.ValueList list in filter.partModuleExtended)
                    {
                        ContractParameter wrapperParam = AddParameter(new AllParameterDelegate <Part>(filter.type.Prefix() + "module", filter.type));

                        foreach (ConfigNode.Value v in list)
                        {
                            string name = Regex.Replace(v.name, @"([A-Z]+?(?=[A-Z][^A-Z])|\B[A-Z]+?(?=[^A-Z]))", " $1");
                            name = name.Substring(0, 1).ToUpper() + name.Substring(1);
                            string value = v.name == "name" ? ModuleName(v.value) : v.value;

                            ParameterDelegateMatchType childFilter = ParameterDelegateMatchType.FILTER;
                            wrapperParam.AddParameter(new ParameterDelegate <Part>(childFilter.Prefix() + name + ": " + value, p => PartModuleCheck(p, v), childFilter));
                        }
                    }

                    // Filter by category
                    if (filter.category != null)
                    {
                        AddParameter(new ParameterDelegate <Part>(filter.type.Prefix() + "category: " + filter.category,
                                                                  p => p.partInfo.category == filter.category.Value, filter.type));
                    }

                    // Filter by manufacturer
                    if (filter.manufacturer != null)
                    {
                        AddParameter(new ParameterDelegate <Part>(filter.type.Prefix() + "manufacturer: " + filter.manufacturer,
                                                                  p => p.partInfo.manufacturer == filter.manufacturer, filter.type));
                    }
                }
            }

            // Validate count
            if (minCount != 0 || maxCount != int.MaxValue && !(minCount == maxCount && maxCount == 0))
            {
                AddParameter(new CountParameterDelegate <Part>(minCount, maxCount));
            }
        }
        protected void CreateDelegates()
        {
            foreach (Filter filter in filters)
            {
                if (filter.type == ParameterDelegateMatchType.VALIDATE)
                {
                    foreach (AvailablePart part in filter.parts)
                    {
                        AddParameter(new CountParameterDelegate <Part>(filter.minCount, filter.maxCount, p => p.partInfo.name == part.name,
                                                                       part.title, false));
                    }

                    // Filter by part modules
                    foreach (string partModule in filter.partModules)
                    {
                        AddParameter(new CountParameterDelegate <Part>(filter.minCount, filter.maxCount, p => PartHasModule(p, partModule),
                                                                       Localizer.Format("#cc.param.PartValidation.withModule", ModuleName(partModule)), false));
                    }

                    // Filter by part module types
                    foreach (string partModuleType in filter.partModuleTypes)
                    {
                        AddParameter(new CountParameterDelegate <Part>(filter.minCount, filter.maxCount, p => PartHasObjective(p, partModuleType),
                                                                       Localizer.Format("#cc.param.PartValidation.withModuleType", ModuleTypeName(partModuleType)), false));
                    }
                }
                else
                {
                    // Filter by part
                    if (filter.parts.Any())
                    {
                        AddParameter(new ParameterDelegate <Part>(
                                         Localizer.Format("#cc.param.PartValidation.type", filter.type.Prefix(), LocalizationUtil.LocalizeList <AvailablePart>(LocalizationUtil.Conjunction.OR, filter.parts, p => p.title)),
                                         p => filter.parts.Any(pp => p.partInfo.name == pp.name), filter.type));
                    }

                    // Filter by part modules
                    foreach (string partModule in filter.partModules)
                    {
                        AddParameter(new ParameterDelegate <Part>(Localizer.Format("#cc.param.PartValidation.module", filter.type.Prefix(), ModuleName(partModule)), p => PartHasModule(p, partModule), filter.type));
                    }

                    // Filter by part modules
                    foreach (string partModuleType in filter.partModuleTypes)
                    {
                        AddParameter(new ParameterDelegate <Part>(Localizer.Format("#cc.param.PartValidation.moduleType", filter.type.Prefix(), ModuleTypeName(partModuleType)), p => PartHasObjective(p, partModuleType), filter.type));
                    }

                    // Filter by part modules - extended mode
                    foreach (List <Tuple <string, string, string> > list in filter.partModuleExtended)
                    {
                        ContractParameter wrapperParam = AddParameter(new AllParameterDelegate <Part>(Localizer.Format("#cc.param.PartValidation.moduleShort", filter.type.Prefix()), filter.type));

                        foreach (Tuple <string, string, string> v in list)
                        {
                            string name = v.Item1;
                            string label;
                            if (string.IsNullOrEmpty(v.Item2))
                            {
                                label = Regex.Replace(name, @"([A-Z]+?(?=[A-Z][^A-Z])|\B[A-Z]+?(?=[^A-Z]))", " $1");
                                label = label.Substring(0, 1).ToUpper() + label.Substring(1);
                            }
                            else
                            {
                                label = v.Item2;
                            }
                            string value = name == "name" ? ModuleName(v.Item3) : v.Item3;

                            ParameterDelegateMatchType childFilter = ParameterDelegateMatchType.FILTER;
                            wrapperParam.AddParameter(new ParameterDelegate <Part>(Localizer.Format("#cc.param.PartValidation.generic", childFilter.Prefix(), label, value), p => PartModuleCheck(p, v), childFilter));
                        }
                    }

                    // Filter by category
                    if (filter.category != null)
                    {
                        AddParameter(new ParameterDelegate <Part>(Localizer.Format("#cc.param.PartValidation.category", filter.type.Prefix(), filter.category),
                                                                  p => p.partInfo.category == filter.category.Value, filter.type));
                    }

                    // Filter by manufacturer
                    if (filter.manufacturer != null)
                    {
                        AddParameter(new ParameterDelegate <Part>(Localizer.Format("#cc.param.PartValidation.manufacturer", filter.type.Prefix(), filter.manufacturer),
                                                                  p => p.partInfo.manufacturer == filter.manufacturer, filter.type));
                    }
                }
            }

            // Validate count
            if (minCount != 0 || maxCount != int.MaxValue && !(minCount == maxCount && maxCount == 0))
            {
                AddParameter(new CountParameterDelegate <Part>(minCount, maxCount));
            }
        }
Beispiel #13
0
        public override bool Load(ConfigNode configNode)
        {
            // Load base class
            bool valid = base.Load(configNode);

            // Read min/max first
            valid &= ConfigNodeUtil.ParseValue <int>(configNode, "minCount", ref minCount, this,
                                                     configNode.HasNode("VALIDATE_ALL") || configNode.HasNode("NONE") ? 0 : 1, x => Validation.GE(x, 0));
            valid &= ConfigNodeUtil.ParseValue <int>(configNode, "maxCount", ref maxCount, this, int.MaxValue, x => Validation.GE(x, 0));

            // Set the default match type
            ParameterDelegateMatchType defaultMatch = ParameterDelegateMatchType.FILTER;

            if (maxCount == 0)
            {
                defaultMatch = ParameterDelegateMatchType.NONE;
            }

            // Standard definition
            if (configNode.HasValue("part") || configNode.HasValue("partModule") || configNode.HasValue("category") || configNode.HasValue("manufacturer"))
            {
                PartValidation.Filter filter = new PartValidation.Filter(defaultMatch);
                valid &= ConfigNodeUtil.ParseValue <AvailablePart>(configNode, "part", ref filter.part, this, (AvailablePart)null);
                valid &= ConfigNodeUtil.ParseValue <List <string> >(configNode, "partModule", ref filter.partModules, this, new List <string>(), x => x.All(Validation.ValidatePartModule));
                valid &= ConfigNodeUtil.ParseValue <PartCategories?>(configNode, "category", ref filter.category, this, (PartCategories?)null);
                valid &= ConfigNodeUtil.ParseValue <string>(configNode, "manufacturer", ref filter.manufacturer, this, (string)null);
                filters.Add(filter);
            }

            // Extended definition
            foreach (ConfigNode child in configNode.GetNodes())
            {
                ParameterDelegateMatchType matchType;
                if (child.name == "FILTER")
                {
                    matchType = ParameterDelegateMatchType.FILTER;
                }
                else if (child.name == "VALIDATE_ALL")
                {
                    matchType = ParameterDelegateMatchType.VALIDATE_ALL;
                }
                else if (child.name == "NONE")
                {
                    matchType = ParameterDelegateMatchType.NONE;
                }
                else
                {
                    LoggingUtil.LogError(this, ErrorPrefix() + ": unexpected node '" + child.name + "'.");
                    valid = false;
                    continue;
                }

                if (defaultMatch == ParameterDelegateMatchType.NONE)
                {
                    matchType = ParameterDelegateMatchType.NONE;
                }

                PartValidation.Filter filter = new PartValidation.Filter(matchType);
                valid &= ConfigNodeUtil.ParseValue <AvailablePart>(child, "part", ref filter.part, this, (AvailablePart)null);
                valid &= ConfigNodeUtil.ParseValue <List <string> >(child, "partModule", ref filter.partModules, this, new List <string>(), x => x.All(Validation.ValidatePartModule));
                valid &= ConfigNodeUtil.ParseValue <PartCategories?>(child, "category", ref filter.category, this, (PartCategories?)null);
                valid &= ConfigNodeUtil.ParseValue <string>(child, "manufacturer", ref filter.manufacturer, this, (string)null);
                filters.Add(filter);
            }

            return(valid);
        }