Mapping autosaves (#10966)
This commit is contained in:
@@ -1,17 +1,18 @@
|
|||||||
// ReSharper disable once RedundantUsingDirective
|
// ReSharper disable once RedundantUsingDirective
|
||||||
// Used to warn the player in big red letters in debug mode
|
// Used to warn the player in big red letters in debug mode
|
||||||
|
|
||||||
|
using System.Linq;
|
||||||
using Content.Server.Administration;
|
using Content.Server.Administration;
|
||||||
using Content.Shared.Administration;
|
using Content.Shared.Administration;
|
||||||
|
using Content.Shared.CCVar;
|
||||||
using Robust.Server.Player;
|
using Robust.Server.Player;
|
||||||
using Robust.Server.Console.Commands;
|
using Robust.Shared.Configuration;
|
||||||
using Robust.Shared.Console;
|
using Robust.Shared.Console;
|
||||||
|
using Robust.Shared.ContentPack;
|
||||||
using Robust.Shared.Map;
|
using Robust.Shared.Map;
|
||||||
using Robust.Shared.Utility;
|
using Robust.Shared.Utility;
|
||||||
using Robust.Shared.ContentPack;
|
|
||||||
using System.Linq;
|
|
||||||
|
|
||||||
namespace Content.Server.GameTicking.Commands
|
namespace Content.Server.Mapping
|
||||||
{
|
{
|
||||||
[AdminCommand(AdminFlags.Server | AdminFlags.Mapping)]
|
[AdminCommand(AdminFlags.Server | AdminFlags.Mapping)]
|
||||||
sealed class MappingCommand : IConsoleCommand
|
sealed class MappingCommand : IConsoleCommand
|
||||||
@@ -89,11 +90,17 @@ namespace Content.Server.GameTicking.Commands
|
|||||||
mapId = mapManager.NextMapId();
|
mapId = mapManager.NextMapId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
string? toLoad = null;
|
||||||
// either load a map or create a new one.
|
// either load a map or create a new one.
|
||||||
if (args.Length <= 1)
|
if (args.Length <= 1)
|
||||||
|
{
|
||||||
shell.ExecuteCommand($"addmap {mapId} false");
|
shell.ExecuteCommand($"addmap {mapId} false");
|
||||||
|
}
|
||||||
else
|
else
|
||||||
shell.ExecuteCommand($"loadmap {mapId} \"{CommandParsing.Escape(args[1])}\" 0 0 0 true");
|
{
|
||||||
|
toLoad = CommandParsing.Escape(args[1]);
|
||||||
|
shell.ExecuteCommand($"loadmap {mapId} \"{toLoad}\" 0 0 0 true");
|
||||||
|
}
|
||||||
|
|
||||||
// was the map actually created?
|
// was the map actually created?
|
||||||
if (!mapManager.MapExists(mapId))
|
if (!mapManager.MapExists(mapId))
|
||||||
@@ -109,7 +116,11 @@ namespace Content.Server.GameTicking.Commands
|
|||||||
shell.ExecuteCommand("aghost");
|
shell.ExecuteCommand("aghost");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var cfg = IoCManager.Resolve<IConfigurationManager>();
|
||||||
|
|
||||||
shell.ExecuteCommand("sudo cvar events.enabled false");
|
shell.ExecuteCommand("sudo cvar events.enabled false");
|
||||||
|
if (cfg.GetCVar(CCVars.AutosaveEnabled))
|
||||||
|
shell.ExecuteCommand($"toggleautosave {mapId} {toLoad ?? "NEWMAP"}");
|
||||||
shell.ExecuteCommand($"tp 0 0 {mapId}");
|
shell.ExecuteCommand($"tp 0 0 {mapId}");
|
||||||
shell.RemoteExecuteCommand("mappingclientsidesetup");
|
shell.RemoteExecuteCommand("mappingclientsidesetup");
|
||||||
mapManager.SetMapPaused(mapId, true);
|
mapManager.SetMapPaused(mapId, true);
|
||||||
158
Content.Server/Mapping/MappingSystem.cs
Normal file
158
Content.Server/Mapping/MappingSystem.cs
Normal file
@@ -0,0 +1,158 @@
|
|||||||
|
using System.IO;
|
||||||
|
using Content.Server.Administration;
|
||||||
|
using Content.Shared.Administration;
|
||||||
|
using Content.Shared.CCVar;
|
||||||
|
using Robust.Server.Maps;
|
||||||
|
using Robust.Shared.Configuration;
|
||||||
|
using Robust.Shared.Console;
|
||||||
|
using Robust.Shared.ContentPack;
|
||||||
|
using Robust.Shared.Map;
|
||||||
|
using Robust.Shared.Timing;
|
||||||
|
using Robust.Shared.Utility;
|
||||||
|
|
||||||
|
namespace Content.Server.Mapping;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Handles autosaving maps.
|
||||||
|
/// </summary>
|
||||||
|
public sealed class MappingSystem : EntitySystem
|
||||||
|
{
|
||||||
|
[Dependency] private readonly IConsoleHost _conHost = default!;
|
||||||
|
[Dependency] private readonly IGameTiming _timing = default!;
|
||||||
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||||
|
[Dependency] private readonly IMapLoader _mapLoader = default!;
|
||||||
|
[Dependency] private readonly IMapManager _mapManager = default!;
|
||||||
|
[Dependency] private readonly IResourceManager _resMan = default!;
|
||||||
|
|
||||||
|
// Not a comp because I don't want to deal with this getting saved onto maps ever
|
||||||
|
/// <summary>
|
||||||
|
/// map id -> next autosave timespan & original filename.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
private Dictionary<MapId, (TimeSpan next, string fileName)> _currentlyAutosaving = new();
|
||||||
|
|
||||||
|
private ISawmill _sawmill = default!;
|
||||||
|
private bool _autosaveEnabled;
|
||||||
|
|
||||||
|
public override void Initialize()
|
||||||
|
{
|
||||||
|
base.Initialize();
|
||||||
|
|
||||||
|
_conHost.RegisterCommand("toggleautosave",
|
||||||
|
"Toggles autosaving for a map.",
|
||||||
|
"autosave <map> <path if enabling>",
|
||||||
|
ToggleAutosaveCommand);
|
||||||
|
|
||||||
|
_sawmill = Logger.GetSawmill("autosave");
|
||||||
|
_cfg.OnValueChanged(CCVars.AutosaveEnabled, SetAutosaveEnabled, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Shutdown()
|
||||||
|
{
|
||||||
|
base.Shutdown();
|
||||||
|
|
||||||
|
_cfg.UnsubValueChanged(CCVars.AutosaveEnabled, SetAutosaveEnabled);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void SetAutosaveEnabled(bool b)
|
||||||
|
{
|
||||||
|
if (!b)
|
||||||
|
_currentlyAutosaving.Clear();
|
||||||
|
_autosaveEnabled = b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void Update(float frameTime)
|
||||||
|
{
|
||||||
|
base.Update(frameTime);
|
||||||
|
|
||||||
|
if (!_autosaveEnabled)
|
||||||
|
return;
|
||||||
|
|
||||||
|
foreach (var (map, (time, name))in _currentlyAutosaving.ToArray())
|
||||||
|
{
|
||||||
|
if (_timing.RealTime <= time)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (!_mapManager.MapExists(map) || _mapManager.IsMapInitialized(map))
|
||||||
|
{
|
||||||
|
_sawmill.Warning($"Can't autosave map {map}; it doesn't exist, or is initialized. Removing from autosave.");
|
||||||
|
_currentlyAutosaving.Remove(map);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var saveDir = Path.Combine(_cfg.GetCVar(CCVars.AutosaveDirectory), name);
|
||||||
|
_resMan.UserData.CreateDir(new ResourcePath(saveDir).ToRootedPath());
|
||||||
|
|
||||||
|
var path = Path.Combine(saveDir, $"{DateTime.Now.ToString("yyyy-M-dd_HH.mm.ss")}-AUTO.yml");
|
||||||
|
_currentlyAutosaving[map] = (CalculateNextTime(), name);
|
||||||
|
_sawmill.Info($"Autosaving map {name} ({map}) to {path}. Next save in {ReadableTimeLeft(map)} seconds.");
|
||||||
|
_mapLoader.SaveMap(map, path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private TimeSpan CalculateNextTime()
|
||||||
|
{
|
||||||
|
return _timing.RealTime + TimeSpan.FromSeconds(_cfg.GetCVar(CCVars.AutosaveInterval));
|
||||||
|
}
|
||||||
|
|
||||||
|
private double ReadableTimeLeft(MapId map)
|
||||||
|
{
|
||||||
|
return Math.Round(_currentlyAutosaving[map].next.TotalSeconds - _timing.RealTime.TotalSeconds);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region Public API
|
||||||
|
|
||||||
|
public void ToggleAutosave(MapId map, string? path=null)
|
||||||
|
{
|
||||||
|
if (!_autosaveEnabled)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (path != null && _currentlyAutosaving.TryAdd(map, (CalculateNextTime(), Path.GetFileName(path))))
|
||||||
|
{
|
||||||
|
if (!_mapManager.MapExists(map) || _mapManager.IsMapInitialized(map))
|
||||||
|
{
|
||||||
|
_sawmill.Warning("Tried to enable autosaving on non-existant or already initialized map!");
|
||||||
|
_currentlyAutosaving.Remove(map);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_sawmill.Info($"Started autosaving map {path} ({map}). Next save in {ReadableTimeLeft(map)} seconds.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
_currentlyAutosaving.Remove(map);
|
||||||
|
_sawmill.Info($"Stopped autosaving on map {map}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Commands
|
||||||
|
|
||||||
|
[AdminCommand(AdminFlags.Server | AdminFlags.Mapping)]
|
||||||
|
private void ToggleAutosaveCommand(IConsoleShell shell, string argstr, string[] args)
|
||||||
|
{
|
||||||
|
if (args.Length != 1 && args.Length != 2)
|
||||||
|
{
|
||||||
|
shell.WriteError(Loc.GetString("shell-wrong-arguments-number"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!int.TryParse(args[0], out var intMapId))
|
||||||
|
{
|
||||||
|
shell.WriteError(Loc.GetString("cmd-mapping-failure-integer", ("arg", args[0])));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
string? path = null;
|
||||||
|
if (args.Length == 2)
|
||||||
|
{
|
||||||
|
path = args[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
var mapId = new MapId(intMapId);
|
||||||
|
ToggleAutosave(mapId, path);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
}
|
||||||
@@ -1075,6 +1075,28 @@ namespace Content.Shared.CCVar
|
|||||||
public static readonly CVarDef<string>
|
public static readonly CVarDef<string>
|
||||||
SalvageForced = CVarDef.Create("salvage.forced", "", CVar.SERVERONLY);
|
SalvageForced = CVarDef.Create("salvage.forced", "", CVar.SERVERONLY);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Mapping
|
||||||
|
*/
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Will mapping mode enable autosaves when it's activated?
|
||||||
|
/// </summary>
|
||||||
|
public static readonly CVarDef<bool>
|
||||||
|
AutosaveEnabled = CVarDef.Create("mapping.autosave", true, CVar.SERVERONLY);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Autosave interval in seconds.
|
||||||
|
/// </summary>
|
||||||
|
public static readonly CVarDef<float>
|
||||||
|
AutosaveInterval = CVarDef.Create("mapping.autosave_interval", 600f, CVar.SERVERONLY);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Directory in server user data to save to. Saves will be inside folders in this directory.
|
||||||
|
/// </summary>
|
||||||
|
public static readonly CVarDef<string>
|
||||||
|
AutosaveDirectory = CVarDef.Create("mapping.autosave_dir", "Autosaves", CVar.SERVERONLY);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Rules
|
* Rules
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -53,6 +53,7 @@
|
|||||||
- showsubfloor
|
- showsubfloor
|
||||||
- showsubfloorforever
|
- showsubfloorforever
|
||||||
- mapping
|
- mapping
|
||||||
|
- toggleautosave
|
||||||
- toggledecals
|
- toggledecals
|
||||||
- nodevis
|
- nodevis
|
||||||
- nodevisfilter
|
- nodevisfilter
|
||||||
|
|||||||
Reference in New Issue
Block a user