20namespace fs = std::filesystem;
31 uuid_unparse_lower(uuid, str);
32 return std::string(str);
36static std::string
jsonEscape(
const std::string& str);
41static std::string
jsonParseStr(
const std::string&
json,
const std::string& key,
size_t start = 0,
42 size_t end = std::string::npos)
44 if (end == std::string::npos)
46 std::string needle =
"\"" + key +
"\": \"";
47 size_t pos =
json.find(needle, start);
48 if (pos == std::string::npos || pos >= end)
50 size_t vs = pos + needle.size();
63 return json.substr(vs, ve - vs);
70static long long jsonParseLong(
const std::string&
json,
const std::string& key,
size_t start = 0,
71 size_t end = std::string::npos,
long long def = 0)
73 if (end == std::string::npos)
75 std::string needle =
"\"" + key +
"\": ";
76 size_t pos =
json.find(needle, start);
77 if (pos == std::string::npos || pos >= end)
79 size_t vs = pos + needle.size();
82 return std::stoll(
json.substr(vs, end - vs));
90static int jsonParseInt(
const std::string&
json,
const std::string& key,
size_t start = 0,
91 size_t end = std::string::npos,
int def = 0)
93 if (end == std::string::npos)
95 std::string needle =
"\"" + key +
"\": ";
96 size_t pos =
json.find(needle, start);
97 if (pos == std::string::npos || pos >= end)
99 size_t vs = pos + needle.size();
102 return std::stoi(
json.substr(vs, end - vs));
112 size_t end = std::string::npos,
double def = 0.0)
114 if (end == std::string::npos)
116 std::string needle =
"\"" + key +
"\": ";
117 size_t pos =
json.find(needle, start);
118 if (pos == std::string::npos || pos >= end)
120 size_t vs = pos + needle.size();
123 return std::stod(
json.substr(vs, end - vs));
133 size_t end = std::string::npos,
bool def =
false)
135 if (end == std::string::npos)
137 std::string needle =
"\"" + key +
"\": ";
138 size_t pos =
json.find(needle, start);
139 if (pos == std::string::npos || pos >= end)
141 size_t vs = pos + needle.size();
142 if (vs + 4 <= end &&
json.compare(vs, 4,
"true") == 0)
144 if (vs + 5 <= end &&
json.compare(vs, 5,
"false") == 0)
152 size_t start = 0,
size_t end = std::string::npos)
154 if (end == std::string::npos)
156 std::string needle =
"\"" + key +
"\": {";
157 size_t pos =
json.find(needle, start);
158 if (pos == std::string::npos || pos >= end)
159 return {std::string::npos, std::string::npos};
160 size_t objStart = pos + needle.size() - 1;
162 size_t i = objStart + 1;
163 while (i < end && depth > 0)
167 else if (
json[i] ==
'}')
171 return {objStart, i};
194 if (frs != std::string::npos)
203 if (scs != std::string::npos)
212 if (wrs != std::string::npos)
222 if (sns != std::string::npos)
237 result.reserve(str.size() + 10);
281 fs::create_directories(
dataDir_ +
"/profiles");
282 fs::create_directories(
dataDir_ +
"/global");
284 catch (
const std::exception& e)
286 Logger::error(
"Failed to create profile directories: {}", e.what());
300 std::string profilesDir =
dataDir_ +
"/profiles";
301 if (!fs::exists(profilesDir))
306 for (
const auto& entry : fs::directory_iterator(profilesDir))
308 if (entry.is_regular_file() && entry.path().extension() ==
".json")
314 catch (
const std::exception& e)
322 fs::path gameIdPath(gameId);
323 if (gameIdPath.is_absolute())
325 Logger::error(
"[PerfProfile] Rejected absolute gameId: {}", gameId);
328 for (
const auto& component : gameIdPath)
330 if (component ==
"..")
332 Logger::error(
"[PerfProfile] Rejected gameId with path traversal: {}", gameId);
336 std::string filePath =
dataDir_ +
"/profiles/" + gameId +
".json";
337 if (!fs::exists(filePath))
340 std::ifstream file(filePath);
344 std::stringstream buffer;
345 buffer << file.rdbuf();
346 std::string
json = buffer.str();
359 size_t arrStart =
json.find(
"\"profiles\": [");
360 if (arrStart != std::string::npos)
362 size_t bracket =
json.find(
'[', arrStart);
364 size_t i = bracket + 1;
365 while (i <
json.size() && depth > 0)
367 if (
json[i] ==
'{' && depth == 1)
372 size_t j = objStart + 1;
373 while (j <
json.size() && objDepth > 0)
377 else if (
json[j] ==
'}')
384 data.
profiles.push_back(std::move(p));
390 else if (
json[i] ==
']')
401 std::string globalDir =
dataDir_ +
"/global";
402 if (!fs::exists(globalDir))
407 for (
const auto& entry : fs::directory_iterator(globalDir))
409 if (!entry.is_regular_file() || entry.path().extension() !=
".json")
412 std::ifstream gf(entry.path());
415 std::stringstream gbuf;
417 std::string
gjson = gbuf.str();
424 catch (
const std::exception& e)
426 Logger::warn(
"Error loading global profiles: {}", e.what());
436 fs::path gameIdPath(gameId);
437 if (gameIdPath.is_absolute())
439 Logger::error(
"[PerfProfile] Rejected absolute gameId: {}", gameId);
442 for (
const auto& component : gameIdPath)
444 if (component ==
"..")
446 Logger::error(
"[PerfProfile] Rejected gameId with path traversal: {}", gameId);
450 const auto& data = it->second;
451 std::string filePath =
dataDir_ +
"/profiles/" + gameId +
".json";
453 std::ofstream file(filePath);
456 Logger::error(
"Failed to save profiles for game {}", gameId);
462 file <<
" \"gameId\": \"" <<
jsonEscape(gameId) <<
"\",\n";
463 file <<
" \"activeProfileId\": \"" <<
jsonEscape(data.activeProfileId) <<
"\",\n";
464 file <<
" \"avgFps\": " << data.avgFps <<
",\n";
465 file <<
" \"avgFrametime\": " << data.avgFrametime <<
",\n";
466 file <<
" \"sessionCount\": " << data.sessionCount <<
",\n";
467 file <<
" \"profiles\": [\n";
469 for (
size_t i = 0; i < data.profiles.size(); ++i)
471 const auto& p = data.profiles[i];
473 file <<
" \"id\": \"" <<
jsonEscape(p.id) <<
"\",\n";
474 file <<
" \"name\": \"" <<
jsonEscape(p.name) <<
"\",\n";
475 file <<
" \"description\": \"" <<
jsonEscape(p.description) <<
"\",\n";
476 file <<
" \"preset\": " <<
static_cast<int>(p.preset) <<
",\n";
479 file <<
" \"framerate\": {\n";
480 file <<
" \"fpsLimit\": " << p.framerate.fpsLimit <<
",\n";
481 file <<
" \"refreshRate\": " << p.framerate.refreshRate <<
",\n";
482 file <<
" \"enableVRR\": " << (p.framerate.enableVRR ?
"true" :
"false")
484 file <<
" \"useGamescope\": " << (p.framerate.useGamescope ?
"true" :
"false")
489 file <<
" \"scaling\": {\n";
490 file <<
" \"internalWidth\": " << p.scaling.internalWidth <<
",\n";
491 file <<
" \"internalHeight\": " << p.scaling.internalHeight <<
",\n";
492 file <<
" \"enableFSR\": " << (p.scaling.enableFSR ?
"true" :
"false") <<
",\n";
493 file <<
" \"fsrSharpness\": " << p.scaling.fsrSharpness <<
"\n";
497 file <<
" \"wrappers\": {\n";
498 file <<
" \"enableMangoHud\": " << (p.wrappers.enableMangoHud ?
"true" :
"false")
500 file <<
" \"enableGameMode\": " << (p.wrappers.enableGameMode ?
"true" :
"false")
502 file <<
" \"enableGamePerformance\": "
503 << (p.wrappers.enableGamePerformance ?
"true" :
"false") <<
",\n";
504 file <<
" \"enableVkBasalt\": " << (p.wrappers.enableVkBasalt ?
"true" :
"false")
509 file <<
" \"sync\": {\n";
510 file <<
" \"autoDetect\": " << (p.sync.autoDetect ?
"true" :
"false") <<
",\n";
511 file <<
" \"enableFsync\": " << (p.sync.enableFsync ?
"true" :
"false") <<
",\n";
512 file <<
" \"enableEsync\": " << (p.sync.enableEsync ?
"true" :
"false") <<
",\n";
513 file <<
" \"enableNTSync\": " << (p.sync.enableNTSync ?
"true" :
"false")
517 file <<
" \"createdAt\": " << p.createdAt <<
",\n";
518 file <<
" \"lastUsedAt\": " << p.lastUsedAt <<
",\n";
519 file <<
" \"useCount\": " << p.useCount <<
"\n";
521 if (i < data.profiles.size() - 1)
549 return impl_->initialize(dataDir);
554 return impl_->initialized_;
560 if (!impl_->initialized_)
564 auto& data = impl_->gameProfiles_[gameId];
565 data.gameId = gameId;
568 for (
const auto& existing : data.profiles)
570 if (existing.name == profile.
name)
572 Logger::warn(
"Profile with name '{}' already exists for game {}", profile.
name, gameId);
579 if (newProfile.
id.empty())
583 newProfile.
createdAt = std::time(
nullptr);
585 data.profiles.push_back(newProfile);
588 if (data.profiles.size() == 1 && data.activeProfileId.empty())
590 data.activeProfileId = newProfile.
id;
594 bool saved = impl_->saveGameProfiles(gameId);
596 if (saved && impl_->onProfileChanged_)
598 impl_->onProfileChanged_(gameId, newProfile.
id);
607 if (!impl_->initialized_)
610 auto it = impl_->gameProfiles_.find(gameId);
611 if (it == impl_->gameProfiles_.end())
614 auto& data = it->second;
615 for (
auto& existing : data.profiles)
617 if (existing.id == profile.
id)
620 return impl_->saveGameProfiles(gameId);
628 const std::string& profileId)
630 if (!impl_->initialized_)
633 auto it = impl_->gameProfiles_.find(gameId);
634 if (it == impl_->gameProfiles_.end())
637 auto& data = it->second;
639 std::find_if(data.profiles.begin(), data.profiles.end(),
642 if (profileIt == data.profiles.end())
646 if (data.profiles.size() == 1)
648 Logger::warn(
"Cannot delete the last profile for game {}", gameId);
653 if (data.activeProfileId == profileId)
655 for (
const auto& p : data.profiles)
657 if (p.
id != profileId)
659 data.activeProfileId = p.
id;
665 data.profiles.erase(profileIt);
666 return impl_->saveGameProfiles(gameId);
670 const std::string& sourceProfileId,
671 const std::string& newName)
673 auto source =
getProfile(gameId, sourceProfileId);
679 duplicate.
name = newName;
680 duplicate.
createdAt = std::time(
nullptr);
692std::vector<PerformanceProfile>
695 auto it = impl_->gameProfiles_.find(gameId);
696 if (it != impl_->gameProfiles_.end())
698 return it->second.profiles;
703std::optional<PerformanceProfile>
706 auto it = impl_->gameProfiles_.find(gameId);
707 if (it == impl_->gameProfiles_.end())
710 for (
const auto& p : it->second.profiles)
712 if (p.id == profileId)
721std::optional<PerformanceProfile>
724 auto it = impl_->gameProfiles_.find(gameId);
725 if (it == impl_->gameProfiles_.end())
728 return getProfile(gameId, it->second.activeProfileId);
732 const std::string& profileId)
734 auto it = impl_->gameProfiles_.find(gameId);
735 if (it == impl_->gameProfiles_.end())
740 for (
auto& p : it->second.profiles)
742 if (p.id == profileId)
745 p.lastUsedAt = std::time(
nullptr);
754 it->second.activeProfileId = profileId;
755 bool saved = impl_->saveGameProfiles(gameId);
757 if (saved && impl_->onProfileChanged_)
759 impl_->onProfileChanged_(gameId, profileId);
766 const std::string& name)
const
817 profile.
description =
"Maximum compatibility - disables most optimizations";
825 profile.
description =
"Prioritizes quiet operation over performance";
844 const std::string& protondbTier)
const
847 profile.
name =
"Optimized";
850 if (protondbTier ==
"native")
856 else if (protondbTier ==
"platinum" || protondbTier ==
"gold")
862 else if (protondbTier ==
"silver")
868 else if (protondbTier ==
"bronze" || protondbTier ==
"borked")
877std::map<std::string, std::string>
880 std::map<std::string, std::string> envVars;
885 envVars[
"MANGOHUD"] =
"1";
895 envVars[
"GAMEMODERUNEXEC"] =
"1";
901 envVars[
"GAME_PERFORMANCE"] =
"1";
907 envVars[
"ENABLE_VKBASALT"] =
"1";
919 envVars[
"WINEFSYNC"] =
"1";
920 envVars[
"PROTON_NO_FSYNC"] =
"0";
924 envVars[
"PROTON_NO_FSYNC"] =
"1";
929 envVars[
"WINEESYNC"] =
"1";
930 envVars[
"PROTON_NO_ESYNC"] =
"0";
935 envVars[
"PROTON_USE_NTSYNC"] =
"1";
942 envVars[
"WINE_FULLSCREEN_FSR"] =
"1";
949 envVars[key] = value;
955std::vector<std::string>
958 std::vector<std::string> args;
963 args.push_back(
"-W");
965 args.push_back(
"-H");
971 args.push_back(
"-w");
973 args.push_back(
"-h");
980 args.push_back(
"-r");
987 args.push_back(
"--fps-limit");
994 args.push_back(
"--fps-limit-when-unfocused");
1001 args.push_back(
"--adaptive-sync");
1007 args.push_back(
"--fsr-sharpness");
1014 args.push_back(
"--integer-scale");
1021 const std::string& ,
double avgFps,
1022 double avgFrametime)
1024 auto it = impl_->gameProfiles_.find(gameId);
1025 if (it == impl_->gameProfiles_.end())
1028 auto& data = it->second;
1031 data.avgFps = (data.avgFps * data.sessionCount + avgFps) / (data.sessionCount + 1);
1033 (data.avgFrametime * data.sessionCount + avgFrametime) / (data.sessionCount + 1);
1034 data.sessionCount++;
1036 impl_->saveGameProfiles(gameId);
1041 return impl_->globalProfiles_;
1047 if (newProfile.
id.empty())
1051 newProfile.
createdAt = std::time(
nullptr);
1053 impl_->globalProfiles_.push_back(newProfile);
1056 std::string filePath = impl_->dataDir_ +
"/global/" + newProfile.
id +
".json";
1057 std::ofstream file(filePath);
1068 const std::string& globalProfileId)
1071 auto globalIt = std::find_if(impl_->globalProfiles_.begin(), impl_->globalProfiles_.end(),
1073 { return p.id == globalProfileId; });
1075 if (globalIt == impl_->globalProfiles_.end())
1079 if (std::find(globalIt->linkedGameIds.begin(), globalIt->linkedGameIds.end(), gameId) ==
1080 globalIt->linkedGameIds.end())
1082 globalIt->linkedGameIds.push_back(gameId);
1090 std::ostringstream
json;
1095 json <<
" \"preset\": " <<
static_cast<int>(profile.
preset) <<
",\n";
1096 json <<
" \"framerate\": {\n";
1102 json <<
" \"scaling\": {\n";
1108 json <<
" \"wrappers\": {\n";
1113 json <<
" \"enableGamePerformance\": "
1118 json <<
" \"sync\": {\n";
1119 json <<
" \"autoDetect\": " << (profile.
sync.
autoDetect ?
"true" :
"false") <<
",\n";
1131std::optional<PerformanceProfile>
1135 if (profile.
id.empty())
1136 return std::nullopt;
1141 const std::string& filePath)
const
1143 auto it = impl_->gameProfiles_.find(gameId);
1144 if (it == impl_->gameProfiles_.end())
1147 std::ofstream file(filePath);
1148 if (!file.is_open())
1153 const auto& profiles = it->second.profiles;
1154 for (
size_t i = 0; i < profiles.size(); ++i)
1157 if (i < profiles.size() - 1)
1167 const std::string& filePath)
1169 if (!fs::exists(filePath))
1172 std::ifstream file(filePath);
1173 if (!file.is_open())
1176 std::stringstream buf;
1177 buf << file.rdbuf();
1178 std::string
json = buf.str();
1182 size_t i =
json.find(
'[');
1183 if (i == std::string::npos)
1187 while (i <
json.size())
1190 while (i <
json.size() && (
json[i] ==
' ' ||
json[i] ==
'\n' ||
json[i] ==
'\r' ||
1191 json[i] ==
'\t' ||
json[i] ==
','))
1193 if (i >=
json.size() ||
json[i] ==
']')
1201 size_t objStart = i;
1203 size_t j = objStart + 1;
1204 while (j <
json.size() && depth > 0)
1208 else if (
json[j] ==
'}')
1218 impl_->gameProfiles_[gameId].profiles.push_back(std::move(p));
1225 impl_->saveGameProfiles(gameId);
1232 impl_->onProfileChanged_ = std::move(callback);
1238 for (
const auto& [gameId, data] : impl_->gameProfiles_)
1240 count +=
static_cast<int>(data.profiles.size());
1242 count +=
static_cast<int>(impl_->globalProfiles_.size());
1248 return static_cast<int>(impl_->gameProfiles_.size());
1251std::optional<PerformanceProfile>
1254 auto it = impl_->gameProfiles_.find(gameId);
1255 if (it == impl_->gameProfiles_.end())
1256 return std::nullopt;
1258 const auto& profiles = it->second.profiles;
1259 if (profiles.empty())
1260 return std::nullopt;
1262 auto maxIt = std::max_element(profiles.begin(), profiles.end(),
1264 { return a.useCount < b.useCount; });
1271 std::ostringstream ss;
1279 ss <<
"Unlocked FPS";
1310 return "Battery Life";
1314 return "Performance";
1318 return "Compatibility";
static void error(const std::string &message, Args &&... args)
Log error message (recoverable failure).
static void warn(const std::string &message, Args &&... args)
Log warning message (non-critical issue).
bool halfRateShading
Enable half-rate shading (power saving).
bool enableVRR
Enable Variable Refresh Rate.
int refreshRate
Target refresh rate (0 = native).
int fpsLimit
FPS limit (0 = unlimited).
int gamescopeUnfocusedFps
FPS when unfocused (0 = same as focused).
bool useGamescope
Use Gamescope compositor.
bool allowBoost
Allow CPU/GPU boost clocks.
int tdpLimit
TDP limit in watts (0 = default).
int fanCurvePreset
Fan curve preset (0 = default).
bool enableFSR
AMD FSR upscaling.
int outputHeight
Output/display height (0 = native).
int internalWidth
Internal render width (0 = native).
bool enableDLSS
NVIDIA DLSS (if available).
int internalHeight
Internal render height (0 = native).
int outputWidth
Output/display width (0 = native).
int fsrSharpness
FSR sharpness (0-20, 0 = max sharp).
bool enableIntegerScale
Force integer scaling.
bool autoDetect
Auto-detect best sync method.
bool enableNTSync
Use NTSync (experimental, kernel 6.14+).
bool enableEsync
Use Esync if Fsync unavailable.
bool enableFsync
Use Fsync if available.
std::string vkBasaltEffects
VkBasalt effects to use.
std::string mangoHudConfig
Custom MangoHud config string.
bool enableGameMode
Feral GameMode.
bool enableVkBasalt
VkBasalt post-processing.
bool enableGamePerformance
CachyOS game-performance.
bool enableMangoHud
MangoHud overlay.