| 1 | package PennMUSH; |
|---|
| 2 | |
|---|
| 3 | use File::Copy; |
|---|
| 4 | use File::Path; |
|---|
| 5 | use MUSHConnection; |
|---|
| 6 | |
|---|
| 7 | my @pids = (); |
|---|
| 8 | |
|---|
| 9 | sub new { |
|---|
| 10 | my $proto = shift; |
|---|
| 11 | my $class = ref($proto) || $proto; |
|---|
| 12 | my $self = {}; |
|---|
| 13 | bless($self, $class); |
|---|
| 14 | if (@_) { |
|---|
| 15 | $self->{HOST} = shift; |
|---|
| 16 | $self->{PORT} = shift; |
|---|
| 17 | $self->start(@_) if @_; |
|---|
| 18 | } else { |
|---|
| 19 | $self->start(); |
|---|
| 20 | } |
|---|
| 21 | return $self; |
|---|
| 22 | } |
|---|
| 23 | |
|---|
| 24 | sub start { |
|---|
| 25 | my $self = shift; |
|---|
| 26 | srand(); |
|---|
| 27 | my $port = $self->{PORT} || int(rand(2000)) + 12000; |
|---|
| 28 | $self->{HOST} = "localhost"; |
|---|
| 29 | $self->{PORT} = $port; |
|---|
| 30 | rmtree("testgame"); |
|---|
| 31 | mkpath(["testgame/data", "testgame/log", "testgame/txt"]); |
|---|
| 32 | copyConfig("../game/mushcnf.dst", "testgame/test.cnf", |
|---|
| 33 | "port" => $port, |
|---|
| 34 | "compress_program" => "", |
|---|
| 35 | "uncompress_program" => "", |
|---|
| 36 | "compress_suffix" => "", |
|---|
| 37 | @_); |
|---|
| 38 | copy("../game/alias.cnf", "testgame/alias.cnf"); |
|---|
| 39 | copy("../game/names.cnf", "testgame/names.cnf"); |
|---|
| 40 | copy("../game/restrict.cnf", "testgame/restrict.cnf"); |
|---|
| 41 | my $file; |
|---|
| 42 | foreach $file (glob("../game/txt/*.txt")) { |
|---|
| 43 | my $target = $file; |
|---|
| 44 | $target =~ s-../game-testgame-o; |
|---|
| 45 | copy($file, $target); |
|---|
| 46 | } |
|---|
| 47 | symlink("../../src/netmud", "testgame/netmush"); |
|---|
| 48 | symlink("../../src/info_slave", "testgame/info_slave"); |
|---|
| 49 | my $child = fork(); |
|---|
| 50 | if ($child > 0) { |
|---|
| 51 | my $j; |
|---|
| 52 | my $line; |
|---|
| 53 | push(@pids, $child); |
|---|
| 54 | $self->{PID} = $child; |
|---|
| 55 | foreach $j (1..20) { |
|---|
| 56 | next unless open(LOG, "testgame/log/netmush.log"); |
|---|
| 57 | while ($line = <LOG>) { |
|---|
| 58 | close(LOG), return $port if $line =~ /^Listening on port $port /; |
|---|
| 59 | } |
|---|
| 60 | } continue { |
|---|
| 61 | sleep(1); |
|---|
| 62 | } |
|---|
| 63 | die "Could not start game process properly; pid $child!\n"; |
|---|
| 64 | } elsif (defined($child)) { |
|---|
| 65 | chdir("testgame"); |
|---|
| 66 | exec("./netmush", "--no-session", "test.cnf", "log/netmush.log"); |
|---|
| 67 | } else { |
|---|
| 68 | die "Could not spawn game process!\n"; |
|---|
| 69 | } |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | sub copyConfig { |
|---|
| 73 | my $from = shift; |
|---|
| 74 | my $to = shift; |
|---|
| 75 | my %subs = @_; |
|---|
| 76 | |
|---|
| 77 | open(FROM, "<$from") || die "Could not open template configuration.\n"; |
|---|
| 78 | open(TO, ">$to") || die "Could not write test configuration.\n"; |
|---|
| 79 | my $line; |
|---|
| 80 | while ($line = <FROM>) { |
|---|
| 81 | next if $line =~ /^\s*#/o; |
|---|
| 82 | next unless $line =~ /^\s*(\w+)\s/o; |
|---|
| 83 | my $key = $1; |
|---|
| 84 | $line = $key . " " . $subs{$key} . "\n" if defined($subs{$key}); |
|---|
| 85 | } continue { |
|---|
| 86 | print TO $line; |
|---|
| 87 | } |
|---|
| 88 | close(TO); |
|---|
| 89 | close(FROM); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | sub login { |
|---|
| 93 | my $self = shift; |
|---|
| 94 | return MUSHConnection->new($self->{HOST}, $self->{PORT}, @_); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | sub loginGod { |
|---|
| 98 | my $self = shift; |
|---|
| 99 | return MUSHConnection->new($self->{HOST}, $self->{PORT}, "One", "one"); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | END { |
|---|
| 103 | kill("TERM", @pids); |
|---|
| 104 | } |
|---|
| 105 | |
|---|
| 106 | 1; |
|---|