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.189 by root, Tue Sep 2 16:50:06 2008 UTC vs.
Revision 1.215 by root, Wed Jan 18 13:42:19 2012 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines