From 7c58c9a23dbd74af34fe975cea0f4796af7f6bdc Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 24 Nov 2022 22:56:55 +0800 Subject: [PATCH 1/4] Fixed crash caused when building scripts in debug mode when a debugger is attached --- SHADE_Engine/src/Scripting/SHScriptEngine.cpp | 23 +++++++++++++++++-- SHADE_Engine/src/Scripting/SHScriptEngine.h | 1 + 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp index 90121994..3746d1d0 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.cpp +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.cpp @@ -197,12 +197,18 @@ namespace SHADE if (BUILD_SUCCESS) { // Copy to built dll to the working directory and replace - std::filesystem::copy_file("./tmp/SHADE_Scripting.dll", "SHADE_Scripting.dll", std::filesystem::copy_options::overwrite_existing); + if (!copyFile("./tmp/SHADE_Scripting.dll", "SHADE_Scripting.dll", std::filesystem::copy_options::overwrite_existing)) + { + SHLOG_ERROR("[ScriptEngine] Failed to replace scripts assembly. Scripts will remain outdated."); + } // If debug, we want to copy the PDB so that we can do script debugging if (debug) { - std::filesystem::copy_file("./tmp/SHADE_Scripting.pdb", "SHADE_Scripting.pdb", std::filesystem::copy_options::overwrite_existing); + if (!copyFile("./tmp/SHADE_Scripting.pdb", "SHADE_Scripting.pdb", std::filesystem::copy_options::overwrite_existing)) + { + SHLOG_WARNING("[ScriptEngine] Breakpoint debugging will not work as PDB cannot be updated. If you are currently debugging, stop the debugger first."); + } } oss << "[ScriptEngine] Successfully built Managed Script Assembly (" << MANAGED_SCRIPT_LIB_NAME << ")!"; @@ -591,6 +597,19 @@ namespace SHADE return false; } + bool SHScriptEngine::copyFile(const std::filesystem::path& from, const std::filesystem::path& to, const std::filesystem::copy_options options) noexcept + { + try + { + return std::filesystem::copy_file(from, to, options); + } + catch (std::exception& e) + { + SHLOG_ERROR("[ScriptEngine] Failed to copy file {} ({})", to.string(), std::string(e.what())); + return false; + } + } + DWORD SHScriptEngine::execProcess(const std::wstring& path, const std::wstring& args) { STARTUPINFOW startInfo; diff --git a/SHADE_Engine/src/Scripting/SHScriptEngine.h b/SHADE_Engine/src/Scripting/SHScriptEngine.h index ef778627..1a38a678 100644 --- a/SHADE_Engine/src/Scripting/SHScriptEngine.h +++ b/SHADE_Engine/src/Scripting/SHScriptEngine.h @@ -319,6 +319,7 @@ namespace SHADE /// File path to the file to check. /// True if the file exists static bool fileExists(const std::filesystem::path& filePath); + static bool copyFile(const std::filesystem::path& from, const std::filesystem::path& to, const std::filesystem::copy_options options) noexcept; static DWORD execProcess(const std::wstring& path, const std::wstring& args); static std::wstring generateBuildCommand(bool debug); }; From 72c8a504c545b7ea53bc790a28a47ffc70738264 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 24 Nov 2022 23:22:02 +0800 Subject: [PATCH 2/4] Fixed crash from adding an element to a list of strings in the script inspector --- SHADE_Managed/src/Editor/Editor.cxx | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/SHADE_Managed/src/Editor/Editor.cxx b/SHADE_Managed/src/Editor/Editor.cxx index 80c73d4f..8b53db1b 100644 --- a/SHADE_Managed/src/Editor/Editor.cxx +++ b/SHADE_Managed/src/Editor/Editor.cxx @@ -200,7 +200,16 @@ namespace SHADE { if (SHEditorUI::Button("Add Item")) { - System::Object^ obj = System::Activator::CreateInstance(listType); + System::Object^ obj; + if (listType == System::String::typeid) + { + // Special case for string + obj = gcnew System::String(""); + } + else + { + obj = System::Activator::CreateInstance(listType); + } iList->Add(obj); registerUndoListAddAction(listType, iList, iList->Count - 1, obj); } From 9ada99815110207e51106d881377a6e35438ebcf Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Thu, 24 Nov 2022 23:27:13 +0800 Subject: [PATCH 3/4] Fixed bug where lists failed to be deserialized correctly --- .../src/Serialisation/SerialisationUtilities.cxx | 10 +++++++++- .../src/Serialisation/SerialisationUtilities.h++ | 8 ++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx b/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx index 2bf05bc5..83da64b8 100644 --- a/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx +++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.cxx @@ -279,7 +279,15 @@ namespace SHADE for (int i = 0; i < LIST_SIZE; ++i) { // Create the object - System::Object^ obj = System::Activator::CreateInstance(elemType); + System::Object^ obj; + if (elemType == System::String::typeid) + { + obj = gcnew System::String(""); + } + else + { + obj = System::Activator::CreateInstance(elemType); + } // Set it's value if (varAssignYaml(obj, node[i])) diff --git a/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ index 04c87ef4..d2043f6b 100644 --- a/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ +++ b/SHADE_Managed/src/Serialisation/SerialisationUtilities.h++ @@ -167,6 +167,10 @@ namespace SHADE { valueObj = 0; } + else + { + return false; + } } else { @@ -181,6 +185,10 @@ namespace SHADE valueObj = FieldType(); } } + else + { + return false; + } } } From 50232cd15faf0ec934b71a200ead3e10bdfaf1e9 Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Fri, 25 Nov 2022 00:07:19 +0800 Subject: [PATCH 4/4] Fixed bug where scripts loaded after a scene change would not have serialised data --- SHADE_Managed/src/Scripts/ScriptStore.cxx | 15 ++++++++++++++- SHADE_Managed/src/Scripts/ScriptStore.hxx | 1 + 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/SHADE_Managed/src/Scripts/ScriptStore.cxx b/SHADE_Managed/src/Scripts/ScriptStore.cxx index 3ffb72b2..a5a0ebc7 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.cxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.cxx @@ -74,7 +74,7 @@ namespace SHADE // Add the script in script->Initialize(GameObject(entity)); entityScriptList->Insert(System::Math::Clamp(index, 0, entityScriptList->Count), script); - if (Application::IsPlaying && !SHSceneManager::HasSceneChanged()) + if (Application::IsPlaying && !isDeserialising) { // Only call immediately if we are in game and is not loading another scene script->Awake(); @@ -423,6 +423,8 @@ namespace SHADE /*---------------------------------------------------------------------------------*/ void ScriptStore::Init() { + isDeserialising = false; + // Create an enumerable list of script types refreshScriptTypeList(); // Get stored methods for interop variants of functions @@ -724,6 +726,10 @@ namespace SHADE bool ScriptStore::DeserialiseScripts(Entity entity, System::IntPtr yamlNodePtr) { SAFE_NATIVE_CALL_BEGIN + + // Flag that deserialization processs is ongoing + isDeserialising = true; + // Convert to pointer YAML::Node* yamlNode = reinterpret_cast(yamlNodePtr.ToPointer()); @@ -765,9 +771,16 @@ namespace SHADE Debug::LogWarning("[ScriptStore] Script with unloaded type detected, skipping."); } } + + // Unset flag for deserialization process + isDeserialising = false; + return true; SAFE_NATIVE_CALL_END_N("SHADE_Managed.ScriptStore") + + // Unset flag for deserialization process + isDeserialising = false; return false; } diff --git a/SHADE_Managed/src/Scripts/ScriptStore.hxx b/SHADE_Managed/src/Scripts/ScriptStore.hxx index 78f8c787..f31836d8 100644 --- a/SHADE_Managed/src/Scripts/ScriptStore.hxx +++ b/SHADE_Managed/src/Scripts/ScriptStore.hxx @@ -337,6 +337,7 @@ namespace SHADE static ScriptSet disposalQueue; static System::Collections::Generic::IEnumerable^ scriptTypeList; static System::Reflection::MethodInfo^ addScriptMethod; + static bool isDeserialising; /*-----------------------------------------------------------------------------*/ /* Helper Functions */