/// <summary>
    /// Gets a blank file for the save file. If the template path exists, a template load will be attempted.
    /// </summary>
    /// <param name="sav">Save File to fetch a template for</param>
    /// <param name="templatePath">Path to look for a template in</param>
    /// <returns>Template if it exists, or a blank <see cref="PKM"/> from the <see cref="sav"/></returns>
    public static PKM LoadTemplate(this SaveFile sav, string?templatePath = null)
    {
        if (templatePath == null || !Directory.Exists(templatePath))
        {
            return(LoadTemplateInternal(sav));
        }

        var    di   = new DirectoryInfo(templatePath);
        string path = Path.Combine(templatePath, $"{di.Name}.{sav.PKMType.Name.ToLowerInvariant()}");

        if (!File.Exists(path))
        {
            return(LoadTemplateInternal(sav));
        }
        var fi = new FileInfo(path);

        if (!EntityDetection.IsSizePlausible(fi.Length))
        {
            return(LoadTemplateInternal(sav));
        }

        var data   = File.ReadAllBytes(path);
        var prefer = EntityFileExtension.GetContextFromExtension(fi.Extension, sav.Context);
        var pk     = EntityFormat.GetFromBytes(data, prefer);

        if (pk?.Species is not > 0)
        {
            return(LoadTemplateInternal(sav));
        }

        return(EntityConverter.ConvertToType(pk, sav.BlankPKM.GetType(), out _) ?? LoadTemplateInternal(sav));
    }
        private static PKM GetPKM(string file, FileInfo fi)
        {
            fi.Should().NotBeNull($"the test file '{file}' should be a valid file");
            EntityDetection.IsSizePlausible(fi.Length).Should().BeTrue($"the test file '{file}' should have a valid file length");

            var data   = File.ReadAllBytes(file);
            var format = EntityFileExtension.GetFormatFromExtension(file[^ 1], -1);
Exemple #3
0
        // ReSharper disable once UnusedParameter.Local
        private static void VerifyAll(string folder, string name, bool isValid, bool checkDir = true)
        {
            var  path   = Path.Combine(folder, name);
            bool exists = Directory.Exists(path);

            if (checkDir)
            {
                exists.Should().BeTrue($"the specified test directory at '{path}' should exist");
            }
            else if (!exists)
            {
                return;
            }

            var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories);
            var ctr   = 0;

            foreach (var file in files)
            {
                var fi = new FileInfo(file);
                fi.Should().NotBeNull($"the test file '{file}' should be a valid file");
                EntityDetection.IsSizePlausible(fi.Length).Should().BeTrue($"the test file '{file}' should have a valid file length");

                var data   = File.ReadAllBytes(file);
                var format = EntityFileExtension.GetFormatFromExtension(file[^ 1], -1);
Exemple #4
0
    /// <summary>
    /// Gets a <see cref="PKM"/> from the provided <see cref="file"/> path, which is to be loaded to the <see cref="SaveFile"/>.
    /// </summary>
    /// <param name="file"><see cref="PKM"/> or <see cref="MysteryGift"/> file path.</param>
    /// <param name="sav">Generation Info</param>
    /// <returns>New <see cref="PKM"/> reference from the file.</returns>
    public static PKM?GetSingleFromPath(string file, ITrainerInfo sav)
    {
        var fi = new FileInfo(file);

        if (!fi.Exists)
        {
            return(null);
        }
        if (fi.Length == GP1.SIZE && TryGetGP1(File.ReadAllBytes(file), out var gp1))
        {
            return(gp1.ConvertToPB7(sav));
        }
        if (!EntityDetection.IsSizePlausible(fi.Length) && !MysteryGift.IsMysteryGift(fi.Length))
        {
            return(null);
        }
        var data = File.ReadAllBytes(file);
        var ext  = fi.Extension;
        var mg   = MysteryGift.GetMysteryGift(data, ext);
        var gift = mg?.ConvertToPKM(sav);

        if (gift != null)
        {
            return(gift);
        }
        _ = TryGetPKM(data, out var pk, ext, sav);
        return(pk);
    }
    // ReSharper disable once UnusedParameter.Local
    private static void VerifyAll(string folder, string name, bool isValid, bool checkDir = true)
    {
        var  path   = Path.Combine(folder, name);
        bool exists = Directory.Exists(path);

        if (checkDir)
        {
            exists.Should().BeTrue($"the specified test directory at '{path}' should exist");
        }
        else if (!exists)
        {
            return;
        }

        var files = Directory.EnumerateFiles(path, "*", SearchOption.AllDirectories);
        var ctr   = 0;

        foreach (var file in files)
        {
            var fi = new FileInfo(file);
            fi.Should().NotBeNull($"the test file '{file}' should be a valid file");
            EntityDetection.IsSizePlausible(fi.Length).Should().BeTrue($"the test file '{file}' should have a valid file length");

            var data   = File.ReadAllBytes(file);
            var prefer = EntityFileExtension.GetContextFromExtension(file);
            prefer.IsValid().Should().BeTrue("filename is expected to have a valid extension");

            var dn = fi.DirectoryName ?? string.Empty;
            ParseSettings.AllowGBCartEra     = dn.Contains("GBCartEra");
            ParseSettings.AllowGen1Tradeback = dn.Contains("1 Tradeback");
            var pk = EntityFormat.GetFromBytes(data, prefer);
            pk.Should().NotBeNull($"the PKM '{new FileInfo(file).Name}' should have been loaded");
            if (pk == null)
            {
                continue;
            }
            var legality = new LegalityAnalysis(pk);
            if (legality.Valid == isValid)
            {
                ctr++;
                continue;
            }

            var fn = Path.Combine(dn, fi.Name);
            if (isValid)
            {
                var info   = legality.Info;
                var result = legality.Results.Cast <ICheckResult>().Concat(info.Moves).Concat(info.Relearn);
                // ReSharper disable once ConstantConditionalAccessQualifier
                var invalid = result.Where(z => !z.Valid);
                var msg     = string.Join(Environment.NewLine, invalid.Select(z => z.Comment));
                legality.Valid.Should().BeTrue($"because the file '{fn}' should be Valid, but found:{Environment.NewLine}{msg}");
            }
            else
            {
                legality.Valid.Should().BeFalse($"because the file '{fn}' should be invalid, but found Valid.");
            }
        }
        ctr.Should().BeGreaterThan(0);
    }
    /// <summary>
    /// Gets the generation of the Pokemon data.
    /// </summary>
    /// <param name="data">Raw data representing a Pokemon.</param>
    /// <returns>Enum indicating the generation of the PKM file, or <see cref="None"/> if the data is invalid.</returns>
    public static EntityFormatDetected GetFormat(ReadOnlySpan <byte> data)
    {
        if (!EntityDetection.IsSizePlausible(data.Length))
        {
            return(None);
        }

        return(GetFormatInternal(data));
    }
Exemple #7
0
        public ActionAttackEntity(Entity owner, Entity target, bool onTargetDeathTryAutoAttack) : base(owner)
        {
            _target = target;

            _entityMovement  = entity.GetCharacterComponent <EntityMovement>();
            _entityDetection = entity.GetCharacterComponent <EntityDetection>();
            _entityAttack    = entity.GetCharacterComponent <EntityAttack>();

            _onTargetDeathTryAutoAttack = onTargetDeathTryAutoAttack;
        }
        private static PKM GetPKM(string file, FileInfo fi)
        {
            fi.Should().NotBeNull($"the test file '{file}' should be a valid file");
            EntityDetection.IsSizePlausible(fi.Length).Should().BeTrue($"the test file '{file}' should have a valid file length");

            var data   = File.ReadAllBytes(file);
            var prefer = EntityFileExtension.GetContextFromExtension(file, EntityContext.None);
            var pkm    = EntityFormat.GetFromBytes(data, prefer: prefer);

            pkm.Should().NotBeNull($"the PKM '{new FileInfo(file).Name}' should have been loaded");
            return(pkm);
        }
Exemple #9
0
        public static IEnumerable <PKM> GetPKMsFromPaths(IEnumerable <string> files, int generation)
        {
            var result = files
                         .Where(file => EntityDetection.IsSizePlausible(new FileInfo(file).Length))
                         .Select(File.ReadAllBytes)
                         .Select(data => EntityFormat.GetFromBytes(data, prefer: generation));

            foreach (var pkm in result)
            {
                if (pkm?.Species is > 0)
                {
                    yield return(pkm);
                }
            }
        }
Exemple #10
0
        public static async Task RepostPKMAsShowdownAsync(this ISocketMessageChannel channel, IAttachment att)
        {
            if (!EntityDetection.IsSizePlausible(att.Size))
            {
                return;
            }
            var result = await NetUtil.DownloadPKMAsync(att).ConfigureAwait(false);

            if (!result.Success)
            {
                return;
            }

            var pkm = result.Data !;
            await channel.SendPKMAsShowdownSetAsync(pkm).ConfigureAwait(false);
        }
Exemple #11
0
    /// <summary>
    /// Tries to get an <see cref="IEnumerable{T}"/> object from the input parameters.
    /// </summary>
    /// <param name="data">Binary data</param>
    /// <param name="pkms">Output result</param>
    /// <param name="sav">Reference savefile used for PC Binary compatibility checks.</param>
    /// <returns>True if file object reference is valid, false if none found.</returns>
    public static bool TryGetPCBoxBin(byte[] data, out IEnumerable <byte[]> pkms, SaveFile?sav)
    {
        if (sav == null)
        {
            pkms = Array.Empty <byte[]>();
            return(false);
        }
        var length = data.Length;

        if (EntityDetection.IsSizePlausible(length / sav.SlotCount) || EntityDetection.IsSizePlausible(length / sav.BoxSlotCount))
        {
            pkms = ArrayUtil.EnumerateSplit(data, length);
            return(true);
        }
        pkms = Array.Empty <byte[]>();
        return(false);
    }
Exemple #12
0
 public static IEnumerable <PKM> GetPKMsFromPaths(IEnumerable <string> files, EntityContext generation)
 {
     foreach (var f in files)
     {
         var fi = new FileInfo(f);
         if (!fi.Exists)
         {
             continue;
         }
         if (!EntityDetection.IsSizePlausible(fi.Length))
         {
             continue;
         }
         var data   = File.ReadAllBytes(f);
         var prefer = EntityFileExtension.GetContextFromExtension(fi.Extension, generation);
         var pk     = EntityFormat.GetFromBytes(data, prefer);
         if (pk?.Species is > 0)
         {
             yield return(pk);
         }
     }
 }
    public static void AddFromLocalFile(string file, ConcurrentBag <SlotCache> db, ITrainerInfo dest, ICollection <string> validExtensions)
    {
        var fi = new FileInfo(file);

        if (!validExtensions.Contains(fi.Extension) || !EntityDetection.IsSizePlausible(fi.Length))
        {
            return;
        }

        var data = File.ReadAllBytes(file);

        _ = FileUtil.TryGetPKM(data, out var pk, fi.Extension, dest);
        if (pk?.Species is not > 0)
        {
            return;
        }

        var info  = new SlotInfoFile(file);
        var entry = new SlotCache(info, pk);

        db.Add(entry);
    }
Exemple #14
0
    void DrawNearestEntities()
    {
        EntityDetection ent = target as EntityDetection;

        if (!ent.Entity.Data.CanDetectEntities)
        {
            return;
        }

        EditorGUILayout.BeginHorizontal();
        {
            EditorGUILayout.PrefixLabel("Nearest Ally");
            EditorGUILayout.LabelField(ent.GetNearestAlly() ? ent.GetNearestAlly().name : "NONE");
        }
        EditorGUILayout.EndHorizontal();

        EditorGUILayout.BeginHorizontal();
        {
            EditorGUILayout.PrefixLabel("Nearest Opponent");
            EditorGUILayout.LabelField(ent.GetNearestOpponent() ? ent.GetNearestOpponent().name : "NONE");
        }
        EditorGUILayout.EndHorizontal();
    }