-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleaguefiles.cpp
97 lines (91 loc) · 4.48 KB
/
leaguefiles.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <QtSystemDetection>
#include "leaguefiles.h"
#include <sqlite3.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <thread>
#include <chrono>
leagueDBfind::leagueDBfind() : lockfile("0"),portnum("0"),password("0") { // constructor tries to find the location of the lutris database with the XDG_DATA_HOME environmental variable. If it doesn't, it gets set to ~/.local/share/lutris/pga.db
#ifdef Q_OS_LINUX
char* env = std::getenv("XDG_DATA_HOME");
XDG_DATA_HOME = env ? env : "";
if (XDG_DATA_HOME.empty()) {
std::cout << "Environmental var: XDG_DATA_HOME doesn't exist. defaulting to ~/.local/share\n";
XDG_DATA_HOME=static_cast<std::string>(std::getenv("HOME"))+"/.local/share";
}
databaselocation = XDG_DATA_HOME+"/lutris/pga.db";
#else
std::cout << "made leagueDBfind object\n" << std::endl;
#endif
}
int leagueDBfind::open() { //give sqlite3 object the location of the lutris database
return sqlite3_open(databaselocation.c_str(), &database);
}
int leagueDBfind::callback(void *currentDB, int a, char **res, char **azColName) { //gets called by getinstalllocation.
static_cast<leagueDBfind *>(currentDB)->installlocation = *res;
static_cast<leagueDBfind *>(currentDB)->lockfile = static_cast<leagueDBfind *>(currentDB)->installlocation+"/drive_c/Riot Games/League of Legends/lockfile";
return 0;
}
int leagueDBfind::getinstalllocation() { // run sql command to find the location league of legends lockfile
return sqlite3_exec(database, "SELECT \"directory\" FROM \"main\".\"games\" WHERE \"slug\" LIKE 'league-of-legends'", callback, this, NULL); //callback function does not get called when using databaselocation.c_str()
}
const std::string& leagueDBfind::getlocklocation() { return lockfile; }
const std::string& leagueDBfind::getdatabaselocation() { return databaselocation; }
const std::string& leagueDBfind::thepassword() { return password; }
const std::string& leagueDBfind::theportnum() { return portnum; }
void leagueDBfind::setpassword(const std::string& pw) { password=pw; }
void leagueDBfind::setportnum(const std::string& port) { portnum=port; }
std::ifstream getfile(leagueDBfind& database) { //Get credentials from lockfile
#ifdef Q_OS_WIN32 // i haven't tried this program with windows at all. I wanted to but I don't know how to cross compile for windows
std::ifstream lockfile("C:\\Riot Games\\League of Legends\\lockfile");
#endif
#ifdef Q_OS_LINUX
std::ifstream lockfile;
if (database.getlocklocation() != "0") { //if the lockfile is already set to something, then it doesn't need to be found again
lockfile.open(database.getlocklocation());
} else {
int errcode = database.open();
if (errcode) std::cerr << "couldn't open database to find league of legends install location\n"; //if the database wasn't opened properly
errcode = database.getinstalllocation();
if (errcode) std::cerr << "couldn't find install location errorcode: " << errcode << '\n';
lockfile.open(database.getlocklocation());
}
while (!lockfile.is_open()) { // if it didn't get anything, something went wrong. Retry until process is killed
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "League isn't open yet or something else went wrong. Retrying..." << std::endl;
lockfile.clear(std::ifstream::eofbit);
lockfile.open(database.getlocklocation());
}
#endif
#ifdef Q_OS_MACOS
const std::string lockpath = "/Applications/League of Legends.app/Contents/LoL/lockfile";
while (!std::filesystem::exists(lockpath)) {
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << "Couldn't find lockfile, is league open? Does /Applications/League of Legends.app/Contents/LoL/lockfile exist?" << std::endl;
}
std::ifstream lockfile(lockpath);
#endif
return lockfile;
}
std::vector<std::string> getdata(std::ifstream &lockfile) { //parses the lockfile for the portnum and password (which is in base64 in the lockfile)
std::vector<std::string> svec;
std::string portnum("");
std::string pass("riot:");
char ch;
for (int coloncount = 0; !lockfile.eof();) {
if (lockfile >> ch && ch == ':') {
++coloncount;
} else switch (coloncount) {
case 2:
portnum += ch;
break;
case 3:
pass += ch;
break;
}
}
svec.push_back(std::string(portnum));
svec.push_back(std::string(pass));
return svec;
}