aboutsummaryrefslogtreecommitdiffstatsci
path: root/src/IMLStream.cs
blob: 290c5355513bac03dd6987d513a989c5abffab28 (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
//
//  IMLStream.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.IO;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading;
using System.Xml.Serialization;
using System.Xml;
using System.Collections.Generic;
using System.Diagnostics;

namespace Crow
{
	public class IMLStream : MemoryStream {
		public string Path;
		public Type RootType;
		public IMLStream(string path) : base (){
			Path = path;
			using (Stream stream = Interface.GetStreamFromPath (path))
				stream.CopyTo (this);
			//RootType = Interface.GetTopContainerOfXMLStream (this);
		}
		public IMLStream(Byte[] b) : base (b){			
			//RootType = Interface.GetTopContainerOfXMLStream (this);
		}
		/// <summary>
		/// Create a graphicObject instance from the this XML stream.
		/// </summary>
		public GraphicObject Instance {
			get {
				System.Globalization.CultureInfo savedCulture = Thread.CurrentThread.CurrentCulture;
				Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;

				Seek (0, SeekOrigin.Begin);
				GraphicObject tmp = null;//Interface.Load (this, this.RootType);

				Thread.CurrentThread.CurrentCulture = savedCulture;
				return tmp;
			}
		}
		/// <summary>
		/// Gets the xml source code as a string
		/// </summary>
		public string Source {
			get {
				Seek (0, SeekOrigin.Begin);
				using (StreamReader sr = new StreamReader(this))
					return sr.ReadToEnd();
			}
		}
	}
	public class ItemTemplate : IMLStream {		
		public EventHandler Expand;

		public ItemTemplate(string path)
			: base(path){}
		public ItemTemplate(Byte[] b)
			: base(b){}

		public void CreateExpandDelegate (TemplatedControl host, string strDataType, string method){
			Type dataType = Type.GetType(strDataType);
			Type hostType = typeof(TemplatedControl);//not sure is the best place to put the dyn method
			Type evtType = typeof(EventHandler);
			Type listBoxType = typeof(ListBox);

			MethodInfo evtInvoke = evtType.GetMethod ("Invoke");
			ParameterInfo [] evtParams = evtInvoke.GetParameters ();
			Type handlerArgsType = evtParams [1].ParameterType;

			Type [] args = { typeof (object), typeof (object), handlerArgsType };
			DynamicMethod dm = new DynamicMethod ("dyn_expand_" + method,
				typeof (void),
				args,
				hostType);


			#region IL generation
			ILGenerator il = dm.GetILGenerator (256);
			il.DeclareLocal(typeof(GraphicObject));

			il.Emit (OpCodes.Ldarg_1);

			MethodInfo miFindByName = typeof(GraphicObject).GetMethod("FindByName");
			il.Emit(OpCodes.Ldstr, "List");
			il.Emit (OpCodes.Callvirt, miFindByName);
			il.Emit (OpCodes.Stloc_0);

			FieldInfo fiTemplates = typeof(TemplatedControl).GetField("ItemTemplates");
			il.Emit (OpCodes.Ldloc_0);
			il.Emit (OpCodes.Ldarg_0);
			il.Emit (OpCodes.Ldfld, fiTemplates);
			il.Emit (OpCodes.Stfld, fiTemplates);

			il.Emit (OpCodes.Ldloc_0);
			il.Emit (OpCodes.Ldarg_1);
			il.Emit (OpCodes.Callvirt, typeof(GraphicObject).GetProperty("DataSource").GetGetMethod ());

			MethodInfo miGetDatas = dataType.GetMethod (method, new Type[] {});
			il.Emit (OpCodes.Callvirt, miGetDatas);

			il.Emit (OpCodes.Callvirt, listBoxType.GetProperty("Data").GetSetMethod ());

			il.Emit (OpCodes.Ret);

			#endregion

			Expand = (EventHandler)dm.CreateDelegate (evtType, host);
		}
	}
}