langpcbuild.php 6.81 KB
Newer Older
Derrick Sobodash's avatar
Derrick Sobodash committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
#!/usr/bin/php -q
<?php
//
// langpcbuild v2.0 for PHP console mode
// (c) 2002, 2003 Derrick Sobodash
//
// This builds a Langrisser (I or II) PC compatable resource file from a
// folder of files. The format has a limit of 16 character filenames
// (including the extension). I didn't bother checking for this because
// the only folder you should be using it on is one generated by
// langpcdump.php
//
// The resource file build will have the same name as the folder it is
// built from.
//
// Updates to 2.0
//   Supports up to 0xFFFFFFFF files per archive (the previous 0xFF file
//   limit caused errors when building Langrisser II scripts).
//   Support for European character (that are available in the LandPC
//   font) added to the character map. Thanks to MadMalkav for mapping
//   the extended character set.
//
// Updates to 1.5
//   Accepts symbols in the input files to process for the game. Accepted
//   symbols are:
//           <> - End text window          <clsr> - Clear text window
//       <str1> - String variable          <str2> - String variable
//   It also passes the script through an English equivalency table when
//   building new output.
//

echo ("\nlangpcbuild v2.0 (c) 2003 Derrick Sobodash\n");
set_time_limit(6000000);

if ($argc < 2) { DisplayOptions(); die; }
else { $path = $argv[1]; }

// Simple routine to read in a directory listing and split it to an array
$mydir="";
$orgfile = "$path";
if ($handle = opendir($orgfile)) {
	while (false !== ($file = readdir($handle))) { 
		$mydir .= $orgfile . "/$file\n";
	}
	closedir($handle);
}
$filelist = split("\n", $mydir);
$i=0;
// Allocate space for the header. Each file entry takes 24 bytes and there's
// a 4 byte file count. We subtract 3 from the count to account for . .. and
// the blank index.
$offset = ((count($filelist) - 3) * 24) + 4;
$bin="";

// .. and . are at the start of the array. Pad in two bytes to skip them
for ($z=2; $z < (count($filelist)-1); $z++) {
	// Parse the file name out of the full path
	$temp = split("/", $filelist[$z]);
	$file[$i] = $temp[count($temp)-1];
	print "Reading $file[$i]... ";
	$fd = fopen ($filelist[$z], "rb");
	$table = TableArray();
	$thisfile = "";
	for ($g=0; $g < filesize($filelist[$z]); $g++) {
		$charchar = fread ($fd, 1);
		if($table[hexdec(bin2hex($charchar))] != "") {
			$rahcrahc = $charchar;
			if ($charchar == "<") {
				$test = fread ($fd, 1);
				if ($test == ">")
					$thisfile .= $charchar . $test;
				else {
					fseek ($fd, -1, SEEK_CUR);
					$test = fread ($fd, 5);
					if (($test == "clsr>")||($test == "str1>")||($test == "str2>"))
						$thisfile .= $charchar . $test;
				}
			}
			else
				$thisfile .= $table[hexdec(bin2hex($charchar))];
		}
		else if (($charchar == chr(13))||($charchar == chr(10)))
			$thisfile .= $charchar;
	}
	$temp2 = $thisfile;
	$temp2 = str_replace("<>" . chr(13) . chr(10) . chr(13) . chr(10), chr(56), $temp2);
	$temp2 = str_replace("<str1>", chr(29) . chr(75), $temp2);
	$temp2 = str_replace("<str2>", chr(29) . chr(107), $temp2);
	$temp2 = str_replace("<clsr>" . chr(13) . chr(10), chr(49), $temp2);
	$temp2 = str_replace(chr(13) . chr(10), chr(24) . chr(50), $temp2);
	$bin .= $temp2;
	//$bin .= fread ($fd, filesize($filelist[$z]));
	$size[$i] = strlen($temp2); //filesize($filelist[$z]); //strlen($bin);
	$off[$i] = $offset;
	// Increment the offset by filesize to keep a running total of where our
	// pointer in the binary is.
	$offset = $offset + strlen($temp2);
	$i++;
	fclose($fd);
	print "done!\n";
}
unset ($z, $i, $temp);

// Write our file count using little endian addressing to start our header
$header = pack("V*", count($file));

for ($z=0; $z < (count($file)); $z++) {
	// The file format only supports a max of 16 character file names
	$header .= str_pad($file[$z], 16, chr(0)) . pack("V*", $off[$z]) . pack("V*", $size[$z]);
}
print "done!\n";

print "Writing new resource file... ";
$fo = fopen("$path-a.res", "w");
fputs($fo, $header . $bin  . "                Generated by langpcbuild v2.0                ");
fclose($fo);
print "done!\n";

echo ("All done!...\n\n");

function DisplayOptions() {
	echo ("Builds a Langrisser (I or II) PC resource from a folder\n  usage: langpcbuild [input path]\n\n");
}

function TableArray() {
	return(array(
		"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
		"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
		//           !        "        #        $        &        '                 (        )        *        +        ,        -        .        /
		chr(24), chr(25), chr(26), chr(27), chr(28), chr(29), chr(30), chr(31), chr(16), chr(17), chr(18), chr(19), chr(20), chr(21), chr(22), chr(23),
		//  0       1       2        3        4        5        6        7        8       9       :       ;       <       =       >       ?
		chr(8), chr(9), chr(10), chr(11), chr(12), chr(13), chr(14), chr(15), chr(0), chr(1), chr(2), chr(3), chr(4), chr(5), chr(6), chr(7),
		//  @         A         B         C         D         E         F         G         H        I         J         K         L         M         N         O
		chr(120), chr(121), chr(122), chr(123), chr(124), chr(125), chr(126), chr(127), chr(112),chr(113), chr(114), chr(115), chr(116), chr(117), chr(118), chr(119),
		//  P         Q         R         S         T         U         V         W         X        Y        Z        [        \         ]         ^         _
		chr(104), chr(105), chr(106), chr(107), chr(108), chr(109), chr(110), chr(111), chr(96), chr(97), chr(98), chr(99), chr(100), chr(101), chr(102), chr(103),
		//  `        a        b        c        d        e        f        g        h        i        j        k        l        m        n        o
		chr(88), chr(89), chr(90), chr(91), chr(92), chr(93), chr(94), chr(95), chr(80), chr(81), chr(82), chr(83), chr(84), chr(85), chr(86), chr(87),
		//  p        q        r        s        t        u        v        w        x        y        z        {        |        }        ~         
		chr(72), chr(73), chr(74), chr(75), chr(76), chr(77), chr(78), chr(79), chr(64), chr(65), chr(66), chr(67), chr(68), chr(69), chr(70), chr(71),
		"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
		"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "",
		"", chr(0x99), "", "", "", "", "", "", "", "", "", "", "", "", "", "",
		"", "", "", "", "", "", "", "", "", "", "", "", "", "", "", chr(0x87),
		"", chr(0xf9), "", "", "", "", "", "", "", chr(0xf1), "", "", "", chr(0xf5), "", "",
		"", chr(0xe9), "", chr(0xeb), "", "", "", "", "", "", chr(0xe2), "", "", "", "", "",
		"", chr(0xd9), "", "", chr(0xdc), "", "", "", "", chr(0xd1), "", "", "", chr(0xd5), "", "",
		"", chr(0xc9), "", chr(0xcb), "", "", chr(0xce), "", "", "", chr(0xc2), "", chr(0xc4), "", "", "" ));
}

?>