Browse Source

Script resources now keep a reference to their managed instance until they're unloaded
Refactored script resources so they use a common interface

BearishSun 10 years ago
parent
commit
5387d9c3d8
52 changed files with 309 additions and 617 deletions
  1. 8 0
      BansheeCore/Include/BsCorePrerequisites.h
  2. 1 4
      BansheeCore/Include/BsResource.h
  3. 1 1
      SBansheeEditor/Source/BsScriptGUIResourceField.cpp
  4. 1 1
      SBansheeEditor/Source/BsScriptGUITextureField.cpp
  5. 1 1
      SBansheeEditor/Source/BsScriptGizmos.cpp
  6. 1 1
      SBansheeEditor/Source/BsScriptPlatformInfo.cpp
  7. 3 3
      SBansheeEditor/Source/BsScriptProjectLibrary.cpp
  8. 1 1
      SBansheeEditor/Source/BsScriptUndoRedo.cpp
  9. 1 17
      SBansheeEngine/Include/BsScriptFont.h
  10. 1 23
      SBansheeEngine/Include/BsScriptGUISkin.h
  11. 9 4
      SBansheeEngine/Include/BsScriptManagedResource.h
  12. 1 22
      SBansheeEngine/Include/BsScriptMaterial.h
  13. 1 22
      SBansheeEngine/Include/BsScriptMesh.h
  14. 1 22
      SBansheeEngine/Include/BsScriptPlainText.h
  15. 1 22
      SBansheeEngine/Include/BsScriptPrefab.h
  16. 87 5
      SBansheeEngine/Include/BsScriptResource.h
  17. 7 0
      SBansheeEngine/Include/BsScriptResourceManager.h
  18. 1 23
      SBansheeEngine/Include/BsScriptScriptCode.h
  19. 1 22
      SBansheeEngine/Include/BsScriptShader.h
  20. 1 23
      SBansheeEngine/Include/BsScriptSpriteTexture.h
  21. 1 23
      SBansheeEngine/Include/BsScriptStringTable.h
  22. 1 22
      SBansheeEngine/Include/BsScriptTexture2D.h
  23. 1 22
      SBansheeEngine/Include/BsScriptTexture3D.h
  24. 1 22
      SBansheeEngine/Include/BsScriptTextureCube.h
  25. 1 0
      SBansheeEngine/Source/BsManagedResource.cpp
  26. 14 14
      SBansheeEngine/Source/BsManagedSerializableField.cpp
  27. 5 20
      SBansheeEngine/Source/BsScriptFont.cpp
  28. 1 1
      SBansheeEngine/Source/BsScriptGUIContent.cpp
  29. 1 1
      SBansheeEngine/Source/BsScriptGUIElementStateStyle.cpp
  30. 1 1
      SBansheeEngine/Source/BsScriptGUIElementStyle.cpp
  31. 11 19
      SBansheeEngine/Source/BsScriptGUISkin.cpp
  32. 2 2
      SBansheeEngine/Source/BsScriptGUITexture.cpp
  33. 1 0
      SBansheeEngine/Source/BsScriptGameObjectManager.cpp
  34. 1 1
      SBansheeEngine/Source/BsScriptManagedResource.cpp
  35. 29 42
      SBansheeEngine/Source/BsScriptMaterial.cpp
  36. 6 19
      SBansheeEngine/Source/BsScriptMesh.cpp
  37. 3 16
      SBansheeEngine/Source/BsScriptPlainText.cpp
  38. 2 15
      SBansheeEngine/Source/BsScriptPrefab.cpp
  39. 3 3
      SBansheeEngine/Source/BsScriptRenderTexture2D.cpp
  40. 3 3
      SBansheeEngine/Source/BsScriptRenderable.cpp
  41. 9 0
      SBansheeEngine/Source/BsScriptResource.cpp
  42. 21 5
      SBansheeEngine/Source/BsScriptResourceManager.cpp
  43. 1 1
      SBansheeEngine/Source/BsScriptResources.cpp
  44. 9 20
      SBansheeEngine/Source/BsScriptScriptCode.cpp
  45. 6 17
      SBansheeEngine/Source/BsScriptShader.cpp
  46. 11 24
      SBansheeEngine/Source/BsScriptSpriteTexture.cpp
  47. 8 21
      SBansheeEngine/Source/BsScriptStringTable.cpp
  48. 1 1
      SBansheeEngine/Source/BsScriptStringTableManager.cpp
  49. 7 7
      SBansheeEngine/Source/BsScriptTexture.cpp
  50. 7 20
      SBansheeEngine/Source/BsScriptTexture2D.cpp
  51. 6 19
      SBansheeEngine/Source/BsScriptTexture3D.cpp
  52. 6 19
      SBansheeEngine/Source/BsScriptTextureCube.cpp

+ 8 - 0
BansheeCore/Include/BsCorePrerequisites.h

@@ -57,6 +57,14 @@
 
 #endif
 
+#if BS_DEBUG_MODE
+#define BS_DEBUG_ONLY(x) x
+#define BS_ASSERT(x) assert(x)
+#else
+#define BS_DEBUG_ONLY(x)
+#define BS_ASSERT(x)
+#endif
+
 #include "BsHString.h"
 
 namespace BansheeEngine 

+ 1 - 4
BansheeCore/Include/BsResource.h

@@ -41,9 +41,6 @@ namespace BansheeEngine
 
 		/**
 		 * @brief	Retrieves a list of all resources that this resource depends on.
-		 *
-		 * TODO - Consider using a stack-allocated data type since returned data is almost
-		 *		  always transient. Then we can save on memory allocations.
 		 */
 		virtual void getResourceDependencies(FrameVector<HResource>& dependencies) const { }
 
@@ -61,6 +58,6 @@ namespace BansheeEngine
 	public:
 		friend class ResourceRTTI;
 		static RTTITypeBase* getRTTIStatic();
-		virtual RTTITypeBase* getRTTI() const;
+		virtual RTTITypeBase* getRTTI() const override;
 	};
 }

+ 1 - 1
SBansheeEditor/Source/BsScriptGUIResourceField.cpp

@@ -91,7 +91,7 @@ namespace BansheeEngine
 		else
 		{
 			ScriptResourceBase* scriptResource = ScriptResource::toNative(value);
-			resourceField->setValue(scriptResource->getNativeHandle());
+			resourceField->setValue(scriptResource->getGenericHandle());
 		}
 	}
 

+ 1 - 1
SBansheeEditor/Source/BsScriptGUITextureField.cpp

@@ -83,7 +83,7 @@ namespace BansheeEngine
 		else
 		{
 			ScriptTextureBase* scriptTexture = ScriptTexture::toNative(value);
-			textureField->setValue(static_resource_cast<Texture>(scriptTexture->getNativeHandle()));
+			textureField->setValue(static_resource_cast<Texture>(scriptTexture->getGenericHandle()));
 		}
 	}
 

+ 1 - 1
SBansheeEditor/Source/BsScriptGizmos.cpp

@@ -76,7 +76,7 @@ namespace BansheeEngine
 	{
 		HSpriteTexture nativeTexture;
 		if (image != nullptr)
-			nativeTexture = ScriptSpriteTexture::toNative(image)->getInternalValue();
+			nativeTexture = ScriptSpriteTexture::toNative(image)->getHandle();
 
 		GizmoManager::instance().drawIcon(position, nativeTexture, fixedScale);
 	}

+ 1 - 1
SBansheeEditor/Source/BsScriptPlatformInfo.cpp

@@ -139,7 +139,7 @@ namespace BansheeEngine
 		HTexture icon;
 
 		if (texturePtr != nullptr)
-			icon = texturePtr->getTextureHandle();
+			icon = texturePtr->getHandle();
 
 		switch (size)
 		{

+ 3 - 3
SBansheeEditor/Source/BsScriptProjectLibrary.cpp

@@ -79,7 +79,7 @@ namespace BansheeEngine
 		ScriptResource* scrResource = ScriptResource::toNative(resource);
 		Path resourcePath = MonoUtil::monoToWString(path);
 
-		gProjectLibrary().createEntry(scrResource->getNativeHandle(), resourcePath);
+		gProjectLibrary().createEntry(scrResource->getGenericHandle(), resourcePath);
 	}
 
 	MonoObject* ScriptProjectLibrary::internal_Load(MonoString* path)
@@ -108,7 +108,7 @@ namespace BansheeEngine
 		ScriptResource* srcResource = ScriptResource::toNative(resource);
 
 		if (srcResource != nullptr)
-			gProjectLibrary().saveEntry(srcResource->getNativeHandle());
+			gProjectLibrary().saveEntry(srcResource->getGenericHandle());
 	}
 
 	MonoObject* ScriptProjectLibrary::internal_GetRoot()
@@ -158,7 +158,7 @@ namespace BansheeEngine
 
 		if (srcResource != nullptr)
 		{
-			Path nativePath = gProjectLibrary().uuidToPath(srcResource->getNativeHandle().getUUID());
+			Path nativePath = gProjectLibrary().uuidToPath(srcResource->getGenericHandle().getUUID());
 			nativePath.getRelative(gProjectLibrary().getResourcesFolder());
 
 			return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), nativePath.toWString());

+ 1 - 1
SBansheeEditor/Source/BsScriptUndoRedo.cpp

@@ -96,7 +96,7 @@ namespace BansheeEngine
 
 	MonoObject* ScriptUndoRedo::internal_Instantiate(ScriptPrefab* prefabPtr, MonoString* description)
 	{
-		HPrefab prefab = prefabPtr->getPrefabHandle();
+		HPrefab prefab = prefabPtr->getHandle();
 		if (!prefab.isLoaded())
 			return nullptr;
 

+ 1 - 17
SBansheeEngine/Include/BsScriptFont.h

@@ -10,32 +10,16 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for Font.
 	 */
-	class BS_SCR_BE_EXPORT ScriptFont : public ScriptObject<ScriptFont, ScriptResourceBase>
+	class BS_SCR_BE_EXPORT ScriptFont : public TScriptResource<ScriptFont, Font>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Font")
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mFont; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
 	private:
 		friend class ScriptResourceManager;
 
 		ScriptFont(MonoObject* instance, const HFont& font);
 
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
-		HFont mFont;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 1 - 23
SBansheeEngine/Include/BsScriptGUISkin.h

@@ -8,36 +8,14 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for GUISkin.
 	 */
-	class BS_SCR_BE_EXPORT ScriptGUISkin : public ScriptObject <ScriptGUISkin, ScriptResourceBase>
+	class BS_SCR_BE_EXPORT ScriptGUISkin : public TScriptResource<ScriptGUISkin, GUISkin>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "GUISkin")
 
 		ScriptGUISkin(MonoObject* instance, const HGUISkin& skin);
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mSkin; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
-		/**
-		 * @brief	Returns the native internal GUI skin resource.
-		 */
-		HGUISkin getGUIUSkinHandle() const { return mSkin; }
-
 	private:
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
-		HGUISkin mSkin;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 9 - 4
SBansheeEngine/Include/BsScriptManagedResource.h

@@ -17,14 +17,19 @@ namespace BansheeEngine
 		ScriptManagedResource(MonoObject* instance, const HManagedResource& resource);
 
 		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
+		 * @copydoc	ScriptResourceBase::getGenericHandle
 		 */
-		HResource getNativeHandle() const override { return mResource; }
+		HResource getGenericHandle() const override { return mResource; }
 
 		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
+		 * @copydoc	ScriptResourceBase::setResource
 		 */
-		void setNativeHandle(const HResource& resource) override;
+		void setResource(const HResource& resource) override;
+
+		/**
+		 * @brief	Returns a handle to the internal wrapped resource.
+		 */
+		const HManagedResource& getHandle() const { return mResource; }
 	private:
 		friend class ScriptResourceManager;
 

+ 1 - 22
SBansheeEngine/Include/BsScriptMaterial.h

@@ -15,37 +15,16 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for Material.
 	 */
-	class BS_SCR_BE_EXPORT ScriptMaterial : public ScriptObject <ScriptMaterial, ScriptResourceBase>
+	class BS_SCR_BE_EXPORT ScriptMaterial : public TScriptResource <ScriptMaterial, Material>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Material")
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mMaterial; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
-		/**
-		 * @brief	Returns the wrapped native material handle.
-		 */
-		HMaterial getMaterialHandle() const { return mMaterial; }
 	private:
 		friend class ScriptResourceManager;
 
 		ScriptMaterial(MonoObject* instance, const HMaterial& material);
 
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
-		HMaterial mMaterial;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 1 - 22
SBansheeEngine/Include/BsScriptMesh.h

@@ -59,35 +59,16 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for Mesh.
 	 */
-	class BS_SCR_BE_EXPORT ScriptMesh : public ScriptObject <ScriptMesh, ScriptResourceBase>
+	class BS_SCR_BE_EXPORT ScriptMesh : public TScriptResource<ScriptMesh, Mesh>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Mesh")
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mMesh; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
-		/**
-		 * @brief	Returns the wrapped native mesh handle.
-		 */
-		HMesh getMeshHandle() const { return mMesh; }
 	private:
 		friend class ScriptResourceManager;
 
 		ScriptMesh(MonoObject* instance, const HMesh& mesh);
 
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
 		/**
 		 * @brief	Converts the C# MeshTopology enum to DrawOperationType enum
 		 *			used by engine internals.
@@ -106,8 +87,6 @@ namespace BansheeEngine
 		 */
 		static Vector<SubMesh> monoToNativeSubMeshes(MonoArray* subMeshes);
 
-		HMesh mMesh;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 1 - 22
SBansheeEngine/Include/BsScriptPlainText.h

@@ -10,37 +10,16 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for PlainText.
 	 */
-	class BS_SCR_BE_EXPORT ScriptPlainText : public ScriptObject <ScriptPlainText, ScriptResourceBase>
+	class BS_SCR_BE_EXPORT ScriptPlainText : public TScriptResource<ScriptPlainText, PlainText>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "PlainText")
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mPlainText; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
-		/**
-		 * @brief	Returns the internal wrapped plain text resource.
-		 */
-		HPlainText getPlainTextHandle() const { return mPlainText; }
 	private:
 		friend class ScriptResourceManager;
 
 		ScriptPlainText(MonoObject* instance, const HPlainText& plainText);
 
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
-		HPlainText mPlainText;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 1 - 22
SBansheeEngine/Include/BsScriptPrefab.h

@@ -9,37 +9,16 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for Prefab.
 	 */
-	class BS_SCR_BE_EXPORT ScriptPrefab : public ScriptObject<ScriptPrefab, ScriptResourceBase>
+	class BS_SCR_BE_EXPORT ScriptPrefab : public TScriptResource<ScriptPrefab, Prefab>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Prefab")
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mPrefab; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
-		/**
-		 * @brief	Returns the internal wrapped prefab resource.
-		 */
-		HPrefab getPrefabHandle() const { return mPrefab; }
 	private:
 		friend class ScriptResourceManager;
 
 		ScriptPrefab(MonoObject* instance, const HPrefab& prefab);
 
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
-		HPrefab mPrefab;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 87 - 5
SBansheeEngine/Include/BsScriptResource.h

@@ -8,8 +8,7 @@ namespace BansheeEngine
 	/**
 	 * @brief	Types of resources accessible from script code.
 	 */
-	// Note: Must be the same as C# enum ScriptResourceType
-	enum class ScriptResourceType
+	enum class ScriptResourceType // Note: Must be the same as C# enum ScriptResourceType
 	{
 		Texture, SpriteTexture, Mesh, Font, Shader, Material, Prefab, PlainText, ScriptCode, StringTable, GUISkin, Undefined
 	};
@@ -21,14 +20,14 @@ namespace BansheeEngine
 	{
 	public:
 		/**
-		 * @brief	Returns the internal wrapped resource.
+		 * @brief	Returns a generic handle to the internal wrapped resource.
 		 */
-		virtual HResource getNativeHandle() const = 0;
+		virtual HResource getGenericHandle() const = 0;
 
 		/**
 		 * @brief	Sets the internal resource this object wraps.
 		 */
-		virtual void setNativeHandle(const HResource& resource) = 0;
+		virtual void setResource(const HResource& resource) = 0;
 
 		/**
 		 * @copydoc	ScriptObjectBase::beginRefresh
@@ -46,9 +45,92 @@ namespace BansheeEngine
 		ScriptResourceBase(MonoObject* instance);
 		virtual ~ScriptResourceBase() {}
 
+		/**
+		 * @brief	Triggered by the script resource managed when the native resource handle this object point to has
+		 * 			been destroyed.
+		 */
+		virtual void notifyResourceDestroyed() { }
+
+		/**
+		 * @brief	Destroys the interop object, unless refresh is in progress in which case it is just prepared
+		 * 			for re-creation.
+		 */
+		void destroy();
+
 		bool mRefreshInProgress;
 	};
 
+	/**
+	 * @brief	Base class for a specific resource's interop object.
+	 */
+	template<class ScriptClass, class ResType>
+	class BS_SCR_BE_EXPORT TScriptResource : public ScriptObject <ScriptClass, ScriptResourceBase>
+	{
+	public:
+		/**
+		 * @copydoc	ScriptResourceBase::getGenericHandle
+		 */
+		HResource getGenericHandle() const override { return mResource; }
+
+		/**
+		 * @copydoc	ScriptResourceBase::setResource
+		 */
+		void setResource(const HResource& resource) override { mResource = static_resource_cast<ResType>(resource); }
+
+		/**
+		 * @brief	Returns a handle to the internal wrapped resource.
+		 */
+		const ResourceHandle<ResType>& getHandle() const { return mResource; }
+
+	protected:
+		friend class ScriptResourceManager;
+
+		TScriptResource(MonoObject* instance, const ResourceHandle<ResType>& resource)
+			:ScriptObject(instance), mResource(resource)
+		{
+			mManagedHandle = mono_gchandle_new(instance, false);
+
+			BS_DEBUG_ONLY(mHandleValid = true);
+		}
+
+		virtual ~TScriptResource() {}
+
+		/**
+		 * @copydoc	ScriptObjectBase::endRefresh
+		 */
+		virtual void endRefresh(const ScriptObjectBackup& backupData) override
+		{
+			BS_ASSERT(!mHandleValid);
+			mManagedHandle = mono_gchandle_new(mManagedInstance, false);
+
+			ScriptObject::endRefresh(backupData);
+		}
+
+		/**
+		 * @copydoc	ScriptObjectBase::notifyResourceDestroyed
+		 */
+		void notifyResourceDestroyed() override
+		{
+			mono_gchandle_free(mManagedHandle);
+			BS_DEBUG_ONLY(mHandleValid = false);
+		}
+
+		/**
+		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
+		 */
+		void _onManagedInstanceDeleted() override
+		{
+			mono_gchandle_free(mManagedHandle);
+			BS_DEBUG_ONLY(mHandleValid = false);
+
+			destroy();
+		}
+
+		ResourceHandle<ResType> mResource;
+		uint32_t mManagedHandle;
+		BS_DEBUG_ONLY(bool mHandleValid);
+	};
+
 	/**
 	 * @brief	Interop class between C++ & CLR for Resource.
 	 */

+ 7 - 0
SBansheeEngine/Include/BsScriptResourceManager.h

@@ -16,6 +16,7 @@ namespace BansheeEngine
 	{
 	public:
 		ScriptResourceManager();
+		~ScriptResourceManager();
 
 		/**
 		 * @brief	Creates a new managed instance and interop object for the specified resource.
@@ -88,6 +89,11 @@ namespace BansheeEngine
 		void destroyScriptResource(ScriptResourceBase* resource);
 
 	private:
+		/**
+		 * @brief	Triggered when the native resource has been unloaded and therefore destroyed.
+		 */
+		void onResourceDestroyed(const HResource& resource);
+
 		/**
 		 * @brief	Throws an exception if the provided UUID already exists in the interop object
 		 *			lookup table.
@@ -95,5 +101,6 @@ namespace BansheeEngine
 		void throwExceptionIfInvalidOrDuplicate(const String& uuid) const;
 
 		UnorderedMap<String, ScriptResourceBase*> mScriptResources;
+		HEvent mResourceDestroyedConn;
 	};
 }

+ 1 - 23
SBansheeEngine/Include/BsScriptScriptCode.h

@@ -10,44 +10,22 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for ScriptCode.
 	 */
-	class BS_SCR_BE_EXPORT ScriptScriptCode : public ScriptObject <ScriptScriptCode, ScriptResourceBase>
+	class BS_SCR_BE_EXPORT ScriptScriptCode : public TScriptResource<ScriptScriptCode, ScriptCode>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "ScriptCode")
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mScriptCode; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
-		/**
-		 * @brief	Returns the native internal script code resource.
-		 */
-		HScriptCode getScriptCodeHandle() const { return mScriptCode; }
 	private:
 		friend class ScriptResourceManager;
 		typedef std::pair<WString, WString> FullTypeName;
 
 		ScriptScriptCode(MonoObject* instance, const HScriptCode& scriptCode);
-
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
 		/**
 		 * @brief	Parses the provided C# code and finds a list of all classes
 		 *			and their namespaces. Nested classes are ignored.
 		 */
 		static Vector<FullTypeName> parseTypes(const WString& code);
 
-		HScriptCode mScriptCode;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 1 - 22
SBansheeEngine/Include/BsScriptShader.h

@@ -9,37 +9,16 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for Shader.
 	 */
-	class BS_SCR_BE_EXPORT ScriptShader : public ScriptObject <ScriptShader, ScriptResourceBase>
+	class BS_SCR_BE_EXPORT ScriptShader : public TScriptResource <ScriptShader, Shader>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Shader")
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mShader; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
-		/**
-		 * @brief	Returns the native internal shader resource.
-		 */
-		HShader getShaderHandle() const { return mShader; }
 	private:
 		friend class ScriptResourceManager;
 
 		ScriptShader(MonoObject* instance, const HShader& shader);
 
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
-		HShader mShader;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 1 - 23
SBansheeEngine/Include/BsScriptSpriteTexture.h

@@ -9,26 +9,11 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for SpriteTexture.
 	 */
-	class BS_SCR_BE_EXPORT ScriptSpriteTexture : public ScriptObject<ScriptSpriteTexture, ScriptResourceBase>
+	class BS_SCR_BE_EXPORT ScriptSpriteTexture : public TScriptResource<ScriptSpriteTexture, SpriteTexture>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "SpriteTexture")
 
-		/**
-		 * @brief	Returns the native wrapped sprite texture resource handle.
-		 */
-		const HSpriteTexture& getInternalValue() const { return mTexture; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mTexture; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
 		/**
 		 * @brief	Returns the native internal sprite texture resource.
 		 */
@@ -39,13 +24,6 @@ namespace BansheeEngine
 
 		ScriptSpriteTexture(MonoObject* instance, const HSpriteTexture& texture);
 
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
-		HSpriteTexture mTexture;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 1 - 23
SBansheeEngine/Include/BsScriptStringTable.h

@@ -9,36 +9,14 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for StringTable.
 	 */
-	class BS_SCR_BE_EXPORT ScriptStringTable : public ScriptObject <ScriptStringTable, ScriptResourceBase>
+	class BS_SCR_BE_EXPORT ScriptStringTable : public TScriptResource <ScriptStringTable, StringTable>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "StringTable")
 
 		ScriptStringTable(MonoObject* instance, const HStringTable& table);
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mTable; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
-		/**
-		 * @brief	Returns the native internal string table resource.
-		 */
-		HStringTable getStringTableHandle() const { return mTable; }
-
 	private:
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
-		HStringTable mTable;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 1 - 22
SBansheeEngine/Include/BsScriptTexture2D.h

@@ -10,37 +10,16 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for a 2D Texture.
 	 */
-	class BS_SCR_BE_EXPORT ScriptTexture2D : public ScriptObject<ScriptTexture2D, ScriptTextureBase>
+	class BS_SCR_BE_EXPORT ScriptTexture2D : public TScriptResource<ScriptTexture2D, Texture>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Texture2D")
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mTexture; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
-		/**
-		 * @brief	Returns the native internal 2D texture resource.
-		 */
-		HTexture getTextureHandle() const { return mTexture; }
 	private:
 		friend class ScriptResourceManager;
 
 		ScriptTexture2D(MonoObject* instance, const HTexture& texture);
 
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
-		HTexture mTexture;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 1 - 22
SBansheeEngine/Include/BsScriptTexture3D.h

@@ -10,37 +10,16 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for 3D Texture.
 	 */
-	class BS_SCR_BE_EXPORT ScriptTexture3D : public ScriptObject <ScriptTexture3D, ScriptTextureBase>
+	class BS_SCR_BE_EXPORT ScriptTexture3D : public TScriptResource <ScriptTexture3D, Texture>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "Texture3D")
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mTexture; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
-		/**
-		 * @brief	Returns the native internal 3D texture resource.
-		 */
-		HTexture getTextureHandle() const { return mTexture; }
 	private:
 		friend class ScriptResourceManager;
 
 		ScriptTexture3D(MonoObject* instance, const HTexture& texture);
 
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
-		HTexture mTexture;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 1 - 22
SBansheeEngine/Include/BsScriptTextureCube.h

@@ -10,37 +10,16 @@ namespace BansheeEngine
 	/**
 	 * @brief	Interop class between C++ & CLR for a cube Texture.
 	 */
-	class BS_SCR_BE_EXPORT ScriptTextureCube : public ScriptObject <ScriptTextureCube, ScriptTextureBase>
+	class BS_SCR_BE_EXPORT ScriptTextureCube : public TScriptResource <ScriptTextureCube, Texture>
 	{
 	public:
 		SCRIPT_OBJ(ENGINE_ASSEMBLY, "BansheeEngine", "TextureCube")
 
-		/**
-		 * @copydoc	ScriptResourceBase::getNativeHandle
-		 */
-		HResource getNativeHandle() const override { return mTexture; }
-
-		/**
-		 * @copydoc	ScriptResourceBase::setNativeHandle
-		 */
-		void setNativeHandle(const HResource& resource) override;
-
-		/**
-		 * @brief	Returns the native internal cube texture resource.
-		 */
-		HTexture getTextureHandle() const { return mTexture; }
 	private:
 		friend class ScriptResourceManager;
 
 		ScriptTextureCube(MonoObject* instance, const HTexture& texture);
 
-		/**
-		 * @copydoc	ScriptObjectBase::_onManagedInstanceDeleted
-		 */
-		void _onManagedInstanceDeleted() override;
-
-		HTexture mTexture;
-
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/

+ 1 - 0
SBansheeEngine/Source/BsManagedResource.cpp

@@ -6,6 +6,7 @@
 #include "BsResources.h"
 #include "BsManagedResourceManager.h"
 #include "BsManagedSerializableObject.h"
+#include "BsScriptManagedResource.h"
 #include "BsMemorySerializer.h"
 #include "BsScriptResourceManager.h"
 #include "BsMonoUtil.h"

+ 14 - 14
SBansheeEngine/Source/BsManagedSerializableField.cpp

@@ -171,7 +171,7 @@ namespace BansheeEngine
 					if(value != nullptr)
 					{
 						ScriptTexture2D* scriptTexture2D = ScriptTexture2D::toNative(value);
-						fieldData->value = static_resource_cast<Texture>(scriptTexture2D->getNativeHandle());
+						fieldData->value = scriptTexture2D->getHandle();
 					}
 
 					return fieldData;
@@ -183,7 +183,7 @@ namespace BansheeEngine
 				if (value != nullptr)
 				{
 					ScriptTexture3D* scriptTexture3D = ScriptTexture3D::toNative(value);
-					fieldData->value = static_resource_cast<Texture>(scriptTexture3D->getNativeHandle());
+					fieldData->value = scriptTexture3D->getHandle();
 				}
 
 				return fieldData;
@@ -195,7 +195,7 @@ namespace BansheeEngine
 				if (value != nullptr)
 				{
 					ScriptTextureCube* scriptTextureCube = ScriptTextureCube::toNative(value);
-					fieldData->value = static_resource_cast<Texture>(scriptTextureCube->getNativeHandle());
+					fieldData->value = scriptTextureCube->getHandle();
 				}
 
 				return fieldData;
@@ -207,7 +207,7 @@ namespace BansheeEngine
 					if(value != nullptr)
 					{
 						ScriptSpriteTexture* scriptSpriteTexture = ScriptSpriteTexture::toNative(value);
-						fieldData->value = static_resource_cast<SpriteTexture>(scriptSpriteTexture->getNativeHandle());
+						fieldData->value = scriptSpriteTexture->getHandle();
 					}
 
 					return fieldData;
@@ -219,7 +219,7 @@ namespace BansheeEngine
 				if (value != nullptr)
 				{
 					ScriptShader* scriptShader = ScriptShader::toNative(value);
-					fieldData->value = static_resource_cast<Shader>(scriptShader->getNativeHandle());
+					fieldData->value = scriptShader->getHandle();
 				}
 
 				return fieldData;
@@ -231,7 +231,7 @@ namespace BansheeEngine
 				if (value != nullptr)
 				{
 					ScriptMaterial* scriptMaterial = ScriptMaterial::toNative(value);
-					fieldData->value = static_resource_cast<Material>(scriptMaterial->getNativeHandle());
+					fieldData->value = scriptMaterial->getHandle();
 				}
 
 				return fieldData;
@@ -243,7 +243,7 @@ namespace BansheeEngine
 				if (value != nullptr)
 				{
 					ScriptMesh* scriptMesh = ScriptMesh::toNative(value);
-					fieldData->value = static_resource_cast<Mesh>(scriptMesh->getNativeHandle());
+					fieldData->value = scriptMesh->getHandle();
 				}
 
 				return fieldData;
@@ -255,7 +255,7 @@ namespace BansheeEngine
 				if (value != nullptr)
 				{
 					ScriptPrefab* scriptPrefab = ScriptPrefab::toNative(value);
-					fieldData->value = static_resource_cast<Prefab>(scriptPrefab->getNativeHandle());
+					fieldData->value = scriptPrefab->getHandle();
 				}
 
 				return fieldData;
@@ -267,7 +267,7 @@ namespace BansheeEngine
 				if (value != nullptr)
 				{
 					ScriptFont* scriptFont = ScriptFont::toNative(value);
-					fieldData->value = static_resource_cast<Font>(scriptFont->getNativeHandle());
+					fieldData->value = scriptFont->getHandle();
 				}
 
 				return fieldData;
@@ -279,7 +279,7 @@ namespace BansheeEngine
 				if (value != nullptr)
 				{
 					ScriptStringTable* scriptStringTable = ScriptStringTable::toNative(value);
-					fieldData->value = static_resource_cast<StringTable>(scriptStringTable->getNativeHandle());
+					fieldData->value = scriptStringTable->getHandle();
 				}
 
 				return fieldData;
@@ -291,7 +291,7 @@ namespace BansheeEngine
 				if (value != nullptr)
 				{
 					ScriptGUISkin* scriptGUISkin = ScriptGUISkin::toNative(value);
-					fieldData->value = static_resource_cast<GUISkin>(scriptGUISkin->getNativeHandle());
+					fieldData->value = scriptGUISkin->getHandle();
 				}
 
 				return fieldData;
@@ -303,7 +303,7 @@ namespace BansheeEngine
 					if(value != nullptr)
 					{
 						ScriptManagedResource* scriptManagedResource = ScriptManagedResource::toNative(value);
-						fieldData->value = static_resource_cast<ManagedResource>(scriptManagedResource->getNativeHandle());
+						fieldData->value = scriptManagedResource->getHandle();
 					}
 
 					return fieldData;
@@ -315,7 +315,7 @@ namespace BansheeEngine
 				if (value != nullptr)
 				{
 					ScriptPlainText* scriptPlainResource = ScriptPlainText::toNative(value);
-					fieldData->value = static_resource_cast<PlainText>(scriptPlainResource->getNativeHandle());
+					fieldData->value = scriptPlainResource->getHandle();
 				}
 
 				return fieldData;
@@ -327,7 +327,7 @@ namespace BansheeEngine
 				if (value != nullptr)
 				{
 					ScriptScriptCode* scriptScriptCode = ScriptScriptCode::toNative(value);
-					fieldData->value = static_resource_cast<ScriptCode>(scriptScriptCode->getNativeHandle());
+					fieldData->value = scriptScriptCode->getHandle();
 				}
 
 				return fieldData;

+ 5 - 20
SBansheeEngine/Source/BsScriptFont.cpp

@@ -9,7 +9,7 @@
 namespace BansheeEngine
 {
 	ScriptFont::ScriptFont(MonoObject* instance, const HFont& font)
-		:ScriptObject(instance), mFont(font)
+		:TScriptResource(instance, font)
 	{
 
 	}
@@ -20,25 +20,11 @@ namespace BansheeEngine
 		metaData.scriptClass->addInternalCall("Internal_GetClosestSize", &ScriptFont::internal_GetClosestSize);
 	}
 
-	void ScriptFont::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
-
-	void ScriptFont::setNativeHandle(const HResource& resource)
-	{
-		mFont = static_resource_cast<Font>(resource);
-	}
-
 	MonoObject* ScriptFont::internal_GetBitmap(ScriptFont* instance, int size)
 	{
-		if (!instance->mFont.isLoaded())
-			return nullptr;
+		HFont font = instance->getHandle();
 
-		SPtr<const FontBitmap> bitmap = instance->mFont->getBitmap(size);
+		SPtr<const FontBitmap> bitmap = font->getBitmap(size);
 		if (bitmap != nullptr)
 			return ScriptFontBitmap::create(bitmap);
 
@@ -47,10 +33,9 @@ namespace BansheeEngine
 
 	int ScriptFont::internal_GetClosestSize(ScriptFont* instance, int size)
 	{
-		if (!instance->mFont.isLoaded())
-			return 0;
+		HFont font = instance->getHandle();
 
-		return instance->mFont->getClosestSize(size);
+		return font->getClosestSize(size);
 	}
 
 	ScriptCharRange::ScriptCharRange(MonoObject* instance)

+ 1 - 1
SBansheeEngine/Source/BsScriptGUIContent.cpp

@@ -58,6 +58,6 @@ namespace BansheeEngine
 			return HSpriteTexture();
 
 		ScriptSpriteTexture* imageScript = ScriptSpriteTexture::toNative(imageManaged);
-		return imageScript->getInternalValue();
+		return imageScript->getHandle();
 	}
 }

+ 1 - 1
SBansheeEngine/Source/BsScriptGUIElementStateStyle.cpp

@@ -60,7 +60,7 @@ namespace BansheeEngine
 	void ScriptGUIElementStateStyle::internal_SetTexture(ScriptGUIElementStateStyle* nativeInstance, MonoObject* value)
 	{
 		ScriptSpriteTexture* nativeValue = ScriptSpriteTexture::toNative(value);
-		nativeInstance->mElementStateStyle->texture = nativeValue->getInternalValue();
+		nativeInstance->mElementStateStyle->texture = nativeValue->getHandle();
 		nativeInstance->mSpriteTexture = nativeValue;
 	}
 

+ 1 - 1
SBansheeEngine/Source/BsScriptGUIElementStyle.cpp

@@ -136,7 +136,7 @@ namespace BansheeEngine
 	void ScriptGUIElementStyle::internal_SetFont(ScriptGUIElementStyle* nativeInstance, MonoObject* value)
 	{
 		ScriptFont* nativeValue = ScriptFont::toNative(value);
-		nativeInstance->mElementStyle.font = static_resource_cast<Font>(nativeValue->getNativeHandle());
+		nativeInstance->mElementStyle.font = static_resource_cast<Font>(nativeValue->getHandle());
 		nativeInstance->mFont = nativeValue;
 	}
 

+ 11 - 19
SBansheeEngine/Source/BsScriptGUISkin.cpp

@@ -11,7 +11,7 @@
 namespace BansheeEngine
 {
 	ScriptGUISkin::ScriptGUISkin(MonoObject* instance, const HGUISkin& skin)
-		:ScriptObject(instance), mSkin(skin)
+		:TScriptResource(instance, skin)
 	{ }
 
 	void ScriptGUISkin::initRuntimeData()
@@ -35,9 +35,10 @@ namespace BansheeEngine
 
 	MonoObject* ScriptGUISkin::internal_GetStyle(ScriptGUISkin* thisPtr, MonoString* name)
 	{
+		HGUISkin skin = thisPtr->getHandle();
 		String nativeName = MonoUtil::monoToString(name);
 
-		const GUIElementStyle* style = thisPtr->mSkin->getStyle(nativeName);
+		const GUIElementStyle* style = skin->getStyle(nativeName);
 		if (style == nullptr)
 			return nullptr;
 		
@@ -46,22 +47,26 @@ namespace BansheeEngine
 
 	void ScriptGUISkin::internal_SetStyle(ScriptGUISkin* thisPtr, MonoString* name, ScriptGUIElementStyle* style)
 	{
+		HGUISkin skin = thisPtr->getHandle();
 		String nativeName = MonoUtil::monoToString(name);
 
 		if (style != nullptr)
-			thisPtr->mSkin->setStyle(nativeName, style->getInternalValue());
+			skin->setStyle(nativeName, style->getInternalValue());
 	}
 
 	void ScriptGUISkin::internal_RemoveStyle(ScriptGUISkin* thisPtr, MonoString* name)
 	{
-		String nativeName = MonoUtil::monoToString(name);
+		HGUISkin skin = thisPtr->getHandle();
 
-		thisPtr->mSkin->removeStyle(nativeName);
+		String nativeName = MonoUtil::monoToString(name);
+		skin->removeStyle(nativeName);
 	}
 
 	MonoArray* ScriptGUISkin::internal_GetStyleNames(ScriptGUISkin* thisPtr)
 	{
-		Vector<String> styleNames = thisPtr->mSkin->getStyleNames();
+		HGUISkin skin = thisPtr->getHandle();
+
+		Vector<String> styleNames = skin->getStyleNames();
 		UINT32 numNames = (UINT32)styleNames.size();
 
 		ScriptArray output = ScriptArray::create<String>(numNames);
@@ -70,17 +75,4 @@ namespace BansheeEngine
 
 		return output.getInternal();
 	}
-
-	void ScriptGUISkin::setNativeHandle(const HResource& resource)
-	{
-		mSkin = static_resource_cast<GUISkin>(resource);
-	}
-
-	void ScriptGUISkin::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
 }

+ 2 - 2
SBansheeEngine/Source/BsScriptGUITexture.cpp

@@ -40,7 +40,7 @@ namespace BansheeEngine
 
 		HSpriteTexture nativeTexture;
 		if(texture != nullptr)
-			nativeTexture = ScriptSpriteTexture::toNative(texture)->getInternalValue();
+			nativeTexture = ScriptSpriteTexture::toNative(texture)->getHandle();
 
 		GUITexture* guiTexture = GUITexture::create(nativeTexture, scale, transparent, options, toString(MonoUtil::monoToWString(style)));
 
@@ -51,7 +51,7 @@ namespace BansheeEngine
 	{
 		HSpriteTexture nativeTexture;
 		if(texture != nullptr)
-			nativeTexture = ScriptSpriteTexture::toNative(texture)->getInternalValue();
+			nativeTexture = ScriptSpriteTexture::toNative(texture)->getHandle();
 
 		GUITexture* guiTexture = (GUITexture*)nativeInstance->getGUIElement();
 		guiTexture->setTexture(nativeTexture);

+ 1 - 0
SBansheeEngine/Source/BsScriptGameObjectManager.cpp

@@ -168,5 +168,6 @@ namespace BansheeEngine
 			return;
 
 		so->_notifyDestroyed();
+		mScriptSceneObjects.erase(instanceId);
 	}
 }

+ 1 - 1
SBansheeEngine/Source/BsScriptManagedResource.cpp

@@ -80,7 +80,7 @@ namespace BansheeEngine
 		}
 	}
 
-	void ScriptManagedResource::setNativeHandle(const HResource& resource)
+	void ScriptManagedResource::setResource(const HResource& resource)
 	{
 		mResource = static_resource_cast<ManagedResource>(resource);
 	}

+ 29 - 42
SBansheeEngine/Source/BsScriptMaterial.cpp

