Manual_Resource.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. //
  2. // Copyright (c) 2008-2022 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/APITemplates.h"
  24. #include "../IO/PackageFile.h"
  25. #include "../Resource/Image.h"
  26. #include "../Resource/ResourceCache.h"
  27. namespace Urho3D
  28. {
  29. // bool Resource::Load(Deserializer& source) | File: ../Resource/Resource.h
  30. template <class T> bool Resource_Load_File(File* file, T* ptr)
  31. {
  32. return file && ptr->Load(*file);
  33. }
  34. // bool Resource::Load(Deserializer& source) | File: ../Resource/Resource.h
  35. template <class T> bool Resource_Load_VectorBuffer(VectorBuffer& buffer, T* ptr)
  36. {
  37. return ptr->Load(buffer);
  38. }
  39. // virtual bool Resource::Save(Serializer& dest) const | File: ../Resource/Resource.h
  40. template <class T> bool Resource_Save_File(File* file, T* ptr)
  41. {
  42. return file && ptr->Save(*file);
  43. }
  44. // virtual bool Resource::Save(Serializer& dest) const | File: ../Resource/Resource.h
  45. template <class T> bool Resource_Save_VectorBuffer(VectorBuffer& buffer, T* ptr)
  46. {
  47. return ptr->Save(buffer);
  48. }
  49. #define REGISTER_MEMBERS_MANUAL_PART_Resource() \
  50. /* bool Resource::Load(Deserializer& source) | File: ../Resource/Resource.h */ \
  51. engine->RegisterObjectMethod(className, "bool Load(File@+)", AS_FUNCTION_OBJLAST(Resource_Load_File<T>), AS_CALL_CDECL_OBJLAST); \
  52. engine->RegisterObjectMethod(className, "bool Load(VectorBuffer&)", AS_FUNCTION_OBJLAST(Resource_Load_VectorBuffer<T>), AS_CALL_CDECL_OBJLAST); \
  53. \
  54. /* virtual bool Resource::Save(Serializer& dest) const | File: ../Resource/Resource.h */ \
  55. engine->RegisterObjectMethod(className, "bool Save(File@+) const", AS_FUNCTION_OBJLAST(Resource_Save_File<T>), AS_CALL_CDECL_OBJLAST); \
  56. engine->RegisterObjectMethod(className, "bool Save(VectorBuffer&) const", AS_FUNCTION_OBJLAST(Resource_Save_VectorBuffer<T>), AS_CALL_CDECL_OBJLAST);
  57. // ========================================================================================
  58. // bool XPathQuery::SetQuery(const String& queryString, const String& variableString = String::EMPTY, bool bind = true) | File: ../Resource/XMLElement.h
  59. template <class T> bool XPathQuery_SetQuery(const String& queryString, T* ptr)
  60. {
  61. return ptr->SetQuery(queryString);
  62. }
  63. #define REGISTER_MEMBERS_MANUAL_PART_XPathQuery() \
  64. /* bool XPathQuery::SetQuery(const String& queryString, const String& variableString = String::EMPTY, bool bind = true) | File: ../Resource/XMLElement.h */ \
  65. engine->RegisterObjectMethod(className, "void set_query(const String&)", AS_FUNCTION_OBJLAST(XPathQuery_SetQuery<T>), AS_CALL_CDECL_OBJLAST);
  66. // ========================================================================================
  67. // XMLElement XMLFile::GetRoot(const String& name = String::EMPTY) | File: ../Resource/XMLFile.h
  68. template <class T> XMLElement XMLFile_GetRoot_Default(T* ptr)
  69. {
  70. return ptr->GetRoot();
  71. }
  72. #define REGISTER_MEMBERS_MANUAL_PART_XMLFile() \
  73. /* XMLElement XMLFile::GetRoot(const String& name = String::EMPTY) | File: ../Resource/XMLFile.h */ \
  74. engine->RegisterObjectMethod(className, "XMLElement get_root()", AS_FUNCTION_OBJLAST(XMLFile_GetRoot_Default<T>), AS_CALL_CDECL_OBJLAST);
  75. // ========================================================================================
  76. void ArrayToVariantVector(CScriptArray* arr, VariantVector& dest);
  77. // XMLElement XMLElement::SelectSingle(const String& query, pugi::xpath_variable_set* variables = nullptr) const | File: ../Resource/XMLElement.h
  78. template <class T> XMLElement XMLElement_SelectSingle(const String& query, T* ptr)
  79. {
  80. return ptr->SelectSingle(query);
  81. }
  82. // XPathResultSet XMLElement::Select(const String& query, pugi::xpath_variable_set* variables = nullptr) const | File: ../Resource/XMLElement.h
  83. template <class T> XPathResultSet XMLElement_Select(const String& query, T* ptr)
  84. {
  85. return ptr->Select(query);
  86. }
  87. // bool XMLElement::SetVariantVector(const VariantVector& value) | File: ../Resource/XMLElement.h
  88. template <class T> bool XMLElement_SetVariantVector(CScriptArray* value, T* ptr)
  89. {
  90. VariantVector src;
  91. ArrayToVariantVector(value, src);
  92. return ptr->SetVariantVector(src);
  93. }
  94. // VariantVector XMLElement::GetVariantVector() const | File: ../Resource/XMLElement.h
  95. template <class T> CScriptArray* XMLElement_GetVariantVector(T* ptr)
  96. {
  97. return VectorToArray<Variant>(ptr->GetVariantVector(), "Array<Variant>");
  98. }
  99. #define REGISTER_MEMBERS_MANUAL_PART_XMLElement() \
  100. /* XMLElement XMLElement::SelectSingle(const String& query, pugi::xpath_variable_set* variables = nullptr) const | File: ../Resource/XMLElement.h */ \
  101. engine->RegisterObjectMethod(className, "XMLElement SelectSingle(const String&in)", AS_FUNCTION_OBJLAST(XMLElement_SelectSingle<T>), AS_CALL_CDECL_OBJLAST); \
  102. \
  103. /* XPathResultSet XMLElement::Select(const String& query, pugi::xpath_variable_set* variables = nullptr) const | File: ../Resource/XMLElement.h */ \
  104. engine->RegisterObjectMethod(className, "XPathResultSet Select(const String&in)", AS_FUNCTION_OBJLAST(XMLElement_Select<T>), AS_CALL_CDECL_OBJLAST); \
  105. \
  106. /* bool XMLElement::SetVariantVector(const VariantVector& value) | File: ../Resource/XMLElement.h */ \
  107. engine->RegisterObjectMethod(className, "bool SetVariantVector(Array<Variant>@+)", AS_FUNCTION_OBJLAST(XMLElement_SetVariantVector<T>), AS_CALL_CDECL_OBJLAST); \
  108. \
  109. /* VariantVector XMLElement::GetVariantVector() const | File: ../Resource/XMLElement.h */ \
  110. engine->RegisterObjectMethod(className, "Array<Variant>@ GetVariantVector() const", AS_FUNCTION_OBJLAST(XMLElement_GetVariantVector<T>), AS_CALL_CDECL_OBJLAST);
  111. // ========================================================================================
  112. // void ResourceCache::GetResources(PODVector<Resource*>& result, StringHash type) const | File: ../Resource/ResourceCache.h
  113. template <class T> CScriptArray* ResourceCache_GetResources(StringHash type, T* ptr)
  114. {
  115. PODVector<Resource*> resources;
  116. ptr->GetResources(resources, type);
  117. return VectorToHandleArray(resources, "Array<Resource@>");
  118. }
  119. #define REGISTER_MEMBERS_MANUAL_PART_ResourceCache() \
  120. /* void ResourceCache::GetResources(PODVector<Resource*>& result, StringHash type) const | File: ../Resource/ResourceCache.h */ \
  121. engine->RegisterObjectMethod(className, "Array<Resource@>@ GetResources(StringHash)", AS_FUNCTION_OBJLAST(ResourceCache_GetResources<T>), AS_CALL_CDECL_OBJLAST);
  122. // ========================================================================================
  123. template <class T> CScriptArray* JSONValue_GetKeys(const T& jsonValue)
  124. {
  125. Vector<String> keys;
  126. if (jsonValue.IsObject())
  127. {
  128. for (ConstJSONObjectIterator i = jsonValue.Begin(); i != jsonValue.End(); ++i)
  129. keys.Push(i->first_);
  130. }
  131. return VectorToArray<String>(keys, "Array<String>");
  132. }
  133. template <class T> CScriptArray* JSONValue_GetValues(const T& jsonValue)
  134. {
  135. if (jsonValue.IsArray())
  136. {
  137. return VectorToArray<JSONValue>(jsonValue.GetArray(), "Array<JSONValue>");
  138. }
  139. else
  140. {
  141. Vector<JSONValue> values;
  142. if (jsonValue.IsObject())
  143. {
  144. for (ConstJSONObjectIterator i = jsonValue.Begin(); i != jsonValue.End(); ++i)
  145. values.Push(i->second_);
  146. }
  147. return VectorToArray<JSONValue>(values, "Array<JSONValue>");
  148. }
  149. }
  150. #define REGISTER_MEMBERS_MANUAL_PART_JSONValue() \
  151. engine->RegisterObjectMethod(className, "Array<String>@ get_keys() const", AS_FUNCTION_OBJLAST(JSONValue_GetKeys<T>), AS_CALL_CDECL_OBJLAST); \
  152. engine->RegisterObjectMethod(className, "Array<JSONValue>@ get_values() const", AS_FUNCTION_OBJLAST(JSONValue_GetValues<T>), AS_CALL_CDECL_OBJLAST);
  153. }