/// <summary> /// Sets the if the <see cref="PKMInfo"/> should be filtered due to the <see cref="StringInstruction"/> provided. /// </summary> /// <param name="cmd">Command Filter</param> /// <param name="info">Pokémon to check.</param> /// <param name="props">PropertyInfo cache (optional)</param> /// <returns>True if filtered, else false.</returns> private static ModifyResult SetPKMProperty(StringInstruction cmd, PKMInfo info, IReadOnlyDictionary <string, PropertyInfo> props) { var pkm = info.pkm; if (cmd.PropertyValue.StartsWith(CONST_BYTES)) { return(SetByteArrayProperty(pkm, cmd)); } if (cmd.PropertyValue == CONST_SUGGEST) { return(SetSuggestedPKMProperty(cmd.PropertyName, info)); } if (cmd.PropertyValue == CONST_RAND && cmd.PropertyName == nameof(PKM.Moves)) { return(SetMoves(pkm, pkm.GetMoveSet(true, info.Legality))); } if (SetComplexProperty(pkm, cmd)) { return(ModifyResult.Modified); } if (!props.TryGetValue(cmd.PropertyName, out var pi)) { return(ModifyResult.Error); } object val = cmd.Random ? (object)cmd.RandomValue : cmd.PropertyValue; ReflectUtil.SetValue(pi, pkm, val); return(ModifyResult.Modified); }
/// <summary> /// Checks if the <see cref="PKMInfo"/> should be filtered due to the <see cref="StringInstruction"/> provided. /// </summary> /// <param name="cmd">Command Filter</param> /// <param name="info">Pokémon to check.</param> /// <param name="props">PropertyInfo cache (optional)</param> /// <returns>True if filter matches, else false.</returns> private static bool IsFilterMatch(StringInstruction cmd, PKMInfo info, IReadOnlyDictionary <string, PropertyInfo> props) { if (IsLegalFiltered(cmd, () => info.Legal)) { return(true); } return(IsPropertyFiltered(cmd, info.pkm, props)); }
/// <summary> /// Sets the <see cref="PKM"/> data with a suggested value based on its <see cref="LegalityAnalysis"/>. /// </summary> /// <param name="name">Property to modify.</param> /// <param name="info">Cached info storing Legal data.</param> private static ModifyResult SetSuggestedPKMProperty(string name, PKMInfo info) { var pk = info.pkm; switch (name) { // pb7 only case nameof(PB7.Stat_CP) when pk is PB7 pb7: pb7.ResetCP(); return(ModifyResult.Modified); case nameof(PB7.HeightAbsolute) when pk is PB7 pb7: pb7.HeightAbsolute = pb7.CalcHeightAbsolute; return(ModifyResult.Modified); case nameof(PB7.WeightAbsolute) when pk is PB7 pb7: pb7.WeightAbsolute = pb7.CalcWeightAbsolute; return(ModifyResult.Modified); case nameof(PKM.Stats): pk.ResetPartyStats(); return(ModifyResult.Modified); case nameof(IHyperTrain.HyperTrainFlags): pk.SetSuggestedHyperTrainingData(); return(ModifyResult.Modified); case nameof(PKM.RelearnMoves): pk.SetRelearnMoves(info.SuggestedRelearn); return(ModifyResult.Modified); case nameof(PKM.Met_Location): var encounter = info.SuggestedEncounter; if (encounter == null) { return(ModifyResult.Error); } int level = encounter.Level; int location = encounter.Location; int minlvl = Legal.GetLowestLevel(pk, encounter.LevelMin); pk.Met_Level = level; pk.Met_Location = location; pk.CurrentLevel = Math.Max(minlvl, level); return(ModifyResult.Modified); case nameof(PKM.Moves): return(SetMoves(pk, pk.GetMoveSet(la: info.Legality))); default: return(ModifyResult.Error); } }
/// <summary> /// Tries to modify the <see cref="PKMInfo"/>. /// </summary> /// <param name="pk">Command Filter</param> /// <param name="filters">Filters which must be satisfied prior to any modifications being made.</param> /// <param name="modifications">Modifications to perform on the <see cref="pk"/>.</param> /// <returns>Result of the attempted modification.</returns> internal static ModifyResult TryModifyPKM(PKM pk, IEnumerable <StringInstruction> filters, IEnumerable <StringInstruction> modifications) { if (!pk.ChecksumValid || pk.Species == 0) { return(ModifyResult.Invalid); } PKMInfo info = new PKMInfo(pk); var pi = Props[Array.IndexOf(Types, pk.GetType())]; foreach (var cmd in filters) { try { if (!IsFilterMatch(cmd, info, pi)) { return(ModifyResult.Filtered); } } #pragma warning disable CA1031 // Do not catch general exception types // Swallow any error because this can be malformed user input. catch (Exception ex) #pragma warning restore CA1031 // Do not catch general exception types { Debug.WriteLine(MsgBEModifyFailCompare + " " + ex.Message, cmd.PropertyName, cmd.PropertyValue); return(ModifyResult.Error); } } ModifyResult result = ModifyResult.Modified; foreach (var cmd in modifications) { try { var tmp = SetPKMProperty(cmd, info, pi); if (tmp != ModifyResult.Modified) { result = tmp; } } #pragma warning disable CA1031 // Do not catch general exception types // Swallow any error because this can be malformed user input. catch (Exception ex) #pragma warning restore CA1031 // Do not catch general exception types { Debug.WriteLine(MsgBEModifyFail + " " + ex.Message, cmd.PropertyName, cmd.PropertyValue); } } return(result); }
/// <summary> /// Tries to modify the <see cref="PKMInfo"/>. /// </summary> /// <param name="pkm">Command Filter</param> /// <param name="filters">Filters which must be satisfied prior to any modifications being made.</param> /// <param name="modifications">Modifications to perform on the <see cref="pkm"/>.</param> /// <returns>Result of the attempted modification.</returns> internal static ModifyResult TryModifyPKM(PKM pkm, IEnumerable <StringInstruction> filters, IEnumerable <StringInstruction> modifications) { if (!pkm.ChecksumValid || pkm.Species == 0) { return(ModifyResult.Invalid); } PKMInfo info = new PKMInfo(pkm); var pi = Props[Array.IndexOf(Types, pkm.GetType())]; foreach (var cmd in filters) { try { if (!IsFilterMatch(cmd, info, pi)) { return(ModifyResult.Filtered); } } catch (Exception ex) { Debug.WriteLine(MsgBEModifyFailCompare + " " + ex.Message, cmd.PropertyName, cmd.PropertyValue); return(ModifyResult.Error); } } ModifyResult result = ModifyResult.Modified; foreach (var cmd in modifications) { try { var tmp = SetPKMProperty(cmd, info, pi); if (result != ModifyResult.Modified) { result = tmp; } } catch (Exception ex) { Debug.WriteLine(MsgBEModifyFail + " " + ex.Message, cmd.PropertyName, cmd.PropertyValue); } } return(result); }
/// <summary> /// Sets the <see cref="PKM"/> data with a suggested value based on its <see cref="LegalityAnalysis"/>. /// </summary> /// <param name="name">Property to modify.</param> /// <param name="info">Cached info storing Legal data.</param> private static ModifyResult SetSuggestedPKMProperty(string name, PKMInfo info) { var PKM = info.pkm; switch (name) { case nameof(PKM.Stats): PKM.SetStats(PKM.GetStats(PKM.PersonalInfo)); return(ModifyResult.Modified); case nameof(IHyperTrain.HyperTrainFlags): PKM.SetSuggestedHyperTrainingData(); return(ModifyResult.Modified); case nameof(PKM.RelearnMoves): PKM.RelearnMoves = info.SuggestedRelearn; return(ModifyResult.Modified); case nameof(PKM.Met_Location): var encounter = info.SuggestedEncounter; if (encounter == null) { return(ModifyResult.Error); } int level = encounter.Level; int location = encounter.Location; int minlvl = Legal.GetLowestLevel(PKM, encounter.LevelMin); PKM.Met_Level = level; PKM.Met_Location = location; PKM.CurrentLevel = Math.Max(minlvl, level); return(ModifyResult.Modified); case nameof(PKM.Moves): return(SetMoves(PKM, PKM.GetMoveSet(la: info.Legality))); default: return(ModifyResult.Error); } }
/// <summary> /// Sets the <see cref="PKM"/> data with a suggested value based on its <see cref="LegalityAnalysis"/>. /// </summary> /// <param name="name">Property to modify.</param> /// <param name="info">Cached info storing Legal data.</param> /// <param name="propValue">Suggestion string which starts with <see cref="CONST_SUGGEST"/></param> private static ModifyResult SetSuggestedPKMProperty(string name, PKMInfo info, string propValue) { bool isAll() => propValue.EndsWith("All", true, CultureInfo.CurrentCulture); bool isNone() => propValue.EndsWith("None", true, CultureInfo.CurrentCulture); var pk = info.Entity; switch (name) { // pb7 only case nameof(PB7.Stat_CP) when pk is PB7 pb7: pb7.ResetCP(); return(ModifyResult.Modified); case nameof(PB7.HeightAbsolute) when pk is PB7 pb7: pb7.HeightAbsolute = pb7.CalcHeightAbsolute; return(ModifyResult.Modified); case nameof(PB7.WeightAbsolute) when pk is PB7 pb7: pb7.WeightAbsolute = pb7.CalcWeightAbsolute; return(ModifyResult.Modified); case nameof(PKM.Nature) when pk.Format >= 8: pk.Nature = pk.StatNature; return(ModifyResult.Modified); case nameof(PKM.StatNature) when pk.Format >= 8: pk.StatNature = pk.Nature; return(ModifyResult.Modified); case nameof(PKM.Stats): pk.ResetPartyStats(); return(ModifyResult.Modified); case nameof(IHyperTrain.HyperTrainFlags): pk.SetSuggestedHyperTrainingData(); return(ModifyResult.Modified); case nameof(PKM.RelearnMoves): if (pk.Format >= 8) { pk.ClearRecordFlags(); if (isAll()) { pk.SetRecordFlags(); // all } else if (!isNone()) { pk.SetRecordFlags(pk.Moves); // whatever fit the current moves } } pk.SetRelearnMoves(info.SuggestedRelearn); return(ModifyResult.Modified); case PROP_RIBBONS: if (isNone()) { RibbonApplicator.RemoveAllValidRibbons(pk); } else // All { RibbonApplicator.SetAllValidRibbons(pk); } return(ModifyResult.Modified); case nameof(PKM.Met_Location): var encounter = info.SuggestedEncounter; if (encounter == null) { return(ModifyResult.Error); } int level = encounter.Level; int location = encounter.Location; int minlvl = Legal.GetLowestLevel(pk, encounter.LevelMin); pk.Met_Level = level; pk.Met_Location = location; pk.CurrentLevel = Math.Max(minlvl, level); return(ModifyResult.Modified); case nameof(PKM.Moves): return(SetMoves(pk, pk.GetMoveSet(la: info.Legality))); case nameof(PKM.Ball): BallRandomizer.ApplyBallLegalByColor(pk); return(ModifyResult.Modified); default: return(ModifyResult.Error); } }
/// <summary> /// Sets the <see cref="PKM"/> data with a suggested value based on its <see cref="LegalityAnalysis"/>. /// </summary> /// <param name="name">Property to modify.</param> /// <param name="info">Cached info storing Legal data.</param> /// <param name="propValue">Suggestion string which starts with <see cref="CONST_SUGGEST"/></param> private static ModifyResult SetSuggestedPKMProperty(string name, PKMInfo info, string propValue) {