Page 1 of 1

Perl script to move WSC data between maps

Posted: Sat Sep 23, 2006 9:07 am
by supremoleet
I came up with this one out of boredom and sleep deprivation. It's a fairly basic perl script I wrote to move everything from the defaultWorldfile from Felucca to Trammel.

This was designed because I had it backwards -- I thought everything started in Trammel, so when I first wrote this I was under the impression that I was moving everything from Trammel (0) to Felucca (1). Then I read up here on the forums and realized Felucca is 0, so everything was already where I wanted it. Regardless, I finished writing the script -- maybe it'll be useful to someone else.

This is one of the several perl scripts I've written to help administer/maintain the server I'm working on. Eventually I'll have to post some of the others up.

Code: Select all

#!/usr/bin/perl -w

# # # # # # # # # # # # # # #
#                           #
# Map Location Switch Thing #
#                           #
#    v1.0 - Frank Zecca     #
#      aka Supremoleet      #
#     fzecca@gmail.com      #
#                           #
# # # # # # # # # # # # # # #

# INSTRUCTIONS
# ------------
# This file MUST be in ./shared folder. It will generate the paths.txt file on its own.
# Make the changes to the map settings below as necessary. By default, this will change
# Felucca (0) to Trammel (1).
#
# This is really only recommended for moving Felucca -> Trammel or Trammel -> Felucca.

# This is where you'll want to specify your changes.
#   0 = Felucca (huzzah!)
#   1 = Trammel (eew)
#   2 = Ilshenar
#   3 = Malas
#   4 = Tokuno

# This is the original value, i.e. - the one you want to change:
$c_from = "0";

# This is what you want to change the map to:
$c_to = "1";

# Now let's do it...
print "Generating the file listing... ";
system("find . -name \"*.wsc\" > ./paths.txt");
print "Done.\n";

print "Opening the file listing... ";
open( MAINFH, "paths.txt" ) || die "Couldn't open the path file.\n";
print "Done.\n";

	while( $fline = <MAINFH> )
	{
		print "Starting file: $fline";
		open( RFILE, "$fline") || die "Could not open $fline.\n";
		
		while( $sline = <RFILE> )
		{
			if( $sline =~ /Location/ )
			{
				print "Correcting $sline";
				$sline =~ s/,$c_from\n/,$c_to\n/;
				print "Done. New: $sline\n";
				$l_corr++;
			}
			$tempfile .= $sline;
			$l_tot++;
		}
		close( RFILE );

		open( WRFILE, ">$fline") || die "Could not open $fline for write.\n";
		print WRFILE $tempfile;
		close( WRFILE );

		$tempfile = "";
	}

close( MAINFH );

print "\n\nFinished!\nLines corrected: $l_corr\nTotal lines processed: $l_tot\n\n";
Its function is simple: It goes through all the files it digs up ending in .WSC, and looks for lines which contain "Location". Any matching lines, it will then search for the criteria ($c_from) followed by a newline (\n). If that is found, it is changed to $c_to with a newline.

For example, if we were switching from Felucca (0) to Trammel (1), it's going to look for ",0\n" and replace it with ",1\n".

Code: Select all

Location=1248,1050,20,0
would become:

Code: Select all

Location=1248,1050,20,1
While the following would be ignored:

Code: Select all

Location=780,510,5,3
(because it ends with ",3\n", not ",0\n").

Anyhow, that should cover it. Pretty straight-forward. Comments/questions/suggestions appreciated.