<commit_before>#include "../World.h"
#include "../Engine.h"

#include "AgentInjector.h"
#include "c1cobfile.h"
#include "../cobFile.h"

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>

#include <QMessageBox>
#include <QPainter>

namespace fs = boost::filesystem;

AgentInjector::AgentInjector(QWidget *parent) : QDialog(parent) {
	ui.setupUi(this);
	
	connect(ui.injectButton, SIGNAL(clicked()), this, SLOT(onInject()));
	connect(ui.removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
	connect(ui.agentList, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), this, SLOT(onSelect(QListWidgetItem *, QListWidgetItem *)));
	connect(ui.agentList, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), ui.imagePreview, SLOT(onSelect(QListWidgetItem *, QListWidgetItem *)));
	
	setAttribute(Qt::WA_QuitOnClose, false);
	
	resize(400,500);

	readAgents();
}

AgentInjector::~AgentInjector() {
	resetAgents();
}

void AgentInjector::onSelect(QListWidgetItem *current, QListWidgetItem *prev) {
	if (engine.version == 2) {
		cobAgentBlock *b = (cobAgentBlock *)current->data(Qt::UserRole).value<void *>();
		assert(b);
		ui.Description->setText(b->description.c_str());
	} else {
		ui.Description->setText("");
	}
}

void AgentInjector::resetAgents() {
	ui.agentList->clear();

	for (std::vector<class cobFile *>::iterator i = cobfiles.begin(); i != cobfiles.end(); i++) {
		delete *i;
	}
}

void AgentInjector::readAgents() {
	resetAgents();

	for (unsigned int i = 0; i < world.data_directories.size(); i++) {
		fs::path p = world.data_directories[i];
		if (engine.version == 2) p = p / "/Objects/";
		if (!fs::exists(p) || !fs::is_directory(p)) {
			continue;
		}

		fs::directory_iterator end_itr; // default constructor is the end
		for (fs::directory_iterator itr(p); itr != end_itr; itr++) {
			std::string cobext = fs::extension(itr->path());
			std::transform(cobext.begin(), cobext.end(), cobext.begin(), (int(*)(int))tolower); // downcase
			if (cobext != ".cob") continue;
			
			std::string cob = itr->path().native_file_string();
			
			if (engine.version == 1) {
				std::ifstream cobstream(cob.c_str(), std::ios::binary);<commit_msg>qtgui agent injector: enable/disable inject/remove buttons as appropriate<commit_after>#include "../World.h"
#include "../Engine.h"

#include "AgentInjector.h"
#include "c1cobfile.h"
#include "../cobFile.h"

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/convenience.hpp>

#include <QMessageBox>
#include <QPainter>

namespace fs = boost::filesystem;

AgentInjector::AgentInjector(QWidget *parent) : QDialog(parent) {
	ui.setupUi(this);
	
	connect(ui.injectButton, SIGNAL(clicked()), this, SLOT(onInject()));
	connect(ui.removeButton, SIGNAL(clicked()), this, SLOT(onRemove()));
	connect(ui.agentList, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), this, SLOT(onSelect(QListWidgetItem *, QListWidgetItem *)));
	connect(ui.agentList, SIGNAL(currentItemChanged(QListWidgetItem *, QListWidgetItem *)), ui.imagePreview, SLOT(onSelect(QListWidgetItem *, QListWidgetItem *)));
	
	setAttribute(Qt::WA_QuitOnClose, false);
	
	resize(400,500);

	readAgents();
}

AgentInjector::~AgentInjector() {
	resetAgents();
}

void AgentInjector::onSelect(QListWidgetItem *current, QListWidgetItem *prev) {
	assert(current);

	ui.injectButton->setEnabled(true);

	if (engine.version == 2) {
		cobAgentBlock *b = (cobAgentBlock *)current->data(Qt::UserRole).value<void *>();
		assert(b);
		ui.Description->setText(b->description.c_str());
		ui.removeButton->setEnabled(b->removescript.size() != 0);
	} else {
		ui.removeButton->setEnabled(false); // TODO: support remover files!
		ui.Description->setText("");
	}
}

void AgentInjector::resetAgents() {
	ui.agentList->clear();
	ui.injectButton->setEnabled(false);
	ui.removeButton->setEnabled(false);

	for (std::vector<class cobFile *>::iterator i = cobfiles.begin(); i != cobfiles.end(); i++) {
		delete *i;
	}
}

void AgentInjector::readAgents() {
	resetAgents();

	for (unsigned int i = 0; i < world.data_directories.size(); i++) {
		fs::path p = world.data_directories[i];
		if (engine.version == 2) p = p / "/Objects/";
		if (!fs::exists(p) || !fs::is_directory(p)) {
			continue;
		}

		fs::directory_iterator end_itr; // default constructor is the end
		for (fs::directory_iterator itr(p); itr != end_itr; itr++) {
			std::string cobext = fs::extension(itr->path());
			std::transform(cobext.begin(), cobext.end(), cobext.begin(), (int(*)(int))tolower); // downcase
			if (cobext != ".cob") continue;
			
			std::string cob = itr->path().native_file_string();
			
			if (engine.version == 1) {
				std::ifstream cobstream(cob.c_str(), std::ios::binary);
{'fraction_non_alphanumeric': 0.18372093023255814, 'fraction_numerical': 0.004862579281183933, 'mean_word_length': 5.616783216783217, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6852035', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1657, 'n_words': 295}