-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPetzThumbnailHandler.cs
232 lines (211 loc) · 10.2 KB
/
PetzThumbnailHandler.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
using AsmResolver;
using AsmResolver.IO;
using AsmResolver.PE.Win32Resources;
using Kaitai;
using SharpShell.Attributes;
using SharpShell.SharpThumbnailHandler;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using static System.Drawing.Imaging.ImageLockMode;
using static System.Drawing.Imaging.PixelFormat;
namespace PetzThumbnails
{
[ComVisible(true)]
[COMServerAssociation(AssociationType.FileExtension, ".pet")]
public class PetzThumbnailHandler : SharpThumbnailHandler
{
protected override Bitmap GetThumbnailImage(uint width)
{
var headerBytes = new byte[] { 0x42, 0x4d, 0xf6, 0x25, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x36, 0x04, 0x00, 0x00 };
var buffer = new byte[13704];
// If pet is pregnant, there will be two thumbnails (pet + baby, baby)
// If pet is not pregnant, there will only be one
// Seek back to where pet + baby thumb would be and attempt to parse
// If that fails, get the normal thumb
// Alternative would be seeking the pfm marker but this is hard with a stream
SelectedItemStream.Seek(-27412, SeekOrigin.End);
SelectedItemStream.Read(buffer, 0, 13704);
try
{
using (var mem = new MemoryStream())
{
mem.Write(headerBytes, 0, headerBytes.Length);
mem.Write(buffer, 0, 13704);
var bitmap = new Bitmap(mem);
var transcolor = bitmap.Palette.Entries[253];
bitmap.MakeTransparent(transcolor);
return width > bitmap.Width ? Helper.ResizeBitmap(bitmap) : bitmap;
}
}
catch (Exception e)
{
SelectedItemStream.Seek(-13704, SeekOrigin.End);
using (var mem = new MemoryStream())
{
mem.Write(headerBytes, 0, headerBytes.Length);
SelectedItemStream.CopyTo(mem);
var bitmap = new Bitmap(mem);
var transcolor = bitmap.Palette.Entries[253];
bitmap.MakeTransparent(transcolor);
return width > bitmap.Width ? Helper.ResizeBitmap(bitmap) : bitmap;
}
}
finally
{
SelectedItemStream.Close();
}
}
}
public class Helper
{
public static readonly Bitmap palette = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("PetzThumbnails.PALETTE.bmp"));
public static readonly Bitmap babyzpalette =
new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("PetzThumbnails.BABYZ.bmp"));
public static Bitmap GetThumbnail(byte[] bytes, string type, uint width)
{
var flhname = "restinga";
if (type == "clo")
{
flhname = "awaya";
}
var asm = AsmResolver.PE.PEImage.FromBytes(bytes);
// Weird way of doing this, but it seems like there is no way to figure it out from data
// Tinker makes you set the palette manually
// There's a flh field which seems like it might indicate palette but
// it's wrong on Babyz toys which came from Petz (e.g. catz plush)
var isBabyz = asm.Imports.Any(x => x.Name == "Babyz.exe");
var resourceTypes = asm.Resources.Entries.Where(x => x.Name == "FLM" || x.Name == "FLH");
// if we only have 1 FLH/FLM set then it's probably a lnz toy with away only
// Babyz toys look better always using the away, Petz toys look better the other way round
bool includeaway = isBabyz || type == "clo" || resourceTypes.Count() == 2;
resourceTypes = resourceTypes.SelectMany(x => (x as IResourceDirectory).Entries)
.Where(x => !x.Name.ToLower().Contains("away") || includeaway);
resourceTypes = resourceTypes.SelectMany(x => (x as IResourceDirectory).Entries);
// Prioritise "away" graphics for clothes and Babyz
BinaryStreamReader flh = resourceTypes.Where(x => x.ParentDirectory.ParentDirectory.Name == "FLH")
.OrderBy(x => (type == "clo" || isBabyz) && x.ParentDirectory.Name.ToLower().Contains("away") ? 0 : 1)
.Select(x => (x as IResourceData).CreateReader())
.First();
BinaryStreamReader flm = resourceTypes.Where(x => x.ParentDirectory.ParentDirectory.Name == "FLM")
.OrderBy(x => (type == "clo" || isBabyz) && x.ParentDirectory.Name.ToLower().Contains("away") ? 0 : 1)
.Select(x => (x as IResourceData).CreateReader()).First();
var kaitaiflh = new Flh(new KaitaiStream(flh.ReadToEnd()));
var frame = kaitaiflh.Frames.FirstOrDefault(x => x.Name.ToLower().Contains(flhname) && (x.Flags & 2) != 0);
if (frame == null)
{
frame = kaitaiflh.Frames.FirstOrDefault(x => x.Name.ToLower().Contains("awaya") && (x.Flags & 2) != 0) ??
kaitaiflh.Frames.First(x => (x.Flags & 2) != 0);
}
var bitmap = new Bitmap(frame.X2 - frame.X1, frame.Y2 - frame.Y1, Format8bppIndexed);
int bitmapWidth = bitmap.Width;
var offset = frame.Offset;
if (bitmap.Width % 4 != 0)
{
bitmapWidth = bitmap.Width + 4 - (bitmap.Width % 4);
}
int size = bitmapWidth * bitmap.Height;
flm.RelativeOffset = offset;
byte[] bmp = flm.ReadSegment((uint)size).ToArray();
var thelock = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), WriteOnly, Format8bppIndexed);
bitmap.Palette = isBabyz ? babyzpalette.Palette : palette.Palette;
Marshal.Copy(bmp, 0, thelock.Scan0, bmp.Length);
bitmap.UnlockBits(thelock);
bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
bitmap.MakeTransparent(palette.Palette.Entries[253]);
return width > bitmap.Width ? Helper.ResizeBitmap(bitmap) : bitmap;
}
public static Bitmap GetBreedThumbnailImage(Stream SelectedItemStream, uint width)
{
byte[] data = new byte[SelectedItemStream.Length];
SelectedItemStream.Read(data, 0, data.Length);
SelectedItemStream.Close();
var asm = AsmResolver.PE.PEImage.FromBytes(data);
var breedStringTable = asm.Resources.GetDirectory(ResourceType.String)
.GetDirectory(63).GetData(1033).Contents.WriteIntoArray();
string name;
using (var binaryReader =
new BinaryReader(new MemoryStream(breedStringTable.SkipWhile(x => x == 0x0).ToArray()),
Encoding.Unicode))
{
var nameLength = binaryReader.ReadInt16();
name = new string(binaryReader.ReadChars(nameLength)).ToUpper();
}
var bmpResourceDir = (IResourceDirectory)asm.Resources.Entries.Where(x => x.Name == "BMP").First();
bmpResourceDir = (IResourceDirectory)bmpResourceDir.Entries.Where(x => x.Name == name).First();
var bmpResource = (IResourceData)bmpResourceDir.Entries.First();
var bmp = bmpResource.Contents.WriteIntoArray();
using (var mem = new MemoryStream())
{
mem.Write(bmp, 0, bmp.Length);
var bitmap = new Bitmap(mem);
try
{
bitmap.MakeTransparent(bitmap.Palette.Entries[253]);
}
catch (IndexOutOfRangeException e)
{
// ok - wrong palette - don't bother making transparent
}
return width > bitmap.Width ? Helper.ResizeBitmap(bitmap) : bitmap;
}
}
public static Bitmap ResizeBitmap(Bitmap bitmap)
{
var scaledbitmap = new Bitmap(bitmap.Width * 2, bitmap.Height * 2);
using (Graphics g = Graphics.FromImage(scaledbitmap))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(bitmap, new Rectangle(Point.Empty, scaledbitmap.Size));
return scaledbitmap;
}
}
}
[ComVisible(true)]
[COMServerAssociation(AssociationType.FileExtension, ".toy")]
public class ToyThumbnailHandler : SharpThumbnailHandler
{
protected override Bitmap GetThumbnailImage(uint width)
{
byte[] data = new byte[SelectedItemStream.Length];
SelectedItemStream.Read(data, 0, data.Length);
SelectedItemStream.Close();
return Helper.GetThumbnail(data, "toy", width);
}
}
[ComVisible(true)]
[COMServerAssociation(AssociationType.FileExtension, ".clo")]
public class CloThumbnailHandler : SharpThumbnailHandler
{
protected override Bitmap GetThumbnailImage(uint width)
{
byte[] data = new byte[SelectedItemStream.Length];
SelectedItemStream.Read(data, 0, data.Length);
SelectedItemStream.Close();
return Helper.GetThumbnail(data, "clo", width);
}
}
[ComVisible(true)]
[COMServerAssociation(AssociationType.FileExtension, ".dog")]
public class DogThumbnailHandler : SharpThumbnailHandler
{
protected override Bitmap GetThumbnailImage(uint width)
{
return Helper.GetBreedThumbnailImage(SelectedItemStream, width);
}
}
[ComVisible(true)]
[COMServerAssociation(AssociationType.FileExtension, ".cat")]
public class CatThumbnailHandler : SharpThumbnailHandler
{
protected override Bitmap GetThumbnailImage(uint width)
{
return Helper.GetBreedThumbnailImage(SelectedItemStream, width);
}
}
}