From dedb3d57fd2d996e31440a3c2245b84313f145d0 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sat, 1 Jun 2024 20:22:16 +0300 Subject: [PATCH 1/2] Fix Variant getters returning already freed managed handles --- Source/Engine/AI/BehaviorKnowledge.cpp | 2 +- Source/Engine/AI/BehaviorKnowledge.h | 2 +- Source/Engine/Engine/GameplayGlobals.cpp | 2 +- Source/Engine/Engine/GameplayGlobals.h | 2 +- Source/Engine/Level/Actors/AnimatedModel.cpp | 4 ++-- Source/Engine/Level/Actors/AnimatedModel.h | 4 ++-- Source/Engine/Particles/ParticleEffect.cpp | 6 +++--- Source/Engine/Particles/ParticleEffect.h | 6 +++--- 8 files changed, 14 insertions(+), 14 deletions(-) diff --git a/Source/Engine/AI/BehaviorKnowledge.cpp b/Source/Engine/AI/BehaviorKnowledge.cpp index a6593dd07..5bca8c627 100644 --- a/Source/Engine/AI/BehaviorKnowledge.cpp +++ b/Source/Engine/AI/BehaviorKnowledge.cpp @@ -212,7 +212,7 @@ bool BehaviorKnowledge::HasGoal(ScriptingTypeHandle type) const return false; } -Variant BehaviorKnowledge::GetGoal(ScriptingTypeHandle type) +const Variant& BehaviorKnowledge::GetGoal(ScriptingTypeHandle type) const { for (const Variant& goal : Goals) { diff --git a/Source/Engine/AI/BehaviorKnowledge.h b/Source/Engine/AI/BehaviorKnowledge.h index 8a92a9fa7..aef894c73 100644 --- a/Source/Engine/AI/BehaviorKnowledge.h +++ b/Source/Engine/AI/BehaviorKnowledge.h @@ -101,7 +101,7 @@ public: /// /// The goal type. /// The goal value or null if not found. - API_FUNCTION() Variant GetGoal(ScriptingTypeHandle type); + API_FUNCTION() const Variant& GetGoal(ScriptingTypeHandle type) const; /// /// Adds the goal to the knowledge. If goal of that type already exists then it's value is updated. diff --git a/Source/Engine/Engine/GameplayGlobals.cpp b/Source/Engine/Engine/GameplayGlobals.cpp index eba975e2b..eedd30d3a 100644 --- a/Source/Engine/Engine/GameplayGlobals.cpp +++ b/Source/Engine/Engine/GameplayGlobals.cpp @@ -124,7 +124,7 @@ void GameplayGlobals::SetDefaultValues(const Dictionary& values } } -Variant GameplayGlobals::GetValue(const StringView& name) const +const Variant& GameplayGlobals::GetValue(const StringView& name) const { ScopeLock lock(Locker); auto e = Variables.TryGet(name); diff --git a/Source/Engine/Engine/GameplayGlobals.h b/Source/Engine/Engine/GameplayGlobals.h index 7f9e50275..4f09ba4c8 100644 --- a/Source/Engine/Engine/GameplayGlobals.h +++ b/Source/Engine/Engine/GameplayGlobals.h @@ -66,7 +66,7 @@ public: /// /// The variable name. /// The value. - API_FUNCTION() Variant GetValue(const StringView& name) const; + API_FUNCTION() const Variant& GetValue(const StringView& name) const; /// /// Sets the value of the global variable (it must be added first). diff --git a/Source/Engine/Level/Actors/AnimatedModel.cpp b/Source/Engine/Level/Actors/AnimatedModel.cpp index 17b9acc11..2c0e8474d 100644 --- a/Source/Engine/Level/Actors/AnimatedModel.cpp +++ b/Source/Engine/Level/Actors/AnimatedModel.cpp @@ -273,7 +273,7 @@ AnimGraphParameter* AnimatedModel::GetParameter(const StringView& name) return nullptr; } -Variant AnimatedModel::GetParameterValue(const StringView& name) +const Variant& AnimatedModel::GetParameterValue(const StringView& name) const { CHECK_ANIM_GRAPH_PARAM_ACCESS_RESULT(Variant::Null); for (auto& param : GraphInstance.Parameters) @@ -299,7 +299,7 @@ void AnimatedModel::SetParameterValue(const StringView& name, const Variant& val LOG(Warning, "Failed to set animated model '{0}' missing parameter '{1}'", ToString(), name); } -Variant AnimatedModel::GetParameterValue(const Guid& id) +const Variant& AnimatedModel::GetParameterValue(const Guid& id) const { CHECK_ANIM_GRAPH_PARAM_ACCESS_RESULT(Variant::Null); for (auto& param : GraphInstance.Parameters) diff --git a/Source/Engine/Level/Actors/AnimatedModel.h b/Source/Engine/Level/Actors/AnimatedModel.h index bb475722a..bb4b3a8f0 100644 --- a/Source/Engine/Level/Actors/AnimatedModel.h +++ b/Source/Engine/Level/Actors/AnimatedModel.h @@ -308,7 +308,7 @@ public: /// /// The parameter name. /// The value. - API_FUNCTION() Variant GetParameterValue(const StringView& name); + API_FUNCTION() const Variant& GetParameterValue(const StringView& name) const; /// /// Sets the anim graph instance parameter value. @@ -322,7 +322,7 @@ public: /// /// The parameter id. /// The value. - API_FUNCTION() Variant GetParameterValue(const Guid& id); + API_FUNCTION() const Variant& GetParameterValue(const Guid& id) const; /// /// Sets the anim graph instance parameter value. diff --git a/Source/Engine/Particles/ParticleEffect.cpp b/Source/Engine/Particles/ParticleEffect.cpp index ab3066372..9fbab80b9 100644 --- a/Source/Engine/Particles/ParticleEffect.cpp +++ b/Source/Engine/Particles/ParticleEffect.cpp @@ -91,14 +91,14 @@ Variant ParticleEffectParameter::GetDefaultValue() const return paramValue; } -Variant ParticleEffectParameter::GetDefaultEmitterValue() const +const Variant& ParticleEffectParameter::GetDefaultEmitterValue() const { CHECK_RETURN(IsValid(), Variant::False); const ParticleSystemParameter& param = _effect->ParticleSystem->Emitters[_emitterIndex]->Graph.Parameters[_paramIndex]; return param.Value; } -Variant ParticleEffectParameter::GetValue() const +const Variant& ParticleEffectParameter::GetValue() const { CHECK_RETURN(IsValid(), Variant::False); const Variant& paramValue = _effect->Instance.Emitters[_emitterIndex].Parameters[_paramIndex]; @@ -205,7 +205,7 @@ ParticleEffectParameter* ParticleEffect::GetParameter(const StringView& emitterT return nullptr; } -Variant ParticleEffect::GetParameterValue(const StringView& emitterTrackName, const StringView& paramName) +const Variant& ParticleEffect::GetParameterValue(const StringView& emitterTrackName, const StringView& paramName) { const auto param = GetParameter(emitterTrackName, paramName); CHECK_RETURN(param, Variant::Null); diff --git a/Source/Engine/Particles/ParticleEffect.h b/Source/Engine/Particles/ParticleEffect.h index 5e2770f89..a338bfee2 100644 --- a/Source/Engine/Particles/ParticleEffect.h +++ b/Source/Engine/Particles/ParticleEffect.h @@ -109,13 +109,13 @@ public: /// Gets the default value of the parameter (set in particle emitter asset). /// /// The default value. - API_PROPERTY() Variant GetDefaultEmitterValue() const; + API_PROPERTY() const Variant& GetDefaultEmitterValue() const; /// /// Gets the value of the parameter. /// /// The value. - API_PROPERTY() Variant GetValue() const; + API_PROPERTY() const Variant& GetValue() const; /// /// Sets the value of the parameter. @@ -293,7 +293,7 @@ public: /// The emitter track name (in particle system asset). /// The emitter parameter name (in particle emitter asset). /// The value. - API_FUNCTION() Variant GetParameterValue(const StringView& emitterTrackName, const StringView& paramName); + API_FUNCTION() const Variant& GetParameterValue(const StringView& emitterTrackName, const StringView& paramName); /// /// Set the particle parameter value. From 272977a521ede5c3ea7cd92d1d52115321e54726 Mon Sep 17 00:00:00 2001 From: Ari Vuollet Date: Sat, 1 Jun 2024 20:24:57 +0300 Subject: [PATCH 2/2] Defer Editor EndInit after loading scripting assemblies Assets containing deserialized data of scripting assembly structures needs to be loaded after the scripting assemblies have been loaded. --- Source/Editor/Managed/ManagedEditor.cpp | 13 +++++++++++++ Source/Editor/States/LoadingState.cs | 1 - 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Source/Editor/Managed/ManagedEditor.cpp b/Source/Editor/Managed/ManagedEditor.cpp index c4c71bdfd..d0b060ffe 100644 --- a/Source/Editor/Managed/ManagedEditor.cpp +++ b/Source/Editor/Managed/ManagedEditor.cpp @@ -223,6 +223,19 @@ void ManagedEditor::Init() { LOG(Info, "Loading managed assemblies (due to disabled compilation on startup)"); Scripting::Load(); + + const auto endInitMethod = mclass->GetMethod("EndInit"); + if (endInitMethod == nullptr) + { + LOG(Fatal, "Invalid Editor assembly! Missing EndInit method."); + } + endInitMethod->Invoke(instance, nullptr, &exception); + if (exception) + { + MException ex(exception); + ex.Log(LogType::Warning, TEXT("ManagedEditor::EndInit")); + LOG_STR(Fatal, TEXT("Failed to initialize editor during EndInit! ") + ex.Message); + } } // Call building if need to (based on CL) diff --git a/Source/Editor/States/LoadingState.cs b/Source/Editor/States/LoadingState.cs index 44a4c7f28..93495f750 100644 --- a/Source/Editor/States/LoadingState.cs +++ b/Source/Editor/States/LoadingState.cs @@ -71,7 +71,6 @@ namespace FlaxEditor.States { // Skip compilation on startup OnCompilationEnd(true); - Editor.EndInit(); } }