Author: miguel
Date: 2008-02-03 23:33:28 -0500 (Sun, 03 Feb 2008)
New Revision: 94726

Modified:
   trunk/olive/class/agclr/Mono/ChangeLog
   trunk/olive/class/agclr/Mono/ManagedXamlLoader.cs
Log:
2008-02-04  Miguel de Icaza  <[EMAIL PROTECTED]>

        Fixes for Phalanger.NET on Silverlight:
        
        http://tomasp.net/files/copter/default.html
        
        * ManagedXamlLoader.cs (assembly_to_source): Add new hashtable to
        track the mapping of loaded assemblies to the location on the file
        system.

        (AssemblyToSource): New helper method that is used to track all
        assemblies loaded and their mapping to the cache in the
        filesystem. 

        (Setup): Setup an Assembly resolver event for the appdomain.

        (AssemblyResolve): Used to resolve assemblies that are loaded
        implicitly from a Silverlight application.   If user code calls
        Assembly.Load ("file.dll") it expects the file.dll to be loaded
        from the same directory from which the originating assembly
        called. 

        If that is ClientBin/Dingus.dll calling Load ("Blah.dll") we need
        to request ClientBin/Blah.dll.

        We walk the stack to find the calling assembly and we map that to
        the actual directory where the data was loaded from.

        (LoadAssembly): Refactored to allow the synchronous parameter to
        be used (independently of the global property). 

        Everywhere: when an assembly is successfully loaded, we add it to
        the AssemblyToSource mapping. 



Modified: trunk/olive/class/agclr/Mono/ChangeLog
===================================================================
--- trunk/olive/class/agclr/Mono/ChangeLog      2008-02-03 22:46:22 UTC (rev 
94725)
+++ trunk/olive/class/agclr/Mono/ChangeLog      2008-02-04 04:33:28 UTC (rev 
94726)
@@ -1,3 +1,37 @@
+2008-02-04  Miguel de Icaza  <[EMAIL PROTECTED]>
+
+       Fixes for Phalanger.NET on Silverlight:
+       
+       http://tomasp.net/files/copter/default.html
+       
+       * ManagedXamlLoader.cs (assembly_to_source): Add new hashtable to
+       track the mapping of loaded assemblies to the location on the file
+       system.
+
+       (AssemblyToSource): New helper method that is used to track all
+       assemblies loaded and their mapping to the cache in the
+       filesystem. 
+
+       (Setup): Setup an Assembly resolver event for the appdomain.
+
+       (AssemblyResolve): Used to resolve assemblies that are loaded
+       implicitly from a Silverlight application.   If user code calls
+       Assembly.Load ("file.dll") it expects the file.dll to be loaded
+       from the same directory from which the originating assembly
+       called. 
+
+       If that is ClientBin/Dingus.dll calling Load ("Blah.dll") we need
+       to request ClientBin/Blah.dll.
+
+       We walk the stack to find the calling assembly and we map that to
+       the actual directory where the data was loaded from.
+
+       (LoadAssembly): Refactored to allow the synchronous parameter to
+       be used (independently of the global property). 
+
+       Everywhere: when an assembly is successfully loaded, we add it to
+       the AssemblyToSource mapping. 
+
 2008-01-09  Chris Toshok  <[EMAIL PROTECTED]>
 
        * Events.cs: no longer marshal the UnmanagedMouseEventArgs -

Modified: trunk/olive/class/agclr/Mono/ManagedXamlLoader.cs
===================================================================
--- trunk/olive/class/agclr/Mono/ManagedXamlLoader.cs   2008-02-03 22:46:22 UTC 
(rev 94725)
+++ trunk/olive/class/agclr/Mono/ManagedXamlLoader.cs   2008-02-04 04:33:28 UTC 
(rev 94726)
@@ -28,6 +28,7 @@
 //
 
 using System;
+using System.Diagnostics;
 using System.Reflection;
 using System.Collections;
 using System.Collections.Generic;
@@ -47,7 +48,18 @@
                IntPtr native_loader;
 //             string filename;
 //             string contents;
+
+               //
+               // Maps the assembly path to the location where the browser 
stored
+               // the downloaded file on its cache (ClientBin/Dingus.dll -> 
/home/xxx/.mozilla/cache/...)
+               // 
                static Dictionary<string, string> mappings = new Dictionary 
<string, string> ();
+
+               //
+               // Maps a loaded assembly to the originating path
+               //
+               internal static Dictionary<Assembly, string> assembly_to_source 
= new Dictionary<Assembly, string>();
+               
                XamlLoaderCallbacks callbacks;
                bool load_deps_synch = false;
 
@@ -107,6 +119,10 @@
 //                     this.filename = filename;
 //                     this.contents = contents;
 
+                       //
+                       // Registers callbacks that are invoked from the
+                       // unmanaged code. 
+                       //
                        callbacks.load_managed_object = new LoadObjectCallback 
(cb_load_object);
                        callbacks.set_custom_attribute = new 
SetAttributeCallback (cb_set_custom_attribute);
                        callbacks.hookup_event = new HookupEventCallback 
(cb_hookup_event);
@@ -124,16 +140,93 @@
 
                        PluginInDomain = plugin;
                        SurfaceInDomain = surface;
+
+                       //
+                       // Sets default handler for loading assemblies, this 
allows
+                       // code to call Assemly.Load ("relative") and trigger a
+                       // download (Phalanger.NET first exposed this). 
+                       //
+
+                       AppDomain.CurrentDomain.AssemblyResolve += 
AssemblyResolver;
+               }
+
+               //
+               // Our assembly resolver, invoked by Assembly.Load if it fails
+               // to find an assembly locally.    This happens if the managed 
code
+               // tries to call Assembly.Load ("file.dll") as it expects the
+               // runtime to download the file.dll that is relative to the 
assembly
+               // that invoked Assembly.Load
+               //
+               internal Assembly AssemblyResolver (object sender, 
ResolveEventArgs args)
+               {
+                       AssemblyName an = new AssemblyName (args.Name);
+                       Assembly result = null;
+
+                       //
+                       // Find the caller that called Load, to use that as the 
base for 
+                       // lookig up assemblies
+                       //
+
+                       // Skip agclr (this frame)
+                       StackFrame [] frames = new StackTrace (1).GetFrames ();
+                       Assembly mscorlib = typeof (object).Assembly;
+                       Assembly caller = null;
+                       foreach (StackFrame f in frames){
+                               MethodBase m = f.GetMethod ();
+                               Assembly mass = m.DeclaringType.Assembly;
+                               if (mass != mscorlib){
+                                       caller = m.DeclaringType.Assembly;
+                                       break;
+                               }
+                       }
+
+                       if (caller == null){
+                               Console.WriteLine ("AssemblyResolver: failed to 
find caller assembly to mscorlib");
+                               return null;
+                       }
+
+                       // We always do sync loads from here
+                       string path = null;
+                       foreach (KeyValuePair<Assembly,string> pair in 
assembly_to_source){
+                               if (pair.Key != caller)
+                                       continue;
+                               
+                               // Found it, lookup the base dir
+                               foreach (KeyValuePair<string,string> ab in 
mappings){
+                                       if (ab.Value == pair.Value){
+                                               string apath = ab.Key;
+                                               if (apath.IndexOf ('/') == -1)
+                                                       path = an.Name;
+                                               else {
+                                                       path = apath.Substring 
(0, apath.LastIndexOf ('/')) + "/" + an.Name + ".dll";
+                                               }
+                                       }
+                               }
+                       }
+                       if (path == null){
+                               Console.WriteLine ("AssemblyResolver: Never 
found the calling assembly and its path");
+                               return null;
+                       }
+
+                       AssemblyLoadResult r = LoadAssemblyPlugin (path, path, 
true, ref result);
+                       return result;
+               }
                        
+               public static void AssemblyToSource (Assembly k, string path)
+               {
+                       if (assembly_to_source.ContainsKey (k))
+                               return;
+                       
+                       assembly_to_source.Add (k, path);
                }
-
+               
                public string GetMapping (string key)
                {
                        if (!mappings.ContainsKey (key))
                                return null;
                        return mappings [key];
                }
-               
+
                public void InsertMapping (string key, string name)
                {
                        //Console.WriteLine ("ManagedXamlLoader::InsertMapping 
({0}, {1}).", key, name);
@@ -158,7 +251,7 @@
                public AssemblyLoadResult LoadAssembly (string asm_path, string 
asm_name, out Assembly clientlib)
                {
                        //Console.WriteLine ("ManagedXamlLoader::LoadAssembly 
(asm_path={0} asm_name={1})", asm_path, asm_name);
-                       
+
                        clientlib = null;
 
                        //
@@ -169,33 +262,43 @@
                        //
                        try {
                                clientlib = Moonlight.LoadFile (asm_path);
-                               if (clientlib != null)
+                               if (clientlib != null){
+                                       AssemblyToSource (clientlib, asm_path);
                                        return AssemblyLoadResult.Success;
-                               
+                               }
                        } catch (System.IO.FileNotFoundException) {
                                //Console.WriteLine 
("ManagedXamlLoader::LoadAssembly (asm_path={0} asm_name={1}): client library 
not found.", asm_path, asm_name);
                                RequestFile (asm_path);
                                return AssemblyLoadResult.MissingAssembly;
                        }
 
-                       if (load_deps_synch) {
-                               //
-                               // FIXME: Move this support into the 
DependencyLoader
-                               //
-                               Console.WriteLine ("WARNING: ManagedXamlLoader 
Sync Assembly Loader has not been ported to use DependencyLoader");
-                               byte[] arr = LoadDependency (asm_path);
-                               if (arr != null)
-                                       clientlib = Assembly.Load (arr);
-                               if (clientlib == null) {
-                                       //Console.WriteLine 
("ManagedXamlLoader::LoadAssembly (asm_path={0} asm_name={1}): could not load 
client library: {2}", asm_path, asm_name, asm_path);
-                                       return AssemblyLoadResult.LoadFailure;
-                               }
-                               return AssemblyLoadResult.Success;
-                       } else {
+                       return LoadAssemblyPlugin (asm_path, asm_name, 
load_deps_synch, ref clientlib);
+               }
+
+               //
+               // Tries to load an assembly, this is called only from the 
plugin
+               // 
+               AssemblyLoadResult LoadAssemblyPlugin (string asm_path, string 
asm_name, bool synchronous, ref Assembly clientlib)
+               {
+                       if (!synchronous) {
                                DependencyLoader dl = new DependencyLoader 
(this, asm_path);
                                
                                return dl.Load (ref clientlib);
                        }
+                       
+                       //
+                       // FIXME: Move this support into the DependencyLoader
+                       //
+                       Console.WriteLine ("WARNING: ManagedXamlLoader Sync 
Assembly Loader has not been ported to use DependencyLoader");
+                       byte[] arr = LoadDependency (asm_path);
+                       if (arr != null)
+                               clientlib = Assembly.Load (arr);
+                       if (clientlib == null) {
+                               //Console.WriteLine 
("ManagedXamlLoader::LoadAssembly (asm_path={0} asm_name={1}): could not load 
client library: {2}", asm_path, asm_name, asm_path);
+                               return AssemblyLoadResult.LoadFailure;
+                       }
+                       AssemblyToSource (clientlib, asm_path);
+                       return AssemblyLoadResult.Success;
                }
                
                private IntPtr LoadObject (string asm_name, string asm_path, 
string ns, string type_name)
@@ -596,6 +699,7 @@
                                Console.WriteLine ("DependencyLoader: LoadMain 
(\"{0}\") failed to load client library", asm_path);
                                return AssemblyLoadResult.LoadFailure;
                        }
+                       ManagedXamlLoader.AssemblyToSource (main, mapped);
                        return AssemblyLoadResult.Success;
                }
                
@@ -629,6 +733,7 @@
 
                                // enter it into the loaded deps
                                deps [key] = a;
+                               ManagedXamlLoader.AssemblyToSource (a, file);
                                missing--;
 
                                return a;

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to