Move auto-saving settings to mod code.

This commit is contained in:
Paul Chote
2025-12-11 11:18:43 +00:00
committed by Gustas Kažukauskas
parent 98f7fe0db7
commit 7e9d87a808
3 changed files with 32 additions and 39 deletions

View File

@@ -300,15 +300,6 @@ namespace OpenRA
public ImmutableArray<Color> CustomColors = [];
}
[YamlNode("SinglePlayerSettings", shared: true)]
public class SinglePlayerGameSettings : SettingsModule
{
[Desc("Sets the Auto-save frequency, in seconds")]
public int AutoSaveInterval = 0;
[Desc("Sets the AutoSave number of max files to bes saved on the file-system")]
public int AutoSaveMaxFileCount = 10;
}
[YamlNode("Game", shared: true)]
public class GameSettings : SettingsModule
{
@@ -382,7 +373,6 @@ namespace OpenRA
public readonly GraphicSettings Graphics;
public readonly ServerSettings Server;
public readonly DebugSettings Debug;
public readonly SinglePlayerGameSettings SinglePlayerSettings;
readonly Arguments args;
readonly TypeDictionary modules = [];
@@ -407,7 +397,6 @@ namespace OpenRA
Graphics = GetOrCreate<GraphicSettings>(null);
Server = GetOrCreate<ServerSettings>(null);
Debug = GetOrCreate<DebugSettings>(null);
SinglePlayerSettings = GetOrCreate<SinglePlayerGameSettings>(null);
}
public T GetOrCreate<T>(ObjectCreator objectCreator, string mod = null) where T : SettingsModule

View File

@@ -18,6 +18,16 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[YamlNode("AutoSave", shared: true)]
public class AutoSaveSettings : SettingsModule
{
[Desc("Sets the Auto-save frequency, in seconds")]
public int AutoSaveInterval = 0;
[Desc("Sets the AutoSave number of max files to bes saved on the file-system")]
public int AutoSaveMaxFileCount = 10;
}
[TraitLocation(SystemActors.World)]
[Desc("Add this trait to the world actor to enable auto-save.")]
public class AutoSaveInfo : TraitInfo
@@ -32,11 +42,13 @@ namespace OpenRA.Mods.Common.Traits
int ticksUntilAutoSave;
int lastSaveInverval;
readonly bool isDisabled;
readonly AutoSaveSettings autoSaveSettings;
public AutoSave(Actor self, AutoSaveInfo info)
{
autoSaveSettings = self.World.GetSettings<AutoSaveSettings>();
ticksUntilAutoSave = GetTicksBetweenAutosaves(self);
lastSaveInverval = Game.Settings.SinglePlayerSettings.AutoSaveInterval;
lastSaveInverval = autoSaveSettings.AutoSaveInterval;
isDisabled = self.World.LobbyInfo.GlobalSettings.Dedicated || self.World.LobbyInfo.NonBotClients.Count() > 1;
}
@@ -46,18 +58,13 @@ namespace OpenRA.Mods.Common.Traits
if (isDisabled || self.World.IsReplay || self.World.IsLoadingGameSave)
return;
var autoSaveValue = Game.Settings.SinglePlayerSettings.AutoSaveInterval;
if (autoSaveValue == 0)
if (autoSaveSettings.AutoSaveInterval == 0)
return;
var autoSaveFileLimit = Game.Settings.SinglePlayerSettings.AutoSaveMaxFileCount;
autoSaveFileLimit = autoSaveFileLimit < 3 ? 3 : autoSaveFileLimit;
if (lastSaveInverval != autoSaveValue)
var autoSaveFileLimit = Math.Max(autoSaveSettings.AutoSaveMaxFileCount, 3);
if (lastSaveInverval != autoSaveSettings.AutoSaveInterval)
{
lastSaveInverval = autoSaveValue;
lastSaveInverval = autoSaveSettings.AutoSaveInterval;
ticksUntilAutoSave = GetTicksBetweenAutosaves(self);
}
@@ -91,9 +98,9 @@ namespace OpenRA.Mods.Common.Traits
return autoSaveDirectoryInfo.EnumerateFiles($"{AutoSavePattern}*{SaveFileExtension}");
}
static int GetTicksBetweenAutosaves(Actor self)
int GetTicksBetweenAutosaves(Actor self)
{
return 1000 / self.World.Timestep * Game.Settings.SinglePlayerSettings.AutoSaveInterval;
return 1000 / self.World.Timestep * autoSaveSettings.AutoSaveInterval;
}
}
}

