-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added a check for overflowing player credits #7374
Conversation
That looks like a huge overkill to me, with regards to performance. |
I don't think there will be a large (if any) performance impact. For reference : And checking for overflows in the first place is also virtually free perfomance wise. |
I think we should fix the problem instead of just throwing when it occurs. |
Logging*, not throwing. |
The problem is "fixed" by this. If the integer overflows the value isn't saved i. e. if you gain credits beyond the limit the "transaction" is cancelled. |
Just in case we decide to go with this:
|
I agree with penev here: setting the affected variable to |
It may be worth considering the following instead of a try-catch too. if ((long)X + num > int.MaxValue)
X = int.MaxValue;
else
X += num; |
} | ||
catch (OverflowException) | ||
{ | ||
Log.Write("debug", "Player cash overflow cought."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As people have noted above:
I don't think this log is necessary, but either way cought
is spelled incorrectly.
It should also set the respective value to int.MaxValue if there is an overflow.
I also think the checked block would be better inside the try block.
Or you could add ints and check |
0bab532
to
86d0da5
Compare
86d0da5
to
4f07de2
Compare
Applied most of the suggestions made here. |
Works as advertised, 👍 |
@@ -79,8 +79,35 @@ public bool TakeResources(int num) | |||
|
|||
public void GiveCash(int num) | |||
{ | |||
Cash += num; | |||
Earned += num; | |||
if (Cash < int.MaxValue) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In what scenario is this false
?
This if
is redundant. It would probably hurt performance more that it would help.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't redundant - it prevents the (otherwise guaranteed) exception throw and catch if the limit has already been reached. The perf overhead of the if() is completely negligible.
Without the if a OverflowException would be thrown everytime you gain money after you've hit the limit. |
The variable Cash can't be over int.MaxValue already, so that check does nothing. On 2015-02-12, at 1:52 PM, DeadlySurprise notifications@github.com wrote:
|
But it can be In this case you can skip the whole check. |
The cases where it is int.MaxValue are not common enough to justify a check for it so better to just have the try blocks on cash and earned. |
@wolfgaming this needs to be proper fixed! as @phrohdoh and @ScottNZ already said in irc =/ instead of hiding Code from the Debugger! |
It does need to be fixed. But I cannot give a +1 with the current |
I’m very surprised how concerned you all are about 2 if-statements. The performance cost of simply calling GiveCash() (not executing just calling!) is more than double that of both ifs. The argument that the cash limit is only hit in extreme cases is of course true, but the performance hit is so miniscule that there is no reason imho to have real performance issues once you hit the limit. |
No, we are concerned about the unneeded clutter they provide. Nor Earned nor Cash may be greater than int.MaxValue. It isn't mathematically possible. Even if they are equal, the ratio of the performance gain to the amount of clutter added is not significant. |
My concern is the performance difference (however small) before reaching that limit. |
We should not use try & catch :( |
I'm confused by the bike shedding here - this looks fine to me. I don't have time to test this, otherwise I would merge this myself. |
@LipkeGu Only way I can see this being done without a try catch is changing it to long and then casting it to int again, and I like that worse then the try-catch. @DeadlySurprise The main problem with the ifs is that they add like 6 lines of code that are unneccesary since it is already handled by the try-catch. Also as a question has this exception ever been reached except when using a cheat command? |
Finally made time to test this, and everything checks out 👍 Thanks. |
Added a check for overflowing player credits
Fixes #7348.