-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
214 lines (188 loc) · 7.67 KB
/
MainForm.cs
File metadata and controls
214 lines (188 loc) · 7.67 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
namespace SSPBundleTool
{
public partial class MainForm : Form
{
private static readonly string SettingsPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"SSPBundleTool", "settings.txt");
private BackgroundWorker worker;
public MainForm()
{
InitializeComponent();
InitializeWorker();
LoadSettings();
}
private void InitializeWorker()
{
worker = new BackgroundWorker();
worker.WorkerReportsProgress = false;
worker.DoWork += Worker_DoWork;
worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
}
// ── 設定の保存・読み込み ───────────────────────────────
private void LoadSettings()
{
if (!File.Exists(SettingsPath))
return;
var settings = new Dictionary<string, string>();
foreach (var line in File.ReadAllLines(SettingsPath))
{
int sep = line.IndexOf('=');
if (sep < 0) continue;
settings[line.Substring(0, sep)] = line.Substring(sep + 1);
}
if (settings.TryGetValue("SfxPath", out string sfx))
txtSfxPath.Text = sfx;
if (settings.TryGetValue("NarPath", out string nar))
txtNarPath.Text = nar;
if (settings.TryGetValue("OutputPath", out string output))
txtOutputPath.Text = output;
}
private void SaveSettings()
{
try
{
Directory.CreateDirectory(Path.GetDirectoryName(SettingsPath));
File.WriteAllLines(SettingsPath, new[]
{
$"SfxPath={txtSfxPath.Text}",
$"NarPath={txtNarPath.Text}",
$"OutputPath={txtOutputPath.Text}",
});
}
catch { /* 保存失敗は無視 */ }
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
SaveSettings();
base.OnFormClosing(e);
}
// ── ファイル参照 ──────────────────────────────────────
private void btnBrowseSfx_Click(object sender, EventArgs e)
{
using (var dlg = new OpenFileDialog())
{
dlg.Title = "自己解凍形式のSSPフルセットを選択";
dlg.Filter = "実行ファイル (*.exe)|*.exe|すべてのファイル (*.*)|*.*";
if (!string.IsNullOrEmpty(txtSfxPath.Text))
dlg.InitialDirectory = Path.GetDirectoryName(txtSfxPath.Text);
if (dlg.ShowDialog() == DialogResult.OK)
{
txtSfxPath.Text = dlg.FileName;
AutoFillOutputPath();
}
}
}
private void btnBrowseNar_Click(object sender, EventArgs e)
{
using (var dlg = new OpenFileDialog())
{
dlg.Title = "initial_install.nar を選択";
dlg.Filter = "narファイル (*.nar)|*.nar|すべてのファイル (*.*)|*.*";
if (!string.IsNullOrEmpty(txtNarPath.Text))
dlg.InitialDirectory = Path.GetDirectoryName(txtNarPath.Text);
if (dlg.ShowDialog() == DialogResult.OK)
txtNarPath.Text = dlg.FileName;
}
}
private void btnBrowseOutput_Click(object sender, EventArgs e)
{
using (var dlg = new SaveFileDialog())
{
dlg.Title = "出力ファイルの保存先を選択";
dlg.Filter = "実行ファイル (*.exe)|*.exe|すべてのファイル (*.*)|*.*";
dlg.DefaultExt = "exe";
if (!string.IsNullOrEmpty(txtOutputPath.Text))
{
dlg.InitialDirectory = Path.GetDirectoryName(txtOutputPath.Text);
dlg.FileName = Path.GetFileName(txtOutputPath.Text);
}
if (dlg.ShowDialog() == DialogResult.OK)
txtOutputPath.Text = dlg.FileName;
}
}
private void AutoFillOutputPath()
{
string sfxPath = txtSfxPath.Text;
if (string.IsNullOrEmpty(sfxPath))
return;
string dir = Path.GetDirectoryName(sfxPath);
string nameWithoutExt = Path.GetFileNameWithoutExtension(sfxPath);
string ext = Path.GetExtension(sfxPath);
txtOutputPath.Text = Path.Combine(dir, nameWithoutExt + "_bundled" + ext);
}
// ── 実行 ──────────────────────────────────────────────
private void btnExecute_Click(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtSfxPath.Text))
{
MessageBox.Show("SFXファイルを選択してください。", "入力エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrWhiteSpace(txtNarPath.Text))
{
MessageBox.Show("narファイルを選択してください。", "入力エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrWhiteSpace(txtOutputPath.Text))
{
MessageBox.Show("出力ファイルのパスを指定してください。", "入力エラー", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
SaveSettings();
txtLog.Clear();
btnExecute.Enabled = false;
worker.RunWorkerAsync(new WorkerArgs
{
InputSfxPath = txtSfxPath.Text,
NarFilePath = txtNarPath.Text,
OutputSfxPath = txtOutputPath.Text,
});
}
private void Worker_DoWork(object sender, DoWorkEventArgs e)
{
var args = (WorkerArgs)e.Argument;
var progress = new Progress<string>(msg =>
{
if (InvokeRequired)
Invoke(new Action(() => AppendLog(msg)));
else
AppendLog(msg);
});
SfxProcessor.AddNarToSfx(
args.InputSfxPath,
args.NarFilePath,
args.OutputSfxPath,
progress);
}
private void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
btnExecute.Enabled = true;
if (e.Error != null)
{
AppendLog($"エラー: {e.Error.Message}");
MessageBox.Show(
$"処理中にエラーが発生しました。\n\n{e.Error.Message}",
"エラー",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void AppendLog(string message)
{
txtLog.AppendText($"[{DateTime.Now:HH:mm:ss}] {message}{Environment.NewLine}");
txtLog.ScrollToCaret();
}
private class WorkerArgs
{
public string InputSfxPath { get; set; }
public string NarFilePath { get; set; }
public string OutputSfxPath { get; set; }
}
}
}