Skip to content

Commit

Permalink
+ use new zlib span interface
Browse files Browse the repository at this point in the history
  • Loading branch information
andreakarasho committed Aug 18, 2024
1 parent f4688cd commit a791421
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 264 deletions.
36 changes: 8 additions & 28 deletions src/ClassicUO.Assets/AnimationsLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -833,21 +833,11 @@ 41F80000 anim 692
}

var span = buffer.AsSpan(0, entry.DecompressedLength);
fixed (byte* destPtr = span)
{
var result = ZLib.Decompress(
reader.PositionAddress,
entry.Length,
0,
(IntPtr)destPtr,
entry.DecompressedLength
);

if (result != ZLib.ZLibError.Okay)
{
Log.Error($"error reading animationsequence {result}");
return;
}
var result = ZLib.Decompress(reader.Buffer.Slice(reader.Position, entry.Length), span);
if (result != ZLib.ZLibError.Okay)
{
continue;
}

var zlibReader = new StackDataReader(span);
Expand Down Expand Up @@ -1254,22 +1244,12 @@ AnimationsLoader.AnimIdxBlock index
_decompressedData = new byte[index.Unknown];
}

fixed (byte* ptr = _decompressedData.AsSpan())
var result = ZLib.Decompress(reader.Buffer.Slice(reader.Position, (int)index.Size), _decompressedData.AsSpan(0, (int)index.Unknown));
if (result != ZLib.ZLibError.Okay)
{
var result = ZLib.Decompress(
reader.PositionAddress,
(int)index.Size,
0,
(IntPtr)ptr,
(int)index.Unknown
);

if (result != ZLib.ZLibError.Okay)
{
Log.Error($"error reading uop animation. AnimID: {animID} | Group: {animGroup} | Dir: {direction} | FileIndex: {fileIndex}");
Log.Error($"error reading uop animation. AnimID: {animID} | Group: {animGroup} | Dir: {direction} | FileIndex: {fileIndex}");

return Span<FrameInfo>.Empty;
}
return Span<FrameInfo>.Empty;
}

var zlibReader = new StackDataReader(
Expand Down
12 changes: 3 additions & 9 deletions src/ClassicUO.Assets/GumpsLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +162,10 @@ public GumpInfo GetGump(uint index)
{
var dbuf = new byte[entry.DecompressedLength];

unsafe
var result = ZLib.Decompress(reader.Buffer.Slice(reader.Position, entry.Length), dbuf);
if (result != ZLib.ZLibError.Okay)
{
fixed (byte* dstPtr = dbuf)
{
var result = ZLib.Decompress(reader.PositionAddress, entry.Length, 0, (IntPtr)dstPtr, dbuf.Length);
if (result != ZLib.ZLibError.Okay)
{
return default;
}
}
return default;
}

var output = entry.CompressionFlag == CompressionType.ZlibBwt ? BwtDecompress.Decompress(dbuf) : dbuf;
Expand Down
4 changes: 2 additions & 2 deletions src/ClassicUO.Assets/MultiLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public override unsafe Task Load()
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public ref struct MultiBlock
public struct MultiBlock
{
public ushort ID;
public short X;
Expand All @@ -96,7 +96,7 @@ public ref struct MultiBlock
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
public ref struct MultiBlockNew
public struct MultiBlockNew
{
public ushort ID;
public short X;
Expand Down
163 changes: 75 additions & 88 deletions src/ClassicUO.Client/Game/GameObjects/Item.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
using ClassicUO.Utility.Logging;
using ClassicUO.Utility.Platforms;
using Microsoft.Xna.Framework;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace ClassicUO.Game.GameObjects
{
Expand Down Expand Up @@ -284,89 +286,74 @@ private unsafe void LoadMulti()

try
{
fixed (byte* dataPtr = span)
{
ZLib.Decompress(
reader.PositionAddress,
entry.Length,
0,
(IntPtr)dataPtr,
entry.DecompressedLength
);
var result = ZLib.Decompress(reader.Buffer.Slice(reader.Position, entry.Length), span.Slice(0, entry.DecompressedLength));

var zlibReader = new StackDataReader(
span.Slice(0, entry.DecompressedLength)
);
zlibReader.Skip(4);
var zlibReader = new StackDataReader(span.Slice(0, entry.DecompressedLength));
zlibReader.Skip(4);

int count = zlibReader.ReadInt32LE();
var count = zlibReader.ReadInt32LE();

int sizeOf = sizeof(MultiBlockNew);
for (int i = 0; i < count; i++)
{
ref var block = ref Unsafe.AsRef<MultiBlockNew>((zlibReader.PositionAddress + i * Unsafe.SizeOf<MultiBlockNew>()).ToPointer());

for (int i = 0; i < count; i++)
if (block.Unknown != 0)
{
MultiBlockNew* block = (MultiBlockNew*)(
zlibReader.PositionAddress + i * sizeOf
);
zlibReader.Skip((int)(block.Unknown * 4));
}

if (block->Unknown != 0)
{
zlibReader.Skip((int)(block->Unknown * 4));
}
if (block.X < minX)
{
minX = block.X;
}

if (block->X < minX)
{
minX = block->X;
}
if (block.X > maxX)
{
maxX = block.X;
}

if (block->X > maxX)
{
maxX = block->X;
}
if (block.Y < minY)
{
minY = block.Y;
}

if (block->Y < minY)
{
minY = block->Y;
}
if (block.Y > maxY)
{
maxY = block.Y;
}

if (block->Y > maxY)
{
maxY = block->Y;
}
if (block.Flags == 0 || block.Flags == 0x100)
{
Multi m = Multi.Create(World, block.ID);
m.MultiOffsetX = block.X;
m.MultiOffsetY = block.Y;
m.MultiOffsetZ = block.Z;
m.Hue = Hue;
m.AlphaHue = 255;
m.IsCustom = false;
m.State = CUSTOM_HOUSE_MULTI_OBJECT_FLAGS.CHMOF_DONT_REMOVE;
m.IsMovable = ItemData.IsMultiMovable;

m.SetInWorldTile(
(ushort)(X + block.X),
(ushort)(Y + block.Y),
(sbyte)(Z + block.Z)
);

if (block->Flags == 0 || block->Flags == 0x100)
{
Multi m = Multi.Create(World, block->ID);
m.MultiOffsetX = block->X;
m.MultiOffsetY = block->Y;
m.MultiOffsetZ = block->Z;
m.Hue = Hue;
m.AlphaHue = 255;
m.IsCustom = false;
m.State = CUSTOM_HOUSE_MULTI_OBJECT_FLAGS.CHMOF_DONT_REMOVE;
m.IsMovable = ItemData.IsMultiMovable;

m.SetInWorldTile(
(ushort)(X + block->X),
(ushort)(Y + block->Y),
(sbyte)(Z + block->Z)
);

house.Components.Add(m);

if (m.ItemData.IsMultiMovable)
{
movable = true;
}
}
else if (i == 0)
house.Components.Add(m);

if (m.ItemData.IsMultiMovable)
{
MultiGraphic = block->ID;
movable = true;
}
}

zlibReader.Release();
else if (i == 0)
{
MultiGraphic = block.ID;
}
}

zlibReader.Release();
}
finally
{
Expand All @@ -386,48 +373,48 @@ private unsafe void LoadMulti()
int count = entry.Length / Client.Game.UO.FileManager.Multis.Offset;
reader.Seek(entry.Offset);

var blocks = MemoryMarshal.Cast<byte, MultiBlock>(reader.Buffer.Slice(reader.Position, count * Unsafe.SizeOf<MultiBlock>()));

for (int i = 0; i < count; i++)
{
MultiBlock* block = (MultiBlock*)(
reader.PositionAddress + i * Client.Game.UO.FileManager.Multis.Offset
);
ref readonly var block = ref blocks[i];

if (block->X < minX)
if (block.X < minX)
{
minX = block->X;
minX = block.X;
}

if (block->X > maxX)
if (block.X > maxX)
{
maxX = block->X;
maxX = block.X;
}

if (block->Y < minY)
if (block.Y < minY)
{
minY = block->Y;
minY = block.Y;
}

if (block->Y > maxY)
if (block.Y > maxY)
{
maxY = block->Y;
maxY = block.Y;
}

if (block->Flags != 0)
if (block.Flags != 0)
{
Multi m = Multi.Create(World, block->ID);
m.MultiOffsetX = block->X;
m.MultiOffsetY = block->Y;
m.MultiOffsetZ = block->Z;
Multi m = Multi.Create(World, block.ID);
m.MultiOffsetX = block.X;
m.MultiOffsetY = block.Y;
m.MultiOffsetZ = block.Z;
m.Hue = Hue;
m.AlphaHue = 255;
m.IsCustom = false;
m.State = CUSTOM_HOUSE_MULTI_OBJECT_FLAGS.CHMOF_DONT_REMOVE;
m.IsMovable = ItemData.IsMultiMovable;

m.SetInWorldTile(
(ushort)(X + block->X),
(ushort)(Y + block->Y),
(sbyte)(Z + block->Z)
(ushort)(X + block.X),
(ushort)(Y + block.Y),
(sbyte)(Z + block.Z)
);

house.Components.Add(m);
Expand All @@ -439,7 +426,7 @@ private unsafe void LoadMulti()
}
else if (i == 0)
{
MultiGraphic = block->ID;
MultiGraphic = block.ID;
}
}
}
Expand Down
Loading

0 comments on commit a791421

Please sign in to comment.