aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/IMLReader.cs
blob: 922a48d7d7b1bc485ca071d4b1f607855515ab57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
//  IMLReader.cs
//
//  Author:
//       Jean-Philippe Bruyère <jp.bruyere@hotmail.com>
//
//  Copyright (c) 2016 jp
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Xml;
using System.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Linq;
using System.Collections.Generic;

namespace Crow
{
	public class IMLReader : XmlTextReader
	{
		InstanciatorInvoker loader = null;

		string ImlPath;
		Stream ImlStream;

		DynamicMethod dm = null;

		public ILGenerator il = null;
		public Type RootType = null;

		/// <summary>
		/// Finalize instatiator MSIL and return LoaderInvoker delegate
		/// </summary>
		public InstanciatorInvoker GetLoader(){
			if (loader != null)
				return loader;

			il.Emit(OpCodes.Ret);
			loader = (InstanciatorInvoker)dm.CreateDelegate (typeof(InstanciatorInvoker));
			return loader;
		}

		#region CTOR
		public IMLReader (string path)
			: this(Interface.GetStreamFromPath (path)){
			ImlPath = path;
		}
		public IMLReader (Stream stream)
			: base(stream)
		{
			ImlStream = stream;
			createInstantiator ();
		}
		/// <summary>
		/// Used to parse xmlFrament with same code generator linked
		/// If ilGen=null, a new Code Generator will be created.
		/// </summary>
		public IMLReader (ILGenerator ilGen, string xmlFragment)
			: base(xmlFragment, XmlNodeType.Element,null){
			il = ilGen;

			if (il != null)
				return;

			createInstantiator();
		}
		#endregion

