This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathL2MonsterSprite.cs
434 lines (347 loc) · 12.5 KB
/
L2MonsterSprite.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
using System.Collections.ObjectModel;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System;
namespace LEdit
{
public class L2MonsterSprite : Content
{
#region STATIC
public const int InitialCount = 0x86;
public const long DimensionTableAddress = 0x1AD140;
public const string DimensionFileExt = ".d";
public const int FileIDOffset = 0x1B8;
public static Collection<L2MonsterSprite> Array = new Collection<L2MonsterSprite>();
public static void ExtractAll(ProgressForm progress)
{
if (progress != null)
{
progress.SetText("Extrahiere MonsterSprites...");
progress.SetMaxProgress(InitialCount);
progress.Reset();
}
FileStream ROMStream = new FileStream(Project.ROMFile.FullName, FileMode.Open, FileAccess.Read);
BinaryReader ROMReader = new BinaryReader(ROMStream);
Project.GetOriginalFolder(ContentType.MonsterSprite).Create();
for (int i = 0; i < InitialCount; i++)
{
FileInfo File = Project.RequestOriginalSource(ContentType.MonsterSprite, i);
if (File.Exists)
File.Delete();
ROMStream.Seek(DimensionTableAddress + (i << 1), SeekOrigin.Begin);
ushort dim = ROMReader.ReadUInt16();
L2ROM.ExtractL2File(ROMStream, (ushort)(i + FileIDOffset), File.FullName);
BinaryWriter FileWriter = new BinaryWriter(
new FileInfo(File.FullName + DimensionFileExt).OpenWrite());
FileWriter.Write(dim);
FileWriter.Close();
if (progress != null)
progress.Progress(1);
}
ROMStream.Close();
}
public static void StaticInit(ProgressForm progress)
{
if (progress != null)
{
progress.SetText("Lade MonsterSprites...");
progress.SetMaxProgress(InitialCount);
progress.Reset();
}
for (int i = 0; i < InitialCount; i++)
{
FileInfo File = Project.RequestSource(ContentType.MonsterSprite, i);
if (!File.Exists)
{
//TODO: Error!
}
L2MonsterSprite MonsterSprite = new L2MonsterSprite();
MonsterSprite.ID = i;
MonsterSprite.Load(File);
Array.Add(MonsterSprite);
if (progress != null)
progress.Progress(1);
}
PrepareContentSelectionDialog();
}
public static void StaticUnload()
{
}
public static void PrepareContentSelectionDialog()
{
ContentSelectionDialog dlg;
try
{
dlg = ContentSelectionDialog.Map[ContentType.MonsterSprite];
dlg.Dispose();
}
catch (Exception)
{
}
dlg = new ContentSelectionDialog("MonsterSprite auswählen");
foreach (L2MonsterSprite monsprite in Array)
dlg.AddItem(monsprite);
ContentSelectionDialog.Map.Remove(ContentType.MonsterSprite);
ContentSelectionDialog.Map.Add(ContentType.MonsterSprite, dlg);
}
public static void Compile(FileStream rom, ProgressForm progress)
{
if (progress != null)
{
progress.SetText("Füge MonsterSprites ein...");
progress.SetMaxProgress(Array.Count);
progress.Reset();
}
BinaryWriter romWriter = new BinaryWriter(rom);
for (int i = 0; i < Array.Count; i++)
{
FileInfo src = Project.HasMod(ContentType.MonsterSprite, i);
if (src != null)
{
L2ROM.InsertL2File(rom, (ushort)(i + FileIDOffset), src);
BinaryReader DimensionReader = new BinaryReader(
new FileInfo(src.FullName + DimensionFileExt).OpenRead());
rom.Seek(DimensionTableAddress + (i << 1), SeekOrigin.Begin);
romWriter.Write(DimensionReader.ReadUInt16());
DimensionReader.Close();
}
if (progress != null)
progress.Progress(1);
}
}
#endregion
public byte TilesX;
public byte TilesY;
public byte Unknown;
public Collection<SNESPalette> Palettes = new Collection<SNESPalette>();
public SNESTileset Tileset;
private bool Drawn;
private Collection<Bitmap> Bitmaps = new Collection<Bitmap>();
public Bitmap GetBitmap(int Palette)
{
if (!Drawn)
Redraw();
if (Palette >= 0 && Palette < Bitmaps.Count)
return Bitmaps[Palette];
else
return null;
}
~L2MonsterSprite()
{
DisposeOfBitmaps();
}
public L2MonsterSprite()
{
}
public L2MonsterSprite(BinaryReader br)
{
FromStream(br);
}
public override string GetName()
{
return Content.FormatID(ID);
}
public override Bitmap GetIcon()
{
return GetBitmap(0);
}
private void DimensionFromStream(BinaryReader br)
{
TilesX = br.ReadByte();
byte y = br.ReadByte();
Unknown = (byte)(y & 0xE0);
TilesY = (byte)(y & 0x1F);
}
private void DimensionToStream(BinaryWriter bw)
{
bw.Write((byte)TilesX);
bw.Write((byte)(TilesY | Unknown));
}
public override void Load(FileInfo File)
{
BinaryReader br = new BinaryReader(new FileInfo(File.FullName + DimensionFileExt).OpenRead());
DimensionFromStream(br);
br.Close();
br = new BinaryReader(File.OpenRead());
FromStream(br);
br.Close();
}
public override void FromStream(BinaryReader br)
{
if (TilesX == 0 || TilesY == 0)
return;
byte NumPals = br.ReadByte();
Palettes.Clear();
for (int i = 0; i < NumPals; i++)
Palettes.Add(new SNESPalette(br));
int NumTiles = TilesX * TilesY;
Tileset = new SNESTileset(br, NumTiles);
DisposeOfBitmaps();
}
public override void Save(FileInfo File)
{
BinaryWriter bw = new BinaryWriter(new FileInfo(File.FullName + DimensionFileExt).OpenWrite());
DimensionToStream(bw);
bw.Close();
bw = new BinaryWriter(File.OpenWrite());
ToStream(bw);
bw.Close();
}
public override void ToStream(BinaryWriter bw)
{
bw.Write((byte)Palettes.Count);
for (int i = 0; i < Palettes.Count; i++)
Palettes[i].ToStream(bw);
Tileset.ToStream(bw);
}
public void AddPalette(SNESPalette pal)
{
Palettes.Add(pal);
Redraw();
}
public void RemovePalette(int n)
{
if (Palettes.Count > 1 && n >= 0 && n < Palettes.Count)
{
Palettes.RemoveAt(n);
Redraw();
}
}
public void SetPalette(int n, SNESPalette pal)
{
if (n >= 0 && n < Palettes.Count)
{
Palettes[n] = pal;
Redraw();
}
}
public void Draw(Graphics dst, byte paln, int dx, int dy)
{
if (!Drawn)
Redraw();
if (paln >= 0 && paln < Palettes.Count)
dst.DrawImage(Bitmaps[paln], new Point(dx, dy));
}
public void ExportPNG(FileInfo File, int Palette)
{
if (!Drawn)
Redraw();
if (Palette >= 0 && Palette < Bitmaps.Count)
{
if (File.Exists)
File.Delete();
FileStream stm = File.Create();
if (Bitmaps.Count > 0)
Bitmaps[Palette].Save(stm, ImageFormat.Png);
stm.Close();
}
}
private void DisposeOfBitmaps()
{
if (!Drawn)
return;
for (int p = 0; p < Palettes.Count; p++)
{
if (Bitmaps[p] != null)
Bitmaps[p].Dispose();
}
Bitmaps.Clear();
Drawn = false;
}
public void Redraw()
{
DisposeOfBitmaps();
for (int p = 0; p < Palettes.Count; p++)
{
Bitmap current_bmp = new Bitmap(TilesX << 3, TilesY << 3, PixelFormat.Format32bppArgb);
Bitmaps.Add(current_bmp);
}
for (int cBY = 0; cBY < (TilesY >> 1); cBY++)
{
for (int cBX = 0; cBX < (TilesX >> 1); cBX++)
{
for (int y = 0; y < 2; y++)
{
for (int x = 0; x < 2; x++)
{
int t = ((y + (cBX << 1) + cBY * TilesX) << 1) + x;
for (int p = 0; p < Palettes.Count; p++)
{
Graphics g = Graphics.FromImage(Bitmaps[p]);
Tileset.tiles[t].Draw(
g,
Palettes[p],
(x + (cBX << 1)) << 3,
(y + (cBY << 1)) << 3,
1, true);
}
}
}
}
}
Drawn = true;
}
public bool ImportBMP(FileInfo File)
{
Rectangle r = BMP4bppIO.GetDimensions(File);
if (r.Width > 0 && r.Width <= 128 && (r.Width % 8 == 0) &&
r.Height > 0 && r.Height <= 128 && (r.Height % 8 == 0))
{
TilesX = (byte)(r.Width / 8);
TilesY = (byte)(r.Height / 8);
byte[,] p = new byte[r.Width, r.Height];
if (BMP4bppIO.Import2DArray(File, p))
{
Tileset.tiles.Clear();
for (int cBY = 0; cBY < (TilesY >> 1); cBY++)
{
for (int cBX = 0; cBX < (TilesX >> 1); cBX++)
{
for (int y = 0; y < 2; y++)
{
for (int x = 0; x < 2; x++)
{
int t = ((y + (cBX << 1) + cBY * TilesX) << 1) + x;
SNESTile tile = new SNESTile();
tile.From2DArray(
p,
(x + (cBX << 1)) << 3,
(y + (cBY << 1)) << 3);
Tileset.tiles.Add(tile);
}
}
}
}
Redraw();
return true;
}
}
return false;
}
public void ExportBMP(FileInfo File, int palette)
{
byte[,] p = new byte[TilesX << 3, TilesY << 3];
for (int cBY = 0; cBY < (TilesY >> 1); cBY++)
{
for (int cBX = 0; cBX < (TilesX >> 1); cBX++)
{
for (int y = 0; y < 2; y++)
{
for (int x = 0; x < 2; x++)
{
int t = ((y + (cBX << 1) + cBY * TilesX) << 1) + x;
Tileset.tiles[t].To2DArray(
p,
(x + (cBX << 1)) << 3,
(y + (cBY << 1)) << 3);
}
}
}
}
//Save
BMP4bppIO.Export2DArray(
File, p, Palettes[(int)palette], TilesX << 3, TilesY << 3);
}
}
}