Skip to content

Commit

Permalink
Fix: store autosave interval setting in minutes too
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueBrain committed Jul 17, 2023
1 parent 9c262b0 commit b2c5145
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 33 deletions.
11 changes: 1 addition & 10 deletions src/openttd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,6 @@ bool _save_config = false;
bool _request_newgrf_scan = false;
NewGRFScanCallback *_request_newgrf_scan_callback = nullptr;

/** Available settings for autosave intervals. */
static const std::chrono::milliseconds _autosave_ticks[] = {
std::chrono::minutes::zero(), ///< never
std::chrono::minutes(10),
std::chrono::minutes(30),
std::chrono::minutes(60),
std::chrono::minutes(120),
};

/**
* Error handling for fatal user errors.
* @param str the string to print.
Expand Down Expand Up @@ -1458,7 +1449,7 @@ static IntervalTimer<TimerGameRealtime> _autosave_interval({std::chrono::millise
*/
void ChangeAutosaveFrequency(bool reset)
{
_autosave_interval.SetInterval({_autosave_ticks[_settings_client.gui.autosave], TimerGameRealtime::AUTOSAVE}, reset);
_autosave_interval.SetInterval({_settings_client.gui.autosave_interval, TimerGameRealtime::AUTOSAVE}, reset);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions src/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ enum IniFileVersion : uint32 {
IFV_LINKGRAPH_SECONDS, ///< 3 PR#10610 Store linkgraph update intervals in seconds instead of days.
IFV_NETWORK_PRIVATE_SETTINGS, ///< 4 PR#10762 Move no_http_content_downloads / use_relay_service to private settings.

IFV_AUTOSAVE_RENAME, ///< 5 PR#11143 Renamed values of autosave to be in minutes.

IFV_MAX_VERSION, ///< Highest possible ini-file version.
};

Expand Down Expand Up @@ -1262,6 +1264,27 @@ void LoadFromConfig(bool startup)
}
}

if (generic_version < IFV_AUTOSAVE_RENAME) {
IniGroup *gui = generic_ini.GetGroup("gui", false);
if (gui != nullptr) {
IniItem *autosave = gui->GetItem("autosave");
if (autosave != nullptr) {
/* Convert the old settings that were in game-time to something comparable in real-time. */
static std::vector<std::string> _old_autosave_interval{"off", "monthly", "quarterly", "half year", "yearly"};
auto old_value = OneOfManySettingDesc::ParseSingleValue(autosave->value->c_str(), autosave->value->size(), _old_autosave_interval);

switch (old_value) {
case 0: _settings_client.gui.autosave_interval = std::chrono::minutes::zero(); break;
case 1: _settings_client.gui.autosave_interval = std::chrono::minutes(10); break;
case 2: _settings_client.gui.autosave_interval = std::chrono::minutes(30); break;
case 3: _settings_client.gui.autosave_interval = std::chrono::minutes(60); break;
case 4: _settings_client.gui.autosave_interval = std::chrono::minutes(120); break;
default: break;
}
}
}
}

_grfconfig_newgame = GRFLoadConfig(generic_ini, "newgrf", false);
_grfconfig_static = GRFLoadConfig(generic_ini, "newgrf-static", true);
AILoadConfig(generic_ini, "ai_players");
Expand Down
31 changes: 27 additions & 4 deletions src/settings_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ static const StringID _autosave_dropdown[] = {
INVALID_STRING_ID,
};

/** Available settings for autosave intervals. */
static const std::chrono::minutes _autosave_dropdown_to_minutes[] = {
std::chrono::minutes::zero(), ///< never
std::chrono::minutes(10),
std::chrono::minutes(30),
std::chrono::minutes(60),
std::chrono::minutes(120),
};

static Dimension _circle_size; ///< Dimension of the circle +/- icon. This is here as not all users are within the class of the settings window.

static const void *ResolveObject(const GameSettings *settings_ptr, const IntSettingDesc *sd);
Expand Down Expand Up @@ -220,7 +229,13 @@ struct GameOptionsWindow : Window {
}

