Add support for binary modules with native-code only

This commit is contained in:
Wojtek Figat
2021-02-25 13:01:15 +01:00
parent 3da6f9186c
commit b193a7abc4
21 changed files with 123 additions and 27 deletions

View File

@@ -633,7 +633,7 @@ void ManagedBinaryModule::OnLoaded(MAssembly* assembly)
// Cache types for managed-only types that can be used in the engine
_firstManagedTypeIndex = Types.Count();
NativeBinaryModule* flaxEngine = GetBinaryModuleFlaxEngine();
NativeBinaryModule* flaxEngine = (NativeBinaryModule*)GetBinaryModuleFlaxEngine();
if (flaxEngine->Assembly->IsLoaded())
{
// TODO: check only assemblies that references FlaxEngine.CSharp.dll
@@ -1039,6 +1039,37 @@ void NativeBinaryModule::Destroy(bool isReloading)
}
}
NativeOnlyBinaryModule::NativeOnlyBinaryModule(const StringAnsiView& name)
: BinaryModule()
, _name(name)
, Library(nullptr)
{
}
const StringAnsi& NativeOnlyBinaryModule::GetName() const
{
return _name;
}
bool NativeOnlyBinaryModule::IsLoaded() const
{
return true;
}
void NativeOnlyBinaryModule::Destroy(bool isReloading)
{
BinaryModule::Destroy(isReloading);
// Release native library
const auto library = Library;
if (library)
{
Library = nullptr;
Platform::FreeLibrary(library);
// Don't do anything after FreeLibrary (this pointer might be invalid)
}
}
Array<GetBinaryModuleFunc>& StaticallyLinkedBinaryModuleInitializer::GetStaticallyLinkedBinaryModules()
{
static Array<GetBinaryModuleFunc> modules;

View File

@@ -355,7 +355,39 @@ public:
void Destroy(bool isReloading) override;
};
typedef NativeBinaryModule* (*GetBinaryModuleFunc)();
/// <summary>
/// The C++ scripting assembly container.
/// </summary>
class FLAXENGINE_API NativeOnlyBinaryModule : public BinaryModule
{
private:
StringAnsi _name;
public:
/// <summary>
/// Initializes a new instance of the <see cref="NativeOnlyBinaryModule" /> class.
/// </summary>
/// <param name="name">The module name.</param>
NativeOnlyBinaryModule(const StringAnsiView& name);
public:
/// <summary>
/// The native library (C++ DLL).
/// </summary>
void* Library;
public:
// [BinaryModule]
const StringAnsi& GetName() const override;
bool IsLoaded() const override;
void Destroy(bool isReloading) override;
};
typedef BinaryModule* (*GetBinaryModuleFunc)();
// Helper utility for registering native binary modules that are statically linked.
class FLAXENGINE_API StaticallyLinkedBinaryModuleInitializer

View File

@@ -66,7 +66,7 @@ void PluginManagerImpl::OnAssemblyLoaded(MAssembly* assembly)
PROFILE_CPU_NAMED("Load Assembly Plugins");
// Prepare FlaxEngine
auto engineAssembly = GetBinaryModuleFlaxEngine()->Assembly;
auto engineAssembly = ((NativeBinaryModule*)GetBinaryModuleFlaxEngine())->Assembly;
if (!engineAssembly->IsLoaded())
{
LOG(Warning, "Cannot find plugin class types for assembly {0} because FlaxEngine is not loaded.", assembly->ToString());

View File

@@ -124,7 +124,7 @@ bool ScriptingService::Init()
const auto startTime = DateTime::NowUTC();
// Link for assemblies events
auto engineAssembly = GetBinaryModuleFlaxEngine()->Assembly;
auto engineAssembly = ((NativeBinaryModule*)GetBinaryModuleFlaxEngine())->Assembly;
engineAssembly->Loaded.Bind(onEngineLoaded);
engineAssembly->Unloading.Bind(onEngineUnloading);
@@ -424,7 +424,7 @@ bool Scripting::Load()
// Load FlaxEngine
const String flaxEnginePath = Globals::BinariesFolder / TEXT("FlaxEngine.CSharp.dll");
if (GetBinaryModuleFlaxEngine()->Assembly->Load(flaxEnginePath))
if (((NativeBinaryModule*)GetBinaryModuleFlaxEngine())->Assembly->Load(flaxEnginePath))
{
LOG(Error, "Failed to load FlaxEngine C# assembly.");
return true;

View File

@@ -48,7 +48,7 @@ void StdTypesContainer::Clear()
bool StdTypesContainer::Gather()
{
#define GET_CLASS(assembly, type, typeName) \
type = CONCAT_MACROS(GetBinaryModule, assembly)()->Assembly->GetClass(typeName); \
type = ((ManagedBinaryModule*)CONCAT_MACROS(GetBinaryModule, assembly)())->Assembly->GetClass(typeName); \
if (type == nullptr) \
{ \
LOG(Error, "Missing managed type: \'{0}\'", String(typeName)); \