Skip to content
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

Fix ID collisions on save and scenario loading #533

Merged
merged 3 commits into from
Feb 7, 2025

Conversation

esbudylin
Copy link
Contributor

This PR fixes ID collisions when loading existing game files by introducing a new constructor to the ID Factory class. The constructor accepts a SaveGame instance and tracks the highest ID count per ID type.

Currently, this is implemented only for units and cities, as they appear to be the only entity types created during gameplay. In contrast, civilizations and technologies are assigned IDs only at the start of the game, so tracking them when loading a save file is unnecessary.

The PR includes unit tests for the ID Factory.

Implement logic to track the highest ID count per ID type. Now, when
loading a save file with units "worker-1" and "worker-2", the ID
generator will start producing "worker" IDs from 3.
No need to pass a SaveGame the ID.Factory here, as SaveGame is empty.
// "worker": 3,
// }
public Factory(SaveGame save) {
IEnumerable<IHasID> entities = new List<IEnumerable<IHasID>>() {save.Units, save.Cities}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the motivation of only doing this for units and cities, but I suspect it may be simpler and less error prone to have a way to get the ID factory used to create the SaveGame, and copy the internal state from that factory instead of iterating over the SaveGame contents themselves

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you elaborate? I don't see how it would be viable for loading a JSON save that already has a set of predefined IDs.

Copy link
Contributor

@TomWerner TomWerner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a reasonable approach to fixing this problem, thank you for adding tests!

I'm going to go ahead and merge this, if pcen@ has followup thoughts they can always be addressed in future PRs.

@TomWerner TomWerner merged commit 0fb23f0 into Development Feb 7, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants