ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/Deliantra-Client/DC/Main.pm
(Generate patch)

Comparing deliantra/Deliantra-Client/DC/Main.pm (file contents):
Revision 1.7 by root, Sat Dec 31 06:51:29 2011 UTC vs.
Revision 1.16 by root, Wed Jan 18 00:51:41 2012 UTC

18} 18}
19 19
20use common::sense; 20use common::sense;
21use Carp 'verbose'; 21use Carp 'verbose';
22use Cwd (); 22use Cwd ();
23use Digest::MD5 ();
23use EV; 24use EV;
24BEGIN { *time = \&EV::time } 25BEGIN { *time = \&EV::time }
25 26
26use List::Util qw(max min); 27use List::Util qw(max min);
27 28
56 DC::fatal Carp::longmess "$_[0]"; 57 DC::fatal Carp::longmess "$_[0]";
57 } 58 }
58} 59}
59 60
60use DC::OpenGL (); 61use DC::OpenGL ();
62use DC::Audio ();
61use DC::Protocol; 63use DC::Protocol;
62use DC::DB; 64use DC::DB;
63use DC::UI; 65use DC::UI;
64use DC::UI::Canvas; 66use DC::UI::Canvas;
65use DC::UI::Inventory; 67use DC::UI::Inventory;
195 AnyEvent::Util::fh_nonblocking $fh, 1; 197 AnyEvent::Util::fh_nonblocking $fh, 1;
196 198
197 $msg =~ s/\s+$//; 199 $msg =~ s/\s+$//;
198 200
199 # backtrace as second step, in case it crashes, too 201 # backtrace as second step, in case it crashes, too
200 crash Carp::longmess "$msg\nbacktrace, for client version $DC::VERSION, generated" 202 crash Carp::longmess "$msg\nbacktrace, for client version $DC::VERSION$Urlader::EXE_VER, generated"
201 if $backtrace; 203 if $backtrace;
202}; 204};
203 205
204sub clienterror($;$) { 206sub clienterror($;$) {
205 my ($msg, $backtrace) = @_; 207 my ($msg, $backtrace) = @_;
207 warn $msg; 209 warn $msg;
208 210
209 return unless $CONN; 211 return unless $CONN;
210 212
211 $CONN->send_exti_msg (clientlog => $msg); 213 $CONN->send_exti_msg (clientlog => $msg);
212 $CONN->send_exti_msg (clientlog => Carp::longmess "$msg\nbacktrace, for client version $DC::VERSION, generated") if $backtrace; 214 $CONN->send_exti_msg (clientlog => Carp::longmess "$msg\nbacktrace, for client version $DC::VERSION$Urlader::EXE_VER, generated") if $backtrace;
213} 215}
214 216
215############################################################################# 217#############################################################################
216 218
217sub status { 219sub status {
472 audio_music_push; 474 audio_music_push;
473} 475}
474 476
475sub audio_init { 477sub audio_init {
476 if ($CFG->{audio_enable}) { 478 if ($CFG->{audio_enable}) {
477 if (length $CFG->{audio_driver}) { 479 DC::Audio::init $CFG->{audio_driver};
478 local $ENV{SDL_AUDIODRIVER} = $CFG->{audio_driver};
479 DC::SDL_Init DC::SDL_INIT_AUDIO
480 and die "SDL::Init failed!\n";
481 } else {
482 DC::SDL_Init DC::SDL_INIT_AUDIO
483 and die "SDL::Init failed!\n";
484 }
485
486 $ENV{MIX_EFFECTSMAXSPEED} = 1;
487 $SDL_MIXER = !DC::Mix_OpenAudio
488 $CFG->{audio_hw_frequency},
489 DC::MIX_DEFAULT_FORMAT,
490 $CFG->{audio_hw_channels},
491 $CFG->{audio_hw_chunksize};
492 480
493 if ($SDL_MIXER) { 481 if ($SDL_MIXER) {
494 DC::Mix_AllocateChannels $CFG->{audio_mix_channels};
495
496 audio_music_finished; 482 audio_music_finished;
497 } else { 483 } else {
498 status "Unable to open sound device: there will be no sound"; 484 status "Unable to open sound device: there will be no sound";
499 } 485 }
500 } else { 486 } else {
522 508
523 DC::Mix_CloseAudio if $SDL_MIXER; 509 DC::Mix_CloseAudio if $SDL_MIXER;
524 undef $SDL_MIXER; 510 undef $SDL_MIXER;
525 511
526 DC::SDL_QuitSubSystem DC::SDL_INIT_AUDIO; 512 DC::SDL_QuitSubSystem DC::SDL_INIT_AUDIO;
513}
514
515#############################################################################
516# Over-the-air updates
517
518sub ota_update {
519 my ($face, $size, $md5) = @_;
520
521 my $coro = Coro::async_pool {
522 my $override = "$Urlader::EXE_DIR/override";
523
524 $MESSAGE_DIST->add_channel ({
525 id => "ota_update",
526 title => "Update",
527 tooltip => "<b>Software Update Log</b>",
528 });
529
530 $MESSAGE_DIST->message ({ type => "ota_update", markup => "preparing override..." });
531
532 my $fh = Coro::AIO::aio_open "$override.tmp", IO::AIO::O_WRONLY | IO::AIO::O_CREAT | IO::AIO::O_TRUNC, 0777;
533
534 unless ($fh) {
535 $MESSAGE_DIST->message ({ type => "ota_update", markup => (DC::asxml "unable to write software update:\n$Urlader::EXE_DIR/override.tmp:\n$!") });
536 return;
537 }
538
539 $MESSAGE_DIST->message ({ type => "ota_update", markup => "downloading $size bytes..." });
540
541 my $cv = AE::cv;
542 my $error;
543
544 $cv->begin (Coro::rouse_cb);
545 $CONN->ask_face (
546 $face,
547 -1000,
548 sub {
549 $STATUSBOX->add (
550 (sprintf "update download: %d/%d", $size - $_[1], $size),
551 pri => -9, group => "ota_update", timeout => 60, fg => [1, 1, 0, 1]
552 );
553
554 $cv->begin;
555 my $len = length $_[2];
556 IO::AIO::aio_write $fh, $_[1], $len, $_[2], undef, sub {
557 $error ||= $_[0] != $len;
558 $cv->end;
559 };
560 },
561 sub {
562 $cv->end;
563 },
564 );
565
566 Coro::rouse_wait;
567
568 $STATUSBOX->clr_group ("ota_update");
569
570 $error ||= Coro::AIO::aio_fsync $fh;
571 $error ||= Coro::AIO::aio_close $fh;
572
573 if ($error) {
574 $MESSAGE_DIST->message ({ type => "ota_update", markup => "file write error, update aborted." });
575 Coro::AIO::aio_unlink "$override.tmp";
576 return;
577 }
578
579 {
580 $MESSAGE_DIST->message ({ type => "ota_update", markup => "verifying update file..." });
581
582 my $fh = Coro::AIO::aio_open "$override.tmp", IO::AIO::O_RDONLY, 0;
583
584 if ($fh) {
585 $error ||= Coro::AIO::aio_stat "$override.tmp";
586 $error ||= -s _ != $size;
587 Coro::AIO::aio_readahead $fh, 0, $size;
588
589 my $f_md5 = new Digest::MD5;
590 binmode $fh; # ugh :(
591 $f_md5->addfile ($fh);
592 $f_md5 = $f_md5->hexdigest;
593 $error ||= $md5 ne $f_md5;
594 }
595 }
596
597 if ($error) {
598 $MESSAGE_DIST->message ({ type => "ota_update", markup => "verification failed, update aborted." });
599 Coro::AIO::aio_unlink "$override.tmp";
600 return;
601 }
602
603 $MESSAGE_DIST->message ({ type => "ota_update", markup => "replacing override file..." });
604
605 if (Coro::AIO::aio_rename "$override.tmp", $override) {
606 $MESSAGE_DIST->message ({ type => "ota_update", markup => "unable to replace override file, update aborted." });
607 Coro::AIO::aio_unlink "$override.tmp";
608 }
609
610 $MESSAGE_DIST->message ({ type => "ota_update", markup => "success - update becomes active after restarting." });
611 };
612
613 $CONN->{ota_update} = Guard::guard {
614 $coro->cancel;
615 };
616}
617
618sub ota_update_ask {
619 my ($ok, $face, $ver, $size, $md5, $changes) = @_;
620
621 $CONN->{w}{ota_dialog} = my $dialog = new DC::UI::Toplevel
622 x => "center",
623 y => "center",
624 z => 55,
625 force_w => $::WIDTH * 0.7,
626 force_h => $::HEIGHT * 0.7,
627 title => "Software update available",
628 child => my $vbox = new DC::UI::VBox,
629 ;
630
631 $vbox->add (new DC::UI::Label
632 ellipsise => 0,
633 text => "The server offers a software update, "
634 . "do you want to start downloading this update in the background?",
635 );
636
637 $vbox->add (new DC::UI::FancyFrame
638 expand => 1,
639 label => "Details",
640 child => (new DC::UI::TextScroller
641 expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4,
642 par => [{
643 markup => "<tt>Old revision: $Urlader::EXE_VER\n"
644 . "New revision: $ver\n"
645 . "Download size: $size bytes</tt>\n\n"
646 . "Changes:\n\n"
647 . DC::asxml $changes
648 }],
649 ),
650 );
651
652 $vbox->add (my $hbox = new DC::UI::HBox);
653
654 $hbox->add (new DC::UI::Button
655 expand => 1,
656 text => "Not now",
657 on_activate => sub {
658 $dialog->destroy;
659 0
660 }
661 );
662 $hbox->add (new DC::UI::Button
663 expand => 1,
664 text => "Yes, start downloading",
665 on_activate => sub {
666 $dialog->destroy;
667 ota_update $face, $size, $md5;
668 0
669 },
670 );
671
672 $dialog->show;
673}
674
675sub ota_update_check {
676 return unless defined $Urlader::EXE_ID;
677
678 ::message { markup => "Checking for software update..." };
679
680 $CONN->send_exti_req (ota_update => $Urlader::URLADER_VERSION, $Urlader::EXE_ID, $Urlader::EXE_VER, sub {
681 my ($ok, $face, $ver, $size, $md5, $changes) = @_;
682
683 if ($ok) {
684 if (defined $ver) {
685 ::message { markup => "Server offers version $ver (we are version $Urlader::EXE_VER)." };
686 &ota_update_ask;
687 } else {
688 ::message { markup => "Server has no newer version." };
689 }
690 } else {
691 ::message { markup => "Server does not support software update." };
692 }
693
694# $self->register_face_handler ($exp_table, sub {
695# my ($face) = @_;
696
697# $self->{exp_table} = $self->{json_coder}->decode (delete $face->{data});
698# $_->() for values %{ $self->{on_exp_update} || {} };
699# });
700
701 ()
702 });
527} 703}
528 704
529############################################################################# 705#############################################################################
530 706
531sub destroy_query_dialog { 707sub destroy_query_dialog {
774 mapw => $mapw, 950 mapw => $mapw,
775 maph => $maph, 951 maph => $maph,
776 952
777 c_version => { 953 c_version => {
778 client => "deliantra", 954 client => "deliantra",
779 clientver => $DC::VERSION, 955 clientver => "$DC::VERSION$Urlader::EXE_VER",
780 gl_vendor => DC::OpenGL::gl_vendor, 956 gl_vendor => DC::OpenGL::gl_vendor,
781 gl_version => DC::OpenGL::gl_version, 957 gl_version => DC::OpenGL::gl_version,
782 }, 958 },
783 959
784 map_widget => $MAPWIDGET, 960 map_widget => $MAPWIDGET,
792 }, 968 },
793 969
794 on_connect => sub { 970 on_connect => sub {
795 if ($_[0]) { 971 if ($_[0]) {
796 DC::lowdelay fileno $CONN->{fh}; 972 DC::lowdelay fileno $CONN->{fh};
973
974 ota_update_check;
797 975
798 status "successfully connected to the server"; 976 status "successfully connected to the server";
799 } else { 977 } else {
800 undef $CONN; 978 undef $CONN;
801 status "unable to connect: $!"; 979 status "unable to connect: $!";
1855 $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $DC::DB::DBDIR, tooltip => ""); 2033 $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $DC::DB::DBDIR, tooltip => "");
1856 $table->add_at (0, $row , new DC::UI::Label align => 1, text => "Urlader (Prebuilt)"); 2034 $table->add_at (0, $row , new DC::UI::Label align => 1, text => "Urlader (Prebuilt)");
1857 $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $ENV{URLADER_VERSION}, tooltip => ""); 2035 $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $ENV{URLADER_VERSION}, tooltip => "");
1858 $table->add_at (0, $row , new DC::UI::Label align => 1, text => "Branch (Prebuilt)"); 2036 $table->add_at (0, $row , new DC::UI::Label align => 1, text => "Branch (Prebuilt)");
1859 $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $ENV{URLADER_EXE_ID}, tooltip => ""); 2037 $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $ENV{URLADER_EXE_ID}, tooltip => "");
1860 $table->add_at (0, $row , new DC::UI::Label align => 1, text => "Version (Prebuilt)"); 2038 $table->add_at (0, $row , new DC::UI::Label align => 1, text => "Revision (Prebuilt)");
1861 $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $ENV{URLADER_EXE_VER}, tooltip => ""); 2039 $table->add_at (1, $row++, new DC::UI::Label align => 0, text => $ENV{URLADER_EXE_VER}, tooltip => "");
1862 } 2040 }
1863 2041
1864 $vbox 2042 $vbox
1865} 2043}
2279sub open_quit_dialog { 2457sub open_quit_dialog {
2280 unless ($QUIT_DIALOG) { 2458 unless ($QUIT_DIALOG) {
2281 $QUIT_DIALOG = new DC::UI::Toplevel 2459 $QUIT_DIALOG = new DC::UI::Toplevel
2282 x => "center", 2460 x => "center",
2283 y => "center", 2461 y => "center",
2284 z => 50, 2462 z => 60,
2285 title => "Really Quit?", 2463 title => "Really Quit?",
2286 on_key_down => sub { 2464 on_key_down => sub {
2287 my ($dialog, $ev) = @_; 2465 my ($dialog, $ev) = @_;
2288 $ev->{sym} == 27 and $dialog->hide; 2466 $ev->{sym} == 27 and $dialog->hide;
2289 } 2467 }
2505 my ($old_w, $old_h) = ($WIDTH, $HEIGHT); 2683 my ($old_w, $old_h) = ($WIDTH, $HEIGHT);
2506 2684
2507 ($WIDTH, $HEIGHT, my ($rgb, $alpha)) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; 2685 ($WIDTH, $HEIGHT, my ($rgb, $alpha)) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
2508 $FULLSCREEN = $CFG->{fullscreen}; 2686 $FULLSCREEN = $CFG->{fullscreen};
2509 $FAST = $CFG->{fast}; 2687 $FAST = $CFG->{fast};
2688
2689 DC::SDL_WM_SetCaption "Deliantra MORPG Client $DC::VERSION$Urlader::EXE_VER", "Deliantra"; # must be after SDL_Init
2510 2690
2511 # due to mac os x braindamage, we simply retry with !fullscreen in case of an error 2691 # due to mac os x braindamage, we simply retry with !fullscreen in case of an error
2512 DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, $FULLSCREEN 2692 DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, $FULLSCREEN
2513 or DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, !$FULLSCREEN 2693 or DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, !$FULLSCREEN
2514 or die "SDL_SetVideoMode failed: " . (DC::SDL_GetError) . "\n"; 2694 or die "SDL_SetVideoMode failed: " . (DC::SDL_GetError) . "\n";
2793 DC::DB::nuke_db; 2973 DC::DB::nuke_db;
2794 $CFG->{db_schema} = 1; 2974 $CFG->{db_schema} = 1;
2795 DC::write_cfg; 2975 DC::write_cfg;
2796 } 2976 }
2797 2977
2978 DC::upgrade_cfg;
2979
2798 DC::DB::open_db; 2980 DC::DB::open_db;
2799 2981
2800 DC::UI::set_layout ($::CFG->{layout}); 2982 DC::UI::set_layout ($::CFG->{layout});
2801
2802 my %DEF_CFG = (
2803 config_autosave => 1,
2804 sdl_mode => undef,
2805 fullscreen => 1,
2806 fast => 0,
2807 force_opengl11 => undef,
2808 disable_alpha => 0,
2809 smooth_movement => 1,
2810 smooth_transitions => 1,
2811 texture_compression => 1,
2812 map_scale => 1,
2813 fow_enable => 1,
2814 fow_intensity => 0,
2815 fow_texture => 0,
2816 map_smoothing => 1,
2817 gui_fontsize => 1,
2818 log_fontsize => 0.7,
2819 gauge_fontsize => 1,
2820 gauge_size => 0.35,
2821 stat_fontsize => 0.7,
2822 mapsize => 100,
2823 audio_enable => 1,
2824 audio_hw_channels => 0,
2825 audio_hw_frequency => 0,
2826 audio_hw_chunksize => 0,
2827 audio_mix_channels => 8,
2828 effects_enable => 1,
2829 effects_volume => 1,
2830 bgm_enable => 1,
2831 bgm_volume => 0.5,
2832 output_rate => "",
2833 pickup => PICKUP_SPELLBOOK | PICKUP_SKILLSCROLL | PICKUP_VALUABLES,
2834 inv_sort => "mtime",
2835 default => "profile", # default profile
2836 show_tips => 1,
2837 logview_max_par => 1000,
2838 shift_fire_stop => 0,
2839 uitheme => "wood",
2840 map_shift_x => -24, # arbitrary
2841 map_shift_y => +24, # arbitrary
2842 );
2843
2844 while (my ($k, $v) = each %DEF_CFG) {
2845 $CFG->{$k} = $v unless exists $CFG->{$k};
2846 }
2847 2983
2848 my @args = @ARGV; 2984 my @args = @ARGV;
2849 2985
2850 # OS X passes some process serial number of other shit. they 2986 # OS X passes some process serial number of other shit. they
2851 # could have used an env var or any other sane mechanism. but 2987 # could have used an env var or any other sane mechanism. but

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines