BsScriptObject.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. Type* nativeInstance = nullptr;
  44. metaData.thisPtrField->getValue(managedInstance, &nativeInstance);
  45. return nativeInstance;
  46. }
  47. static const ScriptMeta* getMetaData() { return &metaData; }
  48. protected:
  49. static ScriptMeta metaData;
  50. MonoObject* mManagedInstance;
  51. void createInstance(MonoObject* instance)
  52. {
  53. if(mManagedInstance != nullptr)
  54. CM_EXCEPT(InvalidStateException, "Trying to instantiate an already instantiated script object.");
  55. mManagedInstance = instance;
  56. }
  57. template <class Type2>
  58. static void throwIfInstancesDontMatch(ScriptObject<Type2>* lhs, void* rhs)
  59. {
  60. #if CM_DEBUG_MODE
  61. if((lhs == nullptr && rhs != nullptr) || (rhs == nullptr && lhs != nullptr) || lhs->getNativeRaw() != rhs)
  62. {
  63. CM_EXCEPT(InvalidStateException, "Native and script instance do not match. This usually happens when you modify a native object " \
  64. " that is also being referenced from script code. You should only modify such objects directly from script code.");
  65. }
  66. #endif
  67. }
  68. private:
  69. static InitScriptObjectOnStart<Type> initOnStart;
  70. };
  71. template <typename Type>
  72. InitScriptObjectOnStart<Type> ScriptObject<Type>::initOnStart;
  73. template <typename Type>
  74. ScriptMeta ScriptObject<Type>::metaData;
  75. }