// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved. #pragma once #include "MTypes.h" /// /// Main handler for CLR Engine. /// class FLAXENGINE_API MCore { public: /// /// Gets the root domain. /// static MDomain* GetRootDomain(); /// /// Gets the currently active domain. /// static MDomain* GetActiveDomain(); /// /// Creates an new empty domain. /// /// The domain name to create. /// The domain object. static MDomain* CreateDomain(const StringAnsi& domainName); /// /// Unloads the domain. /// /// The domain name to remove. static void UnloadDomain(const StringAnsi& domainName); public: /// /// Initialize CLR Engine /// /// True if failed, otherwise false. static bool LoadEngine(); /// /// Unload CLR Engine /// static void UnloadEngine(); #if USE_EDITOR // Called by Scripting in a middle of hot-reload (after unloading modules but before loading them again). static void ReloadScriptingAssemblyLoadContext(); #endif public: /// /// Utilities for C# object management. /// struct FLAXENGINE_API Object { static MObject* Box(void* value, const MClass* klass); static void* Unbox(MObject* obj); static MObject* New(const MClass* klass); static void Init(MObject* obj); static MClass* GetClass(MObject* obj); static MString* ToString(MObject* obj); static int32 GetHashCode(MObject* obj); }; /// /// Utilities for C# string management. /// struct FLAXENGINE_API String { static MString* GetEmpty(MDomain* domain = nullptr); static MString* New(const char* str, int32 length, MDomain* domain = nullptr); static MString* New(const Char* str, int32 length, MDomain* domain = nullptr); static StringView GetChars(MString* obj); }; /// /// Utilities for C# array management. /// struct FLAXENGINE_API Array { static MArray* New(const MClass* elementKlass, int32 length); static MClass* GetClass(MClass* elementKlass); static int32 GetLength(const MArray* obj); static void* GetAddress(const MArray* obj); static MArray* Unbox(MObject* obj); template FORCE_INLINE static T* GetAddress(const MArray* obj) { return (T*)GetAddress(obj); } }; /// /// Utilities for GC Handle management. /// struct FLAXENGINE_API GCHandle { static MGCHandle New(MObject* obj, bool pinned = false); static MGCHandle NewWeak(MObject* obj, bool trackResurrection = false); static MObject* GetTarget(const MGCHandle& handle); static void Free(const MGCHandle& handle); }; /// /// Helper utilities for C# garbage collector. /// struct FLAXENGINE_API GC { static void Collect(); static void Collect(int32 generation); static void Collect(int32 generation, MGCCollectionMode collectionMode, bool blocking, bool compacting); static int32 MaxGeneration(); static void WaitForPendingFinalizers(); static void WriteRef(void* ptr, MObject* ref); static void WriteValue(void* dst, void* src, int32 count, const MClass* klass); static void WriteArrayRef(MArray* dst, MObject* ref, int32 index); static void WriteArrayRef(MArray* dst, Span span); #if USE_NETCORE static void* AllocateMemory(int32 size, bool coTaskMem = false); static void FreeMemory(void* ptr, bool coTaskMem = false); #endif }; /// /// Utilities for C# threads management. /// struct FLAXENGINE_API Thread { static void Attach(); static void Exit(); static bool IsAttached(); }; /// /// Helper utilities for C# exceptions throwing. /// struct FLAXENGINE_API Exception { static void Throw(MObject* exception); static MObject* GetNullReference(); static MObject* Get(const char* msg); static MObject* GetArgument(const char* arg, const char* msg); static MObject* GetArgumentNull(const char* arg); static MObject* GetArgumentOutOfRange(const char* arg); static MObject* GetNotSupported(const char* msg); }; /// /// Helper utilities for C# types information. /// struct FLAXENGINE_API Type { static ::String ToString(MType* type); static MClass* GetClass(MType* type); static MType* GetElementType(MType* type); static int32 GetSize(MType* type); static MTypes GetType(MType* type); static bool IsPointer(MType* type); static bool IsReference(MType* type); #if USE_MONO static MTypeObject* GetObject(MType* type); static MType* Get(MTypeObject* type); #endif }; /// /// Helper types cache from C# corlib and engine. /// struct FLAXENGINE_API TypeCache { static MClass* Void; static MClass* Object; static MClass* Byte; static MClass* Boolean; static MClass* SByte; static MClass* Char; static MClass* Int16; static MClass* UInt16; static MClass* Int32; static MClass* UInt32; static MClass* Int64; static MClass* UInt64; static MClass* IntPtr; static MClass* UIntPtr; static MClass* Single; static MClass* Double; static MClass* String; }; /// /// Utilities for ScriptingObject management. /// struct FLAXENGINE_API ScriptingObject { static void SetInternalValues(MClass* klass, MObject* object, void* unmanagedPtr, const Guid* id); static MObject* CreateScriptingObject(MClass* klass, void* unmanagedPtr, const Guid* id); }; };