diff --git a/Runtime/Game/LootLockerObfuscator.cs b/Runtime/Game/LootLockerObfuscator.cs index b46aa66a..b78f2369 100644 --- a/Runtime/Game/LootLockerObfuscator.cs +++ b/Runtime/Game/LootLockerObfuscator.cs @@ -1,7 +1,12 @@ -using LLlibs.ZeroDepJson; using System; using System.Collections.Generic; using System.Text; +#if LOOTLOCKER_USE_NEWTONSOFTJSON +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +#else +using LLlibs.ZeroDepJson; +#endif namespace LootLocker { diff --git a/Runtime/Libraries/ZeroDepJson/ZeroDepJson.cs b/Runtime/Libraries/ZeroDepJson/ZeroDepJson.cs index 43bacdf5..baa7fac3 100644 --- a/Runtime/Libraries/ZeroDepJson/ZeroDepJson.cs +++ b/Runtime/Libraries/ZeroDepJson/ZeroDepJson.cs @@ -536,6 +536,33 @@ private static bool TryGetObjectDefaultValue(PropertyDescriptor pd, out object v return false; } + private static bool TryGetObjectConditionalSerialization(Type type, MemberDefinition memberDefinition, out MethodInfo conditionalSerializationMethodInfo) + { + conditionalSerializationMethodInfo = null; + List potentialConditionalSerializationMethodNames = new List + { + "ShouldSerialize" + memberDefinition.EscapedWireName, + "ShouldSerialize" + memberDefinition.WireName, + "ShouldSerialize" + memberDefinition.Name + }; + foreach (string methodName in potentialConditionalSerializationMethodNames) + { + try + { + MethodInfo methodInfo = type.GetMethod(methodName); + if (methodInfo != null && methodInfo.ReturnType == typeof(bool) && methodInfo.GetParameters().Length == 0) + { + conditionalSerializationMethodInfo = methodInfo; + break; + } + } + catch (AmbiguousMatchException) { } + catch (ArgumentNullException) { } + } + + return conditionalSerializationMethodInfo != null; + } + private static string GetObjectName(PropertyDescriptor pd, string defaultName) { foreach (var att in pd.Attributes.Cast()) @@ -1649,6 +1676,22 @@ public virtual string EscapedWireName /// public virtual object DefaultValue { get; set; } + /// + /// Gets or sets a value indicating whether this instance has conditional serialization. + /// + /// + /// true if this instance has conditional serialization; otherwise, false. + /// + public virtual bool HasConditionalSerialization { get; set; } + + /// + /// Gets or sets conditional serialization method info + /// + /// + /// The info of the method on the type that tells whether this property should be serialized + /// + public virtual MethodInfo ConditionalSerializationMethod { get; set; } + /// /// Gets or sets the accessor. /// @@ -3208,6 +3251,14 @@ public void WriteValues(TextWriter writer, object component, IDictionary EnumerateDefinitionsUsingReflection ma.HasDefaultValue = TryGetObjectDefaultValue(info, out var defaultValue); ma.DefaultValue = defaultValue; + ma.HasConditionalSerialization = TryGetObjectConditionalSerialization(type, ma, out MethodInfo conditionalSerializationMethodInfo); + ma.ConditionalSerializationMethod = conditionalSerializationMethodInfo; ma.Accessor = (IMemberAccessor)Activator.CreateInstance(typeof(PropertyInfoAccessor<,>).MakeGenericType(info.DeclaringType, info.PropertyType), info); yield return ma; } @@ -3452,6 +3505,8 @@ private static IEnumerable EnumerateDefinitionsUsingReflection ma.HasDefaultValue = TryGetObjectDefaultValue(info, out var defaultValue); ma.DefaultValue = defaultValue; + ma.HasConditionalSerialization = TryGetObjectConditionalSerialization(type, ma, out MethodInfo conditionalSerializationMethodInfo); + ma.ConditionalSerializationMethod = conditionalSerializationMethodInfo; ma.Accessor = (IMemberAccessor)Activator.CreateInstance(typeof(FieldInfoAccessor), info); yield return ma; } @@ -3509,6 +3564,8 @@ private static IEnumerable EnumerateDefinitionsUsingTypeDescri ma.HasDefaultValue = TryGetObjectDefaultValue(descriptor, out var defaultValue); ma.DefaultValue = defaultValue; + ma.HasConditionalSerialization = TryGetObjectConditionalSerialization(type, ma, out MethodInfo conditionalSerializationMethodInfo); + ma.ConditionalSerializationMethod = conditionalSerializationMethodInfo; ma.Accessor = (IMemberAccessor)Activator.CreateInstance(typeof(PropertyDescriptorAccessor), descriptor); yield return ma; }