// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. #pragma once #include "ScriptingType.h" #include "Engine/Core/Object.h" #include "Engine/Core/Delegate.h" #include "ManagedCLR/MTypes.h" /// /// Represents object from unmanaged memory that can use accessed via C# scripting. /// API_CLASS(InBuild) class FLAXENGINE_API ScriptingObject : public RemovableObject { friend class Scripting; friend class BinaryModule; DECLARE_SCRIPTING_TYPE_NO_SPAWN(ScriptingObject); public: typedef ScriptingObjectSpawnParams SpawnParams; protected: uint32 _gcHandle; ScriptingTypeHandle _type; Guid _id; public: /// /// Initializes a new instance of the class. /// /// The object initialization parameters. explicit ScriptingObject(const SpawnParams& params); /// /// Finalizes an instance of the class. /// virtual ~ScriptingObject(); public: /// /// Spawns a new objects of the given type. /// template static T* Spawn() { const SpawnParams params(Guid::New(), T::TypeInitializer); return T::New(params); } public: /// /// Event fired when object gets deleted. /// Delegate Deleted; public: /// /// Gets the managed instance object. /// /// The Mono managed object or null if not created. MonoObject* GetManagedInstance() const; /// /// Gets the managed instance object or creates it if missing. /// /// The Mono managed object. FORCE_INLINE MonoObject* GetOrCreateManagedInstance() const { MonoObject* managedInstance = GetManagedInstance(); if (!managedInstance) { const_cast(this)->CreateManaged(); managedInstance = GetManagedInstance(); } return managedInstance; } /// /// Determines whether managed instance is alive. /// /// True if managed object has been created and exists, otherwise false. FORCE_INLINE bool HasManagedInstance() const { return GetManagedInstance() != nullptr; } /// /// Gets the unique object ID. /// /// The unique object ID. FORCE_INLINE const Guid& GetID() const { return _id; } /// /// Gets the scripting type handle of this object. /// /// The scripting type handle. FORCE_INLINE const ScriptingTypeHandle& GetTypeHandle() const { return _type; } /// /// Gets the scripting type of this object. /// /// The scripting type. FORCE_INLINE const ScriptingType& GetType() const { return _type.GetType(); } /// /// Gets the type class of this object. /// /// The Mono class. MClass* GetClass() const; public: static ScriptingObject* ToNative(MonoObject* obj); static MonoObject* ToManaged(ScriptingObject* obj) { return obj ? obj->GetOrCreateManagedInstance() : nullptr; } /// /// Checks if can cast one scripting object type into another type. /// /// The object type for the cast. /// The destination type to the cast. /// True if can, otherwise false. static bool CanCast(const ScriptingTypeHandle& from, const ScriptingTypeHandle& to); /// /// Checks if can cast one scripting object type into another type. /// /// The object class for the cast. /// The destination class to the cast. /// True if can, otherwise false. static bool CanCast(MClass* from, MClass* to); template static T* Cast(ScriptingObject* obj) { return obj && CanCast(obj->GetClass(), T::GetStaticClass()) ? (T*)obj : nullptr; } bool Is(const ScriptingTypeHandle& type) const; bool Is(MClass* type) const { return CanCast(GetClass(), type); } template bool Is() const { return CanCast(GetClass(), T::GetStaticClass()); } public: /// /// Changes the object id (both managed and unmanaged). Warning! Use with caution as object ID is what it identifies it and change might cause issues. /// /// The new ID. virtual void ChangeID(const Guid& newId); public: virtual void OnManagedInstanceDeleted(); virtual void OnScriptingDispose(); virtual void CreateManaged() = 0; virtual void DestroyManaged(); public: /// /// Determines whether this object is registered or not (can be found by the queries and used in a game). /// /// true if this instance is registered; otherwise, false. FORCE_INLINE bool IsRegistered() const { return (Flags & ObjectFlags::IsRegistered) != 0; } /// /// Registers the object (cannot be called when objects has been already registered). /// void RegisterObject(); /// /// Unregisters the object (cannot be called when objects has not been already registered). /// void UnregisterObject(); protected: /// /// Create a new managed object. /// MonoObject* CreateManagedInternal(); public: // [RemovableObject] void OnDeleteObject() override; String ToString() const override; }; /// /// Managed object that uses weak GC handle to track the target object location in memory. /// Can be destroyed by the GC. /// Used by the objects that lifetime is controlled by the C# side. /// API_CLASS(InBuild) class FLAXENGINE_API ManagedScriptingObject : public ScriptingObject { public: /// /// Initializes a new instance of the class. /// /// The object initialization parameters. explicit ManagedScriptingObject(const SpawnParams& params); public: // [ScriptingObject] void CreateManaged() override; }; /// /// Managed object that uses pinned GC handle to prevent collecting and moving. /// Used by the objects that lifetime is controlled by the C++ side. /// API_CLASS(InBuild) class FLAXENGINE_API PersistentScriptingObject : public ScriptingObject { public: /// /// Initializes a new instance of the class. /// /// The object initialization parameters. explicit PersistentScriptingObject(const SpawnParams& params); /// /// Finalizes an instance of the class. /// ~PersistentScriptingObject(); public: // [ManagedScriptingObject] void OnManagedInstanceDeleted() override; void OnScriptingDispose() override; void CreateManaged() override; };