libBeatsaber/src/beatsaber.cpp

75 lines
1.7 KiB
C++
Raw Normal View History

2021-06-04 20:27:20 +02:00
#include "beatsaber.h"
#if __unix__
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#endif
#include <fstream>
#include <iostream> //debug print
#include <list>
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];
2021-06-04 21:47:28 +02:00
int res = sscanf(line.c_str(), "\t\"%d\" \t\"%[^\"]512s", &num, pathbuffer);
2021-06-04 20:27:20 +02:00
if(res == 2) {
std::string path(pathbuffer);
paths.push_back(path + "/steamapps/");
}
}
}
libinfo.close();
2021-06-04 21:47:28 +02:00
//std::cout << paths.size() << " steam libs found" << std::endl;
2021-06-04 20:27:20 +02:00
//search in libs for beatsaber
for(const std::string& it : paths) {
2021-06-04 21:47:28 +02:00
const std::string appmani = it + "appmanifest_" + std::to_string(STEAMGAMEID) + ".acf";
//std::cout << "Try to read file: " << appmani << std::endl;
std::ifstream appmanifest(appmani);
2021-06-04 20:27:20 +02:00
if(appmanifest.is_open()) {
//found it!
//do stuff with the manifest???
appmanifest.close();
return it + "common/Beat Saber/";
}
}
return ""; //not found
}
}