Skip to content

Commit

Permalink
Adding Comper support.
Browse files Browse the repository at this point in the history
  • Loading branch information
MainMemory committed Mar 3, 2015
1 parent 74bd3dd commit 29baff4
Showing 1 changed file with 55 additions and 3 deletions.
58 changes: 55 additions & 3 deletions SonLVLAPI/Compression.cs
@@ -1,13 +1,58 @@
using System;
using System.IO;
using SonicRetro.KensSharp;
using System.Runtime.InteropServices;

namespace SonicRetro.SonLVL.API
{
[System.Diagnostics.DebuggerNonUserCode]
public class Compression
{
public static byte[] Decompress(string file, CompressionType cmp)
private static class Comper
{
[DllImport("comper", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern int compress_comper(byte[] src, int src_len, out IntPtr dst);

[DllImport("comper", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern int decompress_comper(byte[] src, int src_len, out IntPtr dst);

[DllImport("comper", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
private static extern void free_buffer(IntPtr buffer);

static Comper()
{
string dir = Environment.CurrentDirectory;
Environment.CurrentDirectory = Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName);
Environment.CurrentDirectory = Path.Combine(Environment.CurrentDirectory, IntPtr.Size == 8 ? "lib64" : "lib32");
IntPtr dst;
compress_comper(new byte[2], 2, out dst);
free_buffer(dst);
Environment.CurrentDirectory = dir;
}

public static void Compress(byte[] sourceData, string destinationFilePath)
{
IntPtr dst;
int dst_len = compress_comper(sourceData, sourceData.Length, out dst);
byte[] result = new byte[dst_len];
Marshal.Copy(dst, result, 0, dst_len);
free_buffer(dst);
File.WriteAllBytes(destinationFilePath, result);
}

public static byte[] Decompress(string sourceFilePath)
{
byte[] sourceData = File.ReadAllBytes(sourceFilePath);
IntPtr dst;
int dst_len = decompress_comper(sourceData, sourceData.Length, out dst);
byte[] result = new byte[dst_len];
Marshal.Copy(dst, result, 0, dst_len);
free_buffer(dst);
return result;
}
}

public static byte[] Decompress(string file, CompressionType cmp)
{
byte[] ret = new byte[0];
try
Expand All @@ -32,6 +77,9 @@ public static byte[] Decompress(string file, CompressionType cmp)
case CompressionType.SZDD:
ret = SZDDComp.SZDDComp.Decompress(file);
break;
case CompressionType.Comper:
ret = Comper.Decompress(file);
break;
default:
throw new ArgumentOutOfRangeException("cmp", "Invalid compression type " + cmp + "!");
}
Expand All @@ -46,7 +94,7 @@ public static byte[] Decompress(string file, CompressionType cmp)

public static void Compress(byte[] file, string destination, CompressionType cmp)
{
try
try
{
switch (cmp)
{
Expand Down Expand Up @@ -86,6 +134,9 @@ public static void Compress(byte[] file, string destination, CompressionType cmp
case CompressionType.SZDD:
SZDDComp.SZDDComp.Compress(file, destination);
break;
case CompressionType.Comper:
Comper.Compress(file, destination);
break;
default:
throw new ArgumentOutOfRangeException("cmp", "Invalid compression type " + cmp + "!");
}
Expand All @@ -106,6 +157,7 @@ public enum CompressionType
KosinskiM,
Nemesis,
Enigma,
SZDD
SZDD,
Comper
}
}

0 comments on commit 29baff4

Please sign in to comment.