jschementi / agdlr

Dynamic languages in Silverlight

834f3dfb » Jimmy Schementi 2009-03-17 Refactor extension-specific... 1 using System;
2 using System.Net;
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.Windows.Documents;
6 using System.Windows.Ink;
7 using System.Windows.Input;
8 using System.Windows.Media;
9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11 using System.Windows.Resources;
12 using System.Collections.Generic;
13
14 namespace Microsoft.Scripting.Silverlight {
15
16 public static class Extension {
17
18 private static Action _downloadsCompleted;
19
20 public static void FetchCompleted(List<string> assemblyNames, object sender, OpenReadCompletedEventArgs e) {
21 var sri = new StreamResourceInfo(e.Result, null);
22 foreach (var assembly in assemblyNames) {
23 var dll = Application.GetResourceStream(sri,
24 new Uri(string.Format("{0}.dll", assembly), UriKind.Relative));
25 if (dll != null) {
26 Package.LanguageAssemblies.Add(dll);
27 }
28 }
29 }
30
31 public static void FetchDLR(Action downloadsCompleted) {
32 _downloadsCompleted = downloadsCompleted;
33 WebClient wc = new WebClient();
34 string sRequest = Package.LanguageExtensionUris[0]; // DLR
35 wc.OpenReadCompleted += new OpenReadCompletedEventHandler(FetchDLRCompleted);
36 wc.OpenReadAsync(new Uri(sRequest, UriKind.Absolute));
37 }
38
39 public static void FetchDLRCompleted(object sender, OpenReadCompletedEventArgs e) {
40 FetchCompleted(Package.DLRAssemblyNames, sender, e);
41 FetchRuby();
42 }
43
44 public static void FetchRuby() {
45 WebClient wc = new WebClient();
46 var index = 1; // Ruby
47 var uri = Package.LanguageExtensionUris[index];
48 wc.OpenReadCompleted += new OpenReadCompletedEventHandler(FetchRubyCompleted);
49 wc.OpenReadAsync(new Uri(uri));
50 }
51
52 public static void FetchRubyCompleted(object sender, OpenReadCompletedEventArgs e) {
53 FetchCompleted(Package.LanguageAssemblyNames, sender, e);
54 FetchPythonExtensions();
55 }
56
57 public static void FetchPythonExtensions() {
58 WebClient wc = new WebClient();
59 var index = 2; // Python
60 var uri = Package.LanguageExtensionUris[index];
61 wc.OpenReadCompleted += new OpenReadCompletedEventHandler(FetchPythonExtensionsCompleted);
62 wc.OpenReadAsync(new Uri(uri));
63 }
64
65 public static void FetchPythonExtensionsCompleted(object sender, OpenReadCompletedEventArgs e) {
66 FetchCompleted(Package.LanguageAssemblyNames, sender, e);
67 _downloadsCompleted.Invoke();
68 _downloadsCompleted = null;
69 }
70 }
71 }