示例#1
0
        private static ModifyResult ProcessPKM(PKM PKM, IEnumerable <StringInstruction> Filters, IEnumerable <StringInstruction> Instructions)
        {
            if (!PKM.ChecksumValid || PKM.Species == 0)
            {
                return(ModifyResult.Invalid);
            }

            Type pkm = PKM.GetType();

            foreach (var cmd in Filters)
            {
                try
                {
                    if (!pkm.HasProperty(cmd.PropertyName))
                    {
                        return(ModifyResult.Filtered);
                    }
                    if (ReflectUtil.GetValueEquals(PKM, cmd.PropertyName, cmd.PropertyValue) != cmd.Evaluator)
                    {
                        return(ModifyResult.Filtered);
                    }
                }
                catch
                {
                    Console.WriteLine($"Unable to compare {cmd.PropertyName} to {cmd.PropertyValue}.");
                    return(ModifyResult.Filtered);
                }
            }

            ModifyResult result = ModifyResult.Error;

            foreach (var cmd in Instructions)
            {
                try
                {
                    if (cmd.PropertyName == nameof(PKM.MetDate))
                    {
                        PKM.MetDate = DateTime.ParseExact(cmd.PropertyValue, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
                    }
                    else if (cmd.PropertyName == nameof(PKM.EggMetDate))
                    {
                        PKM.EggMetDate = DateTime.ParseExact(cmd.PropertyValue, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None);
                    }
                    else if (cmd.PropertyName == nameof(PKM.EncryptionConstant) && cmd.PropertyValue == CONST_RAND)
                    {
                        ReflectUtil.SetValue(PKM, cmd.PropertyName, Util.rnd32().ToString());
                    }
                    else if (cmd.PropertyName == nameof(PKM.PID) && cmd.PropertyValue == CONST_RAND)
                    {
                        PKM.setPIDGender(PKM.Gender);
                    }
                    else if (cmd.PropertyName == nameof(PKM.EncryptionConstant) && cmd.PropertyValue == nameof(PKM.PID))
                    {
                        PKM.EncryptionConstant = PKM.PID;
                    }
                    else if (cmd.PropertyName == nameof(PKM.PID) && cmd.PropertyValue == CONST_SHINY)
                    {
                        PKM.setShinyPID();
                    }
                    else if (cmd.PropertyName == nameof(PKM.Species) && cmd.PropertyValue == "0")
                    {
                        PKM.Data = new byte[PKM.Data.Length];
                    }
                    else if (cmd.PropertyName.StartsWith("IV") && cmd.PropertyValue == CONST_RAND)
                    {
                        setRandomIVs(PKM, cmd);
                    }
                    else if (cmd.Random)
                    {
                        ReflectUtil.SetValue(PKM, cmd.PropertyName, cmd.RandomValue);
                    }
                    else
                    {
                        ReflectUtil.SetValue(PKM, cmd.PropertyName, cmd.PropertyValue);
                    }

                    result = ModifyResult.Modified;
                }
                catch { Console.WriteLine($"Unable to set {cmd.PropertyName} to {cmd.PropertyValue}."); }
            }
            return(result);
        }
        // View Updates
        private void B_Search_Click(object sender, EventArgs e)
        {
            // Populate Search Query Result
            IEnumerable <MysteryGift> res = RawDB;

            int format = MAXFORMAT + 1 - CB_Format.SelectedIndex;

            switch (CB_FormatComparator.SelectedIndex)
            {
            case 0: /* Do nothing */ break;

            case 1: res = res.Where(mg => mg.Format >= format); break;

            case 2: res = res.Where(mg => mg.Format == format); break;

            case 3: res = res.Where(mg => mg.Format <= format); break;
            }

            // Primary Searchables
            int species = WinFormsUtil.getIndex(CB_Species);
            int item    = WinFormsUtil.getIndex(CB_HeldItem);

            if (species != -1)
            {
                res = res.Where(pk => pk.Species == species);
            }
            if (item != -1)
            {
                res = res.Where(pk => pk.HeldItem == item);
            }

            // Secondary Searchables
            int move1 = WinFormsUtil.getIndex(CB_Move1);
            int move2 = WinFormsUtil.getIndex(CB_Move2);
            int move3 = WinFormsUtil.getIndex(CB_Move3);
            int move4 = WinFormsUtil.getIndex(CB_Move4);

            if (move1 != -1)
            {
                res = res.Where(pk => pk.Moves.Contains(move1));
            }
            if (move2 != -1)
            {
                res = res.Where(pk => pk.Moves.Contains(move2));
            }
            if (move3 != -1)
            {
                res = res.Where(pk => pk.Moves.Contains(move3));
            }
            if (move4 != -1)
            {
                res = res.Where(pk => pk.Moves.Contains(move4));
            }
            if (CHK_Shiny.CheckState == CheckState.Checked)
            {
                res = res.Where(pk => pk.IsShiny);
            }
            if (CHK_Shiny.CheckState == CheckState.Unchecked)
            {
                res = res.Where(pk => !pk.IsShiny);
            }
            if (CHK_IsEgg.CheckState == CheckState.Checked)
            {
                res = res.Where(pk => pk.IsEgg);
            }
            if (CHK_IsEgg.CheckState == CheckState.Unchecked)
            {
                res = res.Where(pk => !pk.IsEgg);
            }

            slotSelected = -1; // reset the slot last viewed

            if (RTB_Instructions.Lines.Any(line => line.Length > 0))
            {
                var raw =
                    RTB_Instructions.Lines
                    .Where(line => !string.IsNullOrWhiteSpace(line))
                    .Where(line => new[] { '!', '=' }.Contains(line[0]));

                var filters = (from line in raw
                               let eval = line[0] == '='
                                          let split = line.Substring(1).Split('=')
                                                      where split.Length == 2 && !string.IsNullOrWhiteSpace(split[0])
                                                      select new BatchEditor.StringInstruction {
                    PropertyName = split[0], PropertyValue = split[1], Evaluator = eval
                }).ToArray();

                if (filters.Any(z => string.IsNullOrWhiteSpace(z.PropertyValue)))
                {
                    WinFormsUtil.Error("Empty Filter Value detected."); return;
                }

                res = res.Where(gift => // Compare across all filters
                {
                    foreach (var cmd in filters)
                    {
                        if (!gift.GetType().HasPropertyAll(cmd.PropertyName))
                        {
                            return(false);
                        }
                        try { if (ReflectUtil.GetValueEquals(gift, cmd.PropertyName, cmd.PropertyValue) == cmd.Evaluator)
                              {
                                  continue;
                              }
                        }
                        catch { Console.WriteLine($"Unable to compare {cmd.PropertyName} to {cmd.PropertyValue}."); }
                        return(false);
                    }
                    return(true);
                });
            }

            var results = res.ToArray();

            if (results.Length == 0)
            {
                WinFormsUtil.Alert("No results found!");
            }
            setResults(new List <MysteryGift>(results)); // updates Count Label as well.
            System.Media.SystemSounds.Asterisk.Play();
        }
示例#3
0
        private static ModifyResult tryModifyPKM(PKM PKM, IEnumerable <StringInstruction> Filters, IEnumerable <StringInstruction> Instructions)
        {
            if (!PKM.ChecksumValid || PKM.Species == 0)
            {
                return(ModifyResult.Invalid);
            }

            Type    pkm  = PKM.GetType();
            PKMInfo info = new PKMInfo(PKM);

            foreach (var cmd in Filters)
            {
                try
                {
                    if (cmd.PropertyName == PROP_LEGAL)
                    {
                        bool legal;
                        if (!bool.TryParse(cmd.PropertyValue, out legal))
                        {
                            return(ModifyResult.Error);
                        }
                        if (legal == info.Legal == cmd.Evaluator)
                        {
                            continue;
                        }
                        return(ModifyResult.Filtered);
                    }
                    if (!pkm.HasProperty(cmd.PropertyName))
                    {
                        return(ModifyResult.Filtered);
                    }
                    if (ReflectUtil.GetValueEquals(PKM, cmd.PropertyName, cmd.PropertyValue) != cmd.Evaluator)
                    {
                        return(ModifyResult.Filtered);
                    }
                }
                catch
                {
                    Console.WriteLine($"Unable to compare {cmd.PropertyName} to {cmd.PropertyValue}.");
                    return(ModifyResult.Filtered);
                }
            }

            ModifyResult result = ModifyResult.Error;

            foreach (var cmd in Instructions)
            {
                try
                {
                    if (cmd.PropertyValue == CONST_SUGGEST)
                    {
                        result = setSuggestedProperty(PKM, cmd, info)
                            ? ModifyResult.Modified
                            : ModifyResult.Error;
                    }
                    else
                    {
                        setProperty(PKM, cmd);
                        result = ModifyResult.Modified;
                    }
                }
                catch { Console.WriteLine($"Unable to set {cmd.PropertyName} to {cmd.PropertyValue}."); }
            }
            return(result);
        }
示例#4
0
        // View Updates
        private void B_Search_Click(object sender, EventArgs e)
        {
            // Populate Search Query Result
            IEnumerable <PKM> res = RawDB;

            int format = MAXFORMAT + 1 - CB_Format.SelectedIndex;

            switch (CB_FormatComparator.SelectedIndex)
            {
            case 0: /* Do nothing */ break;

            case 1: res = res.Where(pk => pk.Format >= format); break;

            case 2: res = res.Where(pk => pk.Format == format); break;

            case 3: res = res.Where(pk => pk.Format <= format); break;
            }
            if (CB_FormatComparator.SelectedIndex != 0)
            {
                if (format <= 2) // 1-2
                {
                    res = res.Where(pk => pk.Format <= 2);
                }
                if (format >= 3 && format <= 6) // 3-6
                {
                    res = res.Where(pk => pk.Format >= 3);
                }
                if (format >= 7) // 1,3-6,7
                {
                    res = res.Where(pk => pk.Format != 2);
                }
            }

            switch (CB_Generation.SelectedIndex)
            {
            case 0: /* Do nothing */ break;

            case 1: res = res.Where(pk => pk.Gen7); break;

            case 2: res = res.Where(pk => pk.Gen6); break;

            case 3: res = res.Where(pk => pk.Gen5); break;

            case 4: res = res.Where(pk => pk.Gen4); break;

            case 5: res = res.Where(pk => pk.Gen3); break;
            }

            // Primary Searchables
            int species = WinFormsUtil.getIndex(CB_Species);
            int ability = WinFormsUtil.getIndex(CB_Ability);
            int nature  = WinFormsUtil.getIndex(CB_Nature);
            int item    = WinFormsUtil.getIndex(CB_HeldItem);

            if (species != -1)
            {
                res = res.Where(pk => pk.Species == species);
            }
            if (ability != -1)
            {
                res = res.Where(pk => pk.Ability == ability);
            }
            if (nature != -1)
            {
                res = res.Where(pk => pk.Nature == nature);
            }
            if (item != -1)
            {
                res = res.Where(pk => pk.HeldItem == item);
            }

            // Secondary Searchables
            int move1 = WinFormsUtil.getIndex(CB_Move1);
            int move2 = WinFormsUtil.getIndex(CB_Move2);
            int move3 = WinFormsUtil.getIndex(CB_Move3);
            int move4 = WinFormsUtil.getIndex(CB_Move4);

            if (move1 != -1)
            {
                res = res.Where(pk => pk.Moves.Contains(move1));
            }
            if (move2 != -1)
            {
                res = res.Where(pk => pk.Moves.Contains(move2));
            }
            if (move3 != -1)
            {
                res = res.Where(pk => pk.Moves.Contains(move3));
            }
            if (move4 != -1)
            {
                res = res.Where(pk => pk.Moves.Contains(move4));
            }
            int vers = WinFormsUtil.getIndex(CB_GameOrigin);

            if (vers != -1)
            {
                res = res.Where(pk => pk.Version == vers);
            }
            int hptype = WinFormsUtil.getIndex(CB_HPType);

            if (hptype != -1)
            {
                res = res.Where(pk => pk.HPType == hptype);
            }
            if (CHK_Shiny.CheckState == CheckState.Checked)
            {
                res = res.Where(pk => pk.IsShiny);
            }
            if (CHK_Shiny.CheckState == CheckState.Unchecked)
            {
                res = res.Where(pk => !pk.IsShiny);
            }
            if (CHK_IsEgg.CheckState == CheckState.Checked)
            {
                res = res.Where(pk => pk.IsEgg);
            }
            if (CHK_IsEgg.CheckState == CheckState.Unchecked)
            {
                res = res.Where(pk => !pk.IsEgg);
            }
            if (CHK_IsEgg.CheckState == CheckState.Checked && MT_ESV.Text != "")
            {
                res = res.Where(pk => pk.PSV == Convert.ToInt16(MT_ESV.Text));
            }

            // Tertiary Searchables
            if (TB_Level.Text != "") // Level
            {
                int level = Convert.ToInt16(TB_Level.Text);
                if (level <= 100)
                {
                    switch (CB_Level.SelectedIndex)
                    {
                    case 0: break; // Any

                    case 1:        // <=
                        res = res.Where(pk => pk.Stat_Level <= level);
                        break;

                    case 2: // ==
                        res = res.Where(pk => pk.Stat_Level == level);
                        break;

                    case 3: // >=
                        res = res.Where(pk => pk.Stat_Level >= level);
                        break;
                    }
                }
            }
            switch (CB_IV.SelectedIndex)
            {
            case 0: break; // Do nothing

            case 1:        // <= 90
                res = res.Where(pk => pk.IVs.Sum() <= 90);
                break;

            case 2:     // 91-120
                res = res.Where(pk => pk.IVs.Sum() > 90 && pk.IVs.Sum() <= 120);
                break;

            case 3:     // 121-150
                res = res.Where(pk => pk.IVs.Sum() > 120 && pk.IVs.Sum() <= 150);
                break;

            case 4:     // 151-179
                res = res.Where(pk => pk.IVs.Sum() > 150 && pk.IVs.Sum() < 180);
                break;

            case 5:     // 180+
                res = res.Where(pk => pk.IVs.Sum() >= 180);
                break;

            case 6:     // == 186
                res = res.Where(pk => pk.IVs.Sum() == 186);
                break;
            }
            switch (CB_EVTrain.SelectedIndex)
            {
            case 0: break; // Do nothing

            case 1:        // None (0)
                res = res.Where(pk => pk.EVs.Sum() == 0);
                break;

            case 2:     // Some (127-0)
                res = res.Where(pk => pk.EVs.Sum() < 128);
                break;

            case 3:     // Half (128-507)
                res = res.Where(pk => pk.EVs.Sum() >= 128 && pk.EVs.Sum() < 508);
                break;

            case 4:     // Full (508+)
                res = res.Where(pk => pk.EVs.Sum() >= 508);
                break;
            }

            // Filter for Selected Source
            if (!Menu_SearchBoxes.Checked)
            {
                res = res.Where(pk => pk.Identifier.StartsWith(DatabasePath + Path.DirectorySeparatorChar, StringComparison.Ordinal));
            }
            if (!Menu_SearchDatabase.Checked)
            {
                res = res.Where(pk => !pk.Identifier.StartsWith(DatabasePath + Path.DirectorySeparatorChar, StringComparison.Ordinal));
            }

            slotSelected = -1;                                           // reset the slot last viewed

            if (Menu_SearchLegal.Checked && !Menu_SearchIllegal.Checked) // Legal Only
            {
                res = res.Where(pk => pk.GenNumber >= 6 && new LegalityAnalysis(pk).Valid);
            }
            if (!Menu_SearchLegal.Checked && Menu_SearchIllegal.Checked) // Illegal Only
            {
                res = res.Where(pk => pk.GenNumber >= 6 && !new LegalityAnalysis(pk).Valid);
            }

            if (RTB_Instructions.Lines.Any(line => line.Length > 0))
            {
                var raw =
                    RTB_Instructions.Lines
                    .Where(line => !string.IsNullOrWhiteSpace(line))
                    .Where(line => new[] { '!', '=' }.Contains(line[0]));

                var filters = (from line in raw
                               let eval = line[0] == '='
                                          let split = line.Substring(1).Split('=')
                                                      where split.Length == 2 && !string.IsNullOrWhiteSpace(split[0])
                                                      select new BatchEditor.StringInstruction {
                    PropertyName = split[0], PropertyValue = split[1], Evaluator = eval
                }).ToArray();

                BatchEditor.screenStrings(filters);
                res = res.Where(pkm => // Compare across all filters
                {
                    foreach (var cmd in filters)
                    {
                        if (!pkm.GetType().HasPropertyAll(cmd.PropertyName))
                        {
                            return(false);
                        }
                        try { if (ReflectUtil.GetValueEquals(pkm, cmd.PropertyName, cmd.PropertyValue) == cmd.Evaluator)
                              {
                                  continue;
                              }
                        }
                        catch { Console.WriteLine($"Unable to compare {cmd.PropertyName} to {cmd.PropertyValue}."); }
                        return(false);
                    }
                    return(true);
                });
            }

            if (Menu_SearchClones.Checked)
            {
                var r      = res.ToArray();
                var hashes = r.Select(hash).ToArray();
                res = r.Where((t, i) => hashes.Count(x => x == hashes[i]) > 1).OrderBy(hash);
            }

            var results = res.ToArray();

            if (results.Length == 0)
            {
                if (!Menu_SearchBoxes.Checked && !Menu_SearchDatabase.Checked)
                {
                    WinFormsUtil.Alert("No data source to search!", "No results found!");
                }
                else
                {
                    WinFormsUtil.Alert("No results found!");
                }
            }
            setResults(new List <PKM>(results)); // updates Count Label as well.
            System.Media.SystemSounds.Asterisk.Play();
        }