-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
195 lines (173 loc) · 5.7 KB
/
Copy pathMainForm.cs
File metadata and controls
195 lines (173 loc) · 5.7 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
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
namespace LngTool;
public class MainForm : Form
{
private const string DefaultEncoding = "utf-8";
private ComboBox _gameCombo = null!;
private Button _extractBtn = null!;
private Button _rebuildBtn = null!;
private Label _statusLabel = null!;
public MainForm()
{
Text = "ObsCure Text Editor";
StartPosition = FormStartPosition.CenterScreen;
ClientSize = new Size(460, 170);
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
Font = new Font("Segoe UI", 9F);
BuildUi();
}
private void BuildUi()
{
var lblGame = new Label { Text = "Game:", Left = 16, Top = 18, AutoSize = true };
_gameCombo = new ComboBox
{
Left = 110,
Top = 14,
Width = 320,
DropDownStyle = ComboBoxStyle.DropDownList
};
_gameCombo.Items.AddRange(new object[]
{
"Obscure 1 (all platforms - big-endian)",
"Obscure 2 (all platforms - little-endian)",
"Final Exam (all platforms)"
});
_gameCombo.SelectedIndex = 0;
_extractBtn = new Button
{
Text = "Extract .lng → .txt",
Left = 16,
Top = 50,
Width = 414,
Height = 36
};
_extractBtn.Click += OnExtractClick;
_rebuildBtn = new Button
{
Text = "Rebuild .txt → .lng",
Left = 16,
Top = 92,
Width = 414,
Height = 36
};
_rebuildBtn.Click += OnRebuildClick;
_statusLabel = new Label
{
Left = 16,
Top = 138,
Width = 414,
Height = 22,
ForeColor = Color.DimGray,
Text = "Ready."
};
Controls.AddRange(new Control[]
{
lblGame, _gameCombo,
_extractBtn, _rebuildBtn,
_statusLabel
});
}
private GameType ResolveGame() => _gameCombo.SelectedIndex switch
{
0 => GameType.Obscure1,
1 => GameType.Obscure2,
2 => GameType.FinalExam,
_ => GameType.Obscure1
};
private static string GameLabel(GameType g) => g switch
{
GameType.Obscure1 => "OB1",
GameType.Obscure2 => "OB2",
GameType.FinalExam => "FE",
_ => "?"
};
private void OnExtractClick(object? sender, EventArgs e)
{
using var ofd = new OpenFileDialog
{
Title = "Select .lng file to extract",
Filter = "LNG files (*.lng)|*.lng|All files (*.*)|*.*"
};
if (ofd.ShowDialog(this) != DialogResult.OK) return;
string lngPath = ofd.FileName;
string txtPath = Path.Combine(
Path.GetDirectoryName(lngPath) ?? "",
Path.GetFileNameWithoutExtension(lngPath) + ".txt");
try
{
GameType game = ResolveGame();
int count = game switch
{
GameType.Obscure1 => ObscureLng.ExtractOb1ToTxt(lngPath, txtPath),
GameType.Obscure2 => ObscureLng.ExtractOb2ToTxt(lngPath, txtPath, DefaultEncoding),
GameType.FinalExam => ObscureLng.ExtractFinalExamToTxt(lngPath, txtPath),
_ => 0
};
SetStatus($"{GameLabel(game)} — extracted {count} entries → {Path.GetFileName(txtPath)}");
MessageBox.Show(this,
$"Extracted to:\n{txtPath}",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (Exception ex)
{
SetStatus("Failed: " + ex.Message);
MessageBox.Show(this, ex.Message, "Extraction failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void OnRebuildClick(object? sender, EventArgs e)
{
using var ofd = new OpenFileDialog
{
Title = "Select .txt file to rebuild",
Filter = "TXT files (*.txt)|*.txt|All files (*.*)|*.*"
};
if (ofd.ShowDialog(this) != DialogResult.OK) return;
string txtPath = ofd.FileName;
GameType game = ResolveGame();
using var sfd = new SaveFileDialog
{
Title = "Save rebuilt .lng",
Filter = "LNG files (*.lng)|*.lng",
DefaultExt = ".lng",
FileName = Path.GetFileNameWithoutExtension(txtPath) + ".lng"
};
if (sfd.ShowDialog(this) != DialogResult.OK) return;
string outLng = sfd.FileName;
try
{
switch (game)
{
case GameType.Obscure1:
ObscureLng.RebuildOb1FromTxt(txtPath, outLng);
break;
case GameType.Obscure2:
ObscureLng.RebuildOb2FromTxt(txtPath, outLng, DefaultEncoding, addNullTerminator: false);
break;
case GameType.FinalExam:
ObscureLng.RebuildFinalExamFromTxt(txtPath, outLng);
break;
}
SetStatus($"{GameLabel(game)} — rebuilt → {Path.GetFileName(outLng)}");
MessageBox.Show(this,
$"Rebuilt .lng saved to:\n{outLng}",
"Success",
MessageBoxButtons.OK,
MessageBoxIcon.Information);
}
catch (Exception ex)
{
SetStatus("Failed: " + ex.Message);
MessageBox.Show(this, ex.Message, "Rebuild failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void SetStatus(string text)
{
_statusLabel.Text = text;
}
}