-
Notifications
You must be signed in to change notification settings - Fork 0
/
texini2sed.pl
executable file
·131 lines (120 loc) · 4.13 KB
/
texini2sed.pl
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
#!/usr/bin/perl -w
# FS Tools
# Copyright (C) 2009-2011 Jakob Klein, [email protected]
#
# This file is part of FS Tools.
#
# FS Tools 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.
#
# FS Tools 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 FS Tools. If not, see <http://www.gnu.org/licenses/>.
# Function:
# This script will translate a Textures.ini file as used with the old
# Texture Rename Tool into a substitution script which can be applied with
# sed onto the _statelighttable.csvs to achieve the same result as with
# the Texture Rename Tool (but working also on non default light system
# csvs!).
#
# Useage:
# ./texini2sed.pl < "Textures.ini" > "textures.sed"
#
# Further Steps:
# Apply this sed script onto the csv like this:
# - When the main fstools.sh waits for you to finish the edit of the file
# given change to another Console/Terminal Emulation Window or move the
# process to the background (see you systems manuals for further detail,
# e.g. man bash under section "Job Control").
# - go to the folder where you created the "textures.sed" and issue the
# following command to invoke sed on your .csv
# sed -f "textures.sed" "/tmp/xxxx.asm_statettablelist.csv" > result.csv
# Then the substitute the old csv with the one you just applied your
# edits to:
# mv result.csv "/tmp/xxxx.asm_statettablelist.csv"
# Where xxxx has to be substituted by the coorect name of course.
# - Then you can go back to the main script fstools.sh and continue
# execution by pressing Enter.
use strict;
# Global Variables used in this script.
my $mode = undef;
my $prefix = undef;
# Read Standard Input linewise into $_.
while (<>) {
# Remove Standard Lineendings.
chomp ($_);
# Additonally remove any CR \r U+000d if present.
$_ =~ s/\r//g;
# Match sections to find out which mode the following lines are.
if ($_ =~ /^\[Substitution\d+\]/i) {
$mode = 'sub';
}
elsif ($_ =~ /^\[Rename\d+\]/i) {
$mode = 'ren';
}
# If section is not recognized, unset mode.
elsif ($_ =~ /^\[/i) {
$mode = undef;
}
# If mode is set and this is no section heading, process following lines.
elsif ($mode) {
# Mode is substitution.
if ($mode eq 'sub') {
# Match Prefix line and store in global variable $prefix.
if ($_ =~ /^prefix=(.*)$/) {
$prefix = $1;
# Escape Slashes for sed.
$prefix =~ s/\//\\\//g;
next;
}
# Match textures.
if ($_ =~ /^textures=(.*)$/) {
# Textures are listed on one line,
# separated by commas (U+002c) and surrounded by whitespace.
my @textures = split (',', $1);
# Work on each textture in this list.
foreach my $texture (@textures) {
# Remove any whitespace before or after the Texturename.
$texture = trim ($texture);
# Print sed substitution regex.
print "s/$texture/${prefix}${texture}/gi\n";
}
}
}
# Mode is rename.
if ($mode eq 'ren') {
# Match Before and After Parts of lines.
if ($_ =~ /^(.*)=(.*)$/) {
# Escale Slashes for sed on both before and after.
$1 =~ s/\//\\\//g;
$2 =~ s/\//\\\//g;
# Print sed substitution regex.
print "s/$1/$2/gi\n";
}
}
}
}
# Perl trim function to remove whitespace from the start and end of the string.
sub trim {
my $string = shift;
$string = <rim(&rtrim($string));
return $string;
}
# Left trim function to remove leading whitespace.
sub ltrim {
my $string = shift;
$string =~ s/^\s+//;
return $string;
}
# Right trim function to remove trailing whitespace.
sub rtrim {
my $string = shift;
$string =~ s/\s+$//;
return $string;
}