source: binary-improvements/7dtd-server-fixes/src/PersistentData/Players.cs@ 495

Last change on this file since 495 was 492, checked in by alloc, 5 months ago

27_33_48

  • Hotfix for player data storage corruption on server restart
File size: 2.0 KB
RevLine 
[253]1using System;
2using System.Collections.Generic;
3using System.Text.RegularExpressions;
[492]4using Platform.EOS;
[253]5
[325]6namespace AllocsFixes.PersistentData {
[253]7 [Serializable]
8 public class Players {
[369]9 public readonly Dictionary<PlatformUserIdentifierAbs, Player> Dict = new Dictionary<PlatformUserIdentifierAbs, Player> ();
[253]10
[446]11 public int Count => Dict.Count;
[325]12
[446]13 public Player GetOrCreate (PlatformUserIdentifierAbs _internalId, PlatformUserIdentifierAbs _platformId, PlatformUserIdentifierAbs _crossPlatformId) {
14 if (_internalId == null) {
15 return null;
16 }
[325]17
[492]18 if (_internalId is UserIdentifierEos eosId) {
19 string _ = eosId.ProductUserIdString;
20 }
21
[446]22 if (Dict.TryGetValue (_internalId, out Player pOld)) {
23 return pOld;
24 }
[325]25
[455]26 Log.Out ($"Created new player entry for ID: {_internalId}");
[446]27 Player p = new Player (_internalId, _platformId, _crossPlatformId);
28 Dict.Add (_internalId, p);
29 return p;
30 }
31
32 public Player GetByInternalId (PlatformUserIdentifierAbs _internalId) {
33 if (_internalId == null) {
34 return null;
[253]35 }
[446]36
37 return Dict.TryGetValue (_internalId, out Player pOld) ? pOld : null;
[253]38 }
39
[446]40 public Player GetByUserId (PlatformUserIdentifierAbs _userId) {
41 foreach ((_, Player p) in Dict) {
42 if (p.PlatformId.Equals (_userId) || p.CrossPlatformId.Equals (_userId)) {
43 return p;
44 }
45 }
[253]46
[446]47 return null;
48 }
49
50 public Player GetByString (string _nameOrId, bool _ignoreColorCodes) {
[369]51 if (string.IsNullOrEmpty (_nameOrId)) {
[253]52 return null;
53 }
54
[369]55 if (PlatformUserIdentifierAbs.TryFromCombinedString (_nameOrId, out PlatformUserIdentifierAbs userId)) {
[446]56 return GetByUserId (userId);
[325]57 }
[253]58
[369]59 if (int.TryParse (_nameOrId, out int entityId)) {
[446]60 foreach ((_, Player p) in Dict) {
61 if (p.IsOnline && p.EntityID == entityId) {
62 return p;
[253]63 }
64 }
65 }
[325]66
[446]67 foreach ((_, Player p) in Dict) {
68 string name = p.Name;
[325]69 if (_ignoreColorCodes) {
70 name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", "");
71 }
72
[446]73 if (p.IsOnline && name.EqualsCaseInsensitive (_nameOrId)) {
74 return p;
[325]75 }
76 }
77
[253]78 return null;
79 }
80 }
[325]81}
Note: See TracBrowser for help on using the repository browser.