Skip to content

Instantly share code, notes, and snippets.

@kasuganosoras
Created May 8, 2023 02:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kasuganosoras/027dc530647a2e681eac4c296b33c517 to your computer and use it in GitHub Desktop.
Save kasuganosoras/027dc530647a2e681eac4c296b33c517 to your computer and use it in GitHub Desktop.
FiveM Linux server update script
<?php
define('ROOT', __DIR__ . '/');
define('API_URL', 'https://runtime.fivem.net/artifacts/fivem/build_proot_linux/master/');
function GetBuilds() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, API_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$html = curl_exec($ch);
curl_close($ch);
$dom = new DOMDocument();
if (@$dom->loadHTML($html) === false) {
return [
'success' => false,
'message' => "Failed to parse html"
];
}
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//a[@href]");
$builds = [];
// print all href
foreach ($nodes as $node) {
$href = $node->getAttribute('href');
$class = $node->getAttribute('class');
if (strpos($href, 'fx.tar.xz') !== false) {
// if class contains 'is-primary' then it's recommended build
// if class contains 'is-danger' then it's optional build
if (strpos($class, 'is-primary') !== false) {
$type = 'recommended';
} else if (strpos($class, 'is-danger') !== false) {
$type = 'optional';
} else {
$type = 'normal';
}
$exp = explode('/', $href);
$dir = $exp[1];
$url = sprintf('%s%s/fx.tar.xz', API_URL, $dir);
$exp2 = explode('-', $dir);
$version = $exp2[0];
$hash = $exp2[1];
$builds[$version] = [
'url' => $url,
'hash' => $hash,
'type' => $type,
];
}
}
// sort by version
uksort($builds, function ($a, $b) {
return version_compare($a, $b);
});
return [
'success' => true,
'builds' => $builds,
];
}
function PrintLog() {
$args = func_get_args();
if (count($args) == 1) {
$msg = $args[0];
} else {
$msg = call_user_func_array('sprintf', $args);
}
echo sprintf("[%s] %s\n", date('Y-m-d H:i:s'), $msg);
}
function DownloadFile($url, $save) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$fp = fopen($save, 'w+');
curl_setopt($ch, CURLOPT_FILE, $fp);
// with progress
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, function ($resource, $download_size, $downloaded, $upload_size, $uploaded) {
if ($download_size > 0) {
$percent = $downloaded / $download_size * 100;
echo sprintf("\r[%s] Downloading... %.2f%%", date('Y-m-d H:i:s'), $percent);
}
});
curl_exec($ch);
curl_close($ch);
fclose($fp);
echo "\n";
}
PrintLog("FiveM Update Script by Akkariin");
PrintLog("Getting builds...");
$result = GetBuilds();
if ($result['success']) {
$builds = $result['builds'];
// get latest version
$latestVersion = Intval(array_keys($builds)[count($builds) - 1]);
$latestBuild = $builds[$latestVersion];
$localVersion = file_exists('.localartifact') ? Intval(trim(file_get_contents('.localartifact'))) : '0';
PrintLog("Local version: $localVersion");
PrintLog("Latest version: $latestVersion");
PrintLog("Latest build: $latestBuild[hash]");
if ($localVersion == $latestVersion) {
PrintLog("Already up to date");
exit;
}
PrintLog("Find new version {$latestVersion}, download url: $latestBuild[url]");
DownloadFile($latestBuild['url'], 'fx.tar.xz');
PrintLog("Downloaded, extracting...");
shell_exec('tar -xf fx.tar.xz');
PrintLog("Extracted, cleaning up...");
unlink('fx.tar.xz');
PrintLog("Updating local version...");
file_put_contents('.localartifact', $latestVersion);
PrintLog("Updated local version to $latestVersion");
PrintLog("Done!");
} else {
PrintLog("Failed to get builds");
PrintLog($result['message']);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment