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

Feature: Persistant rotation of numbered autosave after restart #9397

Merged
merged 1 commit into from Jul 17, 2021

Conversation

glx22
Copy link
Contributor

@glx22 glx22 commented Jun 24, 2021

Fixes #9396

Motivation / Problem

Autosave numbering always starts from 0 when OpenTTD is started.

Description

Retrieve the number of the most recent autosave and use it as a base for the next one.

Limitations

If more than one OpenTTD instance is running, and if they share the same autosave directory, they will still overwrite each other savegames.

Checklist for review

Some things are not automated, and forgotten often. This list is a reminder for the reviewers.

  • The bug fix is important enough to be backported? (label: 'backport requested')
  • This PR affects the save game format? (label 'savegame upgrade')
  • This PR affects the GS/AI API? (label 'needs review: Script API')
    • ai_changelog.hpp, gs_changelog.hpp need updating.
    • The compatibility wrappers (compat_*.nut) need updating.
  • This PR affects the NewGRF API? (label 'needs review: NewGRF')

@Taschi120
Copy link
Contributor

Note that this PR and PR #9395 have conflicts.


std::string_view name = list.begin()->title;
std::from_chars(name.data() + this->prefix.size(), name.data() + name.size(), this->number);
}
Copy link
Member

Choose a reason for hiding this comment

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

This is a lot of code for in a ctor. Somehow that triggers me. But I do not have any argument for it :P So it might be bullocks. Opinions? :D

Copy link
Contributor

Choose a reason for hiding this comment

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

Well, the thing has to be initialized somewhere and where else should it be, if not in the constructor? And the code seems fairly specific to this one particular problem, so it's not like refactoring anything into separate functions would have much of an advantage.

static fios_getlist_callback_proc *proc = [](SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last) {
if (strcasecmp(ext, ".sav") == 0 && StrStartsWith(file, _prefix)) return FIOS_TYPE_FILE;
return FIOS_TYPE_INVALID;
};
Copy link
Member

Choose a reason for hiding this comment

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

Suggestions to make it a bit more clear what _prefix is doing and that it is static:

	static std::optional<std::string> _autosave_path;
	if (!_autosave_path) _autosave_path = FioFindDirectory(AUTOSAVE_DIR);

	static std::string _prefix; ///< Static as the lambda needs access to it.

	/* Callback for FiosFileScanner. */
	static fios_getlist_callback_proc *proc = [](SaveLoadOperation fop, const std::string &file, const char *ext, char *title, const char *last) {
		if (strcasecmp(ext, ".sav") == 0 && StrStartsWith(file, _prefix)) return FIOS_TYPE_FILE;
		return FIOS_TYPE_INVALID;
	};

	/* Prefix to check in the callback. */
	_prefix = *_autosave_path + this->prefix;

? Dunno :)

It was always starting from 0 on openttd restart.
Now the most recent autosave number will be used as a base to generate the next filename.
@glx22
Copy link
Contributor Author

glx22 commented Jul 9, 2021

Function names are not the best.

@glx22 glx22 merged commit 460991e into OpenTTD:master Jul 17, 2021
@glx22 glx22 deleted the autosave branch July 17, 2021 10:48
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.

Rotation of numbered autosaves is confusing
3 participants