I'm building a game engine from scratch and trying to implement a basic plugin system within my engine.
Using AssemblyLoadContext
to load the target dll dynamicly and unloading it with AssemblyLoadContext.Unload()
However it seems even if there is a one class instance created from this assembly hanging somewhere in the whole app domain AssemblyLoadContext.Unload()
does not unload it until the all the instances are dereferenced.
This is where my problem begins.
There are infinite number of scenarios whether a class which belongs to the plugin can be instantiated and used across the whole appdomain right?.
So how do i keep track of the plugin instances across the whole AppDomain
.
(How unity unloads the user scripts and cleans every last one of the instances i wonder)
My engine system is nearly identical to both unreal and unity.
I have entities and the components just like unity.
My current unload workflow is like this.
- Loop through every component via reflection and check if one the assembly classes instantiated then set that field/property null
- Loop through every assembly and check for static fields/properties which might contain some instance
- Now unload it.
However the approach above seems well idiotic and sluggish not to mention it should be slow as hell.
It seems i have a desing problem within my plugin system and i fear if i overcomplicated things.
Are there better way to cleanup assembly instances or am i missing something? (Please tell me im missing something obvious)