A weird game.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
turings-tarpit/builder.cpp

178 lines
4.4 KiB

#include "builder.hpp"
#include "dbc.hpp"
#include "watcher.hpp"
#include "game_engine.hpp"
#include <chrono> // for milliseconds
#include <efsw/efsw.hpp>
#include <fmt/chrono.h>
#include <fmt/color.h>
#include <fmt/core.h>
#include <fmt/core.h>
#include <fstream>
#include <git2.h>
#include <iostream>
#include <memory> // for allocator, shared_ptr
#include <regex>
#include <stdio.h>
#include <stdlib.h> // for EXIT_SUCCESS
#include <string> // for operator+, to_string
#include <unistd.h>
#include <nlohmann/json.hpp>
#include <fstream>
using std::string;
using namespace fmt;
using namespace nlohmann;
#define BUF_MAX 1024
Builder::Builder(GUI &g, GameEngine &engine)
: gui(g), game(engine)
{
std::ifstream infile(".tarpit.json");
json config = json::parse(infile);
config["git_path"].template get_to<string>(git_path);
config["build_cmd"].template get_to<string>(build_cmd);
}
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);
done_out = res == nullptr;
if(!done_out) {
return string{buffer}; // yeah, that's probably a problem
} else {
return "";
}
}
bool end_build(FILE *build_out, GUI &gui, string &build_cmd) {
int rc = pclose(build_out);
return rc == 0;
}
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) {
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 };
}
}
enum BuildState {
WAITING, BUILDING, DONE
};
void Builder::run() {
git_repository* repo = nullptr;
try {
gui.output(format("Using build command: {}", build_cmd));
efsw::FileWatcher* fileWatcher = new efsw::FileWatcher();
dbc::check(fileWatcher != nullptr, "Failed to create filewatcher.");
git_libgit2_init();
int err = git_repository_open(&repo, git_path.c_str());
dbc::check(err == 0, git_error_last()->message);
UpdateListener* listener = new UpdateListener(repo);
dbc::check(listener != nullptr, "Failed to create listener.");
gui.output(format("Watching directory {} for changes...", git_path));
efsw::WatchID wid = fileWatcher->addWatch(git_path, listener, true);
FILE *build_out = NULL;
bool build_done = false;
BuildState state = WAITING;
fileWatcher->watch();
int rc = gui.main_loop(game, [&] {
switch(state) {
case WAITING:
if(listener->changes) {
game.start_round();
gui.building();
gui.output(format("CHANGES! Running build {}", build_cmd));
build_out = start_command(build_cmd);
state = BUILDING;
}
break;
case BUILDING: {
// check if there's output
string line = read_line(build_out, build_done);
if(build_done) {
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 {
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()) {
gui.you_died();
game.reset();
}
listener->reset_state();
gui.output("^^^^^^^^^^^ END ^^^^^^^^^^^");
state = WAITING;
}
break;
}
return 0;
});
dbc::check(rc == 0, "Invalid return from main_loop.");
fileWatcher->removeWatch(wid);
git_libgit2_shutdown();
} catch(dbc::Error &err) {
if(repo != nullptr) git_repository_free(repo);
git_libgit2_shutdown();
throw err;
}
}