248 lines
6.4 KiB
C++
248 lines
6.4 KiB
C++
#include "../include/installationmanager.hpp"
|
|
|
|
#include <unistd.h>
|
|
#include <sstream>
|
|
#include <deque>
|
|
|
|
#include "../include/exceptionhandler.hpp"
|
|
#include "../include/query.hpp"
|
|
#include "../include/rapidjson/istreamwrapper.h"
|
|
#include "../include/rapidjson/error/en.h"
|
|
#include "../include/rapidjson/filereadstream.h"
|
|
|
|
InstallationManager::InstallationManager(const std::string &hd, const std::string &mod)
|
|
: _hd(hd), _mod(std::move(mod)) {}
|
|
|
|
void InstallationManager::execute()
|
|
{
|
|
_version_query();
|
|
/* --PACKPREINSTALLATION*/
|
|
|
|
/* TEMPORARY JSON */
|
|
const std::string fp = _hd + "/" + _mod + "/" + _ver + "/modconfig.json";
|
|
_grab_json(std::move(fp), _cfg);
|
|
/* TEMPORARY JSON */
|
|
|
|
_campaign_query();
|
|
|
|
std::cout << "\n" << "You have selected:" << "\n";
|
|
_print_selection(2);
|
|
|
|
/* PACKS SELECTION */
|
|
_pack_query();
|
|
|
|
/* SELECTION SUMMARY */
|
|
_print_title("SELECTION SUMMARY");
|
|
|
|
_print_selection(3);
|
|
|
|
std::cout << "\n";
|
|
const Query<bool> query_final = {"Are you sure you want to install this selection?"};
|
|
if (!query_final.execute(_cfg))
|
|
{
|
|
exit(0);
|
|
}
|
|
|
|
// INSTALLATION
|
|
|
|
std::cout << "Installing " << _modname << " version " << _ver << "...\n";
|
|
}
|
|
|
|
void InstallationManager::_version_query()
|
|
{
|
|
/* TEMPORARY JSON */
|
|
const std::string fp = _hd + "/" + _mod + "/modinfo.json";
|
|
rapidjson::Document modlist;
|
|
_grab_json(std::move(fp), modlist);
|
|
/* TEMPORARY JSON */
|
|
|
|
/* Printing mod info */
|
|
std::cout << "Name: " << modlist["name"].GetString() << "\n"
|
|
<< "Author: " << modlist["author"].GetString() << "\n"
|
|
<< "Latest version: " << modlist["versions"][0].GetString() << "\n"
|
|
<< "Available versions: ";
|
|
|
|
for (rapidjson::SizeType i = 0; i < modlist["versions"].Size(); i++)
|
|
{
|
|
if (i != 0)
|
|
{
|
|
std::cout << ", ";
|
|
}
|
|
|
|
std::cout << modlist["versions"][i].GetString();
|
|
}
|
|
|
|
std::cout << "\n";
|
|
|
|
/* PRE-INSTALLATION PROCESS */
|
|
/* --VERSION */
|
|
|
|
const Query<bool> query_verify = {"Are you sure you want to install " + std::string(modlist["name"].GetString()) + " version " + modlist["versions"][0].GetString() + "?"};
|
|
|
|
/* Asks user for mod version */
|
|
if (query_verify.execute(modlist))
|
|
{
|
|
_modname = modlist["name"].GetString();
|
|
_ver = modlist["versions"][0].GetString();
|
|
}
|
|
else
|
|
{
|
|
if (modlist["versions"].Size() > 1)
|
|
{
|
|
// std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
|
const Query<std::string> query_version = {"Please specify the version you would like to install", "versions"};
|
|
_ver = query_version.execute(modlist);
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Exiting installation!" << "\n";
|
|
exit(0);
|
|
}
|
|
}
|
|
|
|
std::cout << "Preparing to install " << _modname << " version " << _ver << "..." << "\n";
|
|
}
|
|
|
|
void InstallationManager::_campaign_query()
|
|
{
|
|
_print_title("CAMPAIGN SELECTION");
|
|
|
|
for (rapidjson::SizeType i = 0; i < _cfg["campaigns"].Size(); i++)
|
|
{
|
|
std::cout << "[" << (i+1) << "] " << _cfg["campaigns"][i]["name"].GetString() << "\n"
|
|
<< "Description: " << _cfg["campaigns"][i]["desc"].GetString() << "\n" << "\n";
|
|
}
|
|
|
|
/* CAMPAIGN SELECTION */
|
|
const Query<int> query_campaign = {"Please select your campaign", "campaigns"};
|
|
_campaigns = query_campaign.execute(_cfg) - 1;
|
|
/* TURN SELECTION */
|
|
const Query<int> query_turns = {"Please select your preferred turns per year", "turns"};
|
|
_turns = query_turns.execute(_cfg) - 1;
|
|
}
|
|
|
|
void InstallationManager::_pack_query()
|
|
{
|
|
_print_title("PACK SELECTION");
|
|
const Query<bool> query_pack = {"Do you want to install this pack?"};
|
|
for (rapidjson::SizeType i = 0; i < _cfg["packs"].Size(); i++)
|
|
{
|
|
std::cout << "\n" << _cfg["packs"][i]["name"].GetString() << "\n"
|
|
<< _cfg["packs"][i]["desc"].GetString() << "\n";
|
|
|
|
_packs.push_back(query_pack.execute(_cfg));
|
|
|
|
std::cin.clear();
|
|
}
|
|
|
|
|
|
}
|
|
|
|
void InstallationManager::_grab_json(const std::string &path, rapidjson::Document &d)
|
|
{
|
|
if ((access(path.c_str(), F_OK) != 0))
|
|
{
|
|
throw ExceptionHandler(INVALID_ARGUMENT_ERROR);
|
|
}
|
|
|
|
/*
|
|
std::ifstream ifs(path.c_str());
|
|
rapidjson::IStreamWrapper isw(ifs);
|
|
d.ParseStream(isw);
|
|
|
|
if (d.ParseStream<0>(isw).HasParseError())
|
|
{
|
|
std::cerr << "Failed to parse via string stream!" << "\n";
|
|
std::cerr << "RapidJson ERROR: ";
|
|
std::cerr << GetParseError_En(d.GetParseError()) << "\n";
|
|
std::cerr << "Filepath: " << path << "\n";
|
|
throw ExceptionHandler(CUSTOM_ERROR, "Failed to parse JSON file!");
|
|
}
|
|
else
|
|
{
|
|
d.ParseStream(isw);
|
|
return 0;
|
|
}
|
|
if (d.Parse<0>(path.c_str()).HasParseError())
|
|
{
|
|
std::cerr << "Failed to parse via string!" << "\n";
|
|
std::cerr << "RapidJson ERROR: ";
|
|
std::cerr << GetParseError_En(d.GetParseError()) << "\n";
|
|
std::cerr << "Filepath: " << path << "\n";
|
|
throw ExceptionHandler(CUSTOM_ERROR, "Failed to parse JSON file!");
|
|
}
|
|
{
|
|
d.Parse(path.c_str());
|
|
return 0;
|
|
}
|
|
*/
|
|
|
|
char readBuffer[65536];
|
|
FILE *fp = fopen(path.c_str(), "r");
|
|
rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
|
|
|
|
if (d.ParseStream<0>(is).HasParseError())
|
|
{
|
|
std::cerr << "Failed to parse via file stream!" << "\n";
|
|
std::cerr << "RapidJson ERROR: ";
|
|
std::cerr << GetParseError_En(d.GetParseError()) << "\n";
|
|
std::cerr << "Filepath: " << path << "\n";
|
|
throw ExceptionHandler(CUSTOM_ERROR, "Failed to parse JSON file!");
|
|
}
|
|
else
|
|
{
|
|
d.ParseStream(is);
|
|
}
|
|
}
|
|
|
|
void InstallationManager::_print_title(const std::string &str)
|
|
{
|
|
std::cout << str << "\n";
|
|
for (auto i : str)
|
|
{
|
|
std::cout << "=";
|
|
}
|
|
std::cout << "\n";
|
|
}
|
|
|
|
void InstallationManager::_print_selection(const int i)
|
|
{
|
|
std::deque<std::string> print;
|
|
switch (i)
|
|
{
|
|
case 3:
|
|
{
|
|
std::stringstream ss;
|
|
std::vector<std::string> packs;
|
|
for (rapidjson::SizeType i = 0; i < _cfg["packs"].Size(); i++)
|
|
{
|
|
if (_packs[i])
|
|
{
|
|
packs.push_back(_cfg["packs"][i]["name"].GetString());
|
|
}
|
|
}
|
|
ss << "Packs: ";
|
|
for (auto it = packs.begin(); it != packs.end(); ++it)
|
|
{
|
|
ss << *it;
|
|
if (it != (packs.end() - 1))
|
|
{
|
|
ss << ", ";
|
|
}
|
|
}
|
|
ss << "\n";
|
|
print.push_front(ss.str());
|
|
}
|
|
case 2:
|
|
{
|
|
print.push_front("Turns: " + std::string(_cfg["turns"][_turns]["name"].GetString()) + "\n");
|
|
}
|
|
case 1:
|
|
{
|
|
print.push_front("Campaign: " + std::string(_cfg["campaigns"][_campaigns]["name"].GetString()) + "\n");
|
|
}
|
|
}
|
|
|
|
for (auto &i : print) std::cout << i;
|
|
}
|