-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
bus: use json for data transport, new plugins #1019
Open
allanon
wants to merge
10
commits into
master
Choose a base branch
from
bus_plugins
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+463
−218
Open
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1dd4882
bus: use json for data transport, new plugins
allanon ef50373
add bus command plugin and command line script
allanon c34b225
Merge branch 'master' into bus_plugins
allanon 8c51168
Merge branch 'master' into bus_plugins
allanon 7ea7594
Merge branch 'master' into bus_plugins
allanon 826915a
Merge remote-tracking branch 'origin/master' into bus_plugins
allanon 087120b
bus: code review feedback
allanon 82a07dd
bus_party only sends data when turned on
allanon 4229943
hopefully these are the right paths
allanon 3d9be62
Merge remote-tracking branch 'origin/master' into bus_plugins
allanon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package OpenKore::Plugins::BusCommand; | ||
############################################################################### | ||
# Plugin to allow console commands to be sent via bus. | ||
# Depends on the bus_hook plugin. | ||
|
||
use strict; | ||
|
||
use Globals qw( $char %config $field $net ); | ||
use Utils qw( &existsInList ); | ||
|
||
our $name = 'bus_command'; | ||
|
||
Plugins::register( $name, "$name plugin", \&Unload, \&Unload ); | ||
|
||
my $hooks = Plugins::addHooks( # | ||
[ 'bus/recv/RUN_COMMAND' => \&onBusRecvRunCommand ], | ||
); | ||
|
||
sub Unload { | ||
Plugins::delHooks( $hooks ); | ||
} | ||
|
||
sub onBusRecvRunCommand { | ||
my ( undef, $args ) = @_; | ||
|
||
my $allow = 1; | ||
$allow = 0 if $args->{group} && !check_group( $args->{group} ); | ||
$allow = 0 if $args->{party} && !( $char && $char->{party} && $char->{party}->{name} eq $args->{party} ); | ||
if ( !$allow ) { | ||
Log::debug( "[$name] Received and ignored command: $args->{command}\n", $name ); | ||
return; | ||
} | ||
|
||
Log::debug( "[$name] Received command: $args->{command}\n", $name ); | ||
Commands::run( $args->{command} ); | ||
} | ||
|
||
sub check_group { | ||
my ( $to ) = @_; | ||
|
||
# Support comma-separated groups, like ONLINE,LEADERS. All checks must match. | ||
return !grep { !check_group( $_ ) } split /\s*,\s*/, $to if index( $to, ',' ) > -1; | ||
|
||
return 1 if $to eq 'ALL'; | ||
return 1 if $to eq 'AI=auto' && AI::state == AI::AUTO; | ||
return 1 if $to eq 'AI=manual' && AI::state == AI::MANUAL; | ||
return 1 if $to eq 'AI=off' && AI::state == AI::OFF; | ||
return 1 if $to eq 'ONLINE' && $net->getState == Network::IN_GAME; | ||
return 1 if $to eq 'OFFLINE' && $net->getState != Network::IN_GAME; | ||
return 1 if $to eq 'LEADERS' && !$config{follow}; | ||
return 1 if $to eq 'FOLLOWERS' && $config{follow} && $config{followTarget}; | ||
return 1 if existsInList( $config{bus_command_groups}, $to ); | ||
return 1 if $to =~ /^MAP=(.*)$/o && $field && $1 eq $field->baseName; | ||
return 1 if $to =~ /^(\w+)=(.*)$/o && $config{$1} eq $2; | ||
|
||
return; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package OpenKore::Plugins::BusHook; | ||
############################################################################### | ||
# Plugin to connect to a Bus server and proxy Bus messages through hooks. | ||
|
||
use strict; | ||
|
||
use Bus::Client; | ||
use Log qw( &debug ); | ||
|
||
our $name = 'bus_hook'; | ||
our $bus; | ||
|
||
Plugins::register( $name, "$name plugin", \&Unload, \&Unload ); | ||
|
||
my $hooks = Plugins::addHooks( # | ||
[ 'start3' => \&onStart ], | ||
[ 'mainLoop_pre' => \&onMainLoopPre ], | ||
[ 'bus/send' => \&onBusSend ], | ||
); | ||
|
||
sub Unload { | ||
$bus = undef; | ||
Plugins::delHooks( $hooks ); | ||
} | ||
|
||
sub onStart { | ||
return if $bus; | ||
|
||
$bus = $Globals::bus || Bus::Client->new( userAgent => "$name plugin" ); | ||
$bus->onConnected->add( undef, \&onConnected ); | ||
$bus->onMessageReceived->add( undef, \&onMessageReceived ); | ||
} | ||
|
||
sub onMainLoopPre { | ||
onStart() if !$bus; | ||
$bus->iterate; | ||
} | ||
|
||
sub onBusSend { | ||
my ( undef, $msg ) = @_; | ||
Plugins::callHook( 'bus/sent' => $msg ); | ||
debug "[$name] >> sent $msg->{messageID}\n", $name; | ||
$bus->send( $msg->{messageID}, $msg->{args} ); | ||
} | ||
|
||
# Ask for a list of other clients. | ||
sub onConnected { | ||
debug "[$name] >> connected\n", $name; | ||
onBusSend( undef, { messageID => 'LIST_CLIENTS2' } ); | ||
Plugins::callHook( 'bus/connect' ); | ||
} | ||
|
||
sub onMessageReceived { | ||
my ( undef, undef, $msg ) = @_; | ||
|
||
my $mid = $msg->{messageID}; | ||
my $args = $msg->{args}; | ||
debug "[$name] << received $mid\n", $name; | ||
Plugins::callHook( 'bus/recv' => $msg ); | ||
Plugins::callHook( "bus/recv/$mid" => $args ); | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package OpenKore::Plugins::BusParty; | ||
############################################################################### | ||
# Plugin to update party information via bus. | ||
|
||
use strict; | ||
|
||
use Globals qw( $accountID $char %config $field $net @partyUsersID ); | ||
use Log qw( &debug &message ); | ||
use Utils qw( &binAdd &binFind &binRemove &calcPosition &timeOut ); | ||
use Time::HiRes qw( &time ); | ||
|
||
our $name = 'bus_party'; | ||
our $timeout = { time => 0, timeout => 0.2 }; | ||
our $bus_party ||= {}; | ||
|
||
Plugins::register( $name, "$name plugin", \&Unload, \&Unload ); | ||
|
||
my $hooks = Plugins::addHooks( # | ||
[ 'mainLoop_pre' => \&onMainLoopPre ], | ||
[ 'bus/connect' => \&onBusConnect ], | ||
[ 'bus/recv/PARTY_REQUEST' => \&onBusRecvPartyRequest ], | ||
[ 'bus/recv/PARTY_UPDATE' => \&onBusRecvPartyUpdate ], | ||
[ 'bus/recv/LEAVE' => \&onBusRecvLeave ], | ||
); | ||
|
||
sub Unload { | ||
Plugins::delHooks( $hooks ); | ||
} | ||
|
||
sub onMainLoopPre { | ||
return if !timeOut($timeout); | ||
$timeout->{time} = time; | ||
|
||
my $party_update = partial_party_update(); | ||
return if !%$party_update; | ||
Plugins::callHook( 'bus/send', { messageID => 'PARTY_UPDATE', args => $party_update } ); | ||
} | ||
|
||
sub onBusConnect{ | ||
Plugins::callHook( 'bus/send', { messageID => 'PARTY_REQUEST' } ); | ||
Plugins::callHook( 'bus/send', { messageID => 'PARTY_UPDATE', args => full_party_update() } ); | ||
} | ||
|
||
sub onBusRecvPartyRequest { | ||
my ( undef, $args ) = @_; | ||
Plugins::callHook( 'bus/send', { messageID => 'PARTY_UPDATE', TO => $args->{FROM}, args => full_party_update() } ); | ||
} | ||
|
||
sub onBusRecvPartyUpdate { | ||
my ( undef, $args ) = @_; | ||
my $actor = $bus_party->{ $args->{FROM} } ||= {}; | ||
$actor->{$_} = $args->{$_} foreach keys %$args; | ||
|
||
# Update the party. | ||
my $id = pack 'V', $actor->{id}; | ||
return if !$actor->{id} || $id eq $accountID; | ||
return if !$actor->{name}; | ||
return if !$char; | ||
return if !$char->{party}; | ||
|
||
my $party_user = $char->{party}{users}{$id}; | ||
if ( binFind( \@partyUsersID, $id ) eq '' ) { | ||
binAdd( \@partyUsersID, $id ); | ||
} | ||
if ( !$party_user ) { | ||
$party_user = $char->{party}{users}{$id} = Actor::Party->new; | ||
$party_user->{ID} = $id; | ||
message "[bot_party] Party Member: $args->{name}\n"; | ||
} | ||
|
||
foreach ( qw( name online hp hp_max ) ) { | ||
$party_user->{$_} = $actor->{$_}; | ||
} | ||
$party_user->{bus_party} = $actor->{party}; | ||
$party_user->{map} = "$actor->{map}.gat"; | ||
$party_user->{pos}->{x} = $actor->{x}; | ||
$party_user->{pos}->{y} = $actor->{y}; | ||
} | ||
|
||
sub onBusRecvLeave { | ||
my ( undef, $args ) = @_; | ||
|
||
my $actor = $bus_party->{ $args->{clientID} }; | ||
return if !$actor; | ||
|
||
# Remove the character from $char->{party} if they're not in our actual party. | ||
my $id = pack 'V', $actor->{id}; | ||
if ( $char && $char->{party} && ( !$char->{party}->{name} || $actor->{party} ne $char->{party}->{name} ) && binFind( \@partyUsersID, $id ) ne '' ) { | ||
delete $char->{party}->{users}->{$id}; | ||
binRemove( \@partyUsersID, $id ); | ||
} | ||
|
||
delete $bus_party->{ $args->{clientID} }; | ||
} | ||
|
||
sub full_party_update { | ||
my $update = {}; | ||
$update->{followTarget} = $config{follow} && $config{followTarget} || ''; | ||
if ( $char ) { | ||
my $pos = calcPosition( $char ); | ||
$update->{id} = unpack 'V', $accountID; | ||
$update->{name} = $char->{name}; | ||
$update->{hp} = $char->{hp}; | ||
$update->{hp_max} = $char->{hp_max}; | ||
$update->{sp} = $char->{sp}; | ||
$update->{sp_max} = $char->{sp_max}; | ||
$update->{lv} = $char->{lv}; | ||
$update->{xp} = $char->{exp}; | ||
$update->{xp_max} = $char->{exp_max}; | ||
$update->{jl} = $char->{lv_job}; | ||
$update->{jp} = $char->{exp_job}; | ||
$update->{jp_max} = $char->{exp_job_max}; | ||
$update->{zeny} = $char->{zeny}; | ||
$update->{status} = $char->{statuses} && %{ $char->{statuses} } ? join ', ', keys %{ $char->{statuses} } : ''; | ||
$update->{x} = $pos->{x}; | ||
$update->{y} = $pos->{y}; | ||
$update->{weight} = $char->{weight}; | ||
$update->{weight_max} = $char->{weight_max}; | ||
if ( $char->{party} ) { | ||
$update->{party} = $char->{party}->{name} || ''; | ||
$update->{admin} = $char->{party}->{users}->{$accountID}->{admin} ? 1 : 0; | ||
} | ||
} | ||
if ( $field ) { | ||
$update->{map} = $field->baseName; | ||
} | ||
if ( $net ) { | ||
$update->{online} = $net->getState == Network::IN_GAME ? 1 : 0; | ||
} | ||
$update; | ||
} | ||
|
||
sub partial_party_update { | ||
our $last_update ||= {}; | ||
my $update = full_party_update(); | ||
my $partial = {}; | ||
foreach ( keys %$update ) { | ||
next if $last_update->{$_} eq $update->{$_}; | ||
$partial->{$_} = $update->{$_}; | ||
} | ||
$last_update = $update; | ||
$partial; | ||
} | ||
|
||
1; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think bus_party should be on by default