@@ -14,7 +14,7 @@
 namespace BansheeEngine
 {
 	ScriptMaterial::ScriptMaterial(MonoObject* instance, const HMaterial& material)
-		:ScriptObject(instance), mMaterial(material)
+		:TScriptResource(instance, material)
 	{
 
 	}
@@ -54,7 +54,7 @@ namespace BansheeEngine
 	{
 		HShader nativeShader;
 		if (shader != nullptr)
-			nativeShader = shader->getShaderHandle();
+			nativeShader = shader->getHandle();
 
 		if (nativeShader == nullptr)
 			nativeShader = BuiltinResources::instance().getDummyShader();
@@ -67,7 +67,7 @@ namespace BansheeEngine
 
 	MonoObject* ScriptMaterial::internal_Clone(ScriptMaterial* nativeInstance)
 	{
-		HMaterial clone = nativeInstance->mMaterial->clone();
+		HMaterial clone = nativeInstance->getHandle()->clone();
 
 		ScriptMaterial* scriptClone;
 		ScriptResourceManager::instance().createScriptResource(clone, &scriptClone);
@@ -77,7 +77,7 @@ namespace BansheeEngine
 
 	MonoObject* ScriptMaterial::internal_GetShader(ScriptMaterial* nativeInstance)
 	{
-		HShader shader = nativeInstance->getMaterialHandle()->getShader();
+		HShader shader = nativeInstance->getHandle()->getShader();
 
 		if (shader == nullptr)
 			return nullptr;
@@ -92,61 +92,61 @@ namespace BansheeEngine
 	{
 		HShader nativeShader;
 		if (shader != nullptr)
-			nativeShader = shader->getShaderHandle();
+			nativeShader = shader->getHandle();
 
 		if (nativeShader == nullptr)
 			nativeShader = BuiltinResources::instance().getDummyShader();
 
-		nativeInstance->getMaterialHandle()->setShader(nativeShader);
+		nativeInstance->getHandle()->setShader(nativeShader);
 	}
 
 	void ScriptMaterial::internal_SetFloat(ScriptMaterial* nativeInstance, MonoString* name, float value)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		nativeInstance->mMaterial->setFloat(paramName, value);
+		nativeInstance->getHandle()->setFloat(paramName, value);
 	}
 
 	void ScriptMaterial::internal_SetVector2(ScriptMaterial* nativeInstance, MonoString* name, Vector2 value)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		nativeInstance->mMaterial->setVec2(paramName, value);
+		nativeInstance->getHandle()->setVec2(paramName, value);
 	}
 
 	void ScriptMaterial::internal_SetVector3(ScriptMaterial* nativeInstance, MonoString* name, Vector3 value)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		nativeInstance->mMaterial->setVec3(paramName, value);
+		nativeInstance->getHandle()->setVec3(paramName, value);
 	}
 
 	void ScriptMaterial::internal_SetVector4(ScriptMaterial* nativeInstance, MonoString* name, Vector4 value)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		nativeInstance->mMaterial->setVec4(paramName, value);
+		nativeInstance->getHandle()->setVec4(paramName, value);
 	}
 
 	void ScriptMaterial::internal_SetMatrix3(ScriptMaterial* nativeInstance, MonoString* name, Matrix3 value)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		nativeInstance->mMaterial->setMat3(paramName, value);
+		nativeInstance->getHandle()->setMat3(paramName, value);
 	}
 
 	void ScriptMaterial::internal_SetMatrix4(ScriptMaterial* nativeInstance, MonoString* name, Matrix4 value)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		nativeInstance->mMaterial->setMat4(paramName, value);
+		nativeInstance->getHandle()->setMat4(paramName, value);
 	}
 
 	void ScriptMaterial::internal_SetColor(ScriptMaterial* nativeInstance, MonoString* name, Color value)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		nativeInstance->mMaterial->setColor(paramName, value);
+		nativeInstance->getHandle()->setColor(paramName, value);
 	}
 
 	void ScriptMaterial::internal_SetTexture2D(ScriptMaterial* nativeInstance, MonoString* name, ScriptTexture2D* value)
@@ -156,9 +156,9 @@ namespace BansheeEngine
 		HTexture texture;
 
 		if (value != nullptr)
-			texture = value->getTextureHandle();
+			texture = value->getHandle();
 
-		nativeInstance->mMaterial->setTexture(paramName, texture);
+		nativeInstance->getHandle()->setTexture(paramName, texture);
 	}
 
 	void ScriptMaterial::internal_SetTexture3D(ScriptMaterial* nativeInstance, MonoString* name, ScriptTexture3D* value)
@@ -168,9 +168,9 @@ namespace BansheeEngine
 		HTexture texture;
 
 		if (value != nullptr)
-			texture = value->getTextureHandle();
+			texture = value->getHandle();
 
-		nativeInstance->mMaterial->setTexture(paramName, texture);
+		nativeInstance->getHandle()->setTexture(paramName, texture);
 	}
 
 	void ScriptMaterial::internal_SetTextureCube(ScriptMaterial* nativeInstance, MonoString* name, ScriptTextureCube* value)
@@ -180,65 +180,65 @@ namespace BansheeEngine
 		HTexture texture;
 
 		if (value != nullptr)
-			texture = value->getTextureHandle();
+			texture = value->getHandle();
 
-		nativeInstance->mMaterial->setTexture(paramName, texture);
+		nativeInstance->getHandle()->setTexture(paramName, texture);
 	}
 
 	float ScriptMaterial::internal_GetFloat(ScriptMaterial* nativeInstance, MonoString* name)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		return nativeInstance->mMaterial->getFloat(paramName);
+		return nativeInstance->getHandle()->getFloat(paramName);
 	}
 
 	Vector2 ScriptMaterial::internal_GetVector2(ScriptMaterial* nativeInstance, MonoString* name)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		return nativeInstance->mMaterial->getVec2(paramName);
+		return nativeInstance->getHandle()->getVec2(paramName);
 	}
 
 	Vector3 ScriptMaterial::internal_GetVector3(ScriptMaterial* nativeInstance, MonoString* name)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		return nativeInstance->mMaterial->getVec3(paramName);
+		return nativeInstance->getHandle()->getVec3(paramName);
 	}
 
 	Vector4 ScriptMaterial::internal_GetVector4(ScriptMaterial* nativeInstance, MonoString* name)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		return nativeInstance->mMaterial->getVec4(paramName);
+		return nativeInstance->getHandle()->getVec4(paramName);
 	}
 
 	Matrix3 ScriptMaterial::internal_GetMatrix3(ScriptMaterial* nativeInstance, MonoString* name)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		return nativeInstance->mMaterial->getMat3(paramName);
+		return nativeInstance->getHandle()->getMat3(paramName);
 	}
 
 	Matrix4 ScriptMaterial::internal_GetMatrix4(ScriptMaterial* nativeInstance, MonoString* name)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		return nativeInstance->mMaterial->getMat4(paramName);
+		return nativeInstance->getHandle()->getMat4(paramName);
 	}
 
 	Color ScriptMaterial::internal_GetColor(ScriptMaterial* nativeInstance, MonoString* name)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		return nativeInstance->mMaterial->getColor(paramName);
+		return nativeInstance->getHandle()->getColor(paramName);
 	}
 
 	MonoObject* ScriptMaterial::internal_GetTexture2D(ScriptMaterial* nativeInstance, MonoString* name)
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		HTexture texture = nativeInstance->mMaterial->getTexture(paramName);
+		HTexture texture = nativeInstance->getHandle()->getTexture(paramName);
 		if (texture == nullptr)
 			return nullptr;
 
@@ -252,7 +252,7 @@ namespace BansheeEngine
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		HTexture texture = nativeInstance->mMaterial->getTexture(paramName);
+		HTexture texture = nativeInstance->getHandle()->getTexture(paramName);
 		if (texture == nullptr)
 			return nullptr;
 
@@ -266,7 +266,7 @@ namespace BansheeEngine
 	{
 		String paramName = MonoUtil::monoToString(name);
 
-		HTexture texture = nativeInstance->mMaterial->getTexture(paramName);
+		HTexture texture = nativeInstance->getHandle()->getTexture(paramName);
 		if (texture == nullptr)
 			return nullptr;
 
@@ -275,17 +275,4 @@ namespace BansheeEngine
 
 		return scriptTexture->getManagedInstance();
 	}
-
-	void ScriptMaterial::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
-
-	void ScriptMaterial::setNativeHandle(const HResource& resource)
-	{
-		mMaterial = static_resource_cast<Material>(resource);
-	}
 }

+ 6 - 19
SBansheeEngine/Source/BsScriptMesh.cpp

@@ -29,7 +29,7 @@ namespace BansheeEngine
 	}
 
 	ScriptMesh::ScriptMesh(MonoObject* instance, const HMesh& mesh)
-		:ScriptObject(instance), mMesh(mesh)
+		:TScriptResource(instance, mesh)
 	{
 
 	}
