-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
474 lines (401 loc) · 16.1 KB
/
Copy pathMainForm.cs
File metadata and controls
474 lines (401 loc) · 16.1 KB
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
using System.Drawing;
namespace DQ5SaveEditor;
public partial class MainForm : Form
{
private SaveData? _save;
private string? _filePath;
private Character? _selectedChar;
private bool _isDirty; // true = unsaved changes in memory
public MainForm()
{
InitializeComponent();
ApplyDarkTheme();
}
// ── Dark theme ────────────────────────────────────────────────────────────
private void ApplyDarkTheme()
{
BackColor = Color.FromArgb(25, 25, 35);
ForeColor = Color.White;
_menu.BackColor = Color.FromArgb(35, 35, 45);
_menu.ForeColor = Color.White;
foreach (ToolStripMenuItem item in _menu.Items)
{
item.BackColor = Color.FromArgb(35, 35, 45); item.ForeColor = Color.White;
}
_topBar.BackColor = Color.FromArgb(30, 30, 30);
_status.BackColor = Color.FromArgb(30, 30, 40);
_statusLabel.ForeColor = Color.LightGray;
_charList.BackColor = Color.FromArgb(30, 30, 45);
_charList.ForeColor = Color.White;
_tabs.BackColor = Color.FromArgb(25, 25, 35);
foreach (TabPage tp in _tabs.TabPages)
{
tp.BackColor = Color.FromArgb(25, 25, 35); tp.ForeColor = Color.White;
}
foreach (var btn in new[] { _maxStatsBtn, _maxExpBtn, _fullHpMpBtn })
{
btn.BackColor = Color.FromArgb(55, 55, 80); btn.ForeColor = Color.White;
}
StyleGrid(_itemsGrid);
StyleGrid(_bagGrid);
}
private static void StyleGrid(DataGridView g)
{
g.BackgroundColor = Color.FromArgb(25, 25, 35);
g.DefaultCellStyle.BackColor = Color.FromArgb(35, 35, 50);
g.DefaultCellStyle.ForeColor = Color.White;
g.DefaultCellStyle.SelectionBackColor = Color.FromArgb(70, 70, 120);
g.ColumnHeadersDefaultCellStyle.BackColor = Color.FromArgb(40, 40, 60);
g.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
g.EnableHeadersVisualStyles = false;
g.GridColor = Color.FromArgb(50, 50, 70);
}
// ── Menu events ───────────────────────────────────────────────────────────
private void OpenMenuItem_Click(object? sender, EventArgs e) => OpenFile();
private void SaveMenuItem_Click(object? sender, EventArgs e) => SaveFile();
private void SaveAsMenuItem_Click(object? sender, EventArgs e) => SaveFileAs();
private void ExitMenuItem_Click(object? sender, EventArgs e) => Close();
// ── Gold ──────────────────────────────────────────────────────────────────
private void ApplyGoldBtn_Click(object? sender, EventArgs e)
{
if (_save == null)
{
return;
}
_save.Gold = (uint)_goldField.Value;
MarkDirty($"Gold set to {_goldField.Value:N0}. Use File → Save (Ctrl+S) to write to disk.");
}
// ── Character list ────────────────────────────────────────────────────────
private void CharList_SelectedIndexChanged(object? sender, EventArgs e)
{
if (_charList.SelectedItem is Character ch)
{
_selectedChar = ch;
LoadCharIntoEditor(ch);
}
}
private void CharList_DrawItem(object? sender, DrawItemEventArgs e)
{
if (e.Index < 0 || _save == null)
{
return;
}
var ch = (Character)_charList.Items[e.Index];
bool sel = (e.State & DrawItemState.Selected) != 0;
using (var bg = new SolidBrush(sel ? Color.FromArgb(70, 70, 120) : Color.FromArgb(30, 30, 45)))
{
e.Graphics.FillRectangle(bg, e.Bounds);
}
using var nameFont = new Font("Segoe UI", 10F, FontStyle.Bold);
using var infoFont = new Font("Segoe UI", 8F);
// Lay the two lines out from the actual font height so the row scales with
// DPI instead of relying on fixed pixel offsets (which overlap at >100%).
int x = e.Bounds.X + 8;
int top = e.Bounds.Y + 2;
int nameHeight = (int)Math.Ceiling(nameFont.GetHeight(e.Graphics));
// Tint monster names so they stand out from the human roster.
Color nameColor = ch.IsMonster ? Color.FromArgb(170, 210, 255) : Color.White;
using (var nameBrush = new SolidBrush(nameColor))
{
e.Graphics.DrawString(ch.Name, nameFont, nameBrush, x, top);
}
string info = $"Lv{ch.Level} HP:{ch.HpMax} MP:{ch.MpMax}";
if (ch.IsMonster && ch.SpeciesName.Length > 0)
{
info += $" · {ch.SpeciesName}";
}
using (var infoBrush = new SolidBrush(Color.FromArgb(160, 200, 160)))
{
e.Graphics.DrawString(info, infoFont, infoBrush, x, top + nameHeight);
}
}
/// <summary>
/// Size each character-list row to fit both text lines at the current DPI.
/// Called on load and whenever the DPI changes; fixed heights overlapped on
/// high-DPI displays.
/// </summary>
private void UpdateCharListItemHeight()
{
if (!_charList.IsHandleCreated)
{
return;
}
using var nameFont = new Font("Segoe UI", 10F, FontStyle.Bold);
using var infoFont = new Font("Segoe UI", 8F);
using var g = _charList.CreateGraphics();
_charList.ItemHeight = (int)Math.Ceiling(nameFont.GetHeight(g) + infoFont.GetHeight(g)) + 8;
_charList.Invalidate();
}
private void LoadCharIntoEditor(Character ch)
{
_charNameLabel.Text = ch.IsMonster && ch.SpeciesName.Length > 0
? $"{ch.Name} ({ch.SpeciesName})"
: ch.Name;
_levelField.Value = ch.Level;
_strField.Value = ch.Str;
_resField.Value = ch.Res;
_aglField.Value = ch.Agl;
_wisField.Value = ch.Wis;
_lckField.Value = ch.Lck;
_expField.Value = Math.Min(ch.Exp, 9_999_999);
_hpCurField.Value = ch.HpCur;
_hpMaxField.Value = ch.HpMax;
_mpCurField.Value = ch.MpCur;
_mpMaxField.Value = ch.MpMax;
PopulateItemsGrid(ch);
}
// ── Stats buttons ─────────────────────────────────────────────────────────
private void MaxStatsBtn_Click(object? sender, EventArgs e)
{
_strField.Value = _resField.Value = _aglField.Value =
_wisField.Value = _lckField.Value = 255;
_levelField.Value = 99;
}
private void MaxExpBtn_Click(object? sender, EventArgs e) => _expField.Value = 9_999_999;
private void FullHpMpBtn_Click(object? sender, EventArgs e)
{
if (_hpMaxField.Value > 0)
{
_hpCurField.Value = _hpMaxField.Value;
}
if (_mpMaxField.Value > 0)
{
_mpCurField.Value = _mpMaxField.Value;
}
}
private void ApplyStatsBtn_Click(object? sender, EventArgs e)
{
if (_selectedChar == null || _save == null)
{
return;
}
_selectedChar.Level = (byte)_levelField.Value;
_selectedChar.Str = (byte)_strField.Value;
_selectedChar.Res = (byte)_resField.Value;
_selectedChar.Agl = (byte)_aglField.Value;
_selectedChar.Wis = (byte)_wisField.Value;
_selectedChar.Lck = (byte)_lckField.Value;
_selectedChar.Exp = (uint)_expField.Value;
_selectedChar.HpCur = (ushort)_hpCurField.Value;
_selectedChar.HpMax = (ushort)_hpMaxField.Value;
_selectedChar.MpCur = (ushort)_mpCurField.Value;
_selectedChar.MpMax = (ushort)_mpMaxField.Value;
_save.FlushCharacter(_selectedChar);
_charList.Invalidate();
MarkDirty($"Changes applied to {_selectedChar.Name}. Use File → Save (Ctrl+S) to write to disk.");
}
// ── Items grid ────────────────────────────────────────────────────────────
private void PopulateItemsGrid(Character ch)
{
_itemsGrid.Rows.Clear();
for (int s = 0; s < ch.Items.Length; s++)
{
var item = ch.Items[s];
if (item == null || item.IsEmpty)
{
continue; // skip empty slots
}
string name = item.ItemName + (item.Qty > 1 ? $" x{item.Qty}" : "");
_itemsGrid.Rows.Add(s + 1, name, $"0x{item.ItemId:X2}",
item.IsEquipped ? "✔" : "");
}
}
// ── Bag / item display is read-only; no edit handlers needed. ──────────────
private void CommitBag() { /* read-only: no-op */ }
// ── File operations ───────────────────────────────────────────────────────
private void OpenFile()
{
using var dlg = new OpenFileDialog
{
Title = "Open melonDS Save State",
Filter = "melonDS Save State (*.ml1)|*.ml1|All Files (*.*)|*.*",
InitialDirectory = @"D:\Emulation\DS\Saves"
};
if (dlg.ShowDialog() != DialogResult.OK)
{
return;
}
try
{
if (_isDirty && !ConfirmDiscardChanges())
{
return;
}
_save = SaveData.LoadSaveState(dlg.FileName);
_filePath = dlg.FileName;
_isDirty = false;
PopulateUI();
SetStatus($"Loaded: {Path.GetFileName(_filePath)} — {_save.Characters.Count} characters found. " +
$"(Close melonDS before editing, then load state with F1)");
Text = $"DQ5 Save Editor — {Path.GetFileName(_filePath)}";
}
catch (Exception ex)
{
MessageBox.Show($"Failed to load save state:\n{ex.Message}", "Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SaveFile()
{
if (_save == null || _filePath == null)
{
SaveFileAs();
return;
}
// Editor is .ml1 focused — no checksum needed for save states.
ApplyStatsBtn_Click(null, EventArgs.Empty);
CommitBag();
_save.Gold = (uint)_goldField.Value;
string bak = _filePath + ".bak";
if (!File.Exists(bak))
{
File.Copy(_filePath, bak);
}
_save.Save(_filePath);
ClearDirty($"Saved — load with F1 in melonDS to apply changes.");
}
private void SaveFileAs()
{
if (_save == null)
{
return;
}
using var dlg = new SaveFileDialog
{
Title = "Save melonDS Save State",
Filter = "melonDS Save State (*.ml1)|*.ml1|All Files (*.*)|*.*",
FileName = _filePath ?? "save.ml1",
DefaultExt = "ml1",
};
if (dlg.ShowDialog() != DialogResult.OK)
{
return;
}
ApplyStatsBtn_Click(null, EventArgs.Empty);
CommitBag();
_save!.Gold = (uint)_goldField.Value;
_save.Save(dlg.FileName);
_filePath = dlg.FileName;
ClearDirty($"Saved to {Path.GetFileName(_filePath)}");
Text = $"DQ5 Save Editor — {Path.GetFileName(_filePath)}";
}
// ── Populate UI ───────────────────────────────────────────────────────────
private void PopulateUI()
{
if (_save == null)
{
return;
}
_goldField.Value = Math.Min(_save.Gold, 9_999_999);
_charList.Items.Clear();
foreach (var ch in _save.Characters)
{
_charList.Items.Add(ch);
}
// For save states, read live in-game data for every party character + bag
if (_save.IsSaveState && _save.HasLiveHeroData)
{
foreach (var ch in _save.Characters)
{
if (ch.SlotIndex == 0)
{
_save.ReadHeroLiveData(ch);
}
else if (ch.LiveStatOffset >= 0)
{
_save.ReadLiveStats(ch, ch.LiveStatOffset);
}
}
_save.ReadLiveBag();
}
if (_charList.Items.Count > 0)
{
_charList.SelectedIndex = 0;
}
PopulateBagGrid();
}
private void PopulateBagGrid()
{
if (_save == null)
{
return;
}
_bagGrid.Rows.Clear();
for (int i = 0; i < _save.BagSlotCount; i++)
{
var item = _save.BagItems[i];
if (item == null || item.IsEmpty)
{
continue; // skip empty slots
}
_bagGrid.Rows.Add(i + 1, item.ItemName, $"0x{item.ItemId:X2}",
item.Quantity.ToString());
}
}
// Shared DataError handler — suppresses the default error dialog for both grids.
private void Grid_DataError(object? sender, DataGridViewDataErrorEventArgs e)
{
e.Cancel = true;
}
// ── Dirty-state helpers ───────────────────────────────────────────────────
private void MarkDirty(string statusMsg)
{
_isDirty = true;
_statusLabel.ForeColor = Color.Gold;
SetStatus("⚠ " + statusMsg);
if (_filePath != null && !Text.StartsWith('*'))
{
Text = "* " + Text;
}
}
private void ClearDirty(string statusMsg)
{
_isDirty = false;
_statusLabel.ForeColor = Color.LightGray;
SetStatus(statusMsg);
if (_filePath != null)
{
Text = $"DQ5 Save Editor — {Path.GetFileName(_filePath)}";
}
}
private bool ConfirmDiscardChanges()
{
var r = MessageBox.Show(
"You have unsaved changes. Save before continuing?",
"Unsaved Changes",
MessageBoxButtons.YesNoCancel,
MessageBoxIcon.Warning);
if (r == DialogResult.Yes)
{
SaveFile(); return !_isDirty;
}
return r == DialogResult.No;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (_isDirty && !ConfirmDiscardChanges())
{
e.Cancel = true;
}
base.OnFormClosing(e);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
UpdateCharListItemHeight();
// Default the two panes to an even 50/50 split; the user can still drag the
// splitter afterward. Done here (not in the designer) because the container
// now has its real width.
int usable = _split.Width - _split.SplitterWidth;
if (usable > 0)
{
_split.SplitterDistance = usable / 2;
}
}
protected override void OnDpiChanged(DpiChangedEventArgs e)
{
base.OnDpiChanged(e);
UpdateCharListItemHeight();
}
private void SetStatus(string msg) => _statusLabel.Text = msg;
}