Add game preset attribute, unhardcode game presets (#3166)
* Add game preset attribute, unhardcode game presets * Add game preset attribute doc * Add preset attribute to extended * Remove try methods from playerdata
This commit is contained in:
@@ -40,4 +40,4 @@ namespace Content.Server.Commands.GameTicking
|
||||
shell.WriteLine($"Forced the game to start with preset {name}.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Immutable;
|
||||
using JetBrains.Annotations;
|
||||
|
||||
namespace Content.Server.GameTicking.GamePresets
|
||||
{
|
||||
/// <summary>
|
||||
/// Attribute that marks a game preset.
|
||||
/// The id and aliases are registered in lowercase in <see cref="GameTicker"/>.
|
||||
/// A duplicate id or alias will throw an exception.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
|
||||
[BaseTypeRequired(typeof(GamePreset))]
|
||||
[MeansImplicitUse]
|
||||
public class GamePresetAttribute : Attribute
|
||||
{
|
||||
public string Id { get; }
|
||||
|
||||
public ImmutableList<string> Aliases { get; }
|
||||
|
||||
public GamePresetAttribute(string id, params string[] aliases)
|
||||
{
|
||||
Id = id;
|
||||
Aliases = aliases.ToImmutableList();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Server.GameTicking.GamePresets
|
||||
{
|
||||
[GamePreset("deathmatch")]
|
||||
public sealed class PresetDeathMatch : GamePreset
|
||||
{
|
||||
[Dependency] private readonly IGameTicker _gameTicker = default!;
|
||||
|
||||
@@ -3,6 +3,7 @@ using Robust.Server.Player;
|
||||
|
||||
namespace Content.Server.GameTicking.GamePresets
|
||||
{
|
||||
[GamePreset("extended")]
|
||||
public class PresetExtended : GamePreset
|
||||
{
|
||||
public override string Description => "No antagonists, have fun!";
|
||||
|
||||
@@ -5,6 +5,7 @@ using Robust.Shared.IoC;
|
||||
|
||||
namespace Content.Server.GameTicking.GamePresets
|
||||
{
|
||||
[GamePreset("sandbox")]
|
||||
public sealed class PresetSandbox : GamePreset
|
||||
{
|
||||
[Dependency] private readonly ISandboxManager _sandboxManager = default!;
|
||||
|
||||
@@ -24,6 +24,7 @@ using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.GameTicking.GamePresets
|
||||
{
|
||||
[GamePreset("suspicion")]
|
||||
public class PresetSuspicion : GamePreset
|
||||
{
|
||||
[Dependency] private readonly IChatManager _chatManager = default!;
|
||||
|
||||
@@ -25,9 +25,10 @@ using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.GameTicking.GamePresets
|
||||
{
|
||||
[GamePreset("traitor")]
|
||||
public class PresetTraitor : GamePreset
|
||||
{
|
||||
[Dependency] private readonly IGameTicker _gameticker = default!;
|
||||
[Dependency] private readonly IGameTicker _gameTicker = default!;
|
||||
[Dependency] private readonly IChatManager _chatManager = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
@@ -154,7 +155,7 @@ namespace Content.Server.GameTicking.GamePresets
|
||||
traitor.GreetTraitor(codewords);
|
||||
}
|
||||
|
||||
_gameticker.AddGameRule<RuleTraitor>();
|
||||
_gameTicker.AddGameRule<RuleTraitor>();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,7 @@ using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.GameTicking.GamePresets
|
||||
{
|
||||
[GamePreset("traitordm", "traitordeathmatch")]
|
||||
public sealed class PresetTraitorDeathMatch : GamePreset
|
||||
{
|
||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using Content.Server.GameObjects.Components.Access;
|
||||
using Content.Server.GameObjects.Components.GUI;
|
||||
@@ -41,6 +44,7 @@ using Robust.Shared.Map;
|
||||
using Robust.Shared.Network;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
using Robust.Shared.Reflection;
|
||||
using Robust.Shared.Timing;
|
||||
using Robust.Shared.Utility;
|
||||
using Robust.Shared.ViewVariables;
|
||||
@@ -118,6 +122,8 @@ namespace Content.Server.GameTicking
|
||||
set => _preset = value;
|
||||
}
|
||||
|
||||
public ImmutableDictionary<string, Type> Presets { get; private set; }
|
||||
|
||||
private GamePreset _preset;
|
||||
|
||||
public event Action<GameRunLevelChangedEventArgs> OnRunLevelChanged;
|
||||
@@ -132,6 +138,22 @@ namespace Content.Server.GameTicking
|
||||
|
||||
DebugTools.Assert(!_initialized);
|
||||
|
||||
var presets = new Dictionary<string, Type>();
|
||||
|
||||
foreach (var type in _reflectionManager.FindTypesWithAttribute<GamePresetAttribute>())
|
||||
{
|
||||
var attribute = type.GetCustomAttribute<GamePresetAttribute>();
|
||||
|
||||
presets.Add(attribute!.Id.ToLowerInvariant(), type);
|
||||
|
||||
foreach (var alias in attribute.Aliases)
|
||||
{
|
||||
presets.Add(alias.ToLowerInvariant(), type);
|
||||
}
|
||||
}
|
||||
|
||||
Presets = presets.ToImmutableDictionary();
|
||||
|
||||
_netManager.RegisterNetMessage<MsgTickerJoinLobby>(nameof(MsgTickerJoinLobby));
|
||||
_netManager.RegisterNetMessage<MsgTickerJoinGame>(nameof(MsgTickerJoinGame));
|
||||
_netManager.RegisterNetMessage<MsgTickerLobbyStatus>(nameof(MsgTickerLobbyStatus));
|
||||
@@ -479,21 +501,10 @@ namespace Content.Server.GameTicking
|
||||
|
||||
public IEnumerable<GameRule> ActiveGameRules => _gameRules;
|
||||
|
||||
public bool TryGetPreset(string name, out Type type)
|
||||
public bool TryGetPreset(string name, [NotNullWhen(true)] out Type type)
|
||||
{
|
||||
type = name.ToLower() switch
|
||||
{
|
||||
"sandbox" => typeof(PresetSandbox),
|
||||
"deathmatch" => typeof(PresetDeathMatch),
|
||||
"suspicion" => typeof(PresetSuspicion),
|
||||
"traitor" => typeof(PresetTraitor),
|
||||
"traitordm" => typeof(PresetTraitorDeathMatch),
|
||||
"traitordeathmatch" => typeof(PresetTraitorDeathMatch),
|
||||
"extended" => typeof(PresetExtended),
|
||||
_ => default
|
||||
};
|
||||
|
||||
return type != default;
|
||||
name = name.ToLowerInvariant();
|
||||
return Presets.TryGetValue(name, out type);
|
||||
}
|
||||
|
||||
public void SetStartPreset(Type type, bool force = false)
|
||||
@@ -513,7 +524,7 @@ namespace Content.Server.GameTicking
|
||||
{
|
||||
if (!TryGetPreset(name, out var type))
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
throw new NotSupportedException($"No preset found with name {name}");
|
||||
}
|
||||
|
||||
SetStartPreset(type, force);
|
||||
@@ -576,7 +587,7 @@ namespace Content.Server.GameTicking
|
||||
|
||||
private IEntity _spawnPlayerMob(Job job, HumanoidCharacterProfile profile, bool lateJoin = true)
|
||||
{
|
||||
EntityCoordinates coordinates = lateJoin ? GetLateJoinSpawnPoint() : GetJobSpawnPoint(job.Prototype.ID);
|
||||
var coordinates = lateJoin ? GetLateJoinSpawnPoint() : GetJobSpawnPoint(job.Prototype.ID);
|
||||
var entity = _entityManager.SpawnEntity(PlayerPrototypeName, coordinates);
|
||||
var startingGear = _prototypeManager.Index<StartingGearPrototype>(job.StartingGear);
|
||||
EquipStartingGear(entity, startingGear, profile);
|
||||
@@ -1071,6 +1082,7 @@ The current game mode is: [color=white]{0}[/color].
|
||||
[Dependency] private readonly IBaseServer _baseServer = default!;
|
||||
[Dependency] private readonly IWatchdogApi _watchdogApi = default!;
|
||||
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
|
||||
[Dependency] private readonly IReflectionManager _reflectionManager = default!;
|
||||
}
|
||||
|
||||
public enum GameRunLevel
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using Content.Server.GameTicking;
|
||||
using Content.Server.Mobs;
|
||||
using Content.Shared.Roles;
|
||||
@@ -17,12 +18,12 @@ namespace Content.Server.Interfaces.GameTicking
|
||||
public interface IGameTicker
|
||||
{
|
||||
GameRunLevel RunLevel { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The map loaded by the GameTicker on round start.
|
||||
/// </summary>
|
||||
MapId DefaultMap { get; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// The GridId loaded by the GameTicker on round start.
|
||||
/// </summary>
|
||||
@@ -59,7 +60,7 @@ namespace Content.Server.Interfaces.GameTicking
|
||||
void RemoveGameRule(GameRule rule);
|
||||
IEnumerable<GameRule> ActiveGameRules { get; }
|
||||
|
||||
bool TryGetPreset(string name, out Type type);
|
||||
bool TryGetPreset(string name, [NotNullWhen(true)] out Type type);
|
||||
void SetStartPreset(Type type, bool force = false);
|
||||
void SetStartPreset(string name, bool force = false);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user