Skip to content

Instantly share code, notes, and snippets.

@froxxxy
Created February 23, 2022 10:40
Show Gist options
  • Save froxxxy/1afa4d987edc288d2c21a188ca541a0c to your computer and use it in GitHub Desktop.
Save froxxxy/1afa4d987edc288d2c21a188ca541a0c to your computer and use it in GitHub Desktop.
Super simple and quite ugly sync tool foor bitburner.
<?php
/*
Super simple and quite ugly sync tool foor bitburner.
- At start it uploads all file in current directory to game
- after that it compares MD5 of files and sleeps for a second
Usage:
- Update YOUR_AUTHENTICATOIN_TOKEN_HERE down below
- run "php up.php"
*/
die("This will overwrite any files in your game. If you know, what you're doing, then delete this line!");
function uploadToBitburner($filename)
{
// Sanitize filename for ingame (thx to https://www.npmjs.com/package/bitburner-sync)
$ingameFilename = str_replace(getcwd(), "", $filename);
$ingameFilename = strchr($ingameFilename, "/") !== false ? "/" . $ingameFilename : $ingameFilename;
// Upload file
$fields = [
'filename' => $ingameFilename,
'code' => base64_encode(file_get_contents($filename)),
];
$postdata = json_encode($fields);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://localhost:9990/');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Authorization: Bearer YOUR_AUTHENTICATOIN_TOKEN_HERE",
"Content-Type: application/json",
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
// No real error handling. just happy wehen response is "written" :)
echo ($result == "written" ? $ingameFilename . " uploaded" : $result) . PHP_EOL;
}
// scan current directory (cwd) and ignore files that dont end with script|ns|js|txt
$directory = new RecursiveDirectoryIterator(getcwd());
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, '/^.+\.(script|ns|js|txt)$/i', RecursiveRegexIterator::GET_MATCH);
// array to store md5 hashes
$files = array();
// file by file test if md5 has changed -> upload
while (true) {
foreach ($regex as $key => $value) {
$absoluteFilename = $value[0];
if (
!isset($files[$absoluteFilename]) ||
($files[$absoluteFilename] !== md5(file_get_contents($absoluteFilename)))
) {
$files[$absoluteFilename] = md5(file_get_contents($absoluteFilename));
uploadToBitburner($absoluteFilename, getcwd());
}
}
sleep(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment