ReflUtil.cs 2.81 KB
Newer Older
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
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);

		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);
		}
	}
}