Re-organize in-memory character profile storage.
Now uses a dictionary of int -> profile instead of an array filled with nulls.
This commit is contained in:
@@ -1,9 +1,12 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Content.Client.Interfaces;
|
using Content.Client.Interfaces;
|
||||||
|
using Content.Shared.Network.NetMessages;
|
||||||
using Content.Shared.Preferences;
|
using Content.Shared.Preferences;
|
||||||
using Robust.Shared.Interfaces.Network;
|
using Robust.Shared.Interfaces.Network;
|
||||||
using Robust.Shared.IoC;
|
using Robust.Shared.IoC;
|
||||||
|
using Robust.Shared.Utility;
|
||||||
|
|
||||||
namespace Content.Client
|
namespace Content.Client
|
||||||
{
|
{
|
||||||
@@ -12,7 +15,7 @@ namespace Content.Client
|
|||||||
/// connection.
|
/// connection.
|
||||||
/// Stores preferences on the server through <see cref="SelectCharacter" /> and <see cref="UpdateCharacter" />.
|
/// Stores preferences on the server through <see cref="SelectCharacter" /> and <see cref="UpdateCharacter" />.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ClientPreferencesManager : SharedPreferencesManager, IClientPreferencesManager
|
public class ClientPreferencesManager : IClientPreferencesManager
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IClientNetManager _netManager = default!;
|
[Dependency] private readonly IClientNetManager _netManager = default!;
|
||||||
|
|
||||||
@@ -44,8 +47,7 @@ namespace Content.Client
|
|||||||
|
|
||||||
public void UpdateCharacter(ICharacterProfile profile, int slot)
|
public void UpdateCharacter(ICharacterProfile profile, int slot)
|
||||||
{
|
{
|
||||||
var characters = Preferences.Characters.ToArray();
|
var characters = new Dictionary<int, ICharacterProfile>(Preferences.Characters) {[slot] = profile};
|
||||||
characters[slot] = profile;
|
|
||||||
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex);
|
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex);
|
||||||
var msg = _netManager.CreateNetMessage<MsgUpdateCharacter>();
|
var msg = _netManager.CreateNetMessage<MsgUpdateCharacter>();
|
||||||
msg.Profile = profile;
|
msg.Profile = profile;
|
||||||
@@ -55,12 +57,21 @@ namespace Content.Client
|
|||||||
|
|
||||||
public void CreateCharacter(ICharacterProfile profile)
|
public void CreateCharacter(ICharacterProfile profile)
|
||||||
{
|
{
|
||||||
var characters = Preferences.Characters.ToList();
|
var characters = new Dictionary<int, ICharacterProfile>(Preferences.Characters);
|
||||||
|
var lowest = Enumerable.Range(0, Settings.MaxCharacterSlots)
|
||||||
|
.Except(characters.Keys)
|
||||||
|
.FirstOrNull();
|
||||||
|
|
||||||
characters.Add(profile);
|
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 = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex);
|
||||||
|
|
||||||
UpdateCharacter(profile, characters.Count - 1);
|
UpdateCharacter(profile, l);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteCharacter(ICharacterProfile profile)
|
public void DeleteCharacter(ICharacterProfile profile)
|
||||||
@@ -70,7 +81,7 @@ namespace Content.Client
|
|||||||
|
|
||||||
public void DeleteCharacter(int slot)
|
public void DeleteCharacter(int slot)
|
||||||
{
|
{
|
||||||
var characters = Preferences.Characters.Where((profile, index) => index != slot).ToArray();
|
var characters = Preferences.Characters.Where(p => p.Key != slot);
|
||||||
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex);
|
Preferences = new PlayerPreferences(characters, Preferences.SelectedCharacterIndex);
|
||||||
var msg = _netManager.CreateNetMessage<MsgDeleteCharacter>();
|
var msg = _netManager.CreateNetMessage<MsgDeleteCharacter>();
|
||||||
msg.Slot = slot;
|
msg.Slot = slot;
|
||||||
|
|||||||
@@ -174,12 +174,10 @@ namespace Content.Client.UserInterface
|
|||||||
_createNewCharacterButton.ToolTip =
|
_createNewCharacterButton.ToolTip =
|
||||||
$"A maximum of {_preferencesManager.Settings.MaxCharacterSlots} characters are allowed.";
|
$"A maximum of {_preferencesManager.Settings.MaxCharacterSlots} characters are allowed.";
|
||||||
|
|
||||||
var characterIndex = 0;
|
foreach (var (slot, character) in _preferencesManager.Preferences.Characters)
|
||||||
foreach (var character in _preferencesManager.Preferences.Characters)
|
|
||||||
{
|
{
|
||||||
if (character is null)
|
if (character is null)
|
||||||
{
|
{
|
||||||
characterIndex++;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -190,7 +188,7 @@ namespace Content.Client.UserInterface
|
|||||||
character);
|
character);
|
||||||
_charactersVBox.AddChild(characterPickerButton);
|
_charactersVBox.AddChild(characterPickerButton);
|
||||||
|
|
||||||
var characterIndexCopy = characterIndex;
|
var characterIndexCopy = slot;
|
||||||
characterPickerButton.OnPressed += args =>
|
characterPickerButton.OnPressed += args =>
|
||||||
{
|
{
|
||||||
_humanoidProfileEditor.Profile = (HumanoidCharacterProfile) character;
|
_humanoidProfileEditor.Profile = (HumanoidCharacterProfile) character;
|
||||||
@@ -200,7 +198,6 @@ namespace Content.Client.UserInterface
|
|||||||
UpdateUI();
|
UpdateUI();
|
||||||
args.Event.Handle();
|
args.Event.Handle();
|
||||||
};
|
};
|
||||||
characterIndex++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_createNewCharacterButton.Disabled =
|
_createNewCharacterButton.Disabled =
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
@@ -24,18 +25,14 @@ namespace Content.Server.Database
|
|||||||
|
|
||||||
if (prefs is null) return null;
|
if (prefs is null) return null;
|
||||||
|
|
||||||
var maxSlot = prefs.Profiles.Max(p => p.Slot)+1;
|
var maxSlot = prefs.Profiles.Max(p => p.Slot) + 1;
|
||||||
var profiles = new ICharacterProfile[maxSlot];
|
var profiles = new Dictionary<int, ICharacterProfile>(maxSlot);
|
||||||
foreach (var profile in prefs.Profiles)
|
foreach (var profile in prefs.Profiles)
|
||||||
{
|
{
|
||||||
profiles[profile.Slot] = ConvertProfiles(profile);
|
profiles[profile.Slot] = ConvertProfiles(profile);
|
||||||
}
|
}
|
||||||
|
|
||||||
return new PlayerPreferences
|
return new PlayerPreferences(profiles, prefs.SelectedCharacterSlot);
|
||||||
(
|
|
||||||
profiles,
|
|
||||||
prefs.SelectedCharacterSlot
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task SaveSelectedCharacterIndexAsync(NetUserId userId, int index)
|
public async Task SaveSelectedCharacterIndexAsync(NetUserId userId, int index)
|
||||||
@@ -113,7 +110,7 @@ namespace Content.Server.Database
|
|||||||
|
|
||||||
await db.DbContext.SaveChangesAsync();
|
await db.DbContext.SaveChangesAsync();
|
||||||
|
|
||||||
return new PlayerPreferences(new []{defaultProfile}, 0);
|
return new PlayerPreferences(new[] {new KeyValuePair<int, ICharacterProfile>(0, defaultProfile)}, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static HumanoidCharacterProfile ConvertProfiles(Profile profile)
|
private static HumanoidCharacterProfile ConvertProfiles(Profile profile)
|
||||||
@@ -216,6 +213,5 @@ namespace Content.Server.Database
|
|||||||
|
|
||||||
public abstract ValueTask DisposeAsync();
|
public abstract ValueTask DisposeAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
|||||||
using Content.Server.Database;
|
using Content.Server.Database;
|
||||||
using Content.Server.Interfaces;
|
using Content.Server.Interfaces;
|
||||||
using Content.Shared;
|
using Content.Shared;
|
||||||
|
using Content.Shared.Network.NetMessages;
|
||||||
using Content.Shared.Preferences;
|
using Content.Shared.Preferences;
|
||||||
using Robust.Server.Interfaces.Player;
|
using Robust.Server.Interfaces.Player;
|
||||||
using Robust.Shared.Interfaces.Configuration;
|
using Robust.Shared.Interfaces.Configuration;
|
||||||
@@ -19,10 +20,10 @@ using Robust.Shared.Prototypes;
|
|||||||
namespace Content.Server.Preferences
|
namespace Content.Server.Preferences
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Sends <see cref="SharedPreferencesManager.MsgPreferencesAndSettings"/> before the client joins the lobby.
|
/// Sends <see cref="MsgPreferencesAndSettings"/> before the client joins the lobby.
|
||||||
/// Receives <see cref="SharedPreferencesManager.MsgSelectCharacter"/> and <see cref="SharedPreferencesManager.MsgUpdateCharacter"/> at any time.
|
/// Receives <see cref="MsgSelectCharacter"/> and <see cref="MsgUpdateCharacter"/> at any time.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class ServerPreferencesManager : SharedPreferencesManager, IServerPreferencesManager
|
public class ServerPreferencesManager : IServerPreferencesManager
|
||||||
{
|
{
|
||||||
[Dependency] private readonly IServerNetManager _netManager = default!;
|
[Dependency] private readonly IServerNetManager _netManager = default!;
|
||||||
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
[Dependency] private readonly IConfigurationManager _cfg = default!;
|
||||||
@@ -99,12 +100,12 @@ namespace Content.Server.Preferences
|
|||||||
|
|
||||||
var curPrefs = prefsData.Prefs!;
|
var curPrefs = prefsData.Prefs!;
|
||||||
|
|
||||||
var arr = new ICharacterProfile[MaxCharacterSlots];
|
var profiles = new Dictionary<int, ICharacterProfile>(curPrefs.Characters)
|
||||||
curPrefs.Characters.ToList().CopyTo(arr, 0);
|
{
|
||||||
|
[slot] = HumanoidCharacterProfile.EnsureValid((HumanoidCharacterProfile) profile, _protos)
|
||||||
|
};
|
||||||
|
|
||||||
arr[slot] = HumanoidCharacterProfile.EnsureValid((HumanoidCharacterProfile) profile, _protos);
|
prefsData.Prefs = new PlayerPreferences(profiles, slot);
|
||||||
|
|
||||||
prefsData.Prefs = new PlayerPreferences(arr, slot);
|
|
||||||
|
|
||||||
if (ShouldStorePrefs(message.MsgChannel.AuthType))
|
if (ShouldStorePrefs(message.MsgChannel.AuthType))
|
||||||
{
|
{
|
||||||
@@ -130,8 +131,8 @@ namespace Content.Server.Preferences
|
|||||||
|
|
||||||
var curPrefs = prefsData.Prefs!;
|
var curPrefs = prefsData.Prefs!;
|
||||||
|
|
||||||
var arr = new ICharacterProfile[MaxCharacterSlots];
|
var arr = new Dictionary<int, ICharacterProfile>(curPrefs.Characters);
|
||||||
curPrefs.Characters.Where((profile, index) => index != slot).ToArray().CopyTo(arr, 0);
|
arr.Remove(slot);
|
||||||
|
|
||||||
prefsData.Prefs = new PlayerPreferences(arr, slot);
|
prefsData.Prefs = new PlayerPreferences(arr, slot);
|
||||||
|
|
||||||
@@ -150,7 +151,7 @@ namespace Content.Server.Preferences
|
|||||||
{
|
{
|
||||||
PrefsLoaded = Task.CompletedTask,
|
PrefsLoaded = Task.CompletedTask,
|
||||||
Prefs = new PlayerPreferences(
|
Prefs = new PlayerPreferences(
|
||||||
new ICharacterProfile[] {HumanoidCharacterProfile.Default()},
|
new[] {new KeyValuePair<int, ICharacterProfile>(0, HumanoidCharacterProfile.Default())},
|
||||||
0)
|
0)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
33
Content.Shared/Network/NetMessages/MsgDeleteCharacter.cs
Normal file
33
Content.Shared/Network/NetMessages/MsgDeleteCharacter.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
using Lidgren.Network;
|
||||||
|
using Robust.Shared.Interfaces.Network;
|
||||||
|
using Robust.Shared.Network;
|
||||||
|
|
||||||
|
namespace Content.Shared.Network.NetMessages
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The client sends this to delete a character profile.
|
||||||
|
/// </summary>
|
||||||
|
public class MsgDeleteCharacter : NetMessage
|
||||||
|
{
|
||||||
|
#region REQUIRED
|
||||||
|
|
||||||
|
public const MsgGroups GROUP = MsgGroups.Command;
|
||||||
|
public const string NAME = nameof(MsgDeleteCharacter);
|
||||||
|
|
||||||
|
public MsgDeleteCharacter(INetChannel channel) : base(NAME, GROUP) { }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public int Slot;
|
||||||
|
|
||||||
|
public override void ReadFromBuffer(NetIncomingMessage buffer)
|
||||||
|
{
|
||||||
|
Slot = buffer.ReadInt32();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void WriteToBuffer(NetOutgoingMessage buffer)
|
||||||
|
{
|
||||||
|
buffer.Write(Slot);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
using System.IO;
|
||||||
|
using Content.Shared.Preferences;
|
||||||
|
using Lidgren.Network;
|
||||||
|
using Robust.Shared.Interfaces.Network;
|
||||||
|
using Robust.Shared.Interfaces.Serialization;
|
||||||
|
using Robust.Shared.IoC;
|
||||||
|
using Robust.Shared.Network;
|
||||||
|
|
||||||
|
namespace Content.Shared.Network.NetMessages
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The server sends this before the client joins the lobby.
|
||||||
|
/// </summary>
|
||||||
|
public class MsgPreferencesAndSettings : NetMessage
|
||||||
|
{
|
||||||
|
#region REQUIRED
|
||||||
|
|
||||||
|
public const MsgGroups GROUP = MsgGroups.Command;
|
||||||
|
public const string NAME = nameof(MsgPreferencesAndSettings);
|
||||||
|
|
||||||
|
public MsgPreferencesAndSettings(INetChannel channel) : base(NAME, GROUP) { }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public PlayerPreferences Preferences;
|
||||||
|
public GameSettings Settings;
|
||||||
|
|
||||||
|
public override void ReadFromBuffer(NetIncomingMessage buffer)
|
||||||
|
{
|
||||||
|
var serializer = IoCManager.Resolve<IRobustSerializer>();
|
||||||
|
var length = buffer.ReadVariableInt32();
|
||||||
|
using (var stream = buffer.ReadAsStream(length))
|
||||||
|
{
|
||||||
|
serializer.DeserializeDirect(stream, out Preferences);
|
||||||
|
}
|
||||||
|
|
||||||
|
length = buffer.ReadVariableInt32();
|
||||||
|
using (var stream = buffer.ReadAsStream(length))
|
||||||
|
{
|
||||||
|
serializer.DeserializeDirect(stream, out Settings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void WriteToBuffer(NetOutgoingMessage buffer)
|
||||||
|
{
|
||||||
|
var serializer = IoCManager.Resolve<IRobustSerializer>();
|
||||||
|
using (var stream = new MemoryStream())
|
||||||
|
{
|
||||||
|
serializer.SerializeDirect(stream, Preferences);
|
||||||
|
buffer.WriteVariableInt32((int) stream.Length);
|
||||||
|
stream.TryGetBuffer(out var segment);
|
||||||
|
buffer.Write(segment);
|
||||||
|
}
|
||||||
|
|
||||||
|
using (var stream = new MemoryStream())
|
||||||
|
{
|
||||||
|
serializer.SerializeDirect(stream, Settings);
|
||||||
|
buffer.WriteVariableInt32((int) stream.Length);
|
||||||
|
stream.TryGetBuffer(out var segment);
|
||||||
|
buffer.Write(segment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
33
Content.Shared/Network/NetMessages/MsgSelectCharacter.cs
Normal file
33
Content.Shared/Network/NetMessages/MsgSelectCharacter.cs
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
using Lidgren.Network;
|
||||||
|
using Robust.Shared.Interfaces.Network;
|
||||||
|
using Robust.Shared.Network;
|
||||||
|
|
||||||
|
namespace Content.Shared.Network.NetMessages
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The client sends this to select a character slot.
|
||||||
|
/// </summary>
|
||||||
|
public class MsgSelectCharacter : NetMessage
|
||||||
|
{
|
||||||
|
#region REQUIRED
|
||||||
|
|
||||||
|
public const MsgGroups GROUP = MsgGroups.Command;
|
||||||
|
public const string NAME = nameof(MsgSelectCharacter);
|
||||||
|
|
||||||
|
public MsgSelectCharacter(INetChannel channel) : base(NAME, GROUP) { }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public int SelectedCharacterIndex;
|
||||||
|
|
||||||
|
public override void ReadFromBuffer(NetIncomingMessage buffer)
|
||||||
|
{
|
||||||
|
SelectedCharacterIndex = buffer.ReadVariableInt32();
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void WriteToBuffer(NetOutgoingMessage buffer)
|
||||||
|
{
|
||||||
|
buffer.WriteVariableInt32(SelectedCharacterIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
50
Content.Shared/Network/NetMessages/MsgUpdateCharacter.cs
Normal file
50
Content.Shared/Network/NetMessages/MsgUpdateCharacter.cs
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
using System.IO;
|
||||||
|
using Content.Shared.Preferences;
|
||||||
|
using Lidgren.Network;
|
||||||
|
using Robust.Shared.Interfaces.Network;
|
||||||
|
using Robust.Shared.Interfaces.Serialization;
|
||||||
|
using Robust.Shared.IoC;
|
||||||
|
using Robust.Shared.Network;
|
||||||
|
|
||||||
|
namespace Content.Shared.Network.NetMessages
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// The client sends this to update a character profile.
|
||||||
|
/// </summary>
|
||||||
|
public class MsgUpdateCharacter : NetMessage
|
||||||
|
{
|
||||||
|
#region REQUIRED
|
||||||
|
|
||||||
|
public const MsgGroups GROUP = MsgGroups.Command;
|
||||||
|
public const string NAME = nameof(MsgUpdateCharacter);
|
||||||
|
|
||||||
|
public MsgUpdateCharacter(INetChannel channel) : base(NAME, GROUP) { }
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
public int Slot;
|
||||||
|
public ICharacterProfile Profile;
|
||||||
|
|
||||||
|
public override void ReadFromBuffer(NetIncomingMessage buffer)
|
||||||
|
{
|
||||||
|
Slot = buffer.ReadInt32();
|
||||||
|
var serializer = IoCManager.Resolve<IRobustSerializer>();
|
||||||
|
var length = buffer.ReadVariableInt32();
|
||||||
|
using var stream = buffer.ReadAsStream(length);
|
||||||
|
Profile = serializer.Deserialize<ICharacterProfile>(stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
public override void WriteToBuffer(NetOutgoingMessage buffer)
|
||||||
|
{
|
||||||
|
buffer.Write(Slot);
|
||||||
|
var serializer = IoCManager.Resolve<IRobustSerializer>();
|
||||||
|
using (var stream = new MemoryStream())
|
||||||
|
{
|
||||||
|
serializer.Serialize(stream, Profile);
|
||||||
|
buffer.WriteVariableInt32((int) stream.Length);
|
||||||
|
stream.TryGetBuffer(out var segment);
|
||||||
|
buffer.Write(segment);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -193,7 +193,7 @@ namespace Content.Shared.Preferences
|
|||||||
_ => PreferenceUnavailableMode.StayInLobby // Invalid enum values.
|
_ => PreferenceUnavailableMode.StayInLobby // Invalid enum values.
|
||||||
};
|
};
|
||||||
|
|
||||||
var priorities = profile.JobPriorities
|
var priorities = new Dictionary<string, JobPriority>(profile.JobPriorities
|
||||||
.Where(p => prototypeManager.HasIndex<JobPrototype>(p.Key) && p.Value switch
|
.Where(p => prototypeManager.HasIndex<JobPrototype>(p.Key) && p.Value switch
|
||||||
{
|
{
|
||||||
JobPriority.Never => false, // Drop never since that's assumed default.
|
JobPriority.Never => false, // Drop never since that's assumed default.
|
||||||
@@ -201,9 +201,7 @@ namespace Content.Shared.Preferences
|
|||||||
JobPriority.Medium => true,
|
JobPriority.Medium => true,
|
||||||
JobPriority.High => true,
|
JobPriority.High => true,
|
||||||
_ => false
|
_ => false
|
||||||
})
|
}));
|
||||||
.ToDictionary(p => p.Key, p => p.Value);
|
|
||||||
|
|
||||||
|
|
||||||
var antags = profile.AntagPreferences
|
var antags = profile.AntagPreferences
|
||||||
.Where(prototypeManager.HasIndex<AntagPrototype>)
|
.Where(prototypeManager.HasIndex<AntagPrototype>)
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
|
||||||
using Robust.Shared.Serialization;
|
using Robust.Shared.Serialization;
|
||||||
|
using Robust.Shared.Utility;
|
||||||
|
|
||||||
namespace Content.Shared.Preferences
|
namespace Content.Shared.Preferences
|
||||||
{
|
{
|
||||||
@@ -13,18 +13,18 @@ namespace Content.Shared.Preferences
|
|||||||
[NetSerializable]
|
[NetSerializable]
|
||||||
public sealed class PlayerPreferences
|
public sealed class PlayerPreferences
|
||||||
{
|
{
|
||||||
private List<ICharacterProfile> _characters;
|
private Dictionary<int, ICharacterProfile> _characters;
|
||||||
|
|
||||||
public PlayerPreferences(IEnumerable<ICharacterProfile> characters, int selectedCharacterIndex)
|
public PlayerPreferences(IEnumerable<KeyValuePair<int, ICharacterProfile>> characters, int selectedCharacterIndex)
|
||||||
{
|
{
|
||||||
_characters = characters.ToList();
|
_characters = new Dictionary<int, ICharacterProfile>(characters);
|
||||||
SelectedCharacterIndex = selectedCharacterIndex;
|
SelectedCharacterIndex = selectedCharacterIndex;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// All player characters.
|
/// All player characters.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public IEnumerable<ICharacterProfile> Characters => _characters.AsEnumerable();
|
public IReadOnlyDictionary<int, ICharacterProfile> Characters => _characters;
|
||||||
|
|
||||||
public ICharacterProfile GetProfile(int index)
|
public ICharacterProfile GetProfile(int index)
|
||||||
{
|
{
|
||||||
@@ -39,7 +39,7 @@ namespace Content.Shared.Preferences
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The currently selected character.
|
/// The currently selected character.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public ICharacterProfile SelectedCharacter => Characters.ElementAtOrDefault(SelectedCharacterIndex);
|
public ICharacterProfile SelectedCharacter => Characters[SelectedCharacterIndex];
|
||||||
|
|
||||||
public int FirstEmptySlot()
|
public int FirstEmptySlot()
|
||||||
{
|
{
|
||||||
@@ -49,7 +49,7 @@ namespace Content.Shared.Preferences
|
|||||||
|
|
||||||
public int IndexOfCharacter(ICharacterProfile profile)
|
public int IndexOfCharacter(ICharacterProfile profile)
|
||||||
{
|
{
|
||||||
return _characters.FindIndex(x => x == profile);
|
return _characters.FirstOrNull(p => p.Value == profile)?.Key ?? -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool TryIndexOfCharacter(ICharacterProfile profile, out int index)
|
public bool TryIndexOfCharacter(ICharacterProfile profile, out int index)
|
||||||
|
|||||||
@@ -1,159 +0,0 @@
|
|||||||
using System.IO;
|
|
||||||
using Lidgren.Network;
|
|
||||||
using Robust.Shared.Interfaces.Network;
|
|
||||||
using Robust.Shared.Interfaces.Serialization;
|
|
||||||
using Robust.Shared.IoC;
|
|
||||||
using Robust.Shared.Network;
|
|
||||||
|
|
||||||
namespace Content.Shared.Preferences
|
|
||||||
{
|
|
||||||
public abstract class SharedPreferencesManager
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// The server sends this before the client joins the lobby.
|
|
||||||
/// </summary>
|
|
||||||
protected class MsgPreferencesAndSettings : NetMessage
|
|
||||||
{
|
|
||||||
#region REQUIRED
|
|
||||||
|
|
||||||
public const MsgGroups GROUP = MsgGroups.Command;
|
|
||||||
public const string NAME = nameof(MsgPreferencesAndSettings);
|
|
||||||
|
|
||||||
public MsgPreferencesAndSettings(INetChannel channel) : base(NAME, GROUP) { }
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
public PlayerPreferences Preferences;
|
|
||||||
public GameSettings Settings;
|
|
||||||
|
|
||||||
public override void ReadFromBuffer(NetIncomingMessage buffer)
|
|
||||||
{
|
|
||||||
var serializer = IoCManager.Resolve<IRobustSerializer>();
|
|
||||||
var length = buffer.ReadVariableInt32();
|
|
||||||
using (var stream = buffer.ReadAsStream(length))
|
|
||||||
{
|
|
||||||
serializer.DeserializeDirect(stream, out Preferences);
|
|
||||||
}
|
|
||||||
|
|
||||||
length = buffer.ReadVariableInt32();
|
|
||||||
using (var stream = buffer.ReadAsStream(length))
|
|
||||||
{
|
|
||||||
serializer.DeserializeDirect(stream, out Settings);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void WriteToBuffer(NetOutgoingMessage buffer)
|
|
||||||
{
|
|
||||||
var serializer = IoCManager.Resolve<IRobustSerializer>();
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
|
||||||
serializer.SerializeDirect(stream, Preferences);
|
|
||||||
buffer.WriteVariableInt32((int)stream.Length);
|
|
||||||
stream.TryGetBuffer(out var segment);
|
|
||||||
buffer.Write(segment);
|
|
||||||
}
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
|
||||||
serializer.SerializeDirect(stream, Settings);
|
|
||||||
buffer.WriteVariableInt32((int)stream.Length);
|
|
||||||
stream.TryGetBuffer(out var segment);
|
|
||||||
buffer.Write(segment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The client sends this to select a character slot.
|
|
||||||
/// </summary>
|
|
||||||
protected class MsgSelectCharacter : NetMessage
|
|
||||||
{
|
|
||||||
#region REQUIRED
|
|
||||||
|
|
||||||
public const MsgGroups GROUP = MsgGroups.Command;
|
|
||||||
public const string NAME = nameof(MsgSelectCharacter);
|
|
||||||
|
|
||||||
public MsgSelectCharacter(INetChannel channel) : base(NAME, GROUP) { }
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
public int SelectedCharacterIndex;
|
|
||||||
|
|
||||||
public override void ReadFromBuffer(NetIncomingMessage buffer)
|
|
||||||
{
|
|
||||||
SelectedCharacterIndex = buffer.ReadVariableInt32();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void WriteToBuffer(NetOutgoingMessage buffer)
|
|
||||||
{
|
|
||||||
buffer.WriteVariableInt32(SelectedCharacterIndex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The client sends this to update a character profile.
|
|
||||||
/// </summary>
|
|
||||||
protected class MsgUpdateCharacter : NetMessage
|
|
||||||
{
|
|
||||||
#region REQUIRED
|
|
||||||
|
|
||||||
public const MsgGroups GROUP = MsgGroups.Command;
|
|
||||||
public const string NAME = nameof(MsgUpdateCharacter);
|
|
||||||
|
|
||||||
public MsgUpdateCharacter(INetChannel channel) : base(NAME, GROUP) { }
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
public int Slot;
|
|
||||||
public ICharacterProfile Profile;
|
|
||||||
|
|
||||||
public override void ReadFromBuffer(NetIncomingMessage buffer)
|
|
||||||
{
|
|
||||||
Slot = buffer.ReadInt32();
|
|
||||||
var serializer = IoCManager.Resolve<IRobustSerializer>();
|
|
||||||
var length = buffer.ReadVariableInt32();
|
|
||||||
using var stream = buffer.ReadAsStream(length);
|
|
||||||
Profile = serializer.Deserialize<ICharacterProfile>(stream);
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void WriteToBuffer(NetOutgoingMessage buffer)
|
|
||||||
{
|
|
||||||
buffer.Write(Slot);
|
|
||||||
var serializer = IoCManager.Resolve<IRobustSerializer>();
|
|
||||||
using (var stream = new MemoryStream())
|
|
||||||
{
|
|
||||||
serializer.Serialize(stream, Profile);
|
|
||||||
buffer.WriteVariableInt32((int)stream.Length);
|
|
||||||
stream.TryGetBuffer(out var segment);
|
|
||||||
buffer.Write(segment);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// The client sends this to delete a character profile.
|
|
||||||
/// </summary>
|
|
||||||
protected class MsgDeleteCharacter : NetMessage
|
|
||||||
{
|
|
||||||
#region REQUIRED
|
|
||||||
|
|
||||||
public const MsgGroups GROUP = MsgGroups.Command;
|
|
||||||
public const string NAME = nameof(MsgDeleteCharacter);
|
|
||||||
|
|
||||||
public MsgDeleteCharacter(INetChannel channel) : base(NAME, GROUP) { }
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
public int Slot;
|
|
||||||
|
|
||||||
public override void ReadFromBuffer(NetIncomingMessage buffer)
|
|
||||||
{
|
|
||||||
Slot = buffer.ReadInt32();
|
|
||||||
}
|
|
||||||
|
|
||||||
public override void WriteToBuffer(NetOutgoingMessage buffer)
|
|
||||||
{
|
|
||||||
buffer.Write(Slot);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -68,7 +68,7 @@ namespace Content.Tests.Server.Preferences
|
|||||||
var originalProfile = CharlieCharlieson();
|
var originalProfile = CharlieCharlieson();
|
||||||
await db.InitPrefsAsync(username, originalProfile);
|
await db.InitPrefsAsync(username, originalProfile);
|
||||||
var prefs = await db.GetPlayerPreferencesAsync(username);
|
var prefs = await db.GetPlayerPreferencesAsync(username);
|
||||||
Assert.That(prefs.Characters.ElementAt(slot).MemberwiseEquals(originalProfile));
|
Assert.That(prefs.Characters.Single(p => p.Key == slot).Value.MemberwiseEquals(originalProfile));
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
@@ -81,7 +81,7 @@ namespace Content.Tests.Server.Preferences
|
|||||||
await db.SaveSelectedCharacterIndexAsync(username, 1);
|
await db.SaveSelectedCharacterIndexAsync(username, 1);
|
||||||
await db.SaveCharacterSlotAsync(username, null, 1);
|
await db.SaveCharacterSlotAsync(username, null, 1);
|
||||||
var prefs = await db.GetPlayerPreferencesAsync(username);
|
var prefs = await db.GetPlayerPreferencesAsync(username);
|
||||||
Assert.That(prefs.Characters.Skip(1).All(character => character is null));
|
Assert.That(!prefs.Characters.Any(p => p.Key != 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static NetUserId NewUserId()
|
private static NetUserId NewUserId()
|
||||||
|
|||||||
Reference in New Issue
Block a user