Exemple #1
0
        // We try decently hard to avoid creating clashing names, and also sanitize any invalid names.
        // You can customize how naming works by modifying this function.
        private void InitializeNaming()
        {
            TypeNamespace = CreateNamespace();
            // Type names that may appear in the header
            foreach (var typeName in new string[] { "CustomAttributesCache", "CustomAttributeTypeCache", "EventInfo", "FieldInfo", "Hash16", "MemberInfo", "MethodInfo", "MethodVariableKind", "MonitorData", "ParameterInfo", "PInvokeArguments", "PropertyInfo", "SequencePointKind", "StackFrameType", "VirtualInvokeData" })
            {
                TypeNamespace.ReserveName(typeName);
            }
            TypeNamer = TypeNamespace.MakeNamer <TypeInfo>((ti) => {
                if (ti.IsArray)
                {
                    return(TypeNamer.GetName(ti.ElementType) + "__Array");
                }
                var name = ti.Name.ToCIdentifier();
                if (name.StartsWith("Il2Cpp"))
                {
                    name = "_" + name;
                }
                name = Regex.Replace(name, "__+", "_");
                // Work around a dumb IDA bug: enums can't be named the same as certain "built-in" types
                // like KeyCode, Position, ErrorType. This only applies to enums, not structs.
                if (ti.IsEnum)
                {
                    name += "__Enum";
                }
                return(name);
            });

            GlobalsNamespace = CreateNamespace();
            GlobalNamer      = GlobalsNamespace.MakeNamer <MethodBase>((method) => $"{TypeNamer.GetName(method.DeclaringType)}_{method.Name.ToCIdentifier()}");
            EnumNamer        = GlobalsNamespace.MakeNamer <FieldInfo>((field) => $"{TypeNamer.GetName(field.DeclaringType)}_{field.Name.ToCIdentifier()}");
        }
 public override int GetHashCode() {
     unchecked {
         int hashCode = Name?.GetHashCode() ?? 0;
         hashCode = (hashCode * 397) ^ (Type?.GetHashCode() ?? 0);
         hashCode = (hashCode * 397) ^ (TypeNamespace?.GetHashCode() ?? 0);
         hashCode = (hashCode * 397) ^ (Data?.GetCollectionHashCode() ?? 0);
         hashCode = (hashCode * 397) ^ (GenericArguments?.GetCollectionHashCode() ?? 0);
         return hashCode;
     }
 }
Exemple #3
0
 public override int GetHashCode()
 {
     unchecked {
         var hashCode = Name == null ? 0 : Name.GetHashCode();
         hashCode = (hashCode * 397) ^ (Type == null ? 0 : Type.GetHashCode());
         hashCode = (hashCode * 397) ^ (TypeNamespace == null ? 0 : TypeNamespace.GetHashCode());
         hashCode = (hashCode * 397) ^ (Data == null ? 0 : Data.GetCollectionHashCode());
         hashCode = (hashCode * 397) ^ (GenericArguments == null ? 0 : GenericArguments.GetCollectionHashCode());
         return(hashCode);
     }
 }
        // We try decently hard to avoid creating clashing names, and also sanitize any invalid names.
        // You can customize how naming works by modifying this function.
        private void InitializeNaming()
        {
            TypeNamespace = CreateNamespace();
            TypeNamer     = TypeNamespace.MakeNamer <TypeInfo>((ti) => {
                if (ti.IsArray)
                {
                    return(TypeNamer.GetName(ti.ElementType) + "__Array");
                }
                var name = ti.Name.ToCIdentifier();
                name     = Regex.Replace(name, "__+", "_");
                // Work around a dumb IDA bug: enums can't be named the same as certain "built-in" types
                // like KeyCode, Position, ErrorType. This only applies to enums, not structs.
                if (ti.IsEnum)
                {
                    name += "__Enum";
                }
                return(name);
            });

            GlobalsNamespace = CreateNamespace();
            GlobalNamer      = GlobalsNamespace.MakeNamer <MethodBase>((method) => $"{TypeNamer.GetName(method.DeclaringType)}_{method.Name.ToCIdentifier()}");
        }
Exemple #5
0
        public void ResolveNamespace(XElement elem, XamlContext ctx)
        {
            if (Namespace != null)
            {
                return;
            }

            // Since XmlnsProperty records are inside the element,
            // the namespace is resolved after processing the element body.

            string xmlNs = null;

            if (elem.Annotation <XmlnsScope>() != null)
            {
                xmlNs = elem.Annotation <XmlnsScope>().LookupXmlns(Assembly, TypeNamespace);
            }
            if (xmlNs == null)
            {
                xmlNs = ctx.XmlNs.LookupXmlns(Assembly, TypeNamespace);
            }

            if (xmlNs == null)
            {
                var nsSeg  = TypeNamespace.Split('.');
                var nsName = nsSeg[nsSeg.Length - 1].ToLowerInvariant();
                var prefix = nsName;
                int count  = 0;
                while (elem.GetNamespaceOfPrefix(prefix) != null)
                {
                    count++;
                    prefix = nsName + count;
                }

                xmlNs = string.Format("clr-namespace:{0};assembly={1}", TypeNamespace, Assembly);
                elem.Add(new XAttribute(XNamespace.Xmlns + XmlConvert.EncodeLocalName(prefix),
                                        ctx.GetXmlNamespace(xmlNs)));
            }
            Namespace = xmlNs;
        }
Exemple #6
0
        public void ResolveNamespace(XElement elem, XamlContext ctx)
        {
            if (Namespace != null)
            {
                return;
            }

            // Since XmlnsProperty records are inside the element,
            // the namespace is resolved after processing the element body.

            string xmlNs = null;

            if (elem.Annotation <XmlnsScope>() != null)
            {
                xmlNs = elem.Annotation <XmlnsScope>().LookupXmlns(Assembly, TypeNamespace);
            }
            if (xmlNs == null)
            {
                xmlNs = ctx.XmlNs.LookupXmlns(Assembly, TypeNamespace);
            }
            // Sometimes there's no reference to System.Xaml even if x:Type is used
            if (xmlNs == null)
            {
                xmlNs = ctx.TryGetXmlNamespace(Assembly, TypeNamespace);
            }

            if (xmlNs == null)
            {
                if (AssemblyNameComparer.CompareAll.Equals(Assembly, ctx.Module.Assembly))
                {
                    xmlNs = $"clr-namespace:{TypeNamespace}";
                }
                else
                {
                    xmlNs = $"clr-namespace:{TypeNamespace};assembly={Assembly.Name}";
                }

                var nsSeg  = TypeNamespace.Split('.');
                var prefix = nsSeg[nsSeg.Length - 1].ToLowerInvariant();
                if (string.IsNullOrEmpty(prefix))
                {
                    if (string.IsNullOrEmpty(TypeNamespace))
                    {
                        prefix = "global";
                    }
                    else
                    {
                        prefix = "empty";
                    }
                }
                int        count = 0;
                var        truePrefix = prefix;
                XNamespace prefixNs, ns = ctx.GetXmlNamespace(xmlNs);
                while ((prefixNs = elem.GetNamespaceOfPrefix(truePrefix)) != null && prefixNs != ns)
                {
                    count++;
                    truePrefix = prefix + count;
                }

                if (prefixNs == null)
                {
                    elem.Add(new XAttribute(XNamespace.Xmlns + XmlConvert.EncodeLocalName(truePrefix), ns));
                    if (string.IsNullOrEmpty(TypeNamespace))
                    {
                        elem.AddBeforeSelf(new XComment(string.Format(dnSpy_BamlDecompiler_Resources.Msg_GlobalNamespace, truePrefix)));
                    }
                }
            }
            Namespace = ctx.GetXmlNamespace(xmlNs);
        }