#!/usr/bin/perl -w

$| = 1;

#a54b0:       8b 44 24 04             mov    0x4(%esp,1),%eax
#a54b4:       83 f8 01                cmp    $0x1,%eax
#a54b7:       74 2e                   je     a54e7 <NET_IsReservedAdr+0x37>
#a54b9:       83 f8 03                cmp    $0x3,%eax
#a54bc:       75 32                   jne    a54f0 <NET_IsReservedAdr+0x40>
#a54be:       8a 44 24 08             mov    0x8(%esp,1),%al
#

$pattern  = pack(H32,"8b44240483f801742e83f80375328a44");
$pattern2 = pack(H32,"8b44240483f801eb2e83f80375328a44");



if (! $ARGV[0] || $ARGV[0] =~ "^--?h") {
	print <<EOL;

This is a NoWON patch for engine_i686.so or engine_amd.so of the 
Half-Life-Server 4.1.1.1 for Linux.
After applying the patch, you can start the server with option 
+sv_lan as a normal worldwide open server, that doesn't requires
the clients to authenticate with WON.
						  [ZVDK]FrietBSD

http://www.zvdk.nl/1.5/

Based on a this program:
                                                     |kpd|camper   
http://kaputtendorf.de/nowon/           <camper\@kaputtendorf.de>

Usage: nowon.pl /home/blabla/hlds_l

EOL

	exit;
}



sub dopatch ($) {
	$wpfile = shift;

	print "\nLooking for code...in $wpfile";
	open(INPUT, "+<".$wpfile) || die print "\nError opening file: $wpfile\n";
	$notfoundyet = 1;
	$count = 0;
	while (read(INPUT, $_, 16)and $notfoundyet){
	
   		if ($_ eq $pattern) {
     			print "found...";
     			seek(INPUT, $count, 0);
     			print INPUT $pattern2;
     			print "patched\n\n";
			$notfoundyet = 0;
   		}

   		$count++;
   		seek (INPUT, $count, 0);

	}
	close(INPUT);
	if ($notfoundyet) {
		print "not found, patch failed.\n\n";
	}
}

sub liblistpatch ($) {
	$wpfile = shift;
	$wpfilebak = $wpfile.".bak";
	system("cp $wpfile $wpfilebak");
	$inmaster = 0;
	open(INPUT,"<$wpfilebak");
	open(OUTPUT,">$wpfile");
	while($regel = <INPUT>) {
		if ($regel =~ "secure") {
			print OUTPUT "secure \"0\"\n";
		} else {
			print OUTPUT $regel;
		}
	}

}

sub wcpatch ($) {
	$wpfile = shift;
	$wpfilebak = $wpfile.".bak";
	system("cp $wpfile $wpfilebak");
	$inmaster = 0;
	open(INPUT,"<$wpfilebak");
	open(OUTPUT,">$wpfile");
	while($regel = <INPUT>) {
		if ($inmaster) {
			if ($regel =~ "}") {
				$inmaster = 0;
			} else {
				print OUTPUT "//";
			}	
			print OUTPUT $regel;
		} else {
			if ($regel =~ "Master") {
				print OUTPUT $regel;
				$regel = <INPUT>;
				print OUTPUT $regel;
				print OUTPUT << EOL;
	zvdk.nl:27010\n";
	master.won2.steamlessproject.nl:27010
	master2.won2.steamlessproject.nl:27010
EOL

				$inmaster = 1;
			} else {
				print OUTPUT $regel;
			}
		}
	}

}

$dir=$ARGV[0];
$tpfile=$dir."/engine_amd.so";
dopatch($tpfile);
$tpfile=$dir."/engine_i686.so";
dopatch($tpfile);
		print "patching woncomm.lst and valvecomm.lst\n\n";
$tpfile=$dir."/valve/woncomm.lst";
wcpatch($tpfile);
$tpfile=$dir."/valve/valvecomm.lst";
wcpatch($tpfile);
$tpfile=$dir."/cstrike/liblist.gam";
liblistpatch($tpfile);


	print <<EOL;

Now every thing is patched, change your startup script and add 
"+sv_lan 1" on the commandline and it will run
as a normal worldwide open server, that doesn't requires
the clients to authenticate with WON.
						  [ZVDK]FrietBSD

http://www.zvdk.nl/1.5/
Based on a this program:
                                                     |kpd|camper   
http://kaputtendorf.de/nowon/           <camper\@kaputtendorf.de>

EOL

