aboutsummaryrefslogtreecommitdiff
path: root/dists/android/mkmanifest.pl
blob: 62caa64a55fd5d66d90fdad24cdcc9eb79ac364f (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/perl

use File::Basename qw(dirname);
use File::Path qw(mkpath);
use IO::File;
use XML::Writer;
use XML::Parser;
use Getopt::Long;

use warnings;
use strict;

use constant ANDROID => 'http://schemas.android.com/apk/res/android';

my $id;
my $package_versionName;
my $package_versionCode;
my $configure = 'configure';
my $stringres = 'res/string/values.xml';
my $manifest = 'AndroidManifest.xml';
my $master_manifest;
my @unpack_libs;
GetOptions('id=s' => \$id,
	   'version-name=s' => \$package_versionName,
	   'version-code=i' => \$package_versionCode,
	   'configure=s' => \$configure,
	   'stringres=s' => \$stringres,
	   'manifest=s' => \$manifest,
	   'master-manifest=s' => \$master_manifest,
	   'unpacklib=s' => \@unpack_libs,
    ) or die;
die "Missing required arg"
    unless $id and $package_versionName and $package_versionCode;


sub grope_engine_info {
    my $configure = shift;
    my @ret;
    while (<$configure>) {
	m/^add_engine \s+ (\w+) \s+ "(.*?)" \s+ \w+ (?:\s+ "([\w\s]*)")?/x
	    or next;
	my $subengines = $3 || '';
	my %info = (id => $1, name => $2,
		    subengines => [split / /, $subengines]);
	push @ret, \%info;
    }
    return @ret;
}

sub read_constraints {
    my $manifest = shift;
    my @constraints;
    my $parser = new XML::Parser Handlers => {
	Start => sub {
	    my $expat = shift;
	    my $elem = shift;
	    return if $elem !~
		/^(uses-configuration|supports-screens|uses-sdk)$/;
	    my @constraint = ($elem);
	    while (@_) {
		my $attr = shift;
		my $value = shift;
		$attr = [ANDROID, $attr] if $attr =~ s/^android://;
		push @constraint, $attr, $value;
	    }
	    push @constraints, \@constraint;
	},
    };
    $parser->parse($manifest);
    return @constraints;
}

sub print_stringres {
    my $output = shift;
    my $info = shift;

    my $writer = new XML::Writer(OUTPUT => $output, ENCODING => 'utf-8',
				 DATA_MODE => 1, DATA_INDENT => 2);

    $writer->xmlDecl();
    $writer->startTag('resources');

    while (my ($k,$v) = each %$info) {
	$writer->dataElement('string', $v, name => $k);
    }

    $writer->endTag('resources');
    $writer->end();
}

sub print_manifest {
    my $output = shift;
    my $info = shift;
    my $constraints = shift;

    my $writer = new XML::Writer(OUTPUT => $output, ENCODING => 'utf-8',
				 DATA_MODE => 1, DATA_INDENT => 2,
				 NAMESPACES => 1,
				 PREFIX_MAP => {ANDROID, 'android'});

    $writer->xmlDecl();

    $writer->startTag(
	'manifest',
	'package' => "org.inodes.gus.scummvm.plugin.$info->{name}",
	[ANDROID, 'versionCode'] => $package_versionCode,
	[ANDROID, 'versionName'] => $package_versionName,
	[ANDROID, 'installLocation'] => 'preferExternal',
	);

    $writer->startTag(
	'application',
	[ANDROID, 'label'] => '@string/app_name',
	[ANDROID, 'description'] => '@string/app_desc',
	[ANDROID, 'icon'] => '@drawable/scummvm',
	);

    $writer->startTag(
	'receiver',
	[ANDROID, 'name'] => 'org.inodes.gus.scummvm.PluginProvider',
	[ANDROID, 'process'] => 'org.inodes.gus.scummvm');

    $writer->startTag('intent-filter');
    $writer->emptyTag('action', [ANDROID, 'name'] =>
		      'org.inodes.gus.scummvm.action.PLUGIN_QUERY');
    $writer->emptyTag('category', [ANDROID, 'name'] =>
		      'android.intent.category.INFO');
    $writer->endTag('intent-filter');
    $writer->emptyTag(
	'meta-data',
	[ANDROID, 'name'] => 'org.inodes.gus.scummvm.meta.UNPACK_LIB',
	[ANDROID, 'value'] => $_)
	for @{$info->{unpack_libs}};

    $writer->endTag('receiver');
    $writer->endTag('application');

    $writer->emptyTag('uses-permission', [ANDROID, 'name'] =>
		      'org.inodes.gus.scummvm.permission.SCUMMVM_PLUGIN');

    $writer->emptyTag(@$_) foreach @$constraints;

    $writer->endTag('manifest');
    $writer->end();
}


my %engines;
for my $engine (grope_engine_info(new IO::File $configure, 'r')) {
    $engines{$engine->{id}} = $engine;
}

my @games = ($id, @{$engines{$id}{subengines}});
my $games_desc = join('; ', map $engines{$_}{name}, @games);

my @constraints = read_constraints(new IO::File $master_manifest, 'r');

print "Writing $stringres ...\n";
mkpath(dirname($stringres));
print_stringres(IO::File->new($stringres, 'w'),
		{app_name => qq{ScummVM plugin: "$id"},
		 app_desc => "Game engine for: $games_desc",
		});

print "Writing $manifest ...\n";
mkpath(dirname($manifest));
print_manifest(IO::File->new($manifest, 'w'),
	       {name => $id, unpack_libs => \@unpack_libs}, \@constraints);

exit 0;