private List <ObjectDefinition> GetEligibleUnits(IReferenceableObjectProvider objectProvider)
        {
            var validUnitObjectTypes =
                new List <ObjectType> {
                ObjectType.MechDef, ObjectType.TurretDef, ObjectType.VehicleDef
            };

            var candidates = objectProvider.GetReferenceableObjects()
                             .Where(o => validUnitObjectTypes.Contains(o.ObjectType));

            var eligibleUnits = candidates.Where(
                o => !this.UnitTags.Except(new[] { "{CUR_TEAM.faction}" }).Except(o.Tags[Keywords.MyTags]).Any());

            var filteredEligibleUnits = eligibleUnits.Where(
                o => !o.Tags[Keywords.MyTags].Any(s => this.ExcludedUnitTags.Contains(s))).ToList();

            if (this.RestrictByFaction)
            {
                filteredEligibleUnits.AsParallel().ForAll(
                    // filteredEligibleUnits.ToList().ForEach(
                    unit =>
                {
                    var unitTags         = unit.Tags[Keywords.MyTags];
                    var factionIntersect = unitTags.Intersect(this.factionService.GetFactions()).ToList();
                    factionIntersect.ForEach(faction => this.factionUnitDictionary[faction] += 1);
                });
            }

            return(filteredEligibleUnits.Cast <ObjectDefinition>().ToList());
        }
        private List <PilotObjectDefinition> GetEligiblePilots(IReferenceableObjectProvider objectProvider)
        {
            var candidates = objectProvider.GetReferenceableObjects().Where(
                o => o.ObjectType == ObjectType.PilotDef && o.Tags.ContainsKey(Keywords.MyTags));

            var eligible = candidates.Where(
                o => !this.PilotTags.Any() || o.Tags.ContainsKey(Keywords.MyTags) &&
                !this.PilotTags.Except(o.Tags[Keywords.MyTags]).Any());
            var filteredEligible = eligible.Where(
                o => !o.Tags[Keywords.MyTags].Any(s => this.ExcludedPilotTags.Contains(s)));

            return(filteredEligible.Cast <PilotObjectDefinition>().ToList());
        }
