|
1 | 1 | using System.Linq; |
2 | 2 | using System.Numerics; |
| 3 | +using Robust.Shared; |
| 4 | +using Robust.Shared.Configuration; |
3 | 5 | using Robust.Shared.Console; |
4 | 6 | using Robust.Shared.ContentPack; |
5 | 7 | using Robust.Shared.EntitySerialization; |
6 | 8 | using Robust.Shared.EntitySerialization.Systems; |
7 | 9 | using Robust.Shared.GameObjects; |
| 10 | +using Robust.Shared.GameSaves; |
8 | 11 | using Robust.Shared.IoC; |
9 | 12 | using Robust.Shared.Localization; |
10 | 13 | using Robust.Shared.Map; |
@@ -334,4 +337,94 @@ public override void Execute(IConsoleShell shell, string argStr, string[] args) |
334 | 337 | shell.WriteLine(Loc.GetString("cmd-loadmap-error", ("path", args[1]))); |
335 | 338 | } |
336 | 339 | } |
| 340 | + |
| 341 | + public sealed class SaveGame : LocalizedCommands |
| 342 | + { |
| 343 | + [Dependency] private readonly IEntitySystemManager _system = default!; |
| 344 | + [Dependency] private readonly IResourceManager _resource = default!; |
| 345 | + [Dependency] private readonly IConfigurationManager _config = default!; |
| 346 | + |
| 347 | + public override string Command => "savegame"; |
| 348 | + |
| 349 | + public override CompletionResult GetCompletion(IConsoleShell shell, string[] args) |
| 350 | + { |
| 351 | + if (args.Length == 1) |
| 352 | + { |
| 353 | + var opts = CompletionHelper.UserFilePath(args[0], _resource.UserData); |
| 354 | + return CompletionResult.FromHintOptions(opts, Loc.GetString("cmd-hint-savemap-path")); |
| 355 | + } |
| 356 | + return CompletionResult.Empty; |
| 357 | + } |
| 358 | + |
| 359 | + public override void Execute(IConsoleShell shell, string argStr, string[] args) |
| 360 | + { |
| 361 | + if (!_config.GetCVar(CVars.GameSavesEnabled)) |
| 362 | + { |
| 363 | + shell.WriteLine(Loc.GetString("cmd-savegame-disabled")); |
| 364 | + return; |
| 365 | + } |
| 366 | + if (args.Length < 1) |
| 367 | + { |
| 368 | + shell.WriteLine(Help); |
| 369 | + return; |
| 370 | + } |
| 371 | + shell.WriteLine(Loc.GetString("cmd-savegame-attempt", ("path", args[0]))); |
| 372 | + var success = _system.GetEntitySystem<GameSavesSystem>().TrySaveGame(new ResPath(args[0])); |
| 373 | + if (success) |
| 374 | + shell.WriteLine(Loc.GetString("cmd-savegame-success")); |
| 375 | + else |
| 376 | + shell.WriteError(Loc.GetString("cmd-savegame-error")); |
| 377 | + } |
| 378 | + } |
| 379 | + |
| 380 | + public sealed class LoadGame : LocalizedCommands |
| 381 | + { |
| 382 | + [Dependency] private readonly IEntityManager _entMan = default!; |
| 383 | + [Dependency] private readonly IEntitySystemManager _system = default!; |
| 384 | + [Dependency] private readonly IResourceManager _resource = default!; |
| 385 | + [Dependency] private readonly IConfigurationManager _config = default!; |
| 386 | + |
| 387 | + public override string Command => "loadgame"; |
| 388 | + |
| 389 | + public override CompletionResult GetCompletion(IConsoleShell shell, string[] args) |
| 390 | + { |
| 391 | + switch (args.Length) |
| 392 | + { |
| 393 | + case 1: |
| 394 | + var opts = CompletionHelper.UserFilePath(args[0], _resource.UserData); |
| 395 | + return CompletionResult.FromHintOptions(opts, Loc.GetString("cmd-hint-savemap-path")); |
| 396 | + case 2: |
| 397 | + return CompletionResult.FromHint(Loc.GetString("cmd-hint-loadgame-flush")); |
| 398 | + } |
| 399 | + return CompletionResult.Empty; |
| 400 | + } |
| 401 | + |
| 402 | + public override void Execute(IConsoleShell shell, string argStr, string[] args) |
| 403 | + { |
| 404 | + if (!_config.GetCVar(CVars.GameSavesEnabled)) |
| 405 | + { |
| 406 | + shell.WriteLine(Loc.GetString("cmd-loadgame-disabled")); |
| 407 | + return; |
| 408 | + } |
| 409 | + if (args.Length < 1) |
| 410 | + { |
| 411 | + shell.WriteLine(Help); |
| 412 | + return; |
| 413 | + } |
| 414 | + var flush = false; |
| 415 | + if (args.Length == 2 && !bool.TryParse(args[1], out flush)) |
| 416 | + { |
| 417 | + shell.WriteError(Loc.GetString("cmd-parse-failure-bool", ("arg", args[1]))); |
| 418 | + return; |
| 419 | + } |
| 420 | + shell.WriteLine(Loc.GetString("cmd-loadgame-attempt", ("path", args[0]))); |
| 421 | + if (flush) |
| 422 | + _entMan.FlushEntities(); |
| 423 | + var success = _system.GetEntitySystem<GameSavesSystem>().TryLoadGame(new ResPath(args[0])); |
| 424 | + if (success) |
| 425 | + shell.WriteLine(Loc.GetString("cmd-loadgame-success")); |
| 426 | + else |
| 427 | + shell.WriteError(Loc.GetString("cmd-loadgame-error")); |
| 428 | + } |
| 429 | + } |
337 | 430 | } |
0 commit comments