| 1 | package Games::TF2::Server; | ||
| 2 | |||
| 3 | 1 | ▶ | use v5.26; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use feature 'signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | no warnings 'experimental::signatures'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '0.01'; | ||
| 9 | |||
| 10 | 1 | ▶ | use Object::Pad 0.800; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | # Loads the XS, which has Games::TF2::Server::_Connection too. | ||
| 13 | 0 | ▶ | use Games::TF2::Demo; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 14 | |||
| 15 | # A connection's handle reads the state as a demo's parser does. | ||
| 16 | @Games::TF2::Server::_Connection::ISA = ('Games::TF2::Demo::_Parser'); | ||
| 17 | |||
| 18 | class Games::TF2::Server :abstract :does(Games::TF2::Demo::StateReader) { | ||
| 19 | use Readonly; | ||
| 20 | use Scalar::Util qw(blessed); | ||
| 21 | |||
| 22 | use Games::TF2::Demo::Error; | ||
| 23 | use Games::TF2::Demo::StateReader; | ||
| 24 | use Games::TF2::Server::Event::Chat; | ||
| 25 | use Games::TF2::Server::Event::Convars; | ||
| 26 | use Games::TF2::Server::Event::CvarQuery; | ||
| 27 | use Games::TF2::Server::Event::Disconnected; | ||
| 28 | use Games::TF2::Server::Event::EntityFrame; | ||
| 29 | use Games::TF2::Server::Event::GameEvent; | ||
| 30 | use Games::TF2::Server::Event::InGame; | ||
| 31 | use Games::TF2::Server::Event::LevelChange; | ||
| 32 | use Games::TF2::Server::Event::Menu; | ||
| 33 | use Games::TF2::Server::Event::Print; | ||
| 34 | use Games::TF2::Server::Event::ServerInfo; | ||
| 35 | use Games::TF2::Server::Event::Stats; | ||
| 36 | use Games::TF2::Server::Event::UserMessage; | ||
| 37 | use Games::TF2::Server::Handler; | ||
| 38 | use Games::TF2::Server::Stats; | ||
| 39 | |||
| 40 | # The longest duration tfnet.h takes, 24 hours in milliseconds. | ||
| 41 | Readonly my $MAX_MILLISECONDS => 86_400_000; | ||
| 42 | |||
| 43 | # tfnet.h's defaults of the two timeouts, which tfnet_options_set_timeouts | ||
| 44 | # sets only together. | ||
| 45 | Readonly my $DEFAULT_CONNECT_TIMEOUT => 10_000; | ||
| 46 | Readonly my $DEFAULT_SILENCE_TIMEOUT => 30_000; | ||
| 47 | |||
| 48 | # How long run waits in one poll; a poll returns early once events are ready. | ||
| 49 | Readonly my $RUN_POLL_MILLISECONDS => 1000; | ||
| 50 | |||
| 51 | # The handler method of each event type (the name of its class). | ||
| 52 | Readonly my %METHOD_OF_TYPE => map { $_ => 'on_' . lc(s/(?<=[a-z])(?=[A-Z])/_/gr) } Games::TF2::Server::_Connection->names('event_type'); | ||
| 53 | |||
| 54 | Readonly my %IS_SOURCE => map { $_ => 1 } Games::TF2::Server::_Connection->names('source'); | ||
| 55 | Readonly my %IS_CVAR_STATUS => map { $_ => 1 } Games::TF2::Server::_Connection->names('cvar_status'); | ||
| 56 | |||
| 57 | # The subscriptions that take a value, with the XS method and the check of the value. | ||
| 58 | Readonly my %SUBSCRIPTION_WITH_VALUE => ( | ||
| 59 | entity => [ subscribe_entity_index => \&_is_count ], | ||
| 60 | entity_class => [ subscribe_entity_class => \&_is_text ], | ||
| 61 | prop => [ subscribe_prop => \&_is_text ], | ||
| 62 | event => [ subscribe_event => \&_is_text ], | ||
| 63 | ); | ||
| 64 | |||
| 65 | Readonly my @COMMON_PARAMS => qw(address tables name version password user_info cvar_answers connect_timeout silence_timeout entities temp_entities sounds); | ||
| 66 | |||
| 67 | # A Games::TF2::Server::_Connection: the C API's connection, freed with it. | ||
| 68 | field $connection; | ||
| 69 | field $address :param :reader = undef; | ||
| 70 | |||
| 71 | # The options of new, until the connect. | ||
| 72 | field %options; | ||
| 73 | |||
| 74 | ADJUST :params ( | ||
| 75 | :$tables = undef, | ||
| 76 | :$name = undef, | ||
| 77 | :$version = undef, | ||
| 78 | :$password = undef, | ||
| 79 | :$user_info = undef, | ||
| 80 | :$cvar_answers = undef, | ||
| 81 | :$connect_timeout = undef, | ||
| 82 | :$silence_timeout = undef, | ||
| 83 | :$entities = undef, | ||
| 84 | :$temp_entities = undef, | ||
| 85 | :$sounds = undef, | ||
| 86 | ) | ||
| 87 | { | ||
| 88 | $self->_argument_error('new needs an address "host:port"') unless _is_text($address); | ||
| 89 | $self->_argument_error('new needs tables, the path of a demo the server recorded') unless _is_text($tables); | ||
| 90 | my %strings = ( name => $name, version => $version, password => $password ); | ||
| 91 | foreach my $param ( sort keys %strings ) { | ||
| 92 | $self->_argument_error("$param must be a string") if defined $strings{$param} && ref $strings{$param}; | ||
| 93 | } | ||
| 94 | my %durations = ( connect_timeout => $connect_timeout, silence_timeout => $silence_timeout ); | ||
| 95 | foreach my $param ( sort keys %durations ) { | ||
| 96 | $self->_argument_error("$param must be milliseconds from 1 to $MAX_MILLISECONDS") if defined $durations{$param} && !_is_duration( $durations{$param} ); | ||
| 97 | } | ||
| 98 | $self->_argument_error('user_info must be a hash reference of strings') if defined $user_info && !_is_hash_of_strings($user_info); | ||
| 99 | $self->_argument_error('cvar_answers must be a hash reference') if defined $cvar_answers && ref $cvar_answers ne 'HASH'; | ||
| 100 | |||
| 101 | %options = ( | ||
| 102 | tables => $tables, | ||
| 103 | ( defined $name ? ( name => $name ) : () ), | ||
| 104 | ( defined $version ? ( version => $version ) : () ), | ||
| 105 | ( defined $password ? ( password => $password ) : () ), | ||
| 106 | ( defined $user_info ? ( user_info => { $user_info->%* } ) : () ), | ||
| 107 | ( defined $cvar_answers ? ( cvar_answers => { map { $_ => $self->_cvar_answer( $cvar_answers->{$_} ) } keys $cvar_answers->%* } ) : () ), | ||
| 108 | ( | ||
| 109 | defined $connect_timeout || defined $silence_timeout | ||
| 110 | ? ( connect_timeout => $connect_timeout // $DEFAULT_CONNECT_TIMEOUT, silence_timeout => $silence_timeout // $DEFAULT_SILENCE_TIMEOUT ) | ||
| 111 | : () | ||
| 112 | ), | ||
| 113 | ( defined $entities ? ( entities => $entities ? 1 : 0 ) : () ), | ||
| 114 | ( defined $temp_entities ? ( temp_entities => $temp_entities ? 1 : 0 ) : () ), | ||
| 115 | ( defined $sounds ? ( sounds => $sounds ? 1 : 0 ) : () ), | ||
| 116 | ); | ||
| 117 | } | ||
| 118 | |||
| 119 | # Connects with the options of new and those of the kind; called by the | ||
| 120 | # ADJUST of Games::TF2::Server::SourceTV and Games::TF2::Server::Player | ||
| 121 | # with the parameters only they know (%unknown: those none knows). | ||
| 122 | method _connect ( $is_player, $kind_options, $unknown ) { | ||
| 123 | my @unknown = sort keys $unknown->%*; | ||
| 124 | $self->_argument_error( 'new does not take ' . join( ', ', @unknown ) . ' (known: ' . join( ', ', @COMMON_PARAMS, sort keys $kind_options->%* ) . ')' ) if @unknown; | ||
| 125 | my %defined_kind_options = map { $_ => $kind_options->{$_} } grep { defined $kind_options->{$_} } keys $kind_options->%*; | ||
| 126 | $connection = Games::TF2::Server::_Connection->connect( $is_player ? 1 : 0, $address, { %options, %defined_kind_options } ); | ||
| 127 | %options = (); | ||
| 128 | return; | ||
| 129 | } | ||
| 130 | |||
| 131 | method poll ($timeout) { | ||
| 132 | $self->_argument_error("poll needs a timeout in milliseconds from 0 to $MAX_MILLISECONDS") unless _is_count($timeout) && $timeout <= $MAX_MILLISECONDS; | ||
| 133 | return map { _event_of($_) } $connection->poll($timeout); | ||
| 134 | } | ||
| 135 | |||
| 136 | method run ($handler) { | ||
| 137 | $self->_argument_error('the handler must be an object that does Games::TF2::Server::Handler') unless blessed($handler) && $handler->DOES('Games::TF2::Server::Handler'); | ||
| 138 | while (1) { | ||
| 139 | foreach my $fields ( $connection->poll($RUN_POLL_MILLISECONDS) ) { | ||
| 140 | my $method = $METHOD_OF_TYPE{ $fields->{type} }; | ||
| 141 | $handler->$method( _event_of($fields) ) if defined $method && $handler->can($method); | ||
| 142 | return $self if $fields->{type} eq 'Disconnected'; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | method phase () { | ||
| 148 | return $connection->phase; | ||
| 149 | } | ||
| 150 | |||
| 151 | method version () { | ||
| 152 | return $connection->version; | ||
| 153 | } | ||
| 154 | |||
| 155 | method stats () { | ||
| 156 | return Games::TF2::Server::Stats->new( $connection->stats->%* ); | ||
| 157 | } | ||
| 158 | |||
| 159 | method send_command ($command) { | ||
| 160 | $self->_argument_error('send_command needs a command') unless _is_text($command); | ||
| 161 | $connection->send_command($command); | ||
| 162 | return $self; | ||
| 163 | } | ||
| 164 | |||
| 165 | method set_cvar_answer ( $name, $answer ) { | ||
| 166 | $self->_argument_error('set_cvar_answer needs a cvar name') unless _is_text($name); | ||
| 167 | $connection->set_cvar_answer( $name, $self->_cvar_answer($answer)->@* ); | ||
| 168 | return $self; | ||
| 169 | } | ||
| 170 | |||
| 171 | method silence ($milliseconds) { | ||
| 172 | $self->_argument_error("silence needs milliseconds from 1 to $MAX_MILLISECONDS") unless _is_duration($milliseconds); | ||
| 173 | $connection->silence($milliseconds); | ||
| 174 | return $self; | ||
| 175 | } | ||
| 176 | |||
| 177 | method disconnect ( $reason = '' ) { | ||
| 178 | $self->_argument_error('the reason must be a string') if !defined $reason || ref $reason; | ||
| 179 | $connection->disconnect($reason); | ||
| 180 | return $self; | ||
| 181 | } | ||
| 182 | |||
| 183 | method subscribe (@what) { | ||
| 184 | $self->_subscriptions( 1, @what ); | ||
| 185 | return $self; | ||
| 186 | } | ||
| 187 | |||
| 188 | method unsubscribe (@what) { | ||
| 189 | $self->_subscriptions( 0, @what ); | ||
| 190 | return $self; | ||
| 191 | } | ||
| 192 | |||
| 193 | method simulation_tick :common ( $value, $tick, $entity_index ) { | ||
| 194 | my %numbers = ( value => $value, tick => $tick, entity_index => $entity_index ); | ||
| 195 | foreach my $name ( sort keys %numbers ) { | ||
| 196 | my $number = $numbers{$name}; | ||
| 197 | next if defined $number && !ref $number && $number =~ /\A-?[0-9]+\z/; | ||
| 198 | Games::TF2::Demo::Error::Argument->throw( message => "Games::TF2::Server: simulation_tick needs an integer $name" ); | ||
| 199 | } | ||
| 200 | return Games::TF2::Server::_Connection->simulation_tick( $value, $tick, $entity_index ); | ||
| 201 | } | ||
| 202 | |||
| 203 | # A player's own entity; a SourceTV spectator has none. | ||
| 204 | method _subscribe_self ($on) { | ||
| 205 | Games::TF2::Demo::Error::State->throw( message => 'Games::TF2::Server: a SourceTV spectator has no entity of its own' ); | ||
| 206 | } | ||
| 207 | |||
| 208 | method _connection () { return $connection } | ||
| 209 | |||
| 210 | method _state_handle () { return $connection } | ||
| 211 | |||
| 212 | method _error_prefix () { return 'Games::TF2::Server' } | ||
| 213 | |||
| 214 | method _argument_error ($message) { | ||
| 215 | Games::TF2::Demo::Error::Argument->throw( message => "Games::TF2::Server: $message" ); | ||
| 216 | } | ||
| 217 | |||
| 218 | method _subscriptions ( $on, @what ) { | ||
| 219 | my $verb = $on ? 'subscribe' : 'unsubscribe'; | ||
| 220 | $self->_argument_error("$verb needs what to $verb to") unless @what; | ||
| 221 | while (@what) { | ||
| 222 | my $word = shift @what; | ||
| 223 | $self->_argument_error("$verb takes names of subscriptions, not references or undef") unless _is_text($word); | ||
| 224 | if ( my $subscription = $SUBSCRIPTION_WITH_VALUE{$word} ) { | ||
| 225 | my ( $xs_method, $is_valid ) = $subscription->@*; | ||
| 226 | my $value = shift @what; | ||
| 227 | $self->_argument_error( "$verb( $word => ... ) needs a " . ( $word eq 'entity' ? 'non-negative integer' : 'name' ) ) unless $is_valid->($value); | ||
| 228 | $connection->$xs_method( $value, $on ); | ||
| 229 | next; | ||
| 230 | } | ||
| 231 | if ( $word eq 'stats' ) { | ||
| 232 | $on ? $self->_subscribe_stats( shift @what ) : $connection->subscribe_stats(0); | ||
| 233 | next; | ||
| 234 | } | ||
| 235 | if ( $word eq 'events' ) { | ||
| 236 | $connection->subscribe_events($on); | ||
| 237 | next; | ||
| 238 | } | ||
| 239 | if ( $word eq 'self' ) { | ||
| 240 | $self->_subscribe_self($on); | ||
| 241 | next; | ||
| 242 | } | ||
| 243 | $self->_argument_error("no subscription $word") unless $IS_SOURCE{$word}; | ||
| 244 | $connection->subscribe_source( $word, $on ); | ||
| 245 | } | ||
| 246 | return; | ||
| 247 | } | ||
| 248 | |||
| 249 | method _subscribe_stats ($milliseconds) { | ||
| 250 | $self->_argument_error("subscribe( stats => ... ) needs milliseconds from 1 to $MAX_MILLISECONDS") unless _is_duration($milliseconds); | ||
| 251 | $connection->subscribe_stats($milliseconds); | ||
| 252 | return; | ||
| 253 | } | ||
| 254 | |||
| 255 | # A cvar answer as [ status, value ]: a string is a value, an array | ||
| 256 | # reference a status and, for the status 'value', the value. | ||
| 257 | method _cvar_answer ($answer) { | ||
| 258 | return [ value => $answer ] if defined $answer && !ref $answer; | ||
| 259 | $self->_argument_error('a cvar answer is a value or [ status, value ]') unless ref $answer eq 'ARRAY' && $answer->@* >= 1 && $answer->@* <= 2; | ||
| 260 | my ( $status, $value ) = $answer->@*; | ||
| 261 | $self->_argument_error( 'no cvar status ' . ( $status // 'undef' ) . ' (known: ' . join( ', ', sort keys %IS_CVAR_STATUS ) . ')' ) | ||
| 262 | unless defined $status && !ref $status && $IS_CVAR_STATUS{$status}; | ||
| 263 | $self->_argument_error('a cvar answer [ value => ... ] needs the value') if $status eq 'value' && ( !defined $value || ref $value ); | ||
| 264 | return [ $status, $status eq 'value' ? $value : undef ]; | ||
| 265 | } | ||
| 266 | |||
| 267 | # A connection belongs to the thread that made it. | ||
| 268 | sub CLONE_SKIP { return 1 } | ||
| 269 | |||
| 270 | sub _event_of ($fields) { | ||
| 271 | my %fields = $fields->%*; | ||
| 272 | my $type = delete $fields{type}; | ||
| 273 | Games::TF2::Demo::Error::Internal->throw( message => "Games::TF2::Server: an event of an unknown type $type" ) unless $METHOD_OF_TYPE{$type}; | ||
| 274 | return "Games::TF2::Server::Event::$type"->new(%fields); | ||
| 275 | } | ||
| 276 | |||
| 277 | sub _is_text ($value) { | ||
| 278 | return defined $value && !ref $value && length $value; | ||
| 279 | } | ||
| 280 | |||
| 281 | sub _is_count ($value) { | ||
| 282 | return defined $value && !ref $value && $value =~ /\A[0-9]+\z/; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub _is_duration ($value) { | ||
| 286 | return _is_count($value) && $value >= 1 && $value <= $MAX_MILLISECONDS; | ||
| 287 | } | ||
| 288 | |||
| 289 | sub _is_hash_of_strings ($hash) { | ||
| 290 | return ref $hash eq 'HASH' && !grep { !defined || ref } values $hash->%*; | ||
| 291 | } | ||
| 292 | } | ||
| 293 | |||
| 294 | 1; | ||
| 295 | |||
| 296 | __END__ | ||