Fix multiplication in multiplier

This commit is contained in:
2025-02-24 08:33:13 +01:00
parent e0b9bed690
commit 13610a301a

View File

@@ -23,6 +23,24 @@ namespace TerraTech {
throw new ArgumentException(
string.Format("Field {0} does not exist on {1}", fieldName, parentTraverse));
}
// Verify TValue is a numeric type
if (!IsNumericType(typeof(TValue))) {
throw new ArgumentException(string.Format("Type {0} must be a numeric type", typeof(TValue).Name));
}
}
private static bool IsNumericType(Type type) {
return type == typeof(byte) || type == typeof(sbyte) || type == typeof(short) || type == typeof(ushort) ||
type == typeof(int) || type == typeof(uint) || type == typeof(long) || type == typeof(ulong) ||
type == typeof(float) || type == typeof(double) || type == typeof(decimal);
}
private static TValue MultiplyValues(TValue a, TValue b) {
// Convert to double for the multiplication
double result = Convert.ToDouble(a) * Convert.ToDouble(b);
// Convert back to TValue
return (TValue)Convert.ChangeType(result, typeof(TValue));
}
public TValue GetValue() {
@@ -48,9 +66,7 @@ namespace TerraTech {
public void Apply() {
try {
dynamic originalDynamic = _originalValue;
dynamic multiplierDynamic = _multiplier.Value;
var newValue = (TValue)(originalDynamic * multiplierDynamic);
var newValue = MultiplyValues(_originalValue, _multiplier.Value);
if (Main.debug.Value)
Console.WriteLine("Applying to {0}: {1} * {2} = {3}", _fieldName, _originalValue, _multiplier.Value,