Worms4Editor/W4Gui/Main.cs

168 lines
5.5 KiB
C#

using LibW4M;
using LibW4M.Data.WeaponFactory;
using LibW4M.PS2;
using LibW4M.XBOX;
using LibXom;
using LibXom.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Security.AccessControl;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using W4Gui.Dialogs;
using W4Gui.Tabs;
namespace W4Gui
{
public partial class Main : Form
{
private static int lastTabIndex = -1;
private const string defaultTitle = "Worms 4: Mayhem! (Save Editor)";
public Main()
{
InitializeComponent();
}
private void W4Gui_Load(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
#if !DEBUG
try
{
#endif
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "Worms 4: Mayhem Save Files|*.xom;*.psu;*.zip|Worms 4: Mayhem PC Save File|*.xom|Worms 4: Mayhem PS2 Save File|*.psu|Worms 4: Mayhem XBOX Save File|*.zip";
fd.Title = "Open Worms 4 Save File";
if (fd.ShowDialog() == DialogResult.OK)
{
DataManager.Load(fd.FileName, (SaveType)(fd.FilterIndex-1));
this.mainTabControl.Enabled = true;
this.saveToolStripMenuItem.Enabled = true;
this.saveAsToolStripMenuItem.Enabled = true;
this.Text = defaultTitle + " (" + DataManager.LoadedSaveType.ToString() + ") " + "[" + fd.FileName + "]";
DataManager.ParseData();
}
#if !DEBUG
}
catch (Exception ex)
{
MessageBox.Show("Failed to load save due to an error\n" + ex.Message + "\nThe file may be invalid or corrupt.", "Load failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endif
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
#if !DEBUG
try
{
#endif
mainTabControl.Enabled = false;
DataManager.CommitData();
DataManager.Save(DataManager.LoadedSavePath, DataManager.LoadedSaveType);
mainTabControl.Enabled = true;
MessageBox.Show("File saved to: " + DataManager.LoadedSavePath, "Save Complete!", MessageBoxButtons.OK, MessageBoxIcon.Information);
#if !DEBUG
}
catch (Exception ex)
{
this.mainTabControl.Enabled = true;
MessageBox.Show("Failed to save: " + ex.Message, "Save failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
#endif
}
private void mainTabControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (lastTabIndex >= 0)
{
foreach (Control ctrl in this.mainTabControl.TabPages[lastTabIndex].Controls)
{
try
{
TabEntry? tab = ctrl as TabEntry;
if (tab is TabEntry)
tab.SaveFromControl();
}
catch { }
}
}
if (this.mainTabControl.SelectedIndex >= 0)
{
foreach (Control ctrl in this.mainTabControl.TabPages[this.mainTabControl.SelectedIndex].Controls)
{
try
{
TabEntry? tab = ctrl as TabEntry;
if (tab is TabEntry)
tab.LoadIntoControl();
}
catch { }
}
}
lastTabIndex = this.mainTabControl.SelectedIndex;
}
private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
#if !DEBUG
try
{
#endif
SaveFileDialog fd = new SaveFileDialog();
fd.Filter = "Worms 4: Mayhem PC Save File|*.xom|Worms 4: Mayhem PS2 Save File|*.psu|Worms 4: Mayhem XBOX \"NTSC\" (United States) Save File|*.zip|Worms 4: Mayhem XBOX \"PAL\" (Europe & Australia) Save File|*.zip";
fd.Title = "Save Worms 4 Save File";
fd.FilterIndex = (int)DataManager.LoadedSaveType;
fd.FileName = Path.GetFileName(DataManager.LoadedSavePath);
if (fd.ShowDialog() == DialogResult.OK)
{
this.mainTabControl.Enabled = false;
DataManager.CommitData();
SaveType saveType = (SaveType)(fd.FilterIndex);
DataManager.Save(fd.FileName, saveType);
this.Text = defaultTitle + " (" + DataManager.LoadedSaveType.ToString() + ") " + "[" + fd.FileName + "]";
this.mainTabControl.Enabled = true;
MessageBox.Show("File saved to: " + fd.FileName, "Save Complete!", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
#if !DEBUG
}
catch (Exception ex)
{
MessageBox.Show("Failed to save: " + ex.Message, "Save failed!", MessageBoxButtons.OK, MessageBoxIcon.Error);
this.mainTabControl.Enabled = true;
}
#endif
}
private void aboutProgramToolStripMenuItem_Click(object sender, EventArgs e)
{
new AboutW4Gui().ShowDialog();
}
}
}