Static variables in scripts are now reset when leaving play mode #351

Merged
Pycorax merged 7 commits from SP3-6-CSharpStaticReset into main 2023-02-20 11:12:09 +08:00
4 changed files with 45 additions and 1 deletions
Showing only changes of commit 72953762cb - Show all commits

View File

@ -383,6 +383,13 @@ namespace SHADE
return eventData->handle;
}
SHEventHandle SHScriptEngine::onScenePlayStart(SHEventPtr eventPtr)
{
auto eventData = reinterpret_cast<const SHEventSpec<SHSceneGraphRemoveChildEvent>*>(eventPtr.get());
csResetStaticData();
return eventData->handle;
}
SHEventHandle SHScriptEngine::onSceneDestroyed(SHEventPtr eventPtr)
{
auto eventData = reinterpret_cast<const SHEventSpec<SHEditorStateChangeEvent>*>(eventPtr.get());
@ -556,6 +563,12 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".Editor",
"Redo"
);
csResetStaticData = dotNet.GetFunctionPtr<CsFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".EngineInterface",
"ResetStaticDataInLoadedAssembly"
);
}
void SHScriptEngine::registerEvents()
@ -569,7 +582,12 @@ namespace SHADE
SHEventManager::SubscribeTo(SH_ENTITY_DESTROYED_EVENT, std::dynamic_pointer_cast<SHEventReceiver>(destroyedEventReceiver));
/* Editor */
// Register for editor state change event
// Register for editor state change events
std::shared_ptr<SHEventReceiverSpec<SHScriptEngine>> startSceneEventReceiver
{
std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onScenePlayStart)
};
SHEventManager::SubscribeTo(SH_EDITOR_ON_PLAY_EVENT, std::dynamic_pointer_cast<SHEventReceiver>(startSceneEventReceiver));
std::shared_ptr<SHEventReceiverSpec<SHScriptEngine>> destroyedSceneEventReceiver
{
std::make_shared<SHEventReceiverSpec<SHScriptEngine>>(this, &SHScriptEngine::onSceneDestroyed)

View File

@ -233,6 +233,10 @@ namespace SHADE
/// </summary>
/// <param name="path"></param>
void OpenFile(const std::filesystem::path& path);
/// <summary>
/// Resets all static data in the loaded assemblies to their default values.
/// </summary>
static void ResetStaticDataInLoadedAssembly();
private:
/*-----------------------------------------------------------------------------*/
@ -292,6 +296,8 @@ namespace SHADE
CsScriptEditorFuncPtr csEditorRenderScripts = nullptr;
CsFuncPtr csEditorUndo = nullptr;
CsFuncPtr csEditorRedo = nullptr;
// - Other
CsFuncPtr csResetStaticData = nullptr;
/*-----------------------------------------------------------------------------*/
/* Event Handler Functions */
@ -304,6 +310,7 @@ namespace SHADE
SHEventHandle onUIElementClicked(SHEventPtr eventPtr);
SHEventHandle onSceneNodeChildrenAdded(SHEventPtr eventPtr);
SHEventHandle onSceneNodeChildrenRemoved(SHEventPtr eventPtr);
SHEventHandle onScenePlayStart(SHEventPtr eventPtr);
SHEventHandle onSceneDestroyed(SHEventPtr eventPtr);
/*-----------------------------------------------------------------------------*/

View File

@ -78,6 +78,21 @@ namespace SHADE
LoadScriptAssembly();
SAFE_NATIVE_CALL_END_N("SHADE_Managed.EngineInterface")
}
void EngineInterface::ResetStaticDataInLoadedAssembly()
{
SAFE_NATIVE_CALL_BEGIN
System::Reflection::Assembly^ assembly = System::Linq::Enumerable::FirstOrDefault(scriptContext->Assemblies);
for each (System::Type ^ type in assembly->ExportedTypes)
{
if (type->TypeInitializer)
{
type->TypeInitializer->Invoke(nullptr, nullptr);
Debug::LogWarning("Called Static Constructor for: " + type->Name);
}
}
SAFE_NATIVE_CALL_END_N("SHADE_Managed.EngineInterface")
}
void EngineInterface::Exit()
{
SAFE_NATIVE_CALL_BEGIN

View File

@ -59,6 +59,10 @@ namespace SHADE
/// </summary>
static void ReloadScriptAssembly();
/// <summary>
/// Resets all static data in the loaded assemblies to their default values.
/// </summary>
static void ResetStaticDataInLoadedAssembly();
/// <summary>
/// Cleans up all required components for managed code.
/// </summary>
static void Exit();