From cc4c62ab5d809ff451ca475a7949adc4fdfd806b Mon Sep 17 00:00:00 2001 From: jpbruyere Date: Mon, 8 Aug 2016 19:22:52 +0200 Subject: IMLInstantiator implementation --- src/Interface.cs | 108 +++++++++++++++++++++++++++---------------------------- 1 file changed, 53 insertions(+), 55 deletions(-) (limited to 'src/Interface.cs') diff --git a/src/Interface.cs b/src/Interface.cs index 7b324de7..4712a54c 100644 --- a/src/Interface.cs +++ b/src/Interface.cs @@ -52,6 +52,10 @@ namespace Crow FontRenderingOptions.HintMetrics = HintMetrics.On; FontRenderingOptions.HintStyle = HintStyle.Medium; FontRenderingOptions.SubpixelOrder = SubpixelOrder.Rgb; + + Thread tBindings = new Thread (BindingThread); + tBindings.IsBackground = true; + tBindings.Start (); } public Interface(){ Interface.CurrentInterface = this; @@ -107,14 +111,16 @@ namespace Crow g.IsQueueForRedraw = true; } } + //fast compiled IML instantiators + public static Dictionary Instantiators = new Dictionary(); #region Default values and Style loading /// Default values of properties from GraphicObjects are retrieve from XML Attributes. /// The reflexion process used to retrieve those values being very slow, it is compiled in MSIL /// and injected as a dynamic method referenced in the DefaultValuesLoader Dictionnary. /// The compilation is done on the first object instancing, and is also done for custom widgets - public delegate void loadDefaultInvoker(object instance); - public static Dictionary DefaultValuesLoader = new Dictionary(); + public delegate void LoaderInvoker(object instance); + public static Dictionary DefaultValuesLoader = new Dictionary(); public static Dictionary> Styling; /// parse all styling data's and build global Styling Dictionary static void LoadStyling() { @@ -176,36 +182,6 @@ namespace Crow return stream; } - /// - /// Pre-read first node to set GraphicObject class for loading - /// and reset stream position to 0 - /// - public static Type GetTopContainerOfXMLStream (Stream stream) - { - string root = "Object"; - stream.Seek (0, SeekOrigin.Begin); - using (XmlReader reader = XmlReader.Create (stream)) { - while (reader.Read ()) { - // first element is the root element - if (reader.NodeType == XmlNodeType.Element) { - root = reader.Name; - break; - } - } - } - - Type t = Type.GetType ("Crow." + root); - if (t == null) { - Assembly a = Assembly.GetEntryAssembly (); - foreach (Type expT in a.GetExportedTypes ()) { - if (expT.Name == root) - t = expT; - } - } - - stream.Seek (0, SeekOrigin.Begin); - return t; - } public static void Save (string file, T graphicObject) { @@ -225,9 +201,10 @@ namespace Crow GraphicObject tmp = null; try { - using (Stream stream = GetStreamFromPath (path)) { - tmp = Load(stream, GetTopContainerOfXMLStream(stream)); - } + if (!Instantiators.ContainsKey(path)) + BuildInstaciator(path); + tmp = Instantiators [path].CreateInstance (); + } catch (Exception ex) { throw new Exception ("Error loading <" + path + ">:", ex); } @@ -236,37 +213,37 @@ namespace Crow return tmp; } - internal static GraphicObject Load (Stream stream, Type type) + public static void BuildInstaciator (string path) { + System.Globalization.CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture; + Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; + #if DEBUG_LOAD Stopwatch loadingTime = new Stopwatch (); loadingTime.Start (); #endif - GraphicObject result; - - CurrentInterface.XmlLoading = true; - - XmlSerializerNamespaces xn = new XmlSerializerNamespaces (); - xn.Add ("", ""); - - XmlSerializer xs = new XmlSerializer (type); - - result = (GraphicObject)xs.Deserialize (stream); - CurrentInterface.XmlLoading = false; + try { + using (Stream stream = GetStreamFromPath (path)) { + using (IMLInstantiatorBuilder itr = new IMLInstantiatorBuilder (stream)){ + itr.ReadRootType(); + itr.InitEmitter(); + CompilerServices.BuildInstanciator(itr, itr.RootType); + itr.Read();//close tag + Instantiators[path] = itr.GetInstanciator(); + } + } + } catch (Exception ex) { + throw new Exception ("Error loading <" + path + ">:", ex); + } #if DEBUG_LOAD - FileStream fs = stream as FileStream; - if (fs!=null) - CurrentGOMLPath = fs.Name; loadingTime.Stop (); - Debug.WriteLine ("GOML Loading ({2}->{3}): {0} ticks, {1} ms", - loadingTime.ElapsedTicks, - loadingTime.ElapsedMilliseconds, - CurrentGOMLPath, result.ToString()); + Debug.WriteLine ("IML Loading '{2}' : {0} ticks, {1} ms", + loadingTime.ElapsedTicks, loadingTime.ElapsedMilliseconds, path); #endif - return result; + Thread.CurrentThread.CurrentCulture = savedCulture; } public GraphicObject LoadInterface (string path) @@ -289,6 +266,27 @@ namespace Crow public List GraphicTree = new List(); + public static List Bindings = new List(); + public static void RegisterBinding (Binding b){ + lock (Bindings) + Bindings.Add (b); + } + public static void BindingThread(){ + while(true){ + Binding[] unresolved; + lock (Bindings){ + unresolved = Bindings.Where (b => !b.Resolved).ToArray (); + } + foreach (Binding ub in unresolved) { + if (ub.TryFindTarget ()) { + ub.Resolved = true; + Debug.WriteLine ("RESOLVED: " + ub); + } + } + Thread.Sleep (5); + } + } + public static Interface CurrentInterface; Rectangles _redrawClip = new Rectangles(); -- cgit v1.3.1-4-g156e