v50 Steam/Premium information for editors
  • v50 information can now be added to pages in the main namespace. v0.47 information can still be found in the DF2014 namespace. See here for more details on the new versioning policy.
  • Use this page to report any issues related to the migration.
This notice may be cached—the current version can be found here.

Difference between revisions of "User:Quietust/rawextract.php"

From Dwarf Fortress Wiki
Jump to navigation Jump to search
m
(improvement: output them to separate files)
Line 1: Line 1:
 
  <nowiki><?
 
  <nowiki><?
 +
$version = 0x050C; // seems to vary depending on the version used to generate the world
 +
 
function decompress ($file)
 
function decompress ($file)
 
{
 
{
 +
global $version;
 
$out = tmpfile();
 
$out = tmpfile();
 
// write 'uncompressed' header
 
// write 'uncompressed' header
fwrite($out, pack('VV', 0x0509, 0));
+
fwrite($out, pack('VV', $version, 0));
 
while (1)
 
while (1)
 
{
 
{
Line 35: Line 38:
 
{
 
{
 
$sig = readLong($raw);
 
$sig = readLong($raw);
if ($sig != 0x0509)
+
if ($sig != $version)
 
die("world.dat file header not found!");
 
die("world.dat file header not found!");
  
Line 48: Line 51:
 
$len = readShort($raw);
 
$len = readShort($raw);
 
$world = fread($raw, $len);
 
$world = fread($raw, $len);
echo "Listing raws for randomly generated creatures of $world:\n\n";
+
echo "Extracting raws for randomly generated creatures of $world:\n\n";
 
}
 
}
 
else
 
else
Line 57: Line 60:
  
 
$sig = readLong($raw);
 
$sig = readLong($raw);
if ($sig != 0x0509)
+
if ($sig != $version)
 
die("world.sav file header not found!");
 
die("world.sav file header not found!");
  
Line 73: Line 76:
 
$len = readShort($raw);
 
$len = readShort($raw);
 
$world = fread($raw, $len);
 
$world = fread($raw, $len);
echo "Listing raws for randomly generated creatures of $world:\n\n";
+
echo "Extracting raws for randomly generated creatures of $world:\n\n";
 
$skip = readLong($raw);
 
$skip = readLong($raw);
 
}
 
}
Line 81: Line 84:
 
for ($i = 0; $i < $numCreatures; $i++)
 
for ($i = 0; $i < $numCreatures; $i++)
 
{
 
{
 +
$data = '';
 +
$file = "creature_$i";
 
$numLines = readLong($raw);
 
$numLines = readLong($raw);
 
for ($j = 0; $j < $numLines; $j++)
 
for ($j = 0; $j < $numLines; $j++)
Line 86: Line 91:
 
$len = readShort($raw);
 
$len = readShort($raw);
 
$str = fread($raw, $len);
 
$str = fread($raw, $len);
echo "$str\n";
+
$data .= "$str\r\n";
 +
if (preg_match('/\[CREATURE:(.*?)\]/', $str, $x))
 +
$file = $x[1];
 
}
 
}
echo "----------\n";
+
echo "Extracting $file...\n";
 +
file_put_contents($file .'.txt', $data);
 
}
 
}
 +
echo "done!\n";
 
?></nowiki>
 
?></nowiki>

Revision as of 21:40, 19 May 2010

<?
$version = 0x050C;	// seems to vary depending on the version used to generate the world

function decompress ($file)
{
	global $version;
	$out = tmpfile();
	// write 'uncompressed' header
	fwrite($out, pack('VV', $version, 0));
	while (1)
	{
		$len = readLong($file);
		if (feof($file))
			break;
		$skip = readShort($file);
		$block = fread($file, $len - 2);
		fwrite($out, gzinflate($block));
	}
	rewind($out);
	return $out;
}

function readLong ($file)
{
	$x = fread($file, 4);
	$pck = @unpack('Vdata', $x);
	return $pck['data'];
}
function readShort ($file)
{
	$x = fread($file, 2);
	$pck = @unpack('vdata', $x);
	return $pck['data'];
}

$raw = @fopen('world.dat', 'rb');
if ($raw)
{
	$sig = readLong($raw);
	if ($sig != $version)
		die("world.dat file header not found!");

	$comp = readLong($raw);
	if ($comp == 1)
		$raw = decompress($raw);
	elseif ($comp > 0)
		die("world.dat file is corrupted!");

	fseek($raw, 0x92, SEEK_SET);

	$len = readShort($raw);
	$world = fread($raw, $len);
	echo "Extracting raws for randomly generated creatures of $world:\n\n";
}
else
{
	$raw = @fopen('world.sav', 'rb');
	if (!$raw)
		die("Could not open world.dat or world.sav!");

	$sig = readLong($raw);
	if ($sig != $version)
		die("world.sav file header not found!");

	$comp = readLong($raw);
	if ($comp == 1)
		$raw = decompress($raw);
	elseif ($comp > 0)
		die("world.sav file is corrupted!");

	fseek($raw, 0x5E, SEEK_SET);

	$len = readShort($raw);
	$name = fread($raw, $len);

	$len = readShort($raw);
	$world = fread($raw, $len);
	echo "Extracting raws for randomly generated creatures of $world:\n\n";
	$skip = readLong($raw);
}

$numCreatures = readLong($raw);

for ($i = 0; $i < $numCreatures; $i++)
{
	$data = '';
	$file = "creature_$i";
	$numLines = readLong($raw);
	for ($j = 0; $j < $numLines; $j++)
	{
		$len = readShort($raw);
		$str = fread($raw, $len);
		$data .= "$str\r\n";
		if (preg_match('/\[CREATURE:(.*?)\]/', $str, $x))
			$file = $x[1];
	}
	echo "Extracting $file...\n";
	file_put_contents($file .'.txt', $data);
}
echo "done!\n";
?>