Skip to content

Instantly share code, notes, and snippets.

@juaxix
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juaxix/22e47ecd2611fc590913 to your computer and use it in GitHub Desktop.
Save juaxix/22e47ecd2611fc590913 to your computer and use it in GitHub Desktop.
Shiva - generate line counts for all AI models
<?php
/*
Example: > c:\php5\php.exe shivaFileCount.php "c:\Stonetrip\ShiVa Editor Basic\Data\Games\7DFPS_13"
*/
/**
* Scan Resources/AIModels for aim files
* @param string $dir
* @return array
*/
function findAllAI ( $dir ){
$t = array();
$dh = opendir($dir."\\Resources\\AIModels");
$n = 0;
while (false !== ($filename = readdir($dh))) {
if ($filename=="."||$filename=="..") continue; //do not include itself or system directories
list($sName, $sExt) = getFileNameAndExtension ( $filename );
if ($sExt!=null){
if (hasExtension ( $filename, "aim" )) {
array_push($t, $sName);
}
}
}
closedir($dh);
return $t;
}
/**
* Check for a filename extension
* @param string $sFilename
* @param string $sExt
* @return boolean
*/
function hasExtension ( $sFilename, $sExt ){
if (!empty($sFilename)){
//return string.match ( string.lower ( sFilename ), "." .. string.lower( sExt ) ) != null
return (strpos(strtolower($sFilename), ".".strtolower( $sExt ))!==false);
}
return false;
}
/**
* Get Filename and Extension in an array
* @param string $sFilename
* @return array('filename','extension')
*/
function getFileNameAndExtension ( $sFilename ){
if (!empty($sFilename)) {
// match (anything not a .).(anything not a .)
if (strpos( $sFilename, ".ccz")===false){
//Should use preg_match,but this is faster
/*for k, v in string.gmatch ( sFilename, "([^\.]+)\.([^\.]+)" ) {
return k, v
}*/
$n = strlen($sFilename);
return array(substr($sFilename,0,$n-4),substr($sFilename, $n-3,3));
}
}
return array(null,null);
}
/**
* Find all AI files inside dir/Resources/Scripts and return 3 groups (functions,handlers and states)
* @param string $dir
* @param string $sAI
* return array
*/
function findAIFiles($dir,$sAI){
$tFuncs = array();
$tHandlers = array();
$tStates = array();
//echo "\nAI:$sAI";
$dh = opendir($dir."\\Resources\\Scripts");
//for file in lfs.dir('./Resources/Scripts') do
while (false !== ($file = readdir($dh))) {
if ($file=="."||$file=="..") continue; //do not include itself or system directories
list($sName, $sExt) = getFileNameAndExtension ( $file );
if ( $sExt != null){
if (hasExtension ( $file, "lua" )){
// Look for "AINAME_"
if (strpos( $sName, $sAI . "_Function")!==false){
array_push($tFuncs, $file);
//echo "F:".$file."\n";
} elseif (strpos( $sName, $sAI . "_Handler")!==false){
array_push($tHandlers, $file);
//echo "H:".$file."\n";
} elseif ( strpos( $sName, $sAI . "_State")!==false){
array_push($tStates,$file);
//echo "S:".$file."\n";
}
}
}
}
closedir($dh);
return array($tFuncs, $tHandlers, $tStates);
}
//Main program
//1) Get Directory from argument:
$dir = $argv[1];
if (!$dir){
die("Need a valid shiva directory");
}
$dir = str_replace("\\","\\\\",$dir);
if( !is_dir($dir)) die(sprintf("Dir %s does not exists",$dir));
//2) Find all AI's inside dir
$t = findAllAI($dir);
$m = count($t);
echo "Found $m AI models";
//3) Generate CSV data from file counts
$output = "AI,Functions,Handlers,States,Lines"."
\n";
$total=0;
for ($iAI = 0; $iAI<$m; $iAI++){
list($tFuncs, $tHandlers, $tStates) = findAIFiles($dir, $t[$iAI]);
$lines = 0;
$nFuncs = count($tFuncs);
for ($iFuncs = 0; $iFuncs<$nFuncs; $iFuncs++){
$lines+=count(file($dir."\\Resources\\Scripts\\" . $tFuncs[$iFuncs]));
}
$nHandlers = count($tHandlers);
for ($iHandlers = 0; $iHandlers<$nHandlers; $iHandlers++){
$lines+=count(file($dir."\\Resources\\Scripts\\" . $tHandlers[$iHandlers]));
}
$nStates = count($tStates);
for ($iStates = 0; $iStates<$nStates; $iStates++){
$lines+=count(file($dir."\\Resources\\Scripts\\" . $tStates[$iStates]));
}
$output .= $t[$iAI] . "," . $nFuncs . "," . $nHandlers . "," . $nStates . "," . $lines .
"
\n";
$total+=$lines;
}
echo "\nTotal lines:$total";
file_put_contents("./analysis-.csv",$output);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment