blib/lib/Games/Axmud/Obj/Zmp.pm untested

filter: Statementstmt 0.0% 0 / 42 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 79 CC 7 SCAR 33.0
CC
7
Cov
0%
new
56.0
name
2.0
packageName
2.0
CRAP 27.2
1 # Copyright (C) 2011-2024 A S Lewis
2 #
3 # This program is free software: you can redistribute it and/or modify it under the terms of the GNU
4 # General Public License as published by the Free Software Foundation, either version 3 of the
5 # License, or (at your option) any later version.
6 #
7 # This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8 # even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License along with this program. If not,
12 # see <http://www.gnu.org/licenses/>.
13 #
14 #
15 # Games::Axmud::Obj::ZMP
16 # Objects used to store ZMP packages
17  
18 { package Games::Axmud::Obj::Zmp;
19  
20 use strict;
21 use warnings;
22 # use diagnostics;
23  
24 use Glib qw(TRUE FALSE);
25  
26 our @ISA = qw(Games::Axmud);
27  
28 ##################
29 # Constructors
30  
31 sub new {
32  
33 # Can be called by anything. To create a new ZMP package object, just get your plugin to
34 # call this ->new function; the code handles everything else
35 #
36 # The package object created is available to every session that's connected to the specified
37 # world. If no world is specified, the package object is available to every world
38 # This object is given an object name. If a world is specified, it's in the form
39 # PackageName@WorldName
40 # (...that is to say, two strings, neither containing an @ character, joined together with
41 # an @ character)
42 # If no world is specified, the object name is in the form
43 # PackageName@
44 # (...that is to say, the package name with an @ appended to it)
45 # NB When connected to a world called 'deathmud', 'mypackage@deathmud' is used instead of
46 # the generic package object, 'mypackage@', if both exist
47 #
48 # Duplicate object names are not allowed. It another plugin has already created a package
49 # object with the same object name, this object won't be created
50 #
51 # Expected arguments
52 # $packageName - The ZMP package name, a string in the form 'Package[.SubPackages]'.
53 # Must not be the string 'zmp'
54 #
55 # Optional arguments
56 # $world - The name of a world profile. If specified, this package object is
57 # available to all sessions using that world; if 'undef', this package
58 # object is available to all sessions. This function doesn't check
59 # that a rofile called $world actually exists
60 # @cmdList - A list of package commands for this package. A list in groups of two,
61 # in the form
62 # (package_command, function_reference...)
63 # - The 'function_reference' should be a reference to a function in the
64 # plugin that called this function
65 # - Duplicate commands are not acceptable, but duplicate function
66 # references are fine (for example, if you want the same function to
67 # be called for every command)
68 # - If the list is empty, the package object is still created, but Axmud
69 # regards it as being unsupported (and that's what it will tell the
70 # server, if queried). The ZMP spec says that commands must not be
71 # changed once a ZMP session has begun, so $self->cmdHash should not
72 # be altered, once created; instead edit your plugin and then restart
73 # Axmud (package objects are not saved to any file). You can prevent
74 # Axmud from supporting the ZMP package 'example' by simply creating
75 # empty package object called 'example'
76 #
77 # Return values
78 # 'undef' on improper arguments, if $name is invalid, if @argList contains an odd number
79 # of items or if @argList contains a duplicate command
80 # Blessed reference to the newly-created object on success
81  
82 my ($class, $packageName, $world, @cmdList) = @_;
83  
84 # Local variables
85 my (
86 $objName,
87 @compList,
88 %cmdHash,
89 );
90  
91 # Check for improper arguments
92 if (! defined $class || ! defined $packageName) {
93  
94 return $axmud::CLIENT->writeImproper($class . '->new', @_);
95 }
96  
97 # Check that $name is valid
98 if (
99 $packageName eq 'zmp'
100 || ! ($packageName =~ m/^[\w\-]([\w\-\.]*[\w\-])?$/)
101 ) {
102 return undef;
103 }
104  
105 # Split $packageName into its components
106 @compList = split(m/\./, $packageName);
107  
108 # Set the object name
109 $objName = $packageName . '@';
110 if (defined $world) {
111  
112 $objName .= $world;
113 }
114  
115 # Prepare the hash of commands (if any were specified)
116 if (@cmdList) {
117  
118 do {
119  
120 my ($cmd, $funcRef);
121  
122 $cmd = shift @cmdList;
123 $funcRef = shift @cmdList;
124  
125 if (! defined $funcRef) {
126  
127 # @argList did not contain an even number of values
128 return undef;
129  
130 } elsif (exists $cmdHash{$cmd}) {
131  
132 # @argList contains a duplicate command
133 return undef;
134  
135 } else {
136  
137 $cmdHash{$cmd} = $funcRef;
138 }
139  
140 } until (! @cmdList);
141 }
142  
143 # Setup
144 my $self = {
145 _objName => $objName,
146 _objClass => $class,
147 _parentFile => undef, # Object not saved even if $world specified
148 _parentWorld => undef,
149 _privFlag => TRUE, # All IVs are private
150  
151 # IVs
152 # ---
153  
154 # The name of this object, in the form 'PackageName@WorldName' or 'PackageName@'
155 name => $objName,
156 # The ZMP package name, a string in the form 'Package[.SubPackages]'. Must not be the
157 # string 'zmp'
158 packageName => $packageName,
159 # $name, split into a list of its components
160 compList => \@compList,
161  
162 # The name of a world profile. If specified, this package object is available to all
163 # sessions using that world; if 'undef', this package object is available to all
164 # sessions. This function doesn't check that a profile called $world actually exists
165 world => $world,
166  
167 # A hash of commands recognised by this package. A hash in the form
168 # $cmdHash{command} = function_reference
169 # where 'function_reference' is a reference to a function in the plugin that called this
170 # ->new function)
171 # If the hash is empty, Axmud regards the package as unsupported. You can prevent Axmud
172 # from supporting the ZMP package 'example' by simply creating an empty package object
173 # called 'example'
174 cmdHash => \%cmdHash,
175 };
176  
177 # Bless the object into existence
178 bless $self, $class;
179  
180 # Add it to the GA::Client hash. If a package object named $objName already exists, it is
181 # not replaced (and 'undef' is returned)
182 return $axmud::CLIENT->add_zmpPackage($self);
183 }
184  
185 ##################
186 # Methods
187  
188 ##################
189 # Accessors - set
190  
191 ##################
192 # Accessors - get
193  
194 sub name
195 { $_[0]->{name} }
196 sub packageName
197 { $_[0]->{packageName} }
198 sub compList
199 { my $self = shift; return @{$self->{compList}}; }
200  
201 sub world
202 { $_[0]->{world} }
203  
204 sub cmdHash
205 { my $self = shift; return %{$self->{cmdHash}}; }
206 }
207  
208 # Package must return a true value
209 1