2 using System.Collections.Generic;
3 using System.ComponentModel;
8 using System.Windows.Forms;
13 public partial class Form1 : Form
17 InitializeComponent();
20 private void Form1_Load(object sender, EventArgs e)
22 RegistryKey reg = Application.UserAppDataRegistry.CreateSubKey("Config");
23 tbPath.Text = (string)reg.GetValue("OutPath", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
24 tabControl1.SelectedIndex = (int)reg.GetValue("TabPage", 0);
25 tbMqoFile.Text = (string)reg.GetValue("MqoIn", "");
26 tbTsoFileRef.Text = (string)reg.GetValue("Tso", "");
27 tbTsoFile.Text = (string)reg.GetValue("TsoEx", "");
28 tbMergeTso.Text = (string)reg.GetValue("MergeTso", "");
29 rbRefBone.Checked = (int)reg.GetValue("RefBone", 1) == 1;
30 rbOneBone.Checked = (int)reg.GetValue("OneBone", 0) == 1;
31 rbBoneNone.Checked = (int)reg.GetValue("BoneNone", 1) == 1;
32 rbBoneRokDeBone.Checked = (int)reg.GetValue("BoneRokDeBone", 0) == 1;
33 cbMakeSub.Checked = (int)reg.GetValue("MakeSub", 1) == 1;
34 cbCopyTSO.Checked = (int)reg.GetValue("CopyTSO", 1) == 1;
35 cbShowMaterials.Checked = (int)reg.GetValue("ShowMaterials", 0) == 1;
37 reg = Application.UserAppDataRegistry.CreateSubKey("Form1");
38 Bounds = new Rectangle(
39 (int)reg.GetValue("Left", 0),
40 (int)reg.GetValue("Top", 0),
41 (int)reg.GetValue("Width", 640),
42 (int)reg.GetValue("Height", 320));
46 Config config = Config.Instance;
49 private void Form1_FormClosed(object sender, FormClosedEventArgs e)
51 RegistryKey reg = Application.UserAppDataRegistry.CreateSubKey("Config");
52 reg.SetValue("OutPath", tbPath.Text);
53 reg.SetValue("TabPage", tabControl1.SelectedIndex);
54 reg.SetValue("MqoIn", tbMqoFile.Text);
55 reg.SetValue("Tso", tbTsoFileRef.Text);
56 reg.SetValue("TsoEx", tbTsoFile.Text);
57 reg.SetValue("MergeTso", tbMergeTso.Text);
58 reg.SetValue("RefBone", rbRefBone.Checked ? 1 : 0);
59 reg.SetValue("OneBone", rbOneBone.Checked ? 1 : 0);
60 reg.SetValue("BoneNone", rbBoneNone.Checked ? 1 : 0);
61 reg.SetValue("BoneRokDeBone", rbBoneRokDeBone.Checked ? 1 : 0);
62 reg.SetValue("MakeSub", cbMakeSub.Checked ? 1 : 0);
63 reg.SetValue("CopyTSO", cbCopyTSO.Checked ? 1 : 0);
64 reg.SetValue("ShowMaterials", cbShowMaterials.Checked ? 1 : 0);
66 reg = Application.UserAppDataRegistry.CreateSubKey("Form1");
68 if ((this.WindowState & FormWindowState.Minimized) == FormWindowState.Minimized)
70 reg.SetValue("Top", RestoreBounds.Top);
71 reg.SetValue("Left", RestoreBounds.Left);
72 reg.SetValue("Width", RestoreBounds.Width);
73 reg.SetValue("Height", RestoreBounds.Height);
77 reg.SetValue("Top", Top);
78 reg.SetValue("Left", Left);
79 reg.SetValue("Width", Width);
80 reg.SetValue("Height", Height);
86 private void Form1_DragDrop(object sender, DragEventArgs e)
90 if (!e.Data.GetDataPresent(DataFormats.FileDrop))
93 string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
95 if (files.Length == 0)
98 switch (tabControl1.SelectedIndex)
101 foreach (string i in files)
103 if (Path.GetExtension(i).ToUpper() == ".TSO")
110 switch (Path.GetExtension(files[0]).ToUpper())
112 case ".TSO": tbTsoFileRef.Text = files[0]; break;
113 case ".MQO": tbMqoFile.Text = files[0]; break;
123 catch (Exception exception)
125 Util.ProcessError(exception);
129 private void Form1_DragEnter(object sender, DragEventArgs e)
131 if (!e.Data.GetDataPresent(DataFormats.FileDrop))
134 e.Effect = DragDropEffects.Copy;
137 private void tbMergeTso_DragDrop(object sender, DragEventArgs e)
139 if (!e.Data.GetDataPresent(DataFormats.FileDrop))
142 string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
144 switch (Path.GetExtension(files[0]).ToUpper())
146 case ".TSO": tbMergeTso.Text = files[0]; break;
150 private void tbMergeTso_DragEnter(object sender, DragEventArgs e)
152 if (!e.Data.GetDataPresent(DataFormats.FileDrop))
155 e.Effect = DragDropEffects.Copy;
158 private void GenerateMqo(string tso_file)
160 string out_path = tbPath.Text;
162 if (cbMakeSub.Checked)
164 out_path = Path.Combine(out_path, Path.GetFileNameWithoutExtension(tso_file));
165 Directory.CreateDirectory(out_path);
170 label2.BackColor = Color.Tomato;
171 label2.ForeColor = Color.White;
172 label2.Text = "Processing";
176 MqoGenerator gen = new MqoGenerator();
177 gen.Generate(tso_file, out_path, rbBoneRokDeBone.Checked);
179 if (cbCopyTSO.Checked)
181 string tso_path = Path.Combine(out_path, Path.GetFileName(tso_file));
183 if (tso_file != tso_path)
184 File.Copy(tso_file, tso_path, true);
189 label2.BackColor = SystemColors.Control;
190 label2.BackColor = label2.Parent.BackColor;
191 label2.ForeColor = SystemColors.ControlText;
192 label2.Text = "Drop TSO File Here!";
196 private void GenerateTso(string file)
198 TSOGeneratorConfig config = new TSOGeneratorConfig();
199 config.ShowMaterials = cbShowMaterials.Checked;
201 if (rbRefBone.Checked)
203 TSOGeneratorMqxBone gen = new TSOGeneratorMqxBone(config);
204 gen.Generate(file, tbTsoFileRef.Text, tbTsoFile.Text);
206 else if (rbOneBone.Checked)
208 TSOGeneratorOneBone gen = new TSOGeneratorOneBone(config);
210 foreach (ListViewItem item in lvObjects.Items)
212 if (item.SubItems[1].Text == "")
214 MessageBox.Show("すべてのオブジェクトにボーンを設定してください");
218 gen.ObjectBoneNames.Add(item.SubItems[0].Text, item.SubItems[1].Text);
221 gen.Generate(file, tbTsoFileRef.Text, tbTsoFile.Text);
225 private void button1_Click(object sender, EventArgs e)
227 FolderBrowserDialog dlg = new FolderBrowserDialog();
228 dlg.SelectedPath = tbPath.Text;
230 if (dlg.ShowDialog() == DialogResult.OK)
231 tbPath.Text = dlg.SelectedPath;
235 private void radioButton1_CheckedChanged(object sender, EventArgs e)
237 EnableControlStuff();
240 private void radioButton2_CheckedChanged(object sender, EventArgs e)
242 EnableControlStuff();
245 private void EnableControlStuff()
247 gbBone.Enabled = rbOneBone.Checked;
250 private void BuildBoneTree(TreeNodeCollection nodes, TSONode node)
252 TreeNode tn = nodes.Add(node.ShortName);
255 if (node.children != null)
256 foreach (TSONode i in node.children)
257 BuildBoneTree(tn.Nodes, i);
260 private void SaveAssign()
262 foreach (ListViewItem item in lvObjects.Items)
264 string obj = item.SubItems[0].Text;
265 string bone = item.SubItems[1].Text;
267 if (Config.Instance.object_bone_map.ContainsKey(obj))
268 Config.Instance.object_bone_map[obj] = bone;
270 Config.Instance.object_bone_map.Add(obj, bone);
274 private void btnMqoFile_Click(object sender, EventArgs e)
278 OpenFileDialog dlg = new OpenFileDialog();
279 dlg.Filter = "Metasequoia File (*.mqo)|*.mqo";
280 dlg.FileName = tbMqoFile.Text;
282 if (dlg.ShowDialog() == DialogResult.OK)
283 tbMqoFile.Text = dlg.FileName;
285 catch (Exception exception)
287 Util.ProcessError(exception);
291 private void btnTsoFileRef_Click(object sender, EventArgs e)
295 OpenFileDialog dlg = new OpenFileDialog();
296 dlg.Filter = "TSO File (*.tso)|*.tso";
297 dlg.FileName = tbTsoFileRef.Text;
299 if (dlg.ShowDialog() == DialogResult.OK)
300 tbTsoFileRef.Text = dlg.FileName;
302 catch (Exception exception)
304 Util.ProcessError(exception);
308 private void btnTsoFile_Click(object sender, EventArgs e)
312 SaveFileDialog dlg = new SaveFileDialog();
313 dlg.Filter = "TSO File (*.tso)|*.tso";
314 dlg.FileName = tbTsoFile.Text;
316 if (dlg.ShowDialog() == DialogResult.OK)
317 tbTsoFile.Text = dlg.FileName;
319 catch (Exception exception)
321 Util.ProcessError(exception);
325 private void btnRefresh_Click(object sender, EventArgs e)
333 MqoReader mqo = new MqoReader();
334 mqo.Load(tbMqoFile.Text);
335 lvObjects.Items.Clear();
337 foreach (MqoObject obj in mqo.Objects)
339 ListViewItem item = lvObjects.Items.Add(obj.name);
343 if (Config.Instance.object_bone_map.TryGetValue(obj.name, out bone))
344 item.SubItems.Add(bone);
346 item.SubItems.Add("");
350 TSOFile tso = new TSOFile(tbTsoFileRef.Text);
352 tvBones.Visible = false;
353 tvBones.Nodes.Clear();
354 BuildBoneTree(tvBones.Nodes, tso.nodes[0]);
356 tvBones.Nodes[0].EnsureVisible();
358 catch (Exception exception)
360 Util.ProcessError(exception);
364 tvBones.Visible = true;
368 private void btnSelectAll_Click(object sender, EventArgs e)
370 foreach (ListViewItem item in lvObjects.Items)
371 item.Selected = true;
374 private void btnDeselectAll_Click(object sender, EventArgs e)
376 foreach (ListViewItem item in lvObjects.Items)
377 item.Selected = false;
380 private void btnAssign_Click(object sender, EventArgs e)
384 TreeNode node = tvBones.SelectedNode;
388 MessageBox.Show("割り当てるボーンを選択してください");
392 foreach (ListViewItem item in lvObjects.SelectedItems)
393 item.SubItems[1].Text = node.Text;
399 Util.ProcessError(ex);
403 private void btnGenerate_Click(object sender, EventArgs e)
405 Color c = tabPage2.BackColor;
409 tabPage2.BackColor = Color.Tomato;
411 string file = tbMqoFile.Text;
414 catch (Exception exception)
416 Util.ProcessError(exception);
420 tabPage2.BackColor = c;
425 private void AddMergeTso(string[] files)
427 foreach (string file in files)
429 if (Path.GetExtension(files[0]).ToUpper() != ".TSO")
432 if (tvMerge.Nodes.Find(file, false).Length == 0)
434 TreeNode node = tvMerge.Nodes.Add(file);
438 TSOFile tso = new TSOFile(file);
441 foreach (TSOMesh j in tso.meshes)
443 TreeNode mesh = node.Nodes.Add(j.Name);
451 private void btnMerge_Click(object sender, EventArgs e)
453 Color c = tabPage2.BackColor;
457 tabPage2.BackColor = Color.Tomato;
458 List<TSOMesh> meshes = new List<TSOMesh>();
459 Dictionary<string, Pair<TSOMaterial, int>> materialmap = new Dictionary<string, Pair<TSOMaterial, int>>();
460 Dictionary<string, TSOTex> textures = new Dictionary<string, TSOTex>();
463 foreach (TreeNode node in tvMerge.Nodes)
465 TSOFile tso = new TSOFile(node.Text);
471 foreach (TSOMesh mesh in tso.meshes)
473 TreeNode[] found = node.Nodes.Find(mesh.Name, false);
475 if (found.Length == 0 || !found[0].Checked)
478 foreach (TSOSubMesh k in mesh.sub_meshes)
479 mtls |= 1ul << k.spec;
484 foreach (TSOMaterial mat in tso.materials)
486 if ((mask & mtls) != 0)
488 if (!materialmap.ContainsKey(mat.Name))
490 Pair<TSOMaterial, int> value = new Pair<TSOMaterial, int>(mat, materialmap.Count);
491 materialmap.Add(mat.Name, value);
493 if (!textures.ContainsKey(mat.ColorTex))
495 TSOTex tex = tso.texturemap[mat.ColorTex];
496 textures.Add(tex.Name, tex);
499 if (!textures.ContainsKey(mat.ShadeTex))
501 TSOTex tex = tso.texturemap[mat.ShadeTex];
502 textures.Add(tex.Name, tex);
511 using (FileStream fs = File.OpenWrite(tbMergeTso.Text))
515 List<TSOTex> texlist = new List<TSOTex>(textures.Values);
516 TSOMaterial[] mtllist = new TSOMaterial[materialmap.Count];
518 foreach (var i in materialmap.Values)
519 mtllist[i.Second] = i.First;
521 foreach (TSOMesh mesh in meshes)
523 foreach (TSOSubMesh sub in mesh.sub_meshes)
525 TSOMaterial spec = mesh.file.materials[sub.spec];
526 sub.spec = materialmap[spec.Name].Second;
530 foreach (TSOTex tex in texlist)
531 TSOFile.ExchangeChannel(tex.data, tex.depth);
533 BinaryWriter bw = new BinaryWriter(fs);
534 TSOWriter.WriteHeader(bw);
535 TSOWriter.Write(bw, last.nodes);
536 TSOWriter.Write(bw, texlist.ToArray());
537 TSOWriter.Write(bw, last.effects);
538 TSOWriter.Write(bw, mtllist);
539 TSOWriter.Write(bw, meshes.ToArray());
542 catch (Exception exception)
544 Util.ProcessError(exception);
548 tabPage2.BackColor = c;
552 private void btnMergeAdd_Click(object sender, EventArgs e)
556 OpenFileDialog dlg = new OpenFileDialog();
557 dlg.Filter = "TSO File(*.tso)|*.tso";
558 dlg.Multiselect = true;
560 if (dlg.ShowDialog() == DialogResult.OK)
561 AddMergeTso(dlg.FileNames);
563 catch (Exception exception)
565 Util.ProcessError(exception);
569 private void btnMergeDel_Click(object sender, EventArgs e)
571 if (tvMerge.SelectedNode != null && tvMerge.SelectedNode.Level == 0)
572 tvMerge.SelectedNode.Remove();
575 private void btnMergeReset_Click(object sender, EventArgs e)
577 tvMerge.Nodes.Clear();
580 private void btnRefMergeTso_Click(object sender, EventArgs e)
584 SaveFileDialog dlg = new SaveFileDialog();
585 dlg.Filter = "TSO File (*.tso)|*.tso";
586 dlg.FileName = tbMergeTso.Text;
588 if (dlg.ShowDialog() == DialogResult.OK)
589 tbMergeTso.Text = dlg.FileName;
591 catch (Exception exception)
593 Util.ProcessError(exception);
597 public static bool bTvMerge_AfterCheck = false;
599 private void tvMerge_AfterCheck(object sender, TreeViewEventArgs e)
601 if (bTvMerge_AfterCheck)
604 bTvMerge_AfterCheck = true;
608 if (e.Node.Level == 0)
610 foreach (TreeNode node in e.Node.Nodes)
611 node.Checked = e.Node.Checked;
617 foreach (TreeNode node in e.Node.Parent.Nodes)
618 if (node.Checked) check = true;
620 e.Node.Parent.Checked = check;
625 bTvMerge_AfterCheck = false;
633 public static void ProcessError(Exception exception)
635 MessageBox.Show(exception.ToString());