public async Task <Packets> LoadSniff(string path, CancellationToken token, IProgress <float> progress)
        {
            Packets packets   = null !;
            var     extension = Path.GetExtension(path);

            if (extension != ".pkt" && extension != ".dat")
            {
                extension = await messageBoxService.ShowDialog(new MessageBoxFactory <string>()
                                                               .SetTitle("Unknown file type")
                                                               .SetMainInstruction("File doesn't have .pkt nor .dat extension")
                                                               .SetContent("Therefore, cannot determine whether to open the file as parsed sniff or unparsed sniff")
                                                               .WithButton("Open as parsed sniff", ".dat")
                                                               .WithButton("Open as raw sniff", ".pkt")
                                                               .Build());
            }

            if (extension == ".pkt")
            {
                progress.Report(-1);
                var parsedPath     = Path.ChangeExtension(path, null) + "_parsed.dat";
                var parsedTextPath = Path.ChangeExtension(path, null) + "_parsed.txt";
                if (IsFileInUse(parsedTextPath) || IsFileInUse(parsedPath))
                {
                    throw new ParserException("Sniff output file already in use, probably in the middle of parsing, thus can't parse sniff. Are you parsing it in another window already?");
                }

                bool runParser = !File.Exists(parsedPath) || !await VerifyParsedVersion(parsedPath);

                if (runParser)
                {
                    await packetParserService.RunParser(path, viewerSettings.Settings.Parser, DumpFormatType.UniversalProtoWithText, token, progress);

                    if (token.IsCancellationRequested)
                    {
                        File.Delete(parsedPath);
                    }
                }
                path = parsedPath;
            }

            if (token.IsCancellationRequested)
            {
                return(packets);
            }

            if (!File.Exists(path))
            {
                throw new ParserException(
                          "For some reason parser didn't generate the output file. Aborting. If it repeats, report bug on github.");
            }

            progress.Report(-1);
            await Task.Run(async() =>
            {
                await using var input = File.OpenRead(path);
                packets = Packets.Parser.ParseFrom(input);
            }, token).ConfigureAwait(true);

            progress.Report(1);

            return(packets);
        }
Пример #2
0
        public async Task <Packets> LoadSniff(string path, int?customVersion, CancellationToken token, IProgress <float> progress)
        {
            Packets packets   = null !;
            var     extension = Path.GetExtension(path);

            if (extension != ".pkt" && extension != ".bin" && extension != ".dat")
            {
                extension = await messageBoxService.ShowDialog(new MessageBoxFactory <string>()
                                                               .SetTitle("Unknown file type")
                                                               .SetMainInstruction("File doesn't have .pkt, .bin nor .dat extension")
                                                               .SetContent("Therefore, cannot determine whether to open the file as parsed sniff or unparsed sniff")
                                                               .WithButton("Open as parsed sniff", ".dat")
                                                               .WithButton("Open as raw sniff", ".pkt")
                                                               .Build());
            }

            if (extension == ".pkt" || extension == ".bin")
            {
                progress.Report(-1);
                var parsedPath     = Path.ChangeExtension(path, null) + "_parsed.dat";
                var parsedTextPath = Path.ChangeExtension(path, null) + "_parsed.txt";
                if (IsFileInUse(parsedTextPath) || IsFileInUse(parsedPath))
                {
                    throw new ParserException("Sniff output file already in use, probably in the middle of parsing, thus can't parse sniff. Are you parsing it in another window already?");
                }

                bool runParser = !File.Exists(parsedPath) || !await VerifyParsedVersion(parsedPath);

                if (runParser)
                {
                    await packetParserService.RunParser(path, viewerSettings.Settings.Parser, DumpFormatType.UniversalProtoWithText, customVersion, token, progress);

                    if (token.IsCancellationRequested)
                    {
                        File.Delete(parsedPath);
                    }
                }
                path = parsedPath;
            }
            else if (extension == ".dat")
            {
                if (!await VerifyParsedVersion(path))
                {
                    throw new ParserException(
                              "You have opened _parsed.dat file generated by an older version of the editor.\nIt is not compatible. Please reparse the sniff by reopening the .pkt file");
                }
            }

            if (token.IsCancellationRequested)
            {
                return(packets);
            }

            if (!File.Exists(path))
            {
                throw new ParserException(
                          "For some reason parser didn't generate the output file. Aborting. If it repeats, report bug on github.");
            }

            progress.Report(-1);
            await Task.Run(async() =>
            {
                await using var input = File.OpenRead(path);
                try
                {
                    packets = Packets.Parser.ParseFrom(input);
                }
                catch (Google.Protobuf.InvalidProtocolBufferException)
                {
                    throw new ParserException("Sniff file is broken. Possible reason: you are trying to open _parsed.dat file generated with an older editor version. Select .pkt file and reparse again.");
                }
            }, token).ConfigureAwait(true);

            progress.Report(1);

            return(packets);
        }