public static List <ClassDeclarationSyntax> SearchForClass(ClassMatch classMatch)
        {
            if (Program.Debug)
            {
                Program.WriteLine(ConsoleColor.Yellow, $"Trying to find {{{UsingDirective.BuildFullTypeTag(classMatch)}}}");
                Program.IndentLevel++;
            }


            List <ClassDeclarationSyntax> ls = Registry.GetMatchedType(classMatch);

            if (!Program.SkipOne)
            {
                ls = Registry.AssemblyCs.Values.Where(c => c is ClassDeclarationSyntax cl && classMatch.Matches(cl))
                     .Cast <ClassDeclarationSyntax>().ToList();
            }

            List <ClassDeclarationSyntax> toRemove = new();

            foreach (var classDeclarationSyntax in ls)
            {
                if (Registry.Fullmatches.Contains(classDeclarationSyntax))
                {
                    toRemove.Add(classDeclarationSyntax);
                }
                else
                {
                    Registry.AddMatchedType(classMatch, classDeclarationSyntax);
                }
            }

            foreach (var torm in toRemove)
            {
                ls.Remove(torm);
            }

            if (ls.Count == 1)
            {
                Registry.Fullmatches.Add(ls[0]);
            }



            if (Program.Debug)
            {
                Program.IndentLevel--;
            }

            return(ls);
        }
Exemple #2
0
        public static bool CheckMatch(object src, object dst, HashSet <object> chkd, ClassMatch checkIfSameReference = ClassMatch.None)
        /// Checks if all fields are equal to test the serialization/copy results
        /// sameReference checks if classes AreSame
        {
            if (chkd == null)
            {
                chkd = new HashSet <object>();
            }

            if (src == null && dst == null)
            {
                return(true);
            }
            if (src == null && dst != null)
            {
                throw new Exception("Src is null while dst isn't");                                     //return false;
            }
            if (src != null && dst == null)
            {
                throw new Exception("Dst is null while src isn't");                                     //return false;
            }
            Type type = src.GetType();

            if (type.IsPrimitive)
            {
                if (!src.Equals(dst))
                {
                    throw new Exception("Primitives not equal");
                }
            }
            else if (type == typeof(string))
            {
                if (src != dst)
                {
                    throw new Exception("String not equal");
                }
            }
            else if (type.IsSubclassOf(typeof(Type)))
            {
                if (src != dst)
                {
                    throw new Exception("Types not equal");
                }
            }
            else if (type.IsSubclassOf(typeof(FieldInfo)))
            {
                if (src != dst)
                {
                    throw new Exception("Field Infos not equal");
                }
            }
            else if (type.IsSubclassOf(typeof(UnityEngine.Object)))
            {
                if (src != dst)
                {
                    throw new Exception("Unity objects not equal");
                }
            }
            else if (type == typeof(AnimationCurve))
            {
                if (checkIfSameReference == ClassMatch.ShouldDiffer && src == dst)
                {
                    throw new Exception("Value references match");
                }

                if (checkIfSameReference == ClassMatch.ShouldMatch && src != dst)
                {
                    throw new Exception("Value references differ");
                }

                AnimationCurve srcCurve = (AnimationCurve)src;
                AnimationCurve dstCurve = (AnimationCurve)dst;
                if (srcCurve.keys.Length != dstCurve.keys.Length)
                {
                    throw new Exception("Anim Curve length differ");                                                               //return false;
                }
                for (int i = 0; i < srcCurve.keys.Length; i++)
                {
                    if (srcCurve.keys[i].time != dstCurve.keys[i].time || srcCurve.keys[i].value != dstCurve.keys[i].value)
                    {
                        return(false);
                    }
                }

                return(true);
            }
            else if (type.IsArray || type.BaseType.IsArray)
            {
                if (checkIfSameReference == ClassMatch.ShouldDiffer && src == dst)
                {
                    throw new Exception("Value references match");
                }

                if (checkIfSameReference == ClassMatch.ShouldMatch && src != dst)
                {
                    throw new Exception("Value references differ");
                }

                Array srcArray = (Array)src;
                Array dstArray = (Array)dst;

                if (chkd.Contains(srcArray))
                {
                    return(true);
                }
                chkd.Add(srcArray);

                if (srcArray.Length != dstArray.Length)
                {
                    return(false);
                }

                for (int i = 0; i < dstArray.Length; i++)
                {
                    if (!CheckMatch(srcArray.GetValue(i), dstArray.GetValue(i), chkd, checkIfSameReference))
                    {
                        return(false);
                    }
                }

                return(true);
            }
            else
            {
                if (checkIfSameReference == ClassMatch.ShouldDiffer && src == dst)
                {
                    throw new Exception("Value references match");
                }

                if (checkIfSameReference == ClassMatch.ShouldMatch && !src.Equals(dst))
                {
                    throw new Exception("Value references differ");
                }

                if (chkd.Contains(src))
                {
                    return(true);
                }
                chkd.Add(src);

                FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
                for (int i = 0; i < fields.Length; i++)
                {
                    if (fields[i].IsLiteral)
                    {
                        continue;                                          //leaving constant fields blank
                    }
                    if (fields[i].FieldType.IsPointer)
                    {
                        continue;                                                    //skipping pointers (they make unity crash. Maybe require unsafe)
                    }
                    if (fields[i].IsNotSerialized)
                    {
                        continue;
                    }

                    object srcVal = fields[i].GetValue(src);
                    object dstVal = fields[i].GetValue(dst);

                    bool match = CheckMatch(srcVal, dstVal, chkd, checkIfSameReference);
                    if (!match)
                    {
                        return(false);
                    }
                }
                return(true);
            }

            return(true);
        }