Example #1
0
        public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent)
        {
            var record = (PropertyRecord)((BamlRecordNode)node).Record;

            var elemType = parent.Xaml.Element.Annotation <XamlType>();
            var xamlProp = ctx.ResolveProperty(record.AttributeId);
            var value    = XamlUtils.Escape(record.Value);

            parent.Xaml.Element.Add(ConstructXAttribute());

            return(null);

            XAttribute ConstructXAttribute()
            {
                if (xamlProp.IsAttachedTo(elemType))
                {
                    return(new XAttribute(xamlProp.ToXName(ctx, parent.Xaml, true), value));
                }

                if (xamlProp.PropertyName == "Name" && elemType.ResolvedType.GetDefinition()?.ParentModule.IsMainModule == true)
                {
                    return(new XAttribute(ctx.GetKnownNamespace("Name", XamlContext.KnownNamespace_Xaml), value));
                }

                return(new XAttribute(xamlProp.ToXName(ctx, parent.Xaml, false), value));
            }
        }
        public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent)
        {
            var record   = (PropertyTypeReferenceRecord)((BamlRecordNode)node).Record;
            var attr     = ctx.ResolveProperty(record.AttributeId);
            var type     = ctx.ResolveType(record.TypeId);
            var typeName = ctx.ToString(parent.Xaml, type);

            var elem = new BamlElement(node);

            var elemAttr = ctx.ResolveProperty(record.AttributeId);

            elem.Xaml = new XElement(elemAttr.ToXName(ctx, null));

            if (attr.ResolvedMember.FullNameIs("System.Windows.Style", "TargetType"))
            {
                parent.Xaml.Element.AddAnnotation(new TargetTypeAnnotation(type));
            }

            elem.Xaml.Element.AddAnnotation(elemAttr);
            parent.Xaml.Element.Add(elem.Xaml.Element);

            var typeElem = new XElement(ctx.GetKnownNamespace("TypeExtension", XamlContext.KnownNamespace_Xaml, parent.Xaml));

            typeElem.AddAnnotation(ctx.ResolveType(0xfd4d));             // Known type - TypeExtension
            typeElem.Add(new XElement(ctx.GetPseudoName("Ctor"), typeName));
            elem.Xaml.Element.Add(typeElem);

            elemAttr.DeclaringType.ResolveNamespace(elem.Xaml, ctx);
            elem.Xaml.Element.Name = elemAttr.ToXName(ctx, null);

            return(elem);
        }
        public void Run(XamlContext ctx, XDocument document)
        {
            var xClass = document.Root.Elements().First().Attribute(ctx.GetKnownNamespace("Class", XamlContext.KnownNamespace_Xaml));

            if (xClass is null)
            {
                return;
            }

            var type = ctx.Module.Find(xClass.Value, true);

            if (type is null)
            {
                return;
            }

            var wbAsm = ctx.Module.CorLibTypes.AssemblyRef.Version == new Version(2, 0, 0, 0) ?
                        new AssemblyNameInfo("WindowsBase, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35").ToAssemblyRef() :
                        new AssemblyNameInfo("WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35").ToAssemblyRef();
            var ifaceRef = new TypeRefUser(ctx.Module, "System.Windows.Markup", "IComponentConnector", wbAsm);
            var iface    = ctx.Module.Context.Resolver.ResolveThrow(ifaceRef);

            var connect = iface.FindMethod("Connect");

            foreach (MethodDef method in type.Methods)
            {
                if (Impl(method, connect))
                {
                    connect = method;
                    iface   = null;
                    break;
                }
            }
            if (iface is not null)
            {
                return;
            }

            Dictionary <int, Action <XamlContext, XElement> > connIds = null;

            try {
                connIds = ExtractConnectionId(ctx, connect);
            }
            catch {
            }

            if (connIds is null)
            {
                var msg = dnSpy_BamlDecompiler_Resources.Error_IComponentConnectorConnetCannotBeParsed;
                document.Root.AddBeforeSelf(new XComment(string.Format(msg, type.ReflectionFullName)));
                return;
            }

            foreach (var elem in document.Elements())
            {
                ProcessElement(ctx, elem, connIds);
            }
        }
            public void Callback(XamlContext ctx, XElement elem)
            {
                var xName = ctx.GetKnownNamespace("Name", XamlContext.KnownNamespace_Xaml);

                if (elem.Attribute("Name") is null && elem.Attribute(xName) is null)
                {
                    elem.Add(new XAttribute(xName, FieldName));
                }
            }
Example #5
0
        public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent)
        {
            var record = (DefAttributeRecord)((BamlRecordNode)node).Record;

            var attrName = ctx.ResolveString(record.NameId);

            parent.Xaml.Element.Add(new XAttribute(ctx.GetKnownNamespace(attrName, XamlContext.KnownNamespace_Xaml), record.Value));

            return(null);
        }
Example #6
0
        void RewriteClass(XamlContext ctx, XElement elem)
        {
            var type = elem.Annotation <XamlType>();

            if (type == null || type.ResolvedType == null)
            {
                return;
            }

            var typeDef = type.ResolvedType.GetDefinition();

            if (typeDef == null || !typeDef.ParentModule.IsMainModule)
            {
                return;
            }

            var newType = typeDef.DirectBaseTypes.First().GetDefinition();

            if (newType == null)
            {
                return;
            }
            var xamlType = new XamlType(newType.ParentModule, newType.ParentModule.FullAssemblyName, newType.Namespace, newType.Name);

            xamlType.ResolveNamespace(elem, ctx);

            elem.Name = xamlType.ToXName(ctx);

            var attrName = ctx.GetKnownNamespace("Class", XamlContext.KnownNamespace_Xaml, elem);

            var attrs = elem.Attributes().ToList();

            if (typeDef.Accessibility != ICSharpCode.Decompiler.TypeSystem.Accessibility.Public)
            {
                var classModifierName = ctx.GetKnownNamespace("ClassModifier", XamlContext.KnownNamespace_Xaml, elem);
                attrs.Insert(0, new XAttribute(classModifierName, "internal"));
            }
            attrs.Insert(0, new XAttribute(attrName, type.ResolvedType.FullName));
            ctx.XClassNames.Add(type.ResolvedType.FullName);
            elem.ReplaceAttributes(attrs);
        }
        public BamlElement TranslateDefer(XamlContext ctx, BamlNode node, BamlElement parent)
        {
            var record   = (DefAttributeKeyTypeRecord)((BamlRecordNode)node).Record;
            var type     = ctx.ResolveType(record.TypeId);
            var typeName = ctx.ToString(parent.Xaml, type);
            var key      = (XamlResourceKey)node.Annotation;

            var bamlElem = new BamlElement(node);

            bamlElem.Xaml = new XElement(ctx.GetKnownNamespace("Key", XamlContext.KnownNamespace_Xaml, parent.Xaml));
            parent.Xaml.Element.Add(bamlElem.Xaml.Element);

            var typeElem = new XElement(ctx.GetKnownNamespace("TypeExtension", XamlContext.KnownNamespace_Xaml, parent.Xaml));

            typeElem.AddAnnotation(ctx.ResolveType(0xfd4d));             // Known type - TypeExtension
            typeElem.Add(new XElement(ctx.GetPseudoName("Ctor"), typeName));
            bamlElem.Xaml.Element.Add(typeElem);

            key.KeyElement = bamlElem;

            return(bamlElem);
        }
Example #8
0
        public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent)
        {
            var record = (LiteralContentRecord)((BamlRecordNode)node).Record;

            var elem    = new XElement(ctx.GetKnownNamespace("XData", XamlContext.KnownNamespace_Xaml, parent.Xaml));
            var content = XElement.Parse(record.Value);

            elem.Add(content);

            parent.Xaml.Element.Add(elem);

            return(null);
        }
Example #9
0
        public BamlElement TranslateDefer(XamlContext ctx, BamlNode node, BamlElement parent)
        {
            var record = (DefAttributeKeyStringRecord)((BamlRecordNode)node).Record;
            var key    = (XamlResourceKey)node.Annotation;

            var bamlElem = new BamlElement(node);

            bamlElem.Xaml = new XElement(ctx.GetKnownNamespace("Key", XamlContext.KnownNamespace_Xaml, parent.Xaml));
            parent.Xaml.Element.Add(bamlElem.Xaml.Element);
            bamlElem.Xaml.Element.Value = ctx.ResolveString(record.ValueId);
            key.KeyElement = bamlElem;

            return(bamlElem);
        }
Example #10
0
        public BamlElement TranslateDefer(XamlContext ctx, BamlNode node, BamlElement parent)
        {
            var record = (KeyElementStartRecord)((BamlBlockNode)node).Header;
            var key    = (XamlResourceKey)node.Annotation;

            var bamlElem = new BamlElement(node);

            bamlElem.Xaml = new XElement(ctx.GetKnownNamespace("Key", XamlContext.KnownNamespace_Xaml, parent.Xaml));
            parent.Xaml.Element.Add(bamlElem.Xaml.Element);
            key.KeyElement = bamlElem;
            base.Translate(ctx, node, bamlElem);

            return(bamlElem);
        }
Example #11
0
        public void Run(XamlContext ctx, XDocument document)
        {
            key = ctx.GetKnownNamespace("Key", XamlContext.KnownNamespace_Xaml);

            bool doWork;

            do
            {
                doWork = false;
                foreach (var elem in document.Elements())
                {
                    doWork |= ProcessElement(ctx, elem);
                }
            } while (doWork);
        }
Example #12
0
        void RewriteClass(XamlContext ctx, XElement elem)
        {
            var type = elem.Annotation <XamlType>();

            if (type is null || type.ResolvedType is null)
            {
                return;
            }

            var typeDef  = type.ResolvedType.ResolveTypeDef();
            var comparer = new AssemblyNameComparer(AssemblyNameComparerFlags.All);

            if (typeDef is null || !comparer.Equals(typeDef.DefinitionAssembly, ctx.Module.Assembly))
            {
                return;
            }

            var newType  = typeDef.BaseType;
            var xamlType = new XamlType(newType.DefinitionAssembly, newType.ReflectionNamespace, newType.Name);

            xamlType.ResolveNamespace(elem, ctx);

            elem.Name = xamlType.ToXName(ctx);

            var attrName = ctx.GetKnownNamespace("Class", XamlContext.KnownNamespace_Xaml, elem);

            var attrs = elem.Attributes().ToList();

            if (typeDef.IsNotPublic)
            {
                var classModifierName = ctx.GetKnownNamespace("ClassModifier", XamlContext.KnownNamespace_Xaml, elem);
                attrs.Insert(0, new XAttribute(classModifierName, ctx.BamlDecompilerOptions.InternalClassModifier));
            }
            attrs.Insert(0, new XAttribute(attrName, type.ResolvedType.ReflectionFullName));
            elem.ReplaceAttributes(attrs);
        }
        public BamlElement Translate(XamlContext ctx, BamlNode node, BamlElement parent)
        {
            var record = (ConstructorParameterTypeRecord)((BamlRecordNode)node).Record;

            var elem = new XElement(ctx.GetKnownNamespace("TypeExtension", XamlContext.KnownNamespace_Xaml, parent.Xaml));

            elem.AddAnnotation(ctx.ResolveType(0xfd4d));             // Known type - TypeExtension

            var bamlElem = new BamlElement(node);

            bamlElem.Xaml = elem;
            parent.Xaml.Element.Add(elem);

            var type     = ctx.ResolveType(record.TypeId);
            var typeName = ctx.ToString(parent.Xaml, type);

            elem.Add(new XElement(ctx.GetPseudoName("Ctor"), typeName));

            return(bamlElem);
        }
        public BamlElement TranslateDefer(XamlContext ctx, BamlNode node, BamlElement parent)
        {
            var    record   = (OptimizedStaticResourceRecord)((BamlRecordNode)node).Record;
            var    bamlElem = new BamlElement(node);
            object key;

            if (record.IsType)
            {
                var value = ctx.ResolveType(record.ValueId);

                var typeElem = new XElement(ctx.GetKnownNamespace("TypeExtension", XamlContext.KnownNamespace_Xaml, parent.Xaml));
                typeElem.AddAnnotation(ctx.ResolveType(0xfd4d));                 // Known type - TypeExtension
                typeElem.Add(new XElement(ctx.GetPseudoName("Ctor"), ctx.ToString(parent.Xaml, value)));
                key = typeElem;
            }
            else if (record.IsStatic)
            {
                string attrName;
                if (record.ValueId > 0x7fff)
                {
                    bool  isKey  = true;
                    short bamlId = unchecked ((short)-record.ValueId);
                    if (bamlId > 232 && bamlId < 464)
                    {
                        bamlId -= 232;
                        isKey   = false;
                    }
                    else if (bamlId > 464 && bamlId < 467)
                    {
                        bamlId -= 231;
                    }
                    else if (bamlId > 467 && bamlId < 470)
                    {
                        bamlId -= 234;
                        isKey   = false;
                    }
                    var    res = ctx.Baml.KnownThings.Resources(bamlId);
                    string name;
                    if (isKey)
                    {
                        name = res.Item1 + "." + res.Item2;
                    }
                    else
                    {
                        name = res.Item1 + "." + res.Item3;
                    }
                    var xmlns = ctx.GetXmlNamespace("http://schemas.microsoft.com/winfx/2006/xaml/presentation");
                    attrName = ctx.ToString(parent.Xaml, xmlns.GetName(name));
                }
                else
                {
                    var value = ctx.ResolveProperty(record.ValueId);

                    value.DeclaringType.ResolveNamespace(parent.Xaml, ctx);
                    var xName = value.ToXName(ctx, parent.Xaml);

                    attrName = ctx.ToString(parent.Xaml, xName);
                }

                var staticElem = new XElement(ctx.GetKnownNamespace("StaticExtension", XamlContext.KnownNamespace_Xaml, parent.Xaml));
                staticElem.AddAnnotation(ctx.ResolveType(0xfda6));                 // Known type - StaticExtension
                staticElem.Add(new XElement(ctx.GetPseudoName("Ctor"), attrName));
                key = staticElem;
            }
            else
            {
                key = ctx.ResolveString(record.ValueId);
            }

            var extType = ctx.ResolveType(0xfda5);
            var resElem = new XElement(extType.ToXName(ctx));

            resElem.AddAnnotation(extType);             // Known type - StaticResourceExtension
            bamlElem.Xaml = resElem;
            parent.Xaml.Element.Add(resElem);

            var attrElem = new XElement(ctx.GetPseudoName("Ctor"));

            attrElem.Add(key);
            resElem.Add(attrElem);

            return(bamlElem);
        }