Skip to content

Commit

Permalink
Changed to assign fields via boxed reference (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
applejag authored Jul 4, 2020
1 parent 121dd64 commit 2d4dc85
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,13 @@ private static CullingGroupEvent CreateInstance(int index, int prevState, int th
byte prevStateByte = (byte)prevState;
byte thisStateByte = (byte)thisState;

var instance = new CullingGroupEvent();
TypedReference reference = __makeref(instance);

_indexField.SetValueDirect(reference, index);
_prevStateField.SetValueDirect(reference, prevStateByte);
_thisStateField.SetValueDirect(reference, thisStateByte);
object boxed = new CullingGroupEvent();

return instance;
_indexField.SetValue(boxed, index);
_prevStateField.SetValue(boxed, prevStateByte);
_thisStateField.SetValue(boxed, thisStateByte);

return (CullingGroupEvent)boxed;
}

private static byte AsVisibility(bool isVisible)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public void CanSetNormalViaBoxing()
Assert.AreNotEqual(normal, new Vector2());
}

#if !ENABLE_IL2CPP
[Test]
public void CanSetNormalViaReference()
{
Expand All @@ -95,5 +96,6 @@ public void CanSetNormalViaReference()
Assert.AreEqual(normal, value.normal);
Assert.AreNotEqual(normal, new Vector2());
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public void CanSetStateViaBoxing()
Assert.AreEqual(newValue, field.GetValue(boxed));
}

#if !ENABLE_IL2CPP
[Test]
public void CanSetStateViaReference()
{
Expand All @@ -97,5 +98,6 @@ public void CanSetStateViaReference()
// Assert
Assert.AreEqual(newValue, field.GetValue(value));
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,18 @@ protected override CullingGroupEvent CreateInstanceFromValues(ValuesArray<object
byte currentDistance = values.GetAsTypeOrDefault<byte>(3);
byte previousDistance = values.GetAsTypeOrDefault<byte>(4);

int thisStateByte = (byte)(isVisibleByte | (currentDistance & DISTANCE_MASK));
int prevStateByte = (byte)(wasVisibleByte | (previousDistance & DISTANCE_MASK));
byte thisStateByte = (byte)(isVisibleByte | (currentDistance & DISTANCE_MASK));
byte prevStateByte = (byte)(wasVisibleByte | (previousDistance & DISTANCE_MASK));

#if ENABLE_IL2CPP
object boxed = new CullingGroupEvent();

_indexField.SetValue(boxed, index);
_prevStateField.SetValue(boxed, prevStateByte);
_thisStateField.SetValue(boxed, thisStateByte);

return (CullingGroupEvent)boxed;
#else
var instance = new CullingGroupEvent();
TypedReference reference = __makeref(instance);

Expand All @@ -57,6 +66,7 @@ protected override CullingGroupEvent CreateInstanceFromValues(ValuesArray<object
_thisStateField.SetValueDirect(reference, thisStateByte);

return instance;
#endif
}

protected override object[] ReadInstanceValues(CullingGroupEvent instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,21 @@ protected override ColliderDistance2D CreateInstanceFromValues(ValuesArray<objec
throw new JsonException("Failed to set value for 'm_Normal' field on UnityEngine.ColliderDistance2D type.");
}

TypedReference reference = __makeref(instance);
Vector2 normal = values.GetAsTypeOrDefault<Vector2>(2);

#if ENABLE_IL2CPP
object boxed = instance;

_normalField.SetValue(boxed, normal);

return (ColliderDistance2D)boxed;
#else
TypedReference reference = __makeref(instance);

_normalField.SetValueDirect(reference, normal);

return instance;
#endif
}

protected override object[] ReadInstanceValues(ColliderDistance2D instance)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,21 @@ protected override RandomState CreateInstanceFromValues(ValuesArray<int> values)
{
var state = new RandomState();

#if ENABLE_IL2CPP
object boxed = state;
_s0Field.SetValue(boxed, values[0]);
_s1Field.SetValue(boxed, values[1]);
_s2Field.SetValue(boxed, values[2]);
_s3Field.SetValue(boxed, values[3]);
return (RandomState)boxed;
#else
TypedReference reference = __makeref(state);
_s0Field.SetValueDirect(reference, values[0]);
_s1Field.SetValueDirect(reference, values[1]);
_s2Field.SetValueDirect(reference, values[2]);
_s3Field.SetValueDirect(reference, values[3]);

return state;
#endif
}

protected override int[] ReadInstanceValues(RandomState instance)
Expand Down

0 comments on commit 2d4dc85

Please sign in to comment.