Useful scripts for developers

From FlightGear wiki
Jump to navigation Jump to search

Aircraft scripts

Files loaded

There are many files used for an aircraft and it could be difficult when developing or maintaining an aircraft to know how all these files are loaded, by which file and if they are used.

The script below searches in xml, nas and ac files other loaded files : nas, xml, wav, rgb and png.

Feel free to improve it !

Perl script (linux)

#!/usr/bin/perl -W

# author : hardball
# version : v20161017-01
# description :
#   recursively search in xml, nas and ac files, other files loaded  : nas, xml,
#   wav, rgb and png

use Term::ANSIColor ;

#===============================================================================
#                                                                     CONSTANTES

# dirname of the aircraft to scan
my $dirname_aircraft = 'bourrasque' ;

# dirpath of fgdata
my $dirpath_fgdata = '/home/nico/FG/fgdata/' ;

# name of aircraft (the one called when launching fgfs)
my $aircraft = 'bourrasque' ;

# starting path for recursive search
my $start_dirpath_root = '/home/nico/my_hangar/' ; # slash ending

# color for type of files found, see perl ANSIColor
my %color ;
$color{'xml'} = 'bright_blue' ;
$color{'ac'}  = 'bright_yellow' ;
$color{'png'} = 'bright_magenta' ;
$color{'rgb'} = 'red' ;
$color{'nas'} = 'yellow' ;
$color{'eff'} = 'bright_red' ;
$color{'wav'} = 'white' ;

#===============================================================================
#                                                       VARIABLES INITIALISATION

my @a = () ;
my $dirpath_root = $start_dirpath_root . $dirname_aircraft .'/' ; # slash ending


#===============================================================================
#                                                                      FUNCTIONS

#-------------------------------------------------------------------------------
#                                                                     print_line
sub print_line_tree
{
    # getting params
    my ($line, $extension, @depth) = @_ ;
    
    my $indent = '' ;
    for(my $d = 0 ; $d < @depth ; $d++)
    {
        if($d == (@depth - 1)) { $indent .= $depth[$d] ? ' `-- ' : ' |-- ' ; }
        else                   { $indent .= $depth[$d] ? '     ' : ' |   ' ; }
    }
    
    print $indent ;
    print color $color{$extension} ;
    print $line ;
    print color 'reset' ;
    print "\n" ;
}

#-------------------------------------------------------------------------------
#                                                                    search_tree
# display result on tree view
# params :
#   $path_found_in_file : file to print and to parse
#   $depth              : depth of recursivity call
#   $from_path          : file's dirpath where the file has been found
#   $from_file          : filepath where the file has been found
#   $list_is_last       : contains data helping to display the tree
sub search_aircraft
{
    # getting params
    my ($path_found_in_file, $depth, $from_path, $from_file, $list_is_last) = @_ ;
    
    # TODO contient chaque fichier parent de l'item en cours si c'est le dernier de la liste
    my @lil = @{$list_is_last} ;
    
    # try to sanitize path
    my $escaped_dirname_aircraft = quotemeta($dirname_aircraft) ;
    $path_found_in_file =~ s/^\/?Aircraft\/$escaped_dirname_aircraft\/// ;
    
    # if relative path : ./foo.xml <=> foo.xml
    $path_found_in_file =~ s/^.\/// ;
    
    # if relative path detected, we add the previous path
    if(($path_found_in_file !~ /\//) || ($path_found_in_file =~ /^\.\.\//))
    {
        $path_found_in_file = $from_path . $path_found_in_file ;
    }
    
    $fullpath_to_try = $dirpath_root . $path_found_in_file ;
    # file does not exist, lets try in fgdata directory :
    if(! -f $fullpath_to_try) { $fullpath_to_try = $dirpath_fgdata . $path_found_in_file ;  }
    # file not found, may be a bug ;)
    if(! -f $fullpath_to_try) { print STDERR "[W] file not found : SKIPPING - $path_found_in_file\n" ; return ; }
    
    #print "                                             DEBUG $path_found_in_file --- $depth --- ".(join('.',@lil))."\n";
    
    ###
    # FIRST, let's print the file found
    #
    my ($extension) = ($fullpath_to_try =~ /\.(\w+)$/) ;
    print_line_tree($path_found_in_file, $extension, @lil) ;
    
    ###
    # THEN, let's search in this file if it loads other files
    #
    
    # initialize new param which will be used by the recursive function
    ($from_path = $path_found_in_file) =~ s/[^\/]+$// ;
    $depth++ ;
    
    # according to file extension, we remove comments and extract filenames
    my $nb_files_found = 0 ;
    my %uniq_f = () ;
    if(($extension eq 'xml') || ($extension eq 'eff'))
    {
        # if xml or eff, we will search xml, nas, ac, wav files :
        open(my $CONTENT, $fullpath_to_try) ; my @CONTENT = <$CONTENT> ; close $CONTENT ;
        
        # hack to avoid parsing comments
        my $XML_CONTENT = join('~~~~~', @CONTENT) ;
        $XML_CONTENT =~ s/<!--.*?-->//mgs ;
        @CONTENT = split('~~~~~', $XML_CONTENT) ;
        
        for(@CONTENT)
        {
            if((/>(.+\.(xml|nas|ac|wav))</) || (/=["'](.+\.(xml|nas|ac|wav))["']/)) { $uniq_f{$1} = 1 ; }
            elsif(/inherits-from>(.+)(\.eff)?</) { $uniq_f{$1 .'.eff'} = 1 ; }
        }
    }
    elsif($extension eq 'nas')
    {
        # if nas, we search xml, nas files :
        open(my $CONTENT, $fullpath_to_try) ; my @CONTENT = <$CONTENT> ; close $CONTENT ;
        for(@CONTENT)
        {
            next if(/^\s*#/) ; # we don't parse commented lines
            s/\s+#.+$// ;      # we don't parse commented end lines
            if(/["']([^"']+$dirname_aircraft\/[^"']+\.(xml|nas))["']/) { $uniq_f{$1} = 1 ; }
        }
    }
    elsif($extension eq 'ac')
    {
        # if ac, we search png, rgb files :
        open(my $CONTENT, $fullpath_to_try) ; my @CONTENT = <$CONTENT> ; close $CONTENT ;
        for(@CONTENT)
        {
            if(/texture\s+["'](.+\.(png|rgb))/) { $uniq_f{$1} = 1 ; }
        }
    }
    else
    {
        pop(@lil) ;
    }
    
    # if files found, we continue recursive search
    for $path(sort keys %uniq_f)
    {
        my $is_last_item = (++$nb_files_found == scalar(keys %uniq_f)) ? 1 : 0 ;
        push(@lil, $is_last_item) ;
        search_aircraft($path, $depth, $from_path, $path_found_in_file, \@lil) ;
        pop(@lil) ;
    }
}

#===============================================================================
#                                                                           MAIN
search_aircraft($aircraft .'-set.xml', 0, '', '', \@a) ;

### EOF

Configuration and installation

  1. Copy-paste the text in a text file named "files-loaded.pl"
  2. You should modify some constants on the top of the file : $dirname_aircraft, $aircraft, $dirpath_fgdata, $dirpath_root
  3. chmod the file : chmod ug+x files-loaded.pl
  4. Run the script : ./files-loaded.pl

External links