2022-06-18 18:04:24 +00:00
|
|
|
using PKHeX.Core;
|
2017-05-12 04:34:18 +00:00
|
|
|
using System;
|
2016-07-09 22:30:12 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Drawing;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2019-11-23 05:23:00 +00:00
|
|
|
using System.Runtime.InteropServices;
|
2023-12-04 04:13:20 +00:00
|
|
|
using System.Text;
|
2016-07-09 22:30:12 +00:00
|
|
|
using System.Windows.Forms;
|
|
|
|
|
2018-04-07 04:23:09 +00:00
|
|
|
using static PKHeX.Core.MessageStrings;
|
2019-11-23 05:23:00 +00:00
|
|
|
using Exception = System.Exception;
|
2018-04-07 04:23:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
namespace PKHeX.WinForms;
|
|
|
|
|
|
|
|
public static class WinFormsUtil
|
2016-07-09 22:30:12 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
internal static void TranslateInterface(Control form, string lang) => form.TranslateInterface(lang);
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Centers the <see cref="child"/> horizontally and vertically so that its center is the same as the <see cref="parent"/>'s center.
|
|
|
|
/// </summary>
|
|
|
|
internal static void CenterToForm(this Control child, Control? parent)
|
2016-07-09 22:30:12 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (parent == null)
|
|
|
|
return;
|
|
|
|
int x = parent.Location.X + ((parent.Width - child.Width) / 2);
|
|
|
|
int y = parent.Location.Y + ((parent.Height - child.Height) / 2);
|
|
|
|
child.Location = new Point(Math.Max(x, 0), Math.Max(y, 0));
|
|
|
|
}
|
2022-05-03 01:11:31 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Horizontally centers the <see cref="child"/> to the <see cref="parent"/>'s horizontal center.
|
|
|
|
/// </summary>
|
|
|
|
internal static void HorizontallyCenter(this Control child, Control parent)
|
|
|
|
{
|
|
|
|
int midpoint = (parent.Width - child.Width) / 2;
|
2023-07-09 02:29:46 +00:00
|
|
|
if (child.Location.X != midpoint)
|
|
|
|
child.SetBounds(midpoint, 0, 0, 0, BoundsSpecified.X);
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2019-10-03 03:04:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static T? FirstFormOfType<T>() where T : Form => (T?)Application.OpenForms.Cast<Form>().FirstOrDefault(form => form is T);
|
2018-07-22 02:20:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static T? FindFirstControlOfType<T>(Control aParent) where T : class
|
|
|
|
{
|
|
|
|
while (true)
|
2018-05-05 15:07:22 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (aParent is T t)
|
|
|
|
return t;
|
2018-05-05 15:07:22 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (aParent.Parent != null)
|
|
|
|
aParent = aParent.Parent;
|
|
|
|
else
|
|
|
|
return null;
|
2018-05-05 15:07:22 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-22 02:20:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static T? GetUnderlyingControl<T>(object sender) where T : class
|
|
|
|
{
|
|
|
|
while (true)
|
2019-09-25 01:14:15 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
switch (sender)
|
2019-09-25 01:14:15 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
case T p:
|
|
|
|
return p;
|
2023-01-22 04:02:33 +00:00
|
|
|
case ToolStripItem { Owner: { } o}:
|
|
|
|
sender = o;
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
2023-01-22 04:02:33 +00:00
|
|
|
case ContextMenuStrip { SourceControl: { } s }:
|
|
|
|
sender = s;
|
2022-06-18 18:04:24 +00:00
|
|
|
continue;
|
|
|
|
default:
|
|
|
|
return default;
|
2019-09-25 01:14:15 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2017-05-23 04:55:05 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool OpenWindowExists<T>(this Form parent) where T : Form
|
|
|
|
{
|
|
|
|
var form = FirstFormOfType<T>();
|
|
|
|
if (form == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
form.CenterToForm(parent);
|
|
|
|
form.BringToFront();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
#region Message Displays
|
|
|
|
/// <summary>
|
|
|
|
/// Displays a dialog showing the details of an error.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="friendlyMessage">User-friendly message about the error.</param>
|
|
|
|
/// <param name="exception">Instance of the error's <see cref="Exception"/>.</param>
|
|
|
|
/// <returns>The <see cref="DialogResult"/> associated with the dialog.</returns>
|
|
|
|
internal static DialogResult Error(string friendlyMessage, Exception exception)
|
|
|
|
{
|
|
|
|
System.Media.SystemSounds.Exclamation.Play();
|
|
|
|
return ErrorWindow.ShowErrorDialog(friendlyMessage, exception, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Displays a dialog showing the details of an error.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="lines">User-friendly message about the error.</param>
|
|
|
|
/// <returns>The <see cref="DialogResult"/> associated with the dialog.</returns>
|
|
|
|
internal static DialogResult Error(params string[] lines)
|
|
|
|
{
|
|
|
|
System.Media.SystemSounds.Hand.Play();
|
|
|
|
string msg = string.Join(Environment.NewLine + Environment.NewLine, lines);
|
|
|
|
return MessageBox.Show(msg, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static DialogResult Alert(params string[] lines) => Alert(true, lines);
|
|
|
|
|
|
|
|
internal static DialogResult Alert(bool sound, params string[] lines)
|
|
|
|
{
|
|
|
|
if (sound)
|
|
|
|
System.Media.SystemSounds.Asterisk.Play();
|
|
|
|
string msg = string.Join(Environment.NewLine + Environment.NewLine, lines);
|
|
|
|
return MessageBox.Show(msg, "Alert", MessageBoxButtons.OK, sound ? MessageBoxIcon.Information : MessageBoxIcon.None);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal static DialogResult Prompt(MessageBoxButtons btn, params string[] lines)
|
|
|
|
{
|
|
|
|
System.Media.SystemSounds.Asterisk.Play();
|
|
|
|
string msg = string.Join(Environment.NewLine + Environment.NewLine, lines);
|
|
|
|
return MessageBox.Show(msg, "Prompt", btn, MessageBoxIcon.Question);
|
|
|
|
}
|
|
|
|
#endregion
|
2019-10-08 01:40:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
internal static bool SetClipboardText(string text)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
Clipboard.SetText(text);
|
2019-10-08 01:40:09 +00:00
|
|
|
return true;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
catch (ExternalException x)
|
2016-08-26 03:59:29 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
Error(MsgClipboardFailWrite, x);
|
2016-08-26 03:59:29 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
// Clipboard might be locked sometimes
|
|
|
|
catch
|
2016-07-09 22:30:12 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
Error(MsgClipboardFailWrite);
|
2016-07-09 22:30:12 +00:00
|
|
|
}
|
2016-08-26 03:59:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-01-03 23:29:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Gets the selected value of the input <see cref="cb"/>. If no value is selected, will return 0.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="cb">ComboBox to retrieve value for.</param>
|
|
|
|
internal static int GetIndex(ListControl cb)
|
|
|
|
{
|
|
|
|
return (int)(cb.SelectedValue ?? 0);
|
|
|
|
}
|
2016-08-26 03:59:29 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void PanelScroll(object? sender, ScrollEventArgs e)
|
|
|
|
{
|
|
|
|
if (sender is not ScrollableControl p || e.NewValue < 0)
|
|
|
|
return;
|
|
|
|
switch (e.ScrollOrientation)
|
2016-07-09 22:30:12 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
case ScrollOrientation.HorizontalScroll:
|
|
|
|
p.HorizontalScroll.Value = Clamp(e.NewValue, p.HorizontalScroll);
|
|
|
|
break;
|
|
|
|
case ScrollOrientation.VerticalScroll:
|
|
|
|
p.VerticalScroll.Value = Clamp(e.NewValue, p.VerticalScroll);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new IndexOutOfRangeException(nameof(e.ScrollOrientation));
|
2016-07-09 22:30:12 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
static int Clamp(int value, ScrollProperties prop) => Math.Max(prop.Minimum, Math.Min(prop.Maximum, value));
|
|
|
|
}
|
2016-07-09 22:30:12 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Initializes the <see cref="control"/> to be bound to a provided <see cref="ComboItem"/> list.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="control">Control to initialize binding</param>
|
|
|
|
public static void InitializeBinding(this ListControl control)
|
|
|
|
{
|
|
|
|
control.DisplayMember = nameof(ComboItem.Text);
|
|
|
|
control.ValueMember = nameof(ComboItem.Value);
|
|
|
|
}
|
2019-11-23 05:23:00 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void SetValueClamped(this NumericUpDown nud, int value) => nud.Value = Math.Min(nud.Maximum, Math.Max(nud.Minimum, value));
|
|
|
|
public static void SetValueClamped(this NumericUpDown nud, uint value) => nud.Value = Math.Min(nud.Maximum, Math.Max(nud.Minimum, value));
|
2016-07-29 23:00:56 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
public static void RemoveDropCB(object? sender, KeyEventArgs e)
|
|
|
|
{
|
|
|
|
if (sender == null)
|
|
|
|
return;
|
|
|
|
((ComboBox)sender).DroppedDown = false;
|
|
|
|
}
|
2018-07-14 23:00:28 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Iterates the Control's child controls recursively to obtain all controls of the specified type.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">Type of control</typeparam>
|
|
|
|
/// <param name="control"></param>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// <returns>All children and sub-children contained by <see cref="control"/>.</returns>
|
2022-06-18 18:04:24 +00:00
|
|
|
public static IEnumerable<Control> GetAllControlsOfType<T>(Control control) where T : Control
|
|
|
|
{
|
|
|
|
foreach (var c in control.Controls.Cast<Control>())
|
2018-08-04 17:06:06 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (c is T match)
|
|
|
|
yield return match;
|
|
|
|
foreach (var sub in GetAllControlsOfType<T>(c))
|
|
|
|
yield return sub;
|
2018-08-04 17:06:06 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-08-04 17:06:06 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Reads in custom extension types that allow the program to open more extensions.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="exts">Extensions to add</param>
|
|
|
|
public static void AddSaveFileExtensions(IEnumerable<string> exts)
|
|
|
|
{
|
|
|
|
// Only add new (unique) extensions
|
|
|
|
var dest = CustomSaveExtensions;
|
|
|
|
foreach (var ext in exts)
|
2018-07-14 23:00:28 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
if (!dest.Contains(ext))
|
|
|
|
dest.Add(ext);
|
2018-07-14 23:00:28 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-14 23:00:28 +00:00
|
|
|
|
2024-05-28 00:23:56 +00:00
|
|
|
private static List<string> CustomSaveExtensions => SaveFileMetadata.CustomSaveExtensions;
|
2022-06-18 18:04:24 +00:00
|
|
|
|
2024-03-19 00:05:22 +00:00
|
|
|
public static bool IsFileExtensionSAV(ReadOnlySpan<char> file)
|
|
|
|
{
|
|
|
|
var ext = Path.GetExtension(file);
|
|
|
|
foreach (var other in CustomSaveExtensions)
|
|
|
|
{
|
|
|
|
if (ext.EndsWith(other))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
private static string ExtraSaveExtensions => ";" + string.Join(";", CustomSaveExtensions.Select(z => $"*.{z}"));
|
|
|
|
|
|
|
|
public static bool DetectSaveFileOnFileOpen { private get; set; } = true;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Opens a dialog to open a <see cref="SaveFile"/>, <see cref="PKM"/> file, or any other supported file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="extensions">Misc extensions of <see cref="PKM"/> files supported by the Save File.</param>
|
|
|
|
/// <param name="path">Output result path</param>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// <returns>Result of the dialog menu indicating if a file is to be loaded from the output path.</returns>
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool OpenSAVPKMDialog(IEnumerable<string> extensions, out string? path)
|
|
|
|
{
|
2023-12-04 04:13:20 +00:00
|
|
|
var sb = new StringBuilder(128);
|
|
|
|
foreach (var type in extensions)
|
|
|
|
sb.Append("*.").Append(type).Append(';');
|
|
|
|
sb.Append("*.pk");
|
|
|
|
|
|
|
|
string supported = sb.ToString();
|
2023-10-15 02:26:56 +00:00
|
|
|
using var ofd = new OpenFileDialog();
|
|
|
|
ofd.Filter = "All Files|*.*" +
|
2022-06-18 18:04:24 +00:00
|
|
|
$"|Supported Files (*.*)|main;*.bin;{supported};*.bak" + ExtraSaveExtensions +
|
|
|
|
"|Save Files (*.sav)|main" + ExtraSaveExtensions +
|
|
|
|
"|Decrypted PKM File (*.pk)|" + supported +
|
|
|
|
"|Binary File|*.bin" +
|
2023-10-15 02:26:56 +00:00
|
|
|
"|Backup File|*.bak";
|
2018-07-22 02:20:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Detect main
|
|
|
|
SaveFile? sav = null;
|
|
|
|
if (DetectSaveFileOnFileOpen)
|
2017-06-18 04:49:14 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
sav = SaveFinder.FindMostRecentSaveFile();
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
2019-01-05 23:40:25 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
Error(ex.Message);
|
2019-01-05 23:40:25 +00:00
|
|
|
}
|
2017-06-18 04:49:14 +00:00
|
|
|
}
|
2017-01-08 07:54:09 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (sav != null)
|
|
|
|
ofd.FileName = sav.Metadata.FileName;
|
|
|
|
|
|
|
|
if (ofd.ShowDialog() != DialogResult.OK)
|
2019-07-14 22:06:45 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
path = null;
|
|
|
|
return false;
|
2019-07-14 22:06:45 +00:00
|
|
|
}
|
2018-07-22 02:20:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
path = ofd.FileName;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Opens a dialog to save a <see cref="PKM"/> file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="pk"><see cref="PKM"/> file to be saved.</param>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// <returns>True if the file was saved.</returns>
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool SavePKMDialog(PKM pk)
|
|
|
|
{
|
|
|
|
string pkx = pk.Extension;
|
|
|
|
bool allowEncrypted = pk.Format >= 3 && pkx[0] == 'p';
|
|
|
|
var genericFilter = $"Decrypted PKM File|*.{pkx}" +
|
|
|
|
(allowEncrypted ? $"|Encrypted PKM File|*.e{pkx[1..]}" : string.Empty) +
|
|
|
|
"|Binary File|*.bin" +
|
|
|
|
"|All Files|*.*";
|
2023-10-15 02:26:56 +00:00
|
|
|
using var sfd = new SaveFileDialog();
|
|
|
|
sfd.Filter = genericFilter;
|
|
|
|
sfd.DefaultExt = pkx;
|
|
|
|
sfd.FileName = Util.CleanFileName(pk.FileName);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (sfd.ShowDialog() != DialogResult.OK)
|
|
|
|
return false;
|
2018-07-22 02:20:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
SavePKM(pk, sfd.FileName, pkx);
|
|
|
|
return true;
|
|
|
|
}
|
2018-05-22 02:12:02 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void SavePKM(PKM pk, string path, string pkx)
|
|
|
|
{
|
|
|
|
SaveBackup(path);
|
|
|
|
string ext = Path.GetExtension(path);
|
|
|
|
var data = $".{pkx}" == ext ? pk.DecryptedPartyData : pk.EncryptedPartyData;
|
|
|
|
File.WriteAllBytes(path, data);
|
|
|
|
}
|
2019-02-23 07:04:10 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void SaveBackup(string path)
|
|
|
|
{
|
|
|
|
if (!File.Exists(path))
|
|
|
|
return;
|
2018-07-15 20:35:58 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// File already exists, save a .bak
|
|
|
|
string bakpath = $"{path}.bak";
|
|
|
|
if (!File.Exists(bakpath))
|
|
|
|
File.Move(path, bakpath);
|
|
|
|
}
|
2017-05-18 05:00:06 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Opens a dialog to save a <see cref="SaveFile"/> file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="sav"><see cref="SaveFile"/> to be saved.</param>
|
|
|
|
/// <param name="currentBox">Box the player will be greeted with when accessing the PC ingame.</param>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// <returns>True if the file was saved.</returns>
|
2022-06-18 18:04:24 +00:00
|
|
|
public static bool ExportSAVDialog(SaveFile sav, int currentBox = 0)
|
|
|
|
{
|
2023-10-15 02:26:56 +00:00
|
|
|
using var sfd = new SaveFileDialog();
|
|
|
|
sfd.Filter = sav.Metadata.Filter;
|
|
|
|
sfd.FileName = sav.Metadata.FileName;
|
|
|
|
sfd.FilterIndex = 1000; // default to last, All Files
|
|
|
|
sfd.RestoreDirectory = true;
|
2022-06-18 18:04:24 +00:00
|
|
|
if (Directory.Exists(sav.Metadata.FileFolder))
|
|
|
|
sfd.InitialDirectory = sav.Metadata.FileFolder;
|
2017-05-18 05:00:06 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
if (sfd.ShowDialog() != DialogResult.OK)
|
|
|
|
return false;
|
2018-07-22 02:20:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
// Set box now that we're saving
|
|
|
|
if (sav.HasBox)
|
|
|
|
sav.CurrentBox = currentBox;
|
2018-07-22 02:20:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
var path = sfd.FileName;
|
2023-02-23 09:07:46 +00:00
|
|
|
ArgumentNullException.ThrowIfNull(path);
|
2022-06-18 18:04:24 +00:00
|
|
|
ExportSAV(sav, path);
|
|
|
|
return true;
|
|
|
|
}
|
2018-02-22 04:35:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
private static void ExportSAV(SaveFile sav, string path)
|
|
|
|
{
|
|
|
|
var ext = Path.GetExtension(path).ToLowerInvariant();
|
|
|
|
var flags = sav.Metadata.GetSuggestedFlags(ext);
|
2018-02-22 04:35:07 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
try
|
2017-05-18 05:00:06 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
File.WriteAllBytes(path, sav.Write(flags));
|
|
|
|
sav.State.Edited = false;
|
|
|
|
sav.Metadata.SetExtraInfo(path);
|
|
|
|
Alert(MsgSaveExportSuccessPath, path);
|
2019-02-17 03:53:14 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
catch (Exception x)
|
2019-02-17 03:53:14 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
switch (x)
|
2017-05-18 05:00:06 +00:00
|
|
|
{
|
2022-06-18 18:04:24 +00:00
|
|
|
case UnauthorizedAccessException:
|
|
|
|
case FileNotFoundException:
|
|
|
|
case IOException:
|
|
|
|
Error(MsgFileWriteFail + Environment.NewLine + x.Message, MsgFileWriteProtectedAdvice);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw;
|
2017-05-18 05:00:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
}
|
2018-07-22 02:20:11 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Opens a dialog to save a <see cref="MysteryGift"/> file.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="gift"><see cref="MysteryGift"/> to be saved.</param>
|
2023-12-04 04:13:20 +00:00
|
|
|
/// <returns>True if the file was saved.</returns>
|
2022-11-25 01:42:17 +00:00
|
|
|
public static bool ExportMGDialog(DataMysteryGift gift)
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2023-10-15 02:26:56 +00:00
|
|
|
using var sfd = new SaveFileDialog();
|
|
|
|
sfd.Filter = GetMysterGiftFilter(gift.Context);
|
|
|
|
sfd.FileName = Util.CleanFileName(gift.FileName);
|
2022-06-18 18:04:24 +00:00
|
|
|
if (sfd.ShowDialog() != DialogResult.OK)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
string path = sfd.FileName;
|
|
|
|
SaveBackup(path);
|
2021-01-02 01:08:49 +00:00
|
|
|
|
2022-06-18 18:04:24 +00:00
|
|
|
File.WriteAllBytes(path, gift.Write());
|
|
|
|
return true;
|
2016-07-09 22:30:12 +00:00
|
|
|
}
|
2022-06-18 18:04:24 +00:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets the File Dialog filter for a Mystery Gift I/O operation.
|
|
|
|
/// </summary>
|
2022-11-25 01:42:17 +00:00
|
|
|
/// <param name="context">Context specifier for the </param>
|
|
|
|
public static string GetMysterGiftFilter(EntityContext context) => context switch
|
2022-06-18 18:04:24 +00:00
|
|
|
{
|
2022-11-25 01:42:17 +00:00
|
|
|
EntityContext.Gen4 => "Gen4 Mystery Gift|*.pgt;*.pcd;*.wc4" + all,
|
2023-04-04 14:19:54 +00:00
|
|
|
EntityContext.Gen5 => "Gen5 Mystery Gift|*.pgf;*.wc5full" + all,
|
2022-11-25 01:42:17 +00:00
|
|
|
EntityContext.Gen6 => "Gen6 Mystery Gift|*.wc6;*.wc6full" + all,
|
|
|
|
EntityContext.Gen7 => "Gen7 Mystery Gift|*.wc7;*.wc7full" + all,
|
|
|
|
EntityContext.Gen8 => "Gen8 Mystery Gift|*.wc8" + all,
|
|
|
|
EntityContext.Gen9 => "Gen9 Mystery Gift|*.wc9" + all,
|
|
|
|
|
|
|
|
EntityContext.Gen7b => "Beluga Gift Record|*.wr7" + all,
|
|
|
|
EntityContext.Gen8b => "BD/SP Gift|*.wb8" + all,
|
|
|
|
EntityContext.Gen8a => "Legends: Arceus Gift|*.wa8" + all,
|
2022-06-18 18:04:24 +00:00
|
|
|
_ => string.Empty,
|
|
|
|
};
|
|
|
|
|
|
|
|
private const string all = "|All Files|*.*";
|
2016-07-09 22:30:12 +00:00
|
|
|
}
|