Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.ComponentModel.Composition / src / Composition.Initialization / System / ComponentModel / Composition / Hosting / Package.cs
1 // -----------------------------------------------------------------------\r
2 // Copyright (c) Microsoft Corporation.  All rights reserved.\r
3 // -----------------------------------------------------------------------\r
4 #if(SILVERLIGHT)\r
5 using System;\r
6 using System.Collections.Generic;\r
7 using System.IO;\r
8 using System.Net;\r
9 using System.Reflection;\r
10 using System.Windows;\r
11 using System.Windows.Resources;\r
12 using System.Xml;\r
13 using System.ComponentModel;\r
14 \r
15 namespace System.ComponentModel.Composition.Hosting\r
16 {\r
17     /// <summary>\r
18     ///     Helper functions for accessing the Silverlight manifest\r
19     /// </summary>\r
20     internal static class Package\r
21     {\r
22         /// <summary>\r
23         ///     Retrieves The current list of assemblies for the application XAP load. Depends on the Deployment.Current property being setup and\r
24         ///     so can only be accessed after the Application object has be completely constructed.\r
25         ///     No caching occurs at this level.\r
26         /// </summary>\r
27         public static IEnumerable<Assembly> CurrentAssemblies\r
28         {\r
29             get\r
30             {\r
31                 var assemblies = new List<Assembly>();\r
32 \r
33                 // While this may seem like somewhat of a hack, walking the AssemblyParts in the active \r
34                 // deployment object is the only way to get the list of assemblies loaded by the initial XAP. \r
35                 foreach (AssemblyPart ap in Deployment.Current.Parts)\r
36                 {\r
37                     StreamResourceInfo sri = Application.GetResourceStream(new Uri(ap.Source, UriKind.Relative));\r
38                     if (sri != null)\r
39                     {\r
40                         // Keep in mind that calling Load on an assembly that is already loaded will\r
41                         // be a no-op and simply return the already loaded assembly object.\r
42                         Assembly assembly = ap.Load(sri.Stream);\r
43                         assemblies.Add(assembly);\r
44                     }\r
45                 }\r
46 \r
47                 return assemblies;\r
48             }\r
49         }\r
50 \r
51 \r
52         public static IEnumerable<Assembly> LoadPackagedAssemblies(Stream packageStream)\r
53         {\r
54             List<Assembly> assemblies = new List<Assembly>();\r
55             StreamResourceInfo packageStreamInfo = new StreamResourceInfo(packageStream, null);\r
56 \r
57             IEnumerable<AssemblyPart> parts = GetDeploymentParts(packageStreamInfo);\r
58 \r
59             foreach (AssemblyPart ap in parts)\r
60             {\r
61                 StreamResourceInfo sri = Application.GetResourceStream(\r
62                     packageStreamInfo, new Uri(ap.Source, UriKind.Relative));\r
63 \r
64                 assemblies.Add(ap.Load(sri.Stream));\r
65             }\r
66             packageStream.Close();\r
67             return assemblies;\r
68         }\r
69 \r
70         /// <summary>\r
71         ///     Only reads AssemblyParts and does not support external parts (aka Platform Extensions or TPEs).\r
72         /// </summary>\r
73         private static IEnumerable<AssemblyPart> GetDeploymentParts(StreamResourceInfo xapStreamInfo)\r
74         {\r
75             Uri manifestUri = new Uri("AppManifest.xaml", UriKind.Relative);\r
76             StreamResourceInfo manifestStreamInfo = Application.GetResourceStream(xapStreamInfo, manifestUri);\r
77             List<AssemblyPart> assemblyParts = new List<AssemblyPart>();\r
78 \r
79             // The code assumes the following format in AppManifest.xaml\r
80             //<Deployment ... >\r
81             //  <Deployment.Parts>\r
82             //    <AssemblyPart x:Name="A" Source="A.dll" />\r
83             //    <AssemblyPart x:Name="B" Source="B.dll" />\r
84             //      ...\r
85             //    <AssemblyPart x:Name="Z" Source="Z.dll" />\r
86             //  </Deployment.Parts>\r
87             //</Deployment>\r
88             if (manifestStreamInfo != null)\r
89             {\r
90                 Stream manifestStream = manifestStreamInfo.Stream;\r
91                 using (XmlReader reader = XmlReader.Create(manifestStream))\r
92                 {\r
93                     if (reader.ReadToFollowing("AssemblyPart"))\r
94                     {\r
95                         do\r
96                         {\r
97                             string source = reader.GetAttribute("Source");\r
98 \r
99                             if (source != null)\r
100                             {\r
101                                 assemblyParts.Add(new AssemblyPart() { Source = source });\r
102                             }\r
103                         }\r
104                         while (reader.ReadToNextSibling("AssemblyPart"));\r
105                     }\r
106                 }\r
107             }\r
108 \r
109             return assemblyParts;\r
110         }\r
111     }\r
112 }\r
113 #endif\r