Added invalid value checks to PhysicsBody property setters & item.SetTransform, checking for excessively large values
This commit is contained in:
@@ -532,6 +532,20 @@ namespace Barotrauma
|
|||||||
|
|
||||||
public void SetTransform(Vector2 simPosition, float rotation, bool findNewHull = true)
|
public void SetTransform(Vector2 simPosition, float rotation, bool findNewHull = true)
|
||||||
{
|
{
|
||||||
|
if (!MathUtils.IsValid(simPosition))
|
||||||
|
{
|
||||||
|
string errorMsg =
|
||||||
|
"Attempted to move the item " + Name +
|
||||||
|
" to an invalid position (" + simPosition + ")\n" + Environment.StackTrace;
|
||||||
|
|
||||||
|
DebugConsole.ThrowError(errorMsg);
|
||||||
|
GameAnalyticsManager.AddErrorEventOnce(
|
||||||
|
"Item.SetPosition:InvalidPosition" + ID,
|
||||||
|
GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
|
||||||
|
errorMsg);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (body != null)
|
if (body != null)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
|||||||
@@ -125,8 +125,7 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!MathUtils.IsValid((Vector2)value)) return;
|
if (!IsValidValue(value.Value, "target position", -1e5f, 1e5f)) return;
|
||||||
|
|
||||||
targetPosition = new Vector2(
|
targetPosition = new Vector2(
|
||||||
MathHelper.Clamp(((Vector2)value).X, -10000.0f, 10000.0f),
|
MathHelper.Clamp(((Vector2)value).X, -10000.0f, 10000.0f),
|
||||||
MathHelper.Clamp(((Vector2)value).Y, -10000.0f, 10000.0f));
|
MathHelper.Clamp(((Vector2)value).Y, -10000.0f, 10000.0f));
|
||||||
@@ -145,7 +144,7 @@ namespace Barotrauma
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!MathUtils.IsValid((float)value)) return;
|
if (!IsValidValue(value.Value, "target rotation")) return;
|
||||||
targetRotation = value;
|
targetRotation = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -223,13 +222,21 @@ namespace Barotrauma
|
|||||||
public Vector2 LinearVelocity
|
public Vector2 LinearVelocity
|
||||||
{
|
{
|
||||||
get { return body.LinearVelocity; }
|
get { return body.LinearVelocity; }
|
||||||
set { body.LinearVelocity = value; }
|
set
|
||||||
|
{
|
||||||
|
if (!IsValidValue(value, "velocity", -1000.0f, 1000.0f)) return;
|
||||||
|
body.LinearVelocity = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public float AngularVelocity
|
public float AngularVelocity
|
||||||
{
|
{
|
||||||
get { return body.AngularVelocity; }
|
get { return body.AngularVelocity; }
|
||||||
set { body.AngularVelocity = value; }
|
set
|
||||||
|
{
|
||||||
|
if (!IsValidValue(value, "angular velocity")) return;
|
||||||
|
body.AngularVelocity = value;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public float Mass
|
public float Mass
|
||||||
@@ -360,18 +367,21 @@ namespace Barotrauma
|
|||||||
this.radius = radius;
|
this.radius = radius;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsValidValue(float value, string valueName)
|
public bool IsValidValue(float value, string valueName, float? minValue = null, float? maxValue = null)
|
||||||
{
|
{
|
||||||
if (!MathUtils.IsValid(value))
|
if (!MathUtils.IsValid(value) ||
|
||||||
|
(minValue.HasValue && value < minValue.Value) ||
|
||||||
|
(maxValue.HasValue && value > maxValue.Value))
|
||||||
{
|
{
|
||||||
|
string userData = UserData == null ? "null" : UserData.ToString();
|
||||||
string errorMsg =
|
string errorMsg =
|
||||||
"Attempted to apply invalid " + valueName +
|
"Attempted to apply invalid " + valueName +
|
||||||
" to a physics body (userdata: " + UserData == null ? "null" : UserData.ToString() +
|
" to a physics body (userdata: " + userData +
|
||||||
"), value: " + value + "\n" + Environment.StackTrace;
|
"), value: " + value + "\n" + Environment.StackTrace;
|
||||||
|
|
||||||
DebugConsole.ThrowError(errorMsg);
|
DebugConsole.ThrowError(errorMsg);
|
||||||
GameAnalyticsManager.AddErrorEventOnce(
|
GameAnalyticsManager.AddErrorEventOnce(
|
||||||
"PhysicsBody.SetPosition:InvalidPosition",
|
"PhysicsBody.SetPosition:InvalidPosition" + userData,
|
||||||
GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
|
GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
|
||||||
errorMsg);
|
errorMsg);
|
||||||
return false;
|
return false;
|
||||||
@@ -379,18 +389,21 @@ namespace Barotrauma
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool IsValidValue(Vector2 value, string valueName)
|
private bool IsValidValue(Vector2 value, string valueName, float? minValue = null, float? maxValue = null)
|
||||||
{
|
{
|
||||||
if (!MathUtils.IsValid(value))
|
if (!MathUtils.IsValid(value) ||
|
||||||
|
(minValue.HasValue && (value.X < minValue.Value || value.Y < minValue.Value)) ||
|
||||||
|
(maxValue.HasValue && (value.X > maxValue.Value || value.Y > maxValue)))
|
||||||
{
|
{
|
||||||
|
string userData = UserData == null ? "null" : UserData.ToString();
|
||||||
string errorMsg =
|
string errorMsg =
|
||||||
"Attempted to apply invalid " + valueName +
|
"Attempted to apply invalid " + valueName +
|
||||||
" to a physics body (userdata: " + UserData == null ? "null" : UserData.ToString() +
|
" to a physics body (userdata: " + userData +
|
||||||
"), value: " + value + "\n" + Environment.StackTrace;
|
"), value: " + value + "\n" + Environment.StackTrace;
|
||||||
|
|
||||||
DebugConsole.ThrowError(errorMsg);
|
DebugConsole.ThrowError(errorMsg);
|
||||||
GameAnalyticsManager.AddErrorEventOnce(
|
GameAnalyticsManager.AddErrorEventOnce(
|
||||||
"PhysicsBody.SetPosition:InvalidPosition",
|
"PhysicsBody.SetPosition:InvalidPosition" + userData,
|
||||||
GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
|
GameAnalyticsSDK.Net.EGAErrorSeverity.Error,
|
||||||
errorMsg);
|
errorMsg);
|
||||||
return false;
|
return false;
|
||||||
@@ -405,7 +418,7 @@ namespace Barotrauma
|
|||||||
|
|
||||||
public void ApplyLinearImpulse(Vector2 impulse)
|
public void ApplyLinearImpulse(Vector2 impulse)
|
||||||
{
|
{
|
||||||
if (!IsValidValue(impulse, "impulse")) return;
|
if (!IsValidValue(impulse, "impulse", -1e10f, 1e10f)) return;
|
||||||
body.ApplyLinearImpulse(impulse);
|
body.ApplyLinearImpulse(impulse);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -414,7 +427,7 @@ namespace Barotrauma
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void ApplyLinearImpulse(Vector2 impulse, float maxVelocity)
|
public void ApplyLinearImpulse(Vector2 impulse, float maxVelocity)
|
||||||
{
|
{
|
||||||
if (!IsValidValue(impulse, "impulse")) return;
|
if (!IsValidValue(impulse, "impulse", -1e10f, 1e10f)) return;
|
||||||
if (!IsValidValue(maxVelocity, "max velocity")) return;
|
if (!IsValidValue(maxVelocity, "max velocity")) return;
|
||||||
|
|
||||||
float currSpeed = body.LinearVelocity.Length();
|
float currSpeed = body.LinearVelocity.Length();
|
||||||
@@ -427,19 +440,19 @@ namespace Barotrauma
|
|||||||
|
|
||||||
public void ApplyLinearImpulse(Vector2 impulse, Vector2 point)
|
public void ApplyLinearImpulse(Vector2 impulse, Vector2 point)
|
||||||
{
|
{
|
||||||
if (!IsValidValue(impulse, "impulse")) return;
|
if (!IsValidValue(impulse, "impulse", -1e10f, 1e10f)) return;
|
||||||
body.ApplyLinearImpulse(impulse, point);
|
body.ApplyLinearImpulse(impulse, point);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ApplyForce(Vector2 force)
|
public void ApplyForce(Vector2 force)
|
||||||
{
|
{
|
||||||
if (!IsValidValue(force, "force")) return;
|
if (!IsValidValue(force, "force", -1e10f, 1e10f)) return;
|
||||||
body.ApplyForce(force);
|
body.ApplyForce(force);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ApplyForce(Vector2 force, Vector2 point)
|
public void ApplyForce(Vector2 force, Vector2 point)
|
||||||
{
|
{
|
||||||
if (!IsValidValue(force, "force")) return;
|
if (!IsValidValue(force, "force", -1e10f, 1e10f)) return;
|
||||||
if (!IsValidValue(point, "point")) return;
|
if (!IsValidValue(point, "point")) return;
|
||||||
body.ApplyForce(force, point);
|
body.ApplyForce(force, point);
|
||||||
}
|
}
|
||||||
@@ -456,7 +469,7 @@ namespace Barotrauma
|
|||||||
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.X) < 1000000.0f);
|
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.X) < 1000000.0f);
|
||||||
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.Y) < 1000000.0f);
|
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.Y) < 1000000.0f);
|
||||||
|
|
||||||
if (!IsValidValue(simPosition, "position")) return;
|
if (!IsValidValue(simPosition, "position", -1e10f, 1e10f)) return;
|
||||||
if (!IsValidValue(rotation, "rotation")) return;
|
if (!IsValidValue(rotation, "rotation")) return;
|
||||||
|
|
||||||
body.SetTransform(simPosition, rotation);
|
body.SetTransform(simPosition, rotation);
|
||||||
@@ -469,7 +482,7 @@ namespace Barotrauma
|
|||||||
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.X) < 1000000.0f);
|
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.X) < 1000000.0f);
|
||||||
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.Y) < 1000000.0f);
|
System.Diagnostics.Debug.Assert(Math.Abs(simPosition.Y) < 1000000.0f);
|
||||||
|
|
||||||
if (!IsValidValue(simPosition, "position")) return;
|
if (!IsValidValue(simPosition, "position", -1e10f, 1e10f)) return;
|
||||||
if (!IsValidValue(rotation, "rotation")) return;
|
if (!IsValidValue(rotation, "rotation")) return;
|
||||||
|
|
||||||
body.SetTransformIgnoreContacts(ref simPosition, rotation);
|
body.SetTransformIgnoreContacts(ref simPosition, rotation);
|
||||||
@@ -478,7 +491,7 @@ namespace Barotrauma
|
|||||||
|
|
||||||
public void SetPrevTransform(Vector2 simPosition, float rotation)
|
public void SetPrevTransform(Vector2 simPosition, float rotation)
|
||||||
{
|
{
|
||||||
if (!IsValidValue(simPosition, "position")) return;
|
if (!IsValidValue(simPosition, "position", -1e10f, 1e10f)) return;
|
||||||
if (!IsValidValue(rotation, "rotation")) return;
|
if (!IsValidValue(rotation, "rotation")) return;
|
||||||
|
|
||||||
prevPosition = simPosition;
|
prevPosition = simPosition;
|
||||||
@@ -504,7 +517,7 @@ namespace Barotrauma
|
|||||||
{
|
{
|
||||||
if (pullPos == null) pullPos = body.Position;
|
if (pullPos == null) pullPos = body.Position;
|
||||||
|
|
||||||
if (!IsValidValue(simPosition, "position")) return;
|
if (!IsValidValue(simPosition, "position", -1e10f, 1e10f)) return;
|
||||||
if (!IsValidValue(force, "force")) return;
|
if (!IsValidValue(force, "force")) return;
|
||||||
|
|
||||||
Vector2 vel = body.LinearVelocity;
|
Vector2 vel = body.LinearVelocity;
|
||||||
|
|||||||
Reference in New Issue
Block a user