Exemplo n.º 1
0
        /// <inheritdoc />
        public async Task <RenderResult> RenderMap(string mapPath, MapRegion region, string outputDirectory, string postfix, CancellationToken cancellationToken)
        {
            if (mapPath == null)
            {
                throw new ArgumentNullException(nameof(mapPath));
            }
            if (outputDirectory == null)
            {
                throw new ArgumentNullException(nameof(outputDirectory));
            }
            if (postfix == null)
            {
                throw new ArgumentNullException(nameof(postfix));
            }

            var mapName = ioManager.GetFileNameWithoutExtension(mapPath);
            var outFile = ioManager.ConcatPath(outputDirectory, String.Format(CultureInfo.InvariantCulture, "{0}.{1}png", mapName, postfix != null ? String.Concat(postfix, '.') : null));
            var args    = String.Format(CultureInfo.InvariantCulture, GenerateRenderCommandLine(region), mapPath);

            await ioManager.CreateDirectory(ioManager.ConcatPath("data", "minimaps"), cancellationToken).ConfigureAwait(false);

            var        output      = new StringBuilder();
            var        errorOutput = new StringBuilder();
            Task <int> processTask;

            using (var P = CreateDMMToolsProcess(output, errorOutput))
            {
                P.StartInfo.Arguments = args;

                processTask = StartAndWaitForProcessExit(P, cancellationToken);

                await processTask.ConfigureAwait(false);
            }
            var toolOutput = String.Format(CultureInfo.InvariantCulture, "Exit Code: {0}{1}StdOut:{1}{2}{1}StdErr:{1}{3}", processTask.Result, Environment.NewLine, output, errorOutput);

            bool   expectNext = false;
            string result     = null;

            foreach (var I in output.ToString().Split(' '))
            {
                var text = I.Trim();
                if (text == "saving")
                {
                    expectNext = true;
                }
                else if (expectNext && text.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase))
                {
                    result = text;
                    break;
                }
                else
                {
                    expectNext = false;
                }
            }
            var rresult = new RenderResult
            {
                MapRegion   = region,
                CommandLine = args,
                ToolOutput  = toolOutput,
                InputPath   = mapPath
            };

            if (result != null)
            {
                await ioManager.MoveFile(result, outFile, cancellationToken).ConfigureAwait(false);

                rresult.OutputPath = ioManager.ResolvePath(outFile);
            }

            return(rresult);
        }
        /// <inheritdoc />
        public async Task CreateDump(global::System.Diagnostics.Process process, string outputFile, CancellationToken cancellationToken)
        {
            if (process == null)
            {
                throw new ArgumentNullException(nameof(process));
            }
            if (outputFile == null)
            {
                throw new ArgumentNullException(nameof(outputFile));
            }

            const string GCorePath = "/usr/bin/gcore";

            if (!await ioManager.FileExists(GCorePath, cancellationToken).ConfigureAwait(false))
            {
                throw new JobException(ErrorCode.MissingGCore);
            }

            int pid;

            try
            {
                if (process.HasExited)
                {
                    throw new JobException(ErrorCode.DreamDaemonOffline);
                }

                pid = process.Id;
            }
            catch (InvalidOperationException ex)
            {
                throw new JobException(ErrorCode.DreamDaemonOffline, ex);
            }

            string output;
            int    exitCode;

            using (var gcoreProc = lazyLoadedProcessExecutor.Value.LaunchProcess(
                       GCorePath,
                       Environment.CurrentDirectory,
                       $"-o {outputFile} {process.Id}",
                       true,
                       true,
                       true))
            {
                using (cancellationToken.Register(() => gcoreProc.Terminate()))
                    exitCode = await gcoreProc.Lifetime.ConfigureAwait(false);

                output = await gcoreProc.GetCombinedOutput(cancellationToken).ConfigureAwait(false);

                logger.LogDebug("gcore output:{0}{1}", Environment.NewLine, output);
            }

            if (exitCode != 0)
            {
                throw new JobException(
                          ErrorCode.GCoreFailure,
                          new JobException(
                              $"Exit Code: {exitCode}{Environment.NewLine}Output:{Environment.NewLine}{output}"));
            }

            // gcore outputs name.pid so remove the pid part
            var generatedGCoreFile = $"{outputFile}.{pid}";
            await ioManager.MoveFile(generatedGCoreFile, outputFile, cancellationToken).ConfigureAwait(false);
        }