summaryrefslogtreecommitdiff
path: root/util/jsgen.pl
blob: 8317f86f4b6d381d0cdb345e7ed022e3730aa76f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/perl

package VNDB;

use strict;
use warnings;
use Encode 'encode_utf8';
use Cwd 'abs_path';
use JSON::XS;

our($ROOT, %S, %O);
BEGIN { ($ROOT = abs_path $0) =~ s{/util/jsgen\.pl$}{}; }
require $ROOT.'/data/global.pl';


# screen resolution information, suitable for usage in filFSelect()
sub resolutions {
  my $cat = '';
  my @r;
  my $push = \@r;
  for my $i (0..$#{$S{resolutions}}) {
    my $r = $S{resolutions}[$i];
    if($cat ne $r->[1]) {
      push @r, [$r->[1]];
      $cat = $r->[1];
      $push = $r[$#r];
    }
    push @$push, [$i, $r->[0]];
  }
  \@r
}


sub vars {
  my %vars = (
    rlist_status  => $S{rlist_status},
    cookie_prefix => $O{cookie_prefix},
    age_ratings   => [ map [ $_, $_ == -1 ? 'Unknown' : $_ == 0 ? 'All ages' : "$_+" ], @{$S{age_ratings}} ],
    languages     => [ map [ $_, $S{languages}{$_} ], keys %{$S{languages}} ],
    platforms     => [ map [ $_, $S{platforms}{$_} ], keys %{$S{platforms}} ],
    char_roles    => [ map [ $_, $S{char_roles}{$_}[0] ], keys %{$S{char_roles}} ],
    media         => [ map [ $_, $S{media}{$_}[1], $S{media}{$_}[0] ], keys %{$S{media}} ],
    release_types => [ map [ $_, ucfirst $_ ], @{$S{release_types}} ],
    animated      => [ map [ $_, $S{animated}[$_] ], 0..$#{$S{animated}} ],
    voiced        => [ map [ $_, $S{voiced}[$_] ], 0..$#{$S{voiced}} ],
    vn_lengths    => [ map [ $_, $S{vn_lengths}[$_][0] ], 0..$#{$S{vn_lengths}} ],
    blood_types   => [ map [ $_, $S{blood_types}{$_} ], keys %{$S{blood_types}} ],
    genders       => [ map [ $_, $S{genders}{$_} ], keys %{$S{genders}} ],
    staff_roles   => [ map [ $_, $S{staff_roles}{$_} ], keys %{$S{staff_roles}} ],
    resolutions   => scalar resolutions(),
  );
  JSON::XS->new->encode(\%vars);
}


# Reads main.js and any included files.
sub readjs {
  my $f = shift || 'main.js';
  open my $JS, '<:utf8', "$ROOT/data/js/$f" or die $!;
  local $/ = undef;
  local $_ = <$JS>;
  close $JS;
  s{^//include (.+)$}{'(function(){'.readjs($1).'})();'}meg;
  $_;
}


sub save {
  my($f, $body) = @_;
  my $content = encode_utf8($body);

  unlink "$f~";
  if(!$VNDB::JSGEN{compress}) {
    open my $F, '>', "$f~" or die $!;
    print $F $content;
    close $F;

  } elsif($VNDB::JSGEN{compress} eq 'JavaScript::Minifier::XS') {
    require JavaScript::Minifier::XS;
    open my $F, '>', "$f~" or die $!;
    print $F JavaScript::Minifier::XS::minify($content);
    close $F;

  } elsif($VNDB::JSGEN{compress} =~ /^\|/) { # External command
    (my $cmd = $VNDB::JSGEN{compress}) =~ s/^\|//;
    open my $C, '|-', "$cmd >'$f~'" or die $!;
    print $C $content;
    close $C or die $!;

  } else {
    die "Unrecognized compression option: '$VNDB::JSGEN{compress}'\n";
  }

  rename "$f~", $f or die $!;

  if($VNDB::JSGEN{gzip}) {
    `$VNDB::JSGEN{gzip} -c '$f' >'$f.gz~'`;
    rename "$f.gz~", "$f.gz";
  }
}


my $js = readjs;
$js =~ s{/\*VARS\*/}{vars()}eg;
save "$ROOT/static/f/vndb.js", $js;