Skip to content

Instantly share code, notes, and snippets.

@Radon8472
Created September 13, 2020 08:00
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 Radon8472/fdf8f698accc0e2cb5953ec21b9fc7be to your computer and use it in GitHub Desktop.
Save Radon8472/fdf8f698accc0e2cb5953ec21b9fc7be to your computer and use it in GitHub Desktop.
Textconf diff for minecraft files using TheFrozenFire/PHP-NBT-Decoder-Encoder
<?php
/**
* test - gitdiff.cmd.php
*
* written : 2018-05-01 by Radon8472
* last modified: 2019-06-22 by Radon8472
*
* @requires: https://github.com/TheFrozenFire/PHP-NBT-Decoder-Encoder
*
* reads nbt or dat-file from commdline parameter or Standard-input, to use it in git-diff textconv
*
* @todo 2019-06-22: add parameter to switch between php and json-format (-php. -json) / or/and encoding for output
*/
$format = "json"; // json / php
$encoding = "utf8"; // utf8 / ascii
function utf8_encode_strings(&$data)
{
if(is_string($data))
{
$data = utf8_encode($data);
}
}
if(isset($argv[1]))
{
$inputFile = $argv[1];
echo "Read file '".$inputFile."'\n";
}
else
{
$inputFile = fopen("compress.zlib://php://stdin",'rb');
## INFO 2018-05-01: nbt-class dont accepts "php://"-Wrapper as String, maybe because "is_file" is used to check arguments
}
require(dirname(__FILE__)."/nbt.class.php");
$nbt = new nbt();
# $nbt->verbose = true;
#$nbt->loadFile("servers.dat");
if(is_resource($inputFile)) $nbt->loadFile($inputFile, null);
else $nbt->loadFile($inputFile);
$nbt->writeFile($tmp = tempnam(sys_get_temp_dir(), "nbt"));
if($format=="json" || $encoding=="utf8") array_walk_recursive($nbt->root, "utf8_encode_strings");
if($format!="json")
{
print_r($nbt->root);
}
else
{
$output = json_encode($nbt->root, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$output = str_replace("},", "},".PHP_EOL, $output);
$errorCode = json_last_error();
if($output !== false && $errorCode == JSON_ERROR_NONE)
{
## doent work correctly, cause UTF-8 signs are changed in the json_encode()
#if($encoding!="utf8") $output = utf8_decode();
echo $output;
}
else
{
$error = json_last_error().PHP_EOL;
if(function_exists("json_last_error_msg")) echo "JSON-Error: " .json_last_error_msg().PHP_EOL;
else echo "JSON-Error: #".$errorCode.PHP_EOL;
exit ($errorCode);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment