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.8 by root, Wed Jan 4 11:23:23 2012 UTC vs.
Revision 1.17 by root, Wed Jan 18 13:42:19 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 {
527} 513}
528 514
529############################################################################# 515#############################################################################
530# Over-the-air updates 516# Over-the-air updates
531 517
532sub ota_update_finish {
533 $MESSAGE_DIST->message ({ type => "ota_update", markup => $_[0] });
534}
535
536sub ota_update_status {
537}
538
539sub ota_update { 518sub ota_update {
540 my ($face, $size) = @_; 519 my ($face, $size, $md5) = @_;
541 520
542 my $coro = Coro::async_pool { 521 my $coro = Coro::async_pool {
543 my $override = "$Urlader::EXE_DIR/override"; 522 my $override = "$Urlader::EXE_DIR/override";
544 523
545 $MESSAGE_DIST->add_channel ({ 524 $MESSAGE_DIST->add_channel ({
551 $MESSAGE_DIST->message ({ type => "ota_update", markup => "preparing override..." }); 530 $MESSAGE_DIST->message ({ type => "ota_update", markup => "preparing override..." });
552 531
553 my $fh = Coro::AIO::aio_open "$override.tmp", IO::AIO::O_WRONLY | IO::AIO::O_CREAT | IO::AIO::O_TRUNC, 0777; 532 my $fh = Coro::AIO::aio_open "$override.tmp", IO::AIO::O_WRONLY | IO::AIO::O_CREAT | IO::AIO::O_TRUNC, 0777;
554 533
555 unless ($fh) { 534 unless ($fh) {
556 ota_update_finish "unable to write software update:\n$Urlader::EXE_DIR/override.tmp:\n$!"; 535 $MESSAGE_DIST->message ({ type => "ota_update", markup => (DC::asxml "unable to write software update:\n$Urlader::EXE_DIR/override.tmp:\n$!") });
557 return; 536 return;
558 } 537 }
559 538
560 $MESSAGE_DIST->message ({ type => "ota_update", markup => "downloading $size bytes..." }); 539 $MESSAGE_DIST->message ({ type => "ota_update", markup => "downloading $size bytes..." });
561 540
562 my $cv = AE::cv; 541 my $cv = AE::cv;
563 my $error; 542 my $error;
564 543
565 $cv->begin (Coro::rouse_cb); 544 $cv->begin (Coro::rouse_cb);
566
567 $CONN->ask_face ( 545 $CONN->ask_face (
568 $face, 546 $face,
569 -1000, 547 -1000,
570 sub { 548 sub {
571 $STATUSBOX->add ( 549 $STATUSBOX->add (
596 $MESSAGE_DIST->message ({ type => "ota_update", markup => "file write error, update aborted." }); 574 $MESSAGE_DIST->message ({ type => "ota_update", markup => "file write error, update aborted." });
597 Coro::AIO::aio_unlink "$override.tmp"; 575 Coro::AIO::aio_unlink "$override.tmp";
598 return; 576 return;
599 } 577 }
600 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
601 $MESSAGE_DIST->message ({ type => "ota_update", markup => "replacing override file..." }); 603 $MESSAGE_DIST->message ({ type => "ota_update", markup => "replacing override file..." });
602 604
603 if (Coro::AIO::aio_rename "$override.tmp", $override) { 605 if (Coro::AIO::aio_rename "$override.tmp", $override) {
604 $MESSAGE_DIST->message ({ type => "ota_update", markup => "unable to replace override file, update aborted." }); 606 $MESSAGE_DIST->message ({ type => "ota_update", markup => "unable to replace override file, update aborted." });
605 Coro::AIO::aio_unlink "$override.tmp"; 607 Coro::AIO::aio_unlink "$override.tmp";
606 } 608 }
607 609
608 $MESSAGE_DIST->message ({ type => "ota_update", markup => "update successfull, changes become active at next start." }); 610 $MESSAGE_DIST->message ({ type => "ota_update", markup => "success - update becomes active after restarting." });
609 }; 611 };
610 612
611 $CONN->{ota_update} = Guard::guard { 613 $CONN->{ota_update} = Guard::guard {
612 $coro->cancel; 614 $coro->cancel;
613 }; 615 };
614} 616}
615 617
616sub ota_update_ask { 618sub ota_update_ask {
617 my ($ok, $face, $ver, $size, $changes) = @_; 619 my ($ok, $face, $ver, $size, $md5, $changes) = @_;
618 620
619 $CONN->{w}{ota_dialog} = my $dialog = new DC::UI::Toplevel 621 $CONN->{w}{ota_dialog} = my $dialog = new DC::UI::Toplevel
620 x => "center", 622 x => "center",
621 y => "center", 623 y => "center",
624 z => 55,
622 max_w => $::WIDTH * 0.7, 625 force_w => $::WIDTH * 0.7,
623 max_h => $::WIDTH * 0.7, 626 force_h => $::HEIGHT * 0.7,
624 title => "Software update available", 627 title => "Software update available",
625 child => my $vbox = new DC::UI::VBox, 628 child => my $vbox = new DC::UI::VBox,
626 ; 629 ;
627 630
628 $vbox->add (new DC::UI::Label 631 $vbox->add (new DC::UI::Label
629 ellipsise => 0, 632 ellipsise => 0,
630 text => "The server offers a software update, " 633 text => "The server offers a software update, "
631 . "do you want to start downloading this update in the background?", 634 . "do you want to start downloading this update in the background?",
632 ); 635 );
633 636
634 $vbox->add (new DC::UI::FancyFrame 637 $vbox->add (new DC::UI::FancyFrame
635 expand => 1, 638 expand => 1,
636 label => "Changes", 639 label => "Details",
637 child => (new DC::UI::TextScroller 640 child => (new DC::UI::TextScroller
638 expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4, 641 expand => 1, fontsize => 0.8, padding_x => 4, padding_y => 4,
639 par => [{ 642 par => [{
640 markup => "<tt>Old revision: $Urlader::EXE_VER\n" 643 markup => "<tt>Old revision: $Urlader::EXE_VER\n"
641 . "New revision: $ver\n" 644 . "New revision: $ver\n"
642 . "Download size: $size bytes</tt>\n\n" 645 . "Download size: $size bytes</tt>\n\n"
646 . "Changes:\n\n"
643 . DC::asxml $changes 647 . DC::asxml $changes
644 }], 648 }],
645 ), 649 ),
646 ); 650 );
647 651
658 $hbox->add (new DC::UI::Button 662 $hbox->add (new DC::UI::Button
659 expand => 1, 663 expand => 1,
660 text => "Yes, start downloading", 664 text => "Yes, start downloading",
661 on_activate => sub { 665 on_activate => sub {
662 $dialog->destroy; 666 $dialog->destroy;
663 ota_update $face, $size; 667 ota_update $face, $size, $md5;
664 0 668 0
665 }, 669 },
666 ); 670 );
667 671
668 $dialog->show; 672 $dialog->show;
672 return unless defined $Urlader::EXE_ID; 676 return unless defined $Urlader::EXE_ID;
673 677
674 ::message { markup => "Checking for software update..." }; 678 ::message { markup => "Checking for software update..." };
675 679
676 $CONN->send_exti_req (ota_update => $Urlader::URLADER_VERSION, $Urlader::EXE_ID, $Urlader::EXE_VER, sub { 680 $CONN->send_exti_req (ota_update => $Urlader::URLADER_VERSION, $Urlader::EXE_ID, $Urlader::EXE_VER, sub {
677 my ($ok, $face, $ver, $size, $changes) = @_; 681 my ($ok, $face, $ver, $size, $md5, $changes) = @_;
678 682
679 if ($ok) { 683 if ($ok) {
680 if (defined $ver) { 684 if (defined $ver) {
681 ::message { markup => "Server offers version $ver (we are version $Urlader::EXE_VER)." }; 685 ::message { markup => "Server offers version $ver (we are version $Urlader::EXE_VER)." };
682 &ota_update_ask; 686 &ota_update_ask;
946 mapw => $mapw, 950 mapw => $mapw,
947 maph => $maph, 951 maph => $maph,
948 952
949 c_version => { 953 c_version => {
950 client => "deliantra", 954 client => "deliantra",
951 clientver => $DC::VERSION, 955 clientver => "$DC::VERSION$Urlader::EXE_VER",
952 gl_vendor => DC::OpenGL::gl_vendor, 956 gl_vendor => DC::OpenGL::gl_vendor,
953 gl_version => DC::OpenGL::gl_version, 957 gl_version => DC::OpenGL::gl_version,
954 }, 958 },
955 959
956 map_widget => $MAPWIDGET, 960 map_widget => $MAPWIDGET,
2010 $table->add_at (2, $row++, new DC::UI::Button 2014 $table->add_at (2, $row++, new DC::UI::Button
2011 text => "Save Now", 2015 text => "Save Now",
2012 tooltip => "Use this to manually save configuration and UI layout when " 2016 tooltip => "Use this to manually save configuration and UI layout when "
2013 . "autosave is disabled.", 2017 . "autosave is disabled.",
2014 on_activate => sub { 2018 on_activate => sub {
2015 DC::write_cfg; 2019 DC::save_cfg;
2016 0 2020 0
2017 } 2021 }
2018 ); 2022 );
2019 } 2023 }
2020 2024
2453sub open_quit_dialog { 2457sub open_quit_dialog {
2454 unless ($QUIT_DIALOG) { 2458 unless ($QUIT_DIALOG) {
2455 $QUIT_DIALOG = new DC::UI::Toplevel 2459 $QUIT_DIALOG = new DC::UI::Toplevel
2456 x => "center", 2460 x => "center",
2457 y => "center", 2461 y => "center",
2458 z => 50, 2462 z => 60,
2459 title => "Really Quit?", 2463 title => "Really Quit?",
2460 on_key_down => sub { 2464 on_key_down => sub {
2461 my ($dialog, $ev) = @_; 2465 my ($dialog, $ev) = @_;
2462 $ev->{sym} == 27 and $dialog->hide; 2466 $ev->{sym} == 27 and $dialog->hide;
2463 } 2467 }
2679 my ($old_w, $old_h) = ($WIDTH, $HEIGHT); 2683 my ($old_w, $old_h) = ($WIDTH, $HEIGHT);
2680 2684
2681 ($WIDTH, $HEIGHT, my ($rgb, $alpha)) = @{ $SDL_MODES[$CFG->{sdl_mode}] }; 2685 ($WIDTH, $HEIGHT, my ($rgb, $alpha)) = @{ $SDL_MODES[$CFG->{sdl_mode}] };
2682 $FULLSCREEN = $CFG->{fullscreen}; 2686 $FULLSCREEN = $CFG->{fullscreen};
2683 $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
2684 2690
2685 # 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
2686 DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, $FULLSCREEN 2692 DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, $FULLSCREEN
2687 or DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, !$FULLSCREEN 2693 or DC::SDL_SetVideoMode $WIDTH, $HEIGHT, $rgb, $alpha, !$FULLSCREEN
2688 or die "SDL_SetVideoMode failed: " . (DC::SDL_GetError) . "\n"; 2694 or die "SDL_SetVideoMode failed: " . (DC::SDL_GetError) . "\n";
2950# due to mac os x + sdl combined braindamage, we need this contortion 2956# due to mac os x + sdl combined braindamage, we need this contortion
2951sub DC::Main::main { 2957sub DC::Main::main {
2952 { 2958 {
2953 DC::Pod::load_docwiki DC::find_rcfile "docwiki.pst"; 2959 DC::Pod::load_docwiki DC::find_rcfile "docwiki.pst";
2954 2960
2955 if (-e "$Deliantra::VARDIR/client.cf") { 2961 DC::load_cfg;
2956 DC::read_cfg "$Deliantra::VARDIR/client.cf";
2957 } else {
2958 #TODO: compatibility cruft
2959 DC::read_cfg "$Deliantra::OLDDIR/cfplusrc";
2960 print STDERR "INFO: used old configuration file\n";
2961 }
2962 2962
2963 DC::DB::Server::run; 2963 DC::DB::Server::run;
2964 2964
2965 if ($CFG->{db_schema} < 1) { 2965 if ($CFG->{db_schema} < 1) {
2966 warn "INFO: upgrading database schema from 0 to 1, mapcache and tilecache will be lost\n"; 2966 warn "INFO: upgrading database schema from 0 to 1, mapcache and tilecache will be lost\n";
2967 DC::DB::nuke_db; 2967 DC::DB::nuke_db;
2968 $CFG->{db_schema} = 1; 2968 $CFG->{db_schema} = 1;
2969 DC::write_cfg; 2969 DC::save_cfg;
2970 } 2970 }
2971
2972 DC::upgrade_cfg;
2971 2973
2972 DC::DB::open_db; 2974 DC::DB::open_db;
2973 2975
2974 DC::UI::set_layout ($::CFG->{layout}); 2976 DC::UI::set_layout ($::CFG->{layout});
2975
2976 my %DEF_CFG = (
2977 config_autosave => 1,
2978 sdl_mode => undef,
2979 fullscreen => 1,
2980 fast => 0,
2981 force_opengl11 => undef,
2982 disable_alpha => 0,
2983 smooth_movement => 1,
2984 smooth_transitions => 1,
2985 texture_compression => 1,
2986 map_scale => 1,
2987 fow_enable => 1,
2988 fow_intensity => 0,
2989 fow_texture => 0,
2990 map_smoothing => 1,
2991 gui_fontsize => 1,
2992 log_fontsize => 0.7,
2993 gauge_fontsize => 1,
2994 gauge_size => 0.35,
2995 stat_fontsize => 0.7,
2996 mapsize => 100,
2997 audio_enable => 1,
2998 audio_hw_channels => 0,
2999 audio_hw_frequency => 0,
3000 audio_hw_chunksize => 0,
3001 audio_mix_channels => 8,
3002 effects_enable => 1,
3003 effects_volume => 1,
3004 bgm_enable => 1,
3005 bgm_volume => 0.5,
3006 output_rate => "",
3007 pickup => PICKUP_SPELLBOOK | PICKUP_SKILLSCROLL | PICKUP_VALUABLES,
3008 inv_sort => "mtime",
3009 default => "profile", # default profile
3010 show_tips => 1,
3011 logview_max_par => 1000,
3012 shift_fire_stop => 0,
3013 uitheme => "wood",
3014 map_shift_x => -24, # arbitrary
3015 map_shift_y => +24, # arbitrary
3016 );
3017
3018 while (my ($k, $v) = each %DEF_CFG) {
3019 $CFG->{$k} = $v unless exists $CFG->{$k};
3020 }
3021 2977
3022 my @args = @ARGV; 2978 my @args = @ARGV;
3023 2979
3024 # OS X passes some process serial number of other shit. they 2980 # OS X passes some process serial number of other shit. they
3025 # could have used an env var or any other sane mechanism. but 2981 # could have used an env var or any other sane mechanism. but
3116 debug_toggle 0; 3072 debug_toggle 0;
3117 3073
3118 delete $SIG{__DIE__}; 3074 delete $SIG{__DIE__};
3119 EV::loop; 3075 EV::loop;
3120 3076
3121 DC::write_cfg if $CFG->{config_autosave}; 3077 DC::save_cfg if $CFG->{config_autosave};
3122 3078
3123 #video_shutdown; 3079 #video_shutdown;
3124 #audio_shutdown; 3080 #audio_shutdown;
3125 3081
3126 DC::OpenGL::quit; 3082 DC::OpenGL::quit;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines