-
Notifications
You must be signed in to change notification settings - Fork 25
/
FFIcon.cs
255 lines (191 loc) · 7.25 KB
/
FFIcon.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Windows.Forms;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using PlayOnline.FFXI;
using System.Runtime.InteropServices;
namespace FFXI_IconChange
{
/// <summary>
/// Summary description for FFIcon.
/// </summary>
public class FFIcon
{
//Entire Icon Object
public static int SIZE_ICON = 0x1800;
//The Text Portion (Description, Help Text, etc)
public static int OFFSET_TEXT = 0x0;
public static int SIZE_TEXT = 0x280;
//The Image Portion (Image Header and raw data)
public static int OFFSET_IMAGE = 0x280;
public static int SIZE_IMAGE = 0x1580;
//Individual Fields below:
private static int OFFSET_ID = 0x0;
private static int SIZE_ID = 0x2;
private static int OFFSET_DESCR = 0x2C;
private static int SIZE_DESCR = 0x100;
private static int OFFSET_DESCR_2 = 0x22;
private static int SIZE_DESCR_2 = 0x20;
private static int OFFSET_HELPTEXT = 0x42;
private static int SIZE_HELPTEXT = 0x80;
private static int OFFSET_HELPTEXT_2 = 0xC2;
private static int SIZE_HELPTEXT_2 = 0x80;
private static int OFFSET_UNKNOWN = 0x142;
private static int SIZE_UNKNOWN = 0x143;
private static int OFFSET_IMAGE_NAME = 0x285;
private static int SIZE_IMAGE_NAME = 0x10;
private static int OFFSET_UNKNOWN_2 = 0x295;
private static int SIZE_UNKNOWN_2 = 0x28;
private static int OFFSET_IMAGE_DATA = 0x2BD;
private static int SIZE_IMAGE_DATA = 0x1000;
private static int SIZE_IMAGE_DATA_SMALL = 0x400;
//Hopefully the total of the field sizes = SIZE_ICON (0x1800)
byte[] mBytes; // Store all the bytes here
public FFIcon()
{
mBytes = new byte[SIZE_ICON];
}
public FFIcon(byte[] input)
{
mBytes = new byte[SIZE_ICON];
input.CopyTo(mBytes,0);
}
/// <summary>
/// This method was mostly copied from the Internet. It works, but I do not fully understand
/// the inner working of the Data Marshal. Perhaps there is a better way to Implement this method.
///
/// Also, this assumes that the Image is stored as a 32-bit RGBA image. Some of the Icons use 8-bit BMP, so those images are returned as garbage.
/// </summary>
public Bitmap Bitmap
{
get
{
Bitmap mIcon;
mIcon = new Bitmap(32, 32, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
BitmapData data = mIcon.LockBits(new System.Drawing.Rectangle(0, 0, mIcon.Width, mIcon.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, mIcon.PixelFormat);
IntPtr ptr = data.Scan0;
System.Runtime.InteropServices.Marshal.Copy(mBytes, OFFSET_IMAGE_DATA, ptr, SIZE_IMAGE_DATA);
mIcon.UnlockBits(data);
mIcon.RotateFlip(RotateFlipType.RotateNoneFlipY); //For some reason, the Image comes out upside-down, so flip it here
return mIcon;
}
}
public Bitmap SmallBitmap
{
get
{
Bitmap mIcon;
mIcon = new Bitmap(16, 16, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
BitmapData data = mIcon.LockBits(new System.Drawing.Rectangle(0, 0, mIcon.Width, mIcon.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, mIcon.PixelFormat);
IntPtr ptr = data.Scan0;
System.Runtime.InteropServices.Marshal.Copy(mBytes, OFFSET_IMAGE_DATA, ptr, SIZE_IMAGE_DATA_SMALL);
mIcon.UnlockBits(data);
mIcon.RotateFlip(RotateFlipType.RotateNoneFlipY); //For some reason, the Image comes out upside-down, so flip it here
return mIcon;
}
}
public String Description
{
get
{
byte[] temp = (byte[])mBytes.Clone();
FFXIEncryption.DecodeDataBlock(temp);
FFXIEncoding e = new FFXIEncoding();
return e.GetString(temp, OFFSET_DESCR, SIZE_DESCR).Trim("\0".ToCharArray());
}
}
public String Description_2
{
get
{
byte[] temp = (byte[])mBytes.Clone();
FFXIEncryption.DecodeDataBlock(temp);
FFXIEncoding e = new FFXIEncoding();
return e.GetString(temp, OFFSET_DESCR_2, SIZE_DESCR_2).Trim("\0".ToCharArray());
}
}
public String HelpText
{
get
{
byte[] temp = (byte[])mBytes.Clone();
FFXIEncryption.DecodeDataBlock(temp);
FFXIEncoding e = new FFXIEncoding();
return e.GetString(temp, OFFSET_HELPTEXT, SIZE_HELPTEXT).Trim("\0".ToCharArray());
}
}
public String HelpText_2
{
get
{
byte[] temp = (byte[])mBytes.Clone();
FFXIEncryption.DecodeDataBlock(temp);
FFXIEncoding e = new FFXIEncoding();
return e.GetString(temp, OFFSET_HELPTEXT_2, SIZE_HELPTEXT_2).Trim("\0".ToCharArray());
}
}
public String Name
{
get
{
FFXIEncoding e = new FFXIEncoding();
return e.GetString(mBytes, OFFSET_IMAGE_NAME, SIZE_IMAGE_NAME);
}
}
public String ID
{
get
{
FFXIEncoding e = new FFXIEncoding();
return e.GetString(mBytes, OFFSET_ID, SIZE_ID);
}
}
public void InjectBMP(Bitmap bmp)
{
bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp.PixelFormat);
IntPtr ptr = data.Scan0;
System.Runtime.InteropServices.Marshal.Copy(ptr, mBytes, OFFSET_IMAGE_DATA, bmp.Width * bmp.Height * 4);
bmp.UnlockBits(data);
//A bit of a hack, we are saying that this is a 32 bit BMP image
mBytes[OFFSET_IMAGE + 1] = 0x04;
mBytes[OFFSET_IMAGE_DATA - 0x1A] = 0x20;
}
public byte[] Bytes
{
get{return mBytes;}
}
[DllImport("msvcrt.dll")]
private static extern int memcmp(IntPtr b1, IntPtr b2, long count);
private bool BitmapEqual(Bitmap b1, Bitmap b2)
{
if ((b1 == null) != (b2 == null)) return false;
if (b1.Size != b2.Size) return false;
var bd1 = b1.LockBits(new Rectangle(new Point(0, 0), b1.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
var bd2 = b2.LockBits(new Rectangle(new Point(0, 0), b2.Size), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
try
{
IntPtr bd1scan0 = bd1.Scan0;
IntPtr bd2scan0 = bd2.Scan0;
int stride = bd1.Stride;
int len = stride * b1.Height;
return memcmp(bd1scan0, bd2scan0, len) == 0;
}
finally
{
b1.UnlockBits(bd1);
b2.UnlockBits(bd2);
}
}
public bool BitmapEquals(Bitmap bmp)
{
return BitmapEqual(this.Bitmap, bmp);
}
public bool SmallBitmapEquals(Bitmap bmp)
{
return BitmapEqual(this.SmallBitmap, bmp);
}
}
}