PangZero/Config.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 155 CC 16 SCAR 28.5
CC
16
Cov
0%
GetConfigFilename
30.0
LoadConfig
30.0
FindDataDir
20.0
CRAP 17.4
1 ##########################################################################
2 # CONFIG SAVE/LOAD
3 ##########################################################################
4  
5 package Games::PangZero::Config;
6  
7 use File::ShareDir qw(dist_dir);
8  
9 sub IsMicrosoftWindows {
10 return $^O eq 'MSWin32';
11 }
12  
13  
14 sub TestDataDir {
15 return -f "$Games::PangZero::DataDir/glossyfont.png"; # Should be a file from the latest version.
16 }
17  
18 sub FindDataDir {
19 return if $Games::PangZero::DataDir and TestDataDir();
20 my @guesses = ('.', dist_dir('Games-PangZero'));
21 foreach my $guess (@guesses) {
22 $Games::PangZero::DataDir = $guess;
23 return if TestDataDir();
24 $Games::PangZero::DataDir = "$guess/data";
25 return if TestDataDir();
26 }
27 die "Couldn't find the data directory. Please set it manually.";
28 }
29  
30 sub GetConfigFilename {
31 if ( IsMicrosoftWindows() ) {
32 if ($ENV{USERPROFILE}) {
33 return "$ENV{USERPROFILE}\\pangzero.cfg";
34 }
35 return "$Games::PangZero::DataDir/pangzero.cfg";
36 }
37 if ($ENV{HOME}) {
38 return "$ENV{HOME}/.pangzerorc";
39 }
40 if (-w $Games::PangZero::DataDir) {
41 return "$Games::PangZero::DataDir/pangzero.cfg";
42 }
43 return "/tmp/pangzero.cfg";
44 }
45  
46 sub GetConfigVars {
47 my ($i, $j);
48 my @result = qw(
49 Games::PangZero::NumGuys
50 Games::PangZero::DifficultyLevelIndex
51 Games::PangZero::WeaponDurationIndex
52 Games::PangZero::Slippery
53 Games::PangZero::MusicEnabled
54 Games::PangZero::SoundEnabled
55 Games::PangZero::FullScreen
56 Games::PangZero::ShowWebsite
57 Games::PangZero::DeathBallsEnabled
58 Games::PangZero::EarthquakeBallsEnabled
59 Games::PangZero::WaterBallsEnabled
60 Games::PangZero::SeekerBallsEnabled
61 );
62 for ($i=0; $i < scalar @Games::PangZero::Players; ++$i) {
63 for ($j=0; $j < 3; ++$j) {
64 push @result, ("Games::PangZero::Players[$i]->{keys}->[$j]");
65 }
66 push @result, ("Games::PangZero::Players[$i]->{colorindex}");
67 push @result, ("Games::PangZero::Players[$i]->{imagefileindex}");
68 }
69 my ($difficulty, $gameMode);
70 for ($difficulty=0; $difficulty < scalar @Games::PangZero::DifficultyLevels; ++$difficulty) {
71 foreach $gameMode ('highScoreTablePan', 'highLevelTablePan', 'highScoreTableCha', 'highLevelTableCha') {
72 next if ($Games::PangZero::DifficultyLevels[$difficulty]->{name} eq 'Miki' and $gameMode eq 'highScoreTableCha');
73 for ($i=0; $i < 5; ++$i) {
74 push @result, "Games::PangZero::DifficultyLevels[$difficulty]->{$gameMode}->[$i]->[0]", # Name of high score
75 "Games::PangZero::DifficultyLevels[$difficulty]->{$gameMode}->[$i]->[1]", # High score
76 }
77 }
78 }
79 return @result;
80 }
81  
82 sub SaveConfig {
83 my ($filename, $varname, $value);
84 $filename = GetConfigFilename();
85  
86 open CONFIG, "> $filename" or return;
87 foreach $varname (GetConfigVars()) {
88 eval("\$value = \$$varname"); die $@ if $@;
89 print CONFIG "\$$varname = $value\n";
90 }
91 close CONFIG;
92 }
93  
94 sub LoadConfig {
95 my ($filename, $text, $varname);
96  
97 $text = '';
98 $filename = GetConfigFilename();
99 if (open CONFIG, "$filename") {
100 read CONFIG, $text, 16384;
101 close CONFIG;
102 }
103  
104 foreach $varname (GetConfigVars()) {
105 my $pattern = $varname;
106 $pattern =~ s/\[/\\[/g;
107 if ($text =~ /$pattern = (.+?)$/m) {
108 $val = $1;
109 if($val =~ /^SDLK_\w+$/) {
110 eval( "\$$varname = SDL::Events::$val()" );
111 }
112 elsif($val =~ /^[\d\.]+$/) {
113 eval( "\$$varname = $val" );
114 }
115 else {
116 eval( "\$$varname = '$val'" );
117 }
118 }
119 }
120  
121 SetDifficultyLevel($Games::PangZero::DifficultyLevelIndex);
122 SetWeaponDuration($Games::PangZero::WeaponDurationIndex);
123 }
124  
125 sub SetDifficultyLevel {
126 my $difficultyLevelIndex = shift;
127 if ($difficultyLevelIndex < 0 or $difficultyLevelIndex > $#Games::PangZero::DifficultyLevels) {
128 $difficultyLevelIndex = $Games::PangZero::DifficultyLevelIndex;
129 }
130 $Games::PangZero::DifficultyLevelIndex = $difficultyLevelIndex;
131 $Games::PangZero::DifficultyLevel = $Games::PangZero::DifficultyLevels[$difficultyLevelIndex];
132 }
133  
134 sub SetWeaponDuration {
135 my $weaponDurationIndex = shift;
136 if ($weaponDurationIndex < 0 or $weaponDurationIndex > $#Games::PangZero::WeaponDurations) {
137 $weaponDurationIndex = $Games::PangZero::WeaponDurationIndex;
138 }
139 $Games::PangZero::WeaponDurationIndex = $weaponDurationIndex;
140 $Games::PangZero::WeaponDuration = $Games::PangZero::WeaponDurations[$Games::PangZero::WeaponDurationIndex];
141 }
142  
143 1;