public BamlDocument GenerateThemesGenericXaml(IEnumerable <string> importedFiles) { BamlDocument document = new BamlDocument { Signature = "MSBAML", ReaderVersion = BamlVersion, WriterVersion = BamlVersion, UpdaterVersion = BamlVersion }; document.Add(new DocumentStartRecord()); AddAssemblyInfos(document); document.AddRange(GetMergedDictionariesAttributes()); document.Add(new ElementStartRecord { TypeId = ResourceDictionaryTypeId }); document.Add(new XmlnsPropertyRecord { Prefix = string.Empty, XmlNamespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation", AssemblyIds = document.OfType <AssemblyInfoRecord>().Select(asm => asm.AssemblyId).ToArray() }); document.AddRange(GetDictionariesList(importedFiles)); ElementEndRecord lastEndRecord = new ElementEndRecord(); document.Add(new DeferableContentStartRecord { Record = lastEndRecord }); document.Add(lastEndRecord); document.Add(new DocumentEndRecord()); return(document); }
public BamlDocument GenerateThemesGenericXaml(IEnumerable<string> importedFiles) { BamlDocument document = new BamlDocument { Signature = "MSBAML", ReaderVersion = BamlVersion, WriterVersion = BamlVersion, UpdaterVersion = BamlVersion }; document.Add(new DocumentStartRecord()); AddAssemblyInfos(document); document.AddRange(GetMergedDictionariesAttributes()); document.Add(new ElementStartRecord { TypeId = ResourceDictionaryTypeId }); document.Add(new XmlnsPropertyRecord { Prefix = string.Empty, XmlNamespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation", AssemblyIds = document.OfType<AssemblyInfoRecord>().Select(asm => asm.AssemblyId).ToArray() }); document.AddRange(GetDictionariesList(importedFiles)); ElementEndRecord lastEndRecord = new ElementEndRecord(); document.Add(new DeferableContentStartRecord { Record = lastEndRecord }); document.Add(lastEndRecord); document.Add(new DocumentEndRecord()); return document; }
private static void AdjustAttributeIds(BamlDocument document, ushort offset) { const string AttributeIdPropertyName = "AttributeId"; var existingAttributeInfoRecords = document.OfType <AttributeInfoRecord>().ToList(); foreach (var record in document) { ushort?attributeId = record.TryGetPropertyValue(AttributeIdPropertyName) as ushort?; if (attributeId == null || record is AttributeInfoRecord || !existingAttributeInfoRecords.Any(r => r.AttributeId == attributeId)) { continue; } record.TrySetPropertyValue(AttributeIdPropertyName, (ushort)(attributeId.Value + offset)); } foreach (var attributeInfoRecord in existingAttributeInfoRecords) { attributeInfoRecord.AttributeId += offset; } }
private void ProcessBaml(ModuleDefinition mod, Dictionary<string, BamlDocument> bamls, ref int cc, DictionaryEntry entry, Stream stream) { cc++; docName = entry.Key as string; doc = BamlReader.ReadDocument(stream); (mod as IAnnotationProvider).Annotations[RenMode] = NameMode.Letters; for (int i = 0; i < doc.Count; i++) { if (doc[i] is LineNumberAndPositionRecord || doc[i] is LinePositionRecord) { doc.RemoveAt(i); i--; } } int asmId = -1; Dictionary<ushort, AssemblyDefinition> asms = new Dictionary<ushort, AssemblyDefinition>(); foreach (var rec in doc.OfType<AssemblyInfoRecord>()) { AssemblyNameReference nameRef = AssemblyNameReference.Parse(rec.AssemblyFullName); if (nameRef.Name == mod.Assembly.Name.Name) { asmId = rec.AssemblyId; rec.AssemblyFullName = GetBamlAssemblyFullName(mod.Assembly.Name); asms.Add(rec.AssemblyId, mod.Assembly); PopulateMembers(mod.Assembly); nameRef = null; } else { bool added = false; foreach (var i in ivtMap) if (i.Key.Name.Name == nameRef.Name) { rec.AssemblyFullName = GetBamlAssemblyFullName(i.Key.Name); asms.Add(rec.AssemblyId, i.Key); PopulateMembers(i.Key); nameRef = null; added = true; break; } if (!added) { var asmRefDef = GlobalAssemblyResolver.Instance.Resolve(nameRef); if (asmRefDef != null) { PopulateMembers(asmRefDef); } } } } Dictionary<ushort, TypeDefinition> types = new Dictionary<ushort, TypeDefinition>(); foreach (var rec in doc.OfType<TypeInfoRecord>()) { AssemblyDefinition asm; if (asms.TryGetValue((ushort)(rec.AssemblyId & 0xfff), out asm)) { TypeReference type = TypeParser.ParseType(asm.MainModule, rec.TypeFullName); if (type != null) { types.Add(rec.TypeId, type.Resolve()); AddTypeRenRefs(type, type, rec); rec.TypeFullName = TypeParser.ToParseable(type); } } } Dictionary<string, string> xmlns = new Dictionary<string, string>(); foreach (var rec in doc.OfType<XmlnsPropertyRecord>()) xmlns[rec.Prefix] = rec.XmlNamespace; Dictionary<ushort, string> ps = new Dictionary<ushort, string>(); foreach (var rec in doc.OfType<AttributeInfoRecord>()) { if (types.ContainsKey(rec.OwnerTypeId)) { PropertyDefinition prop = types[rec.OwnerTypeId].Properties.SingleOrDefault(p => p.Name == rec.Name); if (prop != null) { ((prop as IAnnotationProvider).Annotations[RenRef] as List<IReference>).Add(new BamlAttributeReference(rec)); } EventDefinition evt = types[rec.OwnerTypeId].Events.SingleOrDefault(p => p.Name == rec.Name); if (evt != null) { ((evt as IAnnotationProvider).Annotations[RenRef] as List<IReference>).Add(new BamlAttributeReference(rec)); } FieldDefinition field = types[rec.OwnerTypeId].Fields.SingleOrDefault(p => p.Name == rec.Name); if (field != null) { ((field as IAnnotationProvider).Annotations[RenRef] as List<IReference>).Add(new BamlAttributeReference(rec)); } //Attached property MethodDefinition getM = types[rec.OwnerTypeId].Methods.SingleOrDefault(p => p.Name == "Get" + rec.Name); if (getM != null) { (getM as IAnnotationProvider).Annotations[RenOk] = false; } MethodDefinition setM = types[rec.OwnerTypeId].Methods.SingleOrDefault(p => p.Name == "Set" + rec.Name); if (setM != null) { (setM as IAnnotationProvider).Annotations[RenOk] = false; } } ps.Add(rec.AttributeId, rec.Name); } foreach (var rec in doc.OfType<PropertyWithConverterRecord>()) { if (rec.ConverterTypeId == 0xfd4c || ((short)rec.AttributeId > 0 && ps[rec.AttributeId] == "TypeName")) //TypeExtension { string type = rec.Value; string xmlNamespace; if (type.IndexOf(':') != -1) { xmlNamespace = xmlns[type.Substring(0, type.IndexOf(':'))]; type = type.Substring(type.IndexOf(':') + 1, type.Length - type.IndexOf(':') - 1); } else { xmlNamespace = xmlns[""]; } TypeDefinition typeDef; if ((typeDef = ResolveXmlns(xmlNamespace, type, mod.Assembly, asms.Values)) != null) { ((typeDef as IAnnotationProvider).Annotations[RenRef] as List<IReference>).Add(new BamlTypeExtReference(rec, doc, typeDef.Module.Assembly.Name.Name)); } } if (rec.ConverterTypeId == 0xff77 || rec.ConverterTypeId == 0xfe14 || rec.ConverterTypeId == 0xfd99) { ProcessProperty(rec, rec.Value); } } for (int i = 1; i < doc.Count; i++) { ElementStartRecord binding = doc[i - 1] as ElementStartRecord; ConstructorParametersStartRecord param = doc[i] as ConstructorParametersStartRecord; if (binding != null && param != null && binding.TypeId == 0xffec)//Binding { TextRecord path = doc[i + 1] as TextRecord; ProcessProperty(path, path.Value); } } var rootRec = doc.OfType<ElementStartRecord>().FirstOrDefault(); if (rootRec != null && types.ContainsKey(rootRec.TypeId)) { TypeDefinition root = types[rootRec.TypeId]; Dictionary<string, IMemberDefinition> m = new Dictionary<string, IMemberDefinition>(); foreach (PropertyDefinition prop in root.Properties) m.Add(prop.Name, prop); foreach (EventDefinition evt in root.Events) m.Add(evt.Name, evt); //foreach (MethodDefinition mtd in root.Methods) // mems.Add(mtd.Name, mtd); foreach (var rec in doc.OfType<PropertyRecord>()) { if (!(rec.Value is string)) continue; if (m.ContainsKey((string)rec.Value)) { ((m[(string)rec.Value] as IAnnotationProvider).Annotations[RenRef] as List<IReference>).Add(new BamlPropertyReference(rec)); } } } bamls.Add(entry.Key as string, doc); }
private void PopulateReferences(BamlDocument document) { var clrNs = new Dictionary<string, List<Tuple<AssemblyDef, string>>>(); assemblyRefs.Clear(); foreach (AssemblyInfoRecord rec in document.OfType<AssemblyInfoRecord>()) { AssemblyDef assembly = context.Resolver.ResolveThrow(rec.AssemblyFullName, module); assemblyRefs.Add(rec.AssemblyId, assembly); if (!context.Modules.Any(m => m.Assembly == assembly)) continue; foreach (CustomAttribute attr in assembly.CustomAttributes.FindAll("System.Windows.Markup.XmlnsDefinitionAttribute")) { clrNs.AddListEntry( (UTF8String)attr.ConstructorArguments[0].Value, Tuple.Create(assembly, (string)(UTF8String)attr.ConstructorArguments[1].Value)); } } xmlnsCtx = new XmlNsContext(document, assemblyRefs); typeRefs.Clear(); foreach (TypeInfoRecord rec in document.OfType<TypeInfoRecord>()) { AssemblyDef assembly; var asmId = (short)(rec.AssemblyId & 0xfff); if (asmId == -1) assembly = things.FrameworkAssembly; else assembly = assemblyRefs[(ushort)asmId]; // WPF uses Assembly.GetType to load it, so if no assembly specified in the TypeSig, it must be in current assembly. AssemblyDef assemblyRef = module.Assembly == assembly ? null : context.Resolver.ResolveThrow(module.GetAssemblyRefs().Single(r => r.FullName == assembly.FullName), module); TypeSig typeSig = TypeNameParser.ParseAsTypeSigReflectionThrow(module, rec.TypeFullName, new DummyAssemblyRefFinder(assemblyRef)); typeRefs[rec.TypeId] = typeSig; AddTypeSigReference(typeSig, new BAMLTypeReference(typeSig, rec)); } attrRefs.Clear(); foreach (AttributeInfoRecord rec in document.OfType<AttributeInfoRecord>()) { TypeSig declType; if (typeRefs.TryGetValue(rec.OwnerTypeId, out declType)) { TypeDef type = declType.ToBasicTypeDefOrRef().ResolveTypeDefThrow(); attrRefs[rec.AttributeId] = AnalyzeAttributeReference(type, rec); } else { Debug.Assert((short)rec.OwnerTypeId < 0); TypeDef declTypeDef = things.Types((KnownTypes)(-(short)rec.OwnerTypeId)); attrRefs[rec.AttributeId] = AnalyzeAttributeReference(declTypeDef, rec); } } strings.Clear(); foreach (StringInfoRecord rec in document.OfType<StringInfoRecord>()) { strings[rec.StringId] = rec; } foreach (PIMappingRecord rec in document.OfType<PIMappingRecord>()) { var asmId = (short)(rec.AssemblyId & 0xfff); AssemblyDef assembly; if (asmId == -1) assembly = things.FrameworkAssembly; else assembly = assemblyRefs[(ushort)asmId]; Tuple<AssemblyDef, string> scope = Tuple.Create(assembly, rec.ClrNamespace); clrNs.AddListEntry(rec.XmlNamespace, scope); } xmlns.Clear(); foreach (XmlnsPropertyRecord rec in document.OfType<XmlnsPropertyRecord>()) { List<Tuple<AssemblyDef, string>> clrMap; if (clrNs.TryGetValue(rec.XmlNamespace, out clrMap)) { xmlns[rec.Prefix] = clrMap; foreach (var scope in clrMap) xmlnsCtx.AddNsMap(scope, rec.Prefix); } } }
private static void AdjustAttributeIds(BamlDocument document, ushort offset) { const string AttributeIdPropertyName = "AttributeId"; var existingAttributeInfoRecords = document.OfType<AttributeInfoRecord>().ToList(); foreach (var record in document) { ushort? attributeId = record.TryGetPropertyValue(AttributeIdPropertyName) as ushort?; if (attributeId == null || record is AttributeInfoRecord || !existingAttributeInfoRecords.Any(r => r.AttributeId == attributeId)) { continue; } record.TrySetPropertyValue(AttributeIdPropertyName, (ushort)(attributeId.Value + offset)); } foreach (var attributeInfoRecord in existingAttributeInfoRecords) { attributeInfoRecord.AttributeId += offset; } }
void AnalysisResource(ModuleDefinition mod, int resId) { EmbeddedResource res = mod.Resources[resId] as EmbeddedResource; ResourceReader resRdr = new ResourceReader(res.GetResourceStream()); Dictionary <string, object> ress; Dictionary <string, BamlDocument> bamls; (res as IAnnotationProvider).Annotations["Gresources"] = ress = new Dictionary <string, object>(); (res as IAnnotationProvider).Annotations["Gbamls"] = bamls = new Dictionary <string, BamlDocument>(); int cc = 0; foreach (DictionaryEntry entry in resRdr) { Stream stream = null; if (entry.Value is Stream) { byte[] buff = new byte[(entry.Value as Stream).Length]; (entry.Value as Stream).Position = 0; (entry.Value as Stream).Read(buff, 0, buff.Length); ress.Add(entry.Key as string, stream = new MemoryStream(buff)); } else { ress.Add(entry.Key as string, entry.Value); } if (stream != null && (entry.Key as string).EndsWith(".baml")) { BamlDocument doc = BamlReader.ReadDocument(stream); int c = 0; int asmId = -1; foreach (var rec in doc.OfType <AssemblyInfoRecord>()) { if (AssemblyNameReference.Parse(rec.AssemblyFullName).Name == mod.Assembly.Name.Name) { asmId = rec.AssemblyId; } } Dictionary <ushort, TypeDefinition> types = new Dictionary <ushort, TypeDefinition>(); foreach (var rec in doc.OfType <TypeInfoRecord>()) { if ((rec.AssemblyId & 0xfff) == asmId) { TypeDefinition type = mod.GetType(rec.TypeFullName); if (type != null) { types.Add(rec.TypeId, type); ((type as IAnnotationProvider).Annotations["RenRef"] as List <IReference>).Add(new BamlTypeReference(rec)); c++; cc++; } } } Dictionary <ushort, PropertyDefinition> ps = new Dictionary <ushort, PropertyDefinition>(); foreach (var rec in doc.OfType <AttributeInfoRecord>()) { if (types.ContainsKey(rec.OwnerTypeId)) { PropertyDefinition prop = types[rec.OwnerTypeId].Properties.FirstOrDefault(p => p.Name == rec.Name); if (prop != null && types[rec.OwnerTypeId].Fields.FirstOrDefault(fld => fld.Name == prop.Name + "Property") == null) { ((prop as IAnnotationProvider).Annotations["RenRef"] as List <IReference>).Add(new BamlAttributeReference(rec)); c++; cc++; } } } var rootRec = doc.OfType <ElementStartRecord>().FirstOrDefault(); if (rootRec != null && types.ContainsKey(rootRec.TypeId)) { TypeDefinition root = types[rootRec.TypeId]; Dictionary <string, IMemberDefinition> mems = new Dictionary <string, IMemberDefinition>(); foreach (PropertyDefinition prop in root.Properties) { mems.Add(prop.Name, prop); } foreach (EventDefinition evt in root.Events) { mems.Add(evt.Name, evt); } foreach (MethodDefinition mtd in root.Methods) { mems.Add(mtd.Name, mtd); } foreach (var rec in doc.OfType <PropertyRecord>()) { if (!(rec.Value is string)) { continue; } if (mems.ContainsKey((string)rec.Value)) { ((mems[(string)rec.Value] as IAnnotationProvider).Annotations["RenRef"] as List <IReference>).Add(new BamlPropertyReference(rec)); c++; cc++; } } } if (c != 0) { bamls.Add(entry.Key as string, doc); } } } if (cc != 0) { ((res as IAnnotationProvider).Annotations["RenRef"] as List <IReference>).Add(new SaveBamlsReference(mod, resId)); } }
private void ProcessBaml(ModuleDefinition mod, Dictionary <string, BamlDocument> bamls, ref int cc, DictionaryEntry entry, Stream stream) { cc++; docName = entry.Key as string; doc = BamlReader.ReadDocument(stream); (mod as IAnnotationProvider).Annotations[RenMode] = NameMode.Letters; for (int i = 0; i < doc.Count; i++) { if (doc[i] is LineNumberAndPositionRecord || doc[i] is LinePositionRecord) { doc.RemoveAt(i); i--; } } int asmId = -1; Dictionary <ushort, AssemblyDefinition> asms = new Dictionary <ushort, AssemblyDefinition>(); foreach (var rec in doc.OfType <AssemblyInfoRecord>()) { AssemblyNameReference nameRef = AssemblyNameReference.Parse(rec.AssemblyFullName); if (nameRef.Name == mod.Assembly.Name.Name) { asmId = rec.AssemblyId; rec.AssemblyFullName = GetBamlAssemblyFullName(mod.Assembly.Name); asms.Add(rec.AssemblyId, mod.Assembly); PopulateMembers(mod.Assembly); nameRef = null; } else { bool added = false; foreach (var i in ivtMap) { if (i.Key.Name.Name == nameRef.Name) { rec.AssemblyFullName = GetBamlAssemblyFullName(i.Key.Name); asms.Add(rec.AssemblyId, i.Key); PopulateMembers(i.Key); nameRef = null; added = true; break; } } if (!added) { var asmRefDef = GlobalAssemblyResolver.Instance.Resolve(nameRef); if (asmRefDef != null) { PopulateMembers(asmRefDef); } } } } Dictionary <ushort, TypeDefinition> types = new Dictionary <ushort, TypeDefinition>(); foreach (var rec in doc.OfType <TypeInfoRecord>()) { AssemblyDefinition asm; if (asms.TryGetValue((ushort)(rec.AssemblyId & 0xfff), out asm)) { TypeReference type = TypeParser.ParseType(asm.MainModule, rec.TypeFullName); if (type != null) { types.Add(rec.TypeId, type.Resolve()); AddTypeRenRefs(type, type, rec); rec.TypeFullName = TypeParser.ToParseable(type); } } } Dictionary <string, string> xmlns = new Dictionary <string, string>(); foreach (var rec in doc.OfType <XmlnsPropertyRecord>()) { xmlns[rec.Prefix] = rec.XmlNamespace; } Dictionary <ushort, string> ps = new Dictionary <ushort, string>(); foreach (var rec in doc.OfType <AttributeInfoRecord>()) { if (types.ContainsKey(rec.OwnerTypeId)) { PropertyDefinition prop = types[rec.OwnerTypeId].Properties.SingleOrDefault(p => p.Name == rec.Name); if (prop != null) { ((prop as IAnnotationProvider).Annotations[RenRef] as List <IReference>).Add(new BamlAttributeReference(rec)); } EventDefinition evt = types[rec.OwnerTypeId].Events.SingleOrDefault(p => p.Name == rec.Name); if (evt != null) { ((evt as IAnnotationProvider).Annotations[RenRef] as List <IReference>).Add(new BamlAttributeReference(rec)); } FieldDefinition field = types[rec.OwnerTypeId].Fields.SingleOrDefault(p => p.Name == rec.Name); if (field != null) { ((field as IAnnotationProvider).Annotations[RenRef] as List <IReference>).Add(new BamlAttributeReference(rec)); } //Attached property MethodDefinition getM = types[rec.OwnerTypeId].Methods.SingleOrDefault(p => p.Name == "Get" + rec.Name); if (getM != null) { (getM as IAnnotationProvider).Annotations[RenOk] = false; } MethodDefinition setM = types[rec.OwnerTypeId].Methods.SingleOrDefault(p => p.Name == "Set" + rec.Name); if (setM != null) { (setM as IAnnotationProvider).Annotations[RenOk] = false; } } ps.Add(rec.AttributeId, rec.Name); } foreach (var rec in doc.OfType <PropertyWithConverterRecord>()) { if (rec.ConverterTypeId == 0xfd4c || ((short)rec.AttributeId > 0 && ps[rec.AttributeId] == "TypeName")) //TypeExtension { string type = rec.Value; string xmlNamespace; if (type.IndexOf(':') != -1) { xmlNamespace = xmlns[type.Substring(0, type.IndexOf(':'))]; type = type.Substring(type.IndexOf(':') + 1, type.Length - type.IndexOf(':') - 1); } else { xmlNamespace = xmlns[""]; } TypeDefinition typeDef; if ((typeDef = ResolveXmlns(xmlNamespace, type, mod.Assembly, asms.Values)) != null) { ((typeDef as IAnnotationProvider).Annotations[RenRef] as List <IReference>).Add(new BamlTypeExtReference(rec, doc, typeDef.Module.Assembly.Name.Name)); } } if (rec.ConverterTypeId == 0xff77 || rec.ConverterTypeId == 0xfe14 || rec.ConverterTypeId == 0xfd99) { ProcessProperty(rec, rec.Value); } } for (int i = 1; i < doc.Count; i++) { ElementStartRecord binding = doc[i - 1] as ElementStartRecord; ConstructorParametersStartRecord param = doc[i] as ConstructorParametersStartRecord; if (binding != null && param != null && binding.TypeId == 0xffec)//Binding { TextRecord path = doc[i + 1] as TextRecord; ProcessProperty(path, path.Value); } } var rootRec = doc.OfType <ElementStartRecord>().FirstOrDefault(); if (rootRec != null && types.ContainsKey(rootRec.TypeId)) { TypeDefinition root = types[rootRec.TypeId]; Dictionary <string, IMemberDefinition> m = new Dictionary <string, IMemberDefinition>(); foreach (PropertyDefinition prop in root.Properties) { m.Add(prop.Name, prop); } foreach (EventDefinition evt in root.Events) { m.Add(evt.Name, evt); } //foreach (MethodDefinition mtd in root.Methods) // mems.Add(mtd.Name, mtd); foreach (var rec in doc.OfType <PropertyRecord>()) { if (!(rec.Value is string)) { continue; } if (m.ContainsKey((string)rec.Value)) { ((m[(string)rec.Value] as IAnnotationProvider).Annotations[RenRef] as List <IReference>).Add(new BamlPropertyReference(rec)); } } } bamls.Add(entry.Key as string, doc); }