From b9dd30a0ec16316d5b4fda39e46a6818d9acf3fc Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 30 May 2011 16:31:29 +0200 Subject: [PATCH 01/21] add ignored test for special cases with ResourceDictionaries; remove unneeded private modifier --- .../XmlBamlReader.cs | 187 +++++++++--------- .../Tests/Cases/Resources.xaml | 16 ++ .../Tests/Cases/Resources.xaml.cs | 26 +++ .../Tests/ILSpy.BamlDecompiler.Tests.csproj | 5 + ILSpy.BamlDecompiler/Tests/TestRunner.cs | 8 + 5 files changed, 147 insertions(+), 95 deletions(-) create mode 100644 ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml create mode 100644 ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml.cs diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs index 6417549f7..78a4df1ec 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs @@ -21,40 +21,40 @@ namespace Ricciolo.StylesExplorer.MarkupReflection #region Variables - private BamlBinaryReader reader; - private Dictionary assemblyTable = new Dictionary(); - private Dictionary stringTable = new Dictionary(); - private Dictionary typeTable = new Dictionary(); - private Dictionary propertyTable = new Dictionary(); + BamlBinaryReader reader; + Dictionary assemblyTable = new Dictionary(); + Dictionary stringTable = new Dictionary(); + Dictionary typeTable = new Dictionary(); + Dictionary propertyTable = new Dictionary(); - private readonly ITypeResolver _resolver; + readonly ITypeResolver _resolver; - private BamlRecordType currentType; + BamlRecordType currentType; - private Stack elements = new Stack(); - private Stack readingElements = new Stack(); - private Stack keysResources = new Stack(); - private NodesCollection nodes = new NodesCollection(); - private List _mappings = new List(); - private XmlBamlNode _currentNode; + Stack elements = new Stack(); + Stack readingElements = new Stack(); + Stack keysResources = new Stack(); + NodesCollection nodes = new NodesCollection(); + List _mappings = new List(); + XmlBamlNode _currentNode; - private readonly KnownInfo KnownInfo; + readonly KnownInfo KnownInfo; - private int complexPropertyOpened = 0; + int complexPropertyOpened = 0; - private bool intoAttribute = false; - private bool initialized; - private bool _eof; + bool intoAttribute = false; + bool initialized; + bool _eof; - private bool isPartialDefKeysClosed = true; - private bool isDefKeysClosed = true; + bool isPartialDefKeysClosed = true; + bool isDefKeysClosed = true; - private int bytesToSkip; + int bytesToSkip; - private static readonly MethodInfo staticConvertCustomBinaryToObjectMethod = Type.GetType("System.Windows.Markup.XamlPathDataSerializer,PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35").GetMethod("StaticConvertCustomBinaryToObject", BindingFlags.Static | BindingFlags.Public); - private readonly TypeDeclaration XamlTypeDeclaration; - private readonly XmlNameTable _nameTable = new NameTable(); - private IDictionary _rootNamespaces; + static readonly MethodInfo staticConvertCustomBinaryToObjectMethod = Type.GetType("System.Windows.Markup.XamlPathDataSerializer,PresentationFramework, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35").GetMethod("StaticConvertCustomBinaryToObject", BindingFlags.Static | BindingFlags.Public); + readonly TypeDeclaration XamlTypeDeclaration; + readonly XmlNameTable _nameTable = new NameTable(); + IDictionary _rootNamespaces; public const string XWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml"; public const string DefaultWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"; @@ -240,7 +240,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return ReadInternal(); } - private bool ReadInternal() + bool ReadInternal() { EnsureInit(); @@ -288,10 +288,10 @@ namespace Ricciolo.StylesExplorer.MarkupReflection else currentType = (BamlRecordType)type; -// Debug.WriteLine(currentType); + Debug.WriteLine(string.Format("{0} (0x{0:x})", currentType)); } - private bool SetNextNode() + bool SetNextNode() { while (nodes.Count > 0) { @@ -315,7 +315,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return false; } - private void ProcessNext() + void ProcessNext() { switch (currentType) { @@ -460,7 +460,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(new XmlBamlText(text)); } - private void ComputeBytesToSkip() + void ComputeBytesToSkip() { bytesToSkip = 0; switch (currentType) @@ -485,7 +485,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - private void EnsureInit() + void EnsureInit() { if (!initialized) { @@ -606,7 +606,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - private PropertyDeclaration GetPropertyDeclaration(short identifier) + PropertyDeclaration GetPropertyDeclaration(short identifier) { PropertyDeclaration declaration; if (identifier >= 0) @@ -624,7 +624,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return declaration; } - private object GetResourceName(short identifier) + object GetResourceName(short identifier) { if (identifier >= 0) { @@ -644,7 +644,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - private void ReadPropertyDictionaryStart() + void ReadPropertyDictionaryStart() { short identifier = reader.ReadInt16(); @@ -658,14 +658,14 @@ namespace Ricciolo.StylesExplorer.MarkupReflection isPartialDefKeysClosed = true; } - private void ReadPropertyDictionaryEnd() + void ReadPropertyDictionaryEnd() { keysResources.Pop(); CloseElement(); } - private void ReadPropertyCustom() + void ReadPropertyCustom() { short identifier = reader.ReadInt16(); short serializerTypeId = reader.ReadInt16(); @@ -725,7 +725,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(property); } - private string DeserializePoints() + string DeserializePoints() { using (StringWriter writer = new StringWriter()) { @@ -745,7 +745,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - private String Deserialize3DPoints() + String Deserialize3DPoints() { using (StringWriter writer = new StringWriter()) { @@ -769,7 +769,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - private static Int32Collection DeserializeInt32CollectionFrom(BinaryReader reader) + static Int32Collection DeserializeInt32CollectionFrom(BinaryReader reader) { IntegerCollectionType type = (IntegerCollectionType)reader.ReadByte(); int capacity = reader.ReadInt32(); @@ -799,7 +799,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection throw new ArgumentException(); } - private void ReadPropertyWithExtension() + void ReadPropertyWithExtension() { short identifier = reader.ReadInt16(); short x = reader.ReadInt16(); @@ -855,7 +855,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(property); } - private void ReadProperty() + void ReadProperty() { short identifier = reader.ReadInt16(); string text = reader.ReadString(); @@ -867,7 +867,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(property); } - private void ReadPropertyWithConverter() + void ReadPropertyWithConverter() { short identifier = reader.ReadInt16(); string text = reader.ReadString(); @@ -880,7 +880,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(property); } - private void ReadAttributeInfo() + void ReadAttributeInfo() { short key = reader.ReadInt16(); short identifier = reader.ReadInt16(); @@ -891,7 +891,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection this.propertyTable.Add(key, declaration2); } - private void ReadDefAttributeKeyType() + void ReadDefAttributeKeyType() { short typeIdentifier = reader.ReadInt16(); reader.ReadByte(); @@ -904,7 +904,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection AddDefKey(position, this.GetTypeExtension(typeIdentifier)); } - private void ReadDefAttribute() + void ReadDefAttribute() { string text = reader.ReadString(); short identifier = reader.ReadInt16(); @@ -933,7 +933,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(property); } - private void ReadDefAttributeKeyString() + void ReadDefAttributeKeyString() { short num = reader.ReadInt16(); int position = reader.ReadInt32(); @@ -946,14 +946,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection AddDefKey(position, text); } - private void AddDefKey(long position, string text) + void AddDefKey(long position, string text) { // Guardo se la dichiarazione delle chiavi risulta chiusa // Se è aperta c'è un sotto ResourceDictionary oppure è il root ResourceDictionary if (isDefKeysClosed) - { keysResources.Push(new KeysResourcesCollection()); - } // Guardo se è stata chiusa la dichiarazione parziale (mediante dichiarazione OptimizedStaticResource) // Si chiude il ciclo di chiavi @@ -969,7 +967,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection keysResources.Peek().Last.Keys[position] = text; } - private void ReadXmlnsProperty() + void ReadXmlnsProperty() { string prefix = reader.ReadString(); string @namespace = reader.ReadString(); @@ -1001,7 +999,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection namespaces.Add(new XmlNamespace(prefix, @namespace)); } - private void ReadElementEnd() + void ReadElementEnd() { CloseElement(); @@ -1013,7 +1011,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection keysResources.Peek().RemoveAt(0); } - private void ReadPropertyComplexStart() + void ReadPropertyComplexStart() { short identifier = reader.ReadInt16(); @@ -1026,7 +1024,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection complexPropertyOpened++; } - private XmlBamlElement FindXmlBamlElement() + XmlBamlElement FindXmlBamlElement() { return elements.Peek(); @@ -1040,7 +1038,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection //return element; } - private void ReadPropertyListStart() + void ReadPropertyListStart() { short identifier = reader.ReadInt16(); @@ -1051,12 +1049,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(property); } - private void ReadPropertyListEnd() + void ReadPropertyListEnd() { CloseElement(); } - private void ReadPropertyComplexEnd() + void ReadPropertyComplexEnd() { XmlBamlPropertyElement propertyElement = (XmlBamlPropertyElement) elements.Peek(); @@ -1089,7 +1087,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - private void FormatElementExtension(XmlBamlElement element, StringBuilder sb) + void FormatElementExtension(XmlBamlElement element, StringBuilder sb) { sb.Append("{"); sb.Append(FormatTypeDeclaration(element.TypeDeclaration)); @@ -1147,7 +1145,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - private static bool IsExtension(IEnumerator enumerator) + static bool IsExtension(IEnumerator enumerator) { bool r = true; while (enumerator.MoveNext() && r) @@ -1162,12 +1160,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return r; } - private void CloseElement() + void CloseElement() { nodes.Enqueue(new XmlBamlEndElement(elements.Pop())); } - private void ReadElementStart() + void ReadElementStart() { short identifier = reader.ReadInt16(); sbyte flags = reader.ReadSByte(); @@ -1222,7 +1220,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - private void AddKeyToElement(string key) + void AddKeyToElement(string key) { PropertyDeclaration pd = new PropertyDeclaration("Key", XamlTypeDeclaration); XmlBamlProperty property = new XmlBamlProperty(PropertyType.Key, pd); @@ -1232,12 +1230,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(property); } - private XmlPIMapping FindByClrNamespaceAndAssemblyId(TypeDeclaration declaration) + XmlPIMapping FindByClrNamespaceAndAssemblyId(TypeDeclaration declaration) { return FindByClrNamespaceAndAssemblyId(declaration.Namespace, declaration.AssemblyId); } - private XmlPIMapping FindByClrNamespaceAndAssemblyId(string clrNamespace, int assemblyId) + XmlPIMapping FindByClrNamespaceAndAssemblyId(string clrNamespace, int assemblyId) { if (clrNamespace == XamlTypeDeclaration.Namespace && assemblyId == XamlTypeDeclaration.AssemblyId) return new XmlPIMapping(XmlPIMapping.XamlNamespace, 0, clrNamespace); @@ -1252,7 +1250,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return null; } - private void ReadPIMapping() + void ReadPIMapping() { string xmlNamespace = reader.ReadString(); string clrNamespace = reader.ReadString(); @@ -1261,26 +1259,26 @@ namespace Ricciolo.StylesExplorer.MarkupReflection Mappings.Add(new XmlPIMapping(xmlNamespace, assemblyId, clrNamespace)); } - private void ReadContentProperty() + void ReadContentProperty() { reader.ReadInt16(); // Non serve aprire niente, è il default } - private static void ReadConstructorParametersStart() + static void ReadConstructorParametersStart() { //this.constructorParameterTable.Add(this.elements.Peek()); //PromoteDataToComplexProperty(); } - private static void ReadConstructorParametersEnd() + static void ReadConstructorParametersEnd() { //this.constructorParameterTable.Remove(this.elements.Peek()); //properties.Pop(); } - private void ReadConstructorParameterType() + void ReadConstructorParameterType() { short identifier = reader.ReadInt16(); @@ -1288,14 +1286,14 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(new XmlBamlText(GetTypeExtension(identifier))); } - private void ReadText() + void ReadText() { string text = reader.ReadString(); nodes.Enqueue(new XmlBamlText(text)); } - private void ReadKeyElementStart() + void ReadKeyElementStart() { short typeIdentifier = reader.ReadInt16(); byte valueIdentifier = reader.ReadByte(); @@ -1315,7 +1313,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection complexPropertyOpened++; } - private void ReadKeyElementEnd() + void ReadKeyElementEnd() { XmlBamlPropertyElement propertyElement = (XmlBamlPropertyElement)elements.Peek(); @@ -1332,7 +1330,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - private static void ReadStaticResourceStart() + static void ReadStaticResourceStart() { //short identifier = reader.ReadInt16(); //byte n = reader.ReadByte(); @@ -1342,12 +1340,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection throw new NotImplementedException("StaticResourceStart"); } - private static void ReadStaticResourceEnd() + static void ReadStaticResourceEnd() { throw new NotImplementedException("ReadStaticResourceEnd"); } - private static void ReadStaticResourceId() + static void ReadStaticResourceId() { //short identifier = reader.ReadInt16(); //object staticResource = this.GetStaticResource(identifier); @@ -1356,7 +1354,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection throw new NotImplementedException("StaticResourceId"); } - private void ReadPresentationOptionsAttribute() + void ReadPresentationOptionsAttribute() { string text = reader.ReadString(); short valueIdentifier = reader.ReadInt16(); @@ -1367,7 +1365,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection property.Value = text; } - private void ReadPropertyTypeReference() + void ReadPropertyTypeReference() { short identifier = reader.ReadInt16(); short typeIdentifier = reader.ReadInt16(); @@ -1381,7 +1379,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(property); } - private void ReadOptimizedStaticResource() + void ReadOptimizedStaticResource() { byte num = reader.ReadByte(); short typeIdentifier = reader.ReadInt16(); @@ -1407,12 +1405,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection keysResources.Peek().Last.StaticResources.Add(resource); } - private string GetTemplateBindingExtension(PropertyDeclaration propertyDeclaration) + string GetTemplateBindingExtension(PropertyDeclaration propertyDeclaration) { return String.Format("{{TemplateBinding {0}}}", FormatPropertyDeclaration(propertyDeclaration, true, false, false)); } - private string GetStaticExtension(string name) + string GetStaticExtension(string name) { string prefix = this.LookupPrefix(XmlPIMapping.XamlNamespace, false); if (String.IsNullOrEmpty(prefix)) @@ -1421,12 +1419,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return String.Format("{{{0}:Static {1}}}", prefix, name); } - private string GetExtension(TypeDeclaration declaration, string value) + string GetExtension(TypeDeclaration declaration, string value) { return String.Format("{{{0} {1}}}", FormatTypeDeclaration(declaration), value); } - private string GetTypeExtension(short typeIdentifier) + string GetTypeExtension(short typeIdentifier) { string prefix = this.LookupPrefix(XmlPIMapping.XamlNamespace, false); if (String.IsNullOrEmpty(prefix)) @@ -1435,7 +1433,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return String.Format("{{{0}:Type {1}}}", prefix, FormatTypeDeclaration(GetTypeDeclaration(typeIdentifier))); } - private string FormatTypeDeclaration(TypeDeclaration typeDeclaration) + string FormatTypeDeclaration(TypeDeclaration typeDeclaration) { XmlPIMapping mapping = FindByClrNamespaceAndAssemblyId(typeDeclaration.Namespace, typeDeclaration.AssemblyId); string prefix = (mapping != null) ? this.LookupPrefix(mapping.XmlNamespace, false) : null; @@ -1450,7 +1448,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection - private string FormatPropertyDeclaration(PropertyDeclaration propertyDeclaration, bool withPrefix, bool useReading, bool checkType) + string FormatPropertyDeclaration(PropertyDeclaration propertyDeclaration, bool withPrefix, bool useReading, bool checkType) { StringBuilder sb = new StringBuilder(); @@ -1486,7 +1484,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return sb.ToString(); } - private void ReadPropertyWithStaticResourceIdentifier() + void ReadPropertyWithStaticResourceIdentifier() { short identifier = reader.ReadInt16(); short staticIdentifier = reader.ReadInt16(); @@ -1503,8 +1501,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(property); } - - private object GetStaticResource(short identifier) + object GetStaticResource(short identifier) { // Recupero la risorsa nel gruppo corrente foreach (KeysResourcesCollection resource in keysResources) @@ -1526,7 +1523,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection throw new ArgumentException("Cannot find StaticResource", "identifier"); } - private void ReadTextWithConverter() + void ReadTextWithConverter() { string text = reader.ReadString(); reader.ReadInt16(); @@ -1534,7 +1531,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(new XmlBamlText(text)); } - private void ReadTypeInfo() + void ReadTypeInfo() { short typeId = reader.ReadInt16(); short assemblyId = reader.ReadInt16(); @@ -1555,21 +1552,21 @@ namespace Ricciolo.StylesExplorer.MarkupReflection this.typeTable.Add(typeId, declaration); } - private void ReadAssemblyInfo() + void ReadAssemblyInfo() { short key = reader.ReadInt16(); string text = reader.ReadString(); this.assemblyTable.Add(key, text); } - private void ReadStringInfo() + void ReadStringInfo() { short key = reader.ReadInt16(); string text = reader.ReadString(); this.stringTable.Add(key, text); } - private TypeDeclaration GetTypeDeclaration(short identifier) + TypeDeclaration GetTypeDeclaration(short identifier) { TypeDeclaration declaration; if (identifier >= 0) @@ -1598,7 +1595,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return this.assemblyTable[identifier]; } - private XmlBamlNode CurrentNode + XmlBamlNode CurrentNode { get { @@ -1886,7 +1883,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return this.LookupPrefix(namespaceName, true); } - private string LookupPrefix(string namespaceName, bool useReading) + string LookupPrefix(string namespaceName, bool useReading) { Stack elements; if (useReading) @@ -1900,7 +1897,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return LookupPrefix(namespaceName, namespaces); } - private static string LookupPrefix(string namespaceName, XmlNamespaceCollection namespaces) + static string LookupPrefix(string namespaceName, XmlNamespaceCollection namespaces) { for (int x = 0; x < namespaces.Count; x++) { @@ -1959,7 +1956,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return DequeueInternal(false); } - private XmlBamlNode DequeueInternal(bool remove) + XmlBamlNode DequeueInternal(bool remove) { if (this.Count > 0) { diff --git a/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml b/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml new file mode 100644 index 000000000..30f77b120 --- /dev/null +++ b/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml.cs b/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml.cs new file mode 100644 index 000000000..1dbef3631 --- /dev/null +++ b/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml.cs @@ -0,0 +1,26 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) + +using System; +using System.Collections.Generic; +using System.Text; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; + +namespace ILSpy.BamlDecompiler.Tests.Cases +{ + /// + /// Interaction logic for Resources.xaml + /// + public partial class Resources : Window + { + public Resources() + { + InitializeComponent(); + } + } +} \ No newline at end of file diff --git a/ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj b/ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj index 61cfa995c..4c65ef0e0 100644 --- a/ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj +++ b/ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj @@ -66,6 +66,10 @@ + + Resources.xaml + Code + Simple.xaml Code @@ -100,6 +104,7 @@ + Always diff --git a/ILSpy.BamlDecompiler/Tests/TestRunner.cs b/ILSpy.BamlDecompiler/Tests/TestRunner.cs index 441116873..b262b5322 100644 --- a/ILSpy.BamlDecompiler/Tests/TestRunner.cs +++ b/ILSpy.BamlDecompiler/Tests/TestRunner.cs @@ -31,6 +31,13 @@ namespace ILSpy.BamlDecompiler.Tests RunTest("cases/simpledictionary"); } + [Test, Ignore] + public void Resources() + { + RunTest("cases/resources"); + } + + #region RunTest void RunTest(string name) { string asmPath = typeof(TestRunner).Assembly.Location; @@ -69,5 +76,6 @@ namespace ILSpy.BamlDecompiler.Tests return null; } + #endregion } } From e0932a68600bec0d0e566421f775a08844338bed Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Tue, 31 May 2011 22:07:43 +0200 Subject: [PATCH 02/21] implement detection of implicit elements and static resources --- .../BamlBinaryReader.cs | 11 +- .../XmlBamlElement.cs | 39 ++---- .../XmlBamlReader.cs | 129 ++++++++++-------- .../XmlBamlSimpleProperty.cs | 6 + .../Tests/Cases/Resources.xaml | 4 +- ILSpy.BamlDecompiler/Tests/TestRunner.cs | 2 +- 6 files changed, 92 insertions(+), 99 deletions(-) diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BamlBinaryReader.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BamlBinaryReader.cs index 6df206561..f31e0f5f2 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BamlBinaryReader.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/BamlBinaryReader.cs @@ -17,22 +17,15 @@ namespace Ricciolo.StylesExplorer.MarkupReflection public virtual double ReadCompressedDouble() { - switch (this.ReadByte()) - { + switch (this.ReadByte()) { case 1: return 0; - case 2: return 1; - case 3: return -1; - case 4: - { - double num = this.ReadInt32(); - return (num * 1E-06); - } + return ReadInt32() * 1E-06; case 5: return this.ReadDouble(); } diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs index 393ac5c70..c43e8a1ea 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs @@ -9,19 +9,16 @@ using System.Xml; namespace Ricciolo.StylesExplorer.MarkupReflection { - internal class XmlBamlElement : XmlBamlNode + class XmlBamlElement : XmlBamlNode { - private ArrayList _arguments = new ArrayList(); - private XmlNamespaceCollection _namespaces = new XmlNamespaceCollection(); - private TypeDeclaration _typeDeclaration; - private KeysResourcesCollection _keysResources = new KeysResourcesCollection(); - private long _position; + ArrayList _arguments = new ArrayList(); + XmlNamespaceCollection _namespaces = new XmlNamespaceCollection(); + KeysResourcesCollection _keysResources = new KeysResourcesCollection(); public XmlBamlElement() { } - public XmlBamlElement(XmlBamlElement parent) { this.Namespaces.AddRange(parent.Namespaces); @@ -32,31 +29,15 @@ namespace Ricciolo.StylesExplorer.MarkupReflection get { return _namespaces; } } - public TypeDeclaration TypeDeclaration - { - get - { - return this._typeDeclaration; - } - set - { - this._typeDeclaration = value; - } - } + public TypeDeclaration TypeDeclaration { get; set; } - public override XmlNodeType NodeType - { - get - { - return XmlNodeType.Element; - } + public override XmlNodeType NodeType { + get { return XmlNodeType.Element; } } - public long Position - { - get { return _position; } - set { _position = value; } - } + public long Position { get; set; } + + public bool IsImplicit { get; set; } public override string ToString() { diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs index 78a4df1ec..ff7202cf5 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs @@ -26,6 +26,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection Dictionary stringTable = new Dictionary(); Dictionary typeTable = new Dictionary(); Dictionary propertyTable = new Dictionary(); + List staticResources = new List(); readonly ITypeResolver _resolver; @@ -48,6 +49,8 @@ namespace Ricciolo.StylesExplorer.MarkupReflection bool isPartialDefKeysClosed = true; bool isDefKeysClosed = true; + + int currentKey; int bytesToSkip; @@ -950,8 +953,10 @@ namespace Ricciolo.StylesExplorer.MarkupReflection { // Guardo se la dichiarazione delle chiavi risulta chiusa // Se è aperta c'è un sotto ResourceDictionary oppure è il root ResourceDictionary - if (isDefKeysClosed) + if (isDefKeysClosed) { + currentKey = 0; keysResources.Push(new KeysResourcesCollection()); + } // Guardo se è stata chiusa la dichiarazione parziale (mediante dichiarazione OptimizedStaticResource) // Si chiude il ciclo di chiavi @@ -1061,30 +1066,30 @@ namespace Ricciolo.StylesExplorer.MarkupReflection CloseElement(); complexPropertyOpened--; - // Valuto se contiene tutte extension - int start = nodes.IndexOf(propertyElement) + 1; - IEnumerator enumerator = nodes.GetEnumerator(); - - int c = 0; - while (c < start && enumerator.MoveNext()) - c++; - - if (IsExtension(enumerator)) - { - start--; - nodes.RemoveAt(start); - nodes.RemoveLast(); - - StringBuilder sb = new StringBuilder(); - FormatElementExtension((XmlBamlElement) nodes[start], sb); - - XmlBamlProperty property = - new XmlBamlProperty(PropertyType.Complex, propertyElement.PropertyDeclaration); - property.Value = sb.ToString(); - nodes.Add(property); - - return; - } +// // Valuto se contiene tutte extension +// int start = nodes.IndexOf(propertyElement) + 1; +// IEnumerator enumerator = nodes.GetEnumerator(); +// +// int c = 0; +// while (c < start && enumerator.MoveNext()) +// c++; +// +// if (IsExtension(enumerator)) +// { +// start--; +// nodes.RemoveAt(start); +// nodes.RemoveLast(); +// +// StringBuilder sb = new StringBuilder(); +// FormatElementExtension((XmlBamlElement) nodes[start], sb); +// +// XmlBamlProperty property = +// new XmlBamlProperty(PropertyType.Complex, propertyElement.PropertyDeclaration); +// property.Value = sb.ToString(); +// nodes.Add(property); +// +// return; +// } } void FormatElementExtension(XmlBamlElement element, StringBuilder sb) @@ -1145,24 +1150,22 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - static bool IsExtension(IEnumerator enumerator) + bool IsExtension(IEnumerator enumerator) { - bool r = true; - while (enumerator.MoveNext() && r) - { + while (enumerator.MoveNext()) { object node = enumerator.Current; if (node is XmlBamlElement && !(node is XmlBamlEndElement) && !((XmlBamlElement)node).TypeDeclaration.IsExtension) - { - r = false; - } + return false; } - return r; + return true; } void CloseElement() { - nodes.Enqueue(new XmlBamlEndElement(elements.Pop())); + var e = elements.Pop(); + if (!e.IsImplicit) + nodes.Enqueue(new XmlBamlEndElement(e)); } void ReadElementStart() @@ -1171,6 +1174,8 @@ namespace Ricciolo.StylesExplorer.MarkupReflection sbyte flags = reader.ReadSByte(); if (flags < 0 || flags > 3) throw new NotImplementedException(); + Debug.Print("ElementFlags: " + flags); + TypeDeclaration declaration = GetTypeDeclaration(identifier); XmlBamlElement element; @@ -1195,10 +1200,11 @@ namespace Ricciolo.StylesExplorer.MarkupReflection oldDeclaration = declaration; declaration = GetKnownTypeDeclarationByName(declaration.Type.BaseType.AssemblyQualifiedName); } - element.TypeDeclaration = declaration; + element.IsImplicit = (flags & 2) == 2; elements.Push(element); - nodes.Enqueue(element); + if (!element.IsImplicit) + nodes.Enqueue(element); if (oldDeclaration != null) { nodes.Enqueue(new XmlBamlSimpleProperty(XWPFNamespace, "Class", string.Format("{0}.{1}", oldDeclaration.Namespace, oldDeclaration.Name))); @@ -1207,15 +1213,15 @@ namespace Ricciolo.StylesExplorer.MarkupReflection if (parentElement != null && complexPropertyOpened == 0) { // Calcolo la posizione dell'elemento rispetto al padre - long position = element.Position - parentElement.Position; KeysResource keysResource = (keysResources.Count > 0) ? keysResources.Peek().First : null; - if (keysResource != null && keysResource.Keys.HasKey(position)) + if (keysResource != null && keysResource.Keys.HasKey(currentKey)) { - string key = keysResource.Keys[position]; + string key = keysResource.Keys[currentKey]; // Rimuovo la chiave perché è stata usata - keysResource.Keys.Remove(position); +// keysResource.Keys.Remove(currentKey); AddKeyToElement(key); + currentKey++; } } } @@ -1330,28 +1336,32 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - static void ReadStaticResourceStart() + void ReadStaticResourceStart() { - //short identifier = reader.ReadInt16(); - //byte n = reader.ReadByte(); - //TypeDeclaration declaration = this.GetTypeDeclaration(identifier); - //this.staticResourceTable.Add(declaration); - - throw new NotImplementedException("StaticResourceStart"); + short identifier = reader.ReadInt16(); + byte flags = reader.ReadByte(); + TypeDeclaration declaration = GetTypeDeclaration(identifier); + staticResources.Add(declaration); + + XmlBamlElement element; + if (elements.Any()) + element = new XmlBamlElement(elements.Peek()); + else + element = new XmlBamlElement(); + element.TypeDeclaration = declaration; + elements.Push(element); + nodes.Enqueue(element); } - static void ReadStaticResourceEnd() + void ReadStaticResourceEnd() { - throw new NotImplementedException("ReadStaticResourceEnd"); + CloseElement(); } - static void ReadStaticResourceId() + void ReadStaticResourceId() { - //short identifier = reader.ReadInt16(); - //object staticResource = this.GetStaticResource(identifier); - //TypeDeclaration declaration = this.GetTypeDeclaration(-603); - - throw new NotImplementedException("StaticResourceId"); + short identifier = reader.ReadInt16(); + object staticResource = GetStaticResource(identifier); } void ReadPresentationOptionsAttribute() @@ -1490,7 +1500,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection short staticIdentifier = reader.ReadInt16(); PropertyDeclaration pd = this.GetPropertyDeclaration(identifier); - object staticResource = this.GetStaticResource(staticIdentifier); + object staticResource = GetStaticResource(staticIdentifier); string prefix = this.LookupPrefix(XmlPIMapping.PresentationNamespace, false); string value = String.Format("{{{0}{1}StaticResource {2}}}", prefix, (String.IsNullOrEmpty(prefix)) ? String.Empty : ":", staticResource); @@ -1518,9 +1528,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return resourceGroup.StaticResources[identifier]; } } + + if (identifier < staticResources.Count) + return staticResources[identifier]; - //return "???"; - throw new ArgumentException("Cannot find StaticResource", "identifier"); + return "???" + identifier +"???"; +// throw new ArgumentException("Cannot find StaticResource", "identifier"); } void ReadTextWithConverter() diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlSimpleProperty.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlSimpleProperty.cs index be00e41e1..9440ded4b 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlSimpleProperty.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlSimpleProperty.cs @@ -30,5 +30,11 @@ namespace Ricciolo.StylesExplorer.MarkupReflection public override XmlNodeType NodeType { get { return XmlNodeType.Attribute; } } + + public override string ToString() + { + return string.Format("{{{0}}}{1}=\"{2}\"", NamespaceName, LocalName, Value); + } + } } diff --git a/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml b/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml index 30f77b120..04fc3f921 100644 --- a/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml +++ b/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml @@ -3,13 +3,13 @@ - + - + diff --git a/ILSpy.BamlDecompiler/Tests/TestRunner.cs b/ILSpy.BamlDecompiler/Tests/TestRunner.cs index b262b5322..4211d4102 100644 --- a/ILSpy.BamlDecompiler/Tests/TestRunner.cs +++ b/ILSpy.BamlDecompiler/Tests/TestRunner.cs @@ -31,7 +31,7 @@ namespace ILSpy.BamlDecompiler.Tests RunTest("cases/simpledictionary"); } - [Test, Ignore] + [Test] public void Resources() { RunTest("cases/resources"); From b515326b749294eb1a825ecef7b6d7b4fc89dbba Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Wed, 1 Jun 2011 09:08:36 +0200 Subject: [PATCH 03/21] reimplemented reading of keys --- .../ILSpy.BamlDecompiler.csproj | 1 + .../KeyMapping.cs | 35 ++++++ .../XmlBamlElement.cs | 105 ----------------- .../XmlBamlReader.cs | 109 +++++------------- 4 files changed, 64 insertions(+), 186 deletions(-) create mode 100644 ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/KeyMapping.cs diff --git a/ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj b/ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj index a681f07fd..adc87221c 100644 --- a/ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj +++ b/ILSpy.BamlDecompiler/ILSpy.BamlDecompiler.csproj @@ -88,6 +88,7 @@ + diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/KeyMapping.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/KeyMapping.cs new file mode 100644 index 000000000..968ce104e --- /dev/null +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/KeyMapping.cs @@ -0,0 +1,35 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team +// This code is distributed under the MS-PL (for details please see \doc\MS-PL.txt) + +using System; +using System.Collections.Generic; + +namespace Ricciolo.StylesExplorer.MarkupReflection +{ + public class KeyMapping + { + List staticResources; + + public List StaticResources { + get { return staticResources; } + } + + public bool HasStaticResources { + get { return staticResources != null && staticResources.Count > 0; } + } + + public string KeyString { get; set; } + public bool Shared { get; set; } + public bool SharedSet { get; set; } + + public KeyMapping() + { + this.staticResources = new List(); + } + + public KeyMapping(string key) + { + this.KeyString = key; + } + } +} diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs index c43e8a1ea..cc8d7f316 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs @@ -11,9 +11,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection { class XmlBamlElement : XmlBamlNode { - ArrayList _arguments = new ArrayList(); XmlNamespaceCollection _namespaces = new XmlNamespaceCollection(); - KeysResourcesCollection _keysResources = new KeysResourcesCollection(); public XmlBamlElement() { @@ -67,108 +65,5 @@ namespace Ricciolo.StylesExplorer.MarkupReflection } } - internal class KeyMapping - { - private string _key; - private TypeDeclaration _declaration; - private string _trueKey; - - public KeyMapping(string key, TypeDeclaration declaration, string trueKey) - { - _key = key; - _declaration = declaration; - _trueKey = trueKey; - } - - public string Key - { - get { return _key; } - } - - public TypeDeclaration Declaration - { - get { return _declaration; } - } - - public string TrueKey - { - get { return _trueKey; } - } - - public override string ToString() - { - return String.Format("{0} - {1} - {2}", Key, Declaration, TrueKey); - } - } - - internal class KeysResourcesCollection : List - { - public KeysResource Last - { - get - { - if (this.Count == 0) - return null; - return this[this.Count - 1]; - } - } - - public KeysResource First - { - get - { - if (this.Count == 0) - return null; - return this[0]; - } - } - } - - internal class KeysResource - { - private KeysTable _keys = new KeysTable(); - private ArrayList _staticResources = new ArrayList(); - - public KeysTable Keys - { - get { return _keys; } - } - - public ArrayList StaticResources - { - get { return _staticResources; } - } - } - - internal class KeysTable - { - private Hashtable table = new Hashtable(); - - public String this[long position] - { - get - { - return (string)this.table[position]; - } - set - { - this.table[position] = value; - } - } - public int Count - { - get { return this.table.Count; } - } - - public void Remove(long position) - { - this.table.Remove(position); - } - - public bool HasKey(long position) - { - return this.table.ContainsKey(position); - } - } } diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs index ff7202cf5..a92ed4806 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs @@ -18,15 +18,11 @@ namespace Ricciolo.StylesExplorer.MarkupReflection { public class XmlBamlReader : XmlReader, IXmlNamespaceResolver { - - #region Variables - BamlBinaryReader reader; Dictionary assemblyTable = new Dictionary(); Dictionary stringTable = new Dictionary(); Dictionary typeTable = new Dictionary(); Dictionary propertyTable = new Dictionary(); - List staticResources = new List(); readonly ITypeResolver _resolver; @@ -34,7 +30,6 @@ namespace Ricciolo.StylesExplorer.MarkupReflection Stack elements = new Stack(); Stack readingElements = new Stack(); - Stack keysResources = new Stack(); NodesCollection nodes = new NodesCollection(); List _mappings = new List(); XmlBamlNode _currentNode; @@ -50,7 +45,20 @@ namespace Ricciolo.StylesExplorer.MarkupReflection bool isPartialDefKeysClosed = true; bool isDefKeysClosed = true; + #region Context int currentKey; + List keys = new List(); + + KeyMapping LastKey { + get { + KeyMapping last = keys.LastOrDefault(); + if (last == null) + keys.Add(last = new KeyMapping()); + + return last; + } + } + #endregion int bytesToSkip; @@ -62,8 +70,6 @@ namespace Ricciolo.StylesExplorer.MarkupReflection public const string XWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml"; public const string DefaultWPFNamespace = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"; - #endregion - public XmlBamlReader(Stream stream) : this (stream, AppDomainTypeResolver.GetIntoNewAppDomain(Environment.CurrentDirectory)) { @@ -656,15 +662,10 @@ namespace Ricciolo.StylesExplorer.MarkupReflection XmlBamlPropertyElement property = new XmlBamlPropertyElement(element, PropertyType.Dictionary, pd); elements.Push(property); nodes.Enqueue(property); - - isDefKeysClosed = true; - isPartialDefKeysClosed = true; } void ReadPropertyDictionaryEnd() { - keysResources.Pop(); - CloseElement(); } @@ -899,12 +900,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection short typeIdentifier = reader.ReadInt16(); reader.ReadByte(); int position = reader.ReadInt32(); - // TODO: shared bool shared = reader.ReadBoolean(); bool sharedSet = reader.ReadBoolean(); - - // TODO: handle shared - AddDefKey(position, this.GetTypeExtension(typeIdentifier)); + + string extension = GetTypeExtension(typeIdentifier); + + keys.Add(new KeyMapping(extension) { Shared = shared, SharedSet = sharedSet }); } void ReadDefAttribute() @@ -926,7 +927,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection if (recordName != "Key") throw new NotSupportedException(recordName); pd = new PropertyDeclaration(recordName, XamlTypeDeclaration); - AddDefKey(-1, text); + keys.Add(new KeyMapping(text)); break; } @@ -946,30 +947,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection if (text == null) throw new NotSupportedException(); - AddDefKey(position, text); - } - - void AddDefKey(long position, string text) - { - // Guardo se la dichiarazione delle chiavi risulta chiusa - // Se è aperta c'è un sotto ResourceDictionary oppure è il root ResourceDictionary - if (isDefKeysClosed) { - currentKey = 0; - keysResources.Push(new KeysResourcesCollection()); - } - - // Guardo se è stata chiusa la dichiarazione parziale (mediante dichiarazione OptimizedStaticResource) - // Si chiude il ciclo di chiavi - if (isPartialDefKeysClosed) - { - keysResources.Peek().Add(new KeysResource()); - } - isDefKeysClosed = false; - isPartialDefKeysClosed = false; - - // TODO: handle shared - if (position >= 0) - keysResources.Peek().Last.Keys[position] = text; + keys.Add(new KeyMapping(text)); } void ReadXmlnsProperty() @@ -1007,13 +985,6 @@ namespace Ricciolo.StylesExplorer.MarkupReflection void ReadElementEnd() { CloseElement(); - - // Provvedo all'eliminazione del gruppo di chiavi se sono sul root ResourceDictionary - // e si è chiuso uno degli elementi di primo livello e tutte le chiavi sono state usate - // Passo alla prossima lista - KeysResource keysResource = (elements.Count == 1 && keysResources.Count > 0) ? keysResources.Peek().First : null; - if (keysResource != null && keysResource.Keys.Count == 0) - keysResources.Peek().RemoveAt(0); } void ReadPropertyComplexStart() @@ -1201,7 +1172,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection declaration = GetKnownTypeDeclarationByName(declaration.Type.BaseType.AssemblyQualifiedName); } element.TypeDeclaration = declaration; - element.IsImplicit = (flags & 2) == 2; + element.IsImplicit = (flags & 2) == 2; elements.Push(element); if (!element.IsImplicit) nodes.Enqueue(element); @@ -1210,16 +1181,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(new XmlBamlSimpleProperty(XWPFNamespace, "Class", string.Format("{0}.{1}", oldDeclaration.Namespace, oldDeclaration.Name))); } - if (parentElement != null && complexPropertyOpened == 0) - { - // Calcolo la posizione dell'elemento rispetto al padre - KeysResource keysResource = (keysResources.Count > 0) ? keysResources.Peek().First : null; - if (keysResource != null && keysResource.Keys.HasKey(currentKey)) - { - string key = keysResource.Keys[currentKey]; - // Rimuovo la chiave perché è stata usata -// keysResource.Keys.Remove(currentKey); - + if (parentElement != null && complexPropertyOpened == 0) { + if (keys != null && keys.Count > currentKey) { + string key = keys[currentKey].KeyString; AddKeyToElement(key); currentKey++; } @@ -1332,7 +1296,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection StringBuilder sb = new StringBuilder(); FormatElementExtension((XmlBamlElement)nodes[start], sb); - AddDefKey(propertyElement.Position, sb.ToString()); + keys.Add(new KeyMapping(sb.ToString())); } } @@ -1341,8 +1305,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection short identifier = reader.ReadInt16(); byte flags = reader.ReadByte(); TypeDeclaration declaration = GetTypeDeclaration(identifier); - staticResources.Add(declaration); - + LastKey.StaticResources.Add(declaration); XmlBamlElement element; if (elements.Any()) element = new XmlBamlElement(elements.Peek()); @@ -1412,7 +1375,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection //this.staticResourceTable.Add(resource); isPartialDefKeysClosed = true; // Aggiungo la risorsa nell'ultimo gruppo - keysResources.Peek().Last.StaticResources.Add(resource); + LastKey.StaticResources.Add(resource); } string GetTemplateBindingExtension(PropertyDeclaration propertyDeclaration) @@ -1513,24 +1476,8 @@ namespace Ricciolo.StylesExplorer.MarkupReflection object GetStaticResource(short identifier) { - // Recupero la risorsa nel gruppo corrente - foreach (KeysResourcesCollection resource in keysResources) - { - // TODO: controllare. Se non lo trova nel gruppo corrente, va in quello successivo - for (int x = 0; x < resource.Count; x++) - { - KeysResource resourceGroup = resource[x]; - if (resourceGroup.StaticResources.Count > identifier) - if (x > 0) - break; - //return "%" + resourceGroup.StaticResources[identifier] + "%"; - else - return resourceGroup.StaticResources[identifier]; - } - } - - if (identifier < staticResources.Count) - return staticResources[identifier]; + if (identifier < LastKey.StaticResources.Count) + return LastKey.StaticResources[(int)identifier]; return "???" + identifier +"???"; // throw new ArgumentException("Cannot find StaticResource", "identifier"); From 5ef8af1346af0264943b1b82790e5a72d4ceb7e1 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Wed, 1 Jun 2011 14:09:25 +0200 Subject: [PATCH 04/21] add more unit tests; use current type instead of declaring type for PropertyElements --- .../XmlBamlElement.cs | 5 +- .../XmlBamlPropertyElement.cs | 3 +- .../XmlBamlReader.cs | 9 ++- .../Tests/Cases/AvalonDockBrushes.xaml | 77 +++++++++++++++++++ .../Tests/Cases/Resources.xaml | 8 +- .../Tests/ILSpy.BamlDecompiler.Tests.csproj | 3 + .../Tests/Mocks/AvalonDock.cs | 46 +++++++++++ ILSpy.BamlDecompiler/Tests/TestRunner.cs | 8 +- 8 files changed, 149 insertions(+), 10 deletions(-) create mode 100644 ILSpy.BamlDecompiler/Tests/Cases/AvalonDockBrushes.xaml create mode 100644 ILSpy.BamlDecompiler/Tests/Mocks/AvalonDock.cs diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs index cc8d7f316..25d8f466f 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlElement.cs @@ -19,6 +19,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection public XmlBamlElement(XmlBamlElement parent) { + this.Parent = parent; this.Namespaces.AddRange(parent.Namespaces); } @@ -26,7 +27,9 @@ namespace Ricciolo.StylesExplorer.MarkupReflection { get { return _namespaces; } } - + + public XmlBamlElement Parent { get; private set; } + public TypeDeclaration TypeDeclaration { get; set; } public override XmlNodeType NodeType { diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlPropertyElement.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlPropertyElement.cs index fbb312cf0..a6ce053d9 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlPropertyElement.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlPropertyElement.cs @@ -11,8 +11,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection { private readonly PropertyType _propertyType; private PropertyDeclaration propertyDeclaration; - - + public XmlBamlPropertyElement(PropertyType propertyType, PropertyDeclaration propertyDeclaration) { _propertyType = propertyType; diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs index a92ed4806..48f2f99fc 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs @@ -604,7 +604,12 @@ namespace Ricciolo.StylesExplorer.MarkupReflection else if (node is XmlBamlPropertyElement) { XmlBamlPropertyElement property = (XmlBamlPropertyElement)node; - localName = String.Format("{0}.{1}", property.TypeDeclaration.Name, property.PropertyDeclaration.Name); + string typeName = property.TypeDeclaration.Name; + + if (property.Parent.TypeDeclaration.Type.IsSubclassOf(property.PropertyDeclaration.DeclaringType.Type)) + typeName = property.Parent.TypeDeclaration.Name; + + localName = String.Format("{0}.{1}", typeName, property.PropertyDeclaration.Name); } else if (node is XmlBamlElement) localName = ((XmlBamlElement)node).TypeDeclaration.Name; @@ -1117,7 +1122,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection sb.Append(sep); sb.Append(((XmlBamlText)node).Text); } - sep = ","; + sep = ", "; } } diff --git a/ILSpy.BamlDecompiler/Tests/Cases/AvalonDockBrushes.xaml b/ILSpy.BamlDecompiler/Tests/Cases/AvalonDockBrushes.xaml new file mode 100644 index 000000000..5760368b2 --- /dev/null +++ b/ILSpy.BamlDecompiler/Tests/Cases/AvalonDockBrushes.xaml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml b/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml index 04fc3f921..7e1a0514a 100644 --- a/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml +++ b/ILSpy.BamlDecompiler/Tests/Cases/Resources.xaml @@ -1,16 +1,16 @@  - + - + - + - + \ No newline at end of file diff --git a/ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj b/ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj index 4c65ef0e0..b13ef3e7e 100644 --- a/ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj +++ b/ILSpy.BamlDecompiler/Tests/ILSpy.BamlDecompiler.Tests.csproj @@ -74,6 +74,7 @@ Simple.xaml Code + @@ -101,9 +102,11 @@ + + Always diff --git a/ILSpy.BamlDecompiler/Tests/Mocks/AvalonDock.cs b/ILSpy.BamlDecompiler/Tests/Mocks/AvalonDock.cs new file mode 100644 index 000000000..8b177bedd --- /dev/null +++ b/ILSpy.BamlDecompiler/Tests/Mocks/AvalonDock.cs @@ -0,0 +1,46 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) + +using System; +using System.Windows.Media; + +namespace AvalonDock +{ + public class DockingManager + { + public DockingManager() + { + } + } + + public enum AvalonDockBrushes + { + DefaultBackgroundBrush, + DockablePaneTitleBackground, + DockablePaneTitleBackgroundSelected, + DockablePaneTitleForeground, + DockablePaneTitleForegroundSelected, + PaneHeaderCommandBackground, + PaneHeaderCommandBorderBrush, + DocumentHeaderBackground, + DocumentHeaderForeground, + DocumentHeaderForegroundSelected, + DocumentHeaderForegroundSelectedActivated, + DocumentHeaderBackgroundSelected, + DocumentHeaderBackgroundSelectedActivated, + DocumentHeaderBackgroundMouseOver, + DocumentHeaderBorderBrushMouseOver, + DocumentHeaderBorder, + DocumentHeaderBorderSelected, + DocumentHeaderBorderSelectedActivated, + NavigatorWindowTopBackground, + NavigatorWindowTitleForeground, + NavigatorWindowDocumentTypeForeground, + NavigatorWindowInfoTipForeground, + NavigatorWindowForeground, + NavigatorWindowBackground, + NavigatorWindowSelectionBackground, + NavigatorWindowSelectionBorderbrush, + NavigatorWindowBottomBackground + } +} diff --git a/ILSpy.BamlDecompiler/Tests/TestRunner.cs b/ILSpy.BamlDecompiler/Tests/TestRunner.cs index 4211d4102..ddd4014f3 100644 --- a/ILSpy.BamlDecompiler/Tests/TestRunner.cs +++ b/ILSpy.BamlDecompiler/Tests/TestRunner.cs @@ -37,6 +37,12 @@ namespace ILSpy.BamlDecompiler.Tests RunTest("cases/resources"); } + [Test] + public void AvalonDockBrushes() + { + RunTest("cases/avalondockbrushes"); + } + #region RunTest void RunTest(string name) { @@ -48,7 +54,7 @@ namespace ILSpy.BamlDecompiler.Tests XDocument document = BamlResourceEntryNode.LoadIntoDocument(new DefaultAssemblyResolver(), assembly, bamlStream); string path = Path.Combine("..\\..\\Tests", name + ".xaml"); - CodeAssert.AreEqual(document.ToString(), File.ReadAllText(path)); + CodeAssert.AreEqual(File.ReadAllText(path), document.ToString()); } Stream LoadBaml(Resource res, string name) From 604d43076e95c567008c70526a3eb36181891045 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Thu, 2 Jun 2011 18:20:42 +0200 Subject: [PATCH 05/21] implement correct handling of Keys; add more unit tests --- .../KeyMapping.cs | 5 ++ .../XmlBamlReader.cs | 66 ++++++++++++++--- .../Tests/Cases/AvalonDockBrushes.xaml | 72 +++++++------------ .../Tests/Cases/AvalonDockCommon.xaml | 58 +++++++++++++++ .../Tests/Cases/SimpleNames.xaml | 6 ++ .../Tests/Cases/SimpleNames.xaml.cs | 26 +++++++ .../Tests/ILSpy.BamlDecompiler.Tests.csproj | 6 ++ .../Tests/Mocks/AvalonDock.cs | 26 ++++++- ILSpy.BamlDecompiler/Tests/TestRunner.cs | 12 ++++ 9 files changed, 219 insertions(+), 58 deletions(-) create mode 100644 ILSpy.BamlDecompiler/Tests/Cases/AvalonDockCommon.xaml create mode 100644 ILSpy.BamlDecompiler/Tests/Cases/SimpleNames.xaml create mode 100644 ILSpy.BamlDecompiler/Tests/Cases/SimpleNames.xaml.cs diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/KeyMapping.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/KeyMapping.cs index 968ce104e..0b20620fc 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/KeyMapping.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/KeyMapping.cs @@ -22,14 +22,19 @@ namespace Ricciolo.StylesExplorer.MarkupReflection public bool Shared { get; set; } public bool SharedSet { get; set; } + public int Position { get; set; } + public KeyMapping() { this.staticResources = new List(); + this.Position = -1; } public KeyMapping(string key) { this.KeyString = key; + this.staticResources = new List(); + this.Position = -1; } } } diff --git a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs index 48f2f99fc..ae6f20087 100644 --- a/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs +++ b/ILSpy.BamlDecompiler/Ricciolo.StylesExplorer.MarkupReflection/XmlBamlReader.cs @@ -46,6 +46,35 @@ namespace Ricciolo.StylesExplorer.MarkupReflection bool isDefKeysClosed = true; #region Context + Stack layer = new Stack(); + + class ReaderContext + { + public bool IsDeferred { get; set; } + public bool IsInStaticResource { get; set; } + + public ReaderContext Previous { get; private set; } + + public ReaderContext() + { + this.Previous = this; + } + + public ReaderContext(ReaderContext previous) + { + this.Previous = previous; + } + } + + ReaderContext Current { + get { + if (!layer.Any()) + layer.Push(new ReaderContext()); + + return layer.Peek(); + } + } + int currentKey; List keys = new List(); @@ -58,6 +87,19 @@ namespace Ricciolo.StylesExplorer.MarkupReflection return last; } } + + void LayerPop() + { + layer.Pop(); + } + + void LayerPush() + { + if (layer.Any()) + layer.Push(new ReaderContext(layer.Peek())); + else + layer.Push(new ReaderContext()); + } #endregion int bytesToSkip; @@ -359,6 +401,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection reader.ReadInt32(); break; case BamlRecordType.DeferableContentStart: + Current.IsDeferred = true; reader.ReadInt32(); break; case BamlRecordType.DefAttribute: @@ -910,7 +953,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection string extension = GetTypeExtension(typeIdentifier); - keys.Add(new KeyMapping(extension) { Shared = shared, SharedSet = sharedSet }); + keys.Add(new KeyMapping(extension) { Shared = shared, SharedSet = sharedSet, Position = position }); } void ReadDefAttribute() @@ -932,7 +975,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection if (recordName != "Key") throw new NotSupportedException(recordName); pd = new PropertyDeclaration(recordName, XamlTypeDeclaration); - keys.Add(new KeyMapping(text)); + keys.Add(new KeyMapping(text) { Position = -1 }); break; } @@ -944,15 +987,16 @@ namespace Ricciolo.StylesExplorer.MarkupReflection void ReadDefAttributeKeyString() { - short num = reader.ReadInt16(); + short stringId = reader.ReadInt16(); int position = reader.ReadInt32(); - //bool shared = reader.ReadBoolean(); - //bool sharedSet = reader.ReadBoolean(); - string text = this.stringTable[num]; + bool shared = reader.ReadBoolean(); + bool sharedSet = reader.ReadBoolean(); + + string text = this.stringTable[stringId]; if (text == null) throw new NotSupportedException(); - keys.Add(new KeyMapping(text)); + keys.Add(new KeyMapping(text) { Position = position }); } void ReadXmlnsProperty() @@ -990,6 +1034,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection void ReadElementEnd() { CloseElement(); + LayerPop(); } void ReadPropertyComplexStart() @@ -1146,6 +1191,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection void ReadElementStart() { + LayerPush(); short identifier = reader.ReadInt16(); sbyte flags = reader.ReadSByte(); if (flags < 0 || flags > 3) @@ -1186,7 +1232,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection nodes.Enqueue(new XmlBamlSimpleProperty(XWPFNamespace, "Class", string.Format("{0}.{1}", oldDeclaration.Namespace, oldDeclaration.Name))); } - if (parentElement != null && complexPropertyOpened == 0) { + if (parentElement != null && complexPropertyOpened == 0 && !Current.IsInStaticResource && Current.Previous.IsDeferred) { if (keys != null && keys.Count > currentKey) { string key = keys[currentKey].KeyString; AddKeyToElement(key); @@ -1301,12 +1347,13 @@ namespace Ricciolo.StylesExplorer.MarkupReflection StringBuilder sb = new StringBuilder(); FormatElementExtension((XmlBamlElement)nodes[start], sb); - keys.Add(new KeyMapping(sb.ToString())); + keys.Add(new KeyMapping(sb.ToString()) { Position = -1 }); } } void ReadStaticResourceStart() { + Current.IsInStaticResource = true; short identifier = reader.ReadInt16(); byte flags = reader.ReadByte(); TypeDeclaration declaration = GetTypeDeclaration(identifier); @@ -1324,6 +1371,7 @@ namespace Ricciolo.StylesExplorer.MarkupReflection void ReadStaticResourceEnd() { CloseElement(); + Current.IsInStaticResource = false; } void ReadStaticResourceId() diff --git a/ILSpy.BamlDecompiler/Tests/Cases/AvalonDockBrushes.xaml b/ILSpy.BamlDecompiler/Tests/Cases/AvalonDockBrushes.xaml index 5760368b2..cc7e517c9 100644 --- a/ILSpy.BamlDecompiler/Tests/Cases/AvalonDockBrushes.xaml +++ b/ILSpy.BamlDecompiler/Tests/Cases/AvalonDockBrushes.xaml @@ -3,75 +3,51 @@ - - + + - - + + - - + + - - + + - - + + - - - - - + + + + + - - + + - - - - - + + + + + - + - + \ No newline at end of file diff --git a/ILSpy.BamlDecompiler/Tests/Cases/AvalonDockCommon.xaml b/ILSpy.BamlDecompiler/Tests/Cases/AvalonDockCommon.xaml new file mode 100644 index 000000000..265f0b28f --- /dev/null +++ b/ILSpy.BamlDecompiler/Tests/Cases/AvalonDockCommon.xaml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ILSpy.BamlDecompiler/Tests/Cases/SimpleNames.xaml b/ILSpy.BamlDecompiler/Tests/Cases/SimpleNames.xaml new file mode 100644 index 000000000..91a908fdf --- /dev/null +++ b/ILSpy.BamlDecompiler/Tests/Cases/SimpleNames.xaml @@ -0,0 +1,6 @@ + + +