示例#3
0
        public static List <IObjectReference <TType> > FindReferences <TType>(
            IReferenceableObjectProvider objectProvider,
            IReferenceableObject objectDefinition,
            List <ObjectType> dependantTypesToIgnore)
            where TType : class, IReferenceableObject
        {
            if (objectDefinition == null)
            {
                return(new List <IObjectReference <TType> >());
            }

            // dependantTypesToIgnore = dependantTypesToIgnore ?? new List<ObjectType>();
            var dependenciesRels = RelationshipManager.GetDependenciesRelationshipsForType(objectDefinition.ObjectType);
            var dependantRels    = RelationshipManager.GetDependentRelationShipsForType(objectDefinition.ObjectType);

            if (!dependantRels.Any() && !dependenciesRels.Any())
            {
                return(new List <IObjectReference <TType> >());
            }

            var targetObjectTypes = new HashSet <ObjectType>();

            dependantRels.Select(ship => ship.DependentType).Distinct().ToList()
            .ForEach(type => targetObjectTypes.Add(type));

            var candidates    = objectProvider.GetReferenceableObjects().Where(o => targetObjectTypes.Contains(o.ObjectType)).ToList();
            var dependentRecs = new List <ObjectReference <TType> >();

            Parallel.ForEach(
                dependantRels,
                relationship =>
            {
                Parallel.ForEach(
                    candidates,
                    candidate =>
                {
                    if (candidate.ObjectType != relationship.DependentType)
                    {
                        return;
                    }

                    if (!candidate.MetaData.ContainsKey(relationship.DependentKey) ||
                        !objectDefinition.MetaData.ContainsKey(relationship.DependencyKey))
                    {
                        return;
                    }

                    var objectKey     = objectDefinition.MetaData[relationship.DependencyKey].ToString();
                    var dependentKeys = candidate.MetaData[relationship.DependentKey];

                    if ((relationship.HasMultipleDependencies &&
                         ((List <string>)dependentKeys).Any(s => string.Equals(s, objectKey, StringComparison.OrdinalIgnoreCase))) ||
                        string.Compare(dependentKeys.ToString(), objectKey, StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        lock (dependentRecs)
                        {
                            dependentRecs.Add(
                                new ObjectReference <TType>(
                                    (TType)candidate,
                                    ObjectReferenceType.Dependent,
                                    relationship,
                                    false,
                                    true,
                                    objectKey));
                        }
                    }
                });
            });

            targetObjectTypes.Clear();
            dependenciesRels.Select(ship => ship.DependencyType).Distinct().ToList()
            .ForEach(type => targetObjectTypes.Add(type));
            candidates = objectProvider.GetReferenceableObjects().Where(o => targetObjectTypes.Contains(o.ObjectType)).ToList();

            var dependencyRecs = new List <ObjectReference <TType> >();

            dependenciesRels.ToList().ForEach(
                // dependenciesRels.AsParallel().ForAll(
                relationship =>
            {
                candidates.ToList().ForEach(
                    // candidates.AsParallel().ForAll(
                    candidate =>
                {
                    if (candidate.ObjectType != relationship.DependencyType)
                    {
                        return;
                    }

                    if (!candidate.MetaData.ContainsKey(relationship.DependencyKey) ||
                        !objectDefinition.MetaData.ContainsKey(relationship.DependentKey))
                    {
                        return;
                    }

                    var objectKeys = objectDefinition.MetaData[relationship.DependentKey];

                    var dependencyKey = candidate.MetaData[relationship.DependencyKey].ToString();
                    if ((relationship.HasMultipleDependencies && ((List <string>)objectKeys).Any(s => s.Equals(dependencyKey, StringComparison.OrdinalIgnoreCase))) ||
                        string.Equals(dependencyKey, objectKeys.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        lock (dependencyRecs)
                        {
                            dependencyRecs.Add(
                                new ObjectReference <TType>(
                                    (TType)candidate,
                                    ObjectReferenceType.Dependency,
                                    relationship,
                                    false,
                                    true,
                                    dependencyKey));
                        }
                    }
                });

                // Add an invalid entry for each specified dependency that was not matched...
                List <string> keys = !objectDefinition.MetaData.ContainsKey(relationship.DependentKey)
                                                ?
                                     new List <string>()
                                                : !relationship.HasMultipleDependencies
                                                    ? new List <string>(new string[] { objectDefinition.MetaData[relationship.DependentKey].ToString() })
                                                    : new List <string>((List <string>)objectDefinition.MetaData[relationship.DependentKey]);

                keys.ForEach(
                    s =>
                {
                    if (!dependencyRecs.Any(reference => reference.Relationship == relationship && reference.ReferenceKey == s))
                    {
                        lock (dependencyRecs)
                        {
                            dependencyRecs.Add(
                                new ObjectReference <TType>(
                                    null,
                                    ObjectReferenceType.Dependency,
                                    relationship,
                                    false,
                                    false,
                                    s));
                        }
                    }
                });
            });

            var references = dependentRecs;

            references.AddRange(dependencyRecs);
            return(references.Cast <IObjectReference <TType> >().ToList());
        }
示例#4
0
        public static List <IObjectReference <TType> > FindReferences <TType>(
            IReferenceableObjectProvider objectProvider,
            IReferenceableObject objectDefinition,
            List <IReferenceableObject> baseReferences)
            where TType : class, IReferenceableObject
        {
            if (objectDefinition == null)
            {
                return(new List <IObjectReference <TType> >());
            }

            var dependenciesRels = RelationshipManager.GetDependenciesRelationshipsForType(objectDefinition.ObjectType);
            var dependantRels    = RelationshipManager.GetDependentRelationShipsForType(objectDefinition.ObjectType);

            if (!dependantRels.Any() && !dependenciesRels.Any())
            {
                return(new List <IObjectReference <TType> >());
            }

            var targetObjectTypes = new HashSet <ObjectType>();

            dependantRels.Select(ship => ship.DependentType).Distinct().ToList()
            .ForEach(type => targetObjectTypes.Add(type));

            var candidates    = objectProvider.GetReferenceableObjects().Where(o => targetObjectTypes.Contains(o.ObjectType)).ToList();
            var dependentRecs = new List <ObjectReference <TType> >();

            Parallel.ForEach(
                dependantRels,
                relationship =>
            {
                Parallel.ForEach(
                    candidates.Where(o => o.ObjectType == relationship.DependentType),
                    candidate =>
                {
                    if (candidate.ObjectType != relationship.DependentType)
                    {
                        return;
                    }

                    if (!candidate.MetaData.ContainsKey(relationship.DependentKey) ||
                        !objectDefinition.MetaData.ContainsKey(relationship.DependencyKey))
                    {
                        return;
                    }

                    object objectKeyObject;
                    if (!objectDefinition.MetaData.TryGetValue(
                            relationship.DependencyKey,
                            out objectKeyObject))
                    {
                        return;
                    }

                    var objectKey     = objectKeyObject.ToString();
                    var dependentKeys = candidate.MetaData[relationship.DependentKey];

                    if (dependentKeys == null)
                    {
                        return;
                    }

                    if ((relationship.HasMultipleDependencies &&
                         ((List <string>)dependentKeys).Any(s => string.Equals(s, objectKey, StringComparison.OrdinalIgnoreCase))) ||
                        string.Compare(dependentKeys.ToString(), objectKey, StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        lock (dependentRecs)
                        {
                            dependentRecs.Add(
                                new ObjectReference <TType>(
                                    (TType)candidate,
                                    ObjectReferenceType.Dependent,
                                    relationship,
                                    false,
                                    true,
                                    objectKey));
                        }
                    }
                });
            });

            targetObjectTypes.Clear();
            dependenciesRels.Select(ship => ship.DependencyType).Distinct().ToList()
            .ForEach(type => targetObjectTypes.Add(type));
            candidates = objectProvider.GetReferenceableObjects().Where(o => targetObjectTypes.Contains(o.ObjectType)).ToList();

            var dependencyRecs = new List <ObjectReference <TType> >();

            dependenciesRels.ToList().ForEach(
                // dependenciesRels.AsParallel().ForAll(
                relationship =>
            {
                candidates.Where(o => o.ObjectType == relationship.DependencyType).ToList().ForEach(
                    // candidates.AsParallel().ForAll(
                    candidate =>
                {
                    if (candidate.ObjectType != relationship.DependencyType)
                    {
                        return;
                    }

                    if (!candidate.MetaData.ContainsKey(relationship.DependencyKey) ||
                        !objectDefinition.MetaData.ContainsKey(relationship.DependentKey))
                    {
                        return;
                    }

                    var objectKeys = objectDefinition.MetaData[relationship.DependentKey];

                    if (objectKeys == null || (relationship.HasMultipleDependencies && ((List <string>)objectKeys).Count == 0))
                    {
                        return;
                    }

                    if (relationship.HasMultipleDependencies && ((List <string>)objectKeys).All(s => s.IsNullOrEmpty()))
                    {
                        return;
                    }

                    object dependencyKeyObject;                 //= candidate.MetaData[relationship.DependencyKey].ToString();
                    if (!candidate.MetaData.TryGetValue(relationship.DependencyKey, out dependencyKeyObject))
                    {
                        return;
                    }

                    var dependencyKey = dependencyKeyObject.ToString();

                    if ((relationship.HasMultipleDependencies && ((List <string>)objectKeys).Any(s => s.Equals(dependencyKey, StringComparison.OrdinalIgnoreCase))) ||
                        string.Equals(dependencyKey, objectKeys.ToString(), StringComparison.OrdinalIgnoreCase))
                    {
                        lock (dependencyRecs)
                        {
                            dependencyRecs.Add(
                                new ObjectReference <TType>(
                                    (TType)candidate,
                                    ObjectReferenceType.Dependency,
                                    relationship,
                                    false,
                                    true,
                                    dependencyKey));
                        }
                    }
                });


                var ignoreList = new List <Tuple <ObjectType, ObjectType> >();

                /*{
                 *  new Tuple<ObjectType, ObjectType>(ObjectType.ShopDef, ObjectType.WeaponDef),           // HBS Shopdefs contain refs to non-existent weapons
                 *  new Tuple<ObjectType, ObjectType>(ObjectType.HardpointDataDef, ObjectType.Prefab),     // Ball ache to identify prefabs, and unnecessary.
                 *  new Tuple<ObjectType, ObjectType>(ObjectType.TurretChassisDef, ObjectType.Prefab),     // Ball ache to identify prefabs, and unnecessary.
                 *  new Tuple<ObjectType, ObjectType>(ObjectType.ShopDef, ObjectType.MechDef),             // Stupid Templates.
                 * });*/

                var tempIgnoreList = new List <ObjectType>();
                tempIgnoreList.AddRange(new [] { ObjectType.Prefab, ObjectType.AssetBundle, ObjectType.HardpointDataDef, ObjectType.MovementCapabilitiesDef });

                if (tempIgnoreList.Contains(relationship.DependencyType))
                {
                    return;
                }

                if (baseReferences != null &&
                    baseReferences.Contains(objectDefinition))
                {
                    if (ignoreList.Any(
                            ignoreEntry =>
                            objectDefinition.ObjectType == ignoreEntry.Item1 &&
                            relationship.DependencyType == ignoreEntry.Item2))
                    {
                        return;
                    }
                }

                // Add an invalid entry for each specified dependency that was not matched...
                List <string> keys = !objectDefinition.MetaData.ContainsKey(relationship.DependentKey) || objectDefinition.MetaData[relationship.DependentKey] == null
                                                ?
                                     new List <string>()
                                                : !relationship.HasMultipleDependencies
                                                    ? new List <string>(new string[] { objectDefinition.MetaData[relationship.DependentKey].ToString() })
                                                    : new List <string>((List <string>)objectDefinition.MetaData[relationship.DependentKey]);

                keys.Where(s => !s.IsNullOrEmpty()).ToList().ForEach(
                    s =>
                {
                    if (!dependencyRecs.Any(reference => reference.Relationship == relationship && string.Equals(reference.ReferenceKey.ToString(), s, StringComparison.InvariantCultureIgnoreCase)))
                    {
                        lock (dependencyRecs)
                        {
                            dependencyRecs.Add(
                                new ObjectReference <TType>(
                                    null,
                                    ObjectReferenceType.Dependency,
                                    relationship,
                                    false,
                                    false,
                                    s));
                        }
                    }
                });
            });

            var references = dependentRecs;

            references.AddRange(dependencyRecs);
            return(references.Cast <IObjectReference <TType> >().ToList());
        }
 public void LoadEligibleUnitsAndPilots(IReferenceableObjectProvider objectProvider)
 {
     this.EligibleUnits  = this.GetEligibleUnits(objectProvider);
     this.EligiblePilots = this.GetEligiblePilots(objectProvider);
 }