Added validation of mapgen weight #75770
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
None
Purpose of change
Fix #75741, i.e. too high mapgen weights resulting in it being ignored.
Describe the solution
Add validation of weight parameters when parsing the JSON.
Describe alternatives you've considered
Tackle validation of other JSON parsing. Scope creep...
Testing
Result of loading world created according to the bug report description:
DEBUG : Error: Json error: data/mods/desert_region/mapgen/waterbody.json:20:14: <color_white><color_cyan>min value out of bounds (0 - 20,000,000)
<color_light_red> "weight": 100000000000,
<color_cyan>▲▲▲
"object": {
"fill_ter": "t_mudcrack",
FUNCTION : load_game
FILE : C:\Cataclysm-DDA\src\main_menu.cpp
LINE : 1103
VERSION : 0.G-11455-g8bbef0c5d2-dirty
When the offending entry had its weight adjusted no report was produced and the save loaded normally.
Verified that nothing blows up with the 20 million limit (there might have been existing extreme weights).
Additional context
Note that I don't know WHY it fails. A large double number is converted to an int in the convoluted code mess, and that resulting number isn't > 0. However, preventing such numbers from being used should solve the problem.
There are lots of other places were weights are use. Consider that to be out of scope.
It's weird to parse double but then cast the values to int. Don't understand why a double is used at all.
There are lots of places were int64 are parsed and then cast to ints. Again, that's out of scope.
Decided on 20 million as the max limit to be reasonably sure you won't accidentally add yet another variant of something and then have things go weird. Note that I don't actually know how the weights are processed, but guess they're tallied up, in which case lots of large weights could push you over the limit. Anyway, 20 million should be more than enough for any real usage (trying to provide a huge number to drown out a base version is really a different issue: there's probably a need to have a way to reduce be base version's weight to 0 for mods). Again, out of scope.
Encountered the error report in debug.log after the one produced by this PR, but only when the error is reported, not when the game is loaded normally:
13:48:31.401 ERROR : C:\Cataclysm-DDA\src\flexbuffer_json.cpp:347 [error_skipped_members] (json-error)
Json error: data/json/monsters/bird.json:193:17: <color_white><color_cyan>Invalid or misplaced field name "copy-from" in JSON data
"id": "mon_chickadee_chick",
"type": "MONSTER",
<color_light_red> "copy-from": "mon_generic_chick_tiny",
<color_cyan>▲▲▲
"upgrades": { "age_grow": 7, "into": "mon_chickadee" }
},
Providing it here in case it might be a genuine error that somehow is suppressed normally. The base assumption is that this is a result of the error report disrupting the parsing somehow, though.