Merge remote-tracking branch 'origin/master' into 1.10

# Conflicts:
#	Source/Editor/Windows/Profiler/Memory.cs
This commit is contained in:
Wojtek Figat
2025-03-25 13:37:15 +01:00
94 changed files with 901 additions and 352 deletions

View File

@@ -796,12 +796,8 @@ ScriptingObject* ManagedBinaryModule::ManagedObjectSpawn(const ScriptingObjectSp
// Mark as managed type
object->Flags |= ObjectFlags::IsManagedType;
// Initialize managed instance
if (params.Managed)
{
object->SetManagedInstance((MObject*)params.Managed);
}
else
// Initialize managed instance (ScriptingObject ctor copies managed object handle)
if (!params.Managed)
{
// Invoke managed ctor (to match C++ logic)
object->CreateManaged();

View File

@@ -45,9 +45,16 @@ public:
/// </summary>
static void UnloadEngine();
/// <summary>
/// Creates the assembly load context for assemblies used by Scripting.
/// </summary>
static void CreateScriptingAssemblyLoadContext();
#if USE_EDITOR
// Called by Scripting in a middle of hot-reload (after unloading modules but before loading them again).
static void ReloadScriptingAssemblyLoadContext();
/// <summary>
/// Called by Scripting in a middle of hot-reload (after unloading modules but before loading them again).
/// </summary>
static void UnloadScriptingAssemblyLoadContext();
#endif
public:

View File

@@ -330,9 +330,15 @@ void MCore::UnloadEngine()
ShutdownHostfxr();
}
void MCore::CreateScriptingAssemblyLoadContext()
{
static void* CreateScriptingAssemblyLoadContextPtr = GetStaticMethodPointer(TEXT("CreateScriptingAssemblyLoadContext"));
CallStaticMethod<void>(CreateScriptingAssemblyLoadContextPtr);
}
#if USE_EDITOR
void MCore::ReloadScriptingAssemblyLoadContext()
void MCore::UnloadScriptingAssemblyLoadContext()
{
// Clear any cached class attributes (see https://github.com/FlaxEngine/FlaxEngine/issues/1108)
for (const auto& e : CachedClassHandles)
@@ -377,8 +383,8 @@ void MCore::ReloadScriptingAssemblyLoadContext()
}
}
static void* ReloadScriptingAssemblyLoadContextPtr = GetStaticMethodPointer(TEXT("ReloadScriptingAssemblyLoadContext"));
CallStaticMethod<void>(ReloadScriptingAssemblyLoadContextPtr);
static void* UnloadScriptingAssemblyLoadContextPtr = GetStaticMethodPointer(TEXT("UnloadScriptingAssemblyLoadContext"));
CallStaticMethod<void>(UnloadScriptingAssemblyLoadContextPtr);
}
#endif

View File

@@ -715,9 +715,13 @@ void MCore::UnloadEngine()
#endif
}
void MCore::CreateScriptingAssemblyLoadContext()
{
}
#if USE_EDITOR
void MCore::ReloadScriptingAssemblyLoadContext()
void MCore::UnloadScriptingAssemblyLoadContext()
{
}

View File

@@ -58,9 +58,13 @@ void MCore::UnloadEngine()
MRootDomain = nullptr;
}
void MCore::CreateScriptingAssemblyLoadContext()
{
}
#if USE_EDITOR
void MCore::ReloadScriptingAssemblyLoadContext()
void MCore::UnloadScriptingAssemblyLoadContext()
{
}

View File

@@ -182,6 +182,8 @@ bool ScriptingService::Init()
return true;
}
MCore::CreateScriptingAssemblyLoadContext();
// Cache root domain
_rootDomain = MCore::GetRootDomain();
@@ -710,7 +712,8 @@ void Scripting::Reload(bool canTriggerSceneReload)
_hasGameModulesLoaded = false;
// Release and create a new assembly load context for user assemblies
MCore::ReloadScriptingAssemblyLoadContext();
MCore::UnloadScriptingAssemblyLoadContext();
MCore::CreateScriptingAssemblyLoadContext();
// Give GC a try to cleanup old user objects and the other mess
MCore::GC::Collect();

View File

@@ -170,6 +170,10 @@ namespace FlaxEngine
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
TaskScheduler.UnobservedTaskException += OnUnobservedTaskException;
Localization.LocalizationChanged += OnLocalizationChanged;
#if FLAX_EDITOR
FlaxEditor.ScriptsBuilder.ScriptsReloadBegin += OnScriptsReloadBegin;
FlaxEditor.ScriptsBuilder.ScriptsReloadEnd += OnScriptsReloadEnd;
#endif
OnLocalizationChanged();
if (!Engine.IsEditor)
@@ -178,6 +182,19 @@ namespace FlaxEngine
}
}
#if FLAX_EDITOR
private static void OnScriptsReloadBegin()
{
// Tooltip might hold references to scripting assemblies
Style.Current.SharedTooltip = null;
}
private static void OnScriptsReloadEnd()
{
Style.Current.SharedTooltip = new Tooltip();
}
#endif
private static void OnLocalizationChanged()
{
// Invariant-globalization only (see InitHostfxr with Mono)
@@ -368,6 +385,10 @@ namespace FlaxEngine
MainThreadTaskScheduler.Dispose();
Json.JsonSerializer.Dispose();
#if FLAX_EDITOR
FlaxEditor.ScriptsBuilder.ScriptsReloadBegin -= OnScriptsReloadBegin;
FlaxEditor.ScriptsBuilder.ScriptsReloadEnd -= OnScriptsReloadEnd;
#endif
}
/// <summary>

View File

@@ -89,7 +89,11 @@ void SerializableScriptingObject::Deserialize(DeserializeStream& stream, ISerial
}
ScriptingObject::ScriptingObject(const SpawnParams& params)
: _gcHandle(0)
#if USE_NETCORE
: _gcHandle((MGCHandle)params.Managed)
#elif !COMPILE_WITHOUT_CSHARP
: _gcHandle(params.Managed ? MCore::GCHandle::New(params.Managed) : 0)
#endif
, _type(params.Type)
, _id(params.ID)
{