package XSTAB::XData; # This module stores all data required to be shared across the modules. # $Id: XData.pm,v 1.8 2003/03/24 02:20:38 dranok Exp $ # This module is released under the GPL # Author: DranoK # Email: dranok@users.sourceforge.net ###############################################################
XSTAB::XData;
This is the global storage area for XStab.
The following items are exported:
require Exporter; use IO::File; use strict; our @ISA = qw(Exporter); our @EXPORT = qw(%Players %Global @Debug_log @Voice_queue @Admin_queue load_config write_log do_log XSerialize XSerialStore); my $VERSION = '0.9'; # Global variables # (Other packages must use XSTAB::Xdata) our %Players; our @Debug_log; our @Voice_queue; our @Admin_queue; our %Global;
sub load_config { my $cfile = shift; my $tmpbuf; my $fileh = IO::File->new($cfile) or die "Cannot open $cfile: $!\n"; $fileh->sysread($tmpbuf, 65507); $fileh->close(); my @tmpar = split('\n', $tmpbuf); foreach my $elem (@tmpar) { # KEY = VALUE if ($elem =~ /^[\s]*([a-zA-Z0-9_]+)[\s]*=[\s]*(.*)[\s]*$/) { my $key = $1; my $value = $2; print "INITIALIZATION: Setting Global{$key} = $value\n"; $Global{$key} = $value; } } # Just in case someone's an idiot $Global{CFile} = $cfile; $Global{sp} = ":x:"; $Global{sp2} = "::xxx::"; $Global{MAX_WRITE} = 20; }
NOTE: This function uses blocking IO, despite its use of syswrite. This is because something is terribly, terribly wrong if we can't log, and thus the process probably SHOULD freeze. It's different than timing out a read, when we have log data we want to write we NEED to write it. See notes on blocking used in the XDbiMySQL storage module as well
sub write_log { my $lines = shift; my $time = time; while($lines) { last if (scalar(@Debug_log) < 1); my ($prio, $printline) = split($Global{sp}, shift(@Debug_log)); $main::debug->pushlog($prio, $printline); if ($prio <= $Global{Debug_level}) { $Global{logfh}->syswrite("[$time:$prio] $printline\n"); } $lines--; } }
sub do_log { my $data = shift; my $prio = shift; # Instant debug info if ($Global{Use_debug}) { print "$prio: $data\n"; } push(@Debug_log, "$prio$Global{sp}$data"); }
sub XSerialStore { my $client = shift; my $field = shift; my $string = shift; my @list = split(',', $string); foreach my $elem (@list) { my ($f, $v) = split(':', $elem); $Players{$client}{$field}{$f} = $v; } }
For example:
$Players |-ClientID |-Field |---a = val1 |---b = valb ... |---c = valc
This structure would be returned as ``a:aval$Global{sp}b:bval$Global{sp}...$Global{sp}z:zval'' (``a:aval:x:b:bval:x:...:x:z:zval'')
sub XSerialize { my $client = shift; my $field = shift; my $string; foreach my $elem (keys %{$Players{$client}{$field}}) { $string .= "$elem:$Players{$client}{$field}{$elem},"; } $string =~ s/,$//; $string =~ s/^:$//; return $string; } 1;
This module was coded by DranoK and is part of the core XStab modules. You may directly contact DranoK at dranok@users.sourceforge.net, or by posting to the forums at:
http://www.oltl.net/forums/forumdisplay.php?s=&forumid=25