From f4f93d9db467e3ba22310aa62fc8a52c7f6f4331 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Tue, 18 Dec 2018 07:31:53 +0100 Subject: [PATCH] debug template loading --- Crow/src/CompilerServices/CompilerServices.cs | 1 + Crow/src/GraphicObjects/TemplatedControl.cs | 2 +- Crow/src/Instantiator.cs | 9 ++- Crow/src/Interface.cs | 66 +++++++++++++++---- Crow/src/ItemTemplate.cs | 20 +----- 5 files changed, 62 insertions(+), 36 deletions(-) diff --git a/Crow/src/CompilerServices/CompilerServices.cs b/Crow/src/CompilerServices/CompilerServices.cs index 80e47e9d..7b95c9d7 100644 --- a/Crow/src/CompilerServices/CompilerServices.cs +++ b/Crow/src/CompilerServices/CompilerServices.cs @@ -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).GetMethod ("set_Item", new Type[] { typeof(string), typeof(ItemTemplate) }); diff --git a/Crow/src/GraphicObjects/TemplatedControl.cs b/Crow/src/GraphicObjects/TemplatedControl.cs index 6ecdb496..1f490f11 100644 --- a/Crow/src/GraphicObjects/TemplatedControl.cs +++ b/Crow/src/GraphicObjects/TemplatedControl.cs @@ -76,7 +76,7 @@ namespace Crow if (string.IsNullOrEmpty(_template)) loadTemplate (); else - loadTemplate (IFace.CreateInstance (_template)); + loadTemplate (IFace.CreateTemplateInstance (_template, this.GetType())); } } /// diff --git a/Crow/src/Instantiator.cs b/Crow/src/Instantiator.cs index be534d6c..98852001 100644 --- a/Crow/src/Instantiator.cs +++ b/Crow/src/Instantiator.cs @@ -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)) { diff --git a/Crow/src/Interface.cs b/Crow/src/Interface.cs index 99e385c9..0c461613 100644 --- a/Crow/src/Interface.cs +++ b/Crow/src/Interface.cs @@ -355,6 +355,7 @@ namespace Crow /// on the first instance creation of a IML item. /// public Dictionary Instantiators = new Dictionary(); + public Dictionary Templates = new Dictionary (); /// /// default templates dic by metadata token /// @@ -424,6 +425,28 @@ namespace Crow #region Load/Save + /// 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 + /// + /// The template stream + 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; + } /// Open file or find a resource from path string /// A file or resource stream /// 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 } } /// + /// Create an instance of a GraphicObject linked to this interface but not added to the GraphicTree + /// + /// new instance of graphic object created + /// path of the iml file to load + 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); + } + } + /// /// Fetch instantiator from cache or create it. /// /// new Instantiator diff --git a/Crow/src/ItemTemplate.cs b/Crow/src/ItemTemplate.cs index a27ddeaf..1f41e552 100644 --- a/Crow/src/ItemTemplate.cs +++ b/Crow/src/ItemTemplate.cs @@ -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 /// /// Initializes a new instance of the class by parsing the file passed as argument. @@ -122,7 +104,7 @@ namespace Crow /// type this item will be choosen for, or member of the data item /// for hierarchical data, method to call for children fetching 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; -- 2.47.3