diff --git a/Source/Editor/Progress/Handlers/CompileScriptsProgress.cs b/Source/Editor/Progress/Handlers/CompileScriptsProgress.cs
index 310612a73..cbb383b4c 100644
--- a/Source/Editor/Progress/Handlers/CompileScriptsProgress.cs
+++ b/Source/Editor/Progress/Handlers/CompileScriptsProgress.cs
@@ -25,7 +25,6 @@ namespace FlaxEditor.Progress.Handlers
ScriptsBuilder.ScriptsReloadCalled += () => OnUpdate(0.8f, "Reloading scripts...");
ScriptsBuilder.ScriptsReloadBegin += OnScriptsReloadBegin;
ScriptsBuilder.ScriptsReloadEnd += OnScriptsReloadEnd;
- ScriptsBuilder.ScriptsReload += OnScriptsReload;
}
private void OnScriptsReloadBegin()
@@ -38,14 +37,6 @@ namespace FlaxEditor.Progress.Handlers
Editor.Instance.Scene.ClearRefsToSceneObjects(true);
}
- private void OnScriptsReload()
- {
-#if !USE_NETCORE
- // Clear types cache
- Newtonsoft.Json.JsonSerializer.ClearCache();
-#endif
- }
-
private void OnCompilationFailed()
{
OnFail("Scripts compilation failed");
diff --git a/Source/Editor/States/ReloadingScriptsState.cs b/Source/Editor/States/ReloadingScriptsState.cs
index 4b8866202..d9024d5c8 100644
--- a/Source/Editor/States/ReloadingScriptsState.cs
+++ b/Source/Editor/States/ReloadingScriptsState.cs
@@ -1,7 +1,6 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using FlaxEngine;
-using FlaxEditor.Utilities;
using FlaxEngine.Utilities;
namespace FlaxEditor.States
diff --git a/Source/Engine/Engine/NativeInterop.cs b/Source/Engine/Engine/NativeInterop.cs
index b4635e0b4..3f19f5951 100644
--- a/Source/Engine/Engine/NativeInterop.cs
+++ b/Source/Engine/Engine/NativeInterop.cs
@@ -297,9 +297,10 @@ namespace FlaxEngine.Interop
return type;
- /// Resolve the type by manually checking every scripting assembly
- static Type ResolveSlow(string typeName) {
- foreach (var assembly in scriptingAssemblyLoadContext.Assemblies) {
+ static Type ResolveSlow(string typeName)
+ {
+ foreach (var assembly in scriptingAssemblyLoadContext.Assemblies)
+ {
var type = assembly.GetType(typeName);
if (type != null)
return type;
@@ -307,7 +308,6 @@ namespace FlaxEngine.Interop
return null;
}
- /// Resolve the assembly by name
static Assembly ResolveAssembly(AssemblyName name) => ResolveScriptingAssemblyByName(name, allowPartial: false);
}
diff --git a/Source/Engine/Serialization/JsonCustomSerializers/ExtendedSerializationBinder.cs b/Source/Engine/Serialization/JsonCustomSerializers/ExtendedSerializationBinder.cs
index b4f0173ab..f239a3384 100644
--- a/Source/Engine/Serialization/JsonCustomSerializers/ExtendedSerializationBinder.cs
+++ b/Source/Engine/Serialization/JsonCustomSerializers/ExtendedSerializationBinder.cs
@@ -3,8 +3,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
-using System.Globalization;
-using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using FlaxEngine.Interop;
@@ -22,7 +20,6 @@ namespace FlaxEngine.Json.JsonCustomSerializers
private ConcurrentDictionary _typeCache;
private Func _resolveType;
-
/// Clear the cache
/// Should be cleared on scripting domain reload to avoid out of date types participating in dynamic type resolution
public void ResetCache()
@@ -51,14 +48,19 @@ namespace FlaxEngine.Json.JsonCustomSerializers
Type ResolveType(TypeKey key)
{
Type? type = null;
- if (key.assemblyName is null) { // No assembly name, attempt to find globally
+ if (key.assemblyName is null)
+ {
+ // No assembly name, attempt to find globally
type = FindTypeGlobal(key.typeName);
}
- if (type is null && key.assemblyName is not null) { // Type not found yet, but we have assembly name
+ if (type is null && key.assemblyName is not null)
+ {
+ // Type not found yet, but we have assembly name
var assembly = ResolveAssembly(new(key.assemblyName));
- type = FindTypeInAssembly(key.typeName, assembly); // We have assembly, attempt to load from assembly
+ // We have assembly, attempt to load from assembly
+ type = FindTypeInAssembly(key.typeName, assembly);
}
//if (type is null)
@@ -74,18 +76,13 @@ namespace FlaxEngine.Json.JsonCustomSerializers
Assembly ResolveAssembly(AssemblyName name)
{
Assembly? assembly = null;
-
assembly = FindScriptingAssembly(name); // Attempt to find in scripting assemblies
-
if (assembly is null)
assembly = FindLoadAssembly(name); // Attempt to load
-
if (assembly is null)
assembly = FindDomainAssembly(name); // Attempt to find in the current domain
-
if (assembly is null)
throw MakeAsmResolutionException(name.FullName); // Assembly failed to resolve
-
return assembly;
}
@@ -98,15 +95,14 @@ namespace FlaxEngine.Json.JsonCustomSerializers
/// Attempt to find the assembly in the current domain
Assembly? FindDomainAssembly(AssemblyName assemblyName)
{
- var assemblies = AppDomain.CurrentDomain.GetAssemblies().ToArray();
-
- foreach (Assembly assembly in assemblies) { // Looking in domain may be necessary (in case of anon dynamic assembly for example)
+ var assemblies = AppDomain.CurrentDomain.GetAssemblies();
+ foreach (Assembly assembly in assemblies)
+ {
+ // Looking in domain may be necessary (in case of anon dynamic assembly for example)
var curName = assembly.GetName();
-
if (curName == assemblyName || curName.Name == assemblyName.Name)
return assembly;
}
-
return null;
}
@@ -114,24 +110,20 @@ namespace FlaxEngine.Json.JsonCustomSerializers
Assembly? FindLoadAssembly(AssemblyName assemblyName)
{
Assembly? assembly = null;
-
assembly = Assembly.Load(assemblyName);
-
- if (assembly is null)
+ if (assembly is null && assemblyName.Name is not null)
+#pragma warning disable CS0618 // Type or member is obsolete
assembly = Assembly.LoadWithPartialName(assemblyName.Name); // Copying behavior of DefaultSerializationBinder
-
+#pragma warning restore CS0618 // Type or member is obsolete
return assembly;
}
-
/// Attempt to find a type in a specified assembly
Type? FindTypeInAssembly(string typeName, Assembly assembly)
{
var type = assembly.GetType(typeName); // Attempt to load directly
-
if (type is null && typeName.IndexOf('`') >= 0) // Attempt failed, but name has generic variant tick, try resolving generic manually
type = FindTypeGeneric(typeName, assembly);
-
return type;
}
@@ -146,7 +138,6 @@ namespace FlaxEngine.Json.JsonCustomSerializers
{
return _typeCache.GetOrAdd(key, _resolveType);
}
-
/*********************************************
** Below code is adapted from Newtonsoft.Json
@@ -157,26 +148,33 @@ namespace FlaxEngine.Json.JsonCustomSerializers
{
Type? type = null;
int openBracketIndex = typeName.IndexOf('[', StringComparison.Ordinal);
- if (openBracketIndex >= 0) {
+ if (openBracketIndex >= 0)
+ {
string genericTypeDefName = typeName.Substring(0, openBracketIndex); // Find the unspecialized type
Type? genericTypeDef = assembly.GetType(genericTypeDefName);
- if (genericTypeDef != null) {
+ if (genericTypeDef != null)
+ {
List genericTypeArguments = new List(); // Recursively resolve the arguments
int scope = 0;
int typeArgStartIndex = 0;
int endIndex = typeName.Length - 1;
- for (int i = openBracketIndex + 1; i < endIndex; ++i) {
+ for (int i = openBracketIndex + 1; i < endIndex; ++i)
+ {
char current = typeName[i];
- switch (current) {
+ switch (current)
+ {
case '[':
- if (scope == 0) {
+ if (scope == 0)
+ {
typeArgStartIndex = i + 1;
}
++scope;
break;
case ']':
--scope;
- if (scope == 0) { // All arguments resolved, compose our type
+ if (scope == 0)
+ {
+ // All arguments resolved, compose our type
string typeArgAssemblyQualifiedName = typeName.Substring(typeArgStartIndex, i - typeArgStartIndex);
TypeKey typeNameKey = SplitFullyQualifiedTypeName(typeArgAssemblyQualifiedName);
@@ -189,7 +187,6 @@ namespace FlaxEngine.Json.JsonCustomSerializers
type = genericTypeDef.MakeGenericType(genericTypeArguments.ToArray());
}
}
-
return type;
}
@@ -197,18 +194,17 @@ namespace FlaxEngine.Json.JsonCustomSerializers
private static TypeKey SplitFullyQualifiedTypeName(string fullyQualifiedTypeName)
{
int? assemblyDelimiterIndex = GetAssemblyDelimiterIndex(fullyQualifiedTypeName);
-
- ReadOnlySpan typeName;
- ReadOnlySpan assemblyName;
-
- if (assemblyDelimiterIndex != null) {
+ ReadOnlySpan typeName, assemblyName;
+ if (assemblyDelimiterIndex != null)
+ {
typeName = fullyQualifiedTypeName.AsSpan().Slice(0, assemblyDelimiterIndex ?? 0);
assemblyName = fullyQualifiedTypeName.AsSpan().Slice((assemblyDelimiterIndex ?? 0) + 1);
- } else {
+ }
+ else
+ {
typeName = fullyQualifiedTypeName;
assemblyName = null;
}
-
return new(new(assemblyName), new(typeName));
}
@@ -218,9 +214,11 @@ namespace FlaxEngine.Json.JsonCustomSerializers
// we need to get the first comma following all surrounded in brackets because of generic types
// e.g. System.Collections.Generic.Dictionary`2[[System.String, mscorlib,Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.String, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
int scope = 0;
- for (int i = 0; i < fullyQualifiedTypeName.Length; i++) {
+ for (int i = 0; i < fullyQualifiedTypeName.Length; i++)
+ {
char current = fullyQualifiedTypeName[i];
- switch (current) {
+ switch (current)
+ {
case '[':
scope++;
break;
@@ -228,7 +226,8 @@ namespace FlaxEngine.Json.JsonCustomSerializers
scope--;
break;
case ',':
- if (scope == 0) {
+ if (scope == 0)
+ {
return i;
}
break;
@@ -243,7 +242,7 @@ namespace FlaxEngine.Json.JsonCustomSerializers
{
return new($"Could not load assembly '{asmName}'.");
}
-
+
private static JsonSerializationException MakeTypeResolutionException(string? asmName, string typeName)
{
if (asmName is null)
diff --git a/Source/Engine/Serialization/JsonSerializer.cs b/Source/Engine/Serialization/JsonSerializer.cs
index 2eb9eec99..a5d6e0771 100644
--- a/Source/Engine/Serialization/JsonSerializer.cs
+++ b/Source/Engine/Serialization/JsonSerializer.cs
@@ -19,7 +19,7 @@ namespace FlaxEngine.Json
{
internal class SerializerCache
{
- public readonly JsonSerializerSettings settings;
+ public readonly JsonSerializerSettings JsonSettings;
public Newtonsoft.Json.JsonSerializer JsonSerializer;
public StringBuilder StringBuilder;
public StringWriter StringWriter;
@@ -29,23 +29,27 @@ namespace FlaxEngine.Json
public StreamReader Reader;
public bool IsWriting;
public bool IsReading;
- public uint cacheVersion;
+#if FLAX_EDITOR
+ public uint CacheVersion;
+#endif
public unsafe SerializerCache(JsonSerializerSettings settings)
{
- this.settings = settings;
-
+ JsonSettings = settings;
StringBuilder = new StringBuilder(256);
StringWriter = new StringWriter(StringBuilder, CultureInfo.InvariantCulture);
MemoryStream = new UnmanagedMemoryStream((byte*)0, 0);
- lock (cacheSyncRoot)
+#if FLAX_EDITOR
+ lock (CurrentCacheSyncRoot)
+#endif
{
BuildSerializer();
BuildRead();
BuildWrite();
-
- cacheVersion = currentCacheVersion;
+#if FLAX_EDITOR
+ CacheVersion = Json.JsonSerializer.CurrentCacheVersion;
+#endif
}
}
@@ -57,7 +61,6 @@ namespace FlaxEngine.Json
if (IsReading)
BuildRead();
-
IsWriting = false;
IsReading = true;
}
@@ -88,29 +91,30 @@ namespace FlaxEngine.Json
/// Check that the cache is up to date, rebuild it if it isn't
private void CheckCacheVersionRebuild()
{
- var cCV = currentCacheVersion;
- if (cacheVersion == cCV)
+#if FLAX_EDITOR
+ var version = Json.JsonSerializer.CurrentCacheVersion;
+ if (CacheVersion == version)
return;
- lock (cacheSyncRoot)
+ lock (CurrentCacheSyncRoot)
{
- cCV = currentCacheVersion;
- if (cacheVersion == cCV)
+ version = Json.JsonSerializer.CurrentCacheVersion;
+ if (CacheVersion == version)
return;
BuildSerializer();
-
BuildRead();
BuildWrite();
- cacheVersion = cCV;
+ CacheVersion = version;
}
+#endif
}
/// Builds the serializer
private void BuildSerializer()
{
- JsonSerializer = Newtonsoft.Json.JsonSerializer.CreateDefault(settings);
+ JsonSerializer = Newtonsoft.Json.JsonSerializer.CreateDefault(Settings);
JsonSerializer.Formatting = Formatting.Indented;
JsonSerializer.ReferenceLoopHandling = ReferenceLoopHandling.Serialize;
}
@@ -125,7 +129,8 @@ namespace FlaxEngine.Json
private void BuildWrite()
{
SerializerWriter = new JsonSerializerInternalWriter(JsonSerializer);
- JsonWriter = new JsonTextWriter(StringWriter) {
+ JsonWriter = new JsonTextWriter(StringWriter)
+ {
IndentChar = '\t',
Indentation = 1,
Formatting = JsonSerializer.Formatting,
@@ -143,24 +148,24 @@ namespace FlaxEngine.Json
internal static JsonSerializerSettings SettingsManagedOnly = CreateDefaultSettings(true);
internal static ExtendedSerializationBinder SerializationBinder;
internal static FlaxObjectConverter ObjectConverter;
-
internal static ThreadLocal Current = new ThreadLocal();
internal static ThreadLocal Cache = new ThreadLocal(() => new SerializerCache(Settings));
internal static ThreadLocal CacheManagedOnly = new ThreadLocal(() => new SerializerCache(SettingsManagedOnly));
internal static ThreadLocal CachedGuidBuffer = new ThreadLocal(() => Marshal.AllocHGlobal(32 * sizeof(char)), true);
internal static string CachedGuidDigits = "0123456789abcdef";
+#if FLAX_EDITOR
/// The version of the cache, used to check that a cache is not out of date
- internal static uint currentCacheVersion = 0;
+ internal static uint CurrentCacheVersion = 0;
+
/// Used to synchronize cache operations such as rebuild, and
- internal static readonly object cacheSyncRoot = new();
+ internal static readonly object CurrentCacheSyncRoot = new();
+#endif
internal static JsonSerializerSettings CreateDefaultSettings(bool isManagedOnly)
{
//Newtonsoft.Json.Utilities.MiscellaneousUtils.ValueEquals = ValueEquals;
-
if (SerializationBinder is null)
SerializationBinder = new();
-
var settings = new JsonSerializerSettings
{
ContractResolver = new ExtendedDefaultContractResolver(isManagedOnly),
@@ -185,17 +190,22 @@ namespace FlaxEngine.Json
return settings;
}
- /// Called to reset the serialization cache
+#if FLAX_EDITOR
+ /// Resets the serialization cache.
internal static void ResetCache()
{
- lock (cacheSyncRoot)
+ lock (CurrentCacheSyncRoot)
{
- unchecked { currentCacheVersion++; }
+ unchecked
+ {
+ CurrentCacheVersion++;
+ }
Newtonsoft.Json.JsonSerializer.ClearCache();
SerializationBinder.ResetCache();
}
}
+#endif
internal static void Dispose()
{