Skip to content

Commit

Permalink
Merge pull request #65 from seionmoya/zlib-net6.0
Browse files Browse the repository at this point in the history
Refactor zlib
  • Loading branch information
seionmoya authored Oct 7, 2024
2 parents f417a80 + 1c1a91f commit d06db11
Show file tree
Hide file tree
Showing 35 changed files with 203 additions and 6,492 deletions.
115 changes: 115 additions & 0 deletions Fuyu.Common/Compression/MemoryZlib.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using System;
using System.IO.Compression;

#if NET6_0_OR_GREATER
using System.IO;
#else
using ComponentAce.Compression.Libs.zlib;
#endif

namespace Fuyu.Common.Compression
{
public static class MemoryZlib
{
public static bool IsCompressed(byte[] data)
{
if (data == null || data.Length < 3)
{
return false;
}

// data[0]: Info (CM/CINFO) Header; must be 0x78
if (data[0] != 0x78)
{
return false;
}

// data[1]: Flags (FLG) Header; compression level.
switch (data[1])
{
case 0x01: // lowest (0-2)
case 0x5E: // low (3-4)
case 0x9C: // normal (5-6)
case 0xDA: // high (7-9)
return true;

default: // no match
return false;
}
}

#if NET6_0_OR_GREATER
// NOTE: assumes this is running inside the backend or launcher
// -- seionmoya, 2024-10-07
public static byte[] Compress(byte[] data, CompressionLevel level)
{
using (var msin = new MemoryStream(data))
{
using (var msout = new MemoryStream())
{
using (var zs = new ZLibStream(msout, level))
{
msin.CopyTo(zs);
zs.Flush();
return msout.ToArray();
}
}
}
}
#else
// NOTE: assumes this is running inside the client
// -- seionmoya, 2024-10-07
public static byte[] Compress(byte[] data, CompressionLevel level)
{
var compressLevel = 0;

switch (level)
{
case CompressionLevel.NoCompression:
throw new Exception("CompressToBytes does not support 'no compression'");

case CompressionLevel.Fastest:
compressLevel = 1;
break;

case CompressionLevel.Optimal:
compressLevel = 6;
break;

// NOTE: CompressionLevel.SmallestSize does not exist in
// .NET 5 and below.
// -- seionmoya, 2024-10-07
}

return SimpleZlib.CompressToBytes(data, data.Length, compressLevel);
}
#endif

#if NET6_0_OR_GREATER
// NOTE: assumes this is running inside the backend or launcher
// -- seionmoya, 2024-10-07
public static byte[] Decompress(byte[] data)
{

using (var msin = new MemoryStream(data))
{
using (var msout = new MemoryStream())
{
using (var zs = new ZLibStream(msin, CompressionMode.Decompress))
{
zs.CopyTo(msout);
return msout.ToArray();
}
}
}
}
#else
// NOTE: assumes this is running inside the client
// -- seionmoya, 2024-10-07
public static byte[] Decompress(byte[] data)
{
return SimpleZlib.DecompressToBytes(data);
}
#endif
}
}
4 changes: 2 additions & 2 deletions Fuyu.Common/Fuyu.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<PackageReference Include="Newtonsoft.Json" Version="13.0.3"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../Fuyu.Compression/Fuyu.Compression.csproj" />
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
<ProjectReference Include="../bsg.componentace.compression.libs.zlib/bsg.componentace.compression.libs.zlib.csproj" />
</ItemGroup>

</Project>
15 changes: 13 additions & 2 deletions Fuyu.Common/Networking/EftHttpClient.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.IO.Compression;
using System.Net.Http;
using Fuyu.Compression;
using Fuyu.Common.Compression;

namespace Fuyu.Common.Networking
{
Expand All @@ -15,7 +16,17 @@ public EftHttpClient(string address, string sessionId) : base(address)

protected override byte[] OnSendBody(byte[] body)
{
return MemoryZlib.Compress(body, CompressionLevel.BestCompression);
// NOTE: CompressionLevel.SmallestSize does not exist in
// .NET 5 and below.
// -- seionmoya, 2024-10-07

#if NET6_0_OR_GREATER
var level = CompressionLevel.SmallestSize;
#else
var level = CompressionLevel.Optimal;
#endif

return MemoryZlib.Compress(body, level);
}

protected override byte[] OnReceiveBody(byte[] body)
Expand Down
15 changes: 13 additions & 2 deletions Fuyu.Common/Networking/HttpContext.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using System.IO;
using System.IO.Compression;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using Fuyu.Compression;
using Fuyu.Common.Compression;
using Fuyu.Common.Serialization;

namespace Fuyu.Common.Networking
Expand Down Expand Up @@ -63,7 +64,17 @@ protected async Task SendAsync(byte[] data, string mime, HttpStatusCode status,

if (hasData && zipped)
{
data = MemoryZlib.Compress(data, CompressionLevel.BestCompression);
// NOTE: CompressionLevel.SmallestSize does not exist in
// .NET 5 and below.
// -- seionmoya, 2024-10-07

#if NET6_0_OR_GREATER
var level = CompressionLevel.SmallestSize;
#else
var level = CompressionLevel.Optimal;
#endif

data = MemoryZlib.Compress(data, level);
}

Response.StatusCode = (int)status;
Expand Down
100 changes: 0 additions & 100 deletions Fuyu.Compression/ComponentAce.Compression.Libs.zlib/SimpleZlib.cs

This file was deleted.

73 changes: 0 additions & 73 deletions Fuyu.Compression/CompressionLevel.cs

This file was deleted.

Loading

0 comments on commit d06db11

Please sign in to comment.