view dep/animia/src/strategist.cc @ 138:28842a8d0c6b

dep/animia: huge refactor (again...) but this time, it actually compiles! and it WORKS! (on win32... not sure about other platforms...) configuring players is still not supported: at some point I'll prune something up...
author Paper <mrpapersonic@gmail.com>
date Sun, 12 Nov 2023 04:53:19 -0500
parents
children 478f3b366199
line wrap: on
line source

#include "animia/strategies.h"
#include "animia/util.h"
#include "animia.h"
#include <iostream>

#ifdef ANIMIA_ON_WIN32
#	include "animia/fd/win32.h"
#elif defined(ANIMIA_ON_LINUX)
#	include "animia/fd/linux.h"
#elif defined(ANIMIA_ON_UNIX)
#	include "animia/fd/bsd.h"
#endif

namespace animia::internal {

class Strategist {
	public:
		Strategist(Result& result) : result_(result) {}

		bool ApplyStrategies();

	private:
		bool AddMedia(const MediaInfo media_information);

		bool ApplyOpenFilesStrategy();

		Result& result_;
};

bool Strategist::ApplyStrategies() {
	bool success = false;

	for (const auto strategy : result_.player.strategies) {
		switch (strategy) {
			case Strategy::OpenFiles:
				success |= ApplyOpenFilesStrategy();
				break;
		}
	}

	return success;
}

bool ApplyStrategies(std::vector<Result>& results) {
	bool success = false;

	for (auto& result : results) {
		Strategist strategist(result);
		success |= strategist.ApplyStrategies();
	}

	return success;
}

////////////////////////////////////////////////////////////////////////////////

bool Strategist::ApplyOpenFilesStrategy() {
	bool success = false;

	const std::set<pid_t> pids{result_.process.pid};
	std::vector<std::tuple<pid_t, std::string>> files;

#ifdef ANIMIA_ON_WIN32
	win32::EnumerateOpenFiles(pids, files);
#elif defined(ANIMIA_ON_LINUX)
	linux::EnumerateOpenFiles(pids, files);
#elif defined(ANIMIA_ON_UNIX)
	unix::EnumerateOpenFiles(pids, files);
#endif

	for (const auto& [pid, file] : files) {
		success |= AddMedia({MediaInfoType::File, file});
	}

	return success;
}

////////////////////////////////////////////////////////////////////////////////

bool Strategist::AddMedia(const MediaInfo media_information) {
	if (media_information.value.empty())
		return false;

	Media media;
	media.information.push_back(media_information);
	result_.media.push_back(std::move(media));

	return true;
}

}