Skip to content

Commit

Permalink
Removed code duplication in item serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
sven-n committed Jan 2, 2025
1 parent ba5ace8 commit 75097db
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 76 deletions.
71 changes: 0 additions & 71 deletions src/GameServer/RemoteView/ItemSerializerExtended.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,6 @@ namespace MUnique.OpenMU.GameServer.RemoteView;
[MinimumClient(106, 3, ClientLanguage.Invariant)]
public class ItemSerializerExtended : IItemSerializer
{
private const byte EmptySocket = 0xFE;
private const byte BlackFenrirFlag = 0x01;
private const byte BlueFenrirFlag = 0x02;
private const byte GoldFenrirFlag = 0x04;
private const byte MaximumSocketOptions = 50;

/// <summary>
/// The socket seed index offsets, where the key is the numerical value of a <see cref="SocketSubOptionType"/>
/// and the value is the first index of this corresponding elemental seed.
/// </summary>
/// <remarks>
/// Webzen decided to put every possible socket option of each elemental seed type into one big list,
/// which may contain up to <see cref="MaximumSocketOptions"/> elements.
/// I couldn't figure out a pattern, but found these index offsets by trial and error.
/// Their list contains holes, so expect that index 9 doesn't define an option.
/// </remarks>
private static readonly byte[] SocketOptionIndexOffsets = { 0, 10, 16, 21, 29, 36 };

[Flags]
private enum OptionFlags : byte
{
Expand Down Expand Up @@ -174,59 +156,6 @@ public Item DeserializeItem(Span<byte> array, GameConfiguration gameConfiguratio
return item;
}

private static byte GetHarmonyByte(Item item)
{
byte result = 0;
var harmonyOption = item.ItemOptions.FirstOrDefault(o => o.ItemOption?.OptionType == ItemOptionTypes.HarmonyOption);
if (harmonyOption?.ItemOption is not null)
{
result = (byte)(harmonyOption.ItemOption.Number << 4);
result |= (byte)harmonyOption.Level;
}

return result;
}

private static byte GetSocketBonusByte(Item item)
{
if (item.SocketCount == 0)
{
return 0;
}

var bonusOption = item.ItemOptions.FirstOrDefault(o => o.ItemOption?.OptionType == ItemOptionTypes.SocketBonusOption);
if (bonusOption?.ItemOption != null)
{
return (byte)bonusOption.ItemOption.Number;
}

return 0xFF;
}

private static void SetSocketBytes(Span<byte> target, Item item)
{
for (int i = 0; i < item.SocketCount; i++)
{
target[i] = GetSocketByte(i);
}

byte GetSocketByte(int socketSlot)
{
var optionLink = item.ItemOptions.FirstOrDefault(o => o.ItemOption?.OptionType == ItemOptionTypes.SocketOption && o.Index == socketSlot);
if (optionLink is null)
{
return EmptySocket;
}

var sphereLevel = optionLink.Level;
var elementType = optionLink.ItemOption!.SubOptionType;
var elementOption = optionLink.ItemOption.Number;
var optionIndex = SocketOptionIndexOffsets[elementType] + elementOption;

return (byte)((sphereLevel * MaximumSocketOptions) + optionIndex);
}
}

private OptionFlags GetOptionFlags(Item item)
{
OptionFlags result = default;
Expand Down
10 changes: 5 additions & 5 deletions src/GameServer/RemoteView/ItemSerializerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public static byte GetSocketBonusByte(Item item)
/// <param name="item">The item.</param>
public static void SetSocketBytes(Span<byte> target, Item item)
{
for (int i = 0; i < MaximumSockets; i++)
{
target[i] = i < item.SocketCount ? GetSocketByte(i) : NoSocket;
}

byte GetSocketByte(int socketSlot)
{
var optionLink = item.ItemOptions.FirstOrDefault(o => o.ItemOption?.OptionType == ItemOptionTypes.SocketOption && o.Index == socketSlot);
Expand All @@ -140,11 +145,6 @@ byte GetSocketByte(int socketSlot)

return (byte)((sphereLevel * MaximumSocketOptions) + optionIndex);
}

for (int i = 0; i < MaximumSockets; i++)
{
target[i] = i < item.SocketCount ? GetSocketByte(i) : NoSocket;
}
}

/// <summary>
Expand Down

0 comments on commit 75097db

Please sign in to comment.