BsScriptObject.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #pragma once
  2. #include "BsScriptEnginePrerequisites.h"
  3. #include "BsScriptMeta.h"
  4. #include "CmException.h"
  5. #include <mono/jit/jit.h>
  6. namespace BansheeEngine
  7. {
  8. template <class Type>
  9. struct InitScriptObjectOnStart
  10. {
  11. public:
  12. InitScriptObjectOnStart()
  13. {
  14. ScriptObject<Type>::_initMetaData();
  15. }
  16. void makeSureIAmInstantiated() { }
  17. };
  18. class BS_SCR_BE_EXPORT ScriptObjectBase
  19. {
  20. public:
  21. ScriptObjectBase(MonoObject* instance);
  22. virtual ~ScriptObjectBase();
  23. static const ScriptMeta* getMetaData() { return &metaData; }
  24. MonoObject* getManagedInstance() const { return mManagedInstance; }
  25. virtual void* getNativeRaw() const { return nullptr; }
  26. virtual void _onManagedInstanceDeleted();
  27. protected:
  28. static ScriptMeta metaData;
  29. MonoObject* mManagedInstance;
  30. static void registerTypeWithManager();
  31. static void* getNativeInstance(MonoObject* managedInstance);
  32. };
  33. /**
  34. * @brief Base class for objects that can be extended using Mono scripting
  35. */
  36. template <class Type>
  37. class ScriptObject : public ScriptObjectBase
  38. {
  39. public:
  40. ScriptObject(MonoObject* instance)
  41. :ScriptObjectBase(instance)
  42. {
  43. // Compiler will only generate code for stuff that is directly used, including static data members,
  44. // so we fool it here like we're using the class directly. Otherwise compiler won't generate the code for the member
  45. // and our type won't get initialized on start (Actual behavior is a bit more random)
  46. initOnStart.makeSureIAmInstantiated();
  47. }
  48. virtual ~ScriptObject()
  49. { }
  50. static Type* toNative(MonoObject* managedInstance)
  51. {
  52. return reinterpret_cast<Type*>(getNativeInstance(managedInstance));
  53. }
  54. static void _initMetaData()
  55. {
  56. metaData = ScriptMeta(Type::getAssemblyName(), Type::getNamespace(), Type::getTypeName(), &Type::initRuntimeData);
  57. registerTypeWithManager();
  58. }
  59. protected:
  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. #define SCRIPT_OBJ(assembly, namespace, name) \
  77. static String getAssemblyName() { return assembly; } \
  78. static String getNamespace() { return namespace; } \
  79. static String getTypeName() { return name; } \
  80. static void initRuntimeData();
  81. }