View File

@@ -11,6 +11,7 @@
using System;
using System.Collections.Generic;
using OpenRA.Mods.Common.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
@@ -31,10 +32,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic
readonly int[] autoSaveSeconds = [0, 10, 30, 45, 60, 120, 180, 300, 600];
readonly int[] autoSaveFileNumbers = [3, 5, 10, 20, 50, 100];
readonly AutoSaveSettings autoSaveSettings;
[ObjectCreator.UseCtor]
public GameplaySettingsLogic(Action<string, string, Func<Widget, Func<bool>>, Func<Widget, Action>> registerPanel, string panelID, string label)
public GameplaySettingsLogic(ModData modData, Action<string, string, Func<Widget, Func<bool>>, Func<Widget, Action>> registerPanel,
string panelID, string label)
{
autoSaveSettings = modData.GetSettings<AutoSaveSettings>();
registerPanel(panelID, label, InitPanel, ResetPanel);
}
@@ -49,17 +53,14 @@ namespace OpenRA.Mods.Common.Widgets.Logic
autoSaveIntervalDropDown.OnClick = () =>
ShowAutoSaveIntervalDropdown(autoSaveIntervalDropDown, autoSaveSeconds);
autoSaveIntervalDropDown.GetText = () => GetMessageForAutoSaveInterval(Game.Settings.SinglePlayerSettings.AutoSaveInterval);
autoSaveIntervalDropDown.GetText = () => GetMessageForAutoSaveInterval(autoSaveSettings.AutoSaveInterval);
// Setup dropdown for auto-save number.
var autoSaveNoDropDown = panel.Get<DropDownButtonWidget>("AUTO_SAVE_FILE_NUMBER_DROP_DOWN");
autoSaveNoDropDown.OnMouseDown = _ =>
ShowAutoSaveFileNumberDropdown(autoSaveNoDropDown, autoSaveFileNumbers);
autoSaveNoDropDown.GetText = () => FluentProvider.GetMessage(AutoSaveMaxFileNumber, "saves", Game.Settings.SinglePlayerSettings.AutoSaveMaxFileCount);
autoSaveNoDropDown.IsDisabled = () => Game.Settings.SinglePlayerSettings.AutoSaveInterval <= 0;
autoSaveNoDropDown.OnMouseDown = _ => ShowAutoSaveFileNumberDropdown(autoSaveNoDropDown, autoSaveFileNumbers);
autoSaveNoDropDown.GetText = () => FluentProvider.GetMessage(AutoSaveMaxFileNumber, "saves", autoSaveSettings.AutoSaveMaxFileCount);
autoSaveNoDropDown.IsDisabled = () => autoSaveSettings.AutoSaveInterval <= 0;
return () => false;
}
@@ -71,15 +72,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic
void ShowAutoSaveIntervalDropdown(DropDownButtonWidget dropdown, IEnumerable<int> options)
{
var gsp = Game.Settings.SinglePlayerSettings;
ScrollItemWidget SetupItem(int o, ScrollItemWidget itemTemplate)
{
var item = ScrollItemWidget.Setup(itemTemplate,
() => gsp.AutoSaveInterval == o,
() => autoSaveSettings.AutoSaveInterval == o,
() =>
{
gsp.AutoSaveInterval = o;
autoSaveSettings.AutoSaveInterval = o;
Game.Settings.Save();
});
@@ -94,15 +93,13 @@ namespace OpenRA.Mods.Common.Widgets.Logic
void ShowAutoSaveFileNumberDropdown(DropDownButtonWidget dropdown, IEnumerable<int> options)
{
var gsp = Game.Settings.SinglePlayerSettings;
ScrollItemWidget SetupItem(int o, ScrollItemWidget itemTemplate)
{
var item = ScrollItemWidget.Setup(itemTemplate,
() => gsp.AutoSaveMaxFileCount == o,
() => autoSaveSettings.AutoSaveMaxFileCount == o,
() =>
{
gsp.AutoSaveMaxFileCount = o;
autoSaveSettings.AutoSaveMaxFileCount = o;
Game.Settings.Save();
});