Merge branch 'master' of https://Governor-Tarkin@bitbucket.org/Governor-Tarkin/swg...
[swg-src.git] / tools / test_build_win32.pl
bloba9534e2eb420f7d87199e69298e830f70b127cec
1 #! /usr/bin/perl
2 # ======================================================================
3 # ======================================================================
5 use warnings;
6 use strict;
7 use Socket;
9 # ======================================================================
10 # Constants
11 # ======================================================================
13 use constant START_BOOTLEG_TEST => "A";
14 use constant END_COMMUNICATION => "B";
15 use constant SUCCESSFUL_TEST => "C";
16 use constant UNSUCCESSFUL_TEST => "D";
17 use constant SERVER_READY => "E";
18 use constant BOOTLEG_MISMATCH => "F";
19 use constant CLIENT_KILL => "G";
20 use constant CLIENT_OK => "H";
22 # ======================================================================
23 # Globals
24 # ======================================================================
26 my $scriptName = $0;
27 $scriptName =~ s/^(.*)\\//;
29 my $branch;
30 my $bootlegnum = "";
31 my $waittime = "60";
32 my $port = "98514";
34 # ======================================================================
35 # Subroutines
36 # ======================================================================
38 sub usage
40 die "\n\t$scriptName\n\n";
43 sub perforceWhere
45 local $_;
47 # find out where a perforce file resides on the local machine
48 my $result;
50 open(P4, "p4 where $_[0] |");
51 $_ = <P4>;
52 chomp;
53 my @where = split;
54 $result = $where[2];
55 close(P4);
58 return $result;
61 sub reloop
63 print STDERR "Error with: $_[0]\n" if(defined $_[0]);
64 print STDERR "Exiting this connection\n";
65 goto FAIL;
68 sub installBootleg
70 print "Installing bootleg...\n";
72 open(INSTALL, "perl " . perforceWhere("//depot/swg/current/tools/InstallBootleg.pl") . " --list --force_newest --client_only $branch |") or reloop "Error running InstallBootleg.pl\n";
74 my $complete = 0;
76 while(<INSTALL>)
78 print;
79 $bootlegnum = $1 if(/^Updating to build: (\d+)/);
80 $complete = 1 if(/^Update complete/);
83 close(INSTALL);
85 print "Bootleg installation incomplete\n" if(!$complete);
86 print "Completed installing bootleg.\n" if($complete);
88 return $complete;
91 sub error
93 my $message = shift @_;
94 print SOCKET UNSUCCESSFUL_TEST;
95 print STDERR "$message\n";
96 goto FAIL;
99 sub fatal
101 my $message = shift @_;
102 print SOCKET UNSUCCESSFUL_TEST;
103 close(SOCKET);
104 die "$message\n";
107 sub makeDir
109 my(@tok, $check);
110 @tok = split(/\\|\//, $_[0]);
112 $check = shift(@tok);
113 foreach (@tok)
115 $check .= "/$_";
116 if(!(-d $check))
118 mkdir $check;
124 sub testBootleg
126 my $buffer;
127 print STDERR "Initializing communication...\n";
129 error("problem reading from socket") if (read(SOCKET, $buffer, 4) != 4);
130 my $length = unpack("N", $buffer);
131 error("problem reading from socket") if(read(SOCKET, $branch, $length) != $length);
133 print STDERR "Testing bootleg for branch: $branch\n";
135 my $bootlegdir = perforceWhere("//depot/swg/$branch/bootleg/win32/...");
136 $bootlegdir =~ s/\.{3}//;
138 chdir($bootlegdir) or reloop "Cannot change to bootleg directory\n";
140 installBootleg() || fatal "Error installing bootleg";
142 print SOCKET pack("N", $bootlegnum);
144 error("problem reading from socket") if (read(SOCKET, $buffer, 1) != 1);
145 error("mismatch in client / server bootlegs")if($buffer eq BOOTLEG_MISMATCH);
146 error("server not ready") if ($buffer ne SERVER_READY);
147 print STDERR "Bootleg $bootlegnum verified with server - running client...\n";
149 # Get the ip of the server machine
150 my $other_socket = getpeername(SOCKET) || reloop "Couldn't identify other end: $!\n";
151 my ($other_port, $other_ip) = unpack_sockaddr_in($other_socket);
152 my $server_ip = inet_ntoa($other_ip);
154 my $killresult;
155 my $swgpid = open(SWGCLIENT, "SwgClient_o.exe -- -s ClientGame loginServerAddress=$server_ip skipIntro=true skipSplash=true autoConnectToLoginServer=true loginClientID=bootleg loginClientPassword=bootleg avatarName=\"bootleg bootleg\" autoConnectToGameServer=true autoQuitAfterLoadScreen=true -s SharedFoundation demoMode=true |");
156 error("problem reading from socket") if (read(SOCKET, $buffer, 1) != 1);
157 if($buffer eq CLIENT_KILL)
159 kill 1, $swgpid;
160 error("Test unsuccessful - forced to kill client");
162 elsif($buffer eq CLIENT_OK)
164 # make sure we give the client a chance to exit on its own, then attempt to kill
165 print "Waiting for $waittime seconds for the client to end on its own...\n";
166 sleep($waittime);
167 $killresult = kill 1, $swgpid;
169 close(SWGCLIENT);
171 # clientResult = 1 if return value of SwgClient == 0 and we did not have to kill it ($killresult = 0)
172 my $clientResult = (!($? >> 8) && !$killresult);
173 print "clientresult=$clientResult killresult=$killresult exitresult=$?\n";
175 print SOCKET ($clientResult == 1) ? SUCCESSFUL_TEST : UNSUCCESSFUL_TEST;
177 print STDERR "Test was " . (($clientResult == 1) ? "successful\n" : "unsuccessful\n") . "\n";
180 # ======================================================================
181 # Main
182 # ======================================================================
184 # open the daemon socket
185 print STDERR "Opening socket\n";
186 socket(LISTEN, PF_INET, SOCK_STREAM, getprotobyname('tcp')) || die "socket failed\n";
187 setsockopt(LISTEN, SOL_SOCKET, SO_REUSEADDR, 1) || die "setsockopt failed\n";
188 my $addr = sockaddr_in($port, INADDR_ANY);
189 bind(LISTEN, $addr) || die "bind failed\n";
190 listen(LISTEN, 1) || die "listen failed\n";
192 BUILDLOOP:
193 while (1)
195 print STDERR "Waiting on a connection...\n";
197 accept(SOCKET, LISTEN) || reloop "accept failed\n";
199 # make binary and unbuffer the socket
200 binmode(SOCKET);
201 my $oldSelect = select(SOCKET);
202 $| = 1;
203 select($oldSelect);
205 my $buffer;
206 error("problem reading from socket") if (read(SOCKET, $buffer, 1) != 1);
208 if($buffer eq START_BOOTLEG_TEST)
210 print "Got message to initiate bootleg test.\n";
211 testBootleg();
213 FAIL:
214 close(SOCKET);