Improve supported extension openfiledialog

Existing code supported 3-x, wasn't updated for gen1/2.
Allows gens1/2 to override to their own gen format filter, and allows
gen7 to allow finding of pk1 files (not yet converted).

Noticed the backup would overwrite an existing backup; thus we only back
up if the target file exists AND the backup doesn't already exist.
This commit is contained in:
Kurt 2017-01-04 22:22:50 -08:00
parent 769f58ea97
commit ed47e389d4
7 changed files with 51 additions and 26 deletions

View file

@ -270,14 +270,7 @@ namespace PKHeX
string pkx = pkm.Extension;
string ekx = 'e' + pkx.Substring(1, pkx.Length-1);
string supported = "*.pkm;";
for (int i = 3; i <= SAV.Generation; i++)
{
supported += $"*.pk{i}";
if (i != pkm.Format)
supported += ";";
}
string supported = string.Join(";", SAV.PKMExtensions.Select(s => "*."+s).Concat(new[] {"*.pkm"}));
OpenFileDialog ofd = new OpenFileDialog
{
Filter = $"Supported Files|main;*.sav;*.bin;*.{ekx};{supported};*.bak" +
@ -320,8 +313,12 @@ namespace PKHeX
if (File.Exists(path))
{
// File already exists, save a .bak
byte[] backupfile = File.ReadAllBytes(path);
File.WriteAllBytes(path + ".bak", backupfile);
string bakpath = path + ".bak";
if (!File.Exists(bakpath))
{
byte[] backupfile = File.ReadAllBytes(path);
File.WriteAllBytes(bakpath, backupfile);
}
}
if (new[] {".ekx", "."+ekx, ".bin"}.Contains(ext))

View file

@ -6,6 +6,7 @@ namespace PKHeX
{
public abstract class PKM
{
public static readonly string[] Extensions = PKX.getPKMExtensions();
public abstract int SIZE_PARTY { get; }
public abstract int SIZE_STORED { get; }
public virtual string Extension => "pk" + Format;

View file

@ -1385,7 +1385,7 @@ namespace PKHeX
default: return (PID & 0xFF) <= genderratio ? 1 : 0;
}
}
#region Gen 3 Species Table
#region Gen 3 Species Table
internal static int[] newindex => new[]
{
0,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,
@ -1429,8 +1429,8 @@ namespace PKHeX
388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,
409,410,411,
};
#endregion
#region Gen 3/4 Character Tables (Val->Unicode)
#endregion
#region Gen 3/4 Character Tables (Val->Unicode)
internal static readonly ushort[] G4Values =
{
@ -1821,7 +1821,7 @@ namespace PKHeX
209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 289, // E
452, 355, 373, 379, 387, 405, 411 // F
};
#endregion
#endregion
public static readonly byte[][] G4TransferTrashBytes = {
new byte[] { }, // Unused
@ -1964,7 +1964,7 @@ namespace PKHeX
return item == ITEM_UNK && item > 0;
}
#region Gen 1 Character Tables
#region Gen 1 Character Tables
private static Dictionary<byte, string> RBY2U_U => new Dictionary<byte, string>{
{0x50, "\0"},
{0x5D, "[TRAINER]"},
@ -2472,7 +2472,7 @@ namespace PKHeX
{0xFE, "8"},
{0xFF, "9"}
};
#endregion
#endregion
public static int getG1Species(int raw_id)
{
@ -2523,5 +2523,15 @@ namespace PKHeX
.PadRight(value.Length + 1, (char)0); // Null Terminator
return Encoding.BigEndianUnicode.GetBytes(TempNick);
}
public static string[] getPKMExtensions()
{
const int gens = 7;
var result = new List<string>();
result.AddRange(new [] {"ck3", "xk3", "bk4"}); // Special Cases
for (int i = 1; i < gens; i++)
result.Add("pk"+i);
return result.ToArray();
}
}
}

View file

