void AnalyzeMessageAttach(BAMLAnalyzer analyzer, Tuple<IDnlibDef, AttributeInfoRecord, TypeDef> attr, string value) { if (attr.Item2 == null) return; var attrDeclType = analyzer.ResolveType(attr.Item2.OwnerTypeId); if (attrDeclType.FullName != "Caliburn.Micro.Message") return; foreach (var msg in value.Split(';')) { string msgStr; if (msg.Contains("=")) { msgStr = msg.Split('=')[1].Trim('[', ']', ' '); } else { msgStr = msg.Trim('[', ']', ' '); } if (msgStr.StartsWith("Action")) msgStr = msgStr.Substring(6); int parenIndex = msgStr.IndexOf('('); if (parenIndex != -1) msgStr = msgStr.Substring(0, parenIndex); string actName = msgStr.Trim(); foreach (var method in analyzer.LookupMethod(actName)) analyzer.NameService.SetCanRename(method, false); } }
void AnalyzeAutoBind(BAMLAnalyzer analyzer, Tuple<IDnlibDef, AttributeInfoRecord, TypeDef> attr, string value) { if (!(attr.Item1 is PropertyDef) || ((PropertyDef)attr.Item1).DeclaringType.FullName != "System.Windows.FrameworkElement") return; foreach (var method in analyzer.LookupMethod(value)) analyzer.NameService.SetCanRename(method, false); foreach (var method in analyzer.LookupProperty(value)) analyzer.NameService.SetCanRename(method, false); }
void AnalyzeActionMessage(BAMLAnalyzer analyzer, Tuple<IDnlibDef, AttributeInfoRecord, TypeDef> attr, string value) { if (attr.Item2 == null) return; var attrDeclType = analyzer.ResolveType(attr.Item2.OwnerTypeId); if (attrDeclType.FullName != "Caliburn.Micro.ActionMessage") return; foreach (var method in analyzer.LookupMethod(value)) analyzer.NameService.SetCanRename(method, false); }
void AnalyzeBAMLElement(BAMLAnalyzer analyzer, BamlElement elem) { foreach (var rec in elem.Body) { var prop = rec as PropertyWithConverterRecord; if (prop == null) continue; var attr = analyzer.ResolveAttribute(prop.AttributeId); string attrName = null; if (attr.Item2 != null) attrName = attr.Item2.Name; else if (attr.Item1 != null) attrName = attr.Item1.Name; if (attrName == "Attach") AnalyzeMessageAttach(analyzer, attr, prop.Value); if (attrName == "Name") AnalyzeAutoBind(analyzer, attr, prop.Value); if (attrName == "MethodName") AnalyzeActionMessage(analyzer, attr, prop.Value); } }
void AnalyzeResources(ConfuserContext context, INameService service, ModuleDefMD module) { if (analyzer == null) { analyzer = new BAMLAnalyzer(context, service); analyzer.AnalyzeElement += AnalyzeBAMLElement; } var wpfResInfo = new Dictionary<string, Dictionary<string, BamlDocument>>(); foreach (EmbeddedResource res in module.Resources.OfType<EmbeddedResource>()) { Match match = ResourceNamePattern.Match(res.Name); if (!match.Success) continue; var resInfo = new Dictionary<string, BamlDocument>(); res.Data.Position = 0; var reader = new ResourceReader(new ImageStream(res.Data)); IDictionaryEnumerator enumerator = reader.GetEnumerator(); while (enumerator.MoveNext()) { var name = (string)enumerator.Key; if (!name.EndsWith(".baml")) continue; string typeName; byte[] data; reader.GetResourceData(name, out typeName, out data); BamlDocument document = analyzer.Analyze(module, name, data); document.DocumentName = name; resInfo.Add(name, document); } if (resInfo.Count > 0) wpfResInfo.Add(res.Name, resInfo); } if (wpfResInfo.Count > 0) context.Annotations.Set(module, BAMLKey, wpfResInfo); }