Improve handling of missing game version #2444
Merged
+67
−3
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.
Problem
If you:
buildID.txt
andreadme.txt
, without removing the KSP folder or the CKAN folder... then on load, you get the compatible versions popup warning you about a version change (if in GUI), and clicking Save causes an exception:
(One way to make this happen is apparently to uninstall in Steam, then re-install to a different Steam library folder. See #2443.)
The problem is in Core, so the other UIs are also affected, but the details vary.
Causes
KSP.Version()
is allowed to be null in most places (and it happens when the buildID.txt and readme.txt files are missing), but the code to load and save compatible versions doesn't like it. (This is somewhat ironic as that code would be one of the better ways of handling this situation, since you could use it to set the correct game version manually.)For saving,
ToString()
is called on the null reference.For loading, if
compatible_ksp_versions.json
has a null value for theVersionOfKspWhenWritten
property, it is passed toKspVersion.Parse
, which throws exceptions on null. (That property can't be null currently, but it will be possible once we allow the save to proceed with a null.)To be considered invalid, a game instance must lack a GameData folder. If it merely lacks the files that provide the game version, this is treated as fine; you can attempt to open and use such an instance even though we know it's missing at least some data that should be there.
Changes
Now the null conditional operator ("
?.
") is used to avoid a null reference exception at save. This allows a null value to be written to the file.Now we use
KspVersion.TryParse
instead ofParse
to allowVersionOfKspWhenCompatibleVersionsWereStored
to be set to null at load.Finally, a game instance without a version is now treated as invalid. This means you can't open them from the Select KSP Install window, and they won't be auto-opened at launch. If your only known instance loses its version info (say, by uninstalling in Steam), then it'll automatically open the Select KSP Install window instead.
Fixes #2443.