forked from DeltaV-Station/Delta-v
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Language Refactor 3 (DeltaV-Station#937)
# Description This significantly improves the quality of the language system by fixing the mistakes I've made almost a year ago while developing it. Mainly, this throws away the old half-broken way of networking in favor of the component state system provided by RT. Language speaker comp is now shared with SendOnlyToOwner = true, and its state is handled manually. In addition to that, this brings the following changes: - UniversalLanguageSpeaker and LanguageKnowledge are now server-side - DetermineLanguagesEvent and LanguagesUpdateEvent are now shared (so that future systems can be built in shared, if needed) - Everything now uses the ProtoId<LanguagePrototype> type instead of raw strings (god, I hated those so much) - The server-side language system now accepts Entity<T?> arguments instead of EntityUid + T - UniversalLanguageSpeaker is now based on DetermineEntityLanguagesEvent and gets an Enabled field, which allows to turn it off. This may have some use in the future. - Some minor cleanup <!-- TODO MEDIA <details><summary><h1>Media</h1></summary> <p> ![Example Media Embed](https://example.com/thisimageisntreal.png) </p> </details> --> # Changelog No cl --------- Co-authored-by: VMSolidus <[email protected]>
- Loading branch information
1 parent
14d2280
commit 8c5faf3
Showing
24 changed files
with
247 additions
and
325 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,61 @@ | ||
using Content.Shared.Language; | ||
using Content.Shared.Language.Components; | ||
using Content.Shared.Language.Events; | ||
using Content.Shared.Language.Systems; | ||
using Robust.Client; | ||
using Robust.Client.Player; | ||
using Robust.Shared.GameStates; | ||
using Robust.Shared.Prototypes; | ||
|
||
namespace Content.Client.Language.Systems; | ||
|
||
/// <summary> | ||
/// Client-side language system. | ||
/// </summary> | ||
/// <remarks> | ||
/// Unlike the server, the client is not aware of other entities' languages; it's only notified about the entity that it posesses. | ||
/// Due to that, this system stores such information in a static manner. | ||
/// </remarks> | ||
public sealed class LanguageSystem : SharedLanguageSystem | ||
{ | ||
[Dependency] private readonly IBaseClient _client = default!; | ||
[Dependency] private readonly IPlayerManager _playerManager = default!; | ||
|
||
/// <summary> | ||
/// The current language of the entity currently possessed by the player. | ||
/// Invoked when the languages of the local player entity change, for use in UI. | ||
/// </summary> | ||
public string CurrentLanguage { get; private set; } = default!; | ||
/// <summary> | ||
/// The list of languages the currently possessed entity can speak. | ||
/// </summary> | ||
public List<string> SpokenLanguages { get; private set; } = new(); | ||
/// <summary> | ||
/// The list of languages the currently possessed entity can understand. | ||
/// </summary> | ||
public List<string> UnderstoodLanguages { get; private set; } = new(); | ||
|
||
public event EventHandler<LanguagesUpdatedMessage>? OnLanguagesChanged; | ||
public event Action? OnLanguagesChanged; | ||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
SubscribeNetworkEvent<LanguagesUpdatedMessage>(OnLanguagesUpdated); | ||
_client.RunLevelChanged += OnRunLevelChanged; | ||
_playerManager.LocalPlayerAttached += NotifyUpdate; | ||
SubscribeLocalEvent<LanguageSpeakerComponent, ComponentHandleState>(OnHandleState); | ||
} | ||
|
||
private void OnLanguagesUpdated(LanguagesUpdatedMessage message) | ||
private void OnHandleState(Entity<LanguageSpeakerComponent> ent, ref ComponentHandleState args) | ||
{ | ||
// TODO this entire thing is horrible. If someone is willing to refactor this, LanguageSpeakerComponent should become shared with SendOnlyToOwner = true | ||
// That way, this system will be able to use the existing networking infrastructure instead of relying on this makeshift... whatever this is. | ||
CurrentLanguage = message.CurrentLanguage; | ||
SpokenLanguages = message.Spoken; | ||
UnderstoodLanguages = message.Understood; | ||
if (args.Current is not LanguageSpeakerComponent.State state) | ||
return; | ||
|
||
OnLanguagesChanged?.Invoke(this, message); | ||
} | ||
ent.Comp.CurrentLanguage = state.CurrentLanguage; | ||
ent.Comp.SpokenLanguages = state.SpokenLanguages; | ||
ent.Comp.UnderstoodLanguages = state.UnderstoodLanguages; | ||
|
||
private void OnRunLevelChanged(object? sender, RunLevelChangedEventArgs args) | ||
{ | ||
// Request an update when entering a game | ||
if (args.NewLevel == ClientRunLevel.InGame) | ||
RequestStateUpdate(); | ||
if (ent.Owner == _playerManager.LocalEntity) | ||
NotifyUpdate(ent); | ||
} | ||
|
||
/// <summary> | ||
/// Sends a network request to the server to update this system's state. | ||
/// The server may ignore the said request if the player is not possessing an entity. | ||
/// Returns the LanguageSpeakerComponent of the local player entity. | ||
/// Will return null if the player does not have an entity, or if the client has not yet received the component state. | ||
/// </summary> | ||
public void RequestStateUpdate() | ||
public LanguageSpeakerComponent? GetLocalSpeaker() | ||
{ | ||
RaiseNetworkEvent(new RequestLanguagesMessage()); | ||
return CompOrNull<LanguageSpeakerComponent>(_playerManager.LocalEntity); | ||
} | ||
|
||
public void RequestSetLanguage(LanguagePrototype language) | ||
public void RequestSetLanguage(ProtoId<LanguagePrototype> language) | ||
{ | ||
if (language.ID == CurrentLanguage) | ||
if (GetLocalSpeaker()?.CurrentLanguage?.Equals(language) == true) | ||
return; | ||
|
||
RaiseNetworkEvent(new LanguagesSetMessage(language.ID)); | ||
RaiseNetworkEvent(new LanguagesSetMessage(language)); | ||
} | ||
|
||
// May cause some minor desync... | ||
// So to reduce the probability of desync, we replicate the change locally too | ||
if (SpokenLanguages.Contains(language.ID)) | ||
CurrentLanguage = language.ID; | ||
private void NotifyUpdate(EntityUid localPlayer) | ||
{ | ||
RaiseLocalEvent(localPlayer, new LanguagesUpdateEvent(), broadcast: true); | ||
OnLanguagesChanged?.Invoke(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Content.Shared.Language; | ||
using Robust.Shared.Prototypes; | ||
|
||
namespace Content.Server.Language; | ||
|
||
/// <summary> | ||
/// Stores data about entities' intrinsic language knowledge. | ||
/// </summary> | ||
[RegisterComponent] | ||
public sealed partial class LanguageKnowledgeComponent : Component | ||
{ | ||
/// <summary> | ||
/// List of languages this entity can speak without any external tools. | ||
/// </summary> | ||
[DataField("speaks", required: true)] | ||
public List<ProtoId<LanguagePrototype>> SpokenLanguages = new(); | ||
|
||
/// <summary> | ||
/// List of languages this entity can understand without any external tools. | ||
/// </summary> | ||
[DataField("understands", required: true)] | ||
public List<ProtoId<LanguagePrototype>> UnderstoodLanguages = new(); | ||
} |
Oops, something went wrong.