Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rcfile.pl « mpcresources « mplayerc « apps « src - github.com/mpc-hc/mpc-hc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 81b734152e10d50f415e442f10fd4d7497a71270 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#/bin/perl
#
# $Id$
#
# (C) 2006-2010 see AUTHORS
#
# This file is part of mplayerc.
#
# Mplayerc is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Mplayerc is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Apply mplayerc.rc file changes to locale rc files.
#
# To use this program, you need a perl with PerlIO package installed.
# For help of usage, type: perl rcfile.pl -h
#

use strict;
use Getopt::Long;
use vars qw(@InTags @TextTags $AdjustedDir);

use File::Basename;
use File::Spec;
use File::Glob qw(:globally :nocase);
require "common.pl";

my $BaseFileName = "../mplayerc.rc";
my $NewFileName = "../mplayerc.rc";
my $help;

my $result = GetOptions("base|b=s" =>\$BaseFileName, "new|n=s" =>\$NewFileName, "help|h"=>\$help);

if($help || !$result) {
	print << 'USAGE';
Usage: perl rcfile.pl [Options] | -h --help
Copy all changes between two version of mplayerc.rc files to all rc files in the current directory,
generate new rc files under the "newrc" subdirectory.

Options:
	--base -b	base file, default "../mplayerc.rc" optional
	--new -n	modified file, default "../mplayerc.rc" optional
	--help -h	show this help

	Base file: the previous revision of mplayerc.rc file
	Modified file: the current mplayerc.rc, all the changes in this file will get copied to the files you chose.

	The examples below assume you are now in the "mpcresources" directory.

	example 1: you want to check if there are differences between mplayerc.rc and mplayerc.language.rc files.
	Go to the mpcresources directory, then 	>perl rcfile.pl

	example 2: you changed some gui of mpc-hc, that means you also modified the mplayerc.rc file.
	First: checkout the head revision of mplayerc.rc using the svn client, give it any other name, for example: 
	>svn cat -r head ../mplayerc.rc > mplayer.rc.old
	Second: >perl rcfile.pl -b mplayerc.rc.old
	Or better yet use provided batch file: >rcfile.bat

	After running this script, you will find all new language rc files under "newrc" subdir. 
	These new rc files have all changes copied from your modified mplayerc.rc file and is ready to compile, 
	except use english version strings. Use rcstrings.pl to extract all strings for translators, 
	After recieved translated text files, use patch.pl script to merge back to rc files.
USAGE
	exit(0);
}

my($BaseDialogs, $BaseMenus, $BaseStrings, @BaseOutline) = ({}, {}, {}, ());
my($NewDialogs, $NewMenus, $NewStrings, @NewOutline) = ({}, {}, {}, ());
my($MenuDiffs, $DialogDiffs) = ({}, {});
my($BaseDesignInfos, $NewDesignInfos) = ({}, {});

my @BaseFile = readFile($BaseFileName, 1);
my @NewFile = readFile($NewFileName, 1);
print "Scanning changes between baseline file and new version...\n\n";
getDifference();

#Trace($BaseDialogs, "Base Dialogs");
#Trace($BaseMenus, "Base Menus");
#Trace($BaseStrings, "Base Strings");
#Trace(\@BaseOutline, "Base Outline");
#Trace($MenuDiffs, "MenuDiffs");
#Trace($DialogDiffs, "DialogDiffs");

my @FileLists = ();

if(@ARGV) {	@FileLists = @ARGV; }
else { @FileLists = <*.rc>; }

#put all generted files under "newrc" sub dir.
if(!-e "newrc"){
	mkdir(File::Spec->catdir(".", "newrc")) || die "Can not create \"newrc\" sub directory.";
}

foreach my $filename(@FileLists) {
	print "Analyzing locale file: $filename...\n";
	my @oldrcfile = readFile($filename, 1);
	my($curDialogs, $curMenus, $curStrings, @curOutline) = ({},{},{}, ());
	my @curVersionInfo = ();
	my $curDesignInfos = {};
	analyseData(\@oldrcfile, \@curOutline, $curDialogs, $curMenus, $curStrings, \@curVersionInfo, $curDesignInfos);

	my $newrcfile = File::Spec->catfile(".", "newrc", $filename);

	my @newrc = ();
	my @patches = ();

	writeData(\@newrc, \@patches, \@curOutline, $curDialogs, $curMenus, $curStrings, \@curVersionInfo, $curDesignInfos);

	print "Generating new locale file: $newrcfile...\n\n";
	writeFile($newrcfile, \@newrc, 2);
}

