Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ GameObjectRef gameObjectRef

var newComponent = go.AddComponent(type);

if (newComponent == null)
{
stringBuilder.AppendLine($"[Warning] Component '{componentName}' already exists on GameObject or cannot be added.");
continue;
}

stringBuilder.AppendLine($"[Success] Added component '{componentName}'. Component instanceID='{newComponent.GetInstanceID()}'.");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,30 @@ public partial class UnityArrayReflectionConvertor : ArrayReflectionConvertor
public override IEnumerable<FieldInfo>? GetSerializableFields(Reflector reflector, Type objType, BindingFlags flags, ILogger? logger = null)
=> objType.GetFields(flags)
.Where(field => field.GetCustomAttribute<ObsoleteAttribute>() == null)
.Where(field => field.IsPublic || field.IsPrivate && field.GetCustomAttribute<SerializeField>() != null);
.Where(field => field.IsPublic || field.IsPrivate && field.GetCustomAttribute<SerializeField>() != null)
.Where(field => !IsReflectionType(field.FieldType));

public override IEnumerable<PropertyInfo>? GetSerializableProperties(Reflector reflector, Type objType, BindingFlags flags, ILogger? logger = null)
{
var baseProperties = base.GetSerializableProperties(reflector, objType, flags, logger);
if (baseProperties == null) return null;

return baseProperties.Where(prop => !IsReflectionType(prop.PropertyType));
}

private static bool IsReflectionType(Type type)
{
// Prevent serialization of reflection types that cause circular references
if (type == null) return false;

var fullName = type.FullName;
if (string.IsNullOrEmpty(fullName)) return false;

return fullName.StartsWith("System.Reflection.") ||
fullName == "System.RuntimeType" ||
fullName == "System.Reflection.RuntimeModule" ||
fullName == "System.Reflection.RuntimeAssembly" ||
fullName == "System.Type";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ public partial class UnityGenericReflectionConvertor<T> : GenericReflectionConve
public override IEnumerable<FieldInfo>? GetSerializableFields(Reflector reflector, Type objType, BindingFlags flags, ILogger? logger = null)
=> objType.GetFields(flags)
.Where(field => field.GetCustomAttribute<ObsoleteAttribute>() == null)
.Where(field => field.IsPublic || field.IsPrivate && field.GetCustomAttribute<SerializeField>() != null);
.Where(field => field.IsPublic || field.IsPrivate && field.GetCustomAttribute<SerializeField>() != null)
.Where(field => !IsReflectionType(field.FieldType));

public override IEnumerable<PropertyInfo>? GetSerializableProperties(Reflector reflector, Type objType, BindingFlags flags, ILogger? logger = null)
{
var baseProperties = base.GetSerializableProperties(reflector, objType, flags, logger);
if (baseProperties == null) return null;

return baseProperties.Where(prop => !IsReflectionType(prop.PropertyType));
}

private static bool IsReflectionType(Type type)
{
// Prevent serialization of reflection types that cause circular references
// RuntimeType -> RuntimeModule -> RuntimeAssembly -> RuntimeModule -> ...
if (type == null) return false;

var fullName = type.FullName;
if (string.IsNullOrEmpty(fullName)) return false;

return fullName.StartsWith("System.Reflection.") ||
fullName == "System.RuntimeType" ||
fullName == "System.Reflection.RuntimeModule" ||
fullName == "System.Reflection.RuntimeAssembly" ||
fullName == "System.Type";
}
}
}