Fix crash when using and saving scene with Visual Script object instance which asset was deleted

This commit is contained in:
Wojtek Figat
2024-07-25 17:17:28 +02:00
parent b4d1e6197c
commit 9f078a6e3c
3 changed files with 17 additions and 7 deletions

View File

@@ -1522,12 +1522,16 @@ void VisualScript::unload(bool isReloading)
if (_scriptingTypeHandle)
{
VisualScriptingBinaryModule::Locker.Lock();
auto& type = VisualScriptingModule.Types[_scriptingTypeHandle.TypeIndex];
ScriptingType& type = VisualScriptingModule.Types[_scriptingTypeHandle.TypeIndex];
if (type.Script.DefaultInstance)
{
Delete(type.Script.DefaultInstance);
type.Script.DefaultInstance = nullptr;
}
char* typeName = (char*)Allocator::Allocate(sizeof(_typenameChars));
Platform::MemoryCopy(typeName, _typenameChars, sizeof(_typenameChars));
((StringAnsiView&)type.Fullname) = StringAnsiView(typeName, 32);
VisualScriptingModule._unloadedScriptTypeNames.Add(typeName);
VisualScriptingModule.TypeNameToTypeIndex.RemoveValue(_scriptingTypeHandle.TypeIndex);
VisualScriptingModule.Scripts[_scriptingTypeHandle.TypeIndex] = nullptr;
_scriptingTypeHandleCached = _scriptingTypeHandle;
@@ -1653,6 +1657,8 @@ VisualScriptingBinaryModule::VisualScriptingBinaryModule()
ScriptingObject* VisualScriptingBinaryModule::VisualScriptObjectSpawn(const ScriptingObjectSpawnParams& params)
{
// Create native object (base type can be C++ or C#)
if (params.Type.Module == nullptr)
return nullptr;
ScriptingType& visualScriptType = (ScriptingType&)params.Type.GetType();
ScriptingTypeHandle baseTypeHandle = visualScriptType.GetBaseType();
const ScriptingType* baseTypePtr = &baseTypeHandle.GetType();
@@ -1663,9 +1669,7 @@ ScriptingObject* VisualScriptingBinaryModule::VisualScriptObjectSpawn(const Scri
}
ScriptingObject* object = baseTypePtr->Script.Spawn(params);
if (!object)
{
return nullptr;
}
// Beware! Hacking vtables incoming! Undefined behaviors exploits! Low-level programming!
visualScriptType.HackObjectVTable(object, baseTypeHandle, 1);
@@ -2060,6 +2064,11 @@ void VisualScriptingBinaryModule::Destroy(bool isReloading)
return;
BinaryModule::Destroy(isReloading);
// Free cached script typenames table
for (char* str : _unloadedScriptTypeNames)
Allocator::Free(str);
_unloadedScriptTypeNames.Clear();
}
ScriptingTypeHandle VisualScript::GetScriptingType()

View File

@@ -305,6 +305,7 @@ class FLAXENGINE_API VisualScriptingBinaryModule : public BinaryModule
friend VisualScript;
private:
StringAnsi _name;
Array<char*> _unloadedScriptTypeNames;
public:
/// <summary>

View File

@@ -118,7 +118,7 @@ public:
/// <summary>
/// Tries to find a method in a given scripting type by the method name and parameters count.
/// </summary>
/// <remarks>If the the type contains more than one method of the given name and parameters count the returned value can be non-deterministic (one of the matching methods).</remarks>
/// <remarks>If the type contains more than one method of the given name and parameters count the returned value can be non-deterministic (one of the matching methods).</remarks>
/// <param name="typeHandle">The type to find method inside it.</param>
/// <param name="name">The method name.</param>
/// <param name="numParams">The method parameters count.</param>
@@ -182,7 +182,7 @@ public:
/// Gets the value of a given scripting field.
/// </summary>
/// <param name="field">The field.</param>
/// <param name="instance">The object instance to get it's member field. Unused for static fields.</param>
/// <param name="instance">The object instance to get its member field. Unused for static fields.</param>
/// <param name="result">The output field value.</param>
/// <returns>True if failed, otherwise false.</returns>
virtual bool GetFieldValue(void* field, const Variant& instance, Variant& result)
@@ -194,7 +194,7 @@ public:
/// Sets the value of a given scripting field.
/// </summary>
/// <param name="field">The field.</param>
/// <param name="instance">The object instance to set it's member field. Unused for static fields.</param>
/// <param name="instance">The object instance to set its member field. Unused for static fields.</param>
/// <param name="value">The field value to assign.</param>
/// <returns>True if failed, otherwise false.</returns>
virtual bool SetFieldValue(void* field, const Variant& instance, Variant& value)
@@ -242,7 +242,7 @@ public:
/// <summary>
/// Unloads the module (native library and C# assembly and any other scripting data). Unregisters the module.
/// </summary>
/// <param name="isReloading">If true module is during reloading and should force release the runtime data. Used for C# assembly to cleanup it's runtime data in Mono (or other scripting runtime).</param>
/// <param name="isReloading">If true module is during reloading and should force release the runtime data. Used for C# assembly to clean up it's runtime data in Mono (or other scripting runtime).</param>
virtual void Destroy(bool isReloading);
};