APITemplates.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../AngelScript/Addons.h"
  24. #include "../AngelScript/Script.h"
  25. #include "../AngelScript/ScriptInstance.h"
  26. #include "../Audio/SoundSource.h"
  27. #include "../Core/Context.h"
  28. #include "../Container/HashSet.h"
  29. #include "../Container/Sort.h"
  30. #include "../Graphics/Drawable.h"
  31. #include "../Graphics/StaticModel.h"
  32. #include "../Graphics/Texture.h"
  33. #include "../IO/File.h"
  34. #include "../IO/Log.h"
  35. #include "../IO/VectorBuffer.h"
  36. #include "../Resource/Resource.h"
  37. #include "../Scene/Animatable.h"
  38. #include "../Scene/Node.h"
  39. #include "../UI/BorderImage.h"
  40. #ifdef URHO3D_URHO2D
  41. #include "../Urho2D/Drawable2D.h"
  42. #endif
  43. #include <AngelScript/angelscript.h>
  44. #include <cstring>
  45. #ifdef _MSC_VER
  46. #pragma warning(push)
  47. #pragma warning(disable:4505)
  48. #endif
  49. namespace Urho3D
  50. {
  51. class Camera;
  52. /// Template function for Vector to array conversion.
  53. template <class T> CScriptArray* VectorToArray(const Vector<T>& vector, const char* arrayName)
  54. {
  55. Context* context = GetScriptContext();
  56. if (context)
  57. {
  58. asITypeInfo* type = context->GetSubsystem<Script>()->GetObjectType(arrayName);
  59. CScriptArray* arr = CScriptArray::Create(type, vector.Size());
  60. for (unsigned i = 0; i < arr->GetSize(); ++i)
  61. *(static_cast<T*>(arr->At(i))) = vector[i];
  62. return arr;
  63. }
  64. else
  65. return nullptr;
  66. }
  67. /// Template function for PODVector to array conversion.
  68. template <class T> CScriptArray* VectorToArray(const PODVector<T>& vector, const char* arrayName)
  69. {
  70. Context* context = GetScriptContext();
  71. if (context)
  72. {
  73. asITypeInfo* type = context->GetSubsystem<Script>()->GetObjectType(arrayName);
  74. CScriptArray* arr = CScriptArray::Create(type, vector.Size());
  75. for (unsigned i = 0; i < arr->GetSize(); ++i)
  76. *(static_cast<T*>(arr->At(i))) = vector[i];
  77. return arr;
  78. }
  79. else
  80. return nullptr;
  81. }
  82. /// Template function for data buffer to array conversion.
  83. template <class T> CScriptArray* BufferToArray(const T* buffer, unsigned size, const char* arrayName)
  84. {
  85. Context* context = GetScriptContext();
  86. if (context)
  87. {
  88. asITypeInfo* type = context->GetSubsystem<Script>()->GetObjectType(arrayName);
  89. CScriptArray* arr = CScriptArray::Create(type, size);
  90. for (unsigned i = 0; i < arr->GetSize(); ++i)
  91. *(static_cast<T*>(arr->At(i))) = buffer[i];
  92. return arr;
  93. }
  94. else
  95. return nullptr;
  96. }
  97. /// Template function for Vector to handle array conversion.
  98. template <class T> CScriptArray* VectorToHandleArray(const Vector<T*>& vector, const char* arrayName)
  99. {
  100. Context* context = GetScriptContext();
  101. if (context)
  102. {
  103. asITypeInfo* type = context->GetSubsystem<Script>()->GetObjectType(arrayName);
  104. CScriptArray* arr = CScriptArray::Create(type, vector.Size());
  105. for (unsigned i = 0; i < arr->GetSize(); ++i)
  106. {
  107. // Increment reference count for storing in the array
  108. T* ptr = vector[i];
  109. if (ptr)
  110. ptr->AddRef();
  111. *(static_cast<T**>(arr->At(i))) = ptr;
  112. }
  113. return arr;
  114. }
  115. else
  116. return nullptr;
  117. }
  118. /// Template function for PODVector to handle array conversion.
  119. template <class T> CScriptArray* VectorToHandleArray(const PODVector<T*>& vector, const char* arrayName)
  120. {
  121. Context* context = GetScriptContext();
  122. if (context)
  123. {
  124. asITypeInfo* type = context->GetSubsystem<Script>()->GetObjectType(arrayName);
  125. CScriptArray* arr = CScriptArray::Create(type, vector.Size());
  126. for (unsigned i = 0; i < arr->GetSize(); ++i)
  127. {
  128. // Increment reference count for storing in the array
  129. T* ptr = vector[i];
  130. if (ptr)
  131. ptr->AddRef();
  132. *(static_cast<T**>(arr->At(i))) = ptr;
  133. }
  134. return arr;
  135. }
  136. else
  137. return nullptr;
  138. }
  139. /// Template function for shared pointer Vector to handle array conversion.
  140. template <class T> CScriptArray* VectorToHandleArray(const Vector<SharedPtr<T> >& vector, const char* arrayName)
  141. {
  142. Context* context = GetScriptContext();
  143. if (!context)
  144. return nullptr;
  145. asITypeInfo* type = context->GetSubsystem<Script>()->GetObjectType(arrayName);
  146. CScriptArray* arr = CScriptArray::Create(type, vector.Size());
  147. for (unsigned i = 0; i < arr->GetSize(); ++i)
  148. {
  149. // Increment reference count for storing in the array
  150. T* ptr = vector[i];
  151. if (ptr)
  152. ptr->AddRef();
  153. *(static_cast<T**>(arr->At(i))) = ptr;
  154. }
  155. return arr;
  156. }
  157. /// Template function for array to Vector conversion.
  158. template <class T> Vector<T> ArrayToVector(CScriptArray* arr)
  159. {
  160. Vector<T> dest(arr ? arr->GetSize() : 0);
  161. if (arr)
  162. {
  163. for (unsigned i = 0; i < arr->GetSize(); ++i)
  164. dest[i] = *static_cast<T*>(arr->At(i));
  165. }
  166. return dest;
  167. }
  168. /// Template function for array to PODVector conversion.
  169. template <class T> PODVector<T> ArrayToPODVector(CScriptArray* arr)
  170. {
  171. PODVector<T> dest(arr ? arr->GetSize() : 0);
  172. if (arr)
  173. {
  174. for (unsigned i = 0; i < arr->GetSize(); ++i)
  175. dest[i] = *static_cast<T*>(arr->At(i));
  176. }
  177. return dest;
  178. }
  179. template <class T> Vector<SharedPtr<T> > HandleArrayToVector(CScriptArray* arr)
  180. {
  181. Vector<T*> rawPtrs = ArrayToVector<T*>(arr);
  182. Vector<SharedPtr<T> > result(rawPtrs.Size());
  183. for (unsigned i = 0; i < rawPtrs.Size(); ++i)
  184. result[i] = SharedPtr<T>(rawPtrs[i]);
  185. return result;
  186. }
  187. /// Template function for dynamic cast between two script classes.
  188. template <class A, class B> B* RefCast(A* a)
  189. {
  190. if (!a)
  191. return nullptr;
  192. B* b = dynamic_cast<B*>(a);
  193. return b;
  194. }
  195. /// Template function for registering implicit casts between base and subclass.
  196. // https://www.angelcode.com/angelscript/sdk/docs/manual/doc_adv_class_hierarchy.html
  197. template <class BaseType, class DerivedType> void RegisterSubclass(asIScriptEngine* engine, const char* baseClassName, const char* derivedClassName)
  198. {
  199. if (!strcmp(baseClassName, derivedClassName))
  200. return;
  201. String declReturnBase(String(baseClassName) + "@+ opImplCast()");
  202. engine->RegisterObjectMethod(derivedClassName, declReturnBase.CString(), asFUNCTION((RefCast<DerivedType, BaseType>)), asCALL_CDECL_OBJLAST);
  203. String declReturnBaseConst("const " + String(baseClassName) + "@+ opImplCast() const");
  204. engine->RegisterObjectMethod(derivedClassName, declReturnBaseConst.CString(), asFUNCTION((RefCast<DerivedType, BaseType>)), asCALL_CDECL_OBJLAST);
  205. // TODO fix all scripts to "cast(derivedClass)"
  206. //String declReturnDerived(String(derivedClassName) + "@+ opCast()");
  207. String declReturnDerived(String(derivedClassName) + "@+ opImplCast()");
  208. engine->RegisterObjectMethod(baseClassName, declReturnDerived.CString(), asFUNCTION((RefCast<BaseType, DerivedType>)), asCALL_CDECL_OBJLAST);
  209. //String declReturnDerivedConst("const " + String(derivedClassName) + "@+ opCast() const");
  210. String declReturnDerivedConst("const " + String(derivedClassName) + "@+ opImplCast() const");
  211. engine->RegisterObjectMethod(baseClassName, declReturnDerivedConst.CString(), asFUNCTION((RefCast<BaseType, DerivedType>)), asCALL_CDECL_OBJLAST);
  212. }
  213. // ================================================================================
  214. // TODO Incorrect
  215. // Template function for dynamic cast between two script value classes.
  216. template <class SrcClass, class DstClass> DstClass* ValCast(SrcClass* src)
  217. {
  218. if (!src)
  219. return nullptr;
  220. return dynamic_cast<DstClass*>(src);
  221. }
  222. template <class baseClass, class subclass> void RegisterSubclassValue(asIScriptEngine* engine, const char* baseClassName, const char* subclassName)
  223. {
  224. assert(!strcmp(baseClassName, subclassName));
  225. String decl(String(baseClassName) + " opImplConvCast() const");
  226. engine->RegisterObjectMethod(subclassName, decl.CString(), asFUNCTION((ValCast<subclass, baseClass>)), asCALL_CDECL_OBJLAST);
  227. }
  228. // ================================================================================
  229. // https://en.cppreference.com/w/cpp/types/enable_if
  230. // We need detect implicitly-declared assign operator on complie time because Doxygen not povide this information
  231. template<typename classType, typename std::enable_if<std::is_copy_assignable<classType>::value, int>::type = 0>
  232. void RegisterImplicitlyDeclaredAssignOperatorIfPossible(asIScriptEngine* engine, const String& className)
  233. {
  234. String decl = className + "& opAssign(const " + className + "&in)";
  235. engine->RegisterObjectMethod(className.CString(), decl.CString(), asMETHODPR(classType, operator=, (const classType&), classType&), asCALL_THISCALL);
  236. }
  237. // This empty function need to prevent build errors when classType have no assing operator
  238. template<typename classType, typename std::enable_if<!std::is_copy_assignable<classType>::value, int>::type = 0>
  239. void RegisterImplicitlyDeclaredAssignOperatorIfPossible(asIScriptEngine* engine, const String& className)
  240. {
  241. }
  242. // ================================================================================
  243. static const AttributeInfo noAttributeInfoOLD{};
  244. // To keep Xcode LLVM/Clang happy - it erroneously warns on unused functions defined below which are actually being referenced in the code
  245. #if __clang__
  246. #pragma clang diagnostic push
  247. #pragma clang diagnostic ignored "-Wunused-function"
  248. #endif
  249. static const AttributeInfo& SerializableGetAttributeInfoOLD(unsigned index, Serializable* ptr)
  250. {
  251. const Vector<AttributeInfo>* attributes = ptr->GetAttributes();
  252. if (!attributes || index >= attributes->Size())
  253. {
  254. GetActiveASContext()->SetException("Index out of bounds");
  255. return noAttributeInfoOLD;
  256. }
  257. else
  258. return attributes->At(index);
  259. }
  260. // ================================================================================
  261. // bool Resource::Load(Deserializer &source) | File: ../Resource/Resource.h
  262. static bool ResourceLoadOLD(File* file, Resource* ptr)
  263. {
  264. return file && ptr->Load(*file);
  265. }
  266. // bool Resource::Load(Deserializer &source) | File: ../Resource/Resource.h
  267. static bool ResourceLoadVectorBufferOLD(VectorBuffer& buffer, Resource* ptr)
  268. {
  269. return ptr->Load(buffer);
  270. }
  271. // bool Resource::LoadFile(const String &fileName) | File: ../Resource/Resource.h
  272. static bool ResourceLoadByNameOLD(const String& fileName, Resource* ptr)
  273. {
  274. return ptr->LoadFile(fileName);
  275. }
  276. // virtual bool Resource::Save(Serializer &dest) const | File: ../Resource/Resource.h
  277. static bool ResourceSaveOLD(File* file, Resource* ptr)
  278. {
  279. return file && ptr->Save(*file);
  280. }
  281. // virtual bool Resource::Save(Serializer &dest) const | File: ../Resource/Resource.h
  282. static bool ResourceSaveVectorBufferOLD(VectorBuffer& buffer, Resource* ptr)
  283. {
  284. return ptr->Save(buffer);
  285. }
  286. // virtual bool Resource::SaveFile(const String &fileName) const | File: ../Resource/Resource.h
  287. static bool ResourceSaveByNameOLD(const String& fileName, Resource* ptr)
  288. {
  289. return ptr->SaveFile(fileName);
  290. }
  291. // ================================================================================
  292. template <class T> T* ConstructObject()
  293. {
  294. auto* object = new T(GetScriptContext());
  295. object->AddRef();
  296. return object;
  297. }
  298. template <class T> T* ConstructNamedObject(const String& name)
  299. {
  300. auto* object = new T(GetScriptContext());
  301. object->AddRef();
  302. object->SetName(name);
  303. return object;
  304. }
  305. /// Template function for registering a default constructor for a class derived from Object.
  306. template <class T> void RegisterObjectConstructor(asIScriptEngine* engine, const char* className)
  307. {
  308. String declFactory(String(className) + "@ f()");
  309. engine->RegisterObjectBehaviour(className, asBEHAVE_FACTORY, declFactory.CString(), asFUNCTION(ConstructObject<T>), asCALL_CDECL);
  310. }
  311. /// Template function for registering a named constructor for a class derived from Object.
  312. template <class T> void RegisterNamedObjectConstructor(asIScriptEngine* engine, const char* className)
  313. {
  314. String declFactoryWithName(String(className) + "@ f(const String&in)");
  315. engine->RegisterObjectBehaviour(className, asBEHAVE_FACTORY, declFactoryWithName.CString(), asFUNCTION(ConstructNamedObject<T>), asCALL_CDECL);
  316. }
  317. static bool SerializableLoad(File* file, Serializable* ptr)
  318. {
  319. return file && ptr->Load(*file);
  320. }
  321. static bool SerializableLoadVectorBuffer(VectorBuffer& buffer, Serializable* ptr)
  322. {
  323. return ptr->Load(buffer);
  324. }
  325. static bool SerializableSave(File* file, Serializable* ptr)
  326. {
  327. return file && ptr->Save(*file);
  328. }
  329. static bool SerializableSaveVectorBuffer(VectorBuffer& buffer, Serializable* ptr)
  330. {
  331. return ptr->Save(buffer);
  332. }
  333. #if __clang__
  334. #pragma clang diagnostic pop
  335. #endif
  336. }
  337. #ifdef _MSC_VER
  338. #pragma warning(pop)
  339. #endif