summaryrefslogtreecommitdiff
path: root/Runtime/Scripting/GetComponent.cpp
blob: d30bb41ab771ae9068275136d47b4f3c4331d048 (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
#include "UnityPrefix.h"
#if ENABLE_SCRIPTING
#include "ScriptingUtility.h"
#include "Runtime/Mono/MonoBehaviour.h"
#include "Runtime/Mono/MonoManager.h"
#include "Runtime/Graphics/Transform.h"
#include "Runtime/Misc/GameObjectUtility.h"
#include "Runtime/Scripting/ScriptingUtility.h"
#include "Runtime/Scripting/ScriptingExportUtility.h"
#include "Runtime/Animation/Animation.h"
#include "Runtime/Camera/Camera.h"
#include "Runtime/Utilities/dense_hash_map.h"
#include "Runtime/Scripting/Backend/ScriptingBackendApi.h"
#include "Runtime/Scripting/Backend/ScriptingTypeRegistry.h"
#include "Runtime/Scripting/ScriptingUtility.h"
#include "Runtime/Scripting/Scripting.h"

inline static bool ComponentMatchesRequirement_ByScriptingClass(Unity::GameObject& go, int componentIndex, ScriptingClassPtr compareClass)
{
	int classID = go.GetComponentClassIDAtIndex(componentIndex);
	if (classID != ClassID (MonoBehaviour))
		return false;

	Unity::Component& component = go.GetComponentAtIndex(componentIndex);
	MonoBehaviour& behaviour = static_cast<MonoBehaviour&> (component);
	ScriptingClassPtr klass = behaviour.GetClass ();
	if (klass == SCRIPTING_NULL)
		return false;

	if (klass == compareClass)
		return true;

	return scripting_class_is_subclass_of (klass, compareClass);
}

inline static bool ComponentMatchesRequirement_ByClassID(Unity::GameObject& go, int componentIndex, int compareClassID, bool isCompareClassSealed)
{
	int classID = go.GetComponentClassIDAtIndex(componentIndex);
	
	if (classID == compareClassID)
		return true;

	if (isCompareClassSealed)
		return false;
	
	return Object::IsDerivedFromClassID(classID,compareClassID);
}

namespace SearchMethod
{
	enum {
		ByClassID,
		ByScriptingClass,
		DontEvenStart
	};
}

template<bool getOnlyOne, int searchMethod>
static bool GetComponentsImplementation (GameObject& go, bool includeInactive, void* compareData, void* output)
{
	if (!includeInactive && !go.IsActive())
		return false;

	bool foundAtLeastOne = false;

	bool isSealed;
	UInt64 compareClassID = (UInt64)(uintptr_t)(compareData);
	if (searchMethod == SearchMethod::ByClassID)
	{
		Assert(Transform::IsSealedClass());
		Assert(Animation::IsSealedClass());
		Assert(Camera::IsSealedClass());
		isSealed = compareClassID == ClassID(Transform) || compareClassID == ClassID(Animation) || compareClassID == ClassID(Camera);
	}

	int count = go.GetComponentCount ();
	for (int i = 0; i < count; i++)
	{
		if (searchMethod == SearchMethod::ByClassID && !ComponentMatchesRequirement_ByClassID(go,i,compareClassID,isSealed))
			continue;

		if (searchMethod == SearchMethod::ByScriptingClass && !ComponentMatchesRequirement_ByScriptingClass(go,i, reinterpret_cast<ScriptingClassPtr>(compareData)))
			continue;
		
		Unity::Component* component = &go.GetComponentAtIndex(i);
		if (getOnlyOne)
		{
			*((Unity::Component**) output) = component;
			return true;
		} 

		dynamic_array<Unity::Component*>* manyResults = (dynamic_array<Unity::Component*>*) output;
		if (manyResults->empty())
			manyResults->reserve(10);
		manyResults->push_back(component);
		foundAtLeastOne = true;
	}
	
	return foundAtLeastOne;
}

template<bool getOnlyOne, int searchMethod>
static bool GetComponentsImplementationRecurse (GameObject& go, bool includeInactive, void* compareData, void* output)
{	
	bool foundAtLeastOne = GetComponentsImplementation<getOnlyOne, searchMethod>(go,includeInactive,compareData,output);
	if (foundAtLeastOne && getOnlyOne)
		return true;
	
	// Recurse Transform hierarchy
	Transform& transform = go.GetComponentT<Transform> (ClassID (Transform));
	int count = transform.GetChildrenCount();
	for (int i = 0; i < count; i++)
	{
		Transform& child = transform.GetChild(i);
		foundAtLeastOne = GetComponentsImplementationRecurse<getOnlyOne,searchMethod>(child.GetGameObject(), includeInactive, compareData,output);
		if (foundAtLeastOne && getOnlyOne)
			return true;
	}
	return false;
}

#if MONO_QUALITY_ERRORS
static bool ScriptingClassIsValidGetComponentArgument(ScriptingClass* compareClass)
{
	if (mono_class_is_subclass_of(compareClass, GetMonoManager().GetCommonClasses().component, true))
		return true;

	if (mono_unity_class_is_interface(compareClass))
		return true;
	
	return false;
}


static void VerifyGetComponentsOfTypeFromGameObjectArgument(GameObject& go, ScriptingClassPtr compareKlass)
{
	if (compareKlass == NULL)
	{
		ScriptWarning("GetComponent asking for invalid type", &go);
		return;
	}

	if (ScriptingClassIsValidGetComponentArgument(compareKlass))
		return;

	const char* klassName = mono_class_get_name (compareKlass);
	ScriptWarning(Format("GetComponent requires that the requested component '%s' derives from MonoBehaviour or Component or is an interface.", klassName), &go);
}
#endif

static void DetermineSearchMethod(ScriptingClassPtr klass, int* outputSearchMethod, void** outputCompareData)
{
	int classid = GetScriptingManager().ClassIDForScriptingClass(klass);
	if (classid == -1)
	{
		*outputSearchMethod =  SearchMethod::ByScriptingClass;
		*outputCompareData = (void*)klass;
		return;	
	}

	*outputCompareData = (void*) classid;
	*outputSearchMethod = SearchMethod::ByClassID;
}

typedef bool GetComponentsFunction (GameObject& go, bool includeInactive, void* compareData, void* output);

template<bool getOnlyOne>
static GetComponentsFunction* DetermineGetComponentsImplementationFunction(int searchMethod,bool recursive)
{
	if (searchMethod == SearchMethod::ByClassID && recursive)
		return GetComponentsImplementationRecurse<getOnlyOne,SearchMethod::ByClassID>;

	if (searchMethod == SearchMethod::ByClassID && !recursive)
		return GetComponentsImplementation<getOnlyOne,SearchMethod::ByClassID>;

	if (searchMethod == SearchMethod::ByScriptingClass && recursive)
		return GetComponentsImplementationRecurse<getOnlyOne,SearchMethod::ByScriptingClass>;

	if (searchMethod == SearchMethod::ByScriptingClass && !recursive)
		return GetComponentsImplementation<getOnlyOne,SearchMethod::ByScriptingClass>;

	if (searchMethod == SearchMethod::DontEvenStart)
		return NULL;

	return NULL;
}

template<bool getOnlyOne>
static void GetComponentsOfTypeFromGameObject(GameObject& go, ScriptingClassPtr compareKlass, bool generateErrors, bool recursive, bool includeInactive, void* results)
{
#if MONO_QUALITY_ERRORS
	if (generateErrors)
		VerifyGetComponentsOfTypeFromGameObjectArgument(go,compareKlass);	
#endif	

	if (compareKlass == SCRIPTING_NULL)
		return;
	
	int searchMethod;
	void* compareData;

	DetermineSearchMethod(compareKlass,&searchMethod,&compareData);

	GetComponentsFunction* getComponents = DetermineGetComponentsImplementationFunction<getOnlyOne>(searchMethod,recursive);
	if (getComponents)
		getComponents(go,includeInactive,compareData,results);
}

ScriptingArrayPtr ScriptingGetComponentsOfType (GameObject& go, ScriptingObjectPtr systemTypeInstance, bool useSearchTypeAsArrayReturnType, bool recursive, bool includeInactive)
{
	dynamic_array<Unity::Component*> components(kMemTempAlloc);	
	ScriptingClassPtr compareKlass = GetScriptingTypeRegistry().GetType (systemTypeInstance);
	GetComponentsOfTypeFromGameObject<false>(go,compareKlass, true, recursive, includeInactive, &components);

#if ENABLE_MONO || UNITY_WINRT
	ScriptingClassPtr componentClass = GetMonoManager ().ClassIDToScriptingClass (ClassID (Component));
	ScriptingClassPtr classForArray = useSearchTypeAsArrayReturnType ? compareKlass : componentClass;
	// ToDo: why aren't we using useSearchTypeAsArrayReturnType for flash ?! Lucas, Ralph?
#elif UNITY_FLASH
	ScriptingClassPtr classForArray = compareKlass;
#endif
	return CreateScriptingArrayFromUnityObjects(components,classForArray);
}

ScriptingObjectPtr ScriptingGetComponentOfType (GameObject& go, ScriptingObjectPtr systemTypeInstance, bool generateErrors)
{
	if (systemTypeInstance == SCRIPTING_NULL)
	{
		Scripting::RaiseArgumentException ("Type can not be null.");
		return SCRIPTING_NULL;
	}

	ScriptingClassPtr compareKlass = GetScriptingTypeRegistry().GetType (systemTypeInstance);
	Unity::Component* result = NULL;
	
	GetComponentsOfTypeFromGameObject<true>(go,compareKlass, generateErrors, false, true, &result);

	if (result != NULL)
		return Scripting::ScriptingWrapperFor(result);
	
#if MONO_QUALITY_ERRORS
	if(generateErrors)
		return MonoObjectNULL(compareKlass, MissingComponentString(go,compareKlass));
#endif

	return SCRIPTING_NULL;
}

ScriptingObjectPtr ScriptingGetComponentOfType (GameObject& go, ScriptingClassPtr systemTypeInstance)
{
	Unity::Component* result = NULL;
	GetComponentsOfTypeFromGameObject<true>(go, systemTypeInstance, false, false, true, &result);
	if (result) 
		return Scripting::ScriptingWrapperFor(result);

	return SCRIPTING_NULL;
}


#endif