Advertisement
Guest User

Convert Quake Live maps to Quake 3 Arena: PHP Script

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