From 68c782e6613357b7bb40f0f91dddf5340e29a9bd Mon Sep 17 00:00:00 2001 From: Kurt Date: Mon, 12 Feb 2018 17:36:15 -0800 Subject: [PATCH] Handle version mega import/export Closes #1832 --- PKHeX.Core/PKM/ShowdownSet.cs | 68 ++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 24 deletions(-) diff --git a/PKHeX.Core/PKM/ShowdownSet.cs b/PKHeX.Core/PKM/ShowdownSet.cs index 6bab64b21..be36c8732 100644 --- a/PKHeX.Core/PKM/ShowdownSet.cs +++ b/PKHeX.Core/PKM/ShowdownSet.cs @@ -270,36 +270,49 @@ private void ParseFirstLine(string line) } // Nickname Detection - string spec = line; - if (spec.Contains("(") && spec.Contains(")")) - ParseSpeciesNickname(ref spec); - + if (line.Contains("(") && line.Contains(")")) + ParseSpeciesNickname(line); + else + ParseSpeciesForm(line); + } + private bool ParseSpeciesForm(string spec) + { spec = spec.Trim(); if ((Species = Array.IndexOf(species, spec)) >= 0) // success, nothing else! - return; + return true; // Forme string present. int end = spec.LastIndexOf('-'); if (end < 0) - return; + return false; Species = Array.IndexOf(species, spec.Substring(0, end).Trim()); Form = spec.Substring(end + 1); - if (Species < 0) // failure to parse, check edge cases + if (Species >= 0) + return false; + + // failure to parse, check edge cases + var edge = new[] {784, 250}; // all species with dashes in English Name (Kommo-o & Ho-Oh) + foreach (var e in edge) { - var edge = new[] {784, 250}; // all species with dashes in English Name (Kommo-o & Ho-Oh) - foreach (var e in edge) - { - if (!spec.StartsWith(species[e])) - continue; - Species = e; - Form = spec.Substring(species[e].Length); - return; - } + if (!spec.StartsWith(species[e])) + continue; + Species = e; + Form = spec.Substring(species[e].Length); + return false; } + + // Version Megas + end = spec.LastIndexOf('-', Math.Max(0, end - 1)); + if (end < 0) + return false; + Species = Array.IndexOf(species, spec.Substring(0, end).Trim()); + Form = spec.Substring(end + 1); + + return Species >= 0; } - private void ParseSpeciesNickname(ref string line) + private void ParseSpeciesNickname(string line) { int index = line.LastIndexOf("(", StringComparison.Ordinal); string n1, n2; @@ -316,12 +329,15 @@ private void ParseSpeciesNickname(ref string line) n1 = line.Substring(end + 2); } - int dash = n2.LastIndexOf('-'); - if (dash < 0) dash = n2.Length; - bool inverted = Array.IndexOf(species, n2.Replace(" ", string.Empty)) > -1 - || (Species = Array.IndexOf(species, n2.Substring(0, dash))) > 0; - line = inverted ? n2 : n1; - Nickname = inverted ? n1 : n2; + if (ParseSpeciesForm(n2)) + { + // successful parse on n2=>Species/Form, n1 is nickname + Nickname = n1; + return; + } + // other case is possibly true (or both invalid). + Nickname = n2; + ParseSpeciesForm(n1); } private string ParseLineMove(string line) { @@ -483,6 +499,8 @@ private static string ConvertFormToShowdown(string form, int spec) default: if (Legal.Totem_USUM.Contains(spec) && form == "Large") return Legal.Totem_Alolan.Contains(spec) ? "Alola-Totem" : "Totem"; + if (form.StartsWith("Mega")) + return form.Replace(" ", "-"); return form; } } @@ -524,9 +542,11 @@ private static string ConvertFormFromShowdown(string form, int spec, int ability default: if (form == null) - return form; + return null; if (Legal.Totem_USUM.Contains(spec) && form.EndsWith("Totem")) return "Large"; + if (form.StartsWith("Mega")) + return form.Replace("-", " "); return form; } }