Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * define globals
- */
- $ql_pk3_dir = "C:/Users/USERNAME/AppData/LocalLow/id Software/quakelive/baseq3/"; //path to quakelive baseq3 folder
- $target_dir = "D:/Games/Q3A/baseq3/"; //output folder for converted map pk3's
- $data_dir = "unzipped/"; //temporary folder for unzipped pk3 contents
- $temp_dir = "temp/"; //temporary folder for altered pk3 and converted soundfiles
- $quakelive_dec = "quakelivedec.exe"; //path to quakelivedec.exe
- $vlc = "C:/Program Files (x86)/VideoLAN/VLC/vlc.exe"; //vlc
- /*
- * create folders
- */
- if(!is_dir($target_dir)) mkdir($target_dir);
- if(!is_dir($data_dir)) mkdir($data_dir);
- if(!is_dir($temp_dir)) mkdir($temp_dir);
- /*
- * convert pk3's
- */
- foreach(glob($ql_pk3_dir."*.pk3") as $pk3){
- $pk3_basename = pathinfo($pk3, PATHINFO_BASENAME);
- echo "converting: ".$pk3_basename;
- if(!file_exists($temp_dir.$pk3_basename)){
- exec("\"".$quakelive_dec."\" \"".$pk3."\" \"".$temp_dir.$pk3_basename."\"");
- }
- echo " ... done\r\n";
- }
- /*
- * unpack pk3's
- */
- foreach(glob($temp_dir."*.pk3") as $pk3){
- $pk3_basename = pathinfo($pk3, PATHINFO_BASENAME);
- echo "unpacking: ".$pk3_basename;
- $zip = new ZipArchive;
- $res = $zip->open($pk3);
- if($res === TRUE){
- $zip->extractTo($data_dir);
- $zip->close();
- echo " ... done\r\n";
- }else{
- echo " ... failed\r\n";
- }
- }
- /*
- * compile shaderdata
- */
- $shaders = glob("unzipped/scripts/*.shader");
- $shaderdata = "";
- foreach($shaders as $shader){
- $shaderdata .= file_get_contents($shader)."\r\n";
- }
- $shaderdata = preg_split("/\R/", $shaderdata);
- /*
- * loop through all maps, convert, pack and save to $target_dir
- */
- foreach(glob($data_dir."maps/*.bsp") as $map){
- compileMap(pathinfo($map, PATHINFO_FILENAME));
- }
- /*
- * cleanup
- */
- echo "cleaning up";
- foreach(glob($temp_dir."*") as $file){
- unlink($file);
- }
- foreach(glob($data_dir."*") as $file){
- unlink($file);
- }
- rmdir($temp_dir);
- rmdir($data_dir);
- echo " ... done\r\n";
- /*
- * main function to compile a single map from unzipped resources
- * making a best effort to take their dependencies into account
- */
- function compileMap($mapname){
- echo "compiling: ".$mapname;
- global $data_dir, $target_dir, $temp_dir, $shaderdata, $vlc;
- $zip = new ZipArchive();
- if($zip->open($target_dir.$mapname.".pk3", ZipArchive::CREATE|ZipArchive::OVERWRITE) == TRUE){
- //add bsp, overwrite version
- $data = file_get_contents($data_dir."maps/".$mapname.".bsp");
- $data = "IBSP.".substr($data, 5);
- $zip->addFromString("maps/".$mapname.".bsp", $data);
- //add levelshot
- $zip->addFile($data_dir."levelshots/preview/".$mapname.".jpg", "levelshots/".$mapname.".jpg");
- //add arena script
- $arenas = file_get_contents($data_dir."scripts/arenas.txt");
- preg_match_all("/{\s*map\s*\"".$mapname."\".*?}/s", $arenas, $arena);
- if(sizeof($arena[0]) > 0){
- $zip->addFromString("scripts/".$mapname.".arena", $arena[0][0]);
- }
- //not adding aas since it wont work with q3a anyway
- //$zip->addFile($data_dir."maps/".$mapname.".aas", "maps/".$mapname.".aas");
- //TEXTURES
- preg_match_all("/(textures|models)[a-zA-Z\/_0-9\-]*/", $data, $matches);
- $shaderBuf = "";
- $textures = array();
- foreach($matches[0] as $match){
- //gather textures pass 1/2
- $textures = array_merge($textures , glob($data_dir.$match."*"));
- //gather shaders pass 1/1
- $lvl = 0;
- $feeding = false;
- foreach($shaderdata as $line){
- if(!$feeding && preg_match("/^\s*".preg_quote($match, "/")."\s*$/" , $line)){
- $shaderBuf.=$line."\r\n";
- $feeding = true;
- }else if($feeding){
- if(preg_match("/^\s*{\s*$/" ,$line)){
- $lvl ++;
- }
- if(preg_match("/^\s*}\s*$/" ,$line)){
- $lvl --;
- if($lvl == 0){
- $feeding = false;
- }
- }
- if(!preg_match("/^\s*novlcollapse\s*$/", $line)){
- $shaderBuf.=$line."\r\n";
- }
- }
- }
- }
- //gather textures pass 2/2
- preg_match_all("/(textures|models)[a-zA-Z\/_0-9]*/", $shaderBuf, $matches);
- foreach($matches[0] as $match){
- //echo $match."\r\n";
- $temp = glob($data_dir.$match."*");
- $textures = array_merge($textures , $temp);
- }
- //add shaders
- $zip->addFromString("scripts/".$mapname.".shader", $shaderBuf);
- //add textures
- $textures = array_unique($textures);
- foreach($textures as $texture){
- $zip->addFile($texture, substr($texture, strlen($data_dir)));
- }
- //SOUNDS
- preg_match_all("/(sound|music)[a-z,A-Z,\/,_,0-9]*/", $data, $matches);
- $sounds = array();
- foreach($matches[0] as $match){
- //gather sounds pass 1/1
- $sounds = array_merge($sounds , glob($data_dir.$match.".*"));
- }
- //convert and add sounds
- $sounds = array_unique($sounds);
- foreach($sounds as $sound){
- if(!file_exists($temp_dir.pathinfo($sound, PATHINFO_FILENAME).".wav")){
- exec("\"".$vlc."\" -I dummy \"".$sound."\" --sout=#transcode{acodec=s16l,channels=1,samplerate=44100}:standard{access=file,mux=wav,dst=\"".$temp_dir.pathinfo($sound, PATHINFO_FILENAME).".wav\"} --dummy-quiet vlc://quit");
- }
- $zip->addFile($temp_dir.pathinfo($sound, PATHINFO_FILENAME).".wav", pathinfo(substr($sound, strlen($data_dir)), PATHINFO_DIRNAME)."/".pathinfo(substr($sound, strlen($data_dir)), PATHINFO_FILENAME).".wav");
- }
- $zip->close();
- echo " ... done\r\n";
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement