-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConvertAllStringsToGameData.ps1
271 lines (241 loc) · 11 KB
/
ConvertAllStringsToGameData.ps1
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
[CmdletBinding()]
param (
# Output file type (Required)
# Check out help message in ConvertTo-GameData.ps1
[Parameter(Mandatory)]
[string]
$FileType,
# Source language (Required)
# What language to convert?
[Parameter(Mandatory)]
[string]
$SourceLanguage,
# Target language
# Check out help message in ConvertTo-GameData.ps1
[Parameter()]
[string]
$TargetLanguage = 'en',
# Game version (Default: latest)
# Default 'latest' takes the last folder in dump directory.
# Otherwise specify the _name of the folder_ that you want
# to dump from.
[Parameter()]
[string]
$Version = 'latest',
# Overwrite EXD(s) if they exist? (Default: No)
[Parameter()]
[switch]
$Overwrite = $false,
# Ignore quest include list from ConversionLists.psm1? (Default: No)
[Parameter()]
[switch]
$IgnoreQuestIncludeList = $false
)
# Start importing stuff
$ErrorActionPreference_before = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
Import-Module -Name "./lib/file_types/$FileType.psm1"
$CONFIG = Import-PowerShellDataFile -Path "./config/config.psd1"
foreach ($path_name in [string[]] $CONFIG.PATHS.Keys) {
try {
$CONFIG.PATHS.$path_name = (Resolve-Path -Path $CONFIG.PATHS.$path_name).Path
}
catch {
Write-Error ("Path {0} could not be resolved." -f $CONFIG.PATHS.$path_name)
return 2
}
}
$CONVERSION_LISTS = Import-PowerShellDataFile -Path "./config/conversion_lists.psd1"
$ErrorActionPreference = $ErrorActionPreference_before
# Finish importing stuff
if ($Version -eq 'latest') {
$version_list = Get-ChildItem -Path $CONFIG.PATHS.DUMP_DIR -Directory
$dump_ver_dir = $version_list[-1]
} else {
$dump_ver_path = "{0}/{1}" -f $CONFIG.PATHS.DUMP_DIR, $Version
if (-not $(Test-Path -Path $dump_ver_path)) {
throw "Version $Version was not found in dump folder."
}
$dump_ver_dir = Get-Item -Path $dump_ver_path
}
Write-Information "Using version: $dump_ver_dir" -InformationAction Continue
$search_query = "{0}/*{1}.{2}" -f $CONFIG.PATHS.STRINGS_DIR, $SourceLanguage, (Get-StringsFileExtension)
"Getting all {0} strings files at {1}" -f $SourceLanguage.ToUpper(), $CONFIG.PATHS.STRINGS_DIR
Write-Verbose "Search query: $search_query"
$input_strings_file_list = Get-ChildItem -Path $search_query -Recurse -File
"Done."
# Make a normal array of all files that need to be combined
$files_to_combine = foreach ($split_file in $CONVERSION_LISTS.SPLIT_FILES.GetEnumerator()) {
foreach ($file_name in [string[]] $split_file.Value.Keys) {
$file_name
}
}
# Collection of flags that indicate whether the parent file was combined.
# This prevents the parent file to be combined multiple times.
$files_combined = @{}
foreach ($input_strings_file in $input_strings_file_list) {
# Set up paths
$file_name = $input_strings_file.Directory.BaseName
$game_path = $input_strings_file.DirectoryName `
-creplace "$($CONFIG.PATHS.STRINGS_DIR)/", '' `
-creplace "/$file_name`$", ''
$exh_path = "{0}/{1}/{2}.exh" -f $dump_ver_dir, $game_path, $file_name
$destination_path = "{0}/{1}" -f $CONFIG.PATHS.OUTPUT_DIR, $game_path
if (-not $IgnoreQuestIncludeList -and
($game_path -cmatch '^exd/(?:cut_scene|opening|quest)/') -and
($file_name -notin $CONVERSION_LISTS.INCLUDE_QUESTS)) {
Write-Warning "Quest file is not in include list, skipping - $input_strings_file"
continue
}
# Compare last write time with cached one. Skip if it's the same.
# Time is in UTC for consistency sake
$last_write_time = $input_strings_file.LastWriteTime.ToUniversalTime().ToString()
$cache_file_path = "{0}/{1}/{2}/{3}.{4}.time" -f `
$CONFIG.PATHS.CACHE_DIR, $game_path, $file_name, $SourceLanguage, (Get-StringsFileExtension)
if (Test-Path -Path $cache_file_path) {
$last_write_time_cached = Get-Content -Path $cache_file_path
if ($last_write_time -eq $last_write_time_cached) {
Write-Warning "File didn't change, skipping - $input_strings_file"
continue
}
}
# If current file is one of the split ones, combine them back into
# a single one and then treat it like a normal strings file.
# Split files are expected to be in their nested folders. Combined
# strings file will be saved in their parent folder, like it would
# be if it wasn't split in the first place.
# After all conversions these combined files would be deleted.
if ($file_name -in $files_to_combine) {
# Set up paths again, but differently
$file_name = $input_strings_file.Directory.Parent.Name
$game_path = $input_strings_file.Directory.Parent `
-creplace "$($CONFIG.PATHS.STRINGS_DIR)/", '' `
-creplace "/$file_name`$", ''
$exh_path = "{0}/{1}/{2}.exh" -f $dump_ver_dir, $game_path, $file_name
$destination_path = "{0}/{1}" -f $CONFIG.PATHS.OUTPUT_DIR, $game_path
$destination_strings_path = "{0}/{1}/{2}" -f $CONFIG.PATHS.STRINGS_DIR, $game_path, $file_name
if ($files_combined.ContainsKey($file_name)) {
Write-Verbose "File was already combined into $file_name - $input_strings_file"
continue
}
# Put all split tables into a hashtable
$tables_split = @{}
# Also determine all existing rows to go through them later
$index_list = [System.Collections.Generic.List[int]]::new()
foreach ($split_file in $CONVERSION_LISTS.SPLIT_FILES.$file_name.GetEnumerator()) {
$split_file_name = $split_file.Key
$split_file_lang = $split_file.Value.ContainsKey('Language') ? $split_file.Value.Language : $SourceLanguage
$split_file_path = "{0}/{1}/{2}.{3}" -f `
$input_strings_file.Directory.Parent,
$split_file_name,
$split_file_lang,
(Get-StringsFileExtension)
Write-Verbose "Getting table from $split_file_path..."
$tables_split.$split_file_name = Import-Strings -Path $split_file_path
# Fill empty target strings with source
if ($split_file_lang -ne $CONFIG.MAIN_SOURCE_LANGUAGE) {
$split_file_source_path = "{0}/{1}/{2}.{3}" -f `
$input_strings_file.Directory.Parent,
$split_file_name,
$CONFIG.MAIN_SOURCE_LANGUAGE,
(Get-StringsFileExtension)
$table_source = Import-Strings -Path $split_file_source_path
foreach ($row in $table_source.GetEnumerator()) {
$index = $row.Key
$source_string = $row.Value.String
if (-not $tables_split.$split_file_name.ContainsKey($index)) {
$tables_split.$split_file_name.Add(
$index,
[pscustomobject]@{
String = $source_string
State = $STRING_STATE_NOT_TRANSLATED
}
)
continue
}
if (-not $tables_split.$split_file_name[$index].String.Length) {
$tables_split.$split_file_name[$index].String = $source_string
}
}
}
}
$index_list = [int[]] $tables_split.$( $($tables_split.Keys)[0] ).Keys
# Make a sorted reverse split table that dictates an order of split columns
$reverse_split_table = [System.Collections.Generic.SortedDictionary[int,string]]::new()
foreach ($split_file in $CONVERSION_LISTS.SPLIT_FILES.$file_name.GetEnumerator()) {
foreach ($column in $split_file.Value.Columns) {
$reverse_split_table.$column = $split_file.Key
}
}
# Create a new combined table with all columns
$table_combined = [System.Collections.Generic.SortedDictionary[int,pscustomobject]]::new()
Write-Verbose "Combining tables..."
foreach ($index in $index_list) {
$split_sets = @{}
$i = @{}
foreach ($split_file_name in [string[]] $CONVERSION_LISTS.SPLIT_FILES.$file_name.Keys) {
$split_sets.$split_file_name = $tables_split.$split_file_name[$index].String -split $COLUMN_SEPARATOR
$i.$split_file_name = 0
}
$splits_ordered = [System.Collections.Generic.List[string]]::new()
foreach ($reverse_split in [string[]] $reverse_split_table.Values) {
$splits_ordered.Add(
$split_sets.$reverse_split[$i.$reverse_split]
)
$i.$reverse_split += 1
}
$string = $splits_ordered -join $COLUMN_SEPARATOR
$table_combined.Add($index, [PSCustomObject]@{
String = $string
State = $STRING_STATE_TRANSLATED
})
}
# Export this table to a combined file that would be then processed normally
Write-Verbose "Exporting combined table..."
$error_code = Export-Strings `
-Table $table_combined `
-TargetLanguage $SourceLanguage `
-Destination $destination_strings_path
if ($error_code) {
throw "Error during conversion to a combined file - $destination_strings_path"
}
$files_combined.$file_name = $true
$input_strings_file = Get-Item -Path (
Get-TargetPath -Path $destination_strings_path -Language $SourceLanguage
)
# ConvertTo-GameData script will expect to have a main source language strings file,
# which wouldn't be there after combining just target language files.
# Since combined file simply mirrors the original file, we'll just convert
# the original one.
$error_code = ./ConvertFrom-GameData.ps1 `
-ExhPath $exh_path `
-FileType XLIFFMonolingual `
-Languages 'en' `
-Destination $destination_strings_path `
-Overwrite `
-IgnoreSplits
if ($error_code) {
Write-Error "Error while converting original combined game file to $destination_strings_path"
continue
}
}
$result = ./ConvertTo-GameData.ps1 `
-ExhPath $exh_path `
-StringsPath $input_strings_file `
-FileType XLIFFMonolingual `
-Overwrite `
-Destination $destination_path
if ($result -eq 0) {
$null = New-Item -Path (Split-Path -Path $cache_file_path -Parent) -ItemType Directory -ErrorAction Ignore
$null = New-Item -Path $cache_file_path -ItemType File -ErrorAction Ignore
Set-Content -Value $last_write_time -Path $cache_file_path
}
if ($files_combined.$file_name -and (Test-Path -Path $input_strings_file)) {
$source_strings_file = "{0}/{1}.{2}" -f `
$destination_strings_path,
$CONFIG.MAIN_SOURCE_LANGUAGE,
(Get-StringsFileExtension)
Remove-Item -Path $input_strings_file
Remove-Item -Path $source_strings_file
}
}