@@ -77,7 +77,7 @@ namespace BansheeEngine
 
 	MonoArray* ScriptMesh::internal_GetSubMeshes(ScriptMesh* thisPtr)
 	{
-		HMesh mesh = thisPtr->getMeshHandle();
+		HMesh mesh = thisPtr->getHandle();
 
 		UINT32 numSubMeshes = mesh->getProperties().getNumSubMeshes();
 		ScriptArray subMeshArray = ScriptArray::create<ScriptSubMesh>(numSubMeshes);
@@ -99,14 +99,14 @@ namespace BansheeEngine
 
 	UINT32 ScriptMesh::internal_GetSubMeshCount(ScriptMesh* thisPtr)
 	{
-		HMesh mesh = thisPtr->getMeshHandle();
+		HMesh mesh = thisPtr->getHandle();
 
 		return mesh->getProperties().getNumSubMeshes();
 	}
 
 	void ScriptMesh::internal_GetBounds(ScriptMesh* thisPtr, AABox* box, Sphere* sphere)
 	{
-		HMesh mesh = thisPtr->getMeshHandle();
+		HMesh mesh = thisPtr->getHandle();
 
 		Bounds bounds = mesh->getProperties().getBounds();
 		*box = bounds.getBox();
@@ -115,7 +115,7 @@ namespace BansheeEngine
 
 	MonoObject* ScriptMesh::internal_GetMeshData(ScriptMesh* thisPtr)
 	{
-		HMesh mesh = thisPtr->getMeshHandle();
+		HMesh mesh = thisPtr->getHandle();
 
 		MeshDataPtr meshData = mesh->allocateSubresourceBuffer(0);
 		mesh->readData(*meshData);
@@ -125,7 +125,7 @@ namespace BansheeEngine
 
 	void ScriptMesh::internal_SetMeshData(ScriptMesh* thisPtr, ScriptMeshData* value)
 	{
-		HMesh mesh = thisPtr->getMeshHandle();
+		HMesh mesh = thisPtr->getHandle();
 		if (value != nullptr)
 		{
 			MeshDataPtr meshData = value->getInternalValue()->getData();
@@ -199,17 +199,4 @@ namespace BansheeEngine
 
 		return nativeSubMeshes;
 	}
-
-	void ScriptMesh::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
-
-	void ScriptMesh::setNativeHandle(const HResource& resource)
-	{
-		mMesh = static_resource_cast<Mesh>(resource);
-	}
 }

+ 3 - 16
SBansheeEngine/Source/BsScriptPlainText.cpp

@@ -12,7 +12,7 @@ using namespace std::placeholders;
 namespace BansheeEngine
 {
 	ScriptPlainText::ScriptPlainText(MonoObject* instance, const HPlainText& plainText)
-		:ScriptObject(instance), mPlainText(plainText)
+		:TScriptResource(instance, plainText)
 	{
 
 	}
@@ -35,28 +35,15 @@ namespace BansheeEngine
 
 	MonoString* ScriptPlainText::internal_getText(ScriptPlainText* thisPtr)
 	{
-		HPlainText plainText = thisPtr->mPlainText;
+		HPlainText plainText = thisPtr->getHandle();
 
 		return MonoUtil::wstringToMono(MonoManager::instance().getDomain(), plainText->getString());
 	}
 
 	void ScriptPlainText::internal_setText(ScriptPlainText* thisPtr, MonoString* text)
 	{
-		HPlainText plainText = thisPtr->mPlainText;
+		HPlainText plainText = thisPtr->getHandle();
 
 		plainText->setString(MonoUtil::monoToWString(text));
 	}
-
-	void ScriptPlainText::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
-
-	void ScriptPlainText::setNativeHandle(const HResource& resource)
-	{
-		mPlainText = static_resource_cast<PlainText>(resource);
-	}
 }

+ 2 - 15
SBansheeEngine/Source/BsScriptPrefab.cpp

@@ -11,7 +11,7 @@
 namespace BansheeEngine
 {
 	ScriptPrefab::ScriptPrefab(MonoObject* instance, const HPrefab& prefab)
-		:ScriptObject(instance), mPrefab(prefab)
+		:TScriptResource(instance, prefab)
 	{
 
 	}
@@ -32,24 +32,11 @@ namespace BansheeEngine
 
 	MonoObject* ScriptPrefab::internal_Instantiate(ScriptPrefab* thisPtr)
 	{
-		HPrefab prefab = thisPtr->getPrefabHandle();
+		HPrefab prefab = thisPtr->getHandle();
 
 		HSceneObject instance = prefab->instantiate();
 		ScriptSceneObject* scriptInstance = ScriptGameObjectManager::instance().getOrCreateScriptSceneObject(instance);
 
 		return scriptInstance->getManagedInstance();
 	}
-		
-	void ScriptPrefab::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
-
-	void ScriptPrefab::setNativeHandle(const HResource& resource)
-	{
-		mPrefab = static_resource_cast<Prefab>(resource);
-	}
 }

+ 3 - 3
SBansheeEngine/Source/BsScriptRenderTexture2D.cpp

@@ -60,7 +60,7 @@ namespace BansheeEngine
 			depthStencilSurfaceDesc.face = 0;
 			depthStencilSurfaceDesc.mipLevel = 0;
 
-			HTexture textureHandle = scriptDepthStencilSurface->getTextureHandle();
+			HTexture textureHandle = scriptDepthStencilSurface->getHandle();
 			if (!textureHandle.isLoaded())
 			{
 				LOGERR("Render texture must be created using a fully loaded texture.");
@@ -88,7 +88,7 @@ namespace BansheeEngine
 
 				if (scriptSurface != nullptr)
 				{
-					HTexture textureHandle = scriptSurface->getTextureHandle();
+					HTexture textureHandle = scriptSurface->getHandle();
 					if (!textureHandle.isLoaded())
 					{
 						LOGERR("Render texture must be created using a fully loaded texture.");
@@ -117,7 +117,7 @@ namespace BansheeEngine
 
 				if (scriptSurface != nullptr)
 				{
-					HTexture textureHandle = scriptSurface->getTextureHandle();
+					HTexture textureHandle = scriptSurface->getHandle();
 					if (!textureHandle.isLoaded())
 					{
 						LOGERR("Render texture must be created using a fully loaded texture.");

+ 3 - 3
SBansheeEngine/Source/BsScriptRenderable.cpp

@@ -73,7 +73,7 @@ namespace BansheeEngine
 	{
 		HMesh nativeMesh;
 		if (mesh != nullptr)
-			nativeMesh = mesh->getMeshHandle();
+			nativeMesh = mesh->getHandle();
 
 		thisPtr->getInternal()->setMesh(nativeMesh);
 	}
@@ -112,7 +112,7 @@ namespace BansheeEngine
 				ScriptMaterial* scriptMaterial = ScriptMaterial::toNative(monoMaterial);
 
 				if (scriptMaterial != nullptr)
-					nativeMaterials[i] = scriptMaterial->getMaterialHandle();
+					nativeMaterials[i] = scriptMaterial->getHandle();
 			}
 
 			thisPtr->getInternal()->setMaterials(nativeMaterials);
@@ -127,7 +127,7 @@ namespace BansheeEngine
 	{
 		HMaterial nativeMaterial;
 		if (material != nullptr)
-			nativeMaterial = material->getMaterialHandle();
+			nativeMaterial = material->getHandle();
 
 		thisPtr->getInternal()->setMaterial(index, nativeMaterial);
 	}

+ 9 - 0
SBansheeEngine/Source/BsScriptResource.cpp

@@ -1,4 +1,5 @@
 #include "BsScriptResource.h"
+#include "BsScriptResourceManager.h"
 
 namespace BansheeEngine
 {
@@ -20,6 +21,14 @@ namespace BansheeEngine
 		PersistentScriptObjectBase::endRefresh(backupData);
 	}
 
+	void ScriptResourceBase::destroy()
+	{
+		mManagedInstance = nullptr;
+
+		if (!mRefreshInProgress)
+			ScriptResourceManager::instance().destroyScriptResource(this);
+	}
+
 	void ScriptResource::initRuntimeData()
 	{
 		

+ 21 - 5
SBansheeEngine/Source/BsScriptResourceManager.cpp

@@ -2,6 +2,7 @@
 #include "BsMonoManager.h"
 #include "BsMonoAssembly.h"
 #include "BsMonoClass.h"
+#include "BsResources.h"
 #include "BsScriptTexture2D.h"
 #include "BsScriptTexture3D.h"
 #include "BsScriptTextureCube.h"
@@ -18,10 +19,18 @@
 #include "BsScriptManagedResource.h"
 #include "BsScriptAssemblyManager.h"
 
+using namespace std::placeholders;
+
 namespace BansheeEngine
 {
 	ScriptResourceManager::ScriptResourceManager()
 	{
+		mResourceDestroyedConn = gResources().onResourceDestroyed.connect(std::bind(&ScriptResourceManager::onResourceDestroyed, this, _1));
+	}
+
+	ScriptResourceManager::~ScriptResourceManager()
+	{
+		mResourceDestroyedConn.disconnect();
 	}
 
 	template<class RetType, class InType>
@@ -179,18 +188,25 @@ namespace BansheeEngine
 
 	void ScriptResourceManager::destroyScriptResource(ScriptResourceBase* resource)
 	{
-		HResource resourceHandle = resource->getNativeHandle();
+		HResource resourceHandle = resource->getGenericHandle();
 		const String& uuid = resourceHandle.getUUID();
 
 		if(uuid == "")
 			BS_EXCEPT(InvalidParametersException, "Provided resource handle has an undefined resource UUID.");
 
+		(resource)->~ScriptResourceBase();
+		MemoryAllocator<GenAlloc>::free(resource);
+
+		auto findIter = mScriptResources.erase(uuid);
+	}
+
+	void ScriptResourceManager::onResourceDestroyed(const HResource& resource)
+	{
+		const String& uuid = resource.getUUID();
 		auto findIter = mScriptResources.find(uuid);
-		if(findIter != mScriptResources.end())
+		if (findIter != mScriptResources.end())
 		{
-			(resource)->~ScriptResourceBase();
-			MemoryAllocator<GenAlloc>::free(resource);
-
+			findIter->second->notifyResourceDestroyed();
 			mScriptResources.erase(findIter);
 		}
 	}

+ 1 - 1
SBansheeEngine/Source/BsScriptResources.cpp

@@ -37,7 +37,7 @@ namespace BansheeEngine
 
 	void ScriptResources::internal_Unload(ScriptResourceBase* resourcePtr)
 	{
-		gResources().unload(resourcePtr->getNativeHandle());
+		gResources().unload(resourcePtr->getGenericHandle());
 	}
 
 	void ScriptResources::internal_UnloadUnused()

+ 9 - 20
SBansheeEngine/Source/BsScriptScriptCode.cpp

@@ -14,7 +14,7 @@ using namespace std::placeholders;
 namespace BansheeEngine
 {
 	ScriptScriptCode::ScriptScriptCode(MonoObject* instance, const HScriptCode& scriptCode)
-		:ScriptObject(instance), mScriptCode(scriptCode)
+		:TScriptResource(instance, scriptCode)
 	{
 		
 	}
@@ -40,7 +40,7 @@ namespace BansheeEngine
 
 	MonoString* ScriptScriptCode::internal_getText(ScriptScriptCode* thisPtr)
 	{
-		HScriptCode scriptCode = thisPtr->mScriptCode;
+		HScriptCode scriptCode = thisPtr->getHandle();
 		if (!scriptCode.isLoaded())
 			MonoUtil::wstringToMono(MonoManager::instance().getDomain(), L"");
 
@@ -49,7 +49,7 @@ namespace BansheeEngine
 
 	void ScriptScriptCode::internal_setText(ScriptScriptCode* thisPtr, MonoString* text)
 	{
-		HScriptCode scriptCode = thisPtr->mScriptCode;
+		HScriptCode scriptCode = thisPtr->getHandle();
 		if (!scriptCode.isLoaded())
 			return;
 
@@ -58,7 +58,7 @@ namespace BansheeEngine
 
 	bool ScriptScriptCode::internal_isEditorScript(ScriptScriptCode* thisPtr)
 	{
-		HScriptCode scriptCode = thisPtr->mScriptCode;
+		HScriptCode scriptCode = thisPtr->getHandle();
 		if (!scriptCode.isLoaded())
 			return false;
 
@@ -67,7 +67,7 @@ namespace BansheeEngine
 
 	void ScriptScriptCode::internal_setEditorScript(ScriptScriptCode* thisPtr, bool value)
 	{
-		HScriptCode scriptCode = thisPtr->mScriptCode;
+		HScriptCode scriptCode = thisPtr->getHandle();
 		if (!scriptCode.isLoaded())
 			return;
 
@@ -76,9 +76,11 @@ namespace BansheeEngine
 	
 	MonoArray* ScriptScriptCode::internal_getTypes(ScriptScriptCode* thisPtr)
 	{
+		HScriptCode scriptCode = thisPtr->getHandle();
+
 		Vector<FullTypeName> types;
-		if (thisPtr->getScriptCodeHandle().isLoaded())
-			types = parseTypes(thisPtr->getScriptCodeHandle()->getString());
+		if (scriptCode.isLoaded())
+			types = parseTypes(scriptCode->getString());
 
 		Vector<MonoReflectionType*> validTypes;
 		for (auto& type : types)
@@ -102,19 +104,6 @@ namespace BansheeEngine
 		return output;
 	}
 
-	void ScriptScriptCode::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
-
-	void ScriptScriptCode::setNativeHandle(const HResource& resource)
-	{
-		mScriptCode = static_resource_cast<ScriptCode>(resource);
-	}
-
 	Vector<ScriptScriptCode::FullTypeName> ScriptScriptCode::parseTypes(const WString& code)
 	{
 		struct NamespaceData

+ 6 - 17
SBansheeEngine/Source/BsScriptShader.cpp

@@ -8,8 +8,10 @@
 
 namespace BansheeEngine
 {
-	// Note: This must match C# ShaderParameterType enum
-	enum class ShaderParameterType
+	/**
+	 * @brief	Shader parameter types that can be handled using managed code.
+	 */
+	enum class ShaderParameterType // Note: This must match C# ShaderParameterType enum
 	{
 		Float, Vector2, Vector3, Vector4, Color,
 		Matrix3, Matrix4, Texture2D,
@@ -17,7 +19,7 @@ namespace BansheeEngine
 	};
 
 	ScriptShader::ScriptShader(MonoObject* instance, const HShader& shader)
-		:ScriptObject(instance), mShader(shader)
+		:TScriptResource(instance, shader)
 	{
 
 	}
@@ -29,7 +31,7 @@ namespace BansheeEngine
 
 	void ScriptShader::internal_GetShaderParameters(ScriptShader* nativeInstance, MonoArray** outNames, MonoArray** outTypes)
 	{
-		HShader shader = nativeInstance->getShaderHandle();
+		HShader shader = nativeInstance->getHandle();
 		if (!shader.isLoaded())
 			return;
 
@@ -136,17 +138,4 @@ namespace BansheeEngine
 		*outNames = names.getInternal();
 		*outTypes = types.getInternal();
 	}
-	
-	void ScriptShader::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
-
-	void ScriptShader::setNativeHandle(const HResource& resource)
-	{
-		mShader = static_resource_cast<Shader>(resource);
-	}
 }

+ 11 - 24
SBansheeEngine/Source/BsScriptSpriteTexture.cpp

@@ -10,7 +10,7 @@
 namespace BansheeEngine
 {
 	ScriptSpriteTexture::ScriptSpriteTexture(MonoObject* instance,const HSpriteTexture& texture)
-		:ScriptObject(instance), mTexture(texture)
+		:TScriptResource(instance, texture)
 	{
 
 	}
@@ -28,19 +28,6 @@ namespace BansheeEngine
 		metaData.scriptClass->addInternalCall("Internal_GetHeight", &ScriptSpriteTexture::internal_GetHeight);
 	}
 
-	void ScriptSpriteTexture::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
-
-	void ScriptSpriteTexture::setNativeHandle(const HResource& resource) 
-	{ 
-		mTexture = static_resource_cast<SpriteTexture>(resource);
-	}
-
 	MonoObject* ScriptSpriteTexture::toManaged(const HSpriteTexture& texture)
 	{
 		if (texture == nullptr)
@@ -63,7 +50,7 @@ namespace BansheeEngine
 		}
 		else
 		{
-			HSpriteTexture spriteTexture = SpriteTexture::create(offset, scale, static_resource_cast<Texture>(scriptTexture->getNativeHandle()));
+			HSpriteTexture spriteTexture = SpriteTexture::create(offset, scale, scriptTexture->getHandle());
 
 			ScriptResourceManager::instance().createScriptResource(instance, spriteTexture, &scriptInstance);
 		}
@@ -71,7 +58,7 @@ namespace BansheeEngine
 
 	MonoObject* ScriptSpriteTexture::internal_GetTexture(ScriptSpriteTexture* thisPtr)
 	{
-		HSpriteTexture spriteTexture = thisPtr->mTexture;
+		HSpriteTexture spriteTexture = thisPtr->getHandle();
 		if (!spriteTexture.isLoaded())
 			return nullptr;
 
@@ -87,20 +74,20 @@ namespace BansheeEngine
 
 	void ScriptSpriteTexture::internal_SetTexture(ScriptSpriteTexture* thisPtr, ScriptTexture2D* value)
 	{
-		HSpriteTexture spriteTexture = thisPtr->mTexture;
+		HSpriteTexture spriteTexture = thisPtr->getHandle();
 		if (!spriteTexture.isLoaded())
 			return;
 
 		HTexture texture;
 		if (value != nullptr)
-			texture = value->getTextureHandle();
+			texture = value->getHandle();
 
 		spriteTexture->setTexture(texture);
 	}
 
 	void ScriptSpriteTexture::internal_GetOffset(ScriptSpriteTexture* thisPtr, Vector2* value)
 	{
-		HSpriteTexture spriteTexture = thisPtr->mTexture;
+		HSpriteTexture spriteTexture = thisPtr->getHandle();
 		if (!spriteTexture.isLoaded())
 		{
 			*value = Vector2::ZERO;
@@ -112,7 +99,7 @@ namespace BansheeEngine
 
 	void ScriptSpriteTexture::internal_SetOffset(ScriptSpriteTexture* thisPtr, Vector2 value)
 	{
-		HSpriteTexture spriteTexture = thisPtr->mTexture;
+		HSpriteTexture spriteTexture = thisPtr->getHandle();
 		if (!spriteTexture.isLoaded())
 			return;
 
@@ -121,7 +108,7 @@ namespace BansheeEngine
 
 	void ScriptSpriteTexture::internal_GetScale(ScriptSpriteTexture* thisPtr, Vector2* value)
 	{
-		HSpriteTexture spriteTexture = thisPtr->mTexture;
+		HSpriteTexture spriteTexture = thisPtr->getHandle();
 		if (!spriteTexture.isLoaded())
 		{
 			*value = Vector2::ZERO;
@@ -133,7 +120,7 @@ namespace BansheeEngine
 
 	void ScriptSpriteTexture::internal_SetScale(ScriptSpriteTexture* thisPtr, Vector2 value)
 	{
-		HSpriteTexture spriteTexture = thisPtr->mTexture;
+		HSpriteTexture spriteTexture = thisPtr->getHandle();
 		if (!spriteTexture.isLoaded())
 			return;
 
@@ -142,7 +129,7 @@ namespace BansheeEngine
 
 	UINT32 ScriptSpriteTexture::internal_GetWidth(ScriptSpriteTexture* thisPtr)
 	{
-		HSpriteTexture spriteTexture = thisPtr->mTexture;
+		HSpriteTexture spriteTexture = thisPtr->getHandle();
 		if (!spriteTexture.isLoaded())
 			return 0;
 
@@ -151,7 +138,7 @@ namespace BansheeEngine
 
 	UINT32 ScriptSpriteTexture::internal_GetHeight(ScriptSpriteTexture* thisPtr)
 	{
-		HSpriteTexture spriteTexture = thisPtr->mTexture;
+		HSpriteTexture spriteTexture = thisPtr->getHandle();
 		if (!spriteTexture.isLoaded())
 			return 0;
 

+ 8 - 21
SBansheeEngine/Source/BsScriptStringTable.cpp

@@ -10,7 +10,7 @@
 namespace BansheeEngine
 {
 	ScriptStringTable::ScriptStringTable(MonoObject* instance, const HStringTable& table)
-		:ScriptObject(instance), mTable(table)
+		:TScriptResource(instance, table)
 	{ }
 
 	void ScriptStringTable::initRuntimeData()
@@ -37,12 +37,12 @@ namespace BansheeEngine
 
 	UINT32 ScriptStringTable::internal_GetNumStrings(ScriptStringTable* thisPtr)
 	{
-		return (UINT32)thisPtr->getStringTableHandle()->getIdentifiers().size();
+		return (UINT32)thisPtr->getHandle()->getIdentifiers().size();
 	}
 
 	MonoArray* ScriptStringTable::internal_GetIdentifiers(ScriptStringTable* thisPtr)
 	{
-		const UnorderedSet<WString>& identifiers = thisPtr->getStringTableHandle()->getIdentifiers();
+		const UnorderedSet<WString>& identifiers = thisPtr->getHandle()->getIdentifiers();
 		UINT32 numIdentifiers = (UINT32)identifiers.size();
 
 		ScriptArray outArray = ScriptArray::create<WString>(numIdentifiers);
@@ -62,7 +62,7 @@ namespace BansheeEngine
 		WString nativeIdentifier = MonoUtil::monoToWString(identifier);
 		WString nativeValue = MonoUtil::monoToWString(value);
 
-		thisPtr->getStringTableHandle()->setString(nativeIdentifier, language, nativeValue);
+		thisPtr->getHandle()->setString(nativeIdentifier, language, nativeValue);
 	}
 
 	void ScriptStringTable::internal_SetStringDefault(ScriptStringTable* thisPtr, MonoString* identifier, MonoString* value)
@@ -70,19 +70,19 @@ namespace BansheeEngine
 		WString nativeIdentifier = MonoUtil::monoToWString(identifier);
 		WString nativeValue = MonoUtil::monoToWString(value);
 
-		thisPtr->getStringTableHandle()->setString(nativeIdentifier, StringTableManager::instance().getActiveLanguage(), nativeValue);
+		thisPtr->getHandle()->setString(nativeIdentifier, StringTableManager::instance().getActiveLanguage(), nativeValue);
 	}
 
 	void ScriptStringTable::internal_RemoveString(ScriptStringTable* thisPtr, MonoString* identifier)
 	{
 		WString nativeIdentifier = MonoUtil::monoToWString(identifier);
-		thisPtr->getStringTableHandle()->removeString(nativeIdentifier);
+		thisPtr->getHandle()->removeString(nativeIdentifier);
 	}
 
 	void ScriptStringTable::internal_GetString(ScriptStringTable* thisPtr, MonoString* identifier, Language language, MonoString** value)
 	{
 		WString nativeIdentifier = MonoUtil::monoToWString(identifier);
-		WString nativeValue = thisPtr->getStringTableHandle()->getString(nativeIdentifier, language);
+		WString nativeValue = thisPtr->getHandle()->getString(nativeIdentifier, language);
 
 		*value = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), nativeValue);
 	}
@@ -90,21 +90,8 @@ namespace BansheeEngine
 	void ScriptStringTable::internal_GetStringDefault(ScriptStringTable* thisPtr, MonoString* identifier, MonoString** value)
 	{
 		WString nativeIdentifier = MonoUtil::monoToWString(identifier);
-		WString nativeValue = thisPtr->getStringTableHandle()->getString(nativeIdentifier, StringTableManager::instance().getActiveLanguage());
+		WString nativeValue = thisPtr->getHandle()->getString(nativeIdentifier, StringTableManager::instance().getActiveLanguage());
 
 		*value = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), nativeValue);
 	}
-
-	void ScriptStringTable::setNativeHandle(const HResource& resource)
-	{
-		mTable = static_resource_cast<StringTable>(resource);
-	}
-
-	void ScriptStringTable::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
 }

+ 1 - 1
SBansheeEngine/Source/BsScriptStringTableManager.cpp

@@ -47,7 +47,7 @@ namespace BansheeEngine
 	{
 		HStringTable nativeTable;
 		if (table != nullptr)
-			nativeTable = ScriptStringTable::toNative(table)->getStringTableHandle();
+			nativeTable = ScriptStringTable::toNative(table)->getHandle();
 
 		StringTableManager::instance().setTable(id, nativeTable);
 	}

+ 7 - 7
SBansheeEngine/Source/BsScriptTexture.cpp

@@ -25,43 +25,43 @@ namespace BansheeEngine
 
 	void ScriptTexture::internal_getPixelFormat(ScriptTexture* thisPtr, PixelFormat* value)
 	{
-		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getGenericHandle());
 		*value = texture->getProperties().getFormat();
 	}
 
 	void ScriptTexture::internal_getUsage(ScriptTexture* thisPtr, TextureUsage* value)
 	{
-		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getGenericHandle());
 		*value = (TextureUsage)texture->getProperties().getUsage();
 	}
 
 	void ScriptTexture::internal_getWidth(ScriptTexture* thisPtr, int* value)
 	{
-		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getGenericHandle());
 		*value = (TextureUsage)texture->getProperties().getWidth();
 	}
 
 	void ScriptTexture::internal_getHeight(ScriptTexture* thisPtr, int* value)
 	{
-		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getGenericHandle());
 		*value = (TextureUsage)texture->getProperties().getHeight();
 	}
 
 	void ScriptTexture::internal_getGammaCorrection(ScriptTexture* thisPtr, bool* value)
 	{
-		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getGenericHandle());
 		*value = (TextureUsage)texture->getProperties().isHardwareGammaEnabled();
 	}
 
 	void ScriptTexture::internal_getSampleCount(ScriptTexture* thisPtr, int* value)
 	{
-		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getGenericHandle());
 		*value = (TextureUsage)texture->getProperties().getMultisampleCount();
 	}
 
 	void ScriptTexture::internal_getMipmapCount(ScriptTexture* thisPtr, int* value)
 	{
-		HTexture texture = static_resource_cast<Texture>(thisPtr->getNativeHandle());
+		HTexture texture = static_resource_cast<Texture>(thisPtr->getGenericHandle());
 		*value = (TextureUsage)texture->getProperties().getNumMipmaps();
 	}
 }

+ 7 - 20
SBansheeEngine/Source/BsScriptTexture2D.cpp

@@ -16,7 +16,7 @@ using namespace std::placeholders;
 namespace BansheeEngine
 {
 	ScriptTexture2D::ScriptTexture2D(MonoObject* instance, const HTexture& texture)
-		:ScriptObject(instance), mTexture(texture)
+		:TScriptResource(instance, texture)
 	{
 
 	}
@@ -45,19 +45,19 @@ namespace BansheeEngine
 
 	MonoObject* ScriptTexture2D::internal_getPixels(ScriptTexture2D* thisPtr, UINT32 mipLevel)
 	{
-		HTexture texture = thisPtr->mTexture;
+		HTexture texture = thisPtr->getHandle();
 		UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
 
-		PixelDataPtr pixelData = thisPtr->mTexture->getProperties().allocateSubresourceBuffer(subresourceIdx);
+		PixelDataPtr pixelData = texture->getProperties().allocateSubresourceBuffer(subresourceIdx);
 
-		thisPtr->mTexture->readData(*pixelData, mipLevel);
+		texture->readData(*pixelData, mipLevel);
 
 		return ScriptPixelData::create(pixelData);
 	}
 
 	MonoObject* ScriptTexture2D::internal_getGPUPixels(ScriptTexture2D* thisPtr, UINT32 mipLevel)
 	{
-		HTexture texture = thisPtr->mTexture;
+		HTexture texture = thisPtr->getHandle();
 		UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
 
 		PixelDataPtr readData = texture->getProperties().allocateSubresourceBuffer(subresourceIdx);
@@ -79,7 +79,7 @@ namespace BansheeEngine
 
 		if (scriptPixelData != nullptr)
 		{
-			HTexture texture = thisPtr->mTexture;
+			HTexture texture = thisPtr->getHandle();
 			UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
 
 			texture->writeSubresource(gCoreAccessor(), subresourceIdx, scriptPixelData->getInternalValue(), false);
@@ -91,7 +91,7 @@ namespace BansheeEngine
 		Color* colorsRaw = (Color*)mono_array_addr_with_size(colors, sizeof(Color), 0);
 		UINT32 numElements = (UINT32)mono_array_length(colors);
 
-		HTexture texture = thisPtr->mTexture;
+		HTexture texture = thisPtr->getHandle();
 		const TextureProperties& props = texture->getProperties();
 
 		PixelDataPtr pixelData = bs_shared_ptr_new<PixelData>(props.getWidth(), props.getHeight(), props.getDepth(), props.getFormat());
@@ -101,17 +101,4 @@ namespace BansheeEngine
 		UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
 		texture->writeSubresource(gCoreAccessor(), subresourceIdx, pixelData, false);
 	}
-
-	void ScriptTexture2D::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
-
-	void ScriptTexture2D::setNativeHandle(const HResource& resource) 
-	{ 
-		mTexture = static_resource_cast<Texture>(resource);
-	}
 }

+ 6 - 19
SBansheeEngine/Source/BsScriptTexture3D.cpp

@@ -16,7 +16,7 @@ using namespace std::placeholders;
 namespace BansheeEngine
 {
 	ScriptTexture3D::ScriptTexture3D(MonoObject* instance, const HTexture& texture)
-		:ScriptObject(instance), mTexture(texture)
+		:TScriptResource(instance, texture)
 	{
 
 	}
@@ -44,19 +44,19 @@ namespace BansheeEngine
 
 	MonoObject* ScriptTexture3D::internal_getPixels(ScriptTexture3D* thisPtr, UINT32 mipLevel)
 	{
-		HTexture texture = thisPtr->mTexture;
+		HTexture texture = thisPtr->getHandle();
 		UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
 
-		PixelDataPtr pixelData = thisPtr->mTexture->getProperties().allocateSubresourceBuffer(subresourceIdx);
+		PixelDataPtr pixelData = texture->getProperties().allocateSubresourceBuffer(subresourceIdx);
 
-		thisPtr->mTexture->readData(*pixelData, mipLevel);
+		texture->readData(*pixelData, mipLevel);
 
 		return ScriptPixelData::create(pixelData);
 	}
 
 	MonoObject* ScriptTexture3D::internal_getGPUPixels(ScriptTexture3D* thisPtr, UINT32 mipLevel)
 	{
-		HTexture texture = thisPtr->mTexture;
+		HTexture texture = thisPtr->getHandle();
 		UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
 
 		PixelDataPtr readData = texture->getProperties().allocateSubresourceBuffer(subresourceIdx);
@@ -78,23 +78,10 @@ namespace BansheeEngine
 
 		if (scriptPixelData != nullptr)
 		{
-			HTexture texture = thisPtr->mTexture;
+			HTexture texture = thisPtr->getHandle();
 			UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(0, mipLevel);
 
 			texture->writeSubresource(gCoreAccessor(), subresourceIdx, scriptPixelData->getInternalValue(), false);
 		}
 	}
-
-	void ScriptTexture3D::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
-
-	void ScriptTexture3D::setNativeHandle(const HResource& resource)
-	{
-		mTexture = static_resource_cast<Texture>(resource);
-	}
 }

+ 6 - 19
SBansheeEngine/Source/BsScriptTextureCube.cpp

@@ -16,7 +16,7 @@ using namespace std::placeholders;
 namespace BansheeEngine
 {
 	ScriptTextureCube::ScriptTextureCube(MonoObject* instance, const HTexture& texture)
-		:ScriptObject(instance), mTexture(texture)
+		:TScriptResource(instance, texture)
 	{
 
 	}
@@ -44,19 +44,19 @@ namespace BansheeEngine
 
 	MonoObject* ScriptTextureCube::internal_getPixels(ScriptTextureCube* thisPtr, UINT32 face, UINT32 mipLevel)
 	{
-		HTexture texture = thisPtr->mTexture;
+		HTexture texture = thisPtr->getHandle();
 		UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(face, mipLevel);
 
-		PixelDataPtr pixelData = thisPtr->mTexture->getProperties().allocateSubresourceBuffer(subresourceIdx);
+		PixelDataPtr pixelData = texture->getProperties().allocateSubresourceBuffer(subresourceIdx);
 
-		thisPtr->mTexture->readData(*pixelData, mipLevel, face);
+		texture->readData(*pixelData, mipLevel, face);
 
 		return ScriptPixelData::create(pixelData);
 	}
 
 	MonoObject* ScriptTextureCube::internal_getGPUPixels(ScriptTextureCube* thisPtr, UINT32 face, UINT32 mipLevel)
 	{
-		HTexture texture = thisPtr->mTexture;
+		HTexture texture = thisPtr->getHandle();
 		UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(face, mipLevel);
 
 		PixelDataPtr readData = texture->getProperties().allocateSubresourceBuffer(subresourceIdx);
@@ -77,23 +77,10 @@ namespace BansheeEngine
 
 		if (scriptPixelData != nullptr)
 		{
-			HTexture texture = thisPtr->mTexture;
+			HTexture texture = thisPtr->getHandle();
 			UINT32 subresourceIdx = texture->getProperties().mapToSubresourceIdx(face, mipLevel);
 
 			texture->writeSubresource(gCoreAccessor(), subresourceIdx, scriptPixelData->getInternalValue(), false);
 		}
 	}
-
-	void ScriptTextureCube::_onManagedInstanceDeleted()
-	{
-		mManagedInstance = nullptr;
-
-		if (!mRefreshInProgress)
-			ScriptResourceManager::instance().destroyScriptResource(this);
-	}
-
-	void ScriptTextureCube::setNativeHandle(const HResource& resource)
-	{
-		mTexture = static_resource_cast<Texture>(resource);
-	}
 }