summaryrefslogtreecommitdiff
path: root/util/jsgen.pl
blob: 0cc895aef4071241d4d64f7b246b2b6bfa0aa533 (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
#!/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 (keys %{$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) = @_;
  open my $F, '>', "$f~" or die $!;
  print $F encode_utf8($body);
  close $F;
  rename "$f~", $f or die $!;
}


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