BsScriptGameObject.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Wrappers/BsScriptGameObject.h"
  4. #include "BsMonoUtil.h"
  5. #include <assert.h>
  6. namespace bs
  7. {
  8. ScriptGameObjectBase::ScriptGameObjectBase(MonoObject* instance)
  9. :PersistentScriptObjectBase(instance), mRefreshInProgress(false), mGCHandle(0)
  10. {
  11. }
  12. ScriptGameObjectBase::~ScriptGameObjectBase()
  13. {
  14. BS_ASSERT(mGCHandle == 0 && "Object being destroyed without its managed instance being freed first.");
  15. }
  16. ScriptObjectBackup ScriptGameObjectBase::beginRefresh()
  17. {
  18. mRefreshInProgress = true;
  19. return PersistentScriptObjectBase::beginRefresh();
  20. }
  21. void ScriptGameObjectBase::endRefresh(const ScriptObjectBackup& backupData)
  22. {
  23. mRefreshInProgress = false;
  24. PersistentScriptObjectBase::endRefresh(backupData);
  25. }
  26. MonoObject* ScriptGameObjectBase::getManagedInstance() const
  27. {
  28. return MonoUtil::getObjectFromGCHandle(mGCHandle);
  29. }
  30. void ScriptGameObjectBase::setManagedInstance(::MonoObject* instance)
  31. {
  32. BS_ASSERT(mGCHandle == 0 && "Attempting to set a new managed instance without freeing the old one.");
  33. mGCHandle = MonoUtil::newGCHandle(instance, false);
  34. }
  35. void ScriptGameObjectBase::freeManagedInstance()
  36. {
  37. if (mGCHandle != 0)
  38. {
  39. MonoUtil::freeGCHandle(mGCHandle);
  40. mGCHandle = 0;
  41. }
  42. }
  43. ScriptGameObject::ScriptGameObject(MonoObject* instance)
  44. :ScriptObject(instance)
  45. { }
  46. void ScriptGameObject::initRuntimeData()
  47. {
  48. metaData.scriptClass->addInternalCall("Internal_GetInstanceId", (void*)&ScriptGameObject::internal_getInstanceId);
  49. }
  50. UINT64 ScriptGameObject::internal_getInstanceId(ScriptGameObject* nativeInstance)
  51. {
  52. return nativeInstance->getNativeHandle().getInstanceId();
  53. }
  54. }