Skip to content

Commit 3ee4a5c

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 4b9faa5 commit 3ee4a5c

4 files changed

Lines changed: 118 additions & 1 deletion

File tree

CommunityBugFixCollection/Locale/de.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"CommunityBugFixCollection.ImportWebFilesAsUrls.Description": "Sorgt dafür, dass URLs zu Textdateien oder Resonite Packages nicht importiert werden, statt als Hyperlink aufzutauchen.",
3131
"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.",
3232
"CommunityBugFixCollection.LongerWorldLoadingFailIndication.Description": "Lässt den Welt-Ladefortschritts-Indikator frühestens nach 20s verschwinden, falls der Vorgang fehlgeschlagen ist.",
33+
"CommunityBugFixCollection.NaNtEqual.Description": "Sorgt dafür, dass NaN float / double Werte sich bei den == und != ProtoFlux Nodes sowie bei der ValueEqualityDriver Komponente niemals gleichen.",
3334
"CommunityBugFixCollection.NoLossOfColorProfile.Description": "Verhindert, dass Farbprofile nicht bei allen Berechnungen erhalten bleiben.",
3435
"CommunityBugFixCollection.NoZeroScaleToolRaycast.Description": "Verhinder einen Crash wenn (Multi-)Werkzeuge auf null skaliert werden.",
3536
"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
@@ -30,6 +30,7 @@
3030
"CommunityBugFixCollection.ImportWebFilesAsUrls.Description": "Fixes URLs to text files or Resonite Packages failing to import instead of appearing as a hyperlink.",
3131
"CommunityBugFixCollection.IndependentlyScaleDirectCursor.Description": "Fixes direct cursor size becoming very large when snapped to an object much closer than the true cursor.",
3232
"CommunityBugFixCollection.LongerWorldLoadingFailIndication.Description": "Only lets the World Load Progress Indicator disappear after 20s or more if the process failed.",
33+
"CommunityBugFixCollection.NaNtEqual.Description": "Makes NaN floats / doubles never equal to each other for the ProtoFlux == and != nodes, as well as the ValueEqualityDriver component.",
3334
"CommunityBugFixCollection.NoLossOfColorProfile.Description": "Fixes Color Profile not being preserved on all operations.",
3435
"CommunityBugFixCollection.NoZeroScaleToolRaycast.Description": "Fixes a crash when a (multi)tool is scaled to zero.",
3536
"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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,10 @@ just disable them in the settings in the meantime.
4444
* The NoDestroyUndo component not preventing undoing the destruction of something (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/979)
4545
* References in multiple duplicated or transferred-between-worlds items breaking (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/984)
4646
* Context Menu changing size and becoming unusable with extreme FOVs (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/991)
47-
* World Load Progress Indicator disappearing too quickly on fail (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/1019)
4847
* Hyperlink components only opening http(s) links (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/1018)
48+
* World Load Progress Indicator disappearing too quickly on fail (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/1019)
4949
* Context Menu label outline not fading out like everything else (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/1027)
50+
* 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)
5051
* ColorX From HexCode (ProtoFlux node) defaults to Linear profile (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/1404)
5152
* UserInspectors not listing existing users in the session for non-host users (https://github.com/Yellow-Dog-Man/Resonite-Issues/issues/1964)
5253
* 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)