Skip unnecessary (de-)serializations when reloading script assembly

This commit is contained in:
Simon Lübeß
2026-09-13 21:11:35 +02:00
parent e3619ae93c
commit 8244033ac5
5 changed files with 91 additions and 54 deletions
+18 -11
View File
@@ -192,11 +192,11 @@ static class ScriptEngine
}
/// Changes the path to the current app assembly.
public static void SetAppAssemblyPath(StringView appAssemblyPath)
public static void SetAppAssemblyPath(StringView appAssemblyPath, bool retainScriptData)
{
_appAssemblyPath.Set(appAssemblyPath);
ReloadAssemblies();
ReloadAssemblies(retainScriptData);
}
static void InitRuntime()
@@ -239,7 +239,7 @@ static class ScriptEngine
Application.Instance.InvokeOnMainThread(new () =>
{
ReloadAssemblies();
ReloadAssemblies(retainScriptData: true);
_requestingReload = false;
_userAssemblyWatcher.StartRaisingEvents();
@@ -290,12 +290,12 @@ static class ScriptEngine
}
/// Starts the script runtime and sets the context scene.
public static void StartRuntime(Scene context)
public static void StartRuntime(Scene context, bool retainScriptData = true)
{
Debug.Assert(s_Context == null, "StartRuntime was called twice without StopRuntime in between!");
Context = context;
ReloadAssemblies();
ReloadAssemblies(retainScriptData);
}
/// Stopts the script runtime and disposes of all script instances.
@@ -415,7 +415,7 @@ static class ScriptEngine
CoreClrHelper.FreeScriptClassNames();
}
public static void ReloadAssemblies()
public static void ReloadAssemblies(bool retainScriptData = true)
{
Debug.Profiler.ProfileFunction!();
@@ -423,10 +423,14 @@ static class ScriptEngine
Log.EngineLogger.Info("Reloading script assemblies.");
ScriptInstanceSerializer contextSerializer = scope .();
contextSerializer.SerializeScriptInstances();
ScriptInstanceSerializer contextSerializer = null;
if (retainScriptData)
{
contextSerializer = scope:: .();
contextSerializer.SerializeScriptInstances();
}
UnloadScriptAssemblies();
LoadScriptAssemblies();
@@ -448,8 +452,11 @@ static class ScriptEngine
}
}
}
contextSerializer.DeserializeScriptInstances();
if (retainScriptData)
{
contextSerializer.DeserializeScriptInstances();
}
Log.EngineLogger.Info("Script assemblies reloaded!");
}
+15 -7
View File
@@ -86,6 +86,7 @@ struct GlueParamAttribute : Attribute
}
}
/// Collects all methods in ScriptGlue that have a RegisterCallAttribute. Generates a function pointer for them as well as outputting a json file that can be used to generate the script library side of the ScriptGlue construct.
[AttributeUsage(.Struct)]
struct EngineFunctionsGeneratorAttribute : Attribute, IComptimeTypeApply
{
@@ -195,11 +196,13 @@ struct EngineFunctionsGeneratorAttribute : Attribute, IComptimeTypeApply
}
}
/// A struct containing all pointers to functions that can be called from the script library.
[EngineFunctionsGenerator]
struct EngineFunctions
{
static Self _functions;
/// Sends the references to all ScriptGlue functions to the script library.
[RegisterMethod]
public static void FillEngineFunctions()
{
@@ -210,7 +213,7 @@ struct EngineFunctions
}
}
/* Adding this attribute to a method will log method entry and returned Result<T> errors */
/// Collects all methods in ScriptGlue that have a RegisterCallAttribute. Generates the assignment of the corresponding function pointer in EngineFunctions.
[AttributeUsage(.Method | .Constructor)]
public struct RegisterMethodAttribute : Attribute, IOnMethodInit
{
@@ -262,6 +265,7 @@ static class ScriptGlue
private static String _lastExceptionMessage = new .() ~ delete _;
/// Initializes the glue layer between native engine and script library.
public static void Init()
{
if (_setEngineFunctions == null)
@@ -274,6 +278,13 @@ static class ScriptGlue
RegisterManagedComponents();
}
/// Collects and sends the function pointers to all glue functions to the script library.
private static void RegisterCalls()
{
EngineFunctions.FillEngineFunctions();
}
/// Creates the function pointers to create, remove and check for components and sends them to the script library.
public static void RegisterManagedComponents()
{
Debug.Profiler.ProfileFunction!();
@@ -288,11 +299,7 @@ static class ScriptGlue
RegisterComponent<MeshRendererComponent>();
}
private static void RegisterCalls()
{
EngineFunctions.FillEngineFunctions();
}
/// Register the function pointers to create, remove and check for components of the specified type and sends them to the script library.
private static void RegisterComponent<T>() where T : struct, new
{
String fullComponentTypeName = scope String();
@@ -1558,6 +1565,7 @@ static class ScriptGlue
asset
}*/
/// Set an exception message that the script library can collect when a glue function returned an error.
static void SetExceptionMessage(StringView message)
{
_lastExceptionMessage.Set(message);
@@ -1,6 +1,7 @@
using System.Collections;
using GlitchyEngine.Core;
using GlitchyEngine.Serialization;
using System;
namespace GlitchyEngine.Scripting;
@@ -45,6 +46,8 @@ public class ScriptInstanceSerializer
{
Debug.Profiler.ProfileFunction!();
Log.EngineLogger.Trace($"Serializing {Internal.UnsafeCastToPtr(this)}...");
Init();
for (let (id, scriptInstance) in ScriptEngine._entityScriptInstances)
@@ -78,6 +81,8 @@ public class ScriptInstanceSerializer
public void DeserializeScriptInstances()
{
Debug.Profiler.ProfileFunction!();
Log.EngineLogger.Trace($"Deserializing {Internal.UnsafeCastToPtr(this)}...");
Init();