Manual_Resource.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #include "../Precompiled.h"
  23. #include "../AngelScript/APITemplates.h"
  24. #include "../AngelScript/Manual_Resource.h"
  25. #include "../Resource/ResourceCache.h"
  26. #include "../Resource/Localization.h"
  27. namespace Urho3D
  28. {
  29. // This function is called before ASRegisterGenerated()
  30. void ASRegisterManualFirst_Resource(asIScriptEngine* engine)
  31. {
  32. }
  33. // ========================================================================================
  34. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  35. static ResourceCache* GetResourceCache()
  36. {
  37. return GetScriptContext()->GetSubsystem<ResourceCache>();
  38. }
  39. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  40. static Localization* GetLocalization()
  41. {
  42. return GetScriptContext()->GetSubsystem<Localization>();
  43. }
  44. // This function is called after ASRegisterGenerated()
  45. void ASRegisterManualLast_Resource(asIScriptEngine* engine)
  46. {
  47. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  48. engine->RegisterGlobalFunction("Localization@+ get_localization()", asFUNCTION(GetLocalization), asCALL_CDECL);
  49. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  50. engine->RegisterGlobalFunction("ResourceCache@+ get_resourceCache()", asFUNCTION(GetResourceCache), asCALL_CDECL);
  51. // template<class T> T * Object::GetSubsystem() const | File: ../Core/Object.h
  52. engine->RegisterGlobalFunction("ResourceCache@+ get_cache()", asFUNCTION(GetResourceCache), asCALL_CDECL);
  53. }
  54. // ========================================================================================
  55. // bool Resource::Load(Deserializer &source) | File: ../Resource/Resource.h
  56. bool ResourceLoad(File* file, Resource* ptr)
  57. {
  58. return file && ptr->Load(*file);
  59. }
  60. // bool Resource::Load(Deserializer &source) | File: ../Resource/Resource.h
  61. bool ResourceLoadVectorBuffer(VectorBuffer& buffer, Resource* ptr)
  62. {
  63. return ptr->Load(buffer);
  64. }
  65. // virtual bool Resource::Save(Serializer &dest) const | File: ../Resource/Resource.h
  66. bool ResourceSave(File* file, Resource* ptr)
  67. {
  68. return file && ptr->Save(*file);
  69. }
  70. // virtual bool Resource::Save(Serializer &dest) const | File: ../Resource/Resource.h
  71. bool ResourceSaveVectorBuffer(VectorBuffer& buffer, Resource* ptr)
  72. {
  73. return ptr->Save(buffer);
  74. }
  75. // ========================================================================================
  76. // XMLElement XMLFile::GetRoot(const String &name=String::EMPTY) | File: ../Resource/XMLFile.h
  77. XMLElement XMLFileGetRootDefault(XMLFile* ptr)
  78. {
  79. return ptr->GetRoot();
  80. }
  81. // ========================================================================================
  82. void ArrayToVariantVector(CScriptArray* arr, VariantVector& dest);
  83. // bool XMLElement::SetVariantVector(const VariantVector &value) | File: ../Resource/XMLElement.h
  84. bool XMLElementSetVariantVector(CScriptArray* value, XMLElement* ptr)
  85. {
  86. VariantVector src;
  87. ArrayToVariantVector(value, src);
  88. return ptr->SetVariantVector(src);
  89. }
  90. // VariantVector XMLElement::GetVariantVector() const | File: ../Resource/XMLElement.h
  91. CScriptArray* XMLElementGetVariantVector(XMLElement* ptr)
  92. {
  93. return VectorToArray<Variant>(ptr->GetVariantVector(), "Array<Variant>");
  94. }
  95. // ========================================================================================
  96. // void ResourceCache::GetResources(PODVector< Resource * > &result, StringHash type) const | File: ../Resource/ResourceCache.h
  97. CScriptArray* ResourceCacheGetResources(StringHash type, ResourceCache* ptr)
  98. {
  99. PODVector<Resource*> resources;
  100. ptr->GetResources(resources, type);
  101. return VectorToHandleArray(resources, "Array<Resource@>");
  102. }
  103. // ========================================================================================
  104. // void JSONValue::SetVariant(const Variant &variant, Context *context=nullptr) | File: ../Resource/JSONValue.h
  105. void JSONValueSetVariant(const Variant& variant, JSONValue& jsonValue)
  106. {
  107. jsonValue.SetVariant(variant, GetScriptContext());
  108. }
  109. // void JSONValue::SetVariantMap(const VariantMap &variantMap, Context *context=nullptr) | File: ../Resource/JSONValue.h
  110. void JSONValueSetVariantMap(const VariantMap& variantMap, JSONValue& jsonValue)
  111. {
  112. jsonValue.SetVariantMap(variantMap, GetScriptContext());
  113. }
  114. CScriptArray* JSONValueGetKeys(const JSONValue& jsonValue)
  115. {
  116. Vector<String> keys;
  117. if (jsonValue.IsObject())
  118. {
  119. for (ConstJSONObjectIterator i = jsonValue.Begin(); i != jsonValue.End(); ++i)
  120. keys.Push(i->first_);
  121. }
  122. return VectorToArray<String>(keys, "Array<String>");
  123. }
  124. CScriptArray* JSONValueGetValues(const JSONValue& jsonValue)
  125. {
  126. if (jsonValue.IsArray())
  127. return VectorToArray<JSONValue>(jsonValue.GetArray(), "Array<JSONValue>");
  128. else
  129. {
  130. Vector<JSONValue> values;
  131. if (jsonValue.IsObject())
  132. {
  133. for (ConstJSONObjectIterator i = jsonValue.Begin(); i != jsonValue.End(); ++i)
  134. values.Push(i->second_);
  135. }
  136. return VectorToArray<JSONValue>(values, "Array<JSONValue>");
  137. }
  138. }
  139. }