cellar/src/bottles/bottles.cpp

173 lines
5.8 KiB
C++
Raw Normal View History

2017-03-13 19:03:07 -07:00
#include <cstdlib>
#include <fstream>
#include <map>
2017-03-13 19:03:07 -07:00
#include <string>
#include <sstream>
#include <vector>
#include <boost/filesystem.hpp>
2018-03-13 20:06:15 -07:00
#include "nlohmann/json.hpp"
2017-03-13 19:03:07 -07:00
#include "bottles.hpp"
#include "cmake.hpp"
#include "internal/bottles.hpp"
2017-03-13 19:03:07 -07:00
#include "fs.hpp"
#include "output.hpp"
#ifdef ENABLE_STEAM
#include "steam.hpp"
#endif
2017-03-13 19:03:07 -07:00
using namespace std;
using namespace cellar;
using namespace cellar::bottles;
2017-03-13 19:03:07 -07:00
2017-03-23 12:06:29 -07:00
using CommandFunction = cellar::commands::CommandFunction;
2017-03-13 19:03:07 -07:00
using json = nlohmann::json;
2025-02-03 17:28:18 -08:00
/**
* @brief Construct an empty bottle object.
* This empty bottle object can then be used to help create new ones.
*/
Bottle::Bottle() {
// define a null bottle
// strings handle themselves
config = json({});
type = bottle_anonymous;
}
2025-02-03 17:28:18 -08:00
/**
* @brief Construct a bottle object from at a given path.
*
* @param patharg The path to load a bottle from.
*/
Bottle::Bottle(string patharg) {
output::statement("loading bottle from " + patharg, true);
config = json({});
path = patharg;
boost::filesystem::file_status path_status = boost::filesystem::symlink_status(path);
bool symlink = boost::filesystem::is_symlink(path_status);
if (symlink) {
boost::filesystem::path realpath = boost::filesystem::canonical(path);
canonical_path = realpath.string();
type = bottle_symlink;
} else {
canonical_path = path;
try {
if (load_config()) {
type = bottle_labelled;
} else {
type = bottle_anonymous;
}
}
catch (const exception &exc) {
type = bottle_error;
}
}
}
2025-02-03 17:28:18 -08:00
/**
* @brief Lists all bottles cellar manages or is otherwise aware of.
* This includes bottles managed by other tools like Steam or Lutris,
* assuming support for those tools is compiled into cellar. (Steam
* support is present; Lutris support is planned for the future.)
*
* @return map<string, Bottle> All bottles. Bottles managed by other tools have prefixed keys, e.g. Steam bottles use "steam:"
*/
map<string, Bottle> cellar::bottles::get_bottles() {
map<string, Bottle> result;
2017-03-13 19:03:07 -07:00
string homepath = getenv("HOME");
2018-03-13 20:06:15 -07:00
vector<string> homedir = fs::listdir(homepath + "/.local/share/cellar/bottles");
2017-03-13 19:03:07 -07:00
for (string item : homedir) {
2018-03-13 20:06:15 -07:00
string fullitem = homepath + "/.local/share/cellar/bottles/" + item;
Bottle output(fullitem);
2017-03-13 19:03:07 -07:00
2018-03-13 20:06:15 -07:00
result[item] = output;
2017-03-13 19:03:07 -07:00
}
#ifdef ENABLE_STEAM
map<string, Bottle> bottles_proton = cellar::steam::get_app_bottles();
for (auto item : bottles_proton) {
result.insert_or_assign(item.first, item.second);
}
#endif
2017-03-13 19:03:07 -07:00
return result;
}
2017-03-23 00:02:38 -07:00
2025-02-03 17:28:18 -08:00
/**
* @brief Takes an input that refers to a bottle and returns a path to that bottle.
* This input can be any of the following:
* * A bottle name as managed by cellar
* * A prefixed bottle name, in the format `<tool>:<identifier>`. (e.g. `steam:78000`)
* * An absolute or relative path, in which case it is returned with no modification.
* * A path relative to (and starting with ~). cellar will throw a warning in verbose mode,
* since usually the shell expands this for us, but will do a naive replacement on its own
* and try to prevent confused users.
*
* @param bottlechoice Input, usually referring to a specific bottle known to cellar.
* @return string Path to referenced bottle.
*/
string cellar::bottles::resolve_bottle(string bottlechoice) {
string result;
if (bottlechoice.substr(0,1) == "/" || bottlechoice.substr(0,1) == ".") { // absolute or relative path
result = bottlechoice;
} else if (bottlechoice.substr(0,1) == "~") { // "absolute" path in home directory, not expanded by the shell for some reason (i've seen some shit)
// this is a naive replacement and will fail if the user tries something like ~nicole/.wine
// i'm figuring at that point if you're doing that, you'll also recognize if your shell
// isn't actually expanding your path...
bottlechoice.replace(0,1,getenv("HOME"));
// or at least you'll think to use verbose mode to make sure it's loading the right directory
output::warning("your shell didn't expand your given path properly, doing a naive replacement", true);
result = bottlechoice;
#ifdef ENABLE_STEAM
} else if (bottlechoice.substr(0,6) == "steam:") { // steam bottles
string str_appid = bottlechoice.substr(6);
unsigned long uint_appid = std::stoul(str_appid);
auto steambottle = cellar::steam::app_bottle(uint_appid);
result = steambottle.path;
#endif
} else {
string homepath = getenv("HOME");
2018-03-13 20:06:15 -07:00
string fullbottlepath = homepath + "/.local/share/cellar/bottles" + bottlechoice;
result = fullbottlepath;
}
return result;
}
2025-02-03 17:28:18 -08:00
/**
* @brief Prints bottles. Used as a command in CLI.
*/
void cellar::bottles::print_bottles(int argc, vector<string> argv) {
2017-03-23 00:02:38 -07:00
map<string, Bottle> bottles = get_bottles();
stringstream outstr;
2017-03-23 00:02:38 -07:00
for (auto item : bottles) {
2018-03-13 20:06:15 -07:00
if (item.first == ".wine" || item.first == ".local/share/cellar/bottles/template") {
// .wine is considered to be "active", and .local/share/cellar/bottles/template is used as a template
// and therefore treated specially
continue;
}
2017-03-23 00:02:38 -07:00
Bottle bottle = item.second;
outstr << item.first << " - ";
2017-03-23 00:02:38 -07:00
switch (bottle.type) {
case bottle_anonymous:
outstr << "anonymous wine bottle";
2017-03-23 00:02:38 -07:00
break;
case bottle_symlink:
outstr << "symlink to " << bottle.canonical_path;
2017-03-23 00:02:38 -07:00
break;
case bottle_labelled:
outstr << bottle.config["name"];
2017-03-23 00:02:38 -07:00
break;
default:
outstr << "broken or unsupported wine bottle";
2017-03-23 00:02:38 -07:00
}
output::statement(outstr.str());
outstr.str("");
2017-03-23 00:02:38 -07:00
}
}