Esempio n. 1
0
        public static UnitorMethod ToUnitorMethod(this MethodDef method, UnitorModel lookupModel)
        {
            if (method == null)
            {
                return(new UnitorMethod(lookupModel));
            }

            List <UnitorType> ParameterList = new List <UnitorType>();

            foreach (Parameter param in method.Parameters)
            {
                if (param.Type.IsTypeDefOrRef)
                {
                    ParameterList.Add(param.Type.TryGetTypeDef()?.ToUnitorType(lookupModel, false) ?? new UnitorType(lookupModel));
                }
            }
            UnitorMethod m = new UnitorMethod(lookupModel)
            {
                DeclaringType = method.DeclaringType.ToUnitorType(lookupModel, false),
                ReturnType    = method.ReturnType.TryGetTypeDef()?.ToUnitorType(lookupModel, false) ?? new UnitorType(lookupModel),
                ParameterList = ParameterList,
                MonoMethod    = method
            };

            return(m);
        }
Esempio n. 2
0
        public static UnitorType ToUnitorType(this TypeInfo type, UnitorModel lookupModel, bool recurse)
        {
            if (type == null || type.Name == "<Module>")
            {
                return(new UnitorType(lookupModel));
            }

            lookupModel.Il2CppTypeMatches.GetOrAdd(type, new UnitorType(lookupModel)
            {
                Il2CppType = type, Children = new List <UnitorType>()
            });
            if (lookupModel.Il2CppTypeMatches[type].Resolved)
            {
                return(lookupModel.Il2CppTypeMatches[type]);
            }

            if (lookupModel.Il2CppTypeMatches[type].IsGenericType)
            {
                lookupModel.Il2CppTypeMatches[type].GenericTypeParameters = lookupModel.Il2CppTypeMatches[type].Il2CppType.GenericTypeArguments.Where(t => t != type).ToUnitorTypeList(lookupModel, false, null).ToList();
            }
            if (lookupModel.Il2CppTypeMatches[type].IsArray)
            {
                TypeInfo elementType = lookupModel.Il2CppTypeMatches[type].Il2CppType.ElementType;
                if (elementType != null && elementType != type)
                {
                    lookupModel.Il2CppTypeMatches[type].ElementType = elementType.ToUnitorType(lookupModel, false);
                }
            }
            if (lookupModel.Il2CppTypeMatches[type].IsTypeRef)
            {
                if (lookupModel.Il2CppTypeMatches.TryGetValue(type.ElementType, out UnitorType nonref))
                {
                    nonref.Ref = lookupModel.Il2CppTypeMatches[type];
                }
            }
            if (!recurse)
            {
                return(lookupModel.Il2CppTypeMatches[type]);
            }

            lookupModel.Il2CppTypeMatches[type].Fields        = type.DeclaredFields.ToUnitorFieldList(lookupModel).ToList();
            lookupModel.Il2CppTypeMatches[type].DeclaringType = type.DeclaringType.ToUnitorType(lookupModel, false);
            lookupModel.Il2CppTypeMatches[type].Properties    = type.DeclaredProperties.ToUnitorPropertyList(lookupModel).ToList();
            lookupModel.Il2CppTypeMatches[type].Methods       = type.DeclaredMethods.ToUnitorMethodList(lookupModel).ToList();

            if (!lookupModel.Il2CppTypeMatches[type].DeclaringType.IsEmpty)
            {
                lookupModel.Il2CppTypeMatches[type].DeclaringType.Children.Add(lookupModel.Il2CppTypeMatches[type]);
            }
            lookupModel.Il2CppTypeMatches[type].Resolved = true;
            return(lookupModel.Il2CppTypeMatches[type]);
        }
Esempio n. 3
0
        public static UnitorField ToUnitorField(this FieldInfo field, UnitorModel lookupModel)
        {
            if (field == null)
            {
                return(new UnitorField(lookupModel));
            }

            return(new UnitorField(lookupModel)
            {
                Il2CppField = field,
                Type = field.FieldType.ToUnitorType(lookupModel, false),
                DeclaringType = field.DeclaringType.ToUnitorType(lookupModel, false)
            });
        }
Esempio n. 4
0
        public static UnitorField ToUnitorField(this FieldDef field, UnitorModel lookupModel)
        {
            if (field == null)
            {
                return(new UnitorField(lookupModel));
            }
            TypeDef type = field.FieldType.ToTypeDef();

            return(new UnitorField(lookupModel)
            {
                MonoField = field,
                Type = type.ToUnitorType(lookupModel, false),
                DeclaringType = field.DeclaringType.ToUnitorType(lookupModel, false)
            });
        }
Esempio n. 5
0
        public static UnitorModel FromTypeModel(TypeModel typeModel, EventHandler <string> statusCallback)
        {
            UnitorModel model = new UnitorModel();

            statusCallback?.Invoke(model, "Creating AppModel");
            model.AppModel    = new AppModel(typeModel);
            model.StringTable = model.AppModel.Strings;

            model.Types.AddRange(typeModel.Types.Where(
                                     t => !t.Assembly.ShortName.Contains("System") &&
                                     !t.Assembly.ShortName.Contains("Mono") &&
                                     !t.Assembly.ShortName.Contains("UnityEngine") &&
                                     t.Assembly.ShortName != "mscorlib.dll"
                                     ).ToUnitorTypeList(model, true, statusCallback).Where(t => !t.IsEmpty).Where(t => !t.IsTypeRef));

            model.Namespaces.AddRange(model.Types.Select(t => t.Namespace).Distinct());
            model.TypeModel   = typeModel;
            model.StringTable = model.AppModel.Strings;
            List <UnitorMethod> methods = model.Types.AsParallel().SelectMany(t => t.Methods).ToList();

            statusCallback?.Invoke(null, "Analysing all methods");
            int total   = methods.Count;
            int current = 0;

            Parallel.ForEach(methods, new ParallelOptions {
                MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount / 2, 1)
            }, m =>
            {
                statusCallback?.Invoke(null, $"Analysed {current}/{total} methods");
                current++;
                m.Analyse();
            });
            current = 0;

            model.CalledMethods = new Dictionary <UnitorMethod, int>();
            methods.AsParallel().SelectMany(m => m.MethodCalls).ToList().ForEach((m) =>
            {
                if (!model.CalledMethods.ContainsKey(m))
                {
                    model.CalledMethods.Add(m, 1);
                }
                else
                {
                    model.CalledMethods[m]++;
                }
            });
            return(model);
        }
Esempio n. 6
0
        public static UnitorModel FromModuleDef(ModuleDef moduleDef, EventHandler <string> statusCallback)
        {
            UnitorModel model = new UnitorModel();

            model.Types.AddRange(moduleDef.Types.ToUnitorTypeList(model, true, statusCallback));
            model.Namespaces.AddRange(moduleDef.Types.Select(t => t.Namespace.String).Distinct());
            model.ModuleDef = moduleDef;
            List <UnitorMethod> methods = model.Types.SelectMany(t => t.Methods).ToList();

            statusCallback?.Invoke(null, "Analysing all methods");
            int total   = methods.Count;
            int current = 0;

            Parallel.ForEach(methods, new ParallelOptions {
                MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount / 2, 1)
            }, m =>
            {
                statusCallback?.Invoke(null, $"Analysed {current}/{total} methods");
                current++;
                m.Analyse();
            });
            model.CalledMethods = new Dictionary <UnitorMethod, int>();
            List <KeyValuePair <ulong, string> > strings = new List <KeyValuePair <ulong, string> >();

            methods.SelectMany((m) =>
            {
                strings.AddRange(m.Strings);
                return(m.MethodCalls);
            }
                               ).ToList().ForEach((m) =>
            {
                if (!model.CalledMethods.ContainsKey(m))
                {
                    model.CalledMethods.Add(m, 1);
                }
                else
                {
                    model.CalledMethods[m]++;
                }
            });
            model.StringTable = strings
                                .GroupBy(x => x.Key)
                                .Select(g => g.First())
                                .GroupBy(x => x.Value)
                                .Select(g => g.First())
                                .ToDictionary(p => p.Key, p => p.Value);
            return(model);
        }
