BsScriptRRefBase.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsScriptRRefBase.h"
  4. #include "BsScriptMeta.h"
  5. #include "BsMonoClass.h"
  6. #include "BsMonoUtil.h"
  7. #include "Resources/BsResources.h"
  8. #include "Wrappers/BsScriptResource.h"
  9. #include "BsScriptResourceManager.h"
  10. #include "BsApplication.h"
  11. #include "Serialization/BsScriptAssemblyManager.h"
  12. namespace bs
  13. {
  14. ScriptRRefBase::ScriptRRefBase(MonoObject* instance, ResourceHandle<Resource> resource)
  15. :ScriptObject(instance), mResource(std::move(resource)), mGCHandle(MonoUtil::newGCHandle(instance))
  16. { }
  17. ScriptRRefBase::~ScriptRRefBase()
  18. {
  19. BS_ASSERT(mGCHandle == 0 && "Object being destroyed without its managed instance being freed first.");
  20. }
  21. void ScriptRRefBase::initRuntimeData()
  22. {
  23. metaData.scriptClass->addInternalCall("Internal_IsLoaded", (void*)&ScriptRRefBase::internal_IsLoaded);
  24. metaData.scriptClass->addInternalCall("Internal_GetResource", (void*)&ScriptRRefBase::internal_GetResource);
  25. metaData.scriptClass->addInternalCall("Internal_GetUUID", (void*)&ScriptRRefBase::internal_GetUUID);
  26. metaData.scriptClass->addInternalCall("Internal_CastAs", (void*)&ScriptRRefBase::internal_CastAs);
  27. }
  28. ScriptRRefBase* ScriptRRefBase::createInternal(const ResourceHandle<Resource>& handle, ::MonoClass* rawType)
  29. {
  30. MonoClass* type = nullptr;
  31. if(rawType == nullptr)
  32. type = metaData.scriptClass;
  33. else
  34. {
  35. type = MonoManager::instance().findClass(rawType);
  36. if (type == nullptr)
  37. return nullptr;
  38. assert(type->isSubClassOf(metaData.scriptClass));
  39. }
  40. MonoObject* obj = type->createInstance();
  41. ScriptRRefBase* output = new (bs_alloc<ScriptRRefBase>()) ScriptRRefBase(obj, handle);
  42. return output;
  43. }
  44. MonoObject* ScriptRRefBase::getManagedInstance() const
  45. {
  46. return MonoUtil::getObjectFromGCHandle(mGCHandle);
  47. }
  48. void ScriptRRefBase::_clearManagedInstance()
  49. {
  50. if (mGCHandle != 0)
  51. {
  52. MonoUtil::freeGCHandle(mGCHandle);
  53. mGCHandle = 0;
  54. }
  55. }
  56. void ScriptRRefBase::_onManagedInstanceDeleted(bool assemblyRefresh)
  57. {
  58. if (mGCHandle != 0)
  59. {
  60. MonoUtil::freeGCHandle(mGCHandle);
  61. mGCHandle = 0;
  62. }
  63. }
  64. ::MonoClass* ScriptRRefBase::bindGenericParam(::MonoClass* param)
  65. {
  66. MonoClass* rrefClass = ScriptAssemblyManager::instance().getBuiltinClasses().genericRRefClass;
  67. ::MonoClass* params[1] = { param };
  68. return MonoUtil::bindGenericParameters(rrefClass->_getInternalClass(), params, 1);
  69. }
  70. bool ScriptRRefBase::internal_IsLoaded(ScriptRRefBase* thisPtr)
  71. {
  72. return thisPtr->mResource.isLoaded(false);
  73. }
  74. MonoObject* ScriptRRefBase::internal_GetResource(ScriptRRefBase* thisPtr)
  75. {
  76. ResourceLoadFlags loadFlags = ResourceLoadFlag::LoadDependencies | ResourceLoadFlag::KeepInternalRef;
  77. if (gApplication().isEditor())
  78. loadFlags |= ResourceLoadFlag::KeepSourceData;
  79. const HResource resource = gResources().loadFromUUID(thisPtr->getHandle().getUUID(), loadFlags);
  80. ScriptResourceBase* scriptResource = ScriptResourceManager::instance().getScriptResource(resource, true);
  81. return scriptResource->getManagedInstance();
  82. }
  83. void ScriptRRefBase::internal_GetUUID(ScriptRRefBase* thisPtr, UUID* uuid)
  84. {
  85. *uuid = thisPtr->getHandle().getUUID();
  86. }
  87. MonoObject* ScriptRRefBase::internal_CastAs(ScriptRRefBase* thisPtr, MonoReflectionType* type)
  88. {
  89. ::MonoClass* rawResType = MonoUtil::getClass(type);
  90. MonoClass* resType = MonoManager::instance().findClass(rawResType);
  91. if (resType == nullptr)
  92. return nullptr; // Not a valid type
  93. ::MonoClass* rrefType = nullptr;
  94. if(resType != ScriptResource::getMetaData()->scriptClass)
  95. {
  96. if (!resType->isSubClassOf(ScriptResource::getMetaData()->scriptClass))
  97. return nullptr; // Not a resource type
  98. rrefType = bindGenericParam(rawResType);
  99. }
  100. ScriptRRefBase* castRRefBase = create(thisPtr->mResource, rrefType);
  101. if(castRRefBase)
  102. return castRRefBase->getManagedInstance();
  103. return nullptr;
  104. }
  105. }