From 7560c0bb4a769210e289e2633ddf13d2617f8e6e Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Thu, 11 Aug 2016 02:35:39 +0200 Subject: [PATCH] =?utf8?q?resolved=20bug=20multiple=20template=20level=20r?= =?utf8?q?ead,=20ensure=20reader=20stay=20on=20sybling=20nodes=20=09modifi?= =?utf8?q?=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20src/IMLReader.cs=20creat?= =?utf8?q?e=20instance=20from=20XmlFragment=20=09modifi=C3=A9=C2=A0:=20=20?= =?utf8?q?=20=20=20=20=20=20=20src/Instantiator.cs=20update=20tests=20=09m?= =?utf8?q?odifi=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20Tests/BasicTests.cs?= =?utf8?q?=20=09modifi=C3=A9=C2=A0:=20=20=20=20=20=20=20=20=20Tests/Interf?= =?utf8?q?aces/Divers/imlEditor.crow?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- Tests/BasicTests.cs | 37 +++++++++++++++------- Tests/Interfaces/Divers/imlEditor.crow | 1 + src/IMLReader.cs | 37 +++++++++++----------- src/Instantiator.cs | 43 +++++++++++++++++++++++--- 4 files changed, 83 insertions(+), 35 deletions(-) diff --git a/Tests/BasicTests.cs b/Tests/BasicTests.cs index c862ccac..658cddd8 100644 --- a/Tests/BasicTests.cs +++ b/Tests/BasicTests.cs @@ -4,6 +4,8 @@ using System.Collections.Generic; using System.Linq; using System.IO; using System.Reflection; +using System.Text; +using System.Diagnostics; namespace Tests @@ -147,17 +149,30 @@ namespace Tests } void Tv_SelectedItemChanged (object sender, SelectionChangeEventArgs e) { -// FileInfo fi = e.NewValue as FileInfo; -// if (fi == null) -// return; -// if (fi.Extension == ".crow" || fi.Extension == ".goml") { -// IMLStream imls = new IMLStream (fi.FullName); -// lock (CrowInterface.UpdateMutex) { -// (CrowInterface.FindByName ("crowContainer") as Container).SetChild -// (imls.Instance); -// CurSources = imls.Source; -// } -// } + FileInfo fi = e.NewValue as FileInfo; + if (fi == null) + return; + if (fi.Extension == ".crow" || fi.Extension == ".goml") { + Instantiator i = new Instantiator(fi.FullName); + lock (CrowInterface.UpdateMutex) { + (CrowInterface.FindByName ("crowContainer") as Container).SetChild + (i.CreateInstance()); + CurSources = i.GetImlSourcesCode(); + } + } + } + void onImlSourceChanged(Object sender, TextChangeEventArgs e){ + Instantiator i; + try { + i = Instantiator.CreateFromImlFragment (e.Text); + } catch (Exception ex) { + Debug.WriteLine (ex); + return; + } + lock (CrowInterface.UpdateMutex) { + (CrowInterface.FindByName ("crowContainer") as Container).SetChild + (i.CreateInstance()); + } } void onButClick(object send, MouseButtonEventArgs e) { diff --git a/Tests/Interfaces/Divers/imlEditor.crow b/Tests/Interfaces/Divers/imlEditor.crow index 527745c1..bbad04ef 100644 --- a/Tests/Interfaces/Divers/imlEditor.crow +++ b/Tests/Interfaces/Divers/imlEditor.crow @@ -15,6 +15,7 @@ + /// Finalize instatiator MSIL and return LoaderInvoker delegate + /// + public Interface.LoaderInvoker GetLoader(){ + if (loader != null) + return loader; + + il.Emit(OpCodes.Ret); + loader = (Interface.LoaderInvoker)dm.CreateDelegate (typeof(Interface.LoaderInvoker)); + return loader; + } #region CTOR public IMLReader (string path) @@ -72,17 +85,6 @@ namespace Crow Read();//close tag } /// - /// Finalize instatiator MSIL and return LoaderInvoker delegate - /// - public Interface.LoaderInvoker GetLoader(){ - if (loader != null) - return loader; - - il.Emit(OpCodes.Ret); - loader = (Interface.LoaderInvoker)dm.CreateDelegate (typeof(Interface.LoaderInvoker)); - return loader; - } - /// /// Inits il generator, RootType must have been read first /// void InitEmitter(){ @@ -114,16 +116,15 @@ namespace Crow List itemTemplateIds = new List (); bool inlineTemplate = false; reader.Read (); - + int depth = reader.Depth + 1; while (reader.Read ()) { - if (!reader.IsStartElement ()) + if (!reader.IsStartElement () || reader.Depth > depth) continue; if (reader.Name == "Template") { inlineTemplate = true; reader.Read (); readChildren (reader, crowType,true); - continue; } else if (reader.Name == "ItemTemplate") { string dataType = "default", datas = "", path = ""; while (reader.MoveToNextAttribute ()) { @@ -142,8 +143,6 @@ namespace Crow itemTemplateIds.Add (new string[] { dataType, uid, datas }); } - - continue; } } diff --git a/src/Instantiator.cs b/src/Instantiator.cs index 23871ef2..e79988fb 100644 --- a/src/Instantiator.cs +++ b/src/Instantiator.cs @@ -20,6 +20,8 @@ // along with this program. If not, see . using System; using System.Threading; +using System.IO; +using System.Text; namespace Crow { @@ -27,11 +29,12 @@ namespace Crow { public Type RootType; Interface.LoaderInvoker loader; + string imlPath; + #region CTOR public Instantiator (string path){ - System.Globalization.CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture; - Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; + imlPath = path; #if DEBUG_LOAD Stopwatch loadingTime = new Stopwatch (); @@ -43,7 +46,7 @@ namespace Crow RootType = itr.RootType; } } catch (Exception ex) { - throw new Exception ("Error loading <" + path + ">:", ex); + throw new Exception ("Error loading <" + path + ">:\n", ex); } #if DEBUG_LOAD @@ -51,8 +54,30 @@ namespace Crow Debug.WriteLine ("IML Instantiator creation '{2}' : {0} ticks, {1} ms", loadingTime.ElapsedTicks, loadingTime.ElapsedMilliseconds, path); #endif - - Thread.CurrentThread.CurrentCulture = savedCulture; + } + public static Instantiator CreateFromImlFragment(string fragment){ + try { + using (Stream s = new MemoryStream(Encoding.UTF8.GetBytes(fragment))){ + return new Instantiator(s); + } + } catch (Exception ex) { + throw new Exception ("Error loading fragment:\n" + fragment + "\n", ex); + } + } + public Instantiator (Stream stream){ + #if DEBUG_LOAD + Stopwatch loadingTime = new Stopwatch (); + loadingTime.Start (); + #endif + using (IMLReader itr = new IMLReader (stream)){ + loader = itr.GetLoader (); + RootType = itr.RootType; + } + #if DEBUG_LOAD + loadingTime.Stop (); + Debug.WriteLine ("IML Instantiator creation '{2}' : {0} ticks, {1} ms", + loadingTime.ElapsedTicks, loadingTime.ElapsedMilliseconds, path); + #endif } public Instantiator (Type _root, Interface.LoaderInvoker _loader) { @@ -66,6 +91,14 @@ namespace Crow loader (tmp); return tmp; } + public string GetImlSourcesCode(){ + try { + using (StreamReader sr = new StreamReader (imlPath)) + return sr.ReadToEnd(); + } catch (Exception ex) { + throw new Exception ("Error getting sources for <" + imlPath + ">:", ex); + } + } } } -- 2.47.3