###################################################################################################
sub getDifference {
	my @curVersionInfo = ();	# no use for mplayerc.rc
	analyseData(\@BaseFile, \@BaseOutline,$BaseDialogs, $BaseMenus, $BaseStrings, \@curVersionInfo, $BaseDesignInfos);
	analyseData(\@NewFile,\@NewOutline, $NewDialogs, $NewMenus, $NewStrings, \@curVersionInfo, $NewDesignInfos);

	while (my ($key, $value) = each(%{$BaseMenus})) {
		my $value1 = $NewMenus->{$key};

		if($value1) {
			my @changeset = ();
			lcs($value->{"__TEXT__"}, $value1->{"__TEXT__"}, \@changeset);
			@changeset = @changeset[sort {$changeset[$a][0] <=> $changeset[$b][0];}(0..$#changeset)];
			$MenuDiffs->{$key} = [@changeset];
		}
	}

	while (my ($key, $value) = each(%{$BaseDialogs})) {
		my $value1 = $NewDialogs->{$key};

		if($value1) {
			my @changeset = ();
			my @data0 = map{$_->[1]}@{$value->{"__DATA__"}};
			my @data1 = map{$_->[1]}@{$value1->{"__DATA__"}};
			lcs(\@data0, \@data1, \@changeset);
			@changeset = @changeset[sort {$changeset[$a][0] <=> $changeset[$b][0];}(0..$#changeset)];
			$DialogDiffs->{$key} = [@changeset];
		}
	}

}

#--------------------------------------------------------------------------------------------------
sub writeData {
	my ($newrc, $patches) = (shift, shift);
	my ($curOutline, $curDialogs, $curMenus, $curStrings, $curVersionInfo, $curDesignInfos) = @_;

	my ($curDialogName, $curMenuName, $curDesignName);

	my $headsection = 0;
	my $tailsection = $#NewOutline;
	my $oldtail = @$curOutline - 1;

	my $idx=0;
	foreach (@NewOutline) {
		my $tag = $_->[0];

		if($tag eq "__TEXT__") {
			if($idx==$headsection){
				push(@{$newrc}, @{$curOutline->[0][1]});			# use old language rc file head section
			}
			elsif($idx == $tailsection) {
				writeVersionInfo($newrc, $curVersionInfo);			# TODO: write current version info to it's original place, now just above end section
				push(@{$newrc}, @{$curOutline->[$oldtail][1]});		# use old language rc file head section
			}
			else {
				my @_text = ();
				push(@_text, @{$_->[1]});
				foreach my $line(@_text) {
					foreach my $texttag (@TextTags){
						if($line =~ /\b$texttag\b/) {
							$line =~ s/"res/"${AdjustedDir}res/;	# adjust directory for ICON BITMAP AVI
							last;
						}
					}
				}
				push(@{$newrc}, @_text);	#in general use texts from new rc file
			}
		}
		elsif($tag eq "DESIGNINFO") {
			$curDesignName = $_->[1][0];
			#if($curDesignInfos->{$curDesignName}) {
			#	push(@{$newrc}, @{$curDesignInfos->{$curDesignName}{"__TEXT__"}});	# use locale design info section
			#}
			#else {
				push(@{$newrc}, @{$NewDesignInfos->{$curDesignName}{"__TEXT__"}});	# use new design info section
			#}
		}
		elsif($tag eq "BLOCK") {
			push(@{$newrc}, @{$_->[1]});	# use new file block section
		}
		elsif($tag eq "DIALOG") {
			#use the new rc file dialogs, use locale strings if possible
			$curDialogName = $_->[1][0];
			my @dialogContent = ();
			writeDialogContent(\@dialogContent, $patches, $curDialogs, $curDialogName);
			push(@{$newrc}, @dialogContent);
		}
		elsif($tag eq "MENU") {
			$curMenuName = $_->[1][0];
			my @menuContent = ();
			writeMenuContent(\@menuContent, $patches, $curMenus, $curMenuName);
			push(@{$newrc}, @menuContent);
		}
		elsif($tag eq "STRINGTABLE") {
			#use new rc file stringtables, try to use as many locale strings as possible
			my @newstrings = ();
			push(@newstrings, @{$_->[1]});
			writeStringTable(\@newstrings, $patches, $curStrings);
			@newstrings = grep{$_=~/\S+/;}@newstrings;	#get rid of empty line for patch
			push(@{$newrc}, @newstrings);
		}
		$idx++;
	}
}

#--------------------------------------------------------------------------------------------------
sub writeStringTable {
	my ($output, $patches, $refs) = @_;

	#use new rc file stringtables, try to use as many locale strings as possible
	foreach (@{$output}){
		my ($key, $value);

		if (/\b(ID\S+)\b\s*(".+")/){ #distinguish between key value at same line or not for syntax's sake
			($key, $value)= ($1,$2); 
		}
		elsif (/\b(ID\S+)\b\s*$/){ #value too long to fit in one line but we don't care. :)
			$key = $1;
			$value = $NewStrings->{$key};
			$_ = "    $key  $value";
		}
		elsif(/^\s*".+"\s*$/){ #value not same line with key, already dealed with, so just clear it.
			$_ = " ";
			next;
		}
		else {
			next; #other text
		}

		my $baseStr = $BaseStrings->{$key};
		my $localeStr = $refs->{$key};

		if((!$localeStr) || (!$baseStr) || ($baseStr ne $value)) {
		}
		else {
			s/\Q$value\E/$localeStr/; #use locale string
		}
	}
}

#--------------------------------------------------------------------------------------------------
sub writeDialogContent {
	my ($output, $patches, $refs, $name) = @_;

	my @contents = ();
	push(@contents, @{$NewDialogs->{$name}{"__TEXT__"}});

	my @newdatas = map{$_->[1]}@{$NewDialogs->{$name}{"__DATA__"}};
	my @newidxs = map{$_->[0]}@{$NewDialogs->{$name}{"__DATA__"}};
	my $newdataLines = @newdatas;

	my @baseidxs = map{$_->[0]}@{$BaseDialogs->{$name}{"__DATA__"}};
	my $basedataLines = @baseidxs;

	my @refdatas = map{$_->[1]}@{$refs->{$name}{"__DATA__"}};
	my $refdataLines = @refdatas;

	if($newdataLines && $basedataLines && $refdataLines) {
		my $diffData = $DialogDiffs->{$name}; 

		foreach(@$diffData) {
			my $linenum = $newidxs[$_->[0]];

			my $str = $newdatas[$_->[0]];
			my $localstr = $refdatas[$_->[1]];

			if($localstr) {
				$contents[$linenum-1] =~ s/\Q$str\E/$localstr/;
			}
		}
	}

	my $localfont = $refs->{$name}{"__FONT__"}[1];
	if($localfont) {
		$contents[$NewDialogs->{$name}{"__FONT__"}[0]-1] = $refs->{$name}{"__TEXT__"}[$refs->{$name}{"__FONT__"}[0]-1];
	}

	push(@{$output}, @contents);
}

#--------------------------------------------------------------------------------------------------
sub writeMenuContent {
	my ($output, $patches, $refs, $name) = @_;

	my @contents = ();
	push(@contents, @{$NewMenus->{$name}{"__TEXT__"}});
	my $contentLines = @contents;

	if(my $diffData = $MenuDiffs->{$name}) {
		# this menu exists in old file
		my @changes = grep($_->[0] != $_->[1],@$diffData);	#anything changed for this menu?
		my $samelines = @$diffData; 

		if((!@changes) && ($samelines == $contentLines)) { #no change then just use old data
			@contents = ();
			push(@contents, @{$refs->{$name}{"__TEXT__"}}); 
		}
		else {				#change in this menu
			my @checkIdx=(1..$contentLines);

			foreach (@$diffData) {
				$contents[$_->[0]] = $refs->{$name}{"__TEXT__"}[$_->[1]];	#first use those values from locale file
				$checkIdx[$_->[0]] = 0;
			}
		}
	}

	push(@{$output}, @contents);
}

#--------------------------------------------------------------------------------------------------
sub writeVersionInfo{
	my ($input, $versionInfo) = (shift, shift);

	#Trace($versionInfo, "Current VersionInfo");
	push(@{$input}, "");
	push(@{$input}, "/////////////////////////////////////////////////////////////////////////////");
	push(@{$input}, "//");
	push(@{$input}, "// Version");
	push(@{$input}, "//");
	push(@{$input}, @$versionInfo);
}

###################################################################################################