Skip to content

Instantly share code, notes, and snippets.

/map2json.php Secret

Created October 26, 2016 14:55
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 anonymous/57bf2487fd60121084d3b9f96b8bf2ca to your computer and use it in GitHub Desktop.
Save anonymous/57bf2487fd60121084d3b9f96b8bf2ca to your computer and use it in GitHub Desktop.
<?php
echo "Generate map definitions and map reference arrays...<br>";
ini_set('xdebug.var_display_max_depth',-1);
ini_set('xdebug.var_display_max_data',-1);
ini_set('xdebug.var_display_max_children',-1);
$file = 'tgstation.2.1.3.dmm';
$handle = fopen($file,'r');
$readin = '';
if ($handle) {
while (($line = fgets($handle)) !== false) {
$readin.= $line;
}
fclose($handle);
} else {
echo "Failed to read $file";
}
$counter = 0;
$readin = explode('(1,1,1) = {',$readin);
$defs = $readin[0];
$map = $readin[1];
$defs = explode(")\r\n",$defs);
array_pop($defs);
$newdefs = [];
foreach ($defs as &$line){
$line = rtrim($line);
$line = explode('" = (/', $line);
$line = str_replace('","','---',$line);
$line = str_replace('("', '(', $line);
$line = str_replace('")', ')', $line);
$line = preg_replace("/([^\"]?)(\d{1,3})([;])([\d]{1,3})([^\"]?)/", "$1$2--$4--", $line);
$line[0] = str_replace('"', '', $line[0]);
//$line[1] = str_replace(')', '', $line[1]);
$line[1] = explode(',',$line[1]);
$line[1][0] = "/".$line[1][0];
foreach ($line[1] as &$obj){
if (FALSE !== strpos($obj,'{')){ //Object has parameters
$obj = explode('{',$obj);
$obj = str_replace('}', '', $obj); //Remove trailing }
$obj = str_replace("'", '', $obj);
$obj = str_replace('"', '', $obj);
$obj['object'] = $obj[0]; unset($obj[0]);
$obj['parameters'] = $obj[1]; unset($obj[1]);
$obj['parameters'] = explode(';',$obj['parameters']);
foreach ($obj['parameters'] as $k => &$param) {
$param = explode('=',$param);
$param['key'] = trim($param[0]); unset($param[0]);
if(isset($param[1])){
$param['value'] = trim($param[1]); unset($param[1]);
}
}
}
}
//Rebuild the definitions and clean em up
$count = count($line[1]);
$line['area'] = $line[1][$count-1]; unset($line[1][$count-1]);
$line['turf'] = $line[1][$count-2]; unset($line[1][$count-2]);
$line['def'] = $line[0]; unset($line[0]);
$newdefs[$line['def']] = array(
'turf'=>$line['turf'],
'area'=>$line['area'],
'contents'=>$line[1]
);
}
//The definitions have been parsed! Time to stick em to the map!
$map = str_replace('"', '', $map);
$map = str_replace('}', '', $map);
$map = trim($map);
$map = explode("\r\n",$map);
// unset($map[0]);
// array_pop($map);
// array_pop($map);
foreach ($map as &$line) {
$line = str_split($line,3);
$line = array_reverse($line);
}
$map = array_reverse($map);
echo "Writing map...<br>";
echo "Writing definitions...<br>";
$mapfile = 'tiles.json';
$deffile = 'defs.json';
if (!$maphandle = fopen($mapfile, 'w+')) {
echo "Can't open the map file!";
exit;
}
if (fwrite($maphandle, "var map = ".json_encode($map)) === FALSE) {
echo "Can't write to the map file!";
exit;
}
echo "<a href='map.json'>Map</a><br>";
if (!$defhandle = fopen($deffile, 'w+')) {
echo "Unable to write defines file!";
exit;
}
if (fwrite($defhandle, "var defs = ".json_encode($newdefs)) === FALSE) {
echo "Can't write to the defines file!";
exit;
}
echo "<a href='defs.json'>Defines</a><br>";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment