diff --git a/.config/dotnet-tools.json b/.config/dotnet-tools.json index 524152c71..2074240b9 100644 --- a/.config/dotnet-tools.json +++ b/.config/dotnet-tools.json @@ -3,7 +3,7 @@ "isRoot": true, "tools": { "modernuoschemagenerator": { - "version": "2.0.3", + "version": "2.0.4", "commands": [ "ModernUOSchemaGenerator" ] diff --git a/Projects/Server/Mobiles/Mobile.cs b/Projects/Server/Mobiles/Mobile.cs index b348f1041..91737ff4f 100644 --- a/Projects/Server/Mobiles/Mobile.cs +++ b/Projects/Server/Mobiles/Mobile.cs @@ -2571,8 +2571,6 @@ namespace Server writer.Write(DisarmReady); writer.Write(StunReady); - // Poison.Serialize( m_Poison, writer ); - writer.Write(m_StatCap); writer.Write(NameHue); @@ -6386,7 +6384,7 @@ namespace Server { if (version <= 25) { - Poison.Deserialize(reader); + reader.ReadPoison(); } goto case 3; diff --git a/Projects/Server/Poison.cs b/Projects/Server/Poison.cs index e9ddee248..f309370a5 100644 --- a/Projects/Server/Poison.cs +++ b/Projects/Server/Poison.cs @@ -76,35 +76,5 @@ namespace Server return null; } - - public static void Serialize(Poison p, IGenericWriter writer) - { - if (p == null) - { - writer.Write((byte)0); - } - else - { - writer.Write((byte)1); - writer.Write((byte)p.Level); - } - } - - public static Poison Deserialize(IGenericReader reader) - { - switch (reader.ReadByte()) - { - case 1: return GetPoison(reader.ReadByte()); - case 2: - // no longer used, safe to remove? - reader.ReadInt(); - reader.ReadDouble(); - reader.ReadInt(); - reader.ReadTimeSpan(); - break; - } - - return null; - } } } diff --git a/Projects/Server/Serialization/IRawSerializable.cs b/Projects/Server/Serialization/IRawSerializable.cs deleted file mode 100644 index 731b56bd0..000000000 --- a/Projects/Server/Serialization/IRawSerializable.cs +++ /dev/null @@ -1,23 +0,0 @@ -/************************************************************************* - * ModernUO * - * Copyright 2019-2021 - ModernUO Development Team * - * Email: hi@modernuo.com * - * File: IRawSerializable.cs * - * * - * This program is free software: you can redistribute it and/or modify * - * it under the terms of the GNU General Public License as published by * - * the Free Software Foundation, either version 3 of the License, or * - * (at your option) any later version. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program. If not, see . * - *************************************************************************/ - -namespace Server -{ - public interface IRawSerializable - { - void Deserialize(IGenericReader reader); - void Serialize(IGenericWriter writer); - } -} diff --git a/Projects/Server/Serialization/SerializationExtensions.cs b/Projects/Server/Serialization/SerializationExtensions.cs new file mode 100644 index 000000000..a9d6d94a2 --- /dev/null +++ b/Projects/Server/Serialization/SerializationExtensions.cs @@ -0,0 +1,118 @@ +using System; +using System.Collections.Generic; +using Server.Guilds; + +namespace Server; + +public static class SerializationExtensions +{ + public static T ReadEntity(this IGenericReader reader) where T : class, ISerializable + { + Serial serial = reader.ReadSerial(); + var typeT = typeof(T); + + T entity; + + // Add to this list when creating new serializable types + if (typeof(BaseGuild).IsAssignableFrom(typeT)) + { + entity = World.FindGuild(serial) as T; + // If we check for `entity.Deleted` here during deserialization then all guilds are deleted because + // Deleted -> Disbanded -> No leader, which is the case before deserialization. + // TODO: Use a deleted flag instead, and actively check for dibanded guilds properly. + } + else + { + entity = World.FindEntity(serial) as T; + if (entity?.Deleted == false) + { + return entity; + } + } + + return entity?.Created <= reader.LastSerialized ? entity : null; + } + + public static List ReadEntityList(this IGenericReader reader) where T : class, ISerializable + { + var count = reader.ReadInt(); + + var list = new List(count); + + for (var i = 0; i < count; ++i) + { + var entity = reader.ReadEntity(); + if (entity != null) + { + list.Add(entity); + } + } + + return list; + } + + public static HashSet ReadEntitySet(this IGenericReader reader) where T : class, ISerializable + { + var count = reader.ReadInt(); + + var set = new HashSet(count); + + for (var i = 0; i < count; ++i) + { + var entity = reader.ReadEntity(); + if (entity != null) + { + set.Add(entity); + } + } + + return set; + } + + public static void Write(this IGenericWriter writer, ISerializable value) + { + writer.Write(value?.Deleted != false ? Serial.MinusOne : value.Serial); + } + + public static void Write(this IGenericWriter writer, ICollection coll) where T : class, ISerializable + { + writer.Write(coll.Count); + foreach (var entry in coll) + { + writer.Write(entry); + } + } + + public static void Write( + this IGenericWriter writer, ICollection coll, Action action + ) where T : class, ISerializable + { + if (coll == null) + { + writer.Write(0); + return; + } + + writer.Write(coll.Count); + foreach (var entry in coll) + { + action(writer, entry); + } + } + + public static void Write(this IGenericWriter writer, Poison p) + { + if (p == null) + { + writer.Write(false); + } + else + { + writer.Write(true); + writer.Write((byte)p.Level); + } + } + + public static Poison ReadPoison(this IGenericReader reader) => + reader.ReadBool() ? Poison.GetPoison(reader.ReadByte()) : null; +} diff --git a/Projects/Server/Server.csproj b/Projects/Server/Server.csproj index 68191b6ec..a77dac609 100755 --- a/Projects/Server/Server.csproj +++ b/Projects/Server/Server.csproj @@ -39,7 +39,7 @@ - + diff --git a/Projects/Server/World/World.cs b/Projects/Server/World/World.cs index 119555733..cb2606918 100644 --- a/Projects/Server/World/World.cs +++ b/Projects/Server/World/World.cs @@ -657,99 +657,5 @@ namespace Server [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void RemoveGuild(BaseGuild guild) => Guilds.Remove(guild.Serial); - - public static T ReadEntity(this IGenericReader reader) where T : class, ISerializable - { - Serial serial = reader.ReadSerial(); - var typeT = typeof(T); - - T entity; - - // Add to this list when creating new serializable types - if (typeof(BaseGuild).IsAssignableFrom(typeT)) - { - entity = FindGuild(serial) as T; - // If we check for `entity.Deleted` here during deserialization then all guilds are deleted because - // Deleted -> Disbanded -> No leader, which is the case before deserialization. - // TODO: Use a deleted flag instead, and actively check for dibanded guilds properly. - } - else - { - entity = FindEntity(serial) as T; - if (entity?.Deleted == false) - { - return entity; - } - } - - return entity?.Created <= reader.LastSerialized ? entity : null; - } - - public static List ReadEntityList(this IGenericReader reader) where T : class, ISerializable - { - var count = reader.ReadInt(); - - var list = new List(count); - - for (var i = 0; i < count; ++i) - { - var entity = reader.ReadEntity(); - if (entity != null) - { - list.Add(entity); - } - } - - return list; - } - - public static HashSet ReadEntitySet(this IGenericReader reader) where T : class, ISerializable - { - var count = reader.ReadInt(); - - var set = new HashSet(count); - - for (var i = 0; i < count; ++i) - { - var entity = reader.ReadEntity(); - if (entity != null) - { - set.Add(entity); - } - } - - return set; - } - - public static void Write(this IGenericWriter writer, ISerializable value) - { - writer.Write(value?.Deleted != false ? Serial.MinusOne : value.Serial); - } - - public static void Write(this IGenericWriter writer, ICollection coll) where T : class, ISerializable - { - writer.Write(coll.Count); - foreach (var entry in coll) - { - writer.Write(entry); - } - } - - public static void Write( - this IGenericWriter writer, ICollection coll, Action action - ) where T : class, ISerializable - { - if (coll == null) - { - writer.Write(0); - return; - } - - writer.Write(coll.Count); - foreach (var entry in coll) - { - action(writer, entry); - } - } } } diff --git a/Projects/UOContent/Items/Food/Beverage.cs b/Projects/UOContent/Items/Food/Beverage.cs index 91edd8426..49280cbe8 100644 --- a/Projects/UOContent/Items/Food/Beverage.cs +++ b/Projects/UOContent/Items/Food/Beverage.cs @@ -1101,7 +1101,7 @@ namespace Server.Items writer.Write(Poisoner); - Poison.Serialize(Poison, writer); + writer.Write(Poison); writer.Write((int)m_Content); writer.Write(m_Quantity); } @@ -1133,7 +1133,7 @@ namespace Server.Items } case 0: { - Poison = Poison.Deserialize(reader); + Poison = reader.ReadPoison(); m_Content = (BeverageType)reader.ReadInt(); m_Quantity = reader.ReadInt(); break; diff --git a/Projects/UOContent/Items/Food/Food.cs b/Projects/UOContent/Items/Food/Food.cs index 7049c0a22..b976c8e6f 100644 --- a/Projects/UOContent/Items/Food/Food.cs +++ b/Projects/UOContent/Items/Food/Food.cs @@ -142,7 +142,7 @@ namespace Server.Items writer.Write(Poisoner); - Poison.Serialize(Poison, writer); + writer.Write(Poison); writer.Write(FillFactor); } @@ -170,12 +170,12 @@ namespace Server.Items } case 2: { - Poison = Poison.Deserialize(reader); + Poison = reader.ReadPoison(); break; } case 3: { - Poison = Poison.Deserialize(reader); + Poison = reader.ReadPoison(); FillFactor = reader.ReadInt(); break; } diff --git a/Projects/UOContent/Items/Skill Items/Ninjitsu/Fukiya.cs b/Projects/UOContent/Items/Skill Items/Ninjitsu/Fukiya.cs index fbc502329..45b78a979 100644 --- a/Projects/UOContent/Items/Skill Items/Ninjitsu/Fukiya.cs +++ b/Projects/UOContent/Items/Skill Items/Ninjitsu/Fukiya.cs @@ -123,7 +123,7 @@ namespace Server.Items writer.Write(m_UsesRemaining); - Poison.Serialize(m_Poison, writer); + writer.Write(m_Poison); writer.Write(m_PoisonCharges); } @@ -139,7 +139,7 @@ namespace Server.Items { m_UsesRemaining = reader.ReadInt(); - m_Poison = Poison.Deserialize(reader); + m_Poison = reader.ReadPoison(); m_PoisonCharges = reader.ReadInt(); break; diff --git a/Projects/UOContent/Items/Skill Items/Ninjitsu/FukiyaDarts.cs b/Projects/UOContent/Items/Skill Items/Ninjitsu/FukiyaDarts.cs index 77624e823..858e9c1a7 100644 --- a/Projects/UOContent/Items/Skill Items/Ninjitsu/FukiyaDarts.cs +++ b/Projects/UOContent/Items/Skill Items/Ninjitsu/FukiyaDarts.cs @@ -93,7 +93,7 @@ namespace Server.Items writer.Write(m_UsesRemaining); - Poison.Serialize(m_Poison, writer); + writer.Write(m_Poison); writer.Write(m_PoisonCharges); } @@ -109,7 +109,7 @@ namespace Server.Items { m_UsesRemaining = reader.ReadInt(); - m_Poison = Poison.Deserialize(reader); + m_Poison = reader.ReadPoison(); m_PoisonCharges = reader.ReadInt(); break; diff --git a/Projects/UOContent/Items/Skill Items/Ninjitsu/LeatherNinjaBelt.cs b/Projects/UOContent/Items/Skill Items/Ninjitsu/LeatherNinjaBelt.cs index e93abc6e8..5e58cc004 100644 --- a/Projects/UOContent/Items/Skill Items/Ninjitsu/LeatherNinjaBelt.cs +++ b/Projects/UOContent/Items/Skill Items/Ninjitsu/LeatherNinjaBelt.cs @@ -136,7 +136,7 @@ namespace Server.Items writer.Write(m_UsesRemaining); - Poison.Serialize(m_Poison, writer); + writer.Write(m_Poison); writer.Write(m_PoisonCharges); } @@ -152,7 +152,7 @@ namespace Server.Items { m_UsesRemaining = reader.ReadInt(); - m_Poison = Poison.Deserialize(reader); + m_Poison = reader.ReadPoison(); m_PoisonCharges = reader.ReadInt(); break; diff --git a/Projects/UOContent/Items/Skill Items/Ninjitsu/Shuriken.cs b/Projects/UOContent/Items/Skill Items/Ninjitsu/Shuriken.cs index d87751222..5f07c665f 100644 --- a/Projects/UOContent/Items/Skill Items/Ninjitsu/Shuriken.cs +++ b/Projects/UOContent/Items/Skill Items/Ninjitsu/Shuriken.cs @@ -94,7 +94,7 @@ namespace Server.Items writer.Write(m_UsesRemaining); - Poison.Serialize(m_Poison, writer); + writer.Write(m_Poison); writer.Write(m_PoisonCharges); } @@ -110,7 +110,7 @@ namespace Server.Items { m_UsesRemaining = reader.ReadInt(); - m_Poison = Poison.Deserialize(reader); + m_Poison = reader.ReadPoison(); m_PoisonCharges = reader.ReadInt(); break; diff --git a/Projects/UOContent/Items/Traps/GasTrap.cs b/Projects/UOContent/Items/Traps/GasTrap.cs index 8e07d5646..656a38617 100644 --- a/Projects/UOContent/Items/Traps/GasTrap.cs +++ b/Projects/UOContent/Items/Traps/GasTrap.cs @@ -85,7 +85,7 @@ namespace Server.Items writer.Write(0); // version - Poison.Serialize(Poison, writer); + writer.Write(Poison); } public override void Deserialize(IGenericReader reader) @@ -98,7 +98,7 @@ namespace Server.Items { case 0: { - Poison = Poison.Deserialize(reader); + Poison = reader.ReadPoison(); break; } } diff --git a/Projects/UOContent/Items/Weapons/BaseWeapon.cs b/Projects/UOContent/Items/Weapons/BaseWeapon.cs index 43b45990c..ad0bf9863 100644 --- a/Projects/UOContent/Items/Weapons/BaseWeapon.cs +++ b/Projects/UOContent/Items/Weapons/BaseWeapon.cs @@ -3614,7 +3614,7 @@ namespace Server.Items if (GetSaveFlag(flags, SaveFlag.Poison)) { - Poison.Serialize(m_Poison, writer); + writer.Write(m_Poison); } if (GetSaveFlag(flags, SaveFlag.PoisonCharges)) @@ -3797,7 +3797,7 @@ namespace Server.Items if (GetSaveFlag(flags, SaveFlag.Poison)) { - m_Poison = Poison.Deserialize(reader); + m_Poison = reader.ReadPoison(); } if (GetSaveFlag(flags, SaveFlag.PoisonCharges)) @@ -4057,7 +4057,7 @@ namespace Server.Items m_Crafter = reader.ReadEntity(); - m_Poison = Poison.Deserialize(reader); + m_Poison = reader.ReadPoison(); m_PoisonCharges = reader.ReadInt(); if (m_StrReq == OldStrengthReq) diff --git a/Projects/UOContent/UOContent.csproj b/Projects/UOContent/UOContent.csproj index 90da5adac..024d9bd98 100755 --- a/Projects/UOContent/UOContent.csproj +++ b/Projects/UOContent/UOContent.csproj @@ -46,7 +46,7 @@ - +