		void createInstantiator(){
			readRootType();
			InitEmitter();
			emitLoader(RootType);
			Read();//close tag
		}
		/// <summary>
		/// Inits il generator, RootType must have been read first
		/// </summary>
		void InitEmitter(){

			dm = new DynamicMethod("dyn_instantiator",
				MethodAttributes.Family | MethodAttributes.FamANDAssem | MethodAttributes.NewSlot,
				CallingConventions.Standard,
				typeof(void),new Type[] {typeof(object), typeof(Interface)}, RootType, true);

			il = dm.GetILGenerator(256);
			il.DeclareLocal(typeof(GraphicObject));
			il.Emit(OpCodes.Nop);
			//set local GraphicObject to root object passed as 1st argument
			il.Emit (OpCodes.Ldarg_0);
			il.Emit (OpCodes.Stloc_0);
			CompilerServices.emitSetCurInterface (il);
		}
		void emitLoader(Type crowType){
			string tmpXml = ReadOuterXml ();

			il.Emit (OpCodes.Ldloc_0);//save current go onto the stack if child has to be added

			#region Template and TemplateItems loading
			if (typeof(TemplatedControl).IsAssignableFrom (crowType)) {
				//if its a template, first read template elements
				using (IMLReader reader = new IMLReader (il, tmpXml)) {

					string templatePath = reader.GetAttribute ("Template");
					//string itemTemplatePath = reader.GetAttribute ("ItemTemplate");

					List<string[]> itemTemplateIds = new List<string[]> ();
					bool inlineTemplate = false;
					reader.Read ();
					int depth = reader.Depth + 1;
					while (reader.Read ()) {
						if (!reader.IsStartElement () || reader.Depth > depth)
							continue;
						if (reader.Name == "Template") {
							inlineTemplate = true;
							reader.Read ();

							readChildren (reader, crowType,true);
						} else if (reader.Name == "ItemTemplate") {
							string dataType = "default", datas = "", path = "";
							while (reader.MoveToNextAttribute ()) {
								if (reader.Name == "DataType")
									dataType = reader.Value;
								else if (reader.Name == "Data")
									datas = reader.Value;
								else if (reader.Name == "Path")
									path = reader.Value;
							}
							reader.MoveToElement ();
							using (IMLReader iTmp = new IMLReader (null, reader.ReadInnerXml ())) {
								string uid = Guid.NewGuid ().ToString ();
								Interface.Instantiators [uid] =
									new ItemTemplate (iTmp.RootType, iTmp.GetLoader (), dataType, datas);
								
								itemTemplateIds.Add (new string[] { dataType, uid, datas });
							}
						}
					}

					if (!inlineTemplate) {
						reader.il.Emit (OpCodes.Ldloc_0);//Load  this templateControl ref
						if (string.IsNullOrEmpty (templatePath)) {
							reader.il.Emit (OpCodes.Ldnull);//default template loading
						}else{
							reader.il.Emit (OpCodes.Ldarg_1);//load currentInterface
							reader.il.Emit (OpCodes.Ldstr, templatePath); //Load template path string
							reader.il.Emit (OpCodes.Callvirt,//call Interface.Load(path)
									typeof(Interface).GetMethod ("Load", BindingFlags.Instance | BindingFlags.Public));
						}
						reader.il.Emit (OpCodes.Callvirt,//load template
							crowType.GetMethod ("loadTemplate", BindingFlags.Instance | BindingFlags.NonPublic));
					}
					foreach (string[] iTempId in itemTemplateIds) {
						reader.il.Emit (OpCodes.Ldloc_0);//load TempControl ref
						reader.il.Emit (OpCodes.Ldfld,//load ItemTemplates dic field
							typeof(TemplatedControl).GetField("ItemTemplates"));
						reader.il.Emit (OpCodes.Ldstr, iTempId[0]);//load key
						reader.il.Emit (OpCodes.Ldstr, iTempId[1]);//load value
						reader.il.Emit (OpCodes.Callvirt,
							typeof(Interface).GetMethod ("GetItemTemplate"));
						reader.il.Emit (OpCodes.Callvirt,
							typeof(Dictionary<string, ItemTemplate>).GetMethod ("set_Item",
								new Type[] { typeof(string), typeof(ItemTemplate) }));

						if (!string.IsNullOrEmpty (iTempId [2])) {
							//expand delegate creation
							reader.il.Emit (OpCodes.Ldloc_0);//load TempControl ref
							reader.il.Emit (OpCodes.Ldfld,//load ItemTemplates dic field
								typeof(TemplatedControl).GetField ("ItemTemplates"));
							reader.il.Emit (OpCodes.Ldstr, iTempId [0]);//load key
							reader.il.Emit (OpCodes.Callvirt,
								typeof(Dictionary<string, ItemTemplate>).GetMethod ("get_Item",
									new Type[] { typeof(string) }));
							reader.il.Emit (OpCodes.Ldloc_0);//load root of treeView
							reader.il.Emit (OpCodes.Callvirt,
								typeof(ItemTemplate).GetMethod ("CreateExpandDelegate"));
						}
					}
				}
			}
			#endregion

			using (IMLReader reader = new IMLReader(il,tmpXml)){
				reader.Read ();

				#region Styling and default values loading
				if (reader.HasAttributes) {
					string style = reader.GetAttribute ("Style");
					if (!string.IsNullOrEmpty (style)) {
						PropertyInfo pi = crowType.GetProperty ("Style");
						CompilerServices.EmitSetValue (reader.il, pi, style);
					}
				}
				reader.il.Emit (OpCodes.Ldloc_0);
				reader.il.Emit (OpCodes.Callvirt, typeof(GraphicObject).GetMethod ("loadDefaultValues"));
				#endregion

				#region Attributes reading
				if (reader.HasAttributes) {

					MethodInfo miAddBinding = typeof(GraphicObject).GetMethod ("BindMember");

					while (reader.MoveToNextAttribute ()) {
						if (reader.Name == "Style")
							continue;

						MemberInfo mi = crowType.GetMember (reader.Name).FirstOrDefault();
						if (mi == null)
							throw new Exception ("Member '" + reader.Name + "' not found in " + crowType.Name);
						if (mi.MemberType == MemberTypes.Event) {
							CompilerServices.emitBindingCreation (reader.il, reader.Name, reader.Value);
							continue;
						}
						PropertyInfo pi = mi as PropertyInfo;
						if (pi == null)
							throw new Exception ("Member '" + reader.Name + "' not found in " + crowType.Name);

						if (reader.Value.StartsWith ("{")) {
							CompilerServices.emitBindingCreation (reader.il, reader.Name, reader.Value.Substring (1, reader.Value.Length - 2));
						}else
							CompilerServices.EmitSetValue (reader.il, pi, reader.Value);

					}
					reader.MoveToElement ();
				}
				#endregion

				if (reader.IsEmptyElement) {
					reader.il.Emit (OpCodes.Pop);//pop saved ref to current object
					return;
				}

				readChildren (reader, crowType);
			}
			il.Emit (OpCodes.Pop);//pop saved ref to current object
		}
		/// <summary>
		/// Parse child node an generate corresponding msil
		/// </summary>
		void readChildren(IMLReader reader, Type crowType, bool templateLoading = false){
			MethodInfo miAddChild = null;
			bool endTagReached = false;
			while (reader.Read()){
				switch (reader.NodeType) {
				case XmlNodeType.EndElement:
					endTagReached = true;
					break;
				case XmlNodeType.Element:
					//Templates
					if (reader.Name == "Template" ||
					    reader.Name == "ItemTemplate") {
						reader.Skip ();
						continue;
					}

					if (miAddChild == null) {
						if (typeof(Group).IsAssignableFrom (crowType))
							miAddChild = typeof(Group).GetMethod ("AddChild");
						else if (typeof(Container).IsAssignableFrom (crowType))
							miAddChild = typeof(Container).GetMethod ("SetChild");
						else if (typeof(TemplatedContainer).IsAssignableFrom (crowType) && !templateLoading)
							miAddChild = typeof(TemplatedContainer).GetProperty ("Content").GetSetMethod ();
						else if (typeof(TemplatedGroup).IsAssignableFrom (crowType) && !templateLoading)
							miAddChild = typeof(TemplatedGroup).GetMethod ("AddItem",
								BindingFlags.Instance | BindingFlags.Public);
						else if (typeof(TemplatedControl).IsAssignableFrom (crowType))
							miAddChild = typeof(TemplatedControl).GetMethod ("loadTemplate",
								BindingFlags.Instance | BindingFlags.NonPublic);
						else if (typeof(PrivateContainer).IsAssignableFrom (crowType))
							miAddChild = typeof(PrivateContainer).GetMethod ("SetChild",
								BindingFlags.Instance | BindingFlags.NonPublic);
					}
					//push current instance on stack for parenting
					//loc_0 will be used for child
					reader.il.Emit (OpCodes.Ldloc_0);

					Type t = Type.GetType ("Crow." + reader.Name);
					if (t == null) {
						Assembly a = Assembly.GetEntryAssembly ();
						foreach (Type expT in a.GetExportedTypes ()) {
							if (expT.Name == reader.Name)
								t = expT;
						}
					}
					if (t == null)
						throw new Exception (reader.Name + " type not found");

					reader.il.Emit (OpCodes.Newobj, t.GetConstructors () [0]);//TODO:search parameterless ctor
					reader.il.Emit (OpCodes.Stloc_0);//child is now loc_0
					CompilerServices.emitSetCurInterface (il);

					reader.emitLoader (t);

					reader.il.Emit (OpCodes.Ldloc_0);//load child on stack for parenting
					if (miAddChild == null)
						System.Diagnostics.Debugger.Break ();
					reader.il.Emit (OpCodes.Callvirt, miAddChild);
					reader.il.Emit (OpCodes.Stloc_0); //reset local to current go
					reader.il.Emit (OpCodes.Ldloc_0);//save current go onto the stack if child has to be added
					break;
				}
				if (endTagReached)
					break;
			}
		}
		/// <summary>
		/// read first node to set GraphicObject class for loading
		/// and let reader position on that node
		/// </summary>
		Type readRootType ()
		{
			string root = "Object";
			while (Read()) {
				if (NodeType == XmlNodeType.Element) {
					root = this.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;
				}
			}
			RootType = t;
			return t;
		}
	}
}