Open Source Snapmatic picture and Savegame viewer/editor for GTA V https://gta5view.syping.de/

AppEnv.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. /*****************************************************************************
  2. * gta5view Grand Theft Auto V Profile Viewer
  3. * Copyright (C) 2016-2017 Syping
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *****************************************************************************/
  18. #include "config.h"
  19. #include "AppEnv.h"
  20. #include "StringParser.h"
  21. #include "StandardPaths.h"
  22. #include <QtGlobal>
  23. #include <QStringBuilder>
  24. #include <QDesktopWidget>
  25. #include <QApplication>
  26. #include <QSettings>
  27. #include <QScreen>
  28. #include <QDebug>
  29. #include <QRect>
  30. #include <QDir>
  31. #include <iostream>
  32. using namespace std;
  33. AppEnv::AppEnv()
  34. {
  35. }
  36. // Build Stuff
  37. QString AppEnv::getBuildDateTime()
  38. {
  39. return GTA5SYNC_BUILDDATETIME;
  40. }
  41. QString AppEnv::getBuildCode()
  42. {
  43. return GTA5SYNC_BUILDCODE;
  44. }
  45. // Folder Stuff
  46. QString AppEnv::getGameFolder(bool *ok)
  47. {
  48. QDir dir;
  49. QString GTAV_FOLDER = QString::fromUtf8(qgetenv("GTAV_FOLDER"));
  50. if (GTAV_FOLDER != "")
  51. {
  52. dir.setPath(GTAV_FOLDER);
  53. if (dir.exists())
  54. {
  55. if (ok != NULL) *ok = true;
  56. qputenv("GTAV_FOLDER", dir.absolutePath().toUtf8());
  57. return dir.absolutePath();
  58. }
  59. }
  60. QString GTAV_defaultFolder = StandardPaths::documentsLocation() % QDir::separator() % "Rockstar Games" % QDir::separator() % "GTA V";
  61. QString GTAV_returnFolder = GTAV_defaultFolder;
  62. QSettings settings(GTA5SYNC_APPVENDOR, GTA5SYNC_APPSTR);
  63. settings.beginGroup("dir");
  64. bool forceDir = settings.value("force", false).toBool();
  65. GTAV_returnFolder = settings.value("dir", GTAV_defaultFolder).toString();
  66. settings.endGroup();
  67. if (forceDir)
  68. {
  69. dir.setPath(GTAV_returnFolder);
  70. if (dir.exists())
  71. {
  72. if (ok != 0) *ok = true;
  73. qputenv("GTAV_FOLDER", dir.absolutePath().toUtf8());
  74. return dir.absolutePath();
  75. }
  76. }
  77. dir.setPath(GTAV_defaultFolder);
  78. if (dir.exists())
  79. {
  80. if (ok != 0) *ok = true;
  81. qputenv("GTAV_FOLDER", dir.absolutePath().toUtf8());
  82. return dir.absolutePath();
  83. }
  84. if (!forceDir)
  85. {
  86. dir.setPath(GTAV_returnFolder);
  87. if (dir.exists())
  88. {
  89. if (ok != 0) *ok = true;
  90. qputenv("GTAV_FOLDER", dir.absolutePath().toUtf8());
  91. return dir.absolutePath();
  92. }
  93. }
  94. if (ok != 0) *ok = false;
  95. return "";
  96. }
  97. bool AppEnv::setGameFolder(QString gameFolder)
  98. {
  99. QDir dir;
  100. dir.setPath(gameFolder);
  101. if (dir.exists())
  102. {
  103. qputenv("GTAV_FOLDER", dir.absolutePath().toUtf8());
  104. return true;
  105. }
  106. return false;
  107. }
  108. QString AppEnv::getExLangFolder()
  109. {
  110. return StringParser::convertBuildedString(GTA5SYNC_LANG);
  111. }
  112. QString AppEnv::getInLangFolder()
  113. {
  114. #ifdef GTA5SYNC_QCONF
  115. #ifdef GTA5SYNC_INLANG
  116. return StringParser::convertBuildedString(GTA5SYNC_INLANG);
  117. #else
  118. return StringParser::convertBuildedString(GTA5SYNC_SHARE % QLatin1String("SEPARATOR:APPNAME:SEPARATOR:translations"));
  119. #endif
  120. #else
  121. #ifdef GTA5SYNC_INLANG
  122. return StringParser::convertBuildedString(GTA5SYNC_INLANG);
  123. #else
  124. return QString(":/tr");
  125. #endif
  126. #endif
  127. }
  128. QString AppEnv::getPluginsFolder()
  129. {
  130. return StringParser::convertBuildedString(GTA5SYNC_PLUG);
  131. }
  132. // Web Stuff
  133. QByteArray AppEnv::getUserAgent()
  134. {
  135. #if QT_VERSION >= 0x050400
  136. #ifdef GTA5SYNC_WIN
  137. QString kernelVersion = QSysInfo::kernelVersion();
  138. const QStringList &kernelVersionList = kernelVersion.split(".");
  139. if (kernelVersionList.length() > 2)
  140. {
  141. kernelVersion = kernelVersionList.at(0) % "." % kernelVersionList.at(1);
  142. }
  143. QString runArch = QSysInfo::buildCpuArchitecture();
  144. if (runArch == "x86_64")
  145. {
  146. runArch = "Win64; x64";
  147. }
  148. else if (runArch == "i686")
  149. {
  150. const QString &curArch = QSysInfo::currentCpuArchitecture();
  151. if (curArch == "x86_64")
  152. {
  153. runArch = "WOW64";
  154. }
  155. else if (curArch == "i686")
  156. {
  157. runArch = "Win32; x86";
  158. }
  159. }
  160. return QString("Mozilla/5.0 (Windows NT %1; %2) %3/%4").arg(kernelVersion, runArch, GTA5SYNC_APPSTR, GTA5SYNC_APPVER).toUtf8();
  161. #else
  162. return QString("Mozilla/5.0 (%1; %2) %3/%4").arg(QSysInfo::kernelType(), QSysInfo::kernelVersion(), GTA5SYNC_APPSTR, GTA5SYNC_APPVER).toUtf8();
  163. #endif
  164. #else
  165. return QString("Mozilla/5.0 %1/%2").arg(GTA5SYNC_APPSTR, GTA5SYNC_APPVER).toUtf8();
  166. #endif
  167. }
  168. // QUrl AppEnv::getCrewFetchingUrl(QString crewID)
  169. // {
  170. // return QUrl(QString("https://socialclub.rockstargames.com/reference/crewfeed/%1").arg(crewID));
  171. // }
  172. QUrl AppEnv::getCrewFetchingUrl(QString crewID)
  173. {
  174. return QUrl(QString("https://socialclub.rockstargames.com/crew/%1/%1").arg(crewID));
  175. }
  176. QUrl AppEnv::getPlayerFetchingUrl(QString crewID, QString pageNumber)
  177. {
  178. return QUrl(QString("https://socialclub.rockstargames.com/crewsapi/GetMembersList?crewId=%1&pageNumber=%2&pageSize=5000").arg(crewID, pageNumber));
  179. }
  180. QUrl AppEnv::getPlayerFetchingUrl(QString crewID, int pageNumber)
  181. {
  182. return getPlayerFetchingUrl(crewID, QString::number(pageNumber));
  183. }
  184. // Game Stuff
  185. GameVersion AppEnv::getGameVersion()
  186. {
  187. #ifdef GTA5SYNC_WIN
  188. QString argumentValue;
  189. #ifdef _WIN64
  190. argumentValue = "\\WOW6432Node";
  191. #endif
  192. QSettings registrySettingsSc(QString("HKEY_LOCAL_MACHINE\\SOFTWARE%1\\Rockstar Games\\Grand Theft Auto V").arg(argumentValue), QSettings::NativeFormat);
  193. QString installFolderSc = registrySettingsSc.value("InstallFolder", "").toString();
  194. QDir installFolderScDir(installFolderSc);
  195. bool scVersionInstalled = false;
  196. if (!installFolderSc.isEmpty() && installFolderScDir.exists())
  197. {
  198. #ifdef GTA5SYNC_DEBUG
  199. qDebug() << "gameVersionFoundSocialClubVersion";
  200. #endif
  201. scVersionInstalled = true;
  202. }
  203. QSettings registrySettingsSteam(QString("HKEY_LOCAL_MACHINE\\SOFTWARE%1\\Rockstar Games\\GTAV").arg(argumentValue), QSettings::NativeFormat);
  204. QString installFolderSteam = registrySettingsSteam.value("installfoldersteam", "").toString();
  205. if (installFolderSteam.right(5) == "\\GTAV")
  206. {
  207. installFolderSteam = installFolderSteam.remove(installFolderSteam.length() - 5, 5);
  208. }
  209. QDir installFolderSteamDir(installFolderSteam);
  210. bool steamVersionInstalled = false;
  211. if (!installFolderSteam.isEmpty() && installFolderSteamDir.exists())
  212. {
  213. #ifdef GTA5SYNC_DEBUG
  214. qDebug() << "gameVersionFoundSteamVersion";
  215. #endif
  216. steamVersionInstalled = true;
  217. }
  218. if (scVersionInstalled && steamVersionInstalled)
  219. {
  220. return GameVersion::BothVersions;
  221. }
  222. else if (scVersionInstalled)
  223. {
  224. return GameVersion::SocialClubVersion;
  225. }
  226. else if (steamVersionInstalled)
  227. {
  228. return GameVersion::SteamVersion;
  229. }
  230. else
  231. {
  232. return GameVersion::NoVersion;
  233. }
  234. #else
  235. return GameVersion::NoVersion;
  236. #endif
  237. }
  238. GameLanguage AppEnv::getGameLanguage(GameVersion gameVersion)
  239. {
  240. if (gameVersion == GameVersion::SocialClubVersion)
  241. {
  242. #ifdef GTA5SYNC_WIN
  243. QString argumentValue;
  244. #ifdef _WIN64
  245. argumentValue = "\\WOW6432Node";
  246. #endif
  247. QSettings registrySettingsSc(QString("HKEY_LOCAL_MACHINE\\SOFTWARE%1\\Rockstar Games\\Grand Theft Auto V").arg(argumentValue), QSettings::NativeFormat);
  248. QString languageSc = registrySettingsSc.value("Language", "").toString();
  249. return gameLanguageFromString(languageSc);
  250. #else
  251. return GameLanguage::Undefined;
  252. #endif
  253. }
  254. else if (gameVersion == GameVersion::SteamVersion)
  255. {
  256. #ifdef GTA5SYNC_WIN
  257. QString argumentValue;
  258. #ifdef _WIN64
  259. argumentValue = "\\WOW6432Node";
  260. #endif
  261. QSettings registrySettingsSteam(QString("HKEY_LOCAL_MACHINE\\SOFTWARE%1\\Rockstar Games\\Grand Theft Auto V Steam").arg(argumentValue), QSettings::NativeFormat);
  262. QString languageSteam = registrySettingsSteam.value("Language", "").toString();
  263. return gameLanguageFromString(languageSteam);
  264. #else
  265. return GameLanguage::Undefined;
  266. #endif
  267. }
  268. else
  269. {
  270. return GameLanguage::Undefined;
  271. }
  272. }
  273. GameLanguage AppEnv::gameLanguageFromString(QString gameLanguage)
  274. {
  275. if (gameLanguage == "en-US")
  276. {
  277. return GameLanguage::English;
  278. }
  279. else if (gameLanguage == "fr-FR")
  280. {
  281. return GameLanguage::French;
  282. }
  283. else if (gameLanguage == "it-IT")
  284. {
  285. return GameLanguage::Italian;
  286. }
  287. else if (gameLanguage == "de-DE")
  288. {
  289. return GameLanguage::German;
  290. }
  291. else if (gameLanguage == "es-ES")
  292. {
  293. return GameLanguage::Spanish;
  294. }
  295. else if (gameLanguage == "es-MX")
  296. {
  297. return GameLanguage::Mexican;
  298. }
  299. else if (gameLanguage == "pt-BR")
  300. {
  301. return GameLanguage::Brasilian;
  302. }
  303. else if (gameLanguage == "ru-RU")
  304. {
  305. return GameLanguage::Russian;
  306. }
  307. else if (gameLanguage == "pl-PL")
  308. {
  309. return GameLanguage::Polish;
  310. }
  311. else if (gameLanguage == "ja-JP")
  312. {
  313. return GameLanguage::Japanese;
  314. }
  315. else if (gameLanguage == "zh-CHS")
  316. {
  317. return GameLanguage::SChinese;
  318. }
  319. else if (gameLanguage == "zh-CHT")
  320. {
  321. return GameLanguage::TChinese;
  322. }
  323. else if (gameLanguage == "ko-KR")
  324. {
  325. return GameLanguage::Koreana;
  326. }
  327. else
  328. {
  329. return GameLanguage::Undefined;
  330. }
  331. }
  332. QString AppEnv::gameLanguageToString(GameLanguage gameLanguage)
  333. {
  334. if (gameLanguage == GameLanguage::English)
  335. {
  336. return "en-US";
  337. }
  338. else if (gameLanguage == GameLanguage::French)
  339. {
  340. return "fr-FR";
  341. }
  342. else if (gameLanguage == GameLanguage::Italian)
  343. {
  344. return "it-IT";
  345. }
  346. else if (gameLanguage == GameLanguage::German)
  347. {
  348. return "de-DE";
  349. }
  350. else if (gameLanguage == GameLanguage::Spanish)
  351. {
  352. return "es-ES";
  353. }
  354. else if (gameLanguage == GameLanguage::Mexican)
  355. {
  356. return "es-MX";
  357. }
  358. else if (gameLanguage == GameLanguage::Brasilian)
  359. {
  360. return "pt-BR";
  361. }
  362. else if (gameLanguage == GameLanguage::Russian)
  363. {
  364. return "ru-RU";
  365. }
  366. else if (gameLanguage == GameLanguage::Polish)
  367. {
  368. return "pl-PL";
  369. }
  370. else if (gameLanguage == GameLanguage::Japanese)
  371. {
  372. return "ja-JP";
  373. }
  374. else if (gameLanguage == GameLanguage::SChinese)
  375. {
  376. return "zh-CHS";
  377. }
  378. else if (gameLanguage == GameLanguage::TChinese)
  379. {
  380. return "zh-CHT";
  381. }
  382. else if (gameLanguage == GameLanguage::Koreana)
  383. {
  384. return "ko-KR";
  385. }
  386. else
  387. {
  388. return "Undefinied";
  389. }
  390. }
  391. bool AppEnv::setGameLanguage(GameVersion gameVersion, GameLanguage gameLanguage)
  392. {
  393. bool socialClubVersion = false;
  394. bool steamVersion = false;
  395. if (gameVersion == GameVersion::SocialClubVersion)
  396. {
  397. socialClubVersion = true;
  398. }
  399. else if (gameVersion == GameVersion::SteamVersion)
  400. {
  401. steamVersion = true;
  402. }
  403. else if (gameVersion == GameVersion::BothVersions)
  404. {
  405. socialClubVersion = true;
  406. steamVersion = true;
  407. }
  408. else
  409. {
  410. return false;
  411. }
  412. if (socialClubVersion)
  413. {
  414. #ifdef GTA5SYNC_WIN
  415. QString argumentValue;
  416. #ifdef _WIN64
  417. argumentValue = "\\WOW6432Node";
  418. #endif
  419. QSettings registrySettingsSc(QString("HKEY_LOCAL_MACHINE\\SOFTWARE%1\\Rockstar Games\\Grand Theft Auto V").arg(argumentValue), QSettings::NativeFormat);
  420. if (gameLanguage != GameLanguage::Undefined)
  421. {
  422. registrySettingsSc.setValue("Language", gameLanguageToString(gameLanguage));
  423. }
  424. else
  425. {
  426. registrySettingsSc.remove("Language");
  427. }
  428. registrySettingsSc.sync();
  429. if (registrySettingsSc.status() != QSettings::NoError)
  430. {
  431. return false;
  432. }
  433. #else
  434. Q_UNUSED(gameLanguage)
  435. #endif
  436. }
  437. if (steamVersion)
  438. {
  439. #ifdef GTA5SYNC_WIN
  440. QString argumentValue;
  441. #ifdef _WIN64
  442. argumentValue = "\\WOW6432Node";
  443. #endif
  444. QSettings registrySettingsSteam(QString("HKEY_LOCAL_MACHINE\\SOFTWARE%1\\Rockstar Games\\Grand Theft Auto V Steam").arg(argumentValue), QSettings::NativeFormat);
  445. if (gameLanguage != GameLanguage::Undefined)
  446. {
  447. registrySettingsSteam.setValue("Language", gameLanguageToString(gameLanguage));
  448. }
  449. else
  450. {
  451. registrySettingsSteam.remove("Language");
  452. }
  453. registrySettingsSteam.sync();
  454. if (registrySettingsSteam.status() != QSettings::NoError)
  455. {
  456. return false;
  457. }
  458. #else
  459. Q_UNUSED(gameLanguage)
  460. #endif
  461. }
  462. return true;
  463. }
  464. // Screen Stuff
  465. qreal AppEnv::screenRatio()
  466. {
  467. #if QT_VERSION >= 0x050000
  468. qreal dpi = QGuiApplication::primaryScreen()->logicalDotsPerInch();
  469. #else
  470. qreal dpi = qApp->desktop()->logicalDpiX();
  471. #endif
  472. #ifdef Q_OS_MAC
  473. return (dpi / 72);
  474. #else
  475. return (dpi / 96);
  476. #endif
  477. }