Changeset 325 for binary-improvements/7dtd-server-fixes/src/PersistentData
- Timestamp:
- Sep 4, 2018, 1:00:48 PM (6 years ago)
- Location:
- binary-improvements/7dtd-server-fixes/src/PersistentData
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
binary-improvements/7dtd-server-fixes/src/PersistentData/Attributes.cs
r273 r325 1 1 using System; 2 using System.Collections.Generic;3 using System.Runtime.Serialization;4 using System.Text.RegularExpressions;5 2 6 namespace AllocsFixes.PersistentData 7 { 3 namespace AllocsFixes.PersistentData { 8 4 [Serializable] 9 public class Attributes 10 { 5 public class Attributes { 11 6 private bool hideChatCommands; 12 7 private String hideChatCommandPrefix; 13 8 14 9 public bool HideChatCommands { 15 get { 16 return hideChatCommands; 17 } 18 set { 19 hideChatCommands = value; 20 } 10 get { return hideChatCommands; } 11 set { hideChatCommands = value; } 21 12 } 22 13 … … 26 17 hideChatCommandPrefix = ""; 27 18 } 19 28 20 return hideChatCommandPrefix; 29 21 } 30 set { 31 hideChatCommandPrefix = value; 32 } 22 set { hideChatCommandPrefix = value; } 33 23 } 34 35 24 } 36 25 } 37 -
binary-improvements/7dtd-server-fixes/src/PersistentData/InvItem.cs
r287 r325 2 2 using System.Runtime.Serialization; 3 3 4 namespace AllocsFixes.PersistentData 5 { 4 namespace AllocsFixes.PersistentData { 6 5 [Serializable] 7 6 public class InvItem { … … 26 25 } 27 26 } 28 -
binary-improvements/7dtd-server-fixes/src/PersistentData/Inventory.cs
r324 r325 3 3 4 4 namespace AllocsFixes.PersistentData { 5 6 7 8 9 5 [Serializable] 6 public class Inventory { 7 public List<InvItem> bag; 8 public List<InvItem> belt; 9 public InvItem[] equipment; 10 10 11 12 13 14 15 11 public Inventory () { 12 bag = new List<InvItem> (); 13 belt = new List<InvItem> (); 14 equipment = null; 15 } 16 16 17 18 19 20 21 22 23 24 17 public void Update (PlayerDataFile pdf) { 18 lock (this) { 19 //Log.Out ("Updating player inventory - player id: " + pdf.id); 20 ProcessInv (bag, pdf.bag, pdf.id); 21 ProcessInv (belt, pdf.inventory, pdf.id); 22 ProcessEqu (pdf.equipment, pdf.id); 23 } 24 } 25 25 26 27 28 29 30 31 32 26 private void ProcessInv (List<InvItem> target, ItemStack[] sourceFields, int id) { 27 target.Clear (); 28 for (int i = 0; i < sourceFields.Length; i++) { 29 InvItem item = CreateInvItem (sourceFields [i].itemValue, sourceFields [i].count, id); 30 if (item != null && sourceFields [i].itemValue.Modifications != null) { 31 ProcessParts (sourceFields [i].itemValue.Modifications, item, id); 32 } 33 33 34 35 36 34 target.Add (item); 35 } 36 } 37 37 38 39 40 41 42 43 38 private void ProcessEqu (Equipment sourceEquipment, int _playerId) { 39 equipment = new InvItem[sourceEquipment.GetSlotCount ()]; 40 for (int i = 0; i < sourceEquipment.GetSlotCount (); i++) { 41 equipment [i] = CreateInvItem (sourceEquipment.GetSlotItem (i), 1, _playerId); 42 } 43 } 44 44 45 46 47 48 49 50 51 45 private void ProcessParts (ItemValue[] _parts, InvItem _item, int _playerId) { 46 InvItem[] itemParts = new InvItem[_parts.Length]; 47 for (int i = 0; i < _parts.Length; i++) { 48 InvItem partItem = CreateInvItem (_parts [i], 1, _playerId); 49 if (partItem != null && _parts [i].Modifications != null) { 50 ProcessParts (_parts [i].Modifications, partItem, _playerId); 51 } 52 52 53 54 53 itemParts [i] = partItem; 54 } 55 55 56 57 56 _item.parts = itemParts; 57 } 58 58 59 60 61 62 63 59 private InvItem CreateInvItem (ItemValue _itemValue, int _count, int _playerId) { 60 if (_count > 0 && _itemValue != null && !_itemValue.Equals (ItemValue.None)) { 61 ItemClass itemClass = ItemClass.list [_itemValue.type]; 62 int maxAllowed = itemClass.Stacknumber.Value; 63 string name = itemClass.GetItemName (); 64 64 65 66 67 68 65 if (_count > maxAllowed) { 66 Log.Out ("Player with ID " + _playerId + " has stack for \"" + name + "\" greater than allowed (" + 67 _count + " > " + maxAllowed + ")"); 68 } 69 69 70 71 72 73 74 75 70 InvItem item = null; 71 if (_itemValue.HasQuality) { 72 item = new InvItem (name, _count, _itemValue.Quality, _itemValue.MaxUseTimes, _itemValue.UseTimes); 73 } else { 74 item = new InvItem (name, _count, -1, _itemValue.MaxUseTimes, _itemValue.UseTimes); 75 } 76 76 77 77 item.icon = itemClass.GetIconName (); 78 78 79 79 item.iconcolor = AllocsUtils.ColorToHex (itemClass.GetIconTint ()); 80 80 81 82 } else { 83 return null; 84 } 85 86 81 return item; 82 } 83 84 return null; 85 } 86 } 87 87 } -
binary-improvements/7dtd-server-fixes/src/PersistentData/PersistentContainer.cs
r273 r325 4 4 using System.Runtime.Serialization.Formatters.Binary; 5 5 6 namespace AllocsFixes.PersistentData 7 { 6 namespace AllocsFixes.PersistentData { 8 7 [Serializable] 9 public class PersistentContainer 10 { 8 public class PersistentContainer { 11 9 private Players players; 12 [OptionalField] 13 private Attributes attributes; 10 [OptionalField] private Attributes attributes; 14 11 15 12 public Players Players { 16 13 get { 17 if (players == null) 14 if (players == null) { 18 15 players = new Players (); 16 } 17 19 18 return players; 20 19 } 21 20 } 22 21 23 public Attributes Attributes 24 { 22 public Attributes Attributes { 25 23 get { 26 24 if (attributes == null) { 27 attributes = new Attributes ();25 attributes = new Attributes (); 28 26 } 27 29 28 return attributes; 30 29 } … … 38 37 instance = new PersistentContainer (); 39 38 } 39 40 40 return instance; 41 41 } 42 42 } 43 43 44 private PersistentContainer () 45 { 44 private PersistentContainer () { 46 45 } 47 46 48 public void Save () 49 { 47 public void Save () { 50 48 Stream stream = File.Open (GameUtils.GetSaveGameDir () + "/AllocsPeristentData.bin", FileMode.Create); 51 49 BinaryFormatter bFormatter = new BinaryFormatter (); … … 54 52 } 55 53 56 public static bool Load () 57 { 54 public static bool Load () { 58 55 if (File.Exists (GameUtils.GetSaveGameDir () + "/AllocsPeristentData.bin")) { 59 56 try { … … 61 58 Stream stream = File.Open (GameUtils.GetSaveGameDir () + "/AllocsPeristentData.bin", FileMode.Open); 62 59 BinaryFormatter bFormatter = new BinaryFormatter (); 63 obj = (PersistentContainer) bFormatter.Deserialize (stream);60 obj = (PersistentContainer) bFormatter.Deserialize (stream); 64 61 stream.Close (); 65 62 instance = obj; … … 70 67 } 71 68 } 69 72 70 return false; 73 71 } 74 75 72 } 76 73 } 77 -
binary-improvements/7dtd-server-fixes/src/PersistentData/Player.cs
r324 r325 4 4 5 5 namespace AllocsFixes.PersistentData { 6 [Serializable] 7 public class Player { 8 private readonly string steamId; 9 private int entityId; 10 private string name; 11 private string ip; 12 private long totalPlayTime; 13 14 [OptionalField] private DateTime 15 lastOnline; 16 17 private Inventory inventory; 18 19 [OptionalField] private int 20 lastPositionX, lastPositionY, lastPositionZ; 21 22 [OptionalField] [Obsolete ("experience no longer available, use level and expToNextLevel instead")] 23 private uint experience; 24 25 [OptionalField] private bool chatMuted; 26 [OptionalField] private int maxChatLength; 27 [OptionalField] private string chatColor; 28 [OptionalField] private bool chatName; 29 [OptionalField] private uint expToNextLevel; 30 [OptionalField] private int level; 31 32 [NonSerialized] private ClientInfo 33 clientInfo; 34 35 public string SteamID { 36 get { return steamId; } 37 } 6 [Serializable] 7 public class Player { 8 private readonly string steamId; 9 private int entityId; 10 private string name; 11 private string ip; 12 private long totalPlayTime; 13 14 [OptionalField] private DateTime lastOnline; 15 16 private Inventory inventory; 17 18 [OptionalField] private int lastPositionX, lastPositionY, lastPositionZ; 19 20 [OptionalField] [Obsolete ("experience no longer available, use level and expToNextLevel instead")] 21 private uint experience; 22 23 [OptionalField] private bool chatMuted; 24 [OptionalField] private int maxChatLength; 25 [OptionalField] private string chatColor; 26 [OptionalField] private bool chatName; 27 [OptionalField] private uint expToNextLevel; 28 [OptionalField] private int level; 29 30 [NonSerialized] private ClientInfo clientInfo; 31 32 public string SteamID { 33 get { return steamId; } 34 } 38 35 39 36 public int EntityID { … … 41 38 } 42 39 43 public string Name { 44 get { return name == null ? string.Empty : name; } 45 } 46 47 public string IP { 48 get { return ip == null ? string.Empty : ip; } 49 } 50 51 public Inventory Inventory { 52 get { 53 if (inventory == null) 54 inventory = new Inventory (); 55 return inventory; 56 } 57 } 58 59 public bool IsOnline { 60 get { return clientInfo != null; } 61 } 62 63 public ClientInfo ClientInfo { 64 get { return clientInfo; } 65 } 66 67 public EntityPlayer Entity { 68 get { 69 if (IsOnline) { 70 return GameManager.Instance.World.Players.dict [clientInfo.entityId]; 71 } else { 72 return null; 73 } 74 } 75 } 76 77 public long TotalPlayTime { 78 get { 79 if (IsOnline) { 80 return totalPlayTime + (long) (DateTime.Now - lastOnline).TotalSeconds; 81 } else { 82 return totalPlayTime; 83 } 84 } 85 } 86 87 public DateTime LastOnline { 88 get { 89 if (IsOnline) 90 return DateTime.Now; 91 else 92 return lastOnline; 93 } 94 } 95 96 public Vector3i LastPosition { 97 get { 98 if (IsOnline) 99 return new Vector3i (Entity.GetPosition ()); 100 else 101 return new Vector3i (lastPositionX, lastPositionY, lastPositionZ); 102 } 103 } 104 105 public bool LandProtectionActive { 106 get { 107 return GameManager.Instance.World.IsLandProtectionValidForPlayer (GameManager.Instance 108 .GetPersistentPlayerList ().GetPlayerData (SteamID)); 109 } 110 } 111 112 public float LandProtectionMultiplier { 113 get { 114 return GameManager.Instance.World.GetLandProtectionHardnessModifierForPlayer (GameManager.Instance 115 .GetPersistentPlayerList ().GetPlayerData (SteamID)); 116 } 117 } 118 119 120 [Obsolete ("Experience no longer available, use Level instead")] 121 public uint Experience { 122 get { return 0; } 123 } 124 125 public float Level { 126 get { 127 float expForNextLevel = 128 (int) Math.Min ((Progression.BaseExpToLevel * Mathf.Pow (Progression.ExpMultiplier, level + 1)), 129 int.MaxValue); 130 float fLevel = level + 1f - ((float) expToNextLevel / expForNextLevel); 131 return fLevel; 132 } 133 } 134 135 public bool IsChatMuted { 136 get { return chatMuted; } 137 set { chatMuted = value; } 138 } 139 140 public int MaxChatLength { 141 get { 142 if (maxChatLength == 0) { 143 maxChatLength = 255; 144 } 145 146 return maxChatLength; 147 } 148 set { maxChatLength = value; } 149 } 150 151 public string ChatColor { 152 get { 153 if (chatColor == null || chatColor == "") { 154 chatColor = ""; 155 } 156 157 return chatColor; 158 } 159 160 set { chatColor = value; } 161 } 162 163 public bool ChatName { 164 get { return chatName; } 165 166 set { chatName = value; } 167 } 168 169 public void SetOffline () { 170 if (clientInfo != null) { 171 Log.Out ("Player set to offline: " + steamId); 172 lastOnline = DateTime.Now; 173 try { 174 Vector3i lastPos = new Vector3i (Entity.GetPosition ()); 175 lastPositionX = lastPos.x; 176 lastPositionY = lastPos.y; 177 lastPositionZ = lastPos.z; 178 totalPlayTime += (long) (Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad); 179 } catch (NullReferenceException) { 180 Log.Out ("Entity not available. Something seems to be wrong here..."); 181 } 182 183 clientInfo = null; 184 } 185 } 186 187 public void SetOnline (ClientInfo ci) { 188 Log.Out ("Player set to online: " + steamId); 189 clientInfo = ci; 40 public string Name { 41 get { return name == null ? string.Empty : name; } 42 } 43 44 public string IP { 45 get { return ip == null ? string.Empty : ip; } 46 } 47 48 public Inventory Inventory { 49 get { 50 if (inventory == null) { 51 inventory = new Inventory (); 52 } 53 54 return inventory; 55 } 56 } 57 58 public bool IsOnline { 59 get { return clientInfo != null; } 60 } 61 62 public ClientInfo ClientInfo { 63 get { return clientInfo; } 64 } 65 66 public EntityPlayer Entity { 67 get { 68 if (IsOnline) { 69 return GameManager.Instance.World.Players.dict [clientInfo.entityId]; 70 } 71 72 return null; 73 } 74 } 75 76 public long TotalPlayTime { 77 get { 78 if (IsOnline) { 79 return totalPlayTime + (long) (DateTime.Now - lastOnline).TotalSeconds; 80 } 81 82 return totalPlayTime; 83 } 84 } 85 86 public DateTime LastOnline { 87 get { 88 if (IsOnline) { 89 return DateTime.Now; 90 } 91 92 return lastOnline; 93 } 94 } 95 96 public Vector3i LastPosition { 97 get { 98 if (IsOnline) { 99 return new Vector3i (Entity.GetPosition ()); 100 } 101 102 return new Vector3i (lastPositionX, lastPositionY, lastPositionZ); 103 } 104 } 105 106 public bool LandProtectionActive { 107 get { 108 return GameManager.Instance.World.IsLandProtectionValidForPlayer (GameManager.Instance 109 .GetPersistentPlayerList ().GetPlayerData (SteamID)); 110 } 111 } 112 113 public float LandProtectionMultiplier { 114 get { 115 return GameManager.Instance.World.GetLandProtectionHardnessModifierForPlayer (GameManager.Instance 116 .GetPersistentPlayerList ().GetPlayerData (SteamID)); 117 } 118 } 119 120 121 [Obsolete ("Experience no longer available, use Level instead")] 122 public uint Experience { 123 get { return 0; } 124 } 125 126 public float Level { 127 get { 128 float expForNextLevel = 129 (int) Math.Min (Progression.BaseExpToLevel * Mathf.Pow (Progression.ExpMultiplier, level + 1), 130 int.MaxValue); 131 float fLevel = level + 1f - expToNextLevel / expForNextLevel; 132 return fLevel; 133 } 134 } 135 136 public bool IsChatMuted { 137 get { return chatMuted; } 138 set { chatMuted = value; } 139 } 140 141 public int MaxChatLength { 142 get { 143 if (maxChatLength == 0) { 144 maxChatLength = 255; 145 } 146 147 return maxChatLength; 148 } 149 set { maxChatLength = value; } 150 } 151 152 public string ChatColor { 153 get { 154 if (chatColor == null || chatColor == "") { 155 chatColor = ""; 156 } 157 158 return chatColor; 159 } 160 161 set { chatColor = value; } 162 } 163 164 public bool ChatName { 165 get { return chatName; } 166 167 set { chatName = value; } 168 } 169 170 public Player (string steamId) { 171 this.steamId = steamId; 172 inventory = new Inventory (); 173 } 174 175 public void SetOffline () { 176 if (clientInfo != null) { 177 Log.Out ("Player set to offline: " + steamId); 178 lastOnline = DateTime.Now; 179 try { 180 Vector3i lastPos = new Vector3i (Entity.GetPosition ()); 181 lastPositionX = lastPos.x; 182 lastPositionY = lastPos.y; 183 lastPositionZ = lastPos.z; 184 totalPlayTime += (long) (Time.timeSinceLevelLoad - Entity.CreationTimeSinceLevelLoad); 185 } catch (NullReferenceException) { 186 Log.Out ("Entity not available. Something seems to be wrong here..."); 187 } 188 189 clientInfo = null; 190 } 191 } 192 193 public void SetOnline (ClientInfo ci) { 194 Log.Out ("Player set to online: " + steamId); 195 clientInfo = ci; 190 196 entityId = ci.entityId; 191 name = ci.playerName; 192 ip = ci.ip; 193 lastOnline = DateTime.Now; 194 } 195 196 public void Update (PlayerDataFile _pdf) { 197 UpdateProgression (_pdf); 198 inventory.Update (_pdf); 199 } 200 201 private void UpdateProgression (PlayerDataFile _pdf) { 202 if (_pdf.progressionData.Length > 0) { 203 using (PooledBinaryReader pbr = MemoryPools.poolBinaryReader.AllocSync (false)) { 204 pbr.SetBaseStream (_pdf.progressionData); 205 Progression p = Progression.Read (pbr, null); 206 expToNextLevel = (uint) p.ExpToNextLevel; 207 level = p.Level; 208 } 209 } 210 } 211 212 public Player (string steamId) { 213 this.steamId = steamId; 214 this.inventory = new Inventory (); 215 } 216 } 197 name = ci.playerName; 198 ip = ci.ip; 199 lastOnline = DateTime.Now; 200 } 201 202 public void Update (PlayerDataFile _pdf) { 203 UpdateProgression (_pdf); 204 inventory.Update (_pdf); 205 } 206 207 private void UpdateProgression (PlayerDataFile _pdf) { 208 if (_pdf.progressionData.Length > 0) { 209 using (PooledBinaryReader pbr = MemoryPools.poolBinaryReader.AllocSync (false)) { 210 pbr.SetBaseStream (_pdf.progressionData); 211 Progression p = Progression.Read (pbr, null); 212 expToNextLevel = (uint) p.ExpToNextLevel; 213 level = p.Level; 214 } 215 } 216 } 217 } 217 218 } -
binary-improvements/7dtd-server-fixes/src/PersistentData/Players.cs
r276 r325 1 1 using System; 2 2 using System.Collections.Generic; 3 using System.Runtime.Serialization;4 3 using System.Text.RegularExpressions; 5 4 6 namespace AllocsFixes.PersistentData 7 { 5 namespace AllocsFixes.PersistentData { 8 6 [Serializable] 9 7 public class Players { 10 private Dictionary<string, Player> players = new Dictionary<string, Player> ();8 private readonly Dictionary<string, Player> players = new Dictionary<string, Player> (); 11 9 12 10 public Player this [string steamId, bool create] { … … 14 12 if (string.IsNullOrEmpty (steamId)) { 15 13 return null; 16 } else if (players.ContainsKey (steamId)) { 14 } 15 16 if (players.ContainsKey (steamId)) { 17 17 return players [steamId]; 18 } else {19 if (create && steamId != null && steamId.Length == 17) {20 Log.Out ("Created new player entry for ID: " + steamId);21 Player p = new Player (steamId);22 players.Add (steamId, p);23 return p;24 }25 return null;26 18 } 19 20 if (create && steamId != null && steamId.Length == 17) { 21 Log.Out ("Created new player entry for ID: " + steamId); 22 Player p = new Player (steamId); 23 players.Add (steamId, p); 24 return p; 25 } 26 27 return null; 27 28 } 28 29 } … … 53 54 if (_nameOrId.Length == 17 && long.TryParse (_nameOrId, out tempLong)) { 54 55 return _nameOrId; 55 } else { 56 int entityId = -1; 57 if (int.TryParse (_nameOrId, out entityId)) { 58 foreach (KeyValuePair<string, Player> kvp in players) { 59 if (kvp.Value.IsOnline && kvp.Value.EntityID == entityId) { 60 return kvp.Key; 61 } 62 } 63 } 56 } 64 57 65 _nameOrId = _nameOrId.ToLower (); 58 int entityId = -1; 59 if (int.TryParse (_nameOrId, out entityId)) { 66 60 foreach (KeyValuePair<string, Player> kvp in players) { 67 string name = kvp.Value.Name.ToLower (); 68 if (_ignoreColorCodes) { 69 name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", ""); 70 } 71 if (kvp.Value.IsOnline && name.Equals (_nameOrId)) { 61 if (kvp.Value.IsOnline && kvp.Value.EntityID == entityId) { 72 62 return kvp.Key; 73 63 } 74 64 } 75 65 } 66 67 _nameOrId = _nameOrId.ToLower (); 68 foreach (KeyValuePair<string, Player> kvp in players) { 69 string name = kvp.Value.Name.ToLower (); 70 if (_ignoreColorCodes) { 71 name = Regex.Replace (name, "\\[[0-9a-fA-F]{6}\\]", ""); 72 } 73 74 if (kvp.Value.IsOnline && name.Equals (_nameOrId)) { 75 return kvp.Key; 76 } 77 } 78 76 79 return null; 77 80 } 78 81 } 79 82 } 80
Note:
See TracChangeset
for help on using the changeset viewer.