Codestyle formatting and fixes for #1888
This commit is contained in:
@@ -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");
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
|
||||
|
||||
using FlaxEngine;
|
||||
using FlaxEditor.Utilities;
|
||||
using FlaxEngine.Utilities;
|
||||
|
||||
namespace FlaxEditor.States
|
||||
|
||||
@@ -297,9 +297,10 @@ namespace FlaxEngine.Interop
|
||||
|
||||
return type;
|
||||
|
||||
/// <summary>Resolve the type by manually checking every scripting assembly</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Resolve the assembly by name</summary>
|
||||
static Assembly ResolveAssembly(AssemblyName name) => ResolveScriptingAssemblyByName(name, allowPartial: false);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<TypeKey, Type> _typeCache;
|
||||
private Func<TypeKey, Type> _resolveType;
|
||||
|
||||
|
||||
/// <summary>Clear the cache</summary>
|
||||
/// <remarks>Should be cleared on scripting domain reload to avoid out of date types participating in dynamic type resolution</remarks>
|
||||
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
|
||||
/// <summary>Attempt to find the assembly in the current domain</summary>
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>Attempt to find a type in a specified assembly</summary>
|
||||
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<Type> genericTypeArguments = new List<Type>(); // 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<char> typeName;
|
||||
ReadOnlySpan<char> assemblyName;
|
||||
|
||||
if (assemblyDelimiterIndex != null) {
|
||||
ReadOnlySpan<char> 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)
|
||||
|
||||
@@ -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
|
||||
/// <summary>Check that the cache is up to date, rebuild it if it isn't</summary>
|
||||
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
|
||||
}
|
||||
|
||||
/// <summary>Builds the serializer</summary>
|
||||
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<SerializerCache> Current = new ThreadLocal<SerializerCache>();
|
||||
internal static ThreadLocal<SerializerCache> Cache = new ThreadLocal<SerializerCache>(() => new SerializerCache(Settings));
|
||||
internal static ThreadLocal<SerializerCache> CacheManagedOnly = new ThreadLocal<SerializerCache>(() => new SerializerCache(SettingsManagedOnly));
|
||||
internal static ThreadLocal<IntPtr> CachedGuidBuffer = new ThreadLocal<IntPtr>(() => Marshal.AllocHGlobal(32 * sizeof(char)), true);
|
||||
internal static string CachedGuidDigits = "0123456789abcdef";
|
||||
#if FLAX_EDITOR
|
||||
/// <summary>The version of the cache, used to check that a cache is not out of date</summary>
|
||||
internal static uint currentCacheVersion = 0;
|
||||
internal static uint CurrentCacheVersion = 0;
|
||||
|
||||
/// <summary>Used to synchronize cache operations such as <see cref="SerializerCache"/> rebuild, and <see cref="ResetCache"/></summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Called to reset the serialization cache</summary>
|
||||
#if FLAX_EDITOR
|
||||
/// <summary>Resets the serialization cache.</summary>
|
||||
internal static void ResetCache()
|
||||
{
|
||||
lock (cacheSyncRoot)
|
||||
lock (CurrentCacheSyncRoot)
|
||||
{
|
||||
unchecked { currentCacheVersion++; }
|
||||
unchecked
|
||||
{
|
||||
CurrentCacheVersion++;
|
||||
}
|
||||
|
||||
Newtonsoft.Json.JsonSerializer.ClearCache();
|
||||
SerializationBinder.ResetCache();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
internal static void Dispose()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user