fix: Codegens BaseMulti, Container, and VirtualCheck (#1624)

This commit is contained in:
Kamron Batman 2023-12-02 13:00:02 -05:00 committed by GitHub
parent 4ebdc75bdf
commit e21ff0eb28
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 192 additions and 298 deletions

View file

@ -15,17 +15,15 @@
using System;
using System.Runtime.CompilerServices;
using ModernUO.Serialization;
namespace Server.Items;
[SerializationGenerator(0, false)]
public abstract partial class BaseMulti : Item
{
public BaseMulti(int itemID) : base(itemID) => Movable = false;
public BaseMulti(Serial serial) : base(serial)
{
}
[CommandProperty(AccessLevel.GameMaster)]
public override int ItemID
{
@ -156,26 +154,4 @@ public abstract partial class BaseMulti : Item
return false;
}
public override void Serialize(IGenericWriter writer)
{
base.Serialize(writer);
writer.Write(1); // version
}
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
var version = reader.ReadInt();
if (version == 0)
{
if (ItemID >= 0x4000)
{
ItemID -= 0x4000;
}
}
}
}

View file

@ -4,6 +4,7 @@ using System.IO;
using Server.Collections;
using Server.Logging;
using Server.Network;
using ModernUO.Serialization;
namespace Server.Items;
@ -13,47 +14,38 @@ public delegate int CheckItemGroup(Item a, Item b);
public delegate void ContainerSnoopHandler(Container cont, Mobile from);
[SerializationGenerator(0, false)]
public partial class Container : Item
{
private ContainerData m_ContainerData;
private int m_DropSound;
private int m_GumpID;
internal List<Item> m_Items;
private int m_MaxItems;
private int m_TotalGold;
private int m_TotalItems;
private int m_TotalWeight;
private int _version;
[SerializableField(3)]
[SerializedCommandProperty(AccessLevel.GameMaster)]
private bool _liftOverride;
[SerializableFieldSaveFlag(3)]
private bool ShouldSerializeLiftOverride() => _liftOverride;
public Container(int itemID) : base(itemID)
{
m_GumpID = -1;
m_DropSound = -1;
m_MaxItems = -1;
UpdateContainerData();
}
public Container(Serial serial) : base(serial)
{
_gumpID = -1;
_dropSound = -1;
_maxItems = -1;
}
public static ContainerSnoopHandler SnoopHandler { get; set; }
public ContainerData ContainerData
{
get
{
if (m_ContainerData == null)
{
UpdateContainerData();
}
return m_ContainerData;
}
get => m_ContainerData ?? UpdateContainerData();
set => m_ContainerData = value;
}
@ -69,52 +61,62 @@ public partial class Container : Item
if (ItemID != oldID)
{
UpdateContainerData();
m_ContainerData = null;
}
}
}
[CommandProperty(AccessLevel.GameMaster)]
public int GumpID
{
get => m_GumpID == -1 ? DefaultGumpID : m_GumpID;
set => m_GumpID = value;
}
[CommandProperty(AccessLevel.GameMaster)]
public int DropSound
{
get => m_DropSound == -1 ? DefaultDropSound : m_DropSound;
set => m_DropSound = value;
}
[EncodedInt]
[SerializableProperty(0)]
[CommandProperty(AccessLevel.GameMaster)]
public int MaxItems
{
get => m_MaxItems == -1 ? DefaultMaxItems : m_MaxItems;
get => _maxItems == -1 ? DefaultMaxItems : _maxItems;
set
{
m_MaxItems = value;
_maxItems = value;
InvalidateProperties();
this.MarkDirty();
}
}
[SerializableFieldSaveFlag(0)]
private bool ShouldSerializeMaxItems() => _maxItems != -1;
[EncodedInt]
[SerializableProperty(1)]
[CommandProperty(AccessLevel.GameMaster)]
public virtual int MaxWeight
public int GumpID
{
get
get => _gumpID == -1 ? DefaultGumpID : _gumpID;
set
{
if (Parent is Container container && container.MaxWeight == 0)
{
return 0;
}
return DefaultMaxWeight;
_gumpID = value;
this.MarkDirty();
}
}
[SerializableFieldSaveFlag(1)]
private bool ShouldSerializeGumpId() => _gumpID != -1;
[EncodedInt]
[SerializableProperty(2)]
[CommandProperty(AccessLevel.GameMaster)]
public bool LiftOverride { get; set; }
public int DropSound
{
get => _dropSound == -1 ? DefaultDropSound : _dropSound;
set
{
_dropSound = value;
this.MarkDirty();
}
}
[SerializableFieldSaveFlag(2)]
private bool ShouldSerializeDropSound() => _dropSound != -1;
[CommandProperty(AccessLevel.GameMaster)]
public virtual int MaxWeight => Parent is Container { MaxWeight: 0 } ? 0 : DefaultMaxWeight;
public virtual Rectangle2D Bounds => ContainerData.Bounds;
@ -139,10 +141,7 @@ public partial class Container : Item
public virtual bool IsPublicContainer => false;
public virtual void UpdateContainerData()
{
ContainerData = ContainerData.GetData(ItemID);
}
public virtual ContainerData UpdateContainerData() => ContainerData = ContainerData.GetData(ItemID);
public virtual int GetDroppedSound(Item item)
{
@ -283,118 +282,9 @@ public partial class Container : Item
private static bool GetSaveFlag(SaveFlag flags, SaveFlag toGet) => (flags & toGet) != 0;
public override void Serialize(IGenericWriter writer)
[AfterDeserialization]
private void AfterDeserialization()
{
base.Serialize(writer);
writer.Write(2); // version
var flags = SaveFlag.None;
SetSaveFlag(ref flags, SaveFlag.MaxItems, m_MaxItems != -1);
SetSaveFlag(ref flags, SaveFlag.GumpID, m_GumpID != -1);
SetSaveFlag(ref flags, SaveFlag.DropSound, m_DropSound != -1);
SetSaveFlag(ref flags, SaveFlag.LiftOverride, LiftOverride);
writer.Write((byte)flags);
if (GetSaveFlag(flags, SaveFlag.MaxItems))
{
writer.WriteEncodedInt(m_MaxItems);
}
if (GetSaveFlag(flags, SaveFlag.GumpID))
{
writer.WriteEncodedInt(m_GumpID);
}
if (GetSaveFlag(flags, SaveFlag.DropSound))
{
writer.WriteEncodedInt(m_DropSound);
}
}
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
var version = reader.ReadInt();
switch (version)
{
case 2:
{
var flags = (SaveFlag)reader.ReadByte();
if (GetSaveFlag(flags, SaveFlag.MaxItems))
{
m_MaxItems = reader.ReadEncodedInt();
}
else
{
m_MaxItems = -1;
}
if (GetSaveFlag(flags, SaveFlag.GumpID))
{
m_GumpID = reader.ReadEncodedInt();
}
else
{
m_GumpID = -1;
}
if (GetSaveFlag(flags, SaveFlag.DropSound))
{
m_DropSound = reader.ReadEncodedInt();
}
else
{
m_DropSound = -1;
}
LiftOverride = GetSaveFlag(flags, SaveFlag.LiftOverride);
break;
}
case 1:
{
m_MaxItems = reader.ReadInt();
goto case 0;
}
case 0:
{
if (version < 1)
{
m_MaxItems = GlobalMaxItems;
}
m_GumpID = reader.ReadInt();
m_DropSound = reader.ReadInt();
if (m_GumpID == DefaultGumpID)
{
m_GumpID = -1;
}
if (m_DropSound == DefaultDropSound)
{
m_DropSound = -1;
}
if (m_MaxItems == DefaultMaxItems)
{
m_MaxItems = -1;
}
// m_Bounds = new Rectangle2D( reader.ReadPoint2D(), reader.ReadPoint2D() );
reader.ReadPoint2D();
reader.ReadPoint2D();
break;
}
}
UpdateContainerData();
}
@ -416,18 +306,24 @@ public partial class Container : Item
switch (type)
{
case TotalType.Gold:
m_TotalGold += delta;
break;
{
m_TotalGold += delta;
break;
}
case TotalType.Items:
m_TotalItems += delta;
InvalidateProperties();
break;
{
m_TotalItems += delta;
InvalidateProperties();
break;
}
case TotalType.Weight:
m_TotalWeight += delta;
InvalidateProperties();
break;
{
m_TotalWeight += delta;
InvalidateProperties();
break;
}
}
}
@ -1630,16 +1526,6 @@ public partial class Container : Item
return null;
}
[Flags]
private enum SaveFlag : byte
{
None = 0x00000000,
MaxItems = 0x00000001,
GumpID = 0x00000002,
DropSound = 0x00000004,
LiftOverride = 0x00000008
}
private struct ItemStackEntry
{
public readonly Item m_StackItem;

View file

@ -13,12 +13,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
*************************************************************************/
using ModernUO.Serialization;
using Server.Gumps;
using Server.Network;
namespace Server.Items;
public sealed class VirtualCheck : Item
[SerializationGenerator(0, false)]
public sealed partial class VirtualCheck : Item
{
public static bool UseEditGump { get; private set; }
@ -27,9 +29,9 @@ public sealed class VirtualCheck : Item
UseEditGump = ServerConfiguration.GetSetting("virtualChecks.useEditGump", Core.TOL);
}
private int m_Gold;
private int _gold;
private int m_Plat;
private int _plat;
public VirtualCheck(int plat = 0, int gold = 0) : base(0x14F0)
{
@ -39,11 +41,6 @@ public sealed class VirtualCheck : Item
Movable = false;
}
public VirtualCheck(Serial serial)
: base(serial)
{
}
public override bool IsVirtualItem => true;
public override bool DisplayWeight => false;
@ -58,10 +55,10 @@ public sealed class VirtualCheck : Item
[CommandProperty(AccessLevel.Administrator)]
public int Plat
{
get => m_Plat;
get => _plat;
set
{
m_Plat = value;
_plat = value;
InvalidateProperties();
}
}
@ -69,10 +66,10 @@ public sealed class VirtualCheck : Item
[CommandProperty(AccessLevel.Administrator)]
public int Gold
{
get => m_Gold;
get => _gold;
set
{
m_Gold = value;
_gold = value;
InvalidateProperties();
}
}
@ -159,11 +156,8 @@ public sealed class VirtualCheck : Item
}
}
public override void Serialize(IGenericWriter writer)
{
}
public override void Deserialize(IGenericReader reader)
[AfterDeserialization(false)]
private void AfterDeserialization()
{
Delete();
}
@ -179,15 +173,15 @@ public sealed class VirtualCheck : Item
AllGold
}
private int m_Plat, m_Gold;
private int _plat, _gold;
public EditGump(Mobile user, VirtualCheck check) : base(50, 50)
{
User = user;
Check = check;
m_Plat = Check.Plat;
m_Gold = Check.Gold;
_plat = Check.Plat;
_gold = Check.Gold;
Closable = true;
Disposable = true;
@ -289,7 +283,7 @@ public sealed class VirtualCheck : Item
AddBackground(210, 60, 175, 20, 9300);
AddBackground(215, 45, 165, 30, 9350);
AddTextEntry(225, 50, 145, 20, 0, 0, m_Plat.ToString(), User.Account.TotalPlat.ToString().Length);
AddTextEntry(225, 50, 145, 20, 0, 0, _plat.ToString(), User.Account.TotalPlat.ToString().Length);
// Gold Row
AddBackground(15, 100, 175, 20, 9300);
@ -301,7 +295,7 @@ public sealed class VirtualCheck : Item
AddBackground(210, 100, 175, 20, 9300);
AddBackground(215, 85, 165, 30, 9350);
AddTextEntry(225, 90, 145, 20, 0, 1, m_Gold.ToString(), User.Account.TotalGold.ToString().Length);
AddTextEntry(225, 90, 145, 20, 0, 1, _gold.ToString(), User.Account.TotalGold.ToString().Length);
// Buttons
AddButton(20, 128, 12006, 12007, (int)Buttons.Close);
@ -317,29 +311,28 @@ public sealed class VirtualCheck : Item
return;
}
bool refresh = false, updated = false;
var refresh = false;
var updated = false;
switch ((Buttons)info.ButtonID)
{
case Buttons.Close:
break;
case Buttons.Clear:
{
m_Plat = m_Gold = 0;
_plat = _gold = 0;
refresh = true;
break;
}
break;
case Buttons.Accept:
{
var platText = info.GetTextEntry(0).Text;
var goldText = info.GetTextEntry(1).Text;
if (!int.TryParse(platText, out m_Plat))
if (!int.TryParse(platText, out _plat))
{
User.SendMessage("That is not a valid amount of platinum.");
refresh = true;
}
else if (!int.TryParse(goldText, out m_Gold))
else if (!int.TryParse(goldText, out _gold))
{
User.SendMessage("That is not a valid amount of gold.");
refresh = true;
@ -349,34 +342,34 @@ public sealed class VirtualCheck : Item
var totalPlat = User.Account.TotalPlat;
var totalGold = User.Account.TotalGold;
if (totalPlat < m_Plat || totalGold < m_Gold)
if (totalPlat < _plat || totalGold < _gold)
{
m_Plat = User.Account.TotalPlat;
m_Gold = User.Account.TotalGold;
_plat = User.Account.TotalPlat;
_gold = User.Account.TotalGold;
User.SendMessage("You do not have that much currency.");
refresh = true;
}
else
{
Check.Plat = m_Plat;
Check.Gold = m_Gold;
Check.Plat = _plat;
Check.Gold = _gold;
updated = true;
}
}
break;
}
break;
case Buttons.AllPlat:
{
m_Plat = User.Account.TotalPlat;
_plat = User.Account.TotalPlat;
refresh = true;
break;
}
break;
case Buttons.AllGold:
{
m_Gold = User.Account.TotalGold;
_gold = User.Account.TotalGold;
refresh = true;
break;
}
break;
}
if (updated)

