Skip to content

Commit

Permalink
Fixed RuntimeUtils.GetSerializableFields not following all rules
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsakharov committed Apr 4, 2024
1 parent fae2498 commit 2e8f3d3
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 18 deletions.
11 changes: 0 additions & 11 deletions Prowl.Runtime/Serializer/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,17 +394,6 @@ static object CreateInstance(Type type)
return Activator.CreateInstance(type) ?? throw new InvalidOperationException("Failed to create instance of type: " + type);
}

static IEnumerable<FieldInfo> GetAllFields(Type? t)
{
if (t == null)
return Enumerable.Empty<FieldInfo>();

BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.DeclaredOnly;

return t.GetFields(flags).Concat(GetAllFields(t.BaseType));
}

#endregion
}
}
4 changes: 0 additions & 4 deletions Prowl.Runtime/Utils/NodeSystem/NodeGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,6 @@ protected virtual void OnDestroy()

public void OnBeforeSerialize() { }

public void PostSerialize() { }

public void PreDeserialize() { }

public void OnAfterDeserialize()
{
// Clear null nodes
Expand Down
2 changes: 1 addition & 1 deletion Prowl.Runtime/Utils/NodeSystem/NodePort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Prowl.Runtime.NodeSystem
{

[Serializable]
public class NodePort
{
public enum IO { Input, Output }
Expand Down
15 changes: 13 additions & 2 deletions Prowl.Runtime/Utils/RuntimeUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,25 @@ public static Type FindType(string qualifiedTypeName)

public static FieldInfo[] GetSerializableFields(object target)
{
FieldInfo[] fields = target.GetType().GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
FieldInfo[] fields = GetAllFields(target.GetType()).ToArray();
// Only allow Publics or ones with SerializeField
fields = fields.Where(field => field.IsPublic || Attribute.IsDefined(field, typeof(SerializeFieldAttribute))).ToArray();
fields = fields.Where(field => (field.IsPublic || field.GetCustomAttribute<SerializeFieldAttribute>() != null) && field.GetCustomAttribute<SerializeIgnoreAttribute>() == null).ToArray();
// Remove Public NonSerialized fields
fields = fields.Where(field => !field.IsPublic || field.GetCustomAttribute<NonSerializedAttribute>() == null).ToArray();
return fields;
}

public static IEnumerable<FieldInfo> GetAllFields(Type? t)
{
if (t == null)
return Enumerable.Empty<FieldInfo>();

BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Instance | BindingFlags.DeclaredOnly;

return t.GetFields(flags).Concat(GetAllFields(t.BaseType));
}

public static object? GetValue(this MemberInfo member, object? target)
{
if (member is PropertyInfo prop)
Expand Down

0 comments on commit 2e8f3d3

Please sign in to comment.