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 (moved User:Quietust/rawextract 31.php to User:Quietust/rawextract.php: one script for both versions)
(now handles all versions from 0.31.01 thru 0.34.08)
Line 1: Line 1:
 
  <nowiki><?
 
  <nowiki><?
function decompress ($sig, $file)
+
$versions = array(
 +
1287 => '0.31.01',
 +
1288 => '0.31.02',
 +
1289 => '0.31.03',
 +
1292 => '0.31.04',
 +
1295 => '0.31.05',
 +
1297 => '0.31.06',
 +
1300 => '0.31.08',
 +
1304 => '0.31.09',
 +
1305 => '0.31.10',
 +
1310 => '0.31.11',
 +
1311 => '0.31.12',
 +
1323 => '0.31.13',
 +
1325 => '0.31.14',
 +
1326 => '0.31.15',
 +
1327 => '0.31.16',
 +
1340 => '0.31.17',
 +
1341 => '0.31.18',
 +
1351 => '0.31.19',
 +
1353 => '0.31.20',
 +
1354 => '0.31.21',
 +
1359 => '0.31.22',
 +
1360 => '0.31.23',
 +
1361 => '0.31.24',
 +
1362 => '0.31.25',
 +
 
 +
1372 => '0.34.01',
 +
1374 => '0.34.02',
 +
1376 => '0.34.03',
 +
1377 => '0.34.04',
 +
1378 => '0.34.05',
 +
1382 => '0.34.06',
 +
1383 => '0.34.07',
 +
1400 => '0.34.08',
 +
);
 +
 
 +
function decompress ($version, $file)
 
{
 
{
 
$out = tmpfile();
 
$out = tmpfile();
 
// write 'uncompressed' header
 
// write 'uncompressed' header
fwrite($out, pack('VV', $sig, 0));
+
fwrite($out, pack('VV', $version, 0));
 
while (1)
 
while (1)
 
{
 
{
Line 15: Line 51:
 
}
 
}
 
rewind($out);
 
rewind($out);
 +
// seek past the headers
 +
fseek($out, 8, SEEK_CUR);
 
fclose($file);
 
fclose($file);
 
return $out;
 
return $out;
Line 29: Line 67:
 
$x = fread($file, 2);
 
$x = fread($file, 2);
 
$pck = @unpack('vdata', $x);
 
$pck = @unpack('vdata', $x);
 +
return $pck['data'];
 +
}
 +
function readByte ($file)
 +
{
 +
$x = fread($file, 1);
 +
$pck = @unpack('Cdata', $x);
 
return $pck['data'];
 
return $pck['data'];
 
}
 
}
Line 39: Line 83:
 
if ($raw)
 
if ($raw)
 
{
 
{
$sig = readLong($raw);
+
$version = readLong($raw);
 +
if (isset($versions[$version]))
 +
echo "World is from version $versions[$version]\n";
 +
else echo "World is from an unrecognized version!\n";
 
$comp = readLong($raw);
 
$comp = readLong($raw);
 
if ($comp == 1)
 
if ($comp == 1)
$raw = decompress($sig, $raw);
+
$raw = decompress($version, $raw);
 
elseif ($comp > 0)
 
elseif ($comp > 0)
 
die("world.dat file is corrupted!");
 
die("world.dat file is corrupted!");
  
fseek($raw, 0x92, SEEK_SET);
+
fseek($raw, 0x46, SEEK_CUR);
 +
if ($version >= 1372)
 +
fseek($raw, 0x14, SEEK_CUR);
 +
if ($version >= 1400)
 +
fseek($raw, 0x4, SEEK_CUR);
 +
if (readByte($raw))
 +
{
 +
fseek($raw, readShort($raw), SEEK_CUR);
 +
fseek($raw, readShort($raw), SEEK_CUR);
 +
fseek($raw, 0x30, SEEK_CUR);
 +
}
 +
fseek($raw, 0xF, SEEK_CUR);
  
 
$len = readShort($raw);
 
$len = readShort($raw);
 
$world = fread($raw, $len);
 
$world = fread($raw, $len);
echo "Extracting raws for randomly generated creatures of $world:\n\n";
+
echo "Extracting randomly generated raws of $world:\n\n";
 
}
 
}
 
else
 
else
Line 58: Line 116:
 
die("Could not open world.dat or world.sav!");
 
die("Could not open world.dat or world.sav!");
  
$sig = readLong($raw);
+
$version = readLong($raw);
 +
if (isset($versions[$version]))
 +
echo "World is from version $versions[$version]\n";
 +
else echo "World is from an unrecognized version!\n";
 
$comp = readLong($raw);
 
$comp = readLong($raw);
 
if ($comp == 1)
 
if ($comp == 1)
$raw = decompress($sig, $raw);
+
$raw = decompress($version, $raw);
 
elseif ($comp > 0)
 
elseif ($comp > 0)
 
die("world.sav file is corrupted!");
 
die("world.sav file is corrupted!");
  
fseek($raw, 0x5E, SEEK_SET);
+
fseek($raw, 0x56, SEEK_CUR);
 +
if ($version >= 1372)
 +
fseek($raw, 0x14, SEEK_CUR);
 +
if ($version >= 1400)
 +
fseek($raw, 0x4, SEEK_CUR);
  
 
$len = readShort($raw);
 
$len = readShort($raw);
Line 72: Line 137:
 
$len = readShort($raw);
 
$len = readShort($raw);
 
$world = fread($raw, $len);
 
$world = fread($raw, $len);
echo "Extracting raws for randomly generated creatures of $world:\n\n";
+
echo "Extracting randomly generated raws of $world:\n\n";
$skip = readLong($raw);
+
readLong($raw); // year
 +
}
 +
 
 +