View file

@ -54,46 +54,63 @@ public static class JsonUtilities
switch(je.ValueKind)
{
case JsonValueKind.Object:
writer.WriteStartObject();
// TODO: This is slow, can make it faster?
foreach (JsonProperty x in je.EnumerateObject().OrderBy(prop => prop.Name))
{
writer.WritePropertyName(x.Name);
WriteJsonElementSorted(x.Value, writer);
}
writer.WriteStartObject();
writer.WriteEndObject();
break;
// TODO: This is slow, can make it faster?
foreach (JsonProperty x in je.EnumerateObject().OrderBy(prop => prop.Name))
{
writer.WritePropertyName(x.Name);
WriteJsonElementSorted(x.Value, writer);
}
writer.WriteEndObject();
break;
}
case JsonValueKind.Array:
writer.WriteStartArray();
foreach(JsonElement x in je.EnumerateArray())
{
WriteJsonElementSorted(x, writer);
writer.WriteStartArray();
foreach(JsonElement x in je.EnumerateArray())
{
WriteJsonElementSorted(x, writer);
}
writer.WriteEndArray();
break;
}
writer.WriteEndArray();
break;
case JsonValueKind.Number:
writer.WriteNumberValue(je.GetDouble());
break;
{
writer.WriteNumberValue(je.GetDouble());
break;
}
case JsonValueKind.String:
// Escape the string
writer.WriteStringValue(je.GetString());
break;
{
// Escape the string
writer.WriteStringValue(je.GetString());
break;
}
case JsonValueKind.Null:
writer.WriteNullValue();
break;
{
writer.WriteNullValue();
break;
}
case JsonValueKind.True:
writer.WriteBooleanValue(true);
break;
{
writer.WriteBooleanValue(true);
break;
}
case JsonValueKind.False:
writer.WriteBooleanValue(false);
break;
{
writer.WriteBooleanValue(false);
break;
}
case JsonValueKind.Undefined: // Don't write anything
break;
{
break;
}
default:
throw new NotImplementedException($"Kind: {je.ValueKind}");
{
throw new NotImplementedException($"Kind: {je.ValueKind}");
}
}
}
}

View file

@ -61,33 +61,49 @@ public static class Movement
switch (d & Direction.Mask)
{
case Direction.North:
--y;
break;
{
--y;
break;
}
case Direction.South:
++y;
break;
{
++y;
break;
}
case Direction.West:
--x;
break;
{
--x;
break;
}
case Direction.East:
++x;
break;
{
++x;
break;
}
case Direction.Right:
++x;
--y;
break;
{
++x;
--y;
break;
}
case Direction.Left:
--x;
++y;
break;
{
--x;
++y;
break;
}
case Direction.Down:
++x;
++y;
break;
{
++x;
++y;
break;
}
case Direction.Up:
--x;
--y;
break;
{
--x;
--y;
break;
}
}
}
}

View file

@ -280,7 +280,10 @@ public class GenericEntityPersistence<T> : Persistence, IGenericEntityPersistenc
{
switch (World.WorldState)
{
default: return null;
default:
{
return null;
}
case WorldState.Loading:
case WorldState.Saving:
case WorldState.WritingSave:

View file

@ -133,7 +133,10 @@ public interface IGenericWriter
{
switch (sizeof(T))
{
default: throw new ArgumentException($"Argument of type {typeof(T)} is not a normal enum");
default:
{
throw new ArgumentException($"Argument of type {typeof(T)} is not a normal enum");
}
case 1:
{
Write(*(byte*)&value);

View file

@ -38,7 +38,7 @@
<PackageReference Include="Zlib.Bindings" Version="1.11.0" />
<PackageReference Include="ModernUO.Serialization.Annotations" Version="2.9.1" />
<PackageReference Include="ModernUO.Serialization.Generator" Version="2.9.1" />
<PackageReference Include="ModernUO.Serialization.Generator" Version="2.9.2" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="Migrations/*.v*.json" />

View file

@ -40,7 +40,7 @@
<PackageReference Include="Zstd.Binaries" Version="1.6.0" />
<PackageReference Include="ModernUO.Serialization.Annotations" Version="2.9.1" />
<PackageReference Include="ModernUO.Serialization.Generator" Version="2.9.1" />
<PackageReference Include="ModernUO.Serialization.Generator" Version="2.9.2" />
</ItemGroup>
<ItemGroup>
<AdditionalFiles Include="Migrations/*.v*.json" />