]> O.S.I.I.S - jp/crow.git/commitdiff
debug template loading
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 18 Dec 2018 06:31:53 +0000 (07:31 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 18 Dec 2018 06:31:53 +0000 (07:31 +0100)
Crow/src/CompilerServices/CompilerServices.cs
Crow/src/GraphicObjects/TemplatedControl.cs
Crow/src/Instantiator.cs
Crow/src/Interface.cs
Crow/src/ItemTemplate.cs

index 80e47e9d77c8c3a4caf5479809ead156869c3ae8..7b95c9d7bb5dbd34775a105746cff3681b1567a4 100644 (file)
@@ -97,6 +97,7 @@ namespace Crow.IML
                internal static EventInfo eiLogicalParentChanged = typeof(GraphicObject).GetEvent("LogicalParentChanged");
 
                internal static MethodInfo miIFaceLoad = typeof(Interface).GetMethod ("CreateInstance", BindingFlags.Instance | BindingFlags.Public);
+               internal static MethodInfo miIFaceCreateTemplateInst = typeof (Interface).GetMethod ("CreateTemplateInstance", BindingFlags.Instance | BindingFlags.Public);
                internal static MethodInfo miGetITemp = typeof(Interface).GetMethod ("GetItemTemplate", BindingFlags.Instance | BindingFlags.Public);
 
                internal static MethodInfo miAddITemp = typeof(Dictionary<string, ItemTemplate>).GetMethod ("set_Item", new Type[] { typeof(string), typeof(ItemTemplate) });
index 6ecdb496d4f3999c54376d8c2e1316d7e2467866..1f490f11742f39b258f31e395a782a97c5f8949e 100644 (file)
@@ -76,7 +76,7 @@ namespace Crow
                                if (string.IsNullOrEmpty(_template))
                                        loadTemplate ();
                                else
-                                       loadTemplate (IFace.CreateInstance (_template));
+                                       loadTemplate (IFace.CreateTemplateInstance (_template, this.GetType()));
                        }
                }
                /// <summary>
index be534d6c33c9d5edcaeb30a9084235c9efa2bc15..988520017363addd06301c03c6a7347f48d69fab 100644 (file)
@@ -336,8 +336,11 @@ namespace Crow.IML
                                        } else {
                                                ctx.il.Emit (OpCodes.Ldarg_1);//load currentInterface
                                                ctx.il.Emit (OpCodes.Ldstr, templatePath); //Load template path string
-                                               ctx.il.Emit (OpCodes.Callvirt,//call Interface.Load(path)
-                                                       CompilerServices.miIFaceLoad);
+                                               //get declaring type for search fallback assembly
+                                               ctx.il.Emit (OpCodes.Ldloc_0);
+                                               ctx.il.Emit (OpCodes.Call, CompilerServices.miGetType);
+                                               ctx.il.Emit (OpCodes.Callvirt,
+                                                       CompilerServices.miIFaceCreateTemplateInst);
                                        }
                                        ctx.il.Emit (OpCodes.Callvirt, CompilerServices.miLoadTmp);//load template
                                }
@@ -348,7 +351,7 @@ namespace Crow.IML
                                                if (iface.ItemTemplates.ContainsKey (itemTemplatePath)) {
                                                        itemTemplateIds.Add (new string [] { "default", itemTemplatePath, "" });
                                                } else {
-                                                       using (Stream stream = iface.GetStreamFromPath (itemTemplatePath)) {
+                                                       using (Stream stream = iface.GetTemplateStreamFromPath (itemTemplatePath, ctx.CurrentNodeType)) {
                                                                //itemtemplate files may have multiple root nodes
                                                                XmlReaderSettings itrSettings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
                                                                using (XmlReader itr = XmlReader.Create (stream, itrSettings)) {                                                                        
index 99e385c9f503c6991e6842c4d36ad0b744b1ea1c..0c4616131242f9c4c3dde6da490ed3a74707491d 100644 (file)
@@ -355,6 +355,7 @@ namespace Crow
                /// on the first instance creation of a IML item.
                /// </summary>
                public Dictionary<String, Instantiator> Instantiators = new Dictionary<string, Instantiator>();
+               public Dictionary<String, Instantiator> Templates = new Dictionary<string, Instantiator> ();
                /// <summary>
                /// default templates dic by metadata token
                /// </summary>
@@ -424,6 +425,28 @@ namespace Crow
 
 
                #region Load/Save
+               /// <summary>get template stream from path providing the declaring type for which
+               /// this template is loaded. If not found in entry assembly, the assembly where the type is defined
+               /// will be searched
+               /// </summary>
+               /// <returns>The template stream</returns>
+               public virtual Stream GetTemplateStreamFromPath (string path, Type declaringType)
+               {
+                       Stream s = null;
+                       if (path.StartsWith ("#", StringComparison.Ordinal)) {
+                               string resId = path.Substring (1);
+                               s = Assembly.GetEntryAssembly ().GetManifestResourceStream (resId);
+                               if (s == null)
+                                       s = Assembly.GetAssembly (declaringType).GetManifestResourceStream (resId);
+                               if (s == null)
+                                       throw new Exception ($"Template not found '{path}'");
+                       } else {
+                               if (!File.Exists (path))
+                                       throw new FileNotFoundException ("Template not found: ", path);
+                               s = new FileStream (path, FileMode.Open, FileAccess.Read);
+                       }
+                       return s;
+               }
                /// <summary>Open file or find a resource from path string</summary>
                /// <returns>A file or resource stream</returns>
                /// <param name="path">This could be a normal file path, or an embedded ressource ID
@@ -434,12 +457,11 @@ namespace Crow
 
                        if (path.StartsWith ("#", StringComparison.Ordinal)) {
                                string resId = path.Substring (1);
-                               //try/catch added to prevent nunit error
-                               try {                                   
-                                       stream = System.Reflection.Assembly.GetEntryAssembly ().GetManifestResourceStream (resId);
-                               } catch {}
-                               if (stream == null)//try to find ressource in Crow assembly
-                                       stream = System.Reflection.Assembly.GetExecutingAssembly ().GetManifestResourceStream (resId);
+                               string assemblyName = resId.Split ('.') [0];
+                               Assembly a = AppDomain.CurrentDomain.GetAssemblies ().FirstOrDefault (aa => aa.GetName ().Name == assemblyName);
+                               if (a == null)
+                                       throw new Exception ($"Assembly '{assemblyName}' not found for ressource '{path}'.");
+                               stream = a.GetManifestResourceStream (resId);
                                if (stream == null)
                                        throw new Exception ("Resource not found: " + path);
                        } else {
@@ -455,13 +477,16 @@ namespace Crow
 
                        if (path.StartsWith ("#", StringComparison.Ordinal)) {
                                string resId = path.Substring (1);
-                               //try/catch added to prevent nunit error
-                               stream = Assembly.GetEntryAssembly ().GetManifestResourceStream (resId);                                
-                               if (stream == null)//try to find ressource in Crow assembly
-                                       stream = Assembly.GetExecutingAssembly ().GetManifestResourceStream (resId);
-                if (stream == null)                
-                    throw new Exception("Resource not found: " + path);
-                               Console.WriteLine(Assembly.GetEntryAssembly ().GetName ());
+                               string assemblyName = resId.Split ('.') [0];
+                               Assembly a = AppDomain.CurrentDomain.GetAssemblies ().FirstOrDefault (aa => aa.GetName ().Name == assemblyName);
+                               if (a == null)
+                                       throw new Exception ($"Assembly '{assemblyName}' not found for ressource '{path}'.");
+                               stream = a.GetManifestResourceStream (resId);
+                               /*foreach (var s in a.GetManifestResourceNames()) {
+                                       Console.WriteLine (s);
+                               }*/
+                               if (stream == null)
+                                       throw new Exception ("Resource not found: " + path);
                        } else {
                                if (!File.Exists (path))
                                        throw new FileNotFoundException ("File not found: ", path);
@@ -516,6 +541,21 @@ namespace Crow
                        }
                }
                /// <summary>
+               /// Create an instance of a GraphicObject linked to this interface but not added to the GraphicTree
+               /// </summary>
+               /// <returns>new instance of graphic object created</returns>
+               /// <param name="path">path of the iml file to load</param>
+               public virtual GraphicObject CreateTemplateInstance (string path, Type declaringType)
+               {
+                       try {
+                               if (!Templates.ContainsKey (path))
+                                       Templates [path] = new Instantiator (this, GetTemplateStreamFromPath(path, declaringType), path);
+                               return Templates [path].CreateInstance ();
+                       } catch (Exception ex) {
+                               throw new Exception ("Error loading Template <" + path + ">:", ex);
+                       }
+               }
+               /// <summary>
                /// Fetch instantiator from cache or create it.
                /// </summary>
                /// <returns>new Instantiator</returns>
index a27ddeaf7c46520f13dc42a4101ead7f2d7ee6d4..1f41e5522d64d09085e79965f4f4e8060c9a9cb6 100644 (file)
@@ -96,24 +96,6 @@ namespace Crow
                string fetchMethodName;
                string dataTest;
 
-               static Stream getItemTemplateStream (string path, Type declaringType)
-               {
-                       Stream s = null;
-                       if (path.StartsWith ("#", StringComparison.Ordinal)) {
-                               string resId = path.Substring (1);
-                               s = Assembly.GetEntryAssembly ().GetManifestResourceStream (resId);
-                               if (s == null)
-                                       s = Assembly.GetAssembly (declaringType).GetManifestResourceStream (resId);
-                               if (s == null)
-                                       throw new Exception ($"Item Template not found '{path}'");
-                       } else {
-                               if (!File.Exists (path))
-                                       throw new FileNotFoundException ("Item Template not found: ", path);
-                               s = new FileStream (path, FileMode.Open, FileAccess.Read);
-                       }
-                       return s;
-               }
-
                #region CTOR
                /// <summary>
                /// Initializes a new instance of the <see cref="Crow.ItemTemplate"/> class by parsing the file passed as argument.
@@ -122,7 +104,7 @@ namespace Crow
                /// <param name="_dataType">type this item will be choosen for, or member of the data item</param>
                /// <param name="_fetchDataMethod">for hierarchical data, method to call for children fetching</param>
                public ItemTemplate (Interface _iface, string path, Type declaringType, string _dataTest = "TypeOf", string _dataType = "default", string _fetchDataMethod = null)
-                       : base(_iface, getItemTemplateStream(path, declaringType)) {
+                       : base(_iface, _iface.GetTemplateStreamFromPath (path, declaringType)) {
                        strDataType = _dataType;
                        fetchMethodName = _fetchDataMethod;
                        dataTest = _dataTest;