Skip to content

Commit 0c4aa44

Browse files
committed
Make NaN floats / doubles never equal to each other for the ProtoFlux == and != nodes, as well as the ValueEqualityDriver component
Adds a fix for Yellow-Dog-Man/Resonite-Issues#1046
1 parent d9fd996 commit 0c4aa44

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed

CommunityBugFixCollection/Locale/de.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"CommunityBugFixCollection.ImportMultipleAudioFiles.Description": "Macht es möglich mehrere Audiodateien auf einmal zu importieren.",
2828
"CommunityBugFixCollection.ImportWebFilesAsUrls.Description": "Sorgt dafür, dass URLs zu Textdateien oder Resonite Packages nicht importiert werden, statt als Hyperlink aufzutauchen.",
2929
"CommunityBugFixCollection.IndependentlyScaleDirectCursor.Description": "Verhindert, dass der direkte Cursor sehr groß wird, wenn er an einem deutlich nährem Objekt hängt als der echte Cursor.",
30+
"CommunityBugFixCollection.NaNtEqual.Description": "Sorgt dafür, dass NaN float / double Werte sich bei den == und != ProtoFlux Nodes sowie bei der ValueEqualityDriver Komponente niemals gleichen.",
3031
"CommunityBugFixCollection.NoLossOfColorProfile.Description": "Verhindert, dass Farbprofile nicht bei allen Berechnungen erhalten bleiben.",
3132
"CommunityBugFixCollection.NoZeroScaleToolRaycast.Description": "Verhinder einen Crash wenn (Multi-)Werkzeuge auf null skaliert werden.",
3233
"CommunityBugFixCollection.NodeNameAdjustments.Description": "Korrigiert, dass die Namen der meisten ProtoFlux Nodes in Strings > Constants unsichtbar sind.",

