Skip to content

Commit

Permalink
fixes #619
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Dec 20, 2024
1 parent 25ce7d3 commit 365a5e1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Harmony/Tools/AccessTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1994,7 +1994,8 @@ public static object MakeDeepCopy(object source, Type resultType, Func<string, T
{
var path = pathRoot.Length > 0 ? pathRoot + "." + name : name;
var value = processor is not null ? processor(path, src, dst) : src.GetValue();
_ = dst.SetValue(MakeDeepCopy(value, dst.GetValueType(), processor, path));
if (dst.IsWriteable)
_ = dst.SetValue(MakeDeepCopy(value, dst.GetValueType(), processor, path));
});
return result;
}
Expand Down
35 changes: 34 additions & 1 deletion Harmony/Tools/Traverse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,35 @@ public Type GetValueType()
return null;
}

/// <summary>Checks if the current traverse instance is for a field</summary>
/// <returns>True if its a field</returns>
///
public bool IsField => _info is FieldInfo;

/// <summary>Checks if the current traverse instance is for a property</summary>
/// <returns>True if its a property</returns>
///
public bool IsProperty => _info is PropertyInfo;

/// <summary>Checks if the current field or property is writeable</summary>
/// <returns>True if writing is possible</returns>
///
public bool IsWriteable
{
get
{
if (_info is FieldInfo fi)
{
var isConst = fi.IsLiteral && fi.IsInitOnly == false && fi.IsStatic;
var isStaticReadonly = fi.IsLiteral == false && fi.IsInitOnly && fi.IsStatic;
return isConst == false && isStaticReadonly == false;
}
if (_info is PropertyInfo pi)
return pi.CanWrite;
return false;
}
}

Traverse Resolve()
{
if (_root is null)
Expand Down Expand Up @@ -407,7 +436,11 @@ public static void IterateProperties(object source, object target, Action<string

/// <summary>A default field action that copies fields to fields</summary>
///
public static Action<Traverse, Traverse> CopyFields = (from, to) => { _ = to.SetValue(from.GetValue()); };
public static Action<Traverse, Traverse> CopyFields = (from, to) =>
{
if (to.IsWriteable)
_ = to.SetValue(from.GetValue());
};

/// <summary>Returns a string that represents the current traverse</summary>
/// <returns>A string representation</returns>
Expand Down

0 comments on commit 365a5e1

Please sign in to comment.