if ($version >= 1372)
 +
{
 +
$numMaterials = readLong($raw);
 +
 
 +
for ($i = 0; $i < $numMaterials; $i++)
 +
{
 +
$data = '';
 +
$file = "material_$i";
 +
$numLines = readLong($raw);
 +
for ($j = 0; $j < $numLines; $j++)
 +
{
 +
$len = readShort($raw);
 +
$str = fread($raw, $len);
 +
$data .= "$str\r\n";
 +
if (preg_match('/\[INORGANIC:(.*?)\]/', $str, $x))
 +
$file = $x[1];
 +
}
 +
echo "$file...\n";
 +
file_put_contents($file .'.txt', $data);
 +
}
 +
}
 +
 
 +
if ($version >= 1400)
 +
{
 +
$numItems = readLong($raw);
 +
 
 +
for ($i = 0; $i < $numItems; $i++)
 +
{
 +
$data = '';
 +
$file = "item_$i";
 +
$numLines = readLong($raw);
 +
for ($j = 0; $j < $numLines; $j++)
 +
{
 +
$len = readShort($raw);
 +
$str = fread($raw, $len);
 +
$data .= "$str\r\n";
 +
if (preg_match('/\[ITEM_.*?:(.*?)\]/', $str, $x))
 +
$file = $x[1];
 +
}
 +
echo "$file...\n";
 +
file_put_contents($file .'.txt', $data);
 +
}
 
}
 
}
  
Line 94: Line 203:
 
file_put_contents($file .'.txt', $data);
 
file_put_contents($file .'.txt', $data);
 
}
 
}
 +
 +
if ($version >= 1372)
 +
{
 +
$numInteractions = readLong($raw);
 +
 +
for ($i = 0; $i < $numInteractions; $i++)
 +
{
 +
$data = '';
 +
$file = "interaction_$i";
 +
$numLines = readLong($raw);
 +
for ($j = 0; $j < $numLines; $j++)
 +
{
 +
$len = readShort($raw);
 +
$str = fread($raw, $len);
 +
$data .= "$str\r\n";
 +
if (preg_match('/\[INTERACTION:(.*?)\]/', $str, $x))
 +
$file = $x[1];
 +
}
 +
echo "$file...\n";
 +
file_put_contents($file .'.txt', $data);
 +
}
 +
}
 +
 
echo "done!\n";
 
echo "done!\n";
 
?></nowiki>
 
?></nowiki>

Revision as of 18:59, 14 May 2012

<?
$versions = array(
	1287 => '0.31.01',
	1288 => '0.31.02',
	1289 => '0.31.03',
	1292 => '0.31.04',
	1295 => '0.31.05',
	1297 => '0.31.06',
	1300 => '0.31.08',
	1304 => '0.31.09',
	1305 => '0.31.10',
	1310 => '0.31.11',
	1311 => '0.31.12',
	1323 => '0.31.13',
	1325 => '0.31.14',
	1326 => '0.31.15',
	1327 => '0.31.16',
	1340 => '0.31.17',
	1341 => '0.31.18',
	1351 => '0.31.19',
	1353 => '0.31.20',
	1354 => '0.31.21',
	1359 => '0.31.22',
	1360 => '0.31.23',
	1361 => '0.31.24',
	1362 => '0.31.25',

	1372 => '0.34.01',
	1374 => '0.34.02',
	1376 => '0.34.03',
	1377 => '0.34.04',
	1378 => '0.34.05',
	1382 => '0.34.06',
	1383 => '0.34.07',
	1400 => '0.34.08',
);

function decompress ($version, $file)
{
	$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);
	// seek past the headers
	fseek($out, 8, SEEK_CUR);
	fclose($file);
	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'];
}
function readByte ($file)
{
	$x = fread($file, 1);
	$pck = @unpack('Cdata', $x);
	return $pck['data'];
}

$dir = '';
if ($argc > 1)
	$dir = $argv[1] .'\\';

$raw = @fopen($dir.'world.dat', 'rb');
if ($raw)
{
	$version = readLong($raw);
	if (isset($versions[$version]))
		echo "World is from version $versions[$version]\n";
	else	echo "World is from an unrecognized version!\n";
	$comp = readLong($raw);
	if ($comp == 1)
		$raw = decompress($version, $raw);
	elseif ($comp > 0)
		die("world.dat file is corrupted!");

	fseek($raw, 0x46, SEEK_CUR);
	if ($version >= 1372)
		fseek($raw, 0x14, SEEK_CUR);
	if ($version >= 1400)
		fseek($raw, 0x4, SEEK_CUR);
	if (readByte($raw))
	{
		fseek($raw, readShort($raw), SEEK_CUR);
		fseek($raw, readShort($raw), SEEK_CUR);
		fseek($raw, 0x30, SEEK_CUR);
	}
	fseek($raw, 0xF, SEEK_CUR);

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

	$version = readLong($raw);
	if (isset($versions[$version]))
		echo "World is from version $versions[$version]\n";
	else	echo "World is from an unrecognized version!\n";
	$comp = readLong($raw);
	if ($comp == 1)
		$raw = decompress($version, $raw);
	elseif ($comp > 0)
		die("world.sav file is corrupted!");

	fseek($raw, 0x56, SEEK_CUR);
	if ($version >= 1372)
		fseek($raw, 0x14, SEEK_CUR);
	if ($version >= 1400)
		fseek($raw, 0x4, SEEK_CUR);

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

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

if ($version >= 1372)
{
	$numMaterials = readLong($raw);

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

if ($version >= 1400)
{
	$numItems = readLong($raw);

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

$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 "$file...\n";
	file_put_contents($file .'.txt', $data);
}

if ($version >= 1372)
{
	$numInteractions = readLong($raw);

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

echo "done!\n";
?>