EHDSeven

CFGTownGenerator to Biedi

Jul 19th, 2013
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.41 KB | None | 0 0
  1. <?php
  2. /*******************************/
  3. /* CFGTownGenerator PHP Parser */
  4. /*      Created by Seven       */
  5. /*******************************/
  6. function getClasses($file){
  7.     $classes = array("position[]"=>null, "type"=>null, "direction"=>null);
  8.     $class = array();
  9.     $objects = array();
  10.     $total = 0;
  11.     $skippedJunk = false;
  12.     $inZone = false;
  13.     $inObject = false;
  14.     $file = $file."/config.cpp";
  15.     //Check if file exists.
  16.     if(File_Exists($file)) {
  17.         //Split into lines
  18.         $contents = explode("\n",file_get_contents($file));
  19.     }
  20.     foreach ($contents as $line) {
  21.         //Cleanup any spaces in each line
  22.         $line = trim($line);
  23.         $parts = array();
  24.         //Remove blank lines
  25.         if(empty($line) || $line == ""){
  26.             continue;
  27.         }
  28.         //Find start of object
  29.         if($line == 'class CfgTownGenerator {') {
  30.             $skippedJunk = true;
  31.             continue;
  32.         }
  33.         //Find the Zone
  34.         if( $skippedJunk && preg_match("/class (?!object)/i", $line)) {
  35.             $inZone = true;
  36.             continue;
  37.         }
  38.         if( preg_match("/class object/i", $line)) {
  39.             $class[$total] = array("position[]"=>"0,0,0", "type"=>"blank", "direction"=>"0");
  40.             $inObject = true;
  41.             continue;
  42.         }
  43.         //Don't Parse until we find the start string
  44.         if(!$skippedJunk)
  45.             continue;
  46.         //Find end of Object and reset everything
  47.         if($inObject && $line == "};") {
  48.             $objects[count($objects)] = $class[$total];
  49.             $total++;
  50.             $inObject = false;
  51.             continue;
  52.         }
  53.         if($inZone && $line == "};") {
  54.             $inZone = false;
  55.             continue;
  56.         }
  57.         if($skippedJunk && $line == "};") {
  58.             $skippedJunk = false;
  59.             continue;
  60.         }
  61.         //Split the var from the value
  62.         if($inObject && strpos($line,"="))
  63.             $parts = explode("=", $line);
  64.         //Cleanup and add them to the class array
  65.         if( count($parts) > 0 && array_key_exists(trim($parts[0]), $classes)) {
  66.             if($parts[0] == "type")
  67.                 $parts[1] = substr($parts[1],2,-3);
  68.             if($parts[0] == "position[] ")
  69.                 $parts[1] = substr($parts[1],2,-2);
  70.             if($parts[0] == "direction")
  71.                 (float)$parts[1] = substr($parts[1],1,-2);
  72.             $class[$total][ trim($parts[0]) ] = trim( $parts[1] );
  73.         }
  74.     }
  75.     return $objects;
  76. }
  77.  
  78. function GenerateCFGTown($zone) {
  79.     //Loop through all the zone
  80.     for($i=0;$i<count($zone);$i++) {
  81.     //Check if there is a file for the zone
  82.         if(File_Exists($zone[$i]."/config.cpp")) {
  83.             //Start Generating class for zone
  84.             $classes = getClasses($zone[$i]);
  85.             for($j=0;$j<count($classes);$j++) {
  86.                 echo "class _vehicle_".$j."\n{<br>";
  87.                 echo "  objectType=\"vehicle\";\n   class Arguments\n   {\n";
  88.                 echo "      POSITION=\"[".$classes[$j]["position[]"]."]\";<br>";
  89.                 echo "      TYPE=".$classes[$j]["type"]."<br>";
  90.                 echo "      AZIMUT=\"".$classes[$j]["direction"]."\"<br>";
  91.                 echo "      PARENT=\"\";\n  };\n};<br>";
  92.             }
  93.         }
  94.     }
  95. }
  96. $zones[0] = "cherno";
  97. /*Run The Generator*/
  98. echo "<pre>";
  99. ?>
  100. class _prefix_0
  101. {
  102.     objectType="prefix";
  103.     class Arguments
  104.     {
  105.     };
  106. };
  107. class _center_0
  108. {
  109.     objectType="center";
  110.     class Arguments
  111.     {
  112.         SIDE="west";
  113.     };
  114. };
  115. class _group_0
  116. {
  117.     objectType="group";
  118.     class Arguments
  119.     {
  120.         CENTER="_center_0";
  121.     };
  122. };
  123. class _unit_0
  124. {
  125.     objectType="unit";
  126.     class Arguments
  127.     {
  128.         POSITION="[8085.1489, 9379.293, -6.1035156e-005]";
  129.         GROUP="_group_0";
  130.         TYPE="Sniper1_DZ";
  131.         AZIMUT="10.054947";
  132.         SKILL="0.60000002";
  133.         PLAYER="true";
  134.         PLAYABLE="true";
  135.         LEADER="true";
  136.     };
  137. };
  138. <?php
  139.     GenerateCFGTown($zones);
  140. ?>
  141. class _postfix_0
  142. {
  143.     objectType="postfix";
  144.     class Arguments
  145.     {
  146.     };
  147. };
  148. <?php
  149. echo "</pre>";
  150. ?>
Advertisement
Add Comment
Please, Sign In to add comment