case WID_GO_AUTOSAVE_DROPDOWN: { // Setup autosave dropdown
*selected_index = _settings_client.gui.autosave;
int i = 0;
for (auto &minutes : _autosave_dropdown_to_minutes) {
i++;
if (_settings_client.gui.autosave_interval <= minutes) break;
}
*selected_index = i - 1;

const StringID *items = _autosave_dropdown;
for (uint i = 0; *items != INVALID_STRING_ID; items++, i++) {

Check notice

Code scanning / CodeQL

Declaration hides variable Note

Variable i hides another variable of the same name (on
line 232
).
list.emplace_back(new DropDownListStringItem(*items, i, false));
Expand Down Expand Up @@ -301,7 +316,15 @@ struct GameOptionsWindow : Window {
}
break;
}
case WID_GO_AUTOSAVE_DROPDOWN: SetDParam(0, _autosave_dropdown[_settings_client.gui.autosave]); break;
case WID_GO_AUTOSAVE_DROPDOWN: {
int i = 0;
for (auto &minutes : _autosave_dropdown_to_minutes) {
i++;
if (_settings_client.gui.autosave_interval <= minutes) break;
}
SetDParam(0, _autosave_dropdown[i - 1]);
break;
}
case WID_GO_LANG_DROPDOWN: SetDParamStr(0, _current_language->own_name); break;
case WID_GO_BASE_GRF_DROPDOWN: SetDParamStr(0, BaseGraphics::GetUsedSet()->name); break;
case WID_GO_BASE_GRF_STATUS: SetDParam(0, BaseGraphics::GetUsedSet()->GetNumInvalid()); break;
Expand Down Expand Up @@ -655,7 +678,7 @@ struct GameOptionsWindow : Window {
break;

case WID_GO_AUTOSAVE_DROPDOWN: // Autosave options
_settings_client.gui.autosave = index;
_settings_client.gui.autosave_interval = _autosave_dropdown_to_minutes[index];
ChangeAutosaveFrequency(false);
this->SetDirty();
break;
Expand Down Expand Up @@ -1791,7 +1814,7 @@ static SettingsContainer &GetSettingsTree()
}

interface->Add(new SettingEntry("gui.fast_forward_speed_limit"));
interface->Add(new SettingEntry("gui.autosave"));
interface->Add(new SettingEntry("gui.autosave_interval"));
interface->Add(new SettingEntry("gui.toolbar_pos"));
interface->Add(new SettingEntry("gui.statusbar_pos"));
interface->Add(new SettingEntry("gui.prefer_teamchat"));
Expand Down
2 changes: 1 addition & 1 deletion src/settings_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ struct GUISettings {
ZoomLevel zoom_min; ///< minimum zoom out level
ZoomLevel zoom_max; ///< maximum zoom out level
ZoomLevel sprite_zoom_min; ///< maximum zoom level at which higher-resolution alternative sprites will be used (if available) instead of scaling a lower resolution sprite
byte autosave; ///< how often should we do autosaves?
std::chrono::minutes autosave_interval; ///< how often should we do autosaves?
bool threaded_saves; ///< should we do threaded saves?
bool keep_all_autosave; ///< name the autosave in a different way
bool autosave_on_exit; ///< save an autosave when you quit the game, but do not ask "Do you really want to quit?"
Expand Down
16 changes: 7 additions & 9 deletions src/table/settings/gui_settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ static void InvalidateNewGRFChangeWindows(int32 new_value);
static void ZoomMinMaxChanged(int32 new_value);
static void SpriteZoomMinChanged(int32 new_value);

static constexpr std::initializer_list<const char*> _autosave_interval{"off", "monthly", "quarterly", "half year", "yearly"};
static constexpr std::initializer_list<const char*> _osk_activation{"disabled", "double", "single", "immediately"};
static constexpr std::initializer_list<const char*> _savegame_date{"long", "short", "iso"};

Expand Down Expand Up @@ -48,16 +47,15 @@ extra = 0
startup = false


[SDTC_OMANY]
var = gui.autosave
type = SLE_UINT8
flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC | SF_GUI_DROPDOWN
def = 1
max = 4
full = _autosave_interval
[SDTC_VAR]
var = gui.autosave_interval
type = SLE_UINT32
flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC
def = 10
min = 0
max = 1440
str = STR_CONFIG_SETTING_AUTOSAVE
strhelp = STR_CONFIG_SETTING_AUTOSAVE_HELPTEXT
strval = STR_GAME_OPTIONS_AUTOSAVE_DROPDOWN_OFF
cat = SC_BASIC

[SDTC_BOOL]
Expand Down
9 changes: 0 additions & 9 deletions src/table/settings/old_gameopt_settings.ini
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,6 @@ min = MIN_SNOWLINE_HEIGHT * TILE_HEIGHT
max = UINT8_MAX
to = SLV_22

[SDTC_OMANY]
var = gui.autosave
type = SLE_UINT8
flags = SF_NOT_IN_SAVE | SF_NO_NETWORK_SYNC
def = 1
max = 4
full = _autosave_interval
cat = SC_BASIC

[SDT_OMANY]
var = vehicle.road_side
type = SLE_UINT8
Expand Down

0 comments on commit b2c5145

Please sign in to comment.