104 lines
2.4 KiB
C++
104 lines
2.4 KiB
C++
#include "beatsaber.h"
|
|
|
|
#if __unix__
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
#include <pwd.h>
|
|
#endif
|
|
|
|
#include <fstream>
|
|
#include <iostream> //debug print
|
|
#include <list>
|
|
#include <filesystem>
|
|
|
|
namespace Beatsaber {
|
|
|
|
#if __unix__
|
|
static std::string getSteamDefault() {
|
|
//get the user process
|
|
uid_t user = getuid();
|
|
struct passwd pwd;
|
|
struct passwd* res;
|
|
char buf[4096];
|
|
getpwuid_r(user, &pwd, buf, sizeof(buf), &res);
|
|
|
|
char* userdirc = pwd.pw_dir;
|
|
std::string userdir(userdirc);
|
|
|
|
return userdir + "/.steam/steam";
|
|
}
|
|
#endif
|
|
|
|
std::string findBeatsaberInstallation() {
|
|
//get all steamfolders
|
|
const std::string defaultSteam = getSteamDefault() + "/steamapps/";
|
|
std::ifstream libinfo(defaultSteam + "libraryfolders.vdf");
|
|
std::string firstline;
|
|
libinfo >> firstline; //remove first line, should contain: "\"LibraryFolders\""
|
|
|
|
std::list<std::string> paths;
|
|
paths.push_back(defaultSteam);
|
|
|
|
std::string line;
|
|
while(std::getline(libinfo, line)) {
|
|
if(!line.empty() && line[0] == '\t') {
|
|
int num;
|
|
char pathbuffer[512];
|
|
int res = sscanf(line.c_str(), "\t\"%d\" \t\"%[^\"]512s", &num, pathbuffer);
|
|
if(res == 2) {
|
|
std::string path(pathbuffer);
|
|
paths.push_back(path + "/steamapps/");
|
|
}
|
|
}
|
|
}
|
|
libinfo.close();
|
|
|
|
//std::cout << paths.size() << " steam libs found" << std::endl;
|
|
|
|
//search in libs for beatsaber
|
|
for(const std::string& it : paths) {
|
|
//check if manifest exists
|
|
const std::string appmani = it + "appmanifest_" + std::to_string(STEAMGAMEID) + ".acf";
|
|
//std::cout << "Try to read file: " << appmani << std::endl;
|
|
std::ifstream appmanifest(appmani);
|
|
if(appmanifest.is_open()) {
|
|
//found it!
|
|
//do stuff with the manifest???
|
|
|
|
appmanifest.close();
|
|
|
|
return it + "common/Beat Saber/";
|
|
}
|
|
}
|
|
|
|
return ""; //not found
|
|
}
|
|
|
|
std::list<std::shared_ptr<BeatMap>> loadMapsfromInstallation(std::string gamePath) {
|
|
if(gamePath.empty()) {
|
|
gamePath = findBeatsaberInstallation();
|
|
}
|
|
|
|
std::filesystem::path path(gamePath);
|
|
|
|
std::list<std::shared_ptr<BeatMap>> out;
|
|
|
|
for(const auto& i : std::filesystem::directory_iterator(path / "Beat Saber_Data/CustomLevels")) {
|
|
if(i.exists()) {
|
|
if(i.is_directory()) {
|
|
//try to load
|
|
auto map = BeatMap::loadFromFolder(i.path().string());
|
|
if(map)
|
|
out.push_back(map);
|
|
} else if(i.is_regular_file()) {
|
|
auto map = BeatMap::loadFromZip(i.path().string());
|
|
if(map)
|
|
out.push_back(map);
|
|
}
|
|
}
|
|
}
|
|
|
|
return out;
|
|
}
|
|
|
|
} |