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.184 by root, Mon Jul 7 12:56:07 2008 UTC vs.
Revision 1.209 by root, Tue Dec 22 00:41:47 2009 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
19our $VERSION;
20
21BEGIN { 21BEGIN {
22 $VERSION = '0.9973'; 22 $VERSION = '2.09';
23 23
24 use XSLoader; 24 use XSLoader;
25 XSLoader::load "Deliantra::Client", $VERSION; 25 XSLoader::load "Deliantra::Client", $VERSION;
26} 26}
27 27
28use utf8; 28use utf8;
29use strict qw(vars subs);
29 30
31use Socket ();
30use AnyEvent (); 32use AnyEvent ();
33use AnyEvent::Util ();
31use Pod::POM (); 34use Pod::POM ();
32use File::Path (); 35use File::Path ();
33use Storable (); # finally 36use Storable (); # finally
34use Fcntl (); 37use Fcntl ();
35use JSON::XS qw(encode_json decode_json); 38use JSON::XS qw(encode_json decode_json);
36 39use Guard qw(guard);
37=item guard { BLOCK }
38
39Returns an object that executes the given block as soon as it is destroyed.
40
41=cut
42
43sub guard(&) {
44 bless \(my $cb = $_[0]), "DC::Guard"
45}
46
47sub DC::Guard::DESTROY {
48 ${$_[0]}->()
49}
50 40
51=item shorten $string[, $maxlength] 41=item shorten $string[, $maxlength]
52 42
53=cut 43=cut
54 44
66 s/</&lt;/g; 56 s/</&lt;/g;
67 57
68 $_ 58 $_
69} 59}
70 60
71sub socketpipe() {
72 socketpair my $fh1, my $fh2, Socket::AF_UNIX, Socket::SOCK_STREAM, Socket::PF_UNSPEC
73 or die "cannot establish bidirectional pipe: $!\n";
74
75 ($fh1, $fh2)
76}
77
78sub background(&;&) { 61sub background(&;&) {
79 my ($bg, $cb) = @_; 62 my ($bg, $cb) = @_;
80 63
81 my ($fh_r, $fh_w) = DC::socketpipe; 64 my ($fh_r, $fh_w) = AnyEvent::Util::portable_socketpair
65 or die "unable to create background socketpair: $!";
82 66
83 my $pid = fork; 67 my $pid = fork;
84 68
85 if (defined $pid && !$pid) { 69 if (defined $pid && !$pid) {
86 local $SIG{__DIE__}; 70 local $SIG{__DIE__};
142 print $msg, "\n"; 126 print $msg, "\n";
143} 127}
144 128
145package DC; 129package DC;
146 130
131our $RC_THEME;
132our %THEME;
133our @RC_PATH;
134our $RC_BASE;
135
136for (grep !ref, @INC) {
137 $RC_BASE = "$_/Deliantra/Client/private/resources";
138 last if -d $RC_BASE;
139}
140
147sub find_rcfile($) { 141sub find_rcfile($) {
148 my $path; 142 my $path;
149 143
150 for (grep !ref, @INC) { 144 for (@RC_PATH, "") {
151 $path = "$_/Deliantra/Client/private/resources/$_[0]"; 145 $path = "$RC_BASE/$_/$_[0]";
152 return $path if -r $path; 146 return $path if -e $path;
153 } 147 }
154 148
155 die "FATAL: can't find required file $_[0]\n"; 149 die "FATAL: can't find required file \"$_[0]\" in \"$RC_BASE\"\n";
150}
151
152sub load_json($) {
153 my ($file) = @_;
154
155 open my $fh, $file
156 or return;
157
158 local $/;
159 eval { JSON::XS->new->utf8->relaxed->decode (<$fh>) }
160}
161
162sub set_theme($) {
163 return if $RC_THEME eq $_[0];
164 $RC_THEME = $_[0];
165
166 # kind of hacky, find the main theme file, then load all theme files and merge them
167
168 %THEME = ();
169 @RC_PATH = "theme-$RC_THEME";
170
171 my $theme = load_json find_rcfile "theme.json"
172 or die "FATAL: theme resource file not found";
173
174 @RC_PATH = @{ $theme->{path} } if $theme->{path};
175
176 for (@RC_PATH, "") {
177 my $theme = load_json "$RC_BASE/$_/theme.json"
178 or next;
179
180 %THEME = ( %$theme, %THEME );
181 }
156} 182}
157 183
158sub read_cfg { 184sub read_cfg {
159 my ($file) = @_; 185 my ($file) = @_;
160 186
161 open my $fh, $file 187 $::CFG = (load_json $file) || (load_json "$file.bak");
162 or return;
163
164 local $/;
165 my $CFG = <$fh>;
166
167 $::CFG = decode_json $CFG;
168} 188}
169 189
170sub write_cfg { 190sub write_cfg {
171 my $file = "$Deliantra::VARDIR/client.cf"; 191 my $file = "$Deliantra::VARDIR/client.cf";
172 192
173 $::CFG->{VERSION} = $::VERSION; 193 $::CFG->{VERSION} = $::VERSION;
194 $::CFG->{layout} = DC::UI::get_layout ();
174 195
175 open my $fh, ">:utf8", $file 196 open my $fh, ">:utf8", "$file~"
176 or return; 197 or return;
177 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG); 198 print $fh JSON::XS->new->utf8->pretty->encode ($::CFG);
199 close $fh;
200
201 rename $file, "$file.bak";
202 rename "$file~", $file;
178} 203}
179 204
180sub http_proxy { 205sub http_proxy {
181 my @proxy = win32_proxy_info; 206 my @proxy = win32_proxy_info;
182 207

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines