File: Config.pm

package info (click to toggle)
pangzero 1.4.1%2Bgit20121103-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 3,636 kB
  • sloc: perl: 4,105; makefile: 24
file content (147 lines) | stat: -rw-r--r-- 4,582 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
##########################################################################
# CONFIG SAVE/LOAD
##########################################################################

package Games::PangZero::Config;

use File::ShareDir qw(dist_dir);

sub IsMicrosoftWindows {
  return $^O eq 'MSWin32';
}


sub TestDataDir {
  return -f "$Games::PangZero::DataDir/glossyfont.png";   # Should be a file from the latest version.
}

sub FindDataDir {
  return if $Games::PangZero::DataDir and TestDataDir();
  my @guesses = ('.', dist_dir('Games-PangZero'));
  foreach my $guess (@guesses) {
    $Games::PangZero::DataDir = $guess;
    return if TestDataDir();
    $Games::PangZero::DataDir = "$guess/data";
    return if TestDataDir();
  }
  die "Couldn't find the data directory. Please set it manually.";
}

sub GetConfigFilename {
  if ( IsMicrosoftWindows() ) {
    if ($ENV{USERPROFILE}) {
      return "$ENV{USERPROFILE}\\pangzero.cfg";
    }
    return "$Games::PangZero::DataDir/pangzero.cfg";
  }
  if ($ENV{HOME}) {
    return "$ENV{HOME}/.pangzerorc";
  }
  if (-w $Games::PangZero::DataDir) {
    return "$Games::PangZero::DataDir/pangzero.cfg";
  }
  return "/tmp/pangzero.cfg";
}

sub GetConfigVars {
  my ($i, $j);
  my @result = qw(
    Games::PangZero::NumGuys
    Games::PangZero::DifficultyLevelIndex
    Games::PangZero::WeaponDurationIndex
    Games::PangZero::Slippery
    Games::PangZero::MusicEnabled
    Games::PangZero::SoundEnabled
    Games::PangZero::FullScreen
    Games::PangZero::ShowWebsite
    Games::PangZero::DeathBallsEnabled
    Games::PangZero::EarthquakeBallsEnabled
    Games::PangZero::WaterBallsEnabled
    Games::PangZero::SeekerBallsEnabled
  );
  for ($i=0; $i < scalar @Games::PangZero::Players; ++$i) {
    for ($j=0; $j < 3; ++$j) {
      push @result, ("Games::PangZero::Players[$i]->{keys}->[$j]");
    }
    push @result, ("Games::PangZero::Players[$i]->{colorindex}");
    push @result, ("Games::PangZero::Players[$i]->{imagefileindex}");
  }
  my ($difficulty, $gameMode);
  for ($difficulty=0; $difficulty < scalar @Games::PangZero::DifficultyLevels; ++$difficulty) {
    foreach $gameMode ('highScoreTablePan', 'highLevelTablePan', 'highScoreTableCha', 'highLevelTableCha') {
      next if ($Games::PangZero::DifficultyLevels[$difficulty]->{name} eq 'Miki' and $gameMode eq 'highScoreTableCha');
      for ($i=0; $i < 5; ++$i) {
        push @result, "Games::PangZero::DifficultyLevels[$difficulty]->{$gameMode}->[$i]->[0]", # Name of high score
                      "Games::PangZero::DifficultyLevels[$difficulty]->{$gameMode}->[$i]->[1]", # High score
      }
    }
  }
  return @result;
}

sub SaveConfig {
  my ($filename, $varname, $value);
  $filename = GetConfigFilename();

  open CONFIG, "> $filename" or return;
  foreach $varname (GetConfigVars()) {
    eval("\$value = \$$varname"); die $@ if $@;
    print CONFIG "\$$varname = $value\n";
  }
  close CONFIG;
}

sub LoadConfig {
  my ($filename, $text, $varname);

  $text     = '';
  $filename = GetConfigFilename();
  if (open CONFIG, "$filename") {
    read CONFIG, $text, 16384;
    close CONFIG;
  }

  foreach $varname (GetConfigVars()) {
    my $pattern = $varname;
    $pattern    =~ s/\[/\\[/g;
    $pattern    =~ s/\{/\\{/g;
    if ($text =~ /$pattern = (.+?)$/m) {
      $val = $1;
      if ($varname eq Games::PangZero::ShowWebsite) {
        eval( "\$$varname = '$val'" );
      }
      elsif($val =~ /^SDLK_\w+$/) {
        eval( "\$$varname = SDL::Events::$val()" );
      }
      elsif($val =~ /^[\d\.]+$/) {
        eval( "\$$varname = $val" );
      }
      else {
        eval( "\$$varname = '$val'" );
      }
    }
  }

  SetDifficultyLevel($Games::PangZero::DifficultyLevelIndex);
  SetWeaponDuration($Games::PangZero::WeaponDurationIndex);
}

sub SetDifficultyLevel {
  my $difficultyLevelIndex = shift;
  if ($difficultyLevelIndex < 0 or $difficultyLevelIndex > $#Games::PangZero::DifficultyLevels) {
    $difficultyLevelIndex = $Games::PangZero::DifficultyLevelIndex;
  }
  $Games::PangZero::DifficultyLevelIndex = $difficultyLevelIndex;
  $Games::PangZero::DifficultyLevel      = $Games::PangZero::DifficultyLevels[$difficultyLevelIndex];
}

sub SetWeaponDuration {
  my $weaponDurationIndex = shift;
  if ($weaponDurationIndex < 0 or $weaponDurationIndex > $#Games::PangZero::WeaponDurations) {
    $weaponDurationIndex = $Games::PangZero::WeaponDurationIndex;
  }
  $Games::PangZero::WeaponDurationIndex = $weaponDurationIndex;
  $Games::PangZero::WeaponDuration      = $Games::PangZero::WeaponDurations[$Games::PangZero::WeaponDurationIndex];
}

1;