-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCompressionDllWrapper.cs
147 lines (124 loc) · 5.12 KB
/
CompressionDllWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
namespace BMP2Tile
{
internal class CompressionDllWrapper: ICompressorImpl
{
[DllImport("kernel32.dll")]
private static extern IntPtr LoadLibrary(string dllToLoad);
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
[DllImport("kernel32.dll")]
private static extern bool FreeLibrary(IntPtr hModule);
private readonly IntPtr _hModule;
// BMP2Tile exported function signatures
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int SaveTilesDelegate(byte[] source, uint numTiles, byte[] dest, uint destLen);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate int SaveTilemapDelegate(byte[] source, uint width, uint height, byte[] dest, uint destLen);
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
private delegate IntPtr GetStringDelegate();
private readonly SaveTilesDelegate _saveTiles;
private readonly SaveTilemapDelegate _saveTilemap;
public CompressionDllWrapper(string filename)
{
_hModule = LoadLibrary(filename);
if (_hModule == IntPtr.Zero)
{
return;
}
Name = Marshal.PtrToStringAnsi(GetFunction<GetStringDelegate>("getName")());
Extension = Marshal.PtrToStringAnsi(GetFunction<GetStringDelegate>("getExt")());
_saveTiles = GetFunction<SaveTilesDelegate>("compressTiles");
_saveTilemap = GetFunction<SaveTilemapDelegate>("compressTilemap");
Capabilities = CompressorCapabilities.None;
if (_saveTilemap != null)
{
Capabilities |= CompressorCapabilities.Tilemap;
}
if (_saveTiles != null)
{
Capabilities |= CompressorCapabilities.Tiles;
}
}
public string Extension { get; }
public string Name { get; }
public CompressorCapabilities Capabilities { get; }
public IEnumerable<byte> CompressTiles(IList<Tile> tiles, bool asChunky)
{
if (_saveTiles == null)
{
throw new AppException($"{Name} compressor does not support tiles");
}
// First we convert the tiles to a buffer
var source = tiles.SelectMany(tile => tile.GetValue(asChunky)).ToArray();
return Compress(dest => _saveTiles(source, (uint)tiles.Count, dest, (uint)dest.Length));
}
public IEnumerable<byte> CompressTilemap(Tilemap tilemap)
{
if (_saveTilemap == null)
{
throw new AppException($"{Name} compressor does not support tilemaps");
}
// First we convert the tilemap to a buffer
var source = new List<byte>();
for (var y = 0; y < tilemap.Height; ++y)
for (var x = 0; x < tilemap.Width; ++x)
{
var entry = tilemap[x, y].GetValue();
// Little-endian
source.Add((byte)((entry >> 0) & 0xff));
source.Add((byte)((entry >> 8) & 0xff));
}
return Compress(dest => _saveTilemap(source.ToArray(), (uint)tilemap.Width, (uint)tilemap.Height, dest, (uint)dest.Length));
}
private static IEnumerable<byte> Compress(Func<byte[], int> compressor)
{
// Then we make a buffer to compress into... we try bigger sizes if it doesn't seem big enough
for (var bufferSize = 16 * 1024; bufferSize <= 1024 * 1024; bufferSize *= 2)
{
var dest = new byte[bufferSize];
var result = compressor(dest);
if (result < 0)
{
// Failure
break;
}
if (result > 0)
{
// Success - result is the byte length
return dest.Take(result);
}
// Else we need a bigger buffer...
}
throw new AppException("Failure compressing tiles");
}
private T GetFunction<T>(string functionName) where T: Delegate
{
var functionPointer = GetProcAddress(_hModule, functionName);
if (functionPointer == IntPtr.Zero)
{
return null;
}
return Marshal.GetDelegateForFunctionPointer<T>(functionPointer);
}
private void ReleaseUnmanagedResources()
{
if (_hModule != IntPtr.Zero)
{
FreeLibrary(_hModule);
}
}
public void Dispose()
{
ReleaseUnmanagedResources();
GC.SuppressFinalize(this);
}
~CompressionDllWrapper()
{
ReleaseUnmanagedResources();
}
}
}