BsScriptObject.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #pragma once
  2. #include "BsScriptEnginePrerequisites.h"
  3. #include "BsScriptMeta.h"
  4. #include "BsMonoField.h"
  5. #include "CmException.h"
  6. #include <mono/jit/jit.h>
  7. namespace BansheeEngine
  8. {
  9. template <class Type>
  10. struct InitScriptObjectOnStart
  11. {
  12. public:
  13. InitScriptObjectOnStart()
  14. {
  15. Type::initMetaData();
  16. }
  17. void makeSureIAmInstantiated() { }
  18. };
  19. /**
  20. * @brief Base class for objects that can be extended using Mono scripting
  21. */
  22. template <class Type>
  23. class ScriptObject
  24. {
  25. public:
  26. ScriptObject()
  27. :mManagedInstance(nullptr)
  28. {
  29. // Compiler will only generate code for stuff that is directly used, including static data members,
  30. // so we fool it here like we're using the class directly. Otherwise compiler won't generate the code for the member
  31. // and our type won't get initialized on start (Actual behavior is a bit more random)
  32. initOnStart.makeSureIAmInstantiated();
  33. }
  34. virtual ~ScriptObject()
  35. {
  36. if(mManagedInstance != nullptr)
  37. CM_EXCEPT(InvalidStateException, "Script object is being destroyed without its instance previously being released.");
  38. }
  39. MonoObject* getManagedInstance() const { return mManagedInstance; }
  40. virtual void* getNativeRaw() const { return nullptr; }
  41. static Type* toNative(MonoObject* managedInstance)
  42. {
  43. return reinterpret_cast<Type*>(metaData.thisPtrField->getValue(managedInstance));
  44. }
  45. protected:
  46. static ScriptMeta metaData;
  47. MonoObject* mManagedInstance;
  48. void createInstance(MonoObject* instance)
  49. {
  50. if(mManagedInstance != nullptr)
  51. CM_EXCEPT(InvalidStateException, "Trying to instantiate an already instantiated script object.");
  52. mManagedInstance = instance;
  53. }
  54. void destroyInstance()
  55. {
  56. if(mManagedInstance == nullptr)
  57. return;
  58. mManagedInstance = nullptr;
  59. }
  60. template <class Type2>
  61. static void throwIfInstancesDontMatch(ScriptObject<Type2>* lhs, void* rhs)
  62. {
  63. #if CM_DEBUG_MODE
  64. if((lhs == nullptr && rhs != nullptr) || (rhs == nullptr && lhs != nullptr) || lhs->getNativeRaw() != rhs)
  65. {
  66. CM_EXCEPT(InvalidStateException, "Native and script instance do not match. This usually happens when you modify a native object " \
  67. " that is also being referenced from script code. You should only modify such objects directly from script code.");
  68. }
  69. #endif
  70. }
  71. private:
  72. static InitScriptObjectOnStart<Type> initOnStart;
  73. };
  74. template <typename Type>
  75. InitScriptObjectOnStart<Type> ScriptObject<Type>::initOnStart;
  76. template <typename Type>
  77. ScriptMeta ScriptObject<Type>::metaData;
  78. }