From 50c0ee3424a03f9b47e8607a64e572c4a22e70fa Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 9 Sep 2024 01:10:47 -0400 Subject: [PATCH] Reworked the builder code to setup for more async running of the build. --- Makefile | 6 ++++-- builder.cpp | 54 +++++++++++++++++++++++++++++------------------------ builder.hpp | 11 ++++++++++- 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/Makefile b/Makefile index 49fb0fb..a85a0df 100644 --- a/Makefile +++ b/Makefile @@ -1,13 +1,15 @@ - - all: build test +patch: + cp .\patches\process.h .\subprojects\libgit2-1.8.1\src\util\process.h + build: meson compile -j 4 -C builddir test: meson test -C builddir --suite turings_tarpit +# make an install for real maybe copy dll and .exe to dir and zip? install: build test powershell "cp builddir/escape_turings_tarpit.exe ." diff --git a/builder.cpp b/builder.cpp index 378595a..a8b307b 100644 --- a/builder.cpp +++ b/builder.cpp @@ -39,14 +39,12 @@ Builder::Builder(GUI &g, GameEngine &engine) FILE *start_command(string &build_cmd) { FILE *build_out = popen(build_cmd.c_str(), "r"); dbc::check(build_out != nullptr, "Failed to run command."); - return build_out; } string read_line(FILE *build_out, bool &done_out) { char buffer[BUF_MAX]; char *res = fgets(buffer, BUF_MAX, build_out); - //!!! exception if res == nullptr done_out = res == nullptr; if(!done_out) { @@ -56,33 +54,28 @@ string read_line(FILE *build_out, bool &done_out) { } } -void end_build(FILE *build_out, GUI &gui, string &build_cmd) { +bool end_build(FILE *build_out, GUI &gui, string &build_cmd) { int rc = pclose(build_out); - - if(rc == 0) { - gui.build_works(); - } else { - // BUG: make it not play two sounds - gui.build_failed(true, build_cmd); - } + return rc == 0; } -void Builder::run_build(const string &line) { +MatchResult Builder::parse_line(const string &line) { std::regex err_re("(.*?):([0-9]+):([0-9]+):\\s*(.*?):\\s*(.*)\n*"); std::smatch err; bool match = std::regex_match(line, err, err_re); if(match) { - string file_name = err[1].str(); - string lnumber = err[2].str(); - string col = err[3].str(); - string type = err[4].str(); - string message = err[5].str(); - - gui.output(format("HIT WITH {} @ {}:{}:{} {}", type, file_name, lnumber, col, message)); - - game.hit(type); + return { + .match = true, + .file_name = err[1].str(), + .lnumber = err[2].str(), + .col = err[3].str(), + .type = err[4].str(), + .message = err[5].str(), + }; + } else { + return { .match = false }; } } @@ -130,20 +123,33 @@ void Builder::run() { case BUILDING: { // check if there's output string line = read_line(build_out, build_done); + if(build_done) { - end_build(build_out, gui, build_cmd); + bool good = end_build(build_out, gui, build_cmd); + + if(good) { + gui.build_works(); + } else { + // BUG: make it not play two sounds + gui.build_failed(true, build_cmd); + } + build_out = NULL; state = DONE; } else { - // get the command line - // run the build if it's available - run_build(line); + auto m = parse_line(line); + + if(m.match) { + gui.output(format("HIT WITH {} @ {}:{}:{} {}", m.type, m.file_name, m.lnumber, m.col, m.message)); + game.hit(m.type); + } state = BUILDING; } } break; case DONE: { + game.end_round(); if(game.is_dead()) { diff --git a/builder.hpp b/builder.hpp index 51ef193..57a140c 100644 --- a/builder.hpp +++ b/builder.hpp @@ -5,6 +5,15 @@ using std::string; +struct MatchResult { + bool match = false; + string file_name = ""; + string lnumber = ""; + string col = ""; + string type = ""; + string message = ""; +}; + class Builder { GUI gui; GameEngine game; @@ -15,7 +24,7 @@ class Builder { Builder(GUI &g, GameEngine &engine); - void run_build(const string &line); + MatchResult parse_line(const string &line); void run(); };