$value) { if(preg_match("/^\d+\s/",$value)==0) // ^\d+ = "at least one digit at the beginning of the line followed by whitespace" { array_shift($contents2da2); } } foreach($modelnumbers as $key => $value) { if($contentline=$contents2da2[intval($key)]) { $contentline=substr_replace($contentline,$value,0,strlen($key)); } else { $contentline=create_bogus_line($file,$value); // sometimes there are missing entries in the original hak, we'll try to create some basic parse for those } $newcontents.=$contentline; } $newcontents=$contents2da1.$newcontents; //file_put_contents($output.$file,$newcontents); $handle = fopen($output.$file, "w"); fwrite($handle,$newcontents); fclose($handle); } function create_bogus_line($file, $number) { // only 2 options, either it's a robe or something else if($file=="parts_robe.2da") { $bogusline=$number.BOGUS_ROBE; } else { $bogusline=$number.BOGUS_ARMOR; } return $bogusline; } // check folder integrity and create new folders if needed function check_folders() { if (!$handle=@opendir(INPUT_DIR)): mkdir(INPUT_DIR); else: closedir($handle); endif; if (!$handle=@opendir(INPUT_2DA1)): mkdir(INPUT_2DA1); else: closedir($handle); endif; if (!$handle=@opendir(INPUT_2DA2)): mkdir(INPUT_2DA2); else: closedir($handle); endif; if (!$handle=@opendir(OUTPUT_DIR)): mkdir(OUTPUT_DIR); else: closedir($handle); endif; if (!$handle=@opendir(OUTPUT_2DA)): mkdir(OUTPUT_2DA); else: closedir($handle); endif; } // a simple file count based on the extension function count_files($dir,$filetype=EXTENSION_ALL) // EXTENSION_ALL = NULL { if($filetype==NULL): $filenumber=count(glob($dir."*.*")); else: $filenumber=count(glob($dir."*.".$filetype)); endif; return $filenumber; } // this function gets the last line of the pre-merged .2da files function get_last_2da_line($file) // "$file" should be a .2da file { $contents=file($file); $lastlinenumber=sizeof($contents)-1; $lastlinecontent=$contents[$lastlinenumber]; //if last line is empty, we check the previous one, until we reach the BOF while(preg_match("/^(\s|\n\r)/",$lastlinecontent)) { $lastlinenumber--; $lastlinecontent=$contents[$lastlinenumber]; } unset($contents,$lastlinenumber); return $lastlinecontent; } // this function gets the ID from a .2da line function get_2da_number($line) { $pattern="/^\d+/"; // regex pattern, "1 or more digits at the beginning of the line" preg_match($pattern,$line,$id); return $id[0]; // in preg_match, [0] means "the part of the string that matches the full pattern" } // get all the 2da files in $dir and return them in an array function get_2da_array($dir) { $array2da=array(); if($handle = opendir($dir)) { while(false!==($file=readdir($handle))) { if(file_extension($file)=="2da") { $array2da[]=$file; } } } closedir($handle); return $array2da; } // check whether some 2da belong to the armor hak or not function check_2da($array2da, $type) { // this array stores the actual names of the .2da files from the input folder we can use $arrayproper2da = array(); // $arrayproper2da is filled with the intersection of the keys from $type (check const.php) and the array formed with // the 2da files from the input folder $arrayproper2da=array_intersect($array2da,array_keys($type)); // The function returns an array containing only validated 2da files. // This array will be used AS the main loop in the software, which // will run once per 2da file in the input folder. return $arrayproper2da; } // function to check whether we have stored previously the model in $modelnumbers (an array) // As the array follows this scheme -> $modelnumbers[oldnumber]==newnumber, we search // against its keys to know if an old model number was previously stored function model_is_stored($modelnumbers,$file) { $oldmodelnumber=get_old_model_number($file); return array_key_exists($oldmodelnumber,$modelnumbers); } // function to store model numbers in $modelnumbers, // which is sent through reference and not by value // thus, the function returns nothing function store_model($modelnumbers,$file,$currentmdl) { $oldmodelnumber=get_old_model_number($file); $modelnumbers[$oldmodelnumber]=$currentmdl; } function get_old_model_number($file) { // as the name convention is very explicit (always 3 numbers) // it's theoretically safe to just return the last 3 characters // of the file name return substr(file_name($file),-3,3); } // get file extension function file_extension($filename) { $path_info=pathinfo($filename); return $path_info['extension']; } // get file name function file_name($filename) { $filename=substr($filename,0,strlen($filename)-4); return $filename; } // function to show some messages in the output log; it's a tad poor function logmessage() { $args=func_get_args(); $placeholder=0; $i=0; //first argument must be the CONSTANT with the message foreach($args as $arg) { if($i==0) { $message=$arg; $i=1; } else { $message=str_replace("%".$placeholder,$arg,$message); $placeholder++; } } echo $message.PHP_EOL; //flush(); // this may not work if gzip is enabled } function get_input($length=255) { $fr=fopen('php://stdin',"r"); $input=fgets($fr,$length); $input=rtrim($input); fclose($fr); return $input; } ?>