Exemple #1
0
        public static BamlDocument ReadDocument(Stream str)
        {
            var ret    = new BamlDocument();
            var reader = new BamlBinaryReader(str);

            {
                var  rdr = new BinaryReader(str, Encoding.Unicode);
                uint len = rdr.ReadUInt32();
                ret.Signature = new string(rdr.ReadChars((int)(len >> 1)));
                rdr.ReadBytes((int)(((len + 3) & ~3) - len));
            }
            if (ret.Signature != "MSBAML")
            {
                throw new NotSupportedException();
            }
            ret.ReaderVersion = new BamlDocument.BamlVersion {
                Major = reader.ReadUInt16(), Minor = reader.ReadUInt16()
            };
            ret.UpdaterVersion = new BamlDocument.BamlVersion {
                Major = reader.ReadUInt16(), Minor = reader.ReadUInt16()
            };
            ret.WriterVersion = new BamlDocument.BamlVersion {
                Major = reader.ReadUInt16(), Minor = reader.ReadUInt16()
            };
            if (ret.ReaderVersion.Major != 0 || ret.ReaderVersion.Minor != 0x60 ||
                ret.UpdaterVersion.Major != 0 || ret.UpdaterVersion.Minor != 0x60 ||
                ret.WriterVersion.Major != 0 || ret.WriterVersion.Minor != 0x60)
            {
                throw new NotSupportedException();
            }

            var recs = new Dictionary <long, BamlRecord>();

            while (str.Position < str.Length)
            {
                long       pos  = str.Position;
                var        type = (BamlRecordType)reader.ReadByte();
                BamlRecord rec  = null;
                switch (type)
                {
                case BamlRecordType.AssemblyInfo:
                    rec = new AssemblyInfoRecord();
                    break;

                case BamlRecordType.AttributeInfo:
                    rec = new AttributeInfoRecord();
                    break;

                case BamlRecordType.ConstructorParametersStart:
                    rec = new ConstructorParametersStartRecord();
                    break;

                case BamlRecordType.ConstructorParametersEnd:
                    rec = new ConstructorParametersEndRecord();
                    break;

                case BamlRecordType.ConstructorParameterType:
                    rec = new ConstructorParameterTypeRecord();
                    break;

                case BamlRecordType.ConnectionId:
                    rec = new ConnectionIdRecord();
                    break;

                case BamlRecordType.ContentProperty:
                    rec = new ContentPropertyRecord();
                    break;

                case BamlRecordType.DefAttribute:
                    rec = new DefAttributeRecord();
                    break;

                case BamlRecordType.DefAttributeKeyString:
                    rec = new DefAttributeKeyStringRecord();
                    break;

                case BamlRecordType.DefAttributeKeyType:
                    rec = new DefAttributeKeyTypeRecord();
                    break;

                case BamlRecordType.DeferableContentStart:
                    rec = new DeferableContentStartRecord();
                    break;

                case BamlRecordType.DocumentEnd:
                    rec = new DocumentEndRecord();
                    break;

                case BamlRecordType.DocumentStart:
                    rec = new DocumentStartRecord();
                    break;

                case BamlRecordType.ElementEnd:
                    rec = new ElementEndRecord();
                    break;

                case BamlRecordType.ElementStart:
                    rec = new ElementStartRecord();
                    break;

                case BamlRecordType.KeyElementEnd:
                    rec = new KeyElementEndRecord();
                    break;

                case BamlRecordType.KeyElementStart:
                    rec = new KeyElementStartRecord();
                    break;

                case BamlRecordType.LineNumberAndPosition:
                    rec = new LineNumberAndPositionRecord();
                    break;

                case BamlRecordType.LinePosition:
                    rec = new LinePositionRecord();
                    break;

                case BamlRecordType.LiteralContent:
                    rec = new LiteralContentRecord();
                    break;

                case BamlRecordType.NamedElementStart:
                    rec = new NamedElementStartRecord();
                    break;

                case BamlRecordType.OptimizedStaticResource:
                    rec = new OptimizedStaticResourceRecord();
                    break;

                case BamlRecordType.PIMapping:
                    rec = new PIMappingRecord();
                    break;

                case BamlRecordType.PresentationOptionsAttribute:
                    rec = new PresentationOptionsAttributeRecord();
                    break;

                case BamlRecordType.Property:
                    rec = new PropertyRecord();
                    break;

                case BamlRecordType.PropertyArrayEnd:
                    rec = new PropertyArrayEndRecord();
                    break;

                case BamlRecordType.PropertyArrayStart:
                    rec = new PropertyArrayStartRecord();
                    break;

                case BamlRecordType.PropertyComplexEnd:
                    rec = new PropertyComplexEndRecord();
                    break;

                case BamlRecordType.PropertyComplexStart:
                    rec = new PropertyComplexStartRecord();
                    break;

                case BamlRecordType.PropertyCustom:
                    rec = new PropertyCustomRecord();
                    break;

                case BamlRecordType.PropertyDictionaryEnd:
                    rec = new PropertyDictionaryEndRecord();
                    break;

                case BamlRecordType.PropertyDictionaryStart:
                    rec = new PropertyDictionaryStartRecord();
                    break;

                case BamlRecordType.PropertyListEnd:
                    rec = new PropertyListEndRecord();
                    break;

                case BamlRecordType.PropertyListStart:
                    rec = new PropertyListStartRecord();
                    break;

                case BamlRecordType.PropertyStringReference:
                    rec = new PropertyStringReferenceRecord();
                    break;

                case BamlRecordType.PropertyTypeReference:
                    rec = new PropertyTypeReferenceRecord();
                    break;

                case BamlRecordType.PropertyWithConverter:
                    rec = new PropertyWithConverterRecord();
                    break;

                case BamlRecordType.PropertyWithExtension:
                    rec = new PropertyWithExtensionRecord();
                    break;

                case BamlRecordType.PropertyWithStaticResourceId:
                    rec = new PropertyWithStaticResourceIdRecord();
                    break;

                case BamlRecordType.RoutedEvent:
                    rec = new RoutedEventRecord();
                    break;

                case BamlRecordType.StaticResourceEnd:
                    rec = new StaticResourceEndRecord();
                    break;

                case BamlRecordType.StaticResourceId:
                    rec = new StaticResourceIdRecord();
                    break;

                case BamlRecordType.StaticResourceStart:
                    rec = new StaticResourceStartRecord();
                    break;

                case BamlRecordType.StringInfo:
                    rec = new StringInfoRecord();
                    break;

                case BamlRecordType.Text:
                    rec = new TextRecord();
                    break;

                case BamlRecordType.TextWithConverter:
                    rec = new TextWithConverterRecord();
                    break;

                case BamlRecordType.TextWithId:
                    rec = new TextWithIdRecord();
                    break;

                case BamlRecordType.TypeInfo:
                    rec = new TypeInfoRecord();
                    break;

                case BamlRecordType.TypeSerializerInfo:
                    rec = new TypeSerializerInfoRecord();
                    break;

                case BamlRecordType.XmlnsProperty:
                    rec = new XmlnsPropertyRecord();
                    break;

                case BamlRecordType.XmlAttribute:
                case BamlRecordType.ProcessingInstruction:
                case BamlRecordType.LastRecordType:
                case BamlRecordType.EndAttributes:
                case BamlRecordType.DefTag:
                case BamlRecordType.ClrEvent:
                case BamlRecordType.Comment:
                default:
                    throw new NotSupportedException();
                }
                rec.Position = pos;

                rec.Read(reader);
                ret.Add(rec);
                recs.Add(pos, rec);
            }
            for (int i = 0; i < ret.Count; i++)
            {
                var defer = ret[i] as IBamlDeferRecord;
                if (defer != null)
                {
                    defer.ReadDefer(ret, i, _ => recs[_]);
                }
            }

            return(ret);
        }
Exemple #2
0
        void ProcessConverter(PropertyWithConverterRecord rec, TypeDef type)
        {
            TypeDef converter = ResolveType(rec.ConverterTypeId);

            if (converter.FullName == "System.ComponentModel.EnumConverter")
            {
                if (type != null && context.Modules.Contains((ModuleDefMD)type.Module))
                {
                    FieldDef enumField = type.FindField(rec.Value);
                    if (enumField != null)
                    {
                        service.AddReference(enumField, new BAMLEnumReference(enumField, rec));
                    }
                }
            }
            else if (converter.FullName == "System.Windows.Input.CommandConverter")
            {
                string cmd   = rec.Value.Trim();
                int    index = cmd.IndexOf('.');
                if (index != -1)
                {
                    string  typeName = cmd.Substring(0, index);
                    string  prefix;
                    TypeSig sig = ResolveType(typeName, out prefix);
                    if (sig != null)
                    {
                        string cmdName = cmd.Substring(index + 1);

                        TypeDef typeDef = sig.ToBasicTypeDefOrRef().ResolveTypeDefThrow();
                        if (context.Modules.Contains((ModuleDefMD)typeDef.Module))
                        {
                            PropertyDef property = typeDef.FindProperty(cmdName);
                            if (property != null)
                            {
                                var reference = new BAMLConverterMemberReference(xmlnsCtx, sig, property, rec);
                                AddTypeSigReference(sig, reference);
                                service.ReduceRenameMode(property, RenameMode.Letters);
                                service.AddReference(property, reference);
                            }
                            FieldDef field = typeDef.FindField(cmdName);
                            if (field != null)
                            {
                                var reference = new BAMLConverterMemberReference(xmlnsCtx, sig, field, rec);
                                AddTypeSigReference(sig, reference);
                                service.ReduceRenameMode(field, RenameMode.Letters);
                                service.AddReference(field, reference);
                            }
                            if (property == null && field == null)
                            {
                                context.Logger.WarnFormat("Could not resolve command '{0}' in '{1}'.", cmd, CurrentBAMLName);
                            }
                        }
                    }
                }
            }
            else if (converter.FullName == "System.Windows.Markup.DependencyPropertyConverter")
            {
                // Umm... Again nothing to do, DP already won't be renamed.
            }
            else if (converter.FullName == "System.Windows.PropertyPathConverter")
            {
                AnalyzePropertyPath(rec.Value);
            }
            else if (converter.FullName == "System.Windows.Markup.RoutedEventConverter")
            {
                ;
            }
            else if (converter.FullName == "System.Windows.Markup.TypeTypeConverter")
            {
                string  prefix;
                TypeSig sig = ResolveType(rec.Value.Trim(), out prefix);
                if (sig != null && context.Modules.Contains((ModuleDefMD)sig.ToBasicTypeDefOrRef().ResolveTypeDefThrow().Module))
                {
                    var reference = new BAMLConverterTypeReference(xmlnsCtx, sig, rec);
                    AddTypeSigReference(sig, reference);
                }
            }

            var    attrInfo = ResolveAttribute(rec.AttributeId);
            string attrName = null;

            if (attrInfo.Item1 != null)
            {
                attrName = attrInfo.Item1.Name;
            }
            else if (attrInfo.Item2 != null)
            {
                attrName = attrInfo.Item2.Name;
            }

            if (attrName == "DisplayMemberPath")
            {
                AnalyzePropertyPath(rec.Value);
            }
            else if (attrName == "Source")
            {
                string declType = null;
                if (attrInfo.Item1 is IMemberDef)
                {
                    declType = ((IMemberDef)attrInfo.Item1).DeclaringType.FullName;
                }
                else if (attrInfo.Item2 != null)
                {
                    declType = ResolveType(attrInfo.Item2.OwnerTypeId).FullName;
                }
                if (declType == "System.Windows.ResourceDictionary")
                {
                    var src = rec.Value.ToUpperInvariant();
                    if (src.EndsWith(".BAML") || src.EndsWith(".XAML"))
                    {
                        // Relative address
                        if (src.StartsWith("../"))
                        {
                            var oldSrc      = src;
                            var fake_domain = "http://DotProtect.Ex/";
                            var uri         = new Uri(fake_domain + CurrentBAMLName + "/../" + src);
                            src = uri.ToString().Substring(fake_domain.Length);

                            context.Logger.InfoFormat("Convert [{0}] at [{1}] to [{2}].", oldSrc, CurrentBAMLName, src);
                        }
                        else
                        {
                            var match = WPFAnalyzer.UriPattern.Match(src);
                            if (match.Success)
                            {
                                src = match.Groups[1].Value;
                            }
                            else if (rec.Value.Contains("/"))
                            {
                                context.Logger.WarnFormat("[BAML] Fail to extract XAML name from '{0}'.", rec.Value);
                            }

                            if (!src.Contains("//"))
                            {
                                var rel = new Uri(new Uri(packScheme + "application:,,,/" + CurrentBAMLName), src);
                                src = rel.LocalPath;
                            }
                        }

                        var reference = new BAMLPropertyReference(rec);
                        src = src.TrimStart('/');
                        var baml     = src.Substring(0, src.Length - 5) + ".BAML";
                        var xaml     = src.Substring(0, src.Length - 5) + ".XAML";
                        var bamlRefs = service.FindRenamer <WPFAnalyzer>().bamlRefs;
                        bamlRefs.AddListEntry(baml, reference);
                        bamlRefs.AddListEntry(xaml, reference);
                        bamlRefs.AddListEntry(Uri.EscapeUriString(baml), reference);
                        bamlRefs.AddListEntry(Uri.EscapeUriString(xaml), reference);
                    }
                }
            }
        }