--- deliantra/Deliantra-Client/DC/Protocol.pm 2007/06/26 05:06:01 1.110 +++ deliantra/Deliantra-Client/DC/Protocol.pm 2007/07/19 16:44:14 1.123 @@ -23,7 +23,7 @@ setup_req => { extmap => 1, excmd => 1, - xwidget => 1, + ywidget => 1, %{$arg{setup_req} || {}}, }, ); @@ -50,6 +50,60 @@ } sort { $a->{par} <=> $b->{par} } CFPlus::Pod::find command => "*"; + $self->connect_ext (event_music => sub { + my ($ev) = @_; + + return unless $::CFG->{bgm_enable}; + + my $faces = $ev->{faces}; + my @songs; + + # request music from server if appropriate + if (my $faces = $ev->{faces}) { + for (@$faces) { + my ($face, $pri, $chksum) = @$_; + utf8::downgrade $chksum; + + $chksum = unpack "H*", $chksum; + $self->{music_map}{$face} = $chksum; + + # check if already cached + CFPlus::DB::get "res-metadata" => $chksum, sub { + my ($meta_json) = @_; + my $path = CFPlus::DB::path_of_res $chksum; + + if (defined $meta_json) { + my $meta = $self->{json_coder}->decode ($meta_json); + $meta->{path} = $path; + $self->{music_meta}{$chksum} = $meta; + &::audio_music_set ($self->{songs}); + } else { + ::message ({ markup => "starting to download song #$face/$pri, check your output-rate setting if your connection gets laggy." }); + $self->ask_face ($face, -100 - $pri, undef, sub { + my $len = length $_[1]; + my ($meta_json, $data) = unpack "(w/a*)*", $_[1]; + + CFPlus::DB::write_file $path, $data, sub { + CFPlus::DB::put "res-metadata" => $chksum => $meta_json, sub { + my $meta = $self->{json_coder}->decode ($meta_json); + $meta->{path} = $path; + $self->{music_meta}{$chksum} = $meta; + + ::message ({ markup => "downloaded song #$face, size $len octets" }); + &::audio_music_set ($self->{songs}); + }; + }; + }); + } + } + } + } + + if (my $play = $ev->{play}) { + &::audio_music_set ($self->{songs} = [map $self->{music_map}{$_}, @$play]); + } + }); + $self->connect_ext (event_capabilities => sub { my ($cap) = @_; @@ -67,6 +121,13 @@ } }); + $self->{json_coder} + ->convert_blessed + ->filter_json_single_key_object (__widget_ref__ => sub { + $self->{widget}{$_[0]} + }); + + # widgetset new $self->connect_ext (ws_n => sub { my ($arg) = @_; @@ -75,6 +136,7 @@ }; }); + # widgetset destroy $self->connect_ext (ws_d => sub { my ($arg) = @_; @@ -85,6 +147,7 @@ for values %{$ws->{w}}; }); + # widgetset destroy $self->connect_ext (ws_c => sub { my ($arg) = @_; @@ -124,6 +187,7 @@ } }); + # widget call $self->connect_ext (w_c => sub { my ($arg) = @_; @@ -131,19 +195,16 @@ or return; my $m = $arg->{name}; - my @a = map { - "HASH" eq ref && 1 == (scalar keys %$_) && exists $_->{__widget_ref__} - ? $self->{widget}{$_->{__widget_ref__}} - : $_ - } @{ $arg->{args} || [] }; + my $a = $arg->{args} || []; if (exists $arg->{rid}) { - $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [$w->$m (@a)]); + $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [$w->$m (@$a)]); } else { - $w->$m (@a); + $w->$m (@$a); } }); + # widget set $self->connect_ext (w_s => sub { my ($arg) = @_; @@ -153,6 +214,7 @@ $w->{$arg->{name}} = $arg->{value}; }); + # widget get $self->connect_ext (w_g => sub { my ($arg) = @_; @@ -162,6 +224,7 @@ $self->send_exti_msg (w_r => rid => $arg->{rid}, res => [$w->{$arg->{name}}]); }); + # destroy widgets on logout $self->{on_stop_game_guard} = $self->{map_widget}{root}->connect (stop_game => sub { for my $ws (values %{delete $self->{widgetset} || {}}) { $_->destroy @@ -712,12 +775,28 @@ sub set_texture { my ($self, $id, $data) = @_; + return unless $self->{map}; + $self->{texture}[$id] = my $tex = new_from_image CFPlus::Texture $data, minify => 1, mipmap => 1; $self->{map}->set_texture ($id, @$tex{qw(name w h s t)}, @{$tex->{minified}}); $self->{map_widget}->update; + + $_->() for @{(delete $self->{face_cb}{$id}) || []}; +} + +sub connect_face_update { + my ($self, $id, $cb) = @_; + + push @{$self->{face_cb}{$id}}, $cb; + + CFPlus::guard { + @{$self->{face_cb}{$id}} + = grep $_ != $cb, + @{$self->{face_cb}{$id}}; + } } sub sound_play { @@ -1132,6 +1211,8 @@ if $self->{npc_dialog}; $self->SUPER::destroy; + + %$self = (); } package CFPlus::NPCDialog;