Re-organize all projects (#4166)

This commit is contained in:
DrSmugleaf
2021-06-09 22:19:39 +02:00
committed by GitHub
parent 9f50e4061b
commit ff1a2d97ea
1773 changed files with 5258 additions and 5508 deletions

View File

@@ -0,0 +1,60 @@
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Ghost.Roles.Components
{
public abstract class GhostRoleComponent : Component
{
[DataField("name")] private string _roleName = "Unknown";
[DataField("description")] private string _roleDescription = "Unknown";
// We do this so updating RoleName and RoleDescription in VV updates the open EUIs.
[ViewVariables(VVAccess.ReadWrite)]
public string RoleName
{
get => _roleName;
set
{
_roleName = value;
EntitySystem.Get<GhostRoleSystem>().UpdateAllEui();
}
}
[ViewVariables(VVAccess.ReadWrite)]
public string RoleDescription
{
get => _roleDescription;
set
{
_roleDescription = value;
EntitySystem.Get<GhostRoleSystem>().UpdateAllEui();
}
}
[ViewVariables(VVAccess.ReadOnly)]
public bool Taken { get; protected set; }
[ViewVariables]
public uint Identifier { get; set; }
public override void Initialize()
{
base.Initialize();
EntitySystem.Get<GhostRoleSystem>().RegisterGhostRole(this);
}
protected override void Shutdown()
{
base.Shutdown();
EntitySystem.Get<GhostRoleSystem>().UnregisterGhostRole(this);
}
public abstract bool Take(IPlayerSession session);
}
}

View File

@@ -0,0 +1,72 @@
using System;
using Content.Server.Mind.Commands;
using Content.Server.Mind.Components;
using Content.Server.Players;
using JetBrains.Annotations;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.Ghost.Roles.Components
{
/// <summary>
/// Allows a ghost to take this role, spawning a new entity.
/// </summary>
[RegisterComponent, ComponentReference(typeof(GhostRoleComponent))]
public class GhostRoleMobSpawnerComponent : GhostRoleComponent
{
public override string Name => "GhostRoleMobSpawner";
[ViewVariables(VVAccess.ReadWrite)] [DataField("deleteOnSpawn")]
private bool _deleteOnSpawn = true;
[ViewVariables(VVAccess.ReadWrite)] [DataField("makeSentient")]
private bool _makeSentient = true;
[ViewVariables(VVAccess.ReadWrite)] [DataField("availableTakeovers")]
private int _availableTakeovers = 1;
[ViewVariables]
private int _currentTakeovers = 0;
[CanBeNull]
[ViewVariables(VVAccess.ReadWrite)]
[DataField("prototype")]
public string? Prototype { get; private set; }
public override bool Take(IPlayerSession session)
{
if (Taken)
return false;
if(string.IsNullOrEmpty(Prototype))
throw new NullReferenceException("Prototype string cannot be null or empty!");
var mob = Owner.EntityManager.SpawnEntity(Prototype, Owner.Transform.Coordinates);
if(_makeSentient)
MakeSentientCommand.MakeSentient(mob);
mob.EnsureComponent<MindComponent>();
var mind = session.ContentData()?.Mind;
DebugTools.AssertNotNull(mind);
mind!.TransferTo(mob);
if (++_currentTakeovers < _availableTakeovers) return true;
Taken = true;
if (_deleteOnSpawn)
Owner.Delete();
return true;
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
using Content.Server.Mind.Components;
using Content.Server.Players;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Utility;
namespace Content.Server.Ghost.Roles.Components
{
/// <summary>
/// Allows a ghost to take over the Owner entity.
/// </summary>
[RegisterComponent, ComponentReference(typeof(GhostRoleComponent))]
public class GhostTakeoverAvailableComponent : GhostRoleComponent
{
public override string Name => "GhostTakeoverAvailable";
public override bool Take(IPlayerSession session)
{
if (Taken)
return false;
Taken = true;
var mind = Owner.EnsureComponent<MindComponent>();
if (mind.HasMind)
throw new Exception("MindComponent already has a mind!");
var sessionMind = session.ContentData()?.Mind;
DebugTools.AssertNotNull(sessionMind);
sessionMind!.TransferTo(Owner);
EntitySystem.Get<GhostRoleSystem>().UnregisterGhostRole(this);
return true;
}
}
}

View File

@@ -0,0 +1,174 @@
using System.Collections.Generic;
using Content.Server.Administration;
using Content.Server.EUI;
using Content.Server.Ghost.Components;
using Content.Server.Ghost.Roles.Components;
using Content.Server.Ghost.Roles.UI;
using Content.Shared.GameTicking;
using Content.Shared.Ghost.Roles;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.ViewVariables;
namespace Content.Server.Ghost.Roles
{
[UsedImplicitly]
public class GhostRoleSystem : EntitySystem, IResettingEntitySystem
{
[Dependency] private readonly EuiManager _euiManager = default!;
private uint _nextRoleIdentifier = 0;
private readonly Dictionary<uint, GhostRoleComponent> _ghostRoles = new();
private readonly Dictionary<IPlayerSession, GhostRolesEui> _openUis = new();
private readonly Dictionary<IPlayerSession, MakeGhostRoleEui> _openMakeGhostRoleUis = new();
[ViewVariables]
public IReadOnlyCollection<GhostRoleComponent> GhostRoles => _ghostRoles.Values;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PlayerAttachedEvent>(OnPlayerAttached);
}
public override void Shutdown()
{
base.Shutdown();
UnsubscribeLocalEvent<PlayerAttachedEvent>();
}
private uint GetNextRoleIdentifier()
{
return unchecked(_nextRoleIdentifier++);
}
public void OpenEui(IPlayerSession session)
{
if (session.AttachedEntity == null || !session.AttachedEntity.HasComponent<GhostComponent>())
return;
if(_openUis.ContainsKey(session))
CloseEui(session);
var eui = _openUis[session] = new GhostRolesEui();
_euiManager.OpenEui(eui, session);
eui.StateDirty();
}
public void OpenMakeGhostRoleEui(IPlayerSession session, EntityUid uid)
{
if (session.AttachedEntity == null)
return;
if (_openMakeGhostRoleUis.ContainsKey(session))
CloseEui(session);
var eui = _openMakeGhostRoleUis[session] = new MakeGhostRoleEui(uid);
_euiManager.OpenEui(eui, session);
eui.StateDirty();
}
public void CloseEui(IPlayerSession session)
{
if (!_openUis.ContainsKey(session)) return;
_openUis.Remove(session, out var eui);
eui?.Close();
}
public void CloseMakeGhostRoleEui(IPlayerSession session)
{
if (_openMakeGhostRoleUis.Remove(session, out var eui))
{
eui?.Close();
}
}
public void UpdateAllEui()
{
foreach (var eui in _openUis.Values)
{
eui.StateDirty();
}
}
public void RegisterGhostRole(GhostRoleComponent role)
{
if (_ghostRoles.ContainsValue(role)) return;
_ghostRoles[role.Identifier = GetNextRoleIdentifier()] = role;
UpdateAllEui();
}
public void UnregisterGhostRole(GhostRoleComponent role)
{
if (!_ghostRoles.ContainsKey(role.Identifier) || _ghostRoles[role.Identifier] != role) return;
_ghostRoles.Remove(role.Identifier);
UpdateAllEui();
}
public void Takeover(IPlayerSession player, uint identifier)
{
if (!_ghostRoles.TryGetValue(identifier, out var role)) return;
if (!role.Take(player)) return;
CloseEui(player);
}
public GhostRoleInfo[] GetGhostRolesInfo()
{
var roles = new GhostRoleInfo[_ghostRoles.Count];
var i = 0;
foreach (var (id, role) in _ghostRoles)
{
roles[i] = new GhostRoleInfo(){Identifier = id, Name = role.RoleName, Description = role.RoleDescription};
i++;
}
return roles;
}
private void OnPlayerAttached(PlayerAttachedEvent message)
{
// Close the session of any player that has a ghost roles window open and isn't a ghost anymore.
if (!_openUis.ContainsKey(message.Player)) return;
if (message.Entity.HasComponent<GhostComponent>()) return;
CloseEui(message.Player);
}
public void Reset()
{
foreach (var session in _openUis.Keys)
{
CloseEui(session);
}
_openUis.Clear();
_ghostRoles.Clear();
_nextRoleIdentifier = 0;
}
}
[AnyCommand]
public class GhostRoles : IConsoleCommand
{
public string Command => "ghostroles";
public string Description => "Opens the ghost role request window.";
public string Help => $"{Command}";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if(shell.Player != null)
EntitySystem.Get<GhostRoleSystem>().OpenEui((IPlayerSession)shell.Player);
else
shell.WriteLine("You can only open the ghost roles UI on a client.");
}
}
}

View File

@@ -0,0 +1,63 @@
#nullable enable
using Content.Server.Administration;
using Content.Server.Ghost.Roles.Components;
using Content.Server.Mind.Components;
using Content.Shared.Administration;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Ghost.Roles
{
[AdminCommand(AdminFlags.Fun)]
public class MakeGhostRoleCommand : IConsoleCommand
{
public string Command => "makeghostrole";
public string Description => "Turns an entity into a ghost role.";
public string Help => $"Usage: {Command} <entity uid> <name> <description>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 3)
{
shell.WriteLine($"Invalid amount of arguments.\n{Help}");
return;
}
var entityManager = IoCManager.Resolve<IEntityManager>();
if (!EntityUid.TryParse(args[0], out var uid))
{
shell.WriteLine($"{args[0]} is not a valid entity uid.");
return;
}
if (!entityManager.TryGetEntity(uid, out var entity))
{
shell.WriteLine($"No entity found with uid {uid}");
return;
}
if (entity.TryGetComponent(out MindComponent? mind) &&
mind.HasMind)
{
shell.WriteLine($"Entity {entity.Name} with id {uid} already has a mind.");
return;
}
var name = args[1];
var description = args[2];
if (entity.EnsureComponent(out GhostTakeoverAvailableComponent takeOver))
{
shell.WriteLine($"Entity {entity.Name} with id {uid} already has a {nameof(GhostTakeoverAvailableComponent)}");
return;
}
takeOver.RoleName = name;
takeOver.RoleDescription = description;
shell.WriteLine($"Made entity {entity.Name} a ghost role.");
}
}
}

View File

@@ -0,0 +1,61 @@
#nullable enable
using Content.Server.Mind.Components;
using Content.Shared.Verbs;
using Robust.Server.Console;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
namespace Content.Server.Ghost.Roles
{
[GlobalVerb]
public class MakeGhostRoleVerb : GlobalVerb
{
public override bool RequireInteractionRange => false;
public override bool BlockedByContainers => false;
public override void GetData(IEntity user, IEntity target, VerbData data)
{
data.Visibility = VerbVisibility.Invisible;
var groupController = IoCManager.Resolve<IConGroupController>();
if (target.TryGetComponent(out MindComponent? mind) &&
mind.HasMind)
{
return;
}
if (!user.TryGetComponent(out ActorComponent? actor) ||
!groupController.CanCommand(actor.PlayerSession, "makeghostrole"))
{
return;
}
data.Visibility = VerbVisibility.Visible;
data.Text = Loc.GetString("Make Ghost Role");
data.CategoryData = VerbCategories.Debug;
}
public override void Activate(IEntity user, IEntity target)
{
var groupController = IoCManager.Resolve<IConGroupController>();
if (target.TryGetComponent(out MindComponent? mind) &&
mind.HasMind)
{
return;
}
if (!user.TryGetComponent(out ActorComponent? actor) ||
!groupController.CanCommand(actor.PlayerSession, "makeghostrole"))
{
return;
}
var ghostRoleSystem = EntitySystem.Get<GhostRoleSystem>();
ghostRoleSystem.OpenMakeGhostRoleEui(actor.PlayerSession, target.Uid);
}
}
}

View File

@@ -0,0 +1,38 @@
using Content.Server.EUI;
using Content.Shared.Eui;
using Content.Shared.Ghost.Roles;
using Robust.Shared.GameObjects;
namespace Content.Server.Ghost.Roles.UI
{
public class GhostRolesEui : BaseEui
{
public override GhostRolesEuiState GetNewState()
{
return new(EntitySystem.Get<GhostRoleSystem>().GetGhostRolesInfo());
}
public override void HandleMessage(EuiMessageBase msg)
{
base.HandleMessage(msg);
switch (msg)
{
case GhostRoleTakeoverRequestMessage req:
EntitySystem.Get<GhostRoleSystem>().Takeover(Player, req.Identifier);
break;
case GhostRoleWindowCloseMessage _:
Closed();
break;
}
}
public override void Closed()
{
base.Closed();
EntitySystem.Get<GhostRoleSystem>().CloseEui(Player);
}
}
}

View File

@@ -0,0 +1,41 @@
using Content.Server.EUI;
using Content.Shared.Eui;
using Content.Shared.Ghost.Roles;
using Robust.Shared.GameObjects;
namespace Content.Server.Ghost.Roles.UI
{
public class MakeGhostRoleEui : BaseEui
{
public MakeGhostRoleEui(EntityUid entityUid)
{
EntityUid = entityUid;
}
public EntityUid EntityUid { get; }
public override EuiStateBase GetNewState()
{
return new MakeGhostRoleEuiState(EntityUid);
}
public override void HandleMessage(EuiMessageBase msg)
{
base.HandleMessage(msg);
switch (msg)
{
case MakeGhostRoleWindowClosedMessage _:
Closed();
break;
}
}
public override void Closed()
{
base.Closed();
EntitySystem.Get<GhostRoleSystem>().CloseMakeGhostRoleEui(Player);
}
}
}