summaryrefslogtreecommitdiff
path: root/Runtime/Mono/MonoAttributeHelpers.cpp
blob: cde105481d62e9ed87e965f858c4942784f9266b (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
#include "UnityPrefix.h"
#include "Configuration/UnityConfigure.h" // include before anything, to get Prof_ENABLED if that is defined
#if UNITY_EDITOR && ENABLE_MONO
#include "MonoManager.h"
#include "MonoAttributeHelpers.h"
#include <stdlib.h>

using namespace std;


static string RemoveStringFromString (string str, string remove)
{
	while (true)
	{
		int pos = str.find (remove);
		if (pos == string::npos)
			break;
		str.erase (pos, remove.size());
	}
	return str;
}

static std::string SignatureToString (MonoMethodSignature* signature)
{
	MonoType* methodType;
	void* iter = NULL;
	std::string parameters;
	while ((methodType = mono_signature_get_params (signature, &iter)))
	{
		if (!parameters.empty())
			parameters += ", ";
		parameters += mono_type_get_name(methodType);
	}

	string returnType = mono_type_get_name( mono_signature_get_return_type (signature) );
	string result = Format ("%s Func(%s)", returnType.c_str(), parameters.c_str());
	return RemoveStringFromString (result, "System.");
}



void GetMethodsWithAttribute (ScriptingClass* attributeClass, MonoMethod* comparedParams, std::vector<MonoMethod*>& resultList)
{
	MonoMethodSignature* pCompareSig = NULL;
	if (comparedParams)
		pCompareSig = mono_method_signature( comparedParams );

	for (int i = MonoManager::kEngineAssembly; i < GetMonoManager().GetAssemblyCount(); i++)
	{
		MonoAssembly* assembly = GetMonoManager().GetAssembly(i);
		if (!assembly)
			continue;

		MonoImage* pMonoImage = mono_assembly_get_image( assembly );
		if (!pMonoImage)
			continue;

		// stolen from #include... 
		const int MONO_TABLE_TYPEDEF = 2;				// mono/metadata/blob.h
		const int MONO_TOKEN_TYPE_DEF = 0x02000000;		// mono/metadata/tokentype.h

		int numFields = mono_image_get_table_rows( pMonoImage, MONO_TABLE_TYPEDEF );
		for (int typeIdx = 1; typeIdx < numFields; ++typeIdx)
		{
			guint32 token = MONO_TOKEN_TYPE_DEF | (typeIdx + 1);
			MonoClass* klass = mono_class_get (pMonoImage, token);
			gpointer iter = NULL;
			MonoMethod* method;

			if (klass == NULL) {
				// We need to call these two methods to clear the thread local
				// loader error status in mono. If not we'll randomly process the error
				// the next time it's checked.
				void* last_error = mono_loader_get_last_error ();
				mono_loader_error_prepare_exception (last_error);
 				continue;
			}

			while ((method = mono_class_get_methods(klass, &iter)))
			{
				MonoMethodSignature* sig = mono_method_signature (method);

				if (!sig) 
					continue;

				MonoCustomAttrInfo* attribInfo = mono_custom_attrs_from_method(method);
				if (!attribInfo)
					continue;

				if (!mono_custom_attrs_has_attr (attribInfo, attributeClass))
				{
					mono_custom_attrs_free(attribInfo);
					continue;
				}

				// Get static methods only
				bool isInstanceMethod = mono_signature_is_instance (sig);
				if (isInstanceMethod)
				{
					mono_custom_attrs_free(attribInfo);
					ErrorString (Format("UnityException: An [%s] attributed method is not static", mono_class_get_name(attributeClass)));
					continue;
				}

				if (pCompareSig)
				{
					if (!mono_metadata_signature_equal(sig, pCompareSig))
					{
						mono_custom_attrs_free(attribInfo);
						ErrorString (Format("UnityException: An [%s] attributed method has incorrect signature: %s. Correct signature: %s", mono_class_get_name(attributeClass), SignatureToString(sig).c_str(), SignatureToString(pCompareSig).c_str()));
						continue;
					}
				}
				else
				{
					if (mono_signature_get_param_count (sig) > 0)
					{
						mono_custom_attrs_free(attribInfo);
						ErrorString (Format("UnityException: An [%s] attributed method parameter count should be 0 but signature is: %s", mono_class_get_name(attributeClass), SignatureToString(sig).c_str()));
						continue;
					}
				}

				resultList.push_back(method);
				mono_custom_attrs_free(attribInfo);
			}
		}
	}	
}

bool AttributeSorter( MonoMethod* methodA, MonoMethod* methodB )
{
#if UNITY_EDITOR
	MonoCustomAttrInfo* attribAInfo = mono_custom_attrs_from_method(methodA);
	MonoCustomAttrInfo* attribBInfo = mono_custom_attrs_from_method(methodB);
	MonoObject* objA = mono_custom_attrs_get_attr( attribAInfo, MONO_COMMON.callbackOrderAttribute );
	MonoObject* objB = mono_custom_attrs_get_attr( attribBInfo, MONO_COMMON.callbackOrderAttribute );
	MonoClass* klassA = mono_object_get_class(objA);
	MonoClass* klassB = mono_object_get_class(objB);
	MonoProperty* propertyA = mono_class_get_property_from_name(klassA, "callbackOrder");
	MonoProperty* propertyB = mono_class_get_property_from_name(klassB, "callbackOrder");
	MonoMethod* getterA = mono_property_get_get_method(propertyA);
	MonoMethod* getterB = mono_property_get_get_method(propertyB);
	MonoException* monoException = NULL;
	MonoObject* resA = mono_runtime_invoke(getterA, objA, NULL, &monoException);
	MonoObject* resB = mono_runtime_invoke(getterB, objB, NULL, &monoException);
	int x = ExtractMonoObjectData<signed int>(resA);
	int y = ExtractMonoObjectData<signed int>(resB);

	return x < y;
#else
	return false;
#endif
}

void CallMethodsWithAttribute (ScriptingClass* attributeClass, ScriptingArguments& arguments, MonoMethod* comparedParams)
{
	vector<MonoMethod*> resultList;

	GetMethodsWithAttribute(attributeClass, comparedParams, resultList);
	sort (resultList.begin(), resultList.end(), AttributeSorter);

	for (vector<MonoMethod*>::iterator iter = resultList.begin(); iter != resultList.end(); ++iter) 
	{
		ScriptingInvocation invocation(*iter);
		invocation.Arguments() = arguments;
		invocation.Invoke();
	}
}

bool CallMethodsWithAttributeAndReturnTrueIfUsed (ScriptingClass* attributeClass, ScriptingArguments& arguments, MonoMethod* comparedParams)
{
	vector<MonoMethod*> resultList;

	GetMethodsWithAttribute(attributeClass, comparedParams, resultList);
	sort (resultList.begin(), resultList.end(), AttributeSorter);

	for (vector<MonoMethod*>::iterator iter = resultList.begin(); iter != resultList.end(); ++iter) 
	{
		ScriptingInvocation invocation(*iter);
		invocation.Arguments() = arguments;
		ScriptingObjectPtr returnValueObj = invocation.Invoke();
		if (ExtractMonoObjectData<bool>(returnValueObj))
			return true;
	}

	return false;
}


#endif //UNITY_EDITOR && ENABLE_MONO