Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion JECS/DataFiles/DataFileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ void SetFileContents()
type = concreteType;
}

foreach (var m in type.GetValidMembers())
foreach (var m in type.GetValidMembers(false))
dataFile.SetNonGeneric(m.MemberType, m.Name, m.GetValue(saveThis));
}
}
Expand Down
12 changes: 12 additions & 0 deletions JECS/Parsing Logic/OnlyReadThisAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace JECS
{
/// <summary>
/// Private fields and properties with this attribute WILL be loaded by JECS but WONT be written when saving.
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class OnlyReadThisAttribute : SaveThisAttribute
{
}
}
24 changes: 24 additions & 0 deletions JECS/Parsing Logic/Types/Complex Types/ComplexTypeOverrides.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ public static class ComplexTypeOverrides
{
private static HashSet<PropertyInfo> AlwaysSaveProperties = new HashSet<PropertyInfo>();
private static HashSet<PropertyInfo> NeverSaveProperties = new HashSet<PropertyInfo>();
private static HashSet<PropertyInfo> OnlyReadProperties = new HashSet<PropertyInfo>();

private static HashSet<FieldInfo> AlwaysSaveFields = new HashSet<FieldInfo>();
private static HashSet<FieldInfo> NeverSaveFields = new HashSet<FieldInfo>();
private static HashSet<FieldInfo> OnlyReadFields = new HashSet<FieldInfo>();


/// <summary>
Expand All @@ -39,6 +41,16 @@ public static void NeverSave(PropertyInfo property)
NeverSaveProperties.Add(property);
}

/// <summary>
/// Always read this property, even if it's private but never save it.
/// </summary>
public static void OnlyRead(PropertyInfo property)
{
AlwaysSave(property);

OnlyReadProperties.Add(property);
}


/// <summary>
/// Always save this field, even if it's private.
Expand All @@ -62,10 +74,22 @@ public static void NeverSave(FieldInfo field)
NeverSaveFields.Add(field);
}

/// <summary>
/// Always read this field, even if it's private but never save it.
/// </summary>
public static void OnlyRead(FieldInfo field)
{
AlwaysSave(field);

OnlyReadFields.Add(field);
}

internal static bool IsAlwaysSaved(PropertyInfo property) => AlwaysSaveProperties.Contains(property);
internal static bool IsNeverSaved(PropertyInfo property) => NeverSaveProperties.Contains(property);
internal static bool IsOnlyRead(PropertyInfo property) => OnlyReadProperties.Contains(property);

internal static bool IsAlwaysSaved(FieldInfo field) => AlwaysSaveFields.Contains(field);
internal static bool IsNeverSaved(FieldInfo field) => NeverSaveFields.Contains(field);
internal static bool IsOnlyRead(FieldInfo field) => OnlyReadFields.Contains(field);
}
}
6 changes: 5 additions & 1 deletion JECS/Parsing Logic/Types/Complex Types/ComplexTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal static object RetrieveComplexType(Node node, Type type)
return returnThis;
}

internal static IEnumerable<ClassMember> GetValidMembers(this Type type)
internal static IEnumerable<ClassMember> GetValidMembers(this Type type, bool includeOnlyRead = true)
{
var members = new List<ClassMember>();

Expand All @@ -76,6 +76,8 @@ internal static IEnumerable<ClassMember> GetValidMembers(this Type type)
continue;
if (f.IsPrivate && !Attribute.IsDefined(f, typeof(SaveThisAttribute)) && !ComplexTypeOverrides.IsAlwaysSaved(f))
continue;
if (!includeOnlyRead && (Attribute.IsDefined(f, typeof(OnlyReadThisAttribute)) || ComplexTypeOverrides.IsOnlyRead(f)))
continue;

members.Add(f);
}
Expand All @@ -90,6 +92,8 @@ internal static IEnumerable<ClassMember> GetValidMembers(this Type type)
continue;
if (p.GetMethod.IsPrivate && !Attribute.IsDefined(p, typeof(SaveThisAttribute)) && !ComplexTypeOverrides.IsAlwaysSaved(p))
continue;
if (!includeOnlyRead && (Attribute.IsDefined(p, typeof(OnlyReadThisAttribute)) || ComplexTypeOverrides.IsOnlyRead(p)))
continue;

members.Add(p);
}
Expand Down