// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
#pragma once
#include "Engine/Core/Collections/Dictionary.h"
#include "MTypes.h"
#include "MAssemblyOptions.h"
///
/// Domain separates multiple processes within one executed CLR environment.
///
///
/// At once you can execute methods, get instances etc, only from on Domain at the time.
/// If you want to execute any code that given domain contains, you need to switch context, and dispatch current thread to CLR environment.
///
class FLAXENGINE_API MDomain
{
friend MCore;
friend MAssembly;
public:
typedef Dictionary AssembliesDictionary;
private:
#if USE_MONO
MonoDomain* _monoDomain;
#endif
MString _domainName;
AssembliesDictionary _assemblies;
MCore* _coreInstance;
public:
#if USE_MONO
MDomain(const MString& domainName, MonoDomain* monoDomain);
#endif
public:
#if USE_MONO
///
/// Gets native domain class
///
/// The native domain.
MonoDomain* GetNative() const;
#endif
///
/// Gets current domain name
///
/// The name.
FORCE_INLINE const MString& GetName() const
{
return _domainName;
}
///
/// Create assembly container from current domain
///
/// Assembly name to later receive from assemblies dictionary
/// The assembly options container.
/// MAssembly object ready to Load
MAssembly* CreateEmptyAssembly(const MString& assemblyName, const MAssemblyOptions options);
///
/// Removes assembly from current domain and request unloading.
///
/// Assembly name
void RemoveAssembly(const MString& assemblyName);
///
/// Gets the current domain assemblies.
///
/// The assemblies.
FORCE_INLINE const AssembliesDictionary& GetAssemblies() const
{
return _assemblies;
}
///
/// Gets current domain assembly.
///
/// The managed assembly or null if not found.
MAssembly* GetAssembly(const MString& assemblyName) const;
///
/// Attaches current CLR domain calls to the current thread.
///
void Dispatch() const;
///
/// Sets currently using domain.
///
/// True if succeed in settings, false if failed.
bool SetCurrentDomain(bool force = false);
///
/// Returns class from current domain.
///
MClass* FindClass(const StringAnsiView& fullname) const;
};