ReflUtil.cs 7.48 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace PoroCYon.FNAGLSL {
	public static class ReflUtil {
		readonly static FieldInfo gd_GLDevice_fi = typeof(GraphicsDevice).GetField(
			"GLDevice", BindingFlags.GetField|BindingFlags.NonPublic|BindingFlags.Instance);
		readonly static FieldInfo ef_glEffect_fi = typeof(Effect).GetField(
			"glEffect", BindingFlags.GetField|BindingFlags.NonPublic|BindingFlags.Instance);

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
		readonly static PropertyInfo ef_Techniques_pi = typeof(Effect).GetProperty(
			"Techniques", BindingFlags.SetProperty|BindingFlags.Public|BindingFlags.Instance),
									 ef_Parameters_pi = typeof(Effect).GetProperty(
			"Parameters", BindingFlags.SetProperty|BindingFlags.Public|BindingFlags.Instance);

		readonly static ConstructorInfo
			ci_EffTechColl, ci_EffParamColl, ci_EffAnnotColl,
			ci_EffTech, ci_EffParam, ci_EffAnnot;

		static ReflUtil() {
			var pm1f = new ParameterModifier(1);
			pm1f[0] = false;

			var pm4f = new ParameterModifier(4);
			pm4f[3] = pm4f[2] = pm4f[1] = pm4f[0] = false;

			var pm11f = new ParameterModifier(11);
			pm11f[3] = pm11f[2] = pm11f[1] = pm11f[0] = false;
			pm11f[7] = pm11f[6] = pm11f[5] = pm11f[4] = false;
			           pm11f[10]= pm11f[9] = pm11f[8] = false;

			var pm7f = new ParameterModifier(7);
			pm7f[3] = pm7f[2] = pm7f[1] = pm7f[0] = false;
			          pm7f[6] = pm7f[5] = pm7f[4] = false;

			var pm1fa = new[]{pm1f};
			ci_EffTechColl = typeof(EffectTechniqueCollection)
				.GetConstructor(BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(List<EffectTechnique>)}, pm1fa);

			ci_EffParamColl = typeof(EffectParameterCollection)
				.GetConstructor(BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(List<EffectParameter>)}, pm1fa);

			ci_EffAnnotColl = typeof(EffectAnnotationCollection)
				.GetConstructor(BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(List<EffectAnnotation>)}, pm1fa);

			ci_EffTech = typeof(EffectTechnique).GetConstructor(
				BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(string), typeof(IntPtr),
					typeof(EffectPassCollection), typeof(EffectAnnotationCollection)},
				new[]{pm4f});

			ci_EffParam = /*typeof(EffectParameter).GetConstructor( // FIXME: no worky
				BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(string), typeof(string), typeof(int),
					typeof(int), typeof(int), typeof(EffectParameterClass),
					typeof(EffectParameterType), typeof(EffectParameterCollection),
					typeof(EffectAnnotationCollection), typeof(IntPtr), typeof(uint)},
				new[]{pm11f});*/
				// fuck it
				typeof(EffectParameter).GetConstructors(BindingFlags.Instance
					|BindingFlags.CreateInstance|BindingFlags.NonPublic)[0];

			ci_EffAnnot = typeof(EffectAnnotation).GetConstructor(
				BindingFlags.CreateInstance|BindingFlags.NonPublic|BindingFlags.Instance,
				null, new[]{typeof(string), typeof(string), typeof(int),
					typeof(int), typeof(EffectParameterClass),
					typeof(EffectParameterType), typeof(IntPtr)}, new[]{pm7f});

			if (ci_EffTechColl == null || ci_EffParamColl == null ||
					ci_EffAnnotColl == null || ci_EffTech == null ||
					ci_EffParam == null || ci_EffAnnot == null)
				throw new Exception("can't find ctors");
		}

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
		static MethodInfo gldev_DeleteEffect_mi;
		static PropertyInfo ef_GLEffectData_pi, ef_EffectData_pi;

		public static object GLDevice(this GraphicsDevice gd) =>
			gd_GLDevice_fi.GetValue(gd);
		public static object glEffect(this Effect eff) =>
			ef_glEffect_fi.GetValue(eff);

		static object[] arg1 = new object[1];
		public static void DeleteEffect(this GraphicsDevice gd, Effect eff) {
			var gldev = gd.GLDevice();
			if (gldev_DeleteEffect_mi == null) {
				// type of the gldev not known at compile-time!
				gldev_DeleteEffect_mi = gldev.GetType().GetMethod("DeleteEffect",
					BindingFlags.InvokeMethod|BindingFlags.NonPublic|BindingFlags.Instance);
			}

			arg1[0] = eff.glEffect();
			gldev_DeleteEffect_mi.Invoke(gldev, arg1);
		}

		public static IntPtr GetGLEffectData(this Effect eff) {
			var gleff = eff.glEffect();

			if (ef_GLEffectData_pi == null) {
				// type of the gleff not known at compile-time!
				ef_GLEffectData_pi = gleff.GetType().GetProperty("GLEffectData",
					BindingFlags.SetProperty|BindingFlags.NonPublic|BindingFlags.Instance);
			}

			return (IntPtr)ef_GLEffectData_pi.GetValue(gleff);
		}
		public static void SetGLEffectData(this Effect eff, IntPtr value) {
			var gleff = eff.glEffect();

			if (ef_GLEffectData_pi == null) {
				// type of the gleff not known at compile-time!
				ef_GLEffectData_pi = gleff.GetType().GetProperty("GLEffectData",
					BindingFlags.SetProperty|BindingFlags.NonPublic|BindingFlags.Instance);
			}

			ef_GLEffectData_pi.SetValue(gleff, value);
		}

		public static IntPtr GetEffectData(this Effect eff) {
			var gleff = eff.glEffect();

			if (ef_EffectData_pi == null) {
				// type of the gleff not known at compile-time!
				ef_EffectData_pi = gleff.GetType().GetProperty("EffectData",
					BindingFlags.SetProperty|BindingFlags.NonPublic|BindingFlags.Instance);
			}

			return (IntPtr)ef_EffectData_pi.GetValue(gleff);
		}
		public static void SetEffectData(this Effect eff, IntPtr value) {
			var gleff = eff.glEffect();

			if (ef_EffectData_pi == null) {
				// type of the gleff not known at compile-time!
				ef_EffectData_pi = gleff.GetType().GetProperty("EffectData",
					BindingFlags.SetProperty|BindingFlags.NonPublic|BindingFlags.Instance);
			}

			ef_EffectData_pi.SetValue(gleff, value);
		}
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

		public static void SetTechniques(this Effect eff, EffectTechniqueCollection value) {
			ef_Techniques_pi.SetValue(eff, value);
		}
		public static void SetParameters(this Effect eff, EffectParameterCollection value) {
			ef_Parameters_pi.SetValue(eff, value);
		}

		public static EffectTechniqueCollection CreateTechColl(List<EffectTechnique> techniques)
			=> (EffectTechniqueCollection)ci_EffTechColl.Invoke(new[]{techniques});
		public static EffectParameterCollection CreateParamColl(List<EffectParameter> parms)
			=> (EffectParameterCollection)ci_EffParamColl.Invoke(new[]{parms});
		public static EffectAnnotationCollection CreateAnnotColl(List<EffectAnnotation> annots)
			=> (EffectAnnotationCollection)ci_EffAnnotColl.Invoke(new[]{annots});

		public static EffectTechnique CreateTechnique(string name, IntPtr pointer,
				EffectPassCollection passes, EffectAnnotationCollection annots)
			=> (EffectTechnique)ci_EffTech.Invoke(new object[]{name, pointer,
					passes, annots});
		public static EffectParameter CreateParameter(string name, string semantic,
				int rowCount, int colCount, int elemCount,
				EffectParameterClass paramClass, EffectParameterType paramType,
				EffectParameterCollection structMembers, EffectAnnotationCollection annots,
				IntPtr data, uint dataSize)
			=> (EffectParameter)ci_EffParam.Invoke(new object[]{name, semantic,
					rowCount, colCount, elemCount, paramClass, paramType,
					structMembers, annots, data, dataSize});
		public static EffectAnnotation CreateAnnotation(string name, string semantic,
				int rowCount, int colCount, EffectParameterClass paramClass,
				EffectParameterType paramType, IntPtr data)
			=> (EffectAnnotation)ci_EffAnnot.Invoke(new object[]{name, semantic,
					rowCount, colCount, paramClass, paramType, data});
181
182
183
	}
}