From 51af251f48e4dd5f8058bd726c751541dd027cb4 Mon Sep 17 00:00:00 2001 From: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com> Date: Thu, 13 Aug 2026 19:03:20 +0200 Subject: [PATCH 1/5] Remove ResetStaticFields method from DbConnectionBase Removed ResetStaticFields since [RuntimeInitializeOnLoadMethod] cannot be used on methods within generic classes. DbConnectionBase.IsTesting is the only field reset by this mechanism, and leaving it uncleared should not affect Unity. Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com> --- sdks/csharp/src/SpacetimeDBClient.cs | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/sdks/csharp/src/SpacetimeDBClient.cs b/sdks/csharp/src/SpacetimeDBClient.cs index b5ed336e314..1129319c31c 100644 --- a/sdks/csharp/src/SpacetimeDBClient.cs +++ b/sdks/csharp/src/SpacetimeDBClient.cs @@ -135,6 +135,12 @@ public abstract class DbConnectionBase : IDbConne where DbConnection : DbConnectionBase, new() where Tables : RemoteTablesBase { + /// + /// This isn't reset since [RuntimeInitializeOnLoadMethod] methods cannot be in generic types + /// We assume that the user will reset this if needed; Unity will give an error about this field not being reset. + /// One way we can get around this in the future is using AutoStaticsCleanup + /// But that requires Unity 6.5 + /// internal static bool IsTesting { get; set; } = false; public static DbConnectionBuilder Builder() => new(); @@ -292,23 +298,6 @@ internal struct ParsedMessage private static readonly Status Committed = new Status.Committed(default); -#if UNITY_5_3_OR_NEWER - /// - /// Resets the static instance to prevent data persistence when Enter Play Mode Options (Disable Domain Reloading) is active. - /// RuntimeInitializeOnLoadMethod is used since it is supported in older versions of Unity. - /// AutoStaticsCleanup and NoAutoStaticsCleanup is only supported in Unity 6+ - /// - /// - /// See the Unity Domain Reloading Manual - /// and the RuntimeInitializeOnLoadMethodAttribute API Docs for details. - /// - [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] - private static void ResetStaticFields() - { - IsTesting = false; - } -#endif - /// /// Get a description of a message suitable for storing in the tracker metadata. /// From 4b75793c166451014262eb4a1bb1d446329ecde7 Mon Sep 17 00:00:00 2001 From: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com> Date: Thu, 13 Aug 2026 19:13:36 +0200 Subject: [PATCH 2/5] Correct static reset callbacks for RemoteTableHandle Provides a mechanism for registering and resetting static fields during Unity's subsystem registration phase. Unity's RuntimeInitializeOnLoadMethod can't be used in generic classes. Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com> --- sdks/csharp/src/Table.cs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/sdks/csharp/src/Table.cs b/sdks/csharp/src/Table.cs index 5847f5728df..7e755d7d7bb 100644 --- a/sdks/csharp/src/Table.cs +++ b/sdks/csharp/src/Table.cs @@ -188,6 +188,13 @@ public RemoteTableHandleBase(IDbConnection conn, bool isEventTable = false) : ba IsEventTable = isEventTable; } +#if UNITY_5_3_OR_NEWER + static RemoteTableHandleBase() + { + RemoteTableHandleStaticReset.Register(() => _serializer = null); + } +#endif + // This method needs to be overridden by autogen. protected virtual object? GetPrimaryKey(Row row) => null; @@ -683,5 +690,37 @@ public abstract class RemoteEventTableHandle : RemoteTableHan { protected RemoteEventTableHandle(IDbConnection conn) : base(conn, isEventTable: true) { } } + +#if UNITY_5_3_OR_NEWER + /// + /// Provides a mechanism for registering and invoking static reset callbacks + /// during Unity's subsystem registration phase. + /// This is because Unity's RuntimeInitializeOnLoadMethod can't be used in generic classes + /// One way we can get around this in the future is using AutoStaticsCleanup + /// But that requires Unity 6.5 + /// + /// + /// Our generic static constructor doesn't necessarily execute for every table type before Unity's SubsystemRegistration callback runs + /// So this will only reset types that have already been initialized/registered. Which should be okay. + /// + internal static class RemoteTableHandleStaticReset + { + private static readonly List Resets = new(); + + internal static void Register(Action reset) + { + Resets.Add(reset); + } + + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] + private static void Reset() + { + foreach (var reset in Resets) + { + reset(); + } + } + } +#endif } #nullable disable From 332b9537a8ccdae3a7f23af7c95ffdda8e1b5f44 Mon Sep 17 00:00:00 2001 From: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com> Date: Thu, 13 Aug 2026 19:19:45 +0200 Subject: [PATCH 3/5] Refactor RemoteTableHandleBase static constructor documentation Updated the static constructor documentation for RemoteTableHandleBase to clarify its functionality regarding Unity's domain reloading. Removed the older ResetStaticFields method that was specific to older Unity versions. Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com> --- sdks/csharp/src/Table.cs | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/sdks/csharp/src/Table.cs b/sdks/csharp/src/Table.cs index 7e755d7d7bb..91308c8867f 100644 --- a/sdks/csharp/src/Table.cs +++ b/sdks/csharp/src/Table.cs @@ -187,8 +187,17 @@ public RemoteTableHandleBase(IDbConnection conn, bool isEventTable = false) : ba { IsEventTable = isEventTable; } - + #if UNITY_5_3_OR_NEWER + /// + /// Resets the static instance to prevent data persistence when Enter Play Mode Options (Disable Domain Reloading) is active. + /// RuntimeInitializeOnLoadMethod can't be used here since this is a generic class, so we have to handle it ourselves. + /// AutoStaticsCleanup and NoAutoStaticsCleanup is only supported in Unity 6+ + /// + /// + /// See the Unity Domain Reloading Manual + /// and the RuntimeInitializeOnLoadMethodAttribute API Docs for details. + /// static RemoteTableHandleBase() { RemoteTableHandleStaticReset.Register(() => _serializer = null); @@ -252,24 +261,6 @@ private static IReadWrite Serializer } } - -#if UNITY_5_3_OR_NEWER - /// - /// Resets the static instance to prevent data persistence when Enter Play Mode Options (Disable Domain Reloading) is active. - /// RuntimeInitializeOnLoadMethod is used since it is supported in older versions of Unity. - /// AutoStaticsCleanup and NoAutoStaticsCleanup is only supported in Unity 6+ - /// - /// - /// See the Unity Domain Reloading Manual - /// and the RuntimeInitializeOnLoadMethodAttribute API Docs for details. - /// - [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] - private static void ResetStaticFields() - { - _serializer = null; - } -#endif - // The function to use for decoding a type value. Row DecodeValue(BinaryReader reader) => Serializer.Read(reader); From be5028108dc6c284190bb007452844ec80ea1add Mon Sep 17 00:00:00 2001 From: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com> Date: Thu, 13 Aug 2026 19:20:07 +0200 Subject: [PATCH 4/5] Fix indentation in Reset method Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com> --- sdks/csharp/src/Table.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdks/csharp/src/Table.cs b/sdks/csharp/src/Table.cs index 91308c8867f..e23ad41871a 100644 --- a/sdks/csharp/src/Table.cs +++ b/sdks/csharp/src/Table.cs @@ -708,7 +708,7 @@ private static void Reset() { foreach (var reset in Resets) { - reset(); + reset(); } } } From 91452907dbbd6f43acd58644d30e0c71f00c4b4e Mon Sep 17 00:00:00 2001 From: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com> Date: Sat, 15 Aug 2026 13:21:06 +0200 Subject: [PATCH 5/5] Correct remarks tag in SpacetimeDBClient.cs Fix XML documentation remarks tag formatting. Signed-off-by: Vanessa Vinther <7903603+ImDreamerDev@users.noreply.github.com> --- sdks/csharp/src/SpacetimeDBClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdks/csharp/src/SpacetimeDBClient.cs b/sdks/csharp/src/SpacetimeDBClient.cs index 1129319c31c..6af78ccafc5 100644 --- a/sdks/csharp/src/SpacetimeDBClient.cs +++ b/sdks/csharp/src/SpacetimeDBClient.cs @@ -140,7 +140,7 @@ public abstract class DbConnectionBase : IDbConne /// We assume that the user will reset this if needed; Unity will give an error about this field not being reset. /// One way we can get around this in the future is using AutoStaticsCleanup /// But that requires Unity 6.5 - /// + /// internal static bool IsTesting { get; set; } = false; public static DbConnectionBuilder Builder() => new();