Guest User

quakelive map convert script

a guest
Feb 4th, 2014
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.34 KB | None | 0 0
  1. <?php
  2.    
  3.     /*
  4.      *  define globals
  5.      */
  6.      
  7.     $ql_pk3_dir = "C:/Users/USERNAME/AppData/LocalLow/id Software/quakelive/baseq3/"; //path to quakelive baseq3 folder
  8.     $target_dir = "D:/Games/Q3A/baseq3/";   //output folder for converted map pk3's
  9.     $data_dir   = "unzipped/";  //temporary folder for unzipped pk3 contents
  10.     $temp_dir   = "temp/";  //temporary folder for altered pk3 and converted soundfiles
  11.    
  12.     /*
  13.      *  create folders
  14.      */
  15.  
  16.     if(!is_dir($target_dir)) mkdir($target_dir);
  17.     if(!is_dir($data_dir)) mkdir($data_dir);
  18.     if(!is_dir($temp_dir)) mkdir($temp_dir);
  19.    
  20.     /*
  21.      *  convert pk3's
  22.      */
  23.    
  24.     foreach(glob($ql_pk3_dir."*.pk3") as $pk3){
  25.         $pk3_basename = pathinfo($pk3, PATHINFO_BASENAME);
  26.         echo "converting: ".$pk3_basename;
  27.         if(!file_exists($temp_dir.$pk3_basename)){
  28.             exec("quakelivedec.exe \"".$pk3."\" \"".$temp_dir.$pk3_basename."\"");
  29.         }
  30.         echo " ... done\r\n";
  31.     }
  32.  
  33.     /*
  34.      *  unpack pk3's
  35.      */
  36.      
  37.     foreach(glob($temp_dir."*.pk3") as $pk3){
  38.         $pk3_basename = pathinfo($pk3, PATHINFO_BASENAME);
  39.         echo "unpacking: ".$pk3_basename;
  40.         $zip = new ZipArchive;
  41.         $res = $zip->open($pk3);
  42.         if($res === TRUE){
  43.             $zip->extractTo($data_dir);
  44.             $zip->close();
  45.             echo " ... done\r\n";
  46.         }else{
  47.             echo " ... failed\r\n";
  48.         }
  49.     }
  50.      
  51.      
  52.     /*
  53.      *  compile shaderdata
  54.      */
  55.      
  56.     $shaders = glob("unzipped/scripts/*.shader");
  57.     $shaderdata = "";
  58.     foreach($shaders as $shader){
  59.         $shaderdata .= file_get_contents($shader)."\r\n";
  60.     }  
  61.     $shaderdata = preg_split("/\R/", $shaderdata);
  62.    
  63.     /*
  64.      *  loop through all maps, convert, pack and save to $target_dir
  65.      */
  66.      
  67.     foreach(glob($data_dir."maps/*.bsp") as $map){
  68.         compileMap(pathinfo($map, PATHINFO_FILENAME));
  69.     }
  70.    
  71.     /*
  72.      *  cleanup
  73.      */
  74.     echo "cleaning up";
  75.     foreach(glob($temp_dir."*") as $file){
  76.         unlink($file);
  77.     }
  78.     foreach(glob($data_dir."*") as $file){
  79.         unlink($file);
  80.     }
  81.     rmdir($temp_dir);
  82.     rmdir($data_dir);
  83.     echo " ... done\r\n";
  84.    
  85.     /*
  86.      *  main function to compile a single map from unzipped resources
  87.      *  making a best effort to take their dependencies into account
  88.      */
  89.    
  90.     function compileMap($mapname){
  91.         echo "compiling: ".$mapname;
  92.         global $data_dir, $target_dir, $temp_dir, $shaderdata;
  93.         $zip = new ZipArchive();
  94.        
  95.         if($zip->open($target_dir.$mapname.".pk3", ZipArchive::CREATE|ZipArchive::OVERWRITE) == TRUE){
  96.            
  97.             //add bsp, overwrite version
  98.             $data = file_get_contents($data_dir."maps/".$mapname.".bsp");          
  99.             $data = "IBSP.".substr($data, 5);
  100.             $zip->addFromString("maps/".$mapname.".bsp", $data);
  101.            
  102.             //add levelshot
  103.             $zip->addFile($data_dir."levelshots/preview/".$mapname.".jpg", "levelshots/".$mapname.".jpg");
  104.            
  105.             //add arena script
  106.             $arenas = file_get_contents($data_dir."scripts/arenas.txt");
  107.             preg_match_all("/{\s*map\s*\"".$mapname."\".*?}/s", $arenas, $arena);
  108.             if(sizeof($arena[0]) > 0){
  109.                 $zip->addFromString("scripts/".$mapname.".arena", $arena[0][0]);
  110.             }
  111.            
  112.             //not adding aas since it wont work with q3a anyway
  113.             //$zip->addFile($data_dir."maps/".$mapname.".aas", "maps/".$mapname.".aas");           
  114.            
  115.             //TEXTURES
  116.             preg_match_all("/(textures|models)[a-zA-Z\/_0-9\-]*/", $data, $matches);
  117.             $shaderBuf = "";
  118.             $textures = array();
  119.             foreach($matches[0] as $match){
  120.                 //gather textures pass 1/2
  121.                 $textures = array_merge($textures , glob($data_dir.$match."*"));
  122.                                    
  123.                 //gather shaders pass 1/1
  124.                 $lvl = 0;
  125.                 $feeding = false;
  126.                 foreach($shaderdata as $line){
  127.                     if(!$feeding && preg_match("/^\s*".preg_quote($match, "/")."\s*$/" , $line)){
  128.                         $shaderBuf.=$line."\r\n";
  129.                         $feeding = true;
  130.                     }else if($feeding){
  131.                         if(preg_match("/^\s*{\s*$/" ,$line)){
  132.                             $lvl ++;
  133.                         }
  134.                         if(preg_match("/^\s*}\s*$/" ,$line)){
  135.                             $lvl --;
  136.                             if($lvl == 0){
  137.                                 $feeding = false;
  138.                             }
  139.                         }
  140.                         if(!preg_match("/^\s*novlcollapse\s*$/", $line)){
  141.                             $shaderBuf.=$line."\r\n";
  142.                         }                  
  143.                     }
  144.                 }
  145.             }          
  146.            
  147.             //gather textures pass 2/2
  148.             preg_match_all("/(textures|models)[a-zA-Z\/_0-9]*/", $shaderBuf, $matches);
  149.             foreach($matches[0] as $match){
  150.                 //echo $match."\r\n";
  151.                 $temp = glob($data_dir.$match."*");
  152.                 $textures = array_merge($textures , $temp);
  153.             }
  154.            
  155.             //add shaders
  156.             $zip->addFromString("scripts/".$mapname.".shader", $shaderBuf);        
  157.    
  158.             //add textures
  159.             $textures = array_unique($textures);
  160.             foreach($textures as $texture){
  161.                 $zip->addFile($texture, substr($texture, strlen($data_dir)));
  162.             }
  163.            
  164.             //SOUNDS
  165.             preg_match_all("/(sound|music)[a-z,A-Z,\/,_,0-9]*/", $data, $matches);
  166.             $sounds = array();
  167.             foreach($matches[0] as $match){
  168.                 //gather sounds pass 1/1
  169.                 $sounds = array_merge($sounds , glob($data_dir.$match.".*"));
  170.             }
  171.            
  172.             //convert and add sounds
  173.             $sounds = array_unique($sounds);
  174.             foreach($sounds as $sound){
  175.                 if(!file_exists($temp_dir.pathinfo($sound, PATHINFO_FILENAME).".wav")){
  176.                     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");
  177.                 }
  178.                 $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");
  179.             }
  180.             $zip->close();
  181.            
  182.             echo " ... done\r\n";
  183.         }
  184.     }
  185. ?>
Advertisement
Add Comment
Please, Sign In to add comment