aboutsummaryrefslogtreecommitdiffstatsci
diff options
context:
space:
mode:
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>2018-12-18 07:31:53 +0100
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>2018-12-18 07:31:53 +0100
commitf4f93d9db467e3ba22310aa62fc8a52c7f6f4331 (patch)
treeb5f5a321a1f68476279622471400ef81f21e9752
parent4a8104cc5902a535390e5a3a0cab70102b35bf25 (diff)
debug template loading
-rw-r--r--Crow/src/CompilerServices/CompilerServices.cs1
-rw-r--r--Crow/src/GraphicObjects/TemplatedControl.cs2
-rw-r--r--Crow/src/Instantiator.cs9
-rw-r--r--Crow/src/Interface.cs66
-rw-r--r--Crow/src/ItemTemplate.cs20
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<string, ItemTemplate>).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()));
}
}
/// <summary>
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.
/// </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>
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
/// <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;