30namespace fs = std::filesystem;
41 [](
const std::shared_ptr<std::future<void>>& fu)
43 return fu->wait_for(std::chrono::seconds(0)) ==
44 std::future_status::ready;
57 const char* homeEnv = std::getenv(
"HOME");
58 if (!homeEnv || homeEnv[0] ==
'\0')
61 fs::path canonical = fs::weakly_canonical(fs::path(rawPath), ec);
64 const std::string canonStr = canonical.string();
65 const std::string homeStr = fs::path(homeEnv).string();
67 return canonStr.size() > homeStr.size() && canonStr.compare(0, homeStr.size(), homeStr) == 0 &&
68 (homeStr.back() ==
'/' || canonStr[homeStr.size()] ==
'/');
79 const std::string component = fs::path(raw).filename().string();
80 if (component.empty() || component ==
"." || component ==
"..")
87static bool isPathWithin(
const std::string& candidate,
const std::string& root)
90 fs::path canon = fs::weakly_canonical(fs::path(candidate), ec);
93 fs::path canonRoot = fs::weakly_canonical(fs::path(root), ec);
96 fs::path rel = fs::relative(canon, canonRoot, ec);
97 if (ec || rel.empty())
99 return *rel.begin() !=
"..";
109 return ::desktop_entry::sanitizeField(s);
113 return ::desktop_entry::escapeExec(s);
117static bool endsWith(
const std::string& str,
const std::string& suffix)
119 if (suffix.size() > str.size())
121 return str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
129std::atomic<bool> InstallationExecutor::cancelRequested_{
false};
131std::mutex InstallationExecutor::progressMutex_;
146 auto startTime = std::chrono::steady_clock::now();
150 cancelRequested_ =
false;
155 static_cast<int>(script.
steps.size()) + 3;
159 std::map<std::string, std::string> variables = buildVariables(script, config);
162 addLogMessage(progress, result,
"Preparing installation...");
164 updateProgress(progress, progressCallback);
166 if (!executePrepare(script, mutableConfig, result, variables, progressCallback))
172 if (cancelRequested_)
184 updateProgress(progress, progressCallback);
186 if (!executeDownloads(script, mutableConfig, result, variables, progressCallback))
192 if (cancelRequested_)
203 updateProgress(progress, progressCallback);
205 if (!executeSteps(script, mutableConfig, result, variables, progressCallback))
211 if (cancelRequested_)
222 updateProgress(progress, progressCallback);
224 if (!executeFinalize(script, mutableConfig, result, variables, progressCallback))
231 auto endTime = std::chrono::steady_clock::now();
233 std::chrono::duration_cast<std::chrono::seconds>(endTime - startTime).count());
236 result.
installPath = substituteVariables(
"$GAMEDIR", variables);
238 substituteVariables(script.
prefix.empty() ?
"$GAMEDIR/prefix" : script.
prefix, variables);
255 addLogMessage(progress, result,
"Installation completed successfully!");
256 updateProgress(progress, progressCallback);
269 std::make_shared<std::future<void>>(std::async(std::launch::async,
270 [script, config, onComplete, onProgress]()
273 install(script, config, onProgress);
277 storeFuture(std::move(future));
286 cancelRequested_ =
true;
300 return currentState_;
305 std::lock_guard<std::mutex> lock(progressMutex_);
306 return currentProgress_;
316 std::vector<std::string> errors;
320 errors.push_back(
"Invalid installer script");
325 errors.push_back(
"Installation path is required");
332 if (available > 0 && required > available)
334 std::ostringstream oss;
335 oss <<
"Insufficient disk space. Required: " << (required / (1024 * 1024)) <<
" MB, "
336 <<
"Available: " << (available / (1024 * 1024)) <<
" MB";
337 errors.push_back(oss.str());
341 for (
const auto& file : script.
files)
345 errors.push_back(
"This installer requires manual file: " + file.id +
346 ". Automatic installation not supported.");
355 for (
const auto& step : script.
steps)
357 if (step.task ==
"extract")
365 if (script.
runner ==
"wine")
367 std::string wineCheck =
"command -v wine >/dev/null 2>&1";
368 if (std::system(wineCheck.c_str()) != 0)
370 errors.push_back(
"Wine is not installed");
382 for (
const auto& file : script.
files)
391 total += 500 * 1024 * 1024;
402 while (!fs::exists(p) && p.has_parent_path())
409 fs::space_info si = fs::space(p);
413 catch (
const std::exception& e)
415 Logger::warn(
"Installation: Failed to get disk space: {}", e.what());
427 std::map<std::string, std::string>& variables,
428 const ProgressCallback& progress)
436 std::string installDir = substituteVariables(
"$GAMEDIR", variables);
439 fs::create_directories(installDir);
440 addLogMessage(prog, result,
"Created install directory: " + installDir);
442 catch (
const std::exception& e)
444 result.
errorMessage =
"Failed to create install directory: " + std::string(e.what());
450 std::string cacheDir = substituteVariables(
"$CACHE", variables);
453 fs::create_directories(cacheDir);
455 catch (
const std::exception& e)
457 result.
errorMessage =
"Failed to create cache directory: " + std::string(e.what());
463 variables[
"GAMEDIR"] = installDir;
464 variables[
"CACHE"] = cacheDir;
466 updateProgress(prog, progress);
472 std::map<std::string, std::string>& variables,
473 const ProgressCallback& progress)
475 if (script.
files.empty())
480 InstallProgress prog;
484 std::string cacheDir = variables[
"CACHE"];
487 for (
const auto& file : script.
files)
489 if (cancelRequested_)
496 result.
errorMessage =
"Manual file required: " + file.id;
507 if (safeName.empty())
509 if (safeName.empty())
510 safeName =
"file_" + std::to_string(fileIndex);
514 updateProgress(prog, progress);
523 result.
errorMessage =
"Rejected unsafe download path for installer file: " + file.id;
529 if (fs::exists(localPath))
534 std::string checksumType = (file.checksum.length() == 32) ?
"md5"
535 : (file.checksum.length() == 40) ?
"sha1"
542 addLogMessage(prog, result,
"Using cached file: " + prog.
currentFileName);
543 variables[file.id] = localPath;
549 addLogMessage(prog, result,
"Cached file failed checksum, re-downloading");
550 fs::remove(localPath);
559 req.
checksumType = file.checksum.length() == 32 ?
"md5" :
"sha256";
561 auto downloadProgress = [&prog, &progress](
const DownloadProgress& dp)
567 updateProgress(prog, progress);
580 variables[file.id] = localPath;
591 std::map<std::string, std::string>& variables,
592 const ProgressCallback& progress)
594 InstallProgress prog;
599 if (script.
runner ==
"wine")
603 updateProgress(prog, progress);
605 std::string prefixPath = substituteVariables(
606 script.
prefix.empty() ?
"$GAMEDIR/prefix" : script.
prefix, variables);
610 if (!createWinePrefix(prefixPath, arch, config.
wineVersion, result))
615 variables[
"WINEPREFIX"] = prefixPath;
616 variables[
"PREFIX"] = prefixPath;
619 for (
const auto& step : script.
steps)
621 if (step.task ==
"task")
623 auto nameIt = step.params.find(
"name");
624 if (nameIt != step.params.end() && nameIt->second ==
"winetricks")
627 auto appIt = step.params.find(
"app");
628 if (appIt != step.params.end())
630 verbs = appIt->second;
634 auto argsIt = step.params.find(
"args");
635 if (argsIt != step.params.end())
637 verbs = argsIt->second;
643 std::vector<std::string> verbList;
644 std::istringstream iss(verbs);
648 verbList.push_back(verb);
653 updateProgress(prog, progress);
655 runWinetricks(prefixPath, config.
wineVersion, verbList, result);
664 for (
const auto& step : script.
steps)
666 if (cancelRequested_)
672 if (step.task ==
"task")
674 auto nameIt = step.params.find(
"name");
675 if (nameIt != step.params.end() && nameIt->second ==
"winetricks")
685 addLogMessage(prog, result,
686 "Step " + std::to_string(stepIndex + 1) +
": " + step.description);
687 updateProgress(prog, progress);
689 if (!executeStep(step, variables, result))
692 addWarning(prog, result,
"Step failed: " + step.description);
693 Logger::warn(
"Installation: Step failed but continuing: {}", step.description);
698 updateProgress(prog, progress);
708 std::map<std::string, std::string>& variables,
709 const ProgressCallback& progress)
711 InstallProgress prog;
718 std::string cacheDir = variables[
"CACHE"];
719 if (fs::exists(cacheDir))
721 addLogMessage(prog, result,
"Cleaning up downloaded files...");
724 fs::remove_all(cacheDir);
726 catch (
const std::exception& e)
728 addWarning(prog, result,
"Failed to clean up cache: " + std::string(e.what()));
736 addLogMessage(prog, result,
"Creating desktop shortcut...");
737 if (!createDesktopShortcut(result))
739 result.
warnings.push_back(
"Failed to create desktop shortcut");
746 addLogMessage(prog, result,
"Creating menu entry...");
747 if (!createMenuEntry(result))
749 result.
warnings.push_back(
"Failed to create menu entry");
753 updateProgress(prog, progress);
762 std::map<std::string, std::string>& variables,
768 if (step.
task ==
"extract")
770 return stepExtract(step, variables, result);
772 else if (step.
task ==
"copy" || step.
task ==
"merge")
774 return stepCopy(step, variables, result);
776 else if (step.
task ==
"move")
778 return stepMove(step, variables, result);
780 else if (step.
task ==
"execute")
782 return stepExecute(step, variables, result);
784 else if (step.
task ==
"write_file" || step.
task ==
"write_json" || step.
task ==
"write_config")
786 return stepWriteFile(step, variables, result);
788 else if (step.
task ==
"chmodx")
790 return stepChmodx(step, variables, result);
792 else if (step.
task ==
"delete")
794 return stepDelete(step, variables, result);
796 else if (step.
task ==
"rename")
798 return stepRename(step, variables, result);
800 else if (step.
task ==
"task")
802 return stepTask(step, variables, result);
812 std::map<std::string, std::string>& variables,
815 std::string fileRef = step.
params.count(
"file") ? step.
params.at(
"file") :
"";
816 std::string dst = step.
params.count(
"dst") ? step.
params.at(
"dst") :
"$GAMEDIR";
818 std::string archivePath = substituteVariables(fileRef, variables);
819 std::string destPath = substituteVariables(dst, variables);
821 Logger::info(
"Installation: Extracting {} to {}", archivePath, destPath);
823 if (!fs::exists(archivePath))
825 result.
errorMessage =
"Archive not found: " + archivePath;
831 result.
errorMessage =
"Extract destination outside home directory: " + destPath;
832 Logger::error(
"Installation: Blocked extract to unsafe path: {}", destPath);
853 std::map<std::string, std::string>& variables,
856 std::string src = step.
params.count(
"src") ? step.
params.at(
"src") :
"";
857 std::string dst = step.
params.count(
"dst") ? step.
params.at(
"dst") :
"";
859 std::string srcPath = substituteVariables(src, variables);
860 std::string dstPath = substituteVariables(dst, variables);
866 Logger::error(
"Installation: stepCopy refused dst '{}': path outside home directory",
868 result.
errorMessage =
"Copy refused: destination path outside home directory";
872 Logger::info(
"Installation: Copying {} to {}", srcPath, dstPath);
877 if (srcPath.find(
'*') != std::string::npos)
887 fs::path srcDir = fs::path(srcPath).parent_path();
888 std::string pattern = fs::path(srcPath).filename().string();
890 std::string regexPattern;
891 regexPattern.reserve(pattern.size() * 2 + 2);
892 regexPattern.push_back(
'^');
893 for (
char c : pattern)
898 regexPattern +=
".*";
916 regexPattern.push_back(
'\\');
917 regexPattern.push_back(c);
920 regexPattern.push_back(c);
923 regexPattern.push_back(
'$');
925 std::regex fileRegex(regexPattern);
927 for (
const auto& entry : fs::directory_iterator(srcDir))
929 if (std::regex_match(entry.path().filename().string(), fileRegex))
931 fs::path target = fs::path(dstPath) / entry.path().filename();
932 fs::create_directories(target.parent_path());
933 fs::copy(entry.path(), target,
934 fs::copy_options::recursive | fs::copy_options::overwrite_existing);
940 fs::create_directories(fs::path(dstPath).parent_path());
941 fs::copy(srcPath, dstPath,
942 fs::copy_options::recursive | fs::copy_options::overwrite_existing);
945 catch (
const std::exception& e)
947 result.
errorMessage =
"Copy failed: " + std::string(e.what());
955 std::map<std::string, std::string>& variables,
958 std::string src = step.
params.count(
"src") ? step.
params.at(
"src") :
"";
959 std::string dst = step.
params.count(
"dst") ? step.
params.at(
"dst") :
"";
961 std::string srcPath = substituteVariables(src, variables);
962 std::string dstPath = substituteVariables(dst, variables);
966 Logger::error(
"Installation: stepMove refused '{}' -> '{}': path outside home directory",
968 result.
errorMessage =
"Move refused: path outside home directory";
972 Logger::info(
"Installation: Moving {} to {}", srcPath, dstPath);
976 fs::create_directories(fs::path(dstPath).parent_path());
977 fs::rename(srcPath, dstPath);
979 catch (
const std::exception& e)
981 result.
errorMessage =
"Move failed: " + std::string(e.what());
989 std::map<std::string, std::string>& variables,
992 std::string file = step.
params.count(
"file") ? step.
params.at(
"file") :
"";
993 std::string args = step.
params.count(
"args") ? step.
params.at(
"args") :
"";
995 std::string exePath = substituteVariables(file, variables);
996 std::string arguments = substituteVariables(args, variables);
998 Logger::info(
"Installation: Executing {} {}", exePath, arguments);
1003 std::string prefix = variables.count(
"WINEPREFIX") ? variables[
"WINEPREFIX"] :
"";
1005 std::ostringstream cmd;
1006 if (!prefix.empty())
1014 int exitCode = runCommand(cmd.str(), output);
1018 Logger::warn(
"Installation: Execute returned non-zero: {}", exitCode);
1025 std::ostringstream cmd;
1029 int exitCode = runCommand(cmd.str(), output);
1033 Logger::warn(
"Installation: Execute returned non-zero: {}", exitCode);
1041 std::map<std::string, std::string>& variables,
1044 std::string file = step.
params.count(
"file") ? step.
params.at(
"file") :
"";
1045 std::string content = step.
params.count(
"content") ? step.
params.at(
"content") :
"";
1047 std::string filePath = substituteVariables(file, variables);
1048 std::string fileContent = substituteVariables(content, variables);
1052 Logger::error(
"Installation: stepWriteFile refused '{}': path outside home directory",
1054 result.
errorMessage =
"Write file refused: path outside home directory";
1058 Logger::info(
"Installation: Writing file {}", filePath);
1062 fs::create_directories(fs::path(filePath).parent_path());
1063 std::ofstream out(filePath);
1070 result.
errorMessage =
"Failed to open " + filePath +
" for writing";
1076 result.
errorMessage =
"Write to " + filePath +
" failed (disk full or I/O error)";
1078 std::error_code rmEc;
1079 fs::remove(filePath, rmEc);
1085 result.
errorMessage =
"Failed to flush " + filePath +
" on close";
1089 catch (
const std::exception& e)
1091 result.
errorMessage =
"Write file failed: " + std::string(e.what());
1099 std::map<std::string, std::string>& variables,
1102 std::string name = step.
params.count(
"name") ? step.
params.at(
"name") :
"";
1104 if (name ==
"create_prefix")
1106 std::string prefix =
1107 step.
params.count(
"prefix") ? step.
params.at(
"prefix") :
"$GAMEDIR/prefix";
1108 std::string arch = step.
params.count(
"arch") ? step.
params.at(
"arch") :
"win64";
1110 std::string prefixPath = substituteVariables(prefix, variables);
1111 return createWinePrefix(prefixPath, arch,
"", result);
1113 else if (name ==
"set_regedit")
1115 std::string prefix = variables.count(
"WINEPREFIX") ? variables[
"WINEPREFIX"] :
"";
1116 std::string path = step.
params.count(
"path") ? step.
params.at(
"path") :
"";
1117 std::string key = step.
params.count(
"key") ? step.
params.at(
"key") :
"";
1118 std::string value = step.
params.count(
"value") ? step.
params.at(
"value") :
"";
1119 std::string type = step.
params.count(
"type") ? step.
params.at(
"type") :
"REG_SZ";
1121 return setWineRegistry(prefix,
"", path, key, value, type, result);
1123 else if (name ==
"winekill")
1125 std::string prefix = variables.count(
"WINEPREFIX") ? variables[
"WINEPREFIX"] :
"";
1126 std::ostringstream cmd;
1127 if (!prefix.empty())
1131 cmd <<
"wineserver -k 2>/dev/null";
1132 [[maybe_unused]]
int ret = std::system(cmd.str().c_str());
1141 std::map<std::string, std::string>& variables,
1144 std::string file = step.
params.count(
"file") ? step.
params.at(
"file") :
"";
1145 std::string filePath = substituteVariables(file, variables);
1149 Logger::error(
"Installation: stepChmodx refused '{}': path outside home directory",
1151 result.
errorMessage =
"chmod refused: path outside home directory";
1155 Logger::info(
"Installation: Making executable: {}", filePath);
1159 fs::permissions(filePath,
1160 fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec,
1161 fs::perm_options::add);
1163 catch (
const std::exception& e)
1165 result.
errorMessage =
"chmod failed: " + std::string(e.what());
1173 std::map<std::string, std::string>& variables,
1176 std::string file = step.
params.count(
"file") ? step.
params.at(
"file") :
"";
1177 std::string filePath = substituteVariables(file, variables);
1181 Logger::error(
"Installation: stepDelete refused '{}': path outside home directory",
1190 fs::remove_all(filePath);
1192 catch (
const std::exception& e)
1194 Logger::warn(
"Installation: Delete failed: {}", e.what());
1202 std::map<std::string, std::string>& variables,
1205 std::string src = step.
params.count(
"src") ? step.
params.at(
"src") :
"";
1206 std::string dst = step.
params.count(
"dst") ? step.
params.at(
"dst") :
"";
1208 std::string srcPath = substituteVariables(src, variables);
1209 std::string dstPath = substituteVariables(dst, variables);
1213 Logger::error(
"Installation: stepRename refused '{}' -> '{}': path outside home directory",
1215 result.
errorMessage =
"Rename refused: path outside home directory";
1219 Logger::info(
"Installation: Renaming {} to {}", srcPath, dstPath);
1223 fs::rename(srcPath, dstPath);
1225 catch (
const std::exception& e)
1227 result.
errorMessage =
"Rename failed: " + std::string(e.what());
1238bool InstallationExecutor::createWinePrefix(
const std::string& prefixPath,
const std::string& arch,
1241 Logger::info(
"Installation: Creating Wine prefix at {} ({})", prefixPath, arch);
1246 fs::create_directories(prefixPath);
1248 catch (
const std::exception& e)
1250 result.
errorMessage =
"Failed to create prefix directory: " + std::string(e.what());
1255 std::ostringstream cmd;
1259 if (!wineVersion.empty())
1261 std::string winePath = getWinePath(wineVersion);
1262 if (!winePath.empty())
1268 cmd <<
"wineboot -u 2>&1";
1271 int exitCode = runCommand(cmd.str(), output);
1275 Logger::warn(
"Installation: wineboot returned non-zero: {}", exitCode);
1280 std::this_thread::sleep_for(std::chrono::seconds(2));
1285bool InstallationExecutor::runWinetricks(
const std::string& prefixPath,
1286 const std::string& ,
1287 const std::vector<std::string>& verbs,
1296 if (std::system(
"command -v winetricks >/dev/null 2>&1") != 0)
1298 result.
warnings.push_back(
"winetricks not installed, skipping component installation");
1302 std::ostringstream verbsStr;
1303 for (
size_t i = 0; i < verbs.size(); ++i)
1307 verbsStr << verbs[i];
1310 Logger::info(
"Installation: Running winetricks: {}", verbsStr.str());
1312 std::ostringstream cmd;
1314 cmd <<
"winetricks -q";
1315 for (
const auto& verb : verbs)
1322 int exitCode = runCommand(cmd.str(), output);
1326 Logger::warn(
"Installation: winetricks returned non-zero: {}", exitCode);
1327 result.
warnings.push_back(
"winetricks may have failed for some components");
1333bool InstallationExecutor::setWineRegistry(
const std::string& prefixPath,
1334 const std::string& ,
1335 const std::string& path,
const std::string& key,
1336 const std::string& value,
const std::string& type,
1339 Logger::info(
"Installation: Setting registry {}\\{} = {}", path, key, value);
1341 std::ostringstream cmd;
1349 int exitCode = runCommand(cmd.str(), output);
1351 return exitCode == 0;
1358bool InstallationExecutor::createDesktopShortcut(
const InstallResult& result)
1360 const char* home = std::getenv(
"HOME");
1363 Logger::warn(
"Installation: Cannot create desktop shortcut - HOME not set");
1367 std::string desktopDir = std::string(home) +
"/Desktop";
1370 if (!fs::exists(desktopDir))
1373 std::string xdgConfig = std::string(home) +
"/.config/user-dirs.dirs";
1374 if (fs::exists(xdgConfig))
1376 std::ifstream file(xdgConfig);
1378 while (std::getline(file, line))
1380 if (line.find(
"XDG_DESKTOP_DIR") != std::string::npos)
1382 size_t start = line.find(
'"');
1383 size_t end = line.rfind(
'"');
1384 if (start != std::string::npos && end > start)
1386 desktopDir = line.substr(start + 1, end - start - 1);
1388 size_t homePos = desktopDir.find(
"$HOME");
1389 if (homePos != std::string::npos)
1391 desktopDir.replace(homePos, 5, home);
1400 if (!fs::exists(desktopDir))
1402 Logger::warn(
"Installation: Desktop directory not found: {}", desktopDir);
1407 std::string safeName = result.
gameName;
1409 for (
char& c : safeName)
1411 if (!std::isalnum(
static_cast<unsigned char>(c)) && c !=
' ' && c !=
'-' && c !=
'_')
1417 std::string desktopFile = desktopDir +
"/" + safeName +
".desktop";
1419 std::ofstream file(desktopFile);
1420 if (!file.is_open())
1422 Logger::warn(
"Installation: Failed to create desktop file: {}", desktopFile);
1427 file <<
"[Desktop Entry]\n";
1428 file <<
"Type=Application\n";
1429 file <<
"Name=" << safeDisplayName <<
"\n";
1430 file <<
"Comment=Launch " << safeDisplayName <<
" via Game Launcher\n";
1435 if (result.
runner ==
"wine" || result.
runner ==
"proton")
1447 std::istringstream argStream(result.
arguments);
1449 while (argStream >> arg)
1454 const std::string safePath =
1456 file <<
"Path=" << safePath <<
"\n";
1457 file <<
"Terminal=false\n";
1458 file <<
"Categories=Game;\n";
1466 std::error_code permEc;
1467 fs::permissions(desktopFile,
1468 fs::perms::owner_exec | fs::perms::group_exec | fs::perms::others_exec,
1469 fs::perm_options::add, permEc);
1472 Logger::warn(
"Installation: chmod +x on {} failed: {} (file still created)", desktopFile,
1476 Logger::info(
"Installation: Created desktop shortcut: {}", desktopFile);
1480bool InstallationExecutor::createMenuEntry(
const InstallResult& result)
1482 const char* home = std::getenv(
"HOME");
1485 Logger::warn(
"Installation: Cannot create menu entry - HOME not set");
1490 std::string applicationsDir = std::string(home) +
"/.local/share/applications";
1493 if (!fs::exists(applicationsDir))
1497 fs::create_directories(applicationsDir);
1499 catch (
const std::exception& e)
1501 Logger::warn(
"Installation: Failed to create applications dir: {}", e.what());
1509 for (
char& c : safeName)
1511 if (!std::isalnum(
static_cast<unsigned char>(c)) && c !=
'-' && c !=
'_')
1517 std::transform(safeName.begin(), safeName.end(), safeName.begin(),
1518 [](
unsigned char c) { return static_cast<char>(std::tolower(c)); });
1520 std::string desktopFile = applicationsDir +
"/gamelauncher-" + safeName +
".desktop";
1522 std::ofstream file(desktopFile);
1523 if (!file.is_open())
1525 Logger::warn(
"Installation: Failed to create menu entry: {}", desktopFile);
1530 file <<
"[Desktop Entry]\n";
1531 file <<
"Type=Application\n";
1532 file <<
"Name=" << safeDisplayName <<
"\n";
1533 file <<
"Comment=Launch " << safeDisplayName <<
" via Game Launcher\n";
1537 if (result.
runner ==
"wine" || result.
runner ==
"proton")
1549 std::istringstream argStream(result.
arguments);
1551 while (argStream >> arg)
1556 const std::string safeWorkDir =
1558 file <<
"Path=" << safeWorkDir <<
"\n";
1559 file <<
"Terminal=false\n";
1560 file <<
"Categories=Game;\n";
1561 file <<
"Keywords=game;gaming;wine;\n";
1565 Logger::info(
"Installation: Created menu entry: {}", desktopFile);
1568 [[maybe_unused]]
int ret = std::system(
1579std::map<std::string, std::string>
1583 std::map<std::string, std::string> vars;
1586 vars[
"HOME"] = expandPath(
"~");
1587 const char* userEnv = getenv(
"USER");
1588 vars[
"USER"] = userEnv ? userEnv :
"user";
1593 ? expandPath(
"~/.cache/gamelauncher/downloads/" + script.
gameSlug)
1594 : expandPath(config.downloadCachePath);
1599 vars[
"WINEPREFIX"] = expandPath(config.winePrefixPath);
1600 vars[
"PREFIX"] = vars[
"WINEPREFIX"];
1604 vars[
"GAME_SLUG"] = script.
gameSlug;
1610 vars[key] = expandPath(value);
1617InstallationExecutor::substituteVariables(
const std::string& input,
1618 const std::map<std::string, std::string>& variables)
1620 std::string result = input;
1623 for (
const auto& [key, value] : variables)
1626 std::string pattern1 =
"$" + key;
1628 while ((pos = result.find(pattern1, pos)) != std::string::npos)
1631 size_t nextPos = pos + pattern1.length();
1632 if (nextPos >= result.length() ||
1633 !std::isalnum(
static_cast<unsigned char>(result[nextPos])))
1635 result.replace(pos, pattern1.length(), value);
1636 pos += value.length();
1645 std::string pattern2 =
"${" + key +
"}";
1647 while ((pos = result.find(pattern2, pos)) != std::string::npos)
1649 result.replace(pos, pattern2.length(), value);
1650 pos += value.length();
1655 if (!result.empty() && result[0] ==
'~')
1657 result = expandPath(result);
1663std::string InstallationExecutor::expandPath(
const std::string& path)
1670 std::string result = path;
1673 if (result[0] ==
'~')
1675 const char* home = getenv(
"HOME");
1678 result = std::string(home) + result.substr(1);
1690 const ProgressCallback& callback)
1693 std::lock_guard<std::mutex> lock(progressMutex_);
1694 currentProgress_ = progress;
1704 const std::string& message)
1706 auto now = std::chrono::system_clock::now();
1707 auto time = std::chrono::system_clock::to_time_t(now);
1709 localtime_r(&time, &tm);
1712 std::strftime(timestamp,
sizeof(timestamp),
"[%H:%M:%S]", &tm);
1714 std::string logLine = std::string(timestamp) +
" " + message;
1722 const std::string& warning)
1724 progress.
warnings.push_back(warning);
1725 result.
warnings.push_back(warning);
1729int InstallationExecutor::runCommand(
const std::string& command, std::string& output)
1731 FILE* pipe = popen(command.c_str(),
"r");
1734 output =
"Failed to execute command";
1738 std::array<char, 4096> buffer;
1741 constexpr size_t kMaxOutput = 16u * 1024 * 1024;
1742 while (fgets(buffer.data(),
static_cast<int>(buffer.size()), pipe) !=
nullptr)
1744 output += buffer.data();
1745 if (output.size() > kMaxOutput)
1749 int status = pclose(pipe);
1752 if (WIFEXITED(status))
1753 return WEXITSTATUS(status);
1754 if (WIFSIGNALED(status))
1755 return 128 + WTERMSIG(status);
1759std::string InstallationExecutor::getWinePath(
const std::string& version)
1762 std::vector<std::string> searchPaths = {
1764 "/usr/local/bin/wine",
1765 expandPath(
"~/.local/share/lutris/runners/wine/" + version +
"/bin/wine"),
1766 expandPath(
"~/.steam/steam/compatibilitytools.d/" + version +
"/proton"),
1769 for (
const auto& path : searchPaths)
1771 if (fs::exists(path))
static DownloadResult download(const DownloadRequest &request, const ProgressCallback &progressCallback=nullptr)
Download a file with optional progress tracking.
static void cancel()
Cancel the current download operation.
static bool verifyChecksum(const std::string &filePath, const std::string &expected, const std::string &type)
Verify file checksum.
std::function< void(const InstallProgress &)> ProgressCallback
static bool isInstalling()
Check if an installation is in progress.
static InstallState getCurrentState()
Get current installation state.
std::function< void(const InstallResult &)> CompletionCallback
static uint64_t getAvailableSpace(const std::string &path)
Get available disk space at path.
static uint64_t estimateRequiredSpace(const LutrisInstallerScript &script)
Calculate required disk space.
static std::vector< std::string > validate(const LutrisInstallerScript &script, const InstallConfig &config)
Validate that an installation can proceed.
static InstallProgress getCurrentProgress()
Get current progress.
static void cancel()
Cancel current installation.
static void installAsync(const LutrisInstallerScript &script, const InstallConfig &config, const CompletionCallback &onComplete, const ProgressCallback &onProgress=nullptr)
Execute an installation asynchronously.
static InstallResult install(const LutrisInstallerScript &script, const InstallConfig &config, const ProgressCallback &progressCallback=nullptr)
Execute an installation (blocking).
static void info(const std::string &message, Args &&... args)
Log info message (general information).
static void debug(const std::string &message, Args &&... args)
Log debug message (verbose development info).
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).
static std::string escapeArg(const std::string &arg)
Escape a string for safe use as a shell argument.
static bool isPathWithin(const std::string &candidate, const std::string &root)
static std::string safeCacheComponent(const std::string &raw)
static std::string sanitizeDesktopField(const std::string &s)
static std::string escapeDesktopExec(const std::string &s)
static bool endsWith(const std::string &str, const std::string &suffix)
static bool isPathWithinHome(const std::string &rawPath)
std::vector< std::shared_ptr< std::future< void > > > activeFutures_
void storeFuture(std::shared_ptr< std::future< void > > f)
std::string escapeExec(const std::string &s)
std::string expectedChecksum
bool skipHashVerification
std::string downloadCachePath
std::string winePrefixPath
std::map< std::string, std::string > customVariables
bool createDesktopShortcut
double downloadSpeedBytesPerSec
std::string currentOperation
std::vector< std::string > logMessages
std::string stateDescription
std::vector< std::string > warnings
std::string currentFileName
uint64_t totalBytesExtracted
std::string winePrefixPath
std::vector< std::string > warnings
std::map< std::string, std::string > dllOverrides
std::map< std::string, std::string > environmentVariables
uint64_t totalBytesDownloaded
std::string executablePath
std::vector< std::string > logMessages
std::map< std::string, std::string > params
std::map< std::string, std::string > environmentVariables
std::string installerSlug
LutrisWineConfig wineConfig
std::vector< LutrisInstallerFile > files
std::vector< LutrisInstallStep > steps
std::map< std::string, std::string > dllOverrides
std::map< std::string, std::string > environment