Beispiel #1
0
 public static Type GetTypeOf(string s)
 {
     return(AccessTools.TypeByName(ParseHelper.FixVersions(s)));
 }
Beispiel #2
0
        public static object DoFrom(IEnumerator <string> items, object obj)
        {
            if (obj == null)
            {
                throw new Exception("RimEditor: From command with null object");
            }
            ICollection collection = obj as ICollection;

            if (collection == null)
            {
                throw new Exception("RimEditor: From command with object not castable to ICollection");
            }
            Type childType = obj.GetType().GenericTypeArguments[0];

            if (ParseHelper.CanMakeTypedObject(childType)) // directly findable & we should never be comparing fields
            {
                return(DoFromDirect(items, collection));
            }

            List <Pair <FieldInfo, object> > comparers = new List <Pair <FieldInfo, object> >();

            comparers.Add(GetCompareValue(items, childType));

            while (Parser.ParseCommand(items.Current) == CommandType.__And)
            {
                Parser.notFinished = items.MoveNext();
                comparers.Add(GetCompareValue(items, childType));
            }
            if (Parser.ParseCommand(items.Current) != CommandType.__End)
            {
                throw new Exception("RimEditor: From without matching End, maybe no Ands");
            }
            Parser.notFinished = items.MoveNext(); // now 1 past __End

            var enumerator = collection.GetEnumerator();

            while (enumerator.MoveNext())
            {
                object candidate = enumerator.Current;

                bool found = true;
                foreach (var comparer in comparers)
                {
                    object candidateFieldValue = comparer.First.GetValue(candidate);
                    if (candidateFieldValue == null)
                    {
                        if (comparer.Second != null)
                        {
                            found = false;
                            break;
                        }
                    }
                    else if (!candidateFieldValue.Equals(comparer.Second))
                    {
                        found = false;
                        break;
                    }
                }
                if (found)
                {
                    return(candidate);
                }
            }
            throw new Exception("RimEditor: From Command, could not find item matching criteria: " + comparers.ToStringSafeEnumerable()
                                + "\nCollection was " + collection.ToStringSafeEnumerable());
        }