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

Comparing deliantra/Deliantra-Client/DC.pm (file contents):
Revision 1.175 by root, Mon Mar 24 00:24:46 2008 UTC vs.
Revision 1.214 by root, Wed Jan 18 00:51:22 2012 UTC

14 14
15package DC; 15package DC;
16 16
17use Carp (); 17use Carp ();
18 18
19our $VERSION;
20
19BEGIN { 21BEGIN {
20 $VERSION = '0.9967'; 22 $VERSION = '3.0';
21 23
22 use XSLoader; 24 use XSLoader;
23 XSLoader::load "Deliantra::Client", $VERSION; 25 XSLoader::load "Deliantra::Client", $VERSION;
24} 26}
25 27
26use utf8; 28use utf8;
29use strict qw(vars subs);
27 30
31use Socket ();
28use AnyEvent (); 32use AnyEvent ();
33use AnyEvent::Util ();
29use Pod::POM (); 34use Pod::POM ();
30use File::Path (); 35use File::Path ();
31use Storable (); # finally 36use Storable (); # finally
32use Fcntl (); 37use Fcntl ();
33use JSON::XS qw(encode_json decode_json); 38use JSON::XS qw(encode_json decode_json);
39use Guard qw(guard);
34 40
35=item guard { BLOCK } 41# modules to support other DC::* packages
42use List::Util ();
43use IO::AIO ();
44use Coro::AIO ();
45use AnyEvent::AIO ();
36 46
37Returns an object that executes the given block as soon as it is destroyed. 47use Deliantra::Protocol::Constants ();
38
39=cut
40
41sub guard(&) {
42 bless \(my $cb = $_[0]), "DC::Guard"
43}
44
45sub DC::Guard::DESTROY {
46 ${$_[0]}->()
47}
48 48
49=item shorten $string[, $maxlength] 49=item shorten $string[, $maxlength]
50 50
51=cut 51=cut
52 52
64 s/</&lt;/g; 64 s/</&lt;/g;
65 65
66 $_ 66 $_
67} 67}
68 68
69sub socketpipe() {
70 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
71 or die "cannot establish bidirectional pipe: $!\n";
72
73 ($fh1, $fh2)
74}
75
76sub background(&;&) { 69sub background(&;&) {
77 my ($bg, $cb) = @_; 70 my ($bg, $cb) = @_;
78 71
79 my ($fh_r, $fh_w) = DC::socketpipe; 72 my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair
73 or die "unable to create background socketpair: $!";
80 74
81 my $pid = fork; 75 my $pid = fork;
82 76
83 if (defined $pid && !$pid) { 77 if (defined $pid && !$pid) {
84 local $SIG{__DIE__}; 78 local $SIG{__DIE__};
140 print $msg, "\n"; 134 print $msg, "\n";
141} 135}
142 136
143package DC; 137package DC;
144 138
139our $RC_THEME;
140our %THEME;
141our @RC_PATH;
142our $RC_BASE;
143
144for (grep !ref, @INC) {
145 $RC_BASE = "$_/Deliantra/Client/private/resources";
146 last if -d $RC_BASE;
147}
148
145sub find_rcfile($) { 149sub find_rcfile($) {
146 my $path; 150 my $path;
147 151
148 for (grep !ref, @INC) { 152 for (@RC_PATH, "") {
149 $path = "$_/Deliantra/Client/private/resources/$_[0]"; 153 $path = "$RC_BASE/$_/$_[0]";
150 return $path if -r $path; 154 return $path if -e $path;
151 } 155 }
152 156
153 die "FATAL: can't find required file $_[0]\n"; 157 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n";
158}
159
160sub load_json($) {
161 my ($file) = @_;
162
163 open my $fh, $file
164 or return;
165
166 local $/;
167 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
168}
169
170sub set_theme($) {
171 return if $RC_THEME eq $_[0];
172 $RC_THEME = $_[0];
173
174 # kind of hacky, find the main theme file, then load all theme files and merge them
175
176 %THEME = ();
177 @RC_PATH = "theme-$RC_THEME";
178
179 my $theme = load_json find_rcfile "theme.json"
180 or die "FATAL: theme resource file not found";
181
182 @RC_PATH = @{ $theme->{path} } if $theme->{path};
183
184 for (@RC_PATH, "") {
185 my $theme = load_json "$RC_BASE/$_/theme.json"
186 or next;
187
188 %THEME = ( %$theme, %THEME );
189 }
154} 190}
155 191
156sub read_cfg { 192sub read_cfg {
157 my ($file) = @_; 193 my ($file) = @_;
158 194
195 $::CFG = (load_json $file) || (load_json "$file.bak");
196}
197
198sub write_cfg {
199 my $file = "$Deliantra::VARDIR/client.cf";
200
201 $::CFG->{VERSION} = $::VERSION;
202 $::CFG->{layout} = DC::UI::get_layout ();
203
159 open my $fh, $file 204 open my $fh, ">:utf8", "$file~"
160 or return; 205 or return;
206 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
207 close $fh;
161 208
162 local $/; 209 rename $file, "$file.bak";
163 my $CFG = <$fh>; 210 rename "$file~", $file;
164
165 $::CFG = decode_json $CFG;
166} 211}
167 212
168sub write_cfg { 213sub upgrade_cfg() {
169 my ($file) = @_; 214 my %DEF_CFG = (
215 config_autosave => 1,
216 sdl_mode => undef,
217 fullscreen => 1,
218 fast => 0,
219 force_opengl11 => undef,
220 disable_alpha => 0,
221 smooth_movement => 1,
222 smooth_transitions => 1,
223 texture_compression => 1,
224 map_scale => 1,
225 fow_enable => 1,
226 fow_intensity => 0,
227 fow_texture => 0,
228 map_smoothing => 1,
229 gui_fontsize => 1,
230 log_fontsize => 0.7,
231 gauge_fontsize => 1,
232 gauge_size => 0.35,
233 stat_fontsize => 0.7,
234 mapsize => 100,
235 audio_enable => 1,
236 audio_hw_channels => 0,
237 audio_hw_frequency => 0,
238 audio_hw_chunksize => 0,
239 audio_mix_channels => 8,
240 effects_enable => 1,
241 effects_volume => 1,
242 bgm_enable => 1,
243 bgm_volume => 0.5,
244 output_rate => "",
245 pickup => Deliantra::Protocol::Constants::PICKUP_SPELLBOOK
246 | Deliantra::Protocol::Constants::PICKUP_SKILLSCROLL
247 | Deliantra::Protocol::Constants::PICKUP_VALUABLES,
248 inv_sort => "mtime",
249 default => "profile", # default profile
250 show_tips => 1,
251 logview_max_par => 1000,
252 shift_fire_stop => 0,
253 uitheme => "wood",
254 map_shift_x => -24, # arbitrary
255 map_shift_y => +24, # arbitrary
256 #db_schema => 0,
257 );
170 258
171 $::CFG->{VERSION} = $::VERSION; 259 while (my ($k, $v) = each %DEF_CFG) {
172 260 $::CFG->{$k} = $v unless exists $::CFG->{$k};
173 open my $fh, ">:utf8", $file 261 }
174 or return;
175 print $fh encode_json $::CFG;
176} 262}
177 263
178sub http_proxy { 264sub http_proxy {
179 my @proxy = win32_proxy_info; 265 my @proxy = win32_proxy_info;
180 266
223 $nb = (! ! $nb) + 0; 309 $nb = (! ! $nb) + 0;
224 ioctl $fh, 0x8004667e, \$nb; # FIONBIO 310 ioctl $fh, 0x8004667e, \$nb; # FIONBIO
225 } else { 311 } else {
226 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0; 312 fcntl $fh, &Fcntl::F_SETFL, $nb ? &Fcntl::O_NONBLOCK : 0;
227 } 313 }
228
229} 314}
230 315
231package DC::Layout; 316package DC::Layout;
232 317
233$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub { 318$DC::OpenGL::INIT_HOOK{"DC::Layout"} = sub {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines