public Task <Batch> CreateBatch(IEnumerable <BatchArgument> arguments, BatchOptions options)
        {
            var batch = new Batch();

            // Create the working directory
            batch.Steps.Add(new BatchCallback(BatchStepType.CreateWorkingDirectory, async(b, d) =>
            {
                var workingDir = options.WorkingDirectory ?? Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                if (!Directory.Exists(workingDir))
                {
                    Directory.CreateDirectory(workingDir);
                }
                b.Variables["WorkingDirectory"] = workingDir;

                await Oy.Publish("Compile:Debug", $"Working directory is: {workingDir}\r\n");
            }));

            // Save the file to the working directory
            batch.Steps.Add(new BatchCallback(BatchStepType.ExportDocument, async(b, d) =>
            {
                var fn  = options.MapFileName ?? d.FileName;
                var ext = options.MapFileExtension ?? ".map";

                if (String.IsNullOrWhiteSpace(fn) || fn.IndexOf('.') < 0)
                {
                    fn = Path.GetRandomFileName();
                }
                var mapFile = Path.GetFileNameWithoutExtension(fn) + ext;
                b.Variables["MapFileName"] = mapFile;

                var path = Path.Combine(b.Variables["WorkingDirectory"], mapFile);
                b.Variables["MapFile"] = path;

                if (options.ExportDocument != null)
                {
                    await options.ExportDocument(d, path);
                }
                else
                {
                    var useCordon = options.UseCordonBounds.GetValueOrDefault(true);
                    Box bounds    = null;
                    if (useCordon && options.CordonBounds != null)
                    {
                        bounds = options.CordonBounds;
                    }
                    else if (useCordon)
                    {
                        var cb = d.Map.Data.GetOne <CordonBounds>();
                        if (cb != null && cb.Enabled && !cb.Box.IsEmpty())
                        {
                            bounds = cb.Box;
                        }
                    }
                    await ExportDocumentForBatch(d, path, bounds);
                }

                await Oy.Publish("Compile:Debug", $"Map file is: {path}\r\n");
            }));

            return(Task.FromResult <Batch>(null));
        }
Example #2
0
        public Task <Batch> CreateBatch(IEnumerable <BatchArgument> arguments, BatchOptions options)
        {
            var args = arguments.GroupBy(x => x.Name).ToDictionary(x => x.Key, x => x.First().Arguments);

            var batch = new Batch();

            // Create the working directory
            batch.Steps.Add(new BatchCallback(BatchStepType.CreateWorkingDirectory, async(b, d) =>
            {
                var workingDir = options.WorkingDirectory ?? Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
                if (!Directory.Exists(workingDir))
                {
                    Directory.CreateDirectory(workingDir);
                }
                b.Variables["WorkingDirectory"] = workingDir;

                await Oy.Publish("Compile:Debug", $"Working directory is: {workingDir}\r\n");
            }));

            // Save the file to the working directory
            batch.Steps.Add(new BatchCallback(BatchStepType.ExportDocument, async(b, d) =>
            {
                var fn  = options.MapFileName ?? d.FileName;
                var ext = options.MapFileExtension ?? ".map";
                if (String.IsNullOrWhiteSpace(fn) || fn.IndexOf('.') < 0)
                {
                    fn = Path.GetRandomFileName();
                }
                var mapFile = Path.GetFileNameWithoutExtension(fn) + ext;
                b.Variables["MapFileName"] = mapFile;

                var path = Path.Combine(b.Variables["WorkingDirectory"], mapFile);
                b.Variables["MapFile"] = path;

                if (options.ExportDocument != null)
                {
                    await options.ExportDocument(d, path);
                }
                else
                {
                    var useCordon = options.UseCordonBounds.GetValueOrDefault(true);
                    Box bounds    = null;
                    if (useCordon && options.CordonBounds != null)
                    {
                        bounds = options.CordonBounds;
                    }
                    else if (useCordon)
                    {
                        var cb = d.Map.Data.GetOne <CordonBounds>();
                        if (cb != null && cb.Enabled && !cb.Box.IsEmpty())
                        {
                            bounds = cb.Box;
                        }
                    }
                    await ExportDocumentForBatch(d, path, bounds);
                }

                await Oy.Publish("Compile:Debug", $"Map file is: {path}\r\n");
            }));

            // Run the compile tools
            if (args.ContainsKey("CSG"))
            {
                batch.Steps.Add(new BatchProcess(BatchStepType.RunBuildExecutable, Path.Combine(ToolsDirectory, CsgExe), args["CSG"] + " \"{MapFile}\""));
            }
            if (args.ContainsKey("BSP"))
            {
                batch.Steps.Add(new BatchProcess(BatchStepType.RunBuildExecutable, Path.Combine(ToolsDirectory, BspExe), args["BSP"] + " \"{MapFile}\""));
            }
            if (args.ContainsKey("VIS"))
            {
                batch.Steps.Add(new BatchProcess(BatchStepType.RunBuildExecutable, Path.Combine(ToolsDirectory, VisExe), args["VIS"] + " \"{MapFile}\""));
            }
            if (args.ContainsKey("RAD"))
            {
                batch.Steps.Add(new BatchProcess(BatchStepType.RunBuildExecutable, Path.Combine(ToolsDirectory, RadExe), args["RAD"] + " \"{MapFile}\""));
            }

            // Check for errors
            batch.Steps.Add(new BatchCallback(BatchStepType.CheckIfSuccessful, async(b, d) =>
            {
                var errFile = Path.ChangeExtension(b.Variables["MapFile"], "err");
                if (errFile != null && File.Exists(errFile))
                {
                    var errors   = File.ReadAllText(errFile);
                    b.Successful = false;
                    await Oy.Publish("Compile:Error", errors);
                }

                var bspFile = Path.ChangeExtension(b.Variables["MapFile"], "bsp");
                if (bspFile != null && !File.Exists(bspFile))
                {
                    b.Successful = false;
                }
            }));

            // Copy resulting files around
            batch.Steps.Add(new BatchCallback(BatchStepType.ProcessBuildResults, (b, d) =>
            {
                var mapDir     = Path.GetDirectoryName(d.FileName);
                var gameMapDir = Path.Combine(BaseDirectory, ModDirectory, "maps");

                // Copy configured files to the map directory
                CopyFile(MapCopyBsp, "bsp", mapDir);
                CopyFile(MapCopyMap, "map", mapDir);
                CopyFile(MapCopyRes, "res", mapDir);
                CopyFile(MapCopyErr, "err", mapDir);
                CopyFile(MapCopyLog, "log", mapDir);

                // Always copy pointfiles if they exist
                CopyFile(true, "lin", mapDir);
                CopyFile(true, "pts", mapDir);

                // Copy the BSP/RES to the game dir if configured
                CopyFile(b.Successful && GameCopyBsp, "bsp", gameMapDir);
                CopyFile(b.Successful && GameCopyBsp, "res", gameMapDir);

                void CopyFile(bool test, string extension, string directory)
                {
                    if (!test || directory == null || !Directory.Exists(directory))
                    {
                        return;
                    }

                    var file = Path.ChangeExtension(b.Variables["MapFile"], extension);
                    if (file == null || !File.Exists(file))
                    {
                        return;
                    }

                    File.Copy(file, Path.Combine(directory, Path.GetFileName(file)), true);
                }

                return(Task.CompletedTask);
            }));

            // Delete temp directory
            batch.Steps.Add(new BatchCallback(BatchStepType.DeleteWorkingDirectory, (b, d) =>
            {
                var workingDir = b.Variables["WorkingDirectory"];
                if (Directory.Exists(workingDir))
                {
                    Directory.Delete(workingDir, true);
                }
                return(Task.CompletedTask);
            }));

            if (options.RunGame ?? GameRun)
            {
                batch.Steps.Add(new BatchCallback(BatchStepType.RunGame, (b, d) =>
                {
                    if (!b.Successful)
                    {
                        return(Task.CompletedTask);
                    }

                    var silent = options.AllowUserInterruption.GetValueOrDefault(false);

                    if (options.AskRunGame ?? GameAsk)
                    {
                        // We can't ask to run the game if interruption isn't allowed
                        if (silent)
                        {
                            return(Task.CompletedTask);
                        }

                        var ask = MessageBox.Show(
                            $"The compile of {d.Name} completed successfully.\nWould you like to run the game now?",
                            "Compile Successful!",
                            MessageBoxButtons.YesNo,
                            MessageBoxIcon.Question
                            );
                        if (ask != DialogResult.Yes)
                        {
                            return(Task.CompletedTask);
                        }
                    }

                    var exe = Path.Combine(BaseDirectory, GameExe);
                    if (!File.Exists(exe))
                    {
                        if (!silent)
                        {
                            MessageBox.Show(
                                "The location of the game executable is incorrect. Please ensure that the game configuration has been set up correctly.",
                                "Failed to launch!",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error
                                );
                        }

                        return(Task.CompletedTask);
                    }

                    var gameArg = ModDirectory == "valve" ? "" : $"-game {ModDirectory} ";
                    var mapName = Path.GetFileNameWithoutExtension(b.Variables["MapFileName"]);

                    var flags = $"{gameArg}-dev -console +map \"{mapName}\"";
                    try
                    {
                        Process.Start(exe, flags);
                    }
                    catch (Exception ex)
                    {
                        if (!silent)
                        {
                            MessageBox.Show(
                                "Launching game failed: " + ex.Message,
                                "Failed to launch!",
                                MessageBoxButtons.OK, MessageBoxIcon.Error
                                );
                        }
                    }

                    return(Task.CompletedTask);
                }));
            }

            if (options.BatchSteps != null)
            {
                batch.Steps.RemoveAll(x => !options.BatchSteps.Contains(x.StepType));
            }

            return(Task.FromResult(batch));
        }