@ -8,6 +8,11 @@ namespace PKHeX
public override string BAKName => $"{FileName} [{OT} ({Version}) - {PlayTimeString}].bak";
public override string Filter => "SAV File|*.sav|All Files|*.*";
public override string Extension => ".sav";
public override string[] PKMExtensions => PKM.Extensions.Where(f =>
{
int gen = f.Last() - 0x30;
return 1 <= gen && gen <= 2;
}).ToArray();
public SAV1(byte[] data = null)
{
@ -153,10 +158,8 @@ namespace PKHeX
public override int SIZE_STORED => Japanese ? PKX.SIZE_1JLIST : PKX.SIZE_1ULIST;
public override int SIZE_PARTY => Japanese ? PKX.SIZE_1JLIST : PKX.SIZE_1ULIST;
public int SIZE_BOX => BoxSlotCount*SIZE_STORED;
public int SIZE_STOREDBOX => PokemonList1.GetDataLength(Japanese ? PokemonList1.CapacityType.StoredJP : PokemonList1.CapacityType.Stored, Japanese);
private int SIZE_BOX => BoxSlotCount*SIZE_STORED;
private int SIZE_STOREDBOX => PokemonList1.GetDataLength(Japanese ? PokemonList1.CapacityType.StoredJP : PokemonList1.CapacityType.Stored, Japanese);
public override PKM BlankPKM => new PK1(null, null, Japanese);
public override Type PKMType => typeof(PK1);

View file

@ -8,6 +8,11 @@ namespace PKHeX
public override string BAKName => $"{FileName} [{OT} ({Version}) - {PlayTimeString}].bak";
public override string Filter => "SAV File|*.sav|All Files|*.*";
public override string Extension => ".sav";
public override string[] PKMExtensions => PKM.Extensions.Where(f =>
{
int gen = f.Last() - 0x30;
return 1 <= gen && gen <= 2;
}).ToArray();
public SAV2(byte[] data = null)
{
@ -202,14 +207,12 @@ namespace PKHeX
public override int SIZE_STORED => Japanese ? PKX.SIZE_2JLIST : PKX.SIZE_2ULIST;
public override int SIZE_PARTY => Japanese ? PKX.SIZE_2JLIST : PKX.SIZE_2ULIST;
public int SIZE_BOX => BoxSlotCount*SIZE_STORED;
public int SIZE_STOREDBOX => PokemonList2.GetDataLength(Japanese ? PokemonList2.CapacityType.StoredJP : PokemonList2.CapacityType.Stored, Japanese);
public override PKM BlankPKM => new PK2(null, null, Japanese);
public override Type PKMType => typeof(PK2);
private int SIZE_BOX => BoxSlotCount*SIZE_STORED;
private int SIZE_STOREDBOX => PokemonList2.GetDataLength(Japanese ? PokemonList2.CapacityType.StoredJP : PokemonList2.CapacityType.Stored, Japanese);
public override int MaxMoveID => 251;
public override int MaxSpeciesID => Legal.MaxSpeciesID_2;
public override int MaxAbilityID => 0;

View file

@ -10,6 +10,12 @@ namespace PKHeX
public override string BAKName => $"{FileName} [{OT} ({Version}) - {LastSavedTime}].bak";
public override string Filter => "Main SAV|*.*";
public override string Extension => "";
public override string[] PKMExtensions => PKM.Extensions.Where(f =>
{
int gen = f.Last() - 0x30;
return gen == 1 || (3 <= gen && gen <= 7);
}).ToArray();
public SAV7(byte[] data = null)
{
Data = data == null ? new byte[SaveUtil.SIZE_G7SM] : (byte[])data.Clone();

View file

@ -25,6 +25,12 @@ namespace PKHeX
public virtual bool IndeterminateGame => false;
public virtual bool IndeterminateLanguage => false;
public virtual bool IndeterminateSubVersion => false;
public abstract string Extension { get; }
public virtual string[] PKMExtensions => PKM.Extensions.Where(f =>
{
int gen = f.Last() - 0x30;
return 3 <= gen && gen <= 7;
}).ToArray();
// General PKM Properties
public abstract Type PKMType { get; }
@ -322,7 +328,6 @@ namespace PKHeX
public virtual uint Money { get; set; }
public abstract int BoxCount { get; }
public virtual int PartyCount { get; protected set; }
public abstract string Extension { get; }
// Varied Methods
protected abstract void setChecksums();