using System; using System.Collections.Generic; using System.Linq; using Content.Shared.Preferences; using Robust.Shared.IoC; using Robust.Shared.Network; using Robust.Shared.Utility; namespace Content.Client.Preferences { /// /// Receives and from the server during the initial /// connection. /// Stores preferences on the server through and . /// public sealed class ClientPreferencesManager : IClientPreferencesManager { [Dependency] private readonly IClientNetManager _netManager = default!; public event Action? OnServerDataLoaded; public GameSettings Settings { get; private set; } = default!; public PlayerPreferences Preferences { get; private set; } = default!; public void Initialize() { _netManager.RegisterNetMessage(HandlePreferencesAndSettings); _netManager.RegisterNetMessage(); _netManager.RegisterNetMessage(); _netManager.RegisterNetMessage(); } public void SelectCharacter(ICharacterProfile profile) { SelectCharacter(Preferences.IndexOfCharacter(profile)); } public void SelectCharacter(int slot) { Preferences = new PlayerPreferences(Preferences.Characters, slot, Preferences.AdminOOCColor); var msg = _netManager.CreateNetMessage(); msg.SelectedCharacterIndex = slot; _netManager.ClientSendMessage(msg); } public void UpdateCharacter(ICharacterProfile profile, int slot) { profile.EnsureValid(); var characters = new Dictionary(Preferences.Characters) {[slot] = profile}; Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex, Preferences.AdminOOCColor); var msg = _netManager.CreateNetMessage(); msg.Profile = profile; msg.Slot = slot; _netManager.ClientSendMessage(msg); } public void CreateCharacter(ICharacterProfile profile) { var characters = new Dictionary(Preferences.Characters); var lowest = Enumerable.Range(0, Settings.MaxCharacterSlots) .Except(characters.Keys) .FirstOrNull(); if (lowest == null) { throw new InvalidOperationException("Out of character slots!"); } var l = lowest.Value; characters.Add(l, profile); Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex, Preferences.AdminOOCColor); UpdateCharacter(profile, l); } public void DeleteCharacter(ICharacterProfile profile) { DeleteCharacter(Preferences.IndexOfCharacter(profile)); } public void DeleteCharacter(int slot) { var characters = Preferences.Characters.Where(p => p.Key != slot); Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex, Preferences.AdminOOCColor); var msg = _netManager.CreateNetMessage(); msg.Slot = slot; _netManager.ClientSendMessage(msg); } private void HandlePreferencesAndSettings(MsgPreferencesAndSettings message) { Preferences = message.Preferences; Settings = message.Settings; OnServerDataLoaded?.Invoke(); } } }