CommunityBugFixCollection/Locale/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"CommunityBugFixCollection.ImportMultipleAudioFiles.Description": "Fixes it not being possible to import multiple audio clips at once.",
2828
"CommunityBugFixCollection.ImportWebFilesAsUrls.Description": "Fixes URLs to text files or Resonite Packages failing to import instead of appearing as a hyperlink.",
2929
"CommunityBugFixCollection.IndependentlyScaleDirectCursor.Description": "Fixes direct cursor size becoming very large when snapped to an object much closer than the true cursor.",
30+
"CommunityBugFixCollection.NaNtEqual.Description": "Makes NaN floats / doubles never equal to each other for the ProtoFlux == and != nodes, as well as the ValueEqualityDriver component.",
3031
"CommunityBugFixCollection.NoLossOfColorProfile.Description": "Fixes Color Profile not being preserved on all operations.",
3132
"CommunityBugFixCollection.NoZeroScaleToolRaycast.Description": "Fixes a crash when a (multi)tool is scaled to zero.",
3233
"CommunityBugFixCollection.NodeNameAdjustments.Description": "Fixes most ProtoFlux nodes in Strings > Constants having invisible names.",
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
using Elements.Core;
2+
using FrooxEngine;
3+
using HarmonyLib;
4+
using MonkeyLoader.Resonite;
5+
using ProtoFlux.Core;
6+
using ProtoFlux.Runtimes.Execution;
7+
using ProtoFlux.Runtimes.Execution.Nodes;
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Text;
11+
12+
namespace CommunityBugFixCollection
13+
{
14+
[HarmonyPatch]
15+
[HarmonyPatchCategory(nameof(NaNtEqual))]
16+
internal sealed class NaNtEqual : ResoniteMonkey<NaNtEqual>
17+
{
18+
public override IEnumerable<string> Authors => Contributors.Banane9;
19+
20+
public override bool CanBeDisabled => true;
21+
22+
[HarmonyPrefix]
23+
[HarmonyPatch(typeof(ValueEqualityDriver<double>), nameof(ValueEqualityDriver<double>.OnChanges))]
24+
private static bool ValueEqualityDriverDoublePrefix(ValueEqualityDriver<double> __instance)
25+
{
26+
if (!Enabled)
27+
return true;
28+
29+
if (!__instance.Target.IsLinkValid)
30+
return false;
31+
32+
var value = __instance.TargetValue.Target?.Value ?? default;
33+
34+
var areEqual = (!__instance.UseApproximateComparison.Value || !Coder<double>.SupportsApproximateComparison)
35+
? value == __instance.Reference.Value
36+
: Coder<double>.Approximately(value, __instance.Reference.Value);
37+
38+
// XOR inverts the other bool
39+
__instance.Target.Target.Value = __instance.Invert.Value ^ areEqual;
40+
41+
return false;
42+
}
43+
44+
[HarmonyPrefix]
45+
[HarmonyPatch(typeof(ValueEqualityDriver<float>), nameof(ValueEqualityDriver<float>.OnChanges))]
46+
private static bool ValueEqualityDriverFloatPrefix(ValueEqualityDriver<float> __instance)
47+
{
48+
if (!Enabled)
49+
return true;
50+
51+
if (!__instance.Target.IsLinkValid)
52+
return false;
53+
54+
var value = __instance.TargetValue.Target?.Value ?? default;
55+
56+
var areEqual = (!__instance.UseApproximateComparison.Value || !Coder<float>.SupportsApproximateComparison)
57+
? value == __instance.Reference.Value
58+
: Coder<float>.Approximately(value, __instance.Reference.Value);
59+
60+
// XOR inverts the other bool
61+
__instance.Target.Target.Value = __instance.Invert.Value ^ areEqual;
62+
63+
return false;
64+
}
65+
66+
[HarmonyPrefix]
67+
[HarmonyPatch(typeof(ValueEquals<double>), nameof(ValueEquals<double>.Compute))]
68+
private static bool ValueEqualsDoublePrefix(ExecutionContext context, ref bool __result)
69+
{
70+
if (!Enabled)
71+
return true;
72+
73+
__result = 0.ReadValue<double>(context) == 1.ReadValue<double>(context);
74+
75+
return false;
76+
}
77+
78+
[HarmonyPrefix]
79+
[HarmonyPatch(typeof(ValueEquals<float>), nameof(ValueEquals<float>.Compute))]
80+
private static bool ValueEqualsFloatPrefix(ExecutionContext context, ref bool __result)
81+
{
82+
if (!Enabled)
83+
return true;
84+
85+
__result = 0.ReadValue<float>(context) == 1.ReadValue<float>(context);
86+
87+
return false;
88+
}
89+
90+
[HarmonyPrefix]
91+
[HarmonyPatch(typeof(ValueNotEquals<double>), nameof(ValueNotEquals<double>.Compute))]
92+
private static bool ValueNotEqualsDoublePrefix(ExecutionContext context, ref bool __result)
93+
{
94+
if (!Enabled)
95+
return true;
96+
97+
__result = 0.ReadValue<double>(context) != 1.ReadValue<double>(context);
98+
99+
return false;
100+
}
101+
102+
[HarmonyPrefix]
103+
[HarmonyPatch(typeof(ValueNotEquals<float>), nameof(ValueNotEquals<float>.Compute))]
104+
private static bool ValueNotEqualsFloatPrefix(ExecutionContext context, ref bool __result)
105+
{
106+
if (!Enabled)
107+
return true;
108+
109+
__result = 0.ReadValue<float>(context) != 1.ReadValue<float>(context);
110+
111+
return false;
112+
}
113+
}
114+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ just disable them in the settings in the meantime.
4343
* URLs to text files or Resonite Packages failing to import instead of appearing as a hyperlink (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/785)
4444
* References in multiple duplicated or transferred-between-worlds items breaking (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/984)
4545
* Context Menu changing size and becoming unusable with extreme FOVs (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/991)
46+
* NaN floats / doubles compare as equal in ProtoFlux and ValueEqualityDriver (but only when not approximate) (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/1046)
4647
* ColorX From HexCode (ProtoFlux node) defaults to Linear profile (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/1404)
4748
* UserInspectors not listing existing users in the session for non-host users (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/1964)
4849
* ProtoFlux value casts from byte to other values converting incorrectly (mono / graphical client only) (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/2257)

0 commit comments

Comments
 (0)