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
6 changed files with 41 additions and 40 deletions
Showing only changes of commit 39fbfbd6fa - Show all commits

View File

@ -0,0 +1,37 @@
using SHADE;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SHADE_Scripting
{
public class StaticTest
{
public static int x;
static StaticTest()
{
x = 5;
Debug.Log("Static Constructor!");
}
}
public class ImplicitStaticTest : Script
{
public static int x = 5;
static ImplicitStaticTest()
{
Debug.Log("Static Constructor!");
}
protected override void awake()
{
Debug.LogWarning($"Before Add: x = {x}");
++x;
Debug.LogWarning($"After Add: x = {x}");
}
}
}

View File

@ -0,0 +1,3 @@
Name: StaticTest
ID: 159057282
Type: 9

View File

@ -383,17 +383,11 @@ 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());
csScriptRemoveAllForAllNow(true);
csEngineReloadScripts();
return eventData->handle;
}
@ -563,12 +557,6 @@ namespace SHADE
DEFAULT_CSHARP_NAMESPACE + ".Editor",
"Redo"
);
csResetStaticData = dotNet.GetFunctionPtr<CsFuncPtr>
(
DEFAULT_CSHARP_LIB_NAME,
DEFAULT_CSHARP_NAMESPACE + ".EngineInterface",
"ResetStaticDataInLoadedAssembly"
);
}
void SHScriptEngine::registerEvents()
@ -583,11 +571,6 @@ namespace SHADE
/* Editor */
// 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

@ -296,8 +296,6 @@ namespace SHADE
CsScriptEditorFuncPtr csEditorRenderScripts = nullptr;
CsFuncPtr csEditorUndo = nullptr;
CsFuncPtr csEditorRedo = nullptr;
// - Other
CsFuncPtr csResetStaticData = nullptr;
/*-----------------------------------------------------------------------------*/
/* Event Handler Functions */
@ -310,7 +308,6 @@ 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,21 +78,6 @@ 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,10 +59,6 @@ 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();