Esempio n. 7
0
        public static UnitorProperty ToUnitorProperty(this PropertyInfo property, UnitorModel lookupModel)
        {
            if (property == null)
            {
                return(new UnitorProperty(lookupModel));
            }

            return(new UnitorProperty(lookupModel)
            {
                PropertyType = property.PropertyType.ToUnitorType(lookupModel, false),
                DeclaringType = property.DeclaringType.ToUnitorType(lookupModel, false),
                GetMethod = property.GetMethod.ToUnitorMethod(lookupModel),
                SetMethod = property.SetMethod.ToUnitorMethod(lookupModel),
                Index = property.Index,
                Il2CppProperty = property
            });
        }
Esempio n. 8
0
        public static UnitorType ToUnitorType(this TypeDef type, UnitorModel lookupModel, bool recurse)
        {
            if (type == null)
            {
                return(new UnitorType(lookupModel));
            }
            lookupModel.MonoTypeMatches.GetOrAdd(type, new UnitorType(lookupModel)
            {
                MonoType = type, Children = new List <UnitorType>()
            });
            if (lookupModel.MonoTypeMatches[type].Resolved)
            {
                return(lookupModel.MonoTypeMatches[type]);
            }

            if (lookupModel.MonoTypeMatches[type].IsGenericType)
            {
                lookupModel.MonoTypeMatches[type].GenericTypeParameters = lookupModel.MonoTypeMatches[type].MonoType.GenericParameters.Select(p => p.DeclaringType).Where(t => t != type).ToUnitorTypeList(lookupModel, false, null).ToList();
            }
            if (lookupModel.MonoTypeMatches[type].IsArray)
            {
                TypeDef elementType = lookupModel.MonoTypeMatches[type].MonoType.TryGetArraySig()?.TryGetTypeDef();
                if (elementType != null && elementType != type)
                {
                    lookupModel.MonoTypeMatches[type].ElementType = elementType.ToUnitorType(lookupModel, false) ?? new UnitorType(lookupModel);
                }
            }

            if (!recurse)
            {
                return(lookupModel.MonoTypeMatches[type]);
            }

            lookupModel.MonoTypeMatches[type].Fields        = type.Fields.ToUnitorFieldList(lookupModel).ToList();
            lookupModel.MonoTypeMatches[type].DeclaringType = type.DeclaringType.ToUnitorType(lookupModel, false);
            lookupModel.MonoTypeMatches[type].Properties    = type.Properties.ToUnitorPropertyList(lookupModel).ToList();
            lookupModel.MonoTypeMatches[type].Methods       = type.Methods.ToUnitorMethodList(lookupModel).ToList();

            if (!lookupModel.MonoTypeMatches[type].DeclaringType.IsEmpty)
            {
                lookupModel.MonoTypeMatches[type].DeclaringType.Children.Add(lookupModel.MonoTypeMatches[type]);
            }
            lookupModel.MonoTypeMatches[type].Resolved = true;
            return(lookupModel.MonoTypeMatches[type]);
        }
Esempio n. 9
0
        public static UnitorMethod ToUnitorMethod(this MethodInfo method, UnitorModel lookupModel)
        {
            if (method == null)
            {
                return(new UnitorMethod(lookupModel));
            }

            List <UnitorType> ParameterList = new List <UnitorType>();

            ParameterList.AddRange(method.DeclaredParameters.Select(p => p.ParameterType.ToUnitorType(lookupModel, false)));

            UnitorMethod m = new UnitorMethod(lookupModel)
            {
                DeclaringType = method.DeclaringType.ToUnitorType(lookupModel, false),
                ParameterList = ParameterList,
                ReturnType    = method.ReturnType.ToUnitorType(lookupModel, false),
                Il2CppMethod  = method
            };

            return(m);
        }
Esempio n. 10
0
 public static IEnumerable <UnitorProperty> ToUnitorPropertyList(this IList <PropertyDef> monoProperties, UnitorModel lookupModel) =>
 monoProperties.Select((p, i) => p.ToUnitorProperty(i, lookupModel));
Esempio n. 11
0
 public void Add(UnitorModel model)
 {
     Types.AddRange(model.Types);
     Namespaces.Clear();
     Namespaces.AddRange(Types.Select(t => t.Namespace).Distinct());
 }
Esempio n. 12
0
 public static IEnumerable <UnitorMethod> ToUnitorMethodList(this ReadOnlyCollection <MethodInfo> il2cppMethods, UnitorModel lookupModel) =>
 il2cppMethods.Select(p => p.ToUnitorMethod(lookupModel));
Esempio n. 13
0
 public static IEnumerable <UnitorMethod> ToUnitorMethodList(this IList <MethodDef> monoMethods, UnitorModel lookupModel) =>
 monoMethods.Select(p => p.ToUnitorMethod(lookupModel));
Esempio n. 14
0
        public static IEnumerable <UnitorType> ToUnitorTypeList(this IEnumerable <TypeInfo> il2cppTypes, UnitorModel lookupModel, bool recurse, EventHandler <string> statusCallback)
        {
            int current       = 0;
            var filteredTypes = il2cppTypes.Where(t => !t.IsNested);
            int total         = filteredTypes.Count();

            return(filteredTypes.AsParallel().Select(type =>
            {
                current++;
                statusCallback?.Invoke(null, $"Loaded {current}/{total} types...");
                return type.ToUnitorType(lookupModel, recurse);
            }));
        }
Esempio n. 15
0
 public UnitorField(UnitorModel lookupModel)
 {
     Owner = lookupModel;
 }
Esempio n. 16
0
        public static UnitorProperty ToUnitorProperty(this PropertyDef property, int index, UnitorModel lookupModel)
        {
            if (property == null)
            {
                return(new UnitorProperty(lookupModel));
            }

            return(new UnitorProperty(lookupModel)
            {
                PropertyType = property.PropertySig.RetType.TryGetTypeDef()?.ToUnitorType(lookupModel, false) ?? new UnitorType(lookupModel),
                DeclaringType = property.DeclaringType.ToUnitorType(lookupModel, false),
                GetMethod = property.GetMethod.ToUnitorMethod(lookupModel),
                SetMethod = property.SetMethod.ToUnitorMethod(lookupModel),
                Index = index,
                MonoProperty = property
            });
        }
Esempio n. 17
0
 public static IEnumerable <UnitorField> ToUnitorFieldList(this IList <FieldDef> monoFields, UnitorModel lookupModel) =>
 monoFields.Select(f => f.ToUnitorField(lookupModel));
Esempio n. 18
0
 public static IEnumerable <UnitorField> ToUnitorFieldList(this IReadOnlyCollection <FieldInfo> il2cppFields, UnitorModel lookupModel) =>
 il2cppFields.Select(f => f.ToUnitorField(lookupModel));
Esempio n. 19
0
 public UnitorMethod(UnitorModel lookupModel)
 {
     Owner = lookupModel;
 }
Esempio n. 20
0
 public static IEnumerable <UnitorProperty> ToUnitorPropertyList(this ReadOnlyCollection <PropertyInfo> il2cppProperties, UnitorModel lookupModel) =>
 il2cppProperties.Select(p => p.ToUnitorProperty(lookupModel));
Esempio n. 21
0
 public UnitorProperty(UnitorModel lookupModel)
 {
     Owner = lookupModel;
 }
Esempio n. 22
0
 public UnitorType(UnitorModel lookupModel)
 {
     Owner = lookupModel;
 }