strindex 3.3.2__tar.gz → 3.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {strindex-3.3.2/strindex.egg-info → strindex-3.4.0}/PKG-INFO +5 -24
- {strindex-3.3.2 → strindex-3.4.0}/README.md +4 -23
- {strindex-3.3.2 → strindex-3.4.0}/setup.py +1 -1
- strindex-3.4.0/strindex/__init__.py +1 -0
- strindex-3.4.0/strindex/__main__.py +4 -0
- strindex-3.4.0/strindex/filetypes/__init__.py +6 -0
- strindex-3.3.2/strindex/filetypes/datawin.py → strindex-3.4.0/strindex/filetypes/iff.py +13 -8
- strindex-3.3.2/strindex/filetypes/pefile.py → strindex-3.4.0/strindex/filetypes/pe.py +10 -9
- {strindex-3.3.2 → strindex-3.4.0}/strindex/strindex.py +156 -130
- {strindex-3.3.2 → strindex-3.4.0}/strindex/utils.py +101 -97
- {strindex-3.3.2 → strindex-3.4.0/strindex.egg-info}/PKG-INFO +5 -24
- {strindex-3.3.2 → strindex-3.4.0}/strindex.egg-info/SOURCES.txt +3 -2
- strindex-3.3.2/strindex/__init__.py +0 -1
- strindex-3.3.2/strindex/__main__.py +0 -4
- {strindex-3.3.2 → strindex-3.4.0}/LICENSE +0 -0
- {strindex-3.3.2 → strindex-3.4.0}/setup.cfg +0 -0
- {strindex-3.3.2 → strindex-3.4.0}/strindex.egg-info/dependency_links.txt +0 -0
- {strindex-3.3.2 → strindex-3.4.0}/strindex.egg-info/entry_points.txt +0 -0
- {strindex-3.3.2 → strindex-3.4.0}/strindex.egg-info/requires.txt +0 -0
- {strindex-3.3.2 → strindex-3.4.0}/strindex.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: strindex
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.4.0
|
|
4
4
|
Summary: A command line utility to extract and patch strings of various filetypes, with a focus on compatibility and translation.
|
|
5
5
|
Home-page: https://github.com/zWolfrost/strindex
|
|
6
6
|
Author: zWolfrost
|
|
@@ -36,7 +36,7 @@ The release includes a precompiled version of the program, which ONLY does the p
|
|
|
36
36
|
## Supported filetypes
|
|
37
37
|
*Support for more types won't be added unless I find some motivation to...*
|
|
38
38
|
- PE files (".exe, ".dll"...)
|
|
39
|
-
- Gamemaker data
|
|
39
|
+
- Gamemaker data files ("data.win")
|
|
40
40
|
|
|
41
41
|
## Installation
|
|
42
42
|
You can install the program with the command `pip install strindex`.
|
|
@@ -48,6 +48,7 @@ These are the available actions:
|
|
|
48
48
|
- `create`: Create a list of strings from a PE file (a strindex). Use `-c` to create a strindex that uses the original strings as references.
|
|
49
49
|
- `patch`: Patch a PE file with a strindex.
|
|
50
50
|
- `patch_gui`: Patch a PE file with a strindex using a GUI.
|
|
51
|
+
- `update` Update a strindex pointers' with the updated version of a file.
|
|
51
52
|
- `filter`: Filter a strindex by detected language, wordlist or length. You can specify those in the strindex settings.
|
|
52
53
|
- `delta`: Create a delta file between two strindexes, that only contains the lines of the first strindex missing in the second one.
|
|
53
54
|
- `spellcheck`: Spellcheck a strindex. You can specify the target language in the strindex settings as an ISO 639-1 code.
|
|
@@ -62,26 +63,6 @@ These are the available actions:
|
|
|
62
63
|
strindex patch program.exe strindex.txt
|
|
63
64
|
```
|
|
64
65
|
|
|
65
|
-
## Strindex
|
|
66
|
-
|
|
67
|
-
{
|
|
68
|
-
"md5": "29ed1f9e450d43815c2d1a0cab168da3",
|
|
69
|
-
|
|
70
|
-
"prefix_bytes": ["24c7442404", "ec04c70424"],
|
|
71
|
-
|
|
72
|
-
"patch_replace": {
|
|
73
|
-
"ì": "í",
|
|
74
|
-
"Ì": "Í",
|
|
75
|
-
"ò": "ó",
|
|
76
|
-
"Ò": "Ó"
|
|
77
|
-
},
|
|
78
|
-
|
|
79
|
-
"clean_pattern": "\\[.*?\\]|\\*",
|
|
80
|
-
"whitelist": ["latin", "spanish", "cyrillic"],
|
|
81
|
-
|
|
82
|
-
"source_language": "es",
|
|
83
|
-
"target_language": "it",
|
|
84
|
-
"among_languages": ["en", "ja", "ko", "de", "fr", "es", "pt", "ru", "zh"]
|
|
85
|
-
}
|
|
86
|
-
```
|
|
66
|
+
## Strindex Example
|
|
67
|
+
You can find an example of a strindex file and an explanation of its settings [here](strindex_example.txt).
|
|
87
68
|
|
|
@@ -17,7 +17,7 @@ The release includes a precompiled version of the program, which ONLY does the p
|
|
|
17
17
|
## Supported filetypes
|
|
18
18
|
*Support for more types won't be added unless I find some motivation to...*
|
|
19
19
|
- PE files (".exe, ".dll"...)
|
|
20
|
-
- Gamemaker data
|
|
20
|
+
- Gamemaker data files ("data.win")
|
|
21
21
|
|
|
22
22
|
## Installation
|
|
23
23
|
You can install the program with the command `pip install strindex`.
|
|
@@ -29,6 +29,7 @@ These are the available actions:
|
|
|
29
29
|
- `create`: Create a list of strings from a PE file (a strindex). Use `-c` to create a strindex that uses the original strings as references.
|
|
30
30
|
- `patch`: Patch a PE file with a strindex.
|
|
31
31
|
- `patch_gui`: Patch a PE file with a strindex using a GUI.
|
|
32
|
+
- `update` Update a strindex pointers' with the updated version of a file.
|
|
32
33
|
- `filter`: Filter a strindex by detected language, wordlist or length. You can specify those in the strindex settings.
|
|
33
34
|
- `delta`: Create a delta file between two strindexes, that only contains the lines of the first strindex missing in the second one.
|
|
34
35
|
- `spellcheck`: Spellcheck a strindex. You can specify the target language in the strindex settings as an ISO 639-1 code.
|
|
@@ -43,25 +44,5 @@ These are the available actions:
|
|
|
43
44
|
strindex patch program.exe strindex.txt
|
|
44
45
|
```
|
|
45
46
|
|
|
46
|
-
## Strindex
|
|
47
|
-
|
|
48
|
-
{
|
|
49
|
-
"md5": "29ed1f9e450d43815c2d1a0cab168da3",
|
|
50
|
-
|
|
51
|
-
"prefix_bytes": ["24c7442404", "ec04c70424"],
|
|
52
|
-
|
|
53
|
-
"patch_replace": {
|
|
54
|
-
"ì": "í",
|
|
55
|
-
"Ì": "Í",
|
|
56
|
-
"ò": "ó",
|
|
57
|
-
"Ò": "Ó"
|
|
58
|
-
},
|
|
59
|
-
|
|
60
|
-
"clean_pattern": "\\[.*?\\]|\\*",
|
|
61
|
-
"whitelist": ["latin", "spanish", "cyrillic"],
|
|
62
|
-
|
|
63
|
-
"source_language": "es",
|
|
64
|
-
"target_language": "it",
|
|
65
|
-
"among_languages": ["en", "ja", "ko", "de", "fr", "es", "pt", "ru", "zh"]
|
|
66
|
-
}
|
|
67
|
-
```
|
|
47
|
+
## Strindex Example
|
|
48
|
+
You can find an example of a strindex file and an explanation of its settings [here](strindex_example.txt).
|
|
@@ -3,7 +3,7 @@ from setuptools import setup
|
|
|
3
3
|
|
|
4
4
|
setup(
|
|
5
5
|
name = "strindex",
|
|
6
|
-
version = "3.
|
|
6
|
+
version = "3.4.0",
|
|
7
7
|
author = "zWolfrost",
|
|
8
8
|
author_email = "zwolfrost@gmail.com",
|
|
9
9
|
description = "A command line utility to extract and patch strings of various filetypes, with a focus on compatibility and translation.",
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
from strindex import strindex, utils
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from
|
|
1
|
+
from ..utils import Strindex, StrindexSettings, FileBytearray
|
|
2
2
|
|
|
3
3
|
|
|
4
4
|
def get_last_chunk_pointer(data: FileBytearray) -> int:
|
|
@@ -12,9 +12,10 @@ def get_last_chunk_pointer(data: FileBytearray) -> int:
|
|
|
12
12
|
|
|
13
13
|
|
|
14
14
|
def is_valid(data: FileBytearray) -> bool:
|
|
15
|
+
""" Checks if the file is an IFF file. """
|
|
15
16
|
return data[0:4] == b"FORM"
|
|
16
17
|
|
|
17
|
-
def create(data: FileBytearray,
|
|
18
|
+
def create(data: FileBytearray, settings: StrindexSettings) -> Strindex:
|
|
18
19
|
BYTE_LENGTH = 4
|
|
19
20
|
|
|
20
21
|
temp_strindex = {
|
|
@@ -25,7 +26,7 @@ def create(data: FileBytearray, min_length: int, prefixes: list[bytes]) -> Strin
|
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
for string, offset in data.yield_strings():
|
|
28
|
-
if len(string) >= min_length:
|
|
29
|
+
if len(string) >= settings.min_length:
|
|
29
30
|
offset -= BYTE_LENGTH
|
|
30
31
|
temp_strindex["original"].append(string)
|
|
31
32
|
temp_strindex["offsets"].append(offset)
|
|
@@ -35,8 +36,7 @@ def create(data: FileBytearray, min_length: int, prefixes: list[bytes]) -> Strin
|
|
|
35
36
|
raise ValueError("No strings found in the file.")
|
|
36
37
|
print(f"(1/2) Created search dictionary with {len(temp_strindex['original'])} strings.")
|
|
37
38
|
|
|
38
|
-
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["offset_bytes"],
|
|
39
|
-
print(f"(2/2) Found pointers for {len([p for p in temp_strindex['pointers'] if p])} / {len(temp_strindex['original'])} strings.")
|
|
39
|
+
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["offset_bytes"], settings.prefix_bytes, settings.suffix_bytes)
|
|
40
40
|
|
|
41
41
|
STRINDEX = Strindex()
|
|
42
42
|
for original, offset, _, pointers in zip(*temp_strindex.values()):
|
|
@@ -45,14 +45,19 @@ def create(data: FileBytearray, min_length: int, prefixes: list[bytes]) -> Strin
|
|
|
45
45
|
STRINDEX.offsets.append(offset)
|
|
46
46
|
STRINDEX.pointers.append(pointers)
|
|
47
47
|
|
|
48
|
+
print(f"(2/2) Found pointers for {len(STRINDEX.overwrite)} / {len(temp_strindex['original'])} strings.")
|
|
49
|
+
|
|
48
50
|
return STRINDEX
|
|
49
51
|
|
|
50
52
|
def patch(data: FileBytearray, strindex: Strindex) -> FileBytearray:
|
|
51
53
|
"""
|
|
52
54
|
The patching is done by increasing both
|
|
53
55
|
the "FORM" chunk size and the last chunk size to fit the new data.
|
|
54
|
-
The data is thus contained in the last chunk of the file
|
|
56
|
+
The data is thus contained in the last chunk of the file,
|
|
57
|
+
to avoid having to change the pointers of the other chunks as well.
|
|
55
58
|
The pointers are changed to reference the new data.
|
|
59
|
+
This works fine with gamemaker "data.win" files,
|
|
60
|
+
and might also work with IFF files in general, but I haven't tested it.
|
|
56
61
|
"""
|
|
57
62
|
|
|
58
63
|
BYTE_LENGTH = 4
|
|
@@ -65,7 +70,7 @@ def patch(data: FileBytearray, strindex: Strindex) -> FileBytearray:
|
|
|
65
70
|
def get_replaced_offset() -> bytes:
|
|
66
71
|
return (DATA_LEN + len(new_section_data)).to_bytes(BYTE_LENGTH, 'little')
|
|
67
72
|
def new_section_string(string: str) -> bytes:
|
|
68
|
-
return len(string).to_bytes(BYTE_LENGTH, 'little') + bytearray(strindex.patch_replace_string(string), 'utf-8') + b'\x00'
|
|
73
|
+
return len(string).to_bytes(BYTE_LENGTH, 'little') + bytearray(strindex.settings.patch_replace_string(string), 'utf-8') + b'\x00'
|
|
69
74
|
|
|
70
75
|
temp_strindex = {
|
|
71
76
|
"original_offset": [],
|
|
@@ -88,7 +93,7 @@ def patch(data: FileBytearray, strindex: Strindex) -> FileBytearray:
|
|
|
88
93
|
|
|
89
94
|
new_section_data += new_section_string(strindex.replace[strindex_index])
|
|
90
95
|
|
|
91
|
-
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["original_offset"], strindex.settings
|
|
96
|
+
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["original_offset"], strindex.settings.prefix_bytes, strindex.settings.suffix_bytes)
|
|
92
97
|
|
|
93
98
|
for original_offset, replaced_offset, pointers, pointers_switches in zip(*temp_strindex.values()):
|
|
94
99
|
if pointers:
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
from
|
|
1
|
+
from ..utils import Strindex, StrindexSettings, FileBytearray
|
|
2
2
|
import pefile
|
|
3
3
|
pefile.fast_load = True
|
|
4
4
|
|
|
@@ -226,11 +226,11 @@ def is_valid(data: FileBytearray) -> bool:
|
|
|
226
226
|
return False
|
|
227
227
|
return pe.DOS_HEADER.e_magic == 0x5A4D
|
|
228
228
|
|
|
229
|
-
def create(data: FileBytearray,
|
|
229
|
+
def create(data: FileBytearray, settings: StrindexSettings) -> Strindex:
|
|
230
230
|
pe = pefile.PE(data=bytes(data))
|
|
231
231
|
|
|
232
232
|
if pe_section_exists(pe, SECTION_NAME):
|
|
233
|
-
print(f"
|
|
233
|
+
print(f"Warning: this file already contains a '{SECTION_NAME.decode('utf-8')}' section.")
|
|
234
234
|
|
|
235
235
|
BYTE_LENGTH = 4 if pe.OPTIONAL_HEADER.Magic == 0x10b else 8
|
|
236
236
|
|
|
@@ -243,7 +243,7 @@ def create(data: FileBytearray, min_length: int, prefixes: list[bytes]) -> Strin
|
|
|
243
243
|
}
|
|
244
244
|
|
|
245
245
|
for string, offset in data.yield_strings():
|
|
246
|
-
if len(string) >= min_length:
|
|
246
|
+
if len(string) >= settings.min_length:
|
|
247
247
|
rva = pe.get_rva_from_offset(offset)
|
|
248
248
|
if rva:
|
|
249
249
|
rva += pe.OPTIONAL_HEADER.ImageBase
|
|
@@ -256,8 +256,7 @@ def create(data: FileBytearray, min_length: int, prefixes: list[bytes]) -> Strin
|
|
|
256
256
|
raise ValueError("No strings found in the file.")
|
|
257
257
|
print(f"(1/2) Created search dictionary with {len(temp_strindex['original'])} strings.")
|
|
258
258
|
|
|
259
|
-
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["rva_bytes"],
|
|
260
|
-
print(f"(2/2) Found pointers for {len([p for p in temp_strindex['pointers'] if p])} / {len(temp_strindex['original'])} strings.")
|
|
259
|
+
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["rva_bytes"], settings.prefix_bytes, settings.suffix_bytes)
|
|
261
260
|
|
|
262
261
|
STRINDEX = Strindex()
|
|
263
262
|
for string, offset, rva, _, pointers in zip(*temp_strindex.values()):
|
|
@@ -266,12 +265,14 @@ def create(data: FileBytearray, min_length: int, prefixes: list[bytes]) -> Strin
|
|
|
266
265
|
STRINDEX.offsets.append(offset)
|
|
267
266
|
STRINDEX.pointers.append(pointers)
|
|
268
267
|
|
|
268
|
+
print(f"(2/2) Found pointers for {len(STRINDEX.overwrite)} / {len(temp_strindex['original'])} strings.")
|
|
269
|
+
|
|
269
270
|
return STRINDEX
|
|
270
271
|
|
|
271
272
|
def patch(data: FileBytearray, strindex: Strindex) -> FileBytearray:
|
|
272
273
|
"""
|
|
273
274
|
The patching is done by adding a new section to the PE file, containing the new data.
|
|
274
|
-
The pointers are changed to reference the new data.
|
|
275
|
+
The pointers are changed to reference the new data RVAs'.
|
|
275
276
|
"""
|
|
276
277
|
|
|
277
278
|
pe = pefile.PE(data=bytes(data))
|
|
@@ -289,7 +290,7 @@ def patch(data: FileBytearray, strindex: Strindex) -> FileBytearray:
|
|
|
289
290
|
def get_replaced_rva() -> bytes:
|
|
290
291
|
return (STRDEX_SECTION_BASE_RVA + len(new_section_data)).to_bytes(BYTE_LENGTH, 'little')
|
|
291
292
|
def new_section_string(string: str) -> bytes:
|
|
292
|
-
return bytearray(strindex.patch_replace_string(string), 'utf-8') + b'\x00'
|
|
293
|
+
return bytearray(strindex.settings.patch_replace_string(string), 'utf-8') + b'\x00'
|
|
293
294
|
|
|
294
295
|
temp_strindex = {
|
|
295
296
|
"original_rva": [],
|
|
@@ -310,7 +311,7 @@ def patch(data: FileBytearray, strindex: Strindex) -> FileBytearray:
|
|
|
310
311
|
|
|
311
312
|
new_section_data += new_section_string(strindex.replace[strindex_index])
|
|
312
313
|
|
|
313
|
-
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["original_rva"], strindex.settings
|
|
314
|
+
temp_strindex["pointers"] = data.get_indices_fixed(temp_strindex["original_rva"], strindex.settings.prefix_bytes, strindex.settings.suffix_bytes)
|
|
314
315
|
|
|
315
316
|
for original_rva, replaced_rva, pointers, pointers_switches in zip(*temp_strindex.values()):
|
|
316
317
|
if pointers:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import os, sys, argparse
|
|
1
|
+
import os, sys, argparse
|
|
2
2
|
from hashlib import md5
|
|
3
|
-
from strindex.utils import PrintProgress, Strindex,
|
|
4
|
-
from strindex.filetypes import
|
|
3
|
+
from strindex.utils import PrintProgress, Strindex, StrindexSettings, FileBytearray
|
|
4
|
+
from strindex.filetypes import MODULES
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
def get_module_methods(data: FileBytearray, action: str) -> dict:
|
|
@@ -9,7 +9,7 @@ def get_module_methods(data: FileBytearray, action: str) -> dict:
|
|
|
9
9
|
Returns the methods of the module associated with the file type.
|
|
10
10
|
"""
|
|
11
11
|
|
|
12
|
-
for module in
|
|
12
|
+
for module in MODULES:
|
|
13
13
|
if module.is_valid(data):
|
|
14
14
|
print(f'Detected filetype: "{module.__name__.split(".")[-1]}".')
|
|
15
15
|
assert action in module.__dict__, f"Action '{action}' is not available for this file type."
|
|
@@ -18,25 +18,29 @@ def get_module_methods(data: FileBytearray, action: str) -> dict:
|
|
|
18
18
|
raise ValueError("This file type has no associated module, or the required libraries to handle it are not installed.")
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
def create(file_filepath: str, strindex_filepath: str, compatible: bool,
|
|
21
|
+
def create(file_filepath: str, strindex_filepath: str, compatible: bool, settings: StrindexSettings):
|
|
22
22
|
"""
|
|
23
23
|
Calls the create method of the module associated with the file type.
|
|
24
24
|
"""
|
|
25
25
|
|
|
26
26
|
data = FileBytearray(open(file_filepath, 'rb').read())
|
|
27
27
|
|
|
28
|
-
STRINDEX: Strindex = get_module_methods(data, "create")(data,
|
|
28
|
+
STRINDEX: Strindex = get_module_methods(data, "create")(data, settings)
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
if compatible:
|
|
31
|
+
STRINDEX.type_order = ["compatible"] * len(STRINDEX.overwrite)
|
|
32
|
+
STRINDEX.original = STRINDEX.overwrite
|
|
33
|
+
STRINDEX.replace = STRINDEX.overwrite
|
|
34
|
+
STRINDEX.overwrite = []
|
|
35
|
+
STRINDEX.pointers_switches = STRINDEX.pointers
|
|
36
|
+
STRINDEX.pointers = []
|
|
37
|
+
else:
|
|
38
|
+
STRINDEX.type_order = ["overwrite"] * len(STRINDEX.overwrite)
|
|
34
39
|
|
|
35
|
-
STRINDEX.settings
|
|
36
|
-
|
|
37
|
-
STRINDEX.settings["prefix_bytes"] = [prefix.hex() for prefix in prefixes]
|
|
40
|
+
STRINDEX.settings = settings
|
|
41
|
+
STRINDEX.settings.md5 = md5(data).hexdigest()
|
|
38
42
|
|
|
39
|
-
STRINDEX.
|
|
43
|
+
STRINDEX.write(strindex_filepath)
|
|
40
44
|
|
|
41
45
|
print("Created strindex file.")
|
|
42
46
|
|
|
@@ -49,123 +53,22 @@ def patch(file_filepath: str, strindex_filepath: str, file_patched_filepath: str
|
|
|
49
53
|
|
|
50
54
|
data = FileBytearray(open(file_filepath_bak if os.path.exists(file_filepath_bak) else file_filepath, 'rb').read())
|
|
51
55
|
|
|
52
|
-
STRINDEX
|
|
56
|
+
STRINDEX = Strindex.read(strindex_filepath)
|
|
57
|
+
|
|
58
|
+
if STRINDEX.settings.md5 and STRINDEX.settings.md5 != md5(data).hexdigest():
|
|
59
|
+
print("MD5 hash does not match the one the strindex was created for. You may encounter issues.")
|
|
53
60
|
|
|
54
|
-
|
|
55
|
-
print("MD5 hash does not match the one the strindex was created for. You might encounter issues.")
|
|
61
|
+
data = get_module_methods(data, "patch")(data, STRINDEX)
|
|
56
62
|
|
|
57
63
|
if not file_patched_filepath:
|
|
58
64
|
if not os.path.exists(file_filepath_bak):
|
|
59
65
|
os.rename(file_filepath, file_filepath_bak)
|
|
60
66
|
file_patched_filepath = file_filepath
|
|
61
67
|
|
|
62
|
-
data = get_module_methods(data, "patch")(data, STRINDEX)
|
|
63
68
|
open(file_patched_filepath, 'wb').write(data)
|
|
64
69
|
|
|
65
70
|
print("File was patched successfully.")
|
|
66
71
|
|
|
67
|
-
def filter(strindex_filepath: str, strindex_filter_filepath: str):
|
|
68
|
-
"""
|
|
69
|
-
Filters a strindex file with another with respect to length, whitelist and source language.
|
|
70
|
-
"""
|
|
71
|
-
|
|
72
|
-
STRINDEX = Strindex.from_file(strindex_filepath)
|
|
73
|
-
STRINDEX_LINES = STRINDEX.get_ordered_strings(lambda t: "original" if t == "compatible" else t)
|
|
74
|
-
|
|
75
|
-
if STRINDEX.settings["source_language"]:
|
|
76
|
-
try:
|
|
77
|
-
from lingua import IsoCode639_1, LanguageDetectorBuilder
|
|
78
|
-
except ImportError:
|
|
79
|
-
raise ImportError(
|
|
80
|
-
"Please install the 'lingua' package (pip install lingua) to filter by language."
|
|
81
|
-
)
|
|
82
|
-
|
|
83
|
-
ALL_LANGUAGES = [code for code in IsoCode639_1.__dict__.values() if isinstance(code, IsoCode639_1)]
|
|
84
|
-
SETTINGS_LANGUAGES = [getattr(IsoCode639_1, code.upper()) for code in STRINDEX.settings["among_languages"]]
|
|
85
|
-
|
|
86
|
-
detector = LanguageDetectorBuilder.from_iso_codes_639_1(*(SETTINGS_LANGUAGES or ALL_LANGUAGES)).build()
|
|
87
|
-
|
|
88
|
-
def is_source_language(string: str) -> bool:
|
|
89
|
-
line_clean = re.sub(STRINDEX.settings["clean_pattern"], "", string)
|
|
90
|
-
confidence = detector.compute_language_confidence_values(line_clean)[0]
|
|
91
|
-
return confidence.language.iso_code_639_1 == getattr(IsoCode639_1, STRINDEX.settings["source_language"].upper()) and confidence.value > 0.5
|
|
92
|
-
|
|
93
|
-
print_progress = PrintProgress(len(STRINDEX_LINES))
|
|
94
|
-
with open(strindex_filter_filepath, 'w', encoding='utf-8') as f:
|
|
95
|
-
f.write(STRINDEX.full_header)
|
|
96
|
-
|
|
97
|
-
for strindex_index, line in enumerate(STRINDEX_LINES):
|
|
98
|
-
if (
|
|
99
|
-
(not STRINDEX.settings["source_language"] or is_source_language(line)) and
|
|
100
|
-
(len(line) >= STRINDEX.settings["min_length"]) and
|
|
101
|
-
(not STRINDEX.settings.get("whitelist") or not any(ch not in STRINDEX.settings["whitelist"] for ch in line))
|
|
102
|
-
):
|
|
103
|
-
f.write(STRINDEX.create_raw_from_index(strindex_index))
|
|
104
|
-
|
|
105
|
-
print_progress(strindex_index)
|
|
106
|
-
|
|
107
|
-
truncate_prev_char(f)
|
|
108
|
-
print("Created filtered strindex file.")
|
|
109
|
-
|
|
110
|
-
def delta(strindex_full_filepath: str, strindex_diff_filepath: str, strindex_delta_filepath: str, reverse: bool = False):
|
|
111
|
-
"""
|
|
112
|
-
Filters a full strindex file with a delta strindex file, or intersects them.
|
|
113
|
-
"""
|
|
114
|
-
|
|
115
|
-
STRINDEX_1 = Strindex.from_file(strindex_full_filepath)
|
|
116
|
-
STRINDEX_2 = Strindex.from_file(strindex_diff_filepath)
|
|
117
|
-
|
|
118
|
-
STRINDEX_1_LINES = STRINDEX_1.get_ordered_strings(lambda t: "original" if t == "compatible" else t)
|
|
119
|
-
STRINDEX_2_LINES = STRINDEX_2.get_ordered_strings(lambda t: "original" if t == "compatible" else t)
|
|
120
|
-
|
|
121
|
-
with open(strindex_delta_filepath, 'w', encoding='utf-8') as f:
|
|
122
|
-
f.write(STRINDEX_1.full_header)
|
|
123
|
-
|
|
124
|
-
index_2 = 0
|
|
125
|
-
for full_index in range(len(STRINDEX_1.type_order)):
|
|
126
|
-
try:
|
|
127
|
-
index_2 = STRINDEX_2_LINES.index(STRINDEX_1_LINES[full_index], index_2)
|
|
128
|
-
except ValueError:
|
|
129
|
-
if not reverse:
|
|
130
|
-
f.write(STRINDEX_1.create_raw_from_index(full_index))
|
|
131
|
-
else:
|
|
132
|
-
if reverse:
|
|
133
|
-
f.write(STRINDEX_1.create_raw_from_index(full_index))
|
|
134
|
-
|
|
135
|
-
truncate_prev_char(f)
|
|
136
|
-
|
|
137
|
-
print("Created delta strindex file.")
|
|
138
|
-
|
|
139
|
-
def spellcheck(strindex_filepath: str, strindex_spellcheck_filepath: str):
|
|
140
|
-
"""
|
|
141
|
-
Creates a spellcheck file from a strindex file, for the specified language.
|
|
142
|
-
"""
|
|
143
|
-
|
|
144
|
-
try:
|
|
145
|
-
from language_tool_python import LanguageTool
|
|
146
|
-
except ImportError:
|
|
147
|
-
raise ImportError("Please install the 'language-tool-python' package (pip install language-tool-python) to use this feature.")
|
|
148
|
-
|
|
149
|
-
STRINDEX = Strindex.from_file(strindex_filepath)
|
|
150
|
-
STRINDEX_LINES = STRINDEX.get_ordered_strings(lambda t: "replace" if t == "compatible" else t)
|
|
151
|
-
|
|
152
|
-
if not STRINDEX.settings["target_language"]:
|
|
153
|
-
raise ValueError("Please specify the target language to spellcheck in the strindex file ('target_language').")
|
|
154
|
-
|
|
155
|
-
lang = LanguageTool(STRINDEX.settings["target_language"])
|
|
156
|
-
print("Created language tool.")
|
|
157
|
-
|
|
158
|
-
with open(strindex_spellcheck_filepath, 'w', encoding='utf-8') as f:
|
|
159
|
-
print_progress = PrintProgress(len(STRINDEX_LINES))
|
|
160
|
-
for strindex_index, line in enumerate(STRINDEX_LINES):
|
|
161
|
-
line_clean = re.sub(STRINDEX.settings["clean_pattern"], "", line)
|
|
162
|
-
for error in lang.check(line_clean):
|
|
163
|
-
f.write('\n'.join(str(error).split('\n')[-3:]) + '\n')
|
|
164
|
-
|
|
165
|
-
print_progress(strindex_index)
|
|
166
|
-
print("Spellchecked strindex file.")
|
|
167
|
-
|
|
168
|
-
|
|
169
72
|
def patch_gui():
|
|
170
73
|
try:
|
|
171
74
|
from PySide6 import QtCore, QtWidgets, QtGui
|
|
@@ -274,6 +177,125 @@ def patch_gui():
|
|
|
274
177
|
gui.show()
|
|
275
178
|
sys.exit(app.exec())
|
|
276
179
|
|
|
180
|
+
def update(file_filepath: str, strindex_filepath: str, file_updated_filepath: str):
|
|
181
|
+
"""
|
|
182
|
+
Update a strindex file with newly created pointers.
|
|
183
|
+
"""
|
|
184
|
+
|
|
185
|
+
data = FileBytearray(open(file_filepath, 'rb').read())
|
|
186
|
+
|
|
187
|
+
STRINDEX = Strindex.read(strindex_filepath)
|
|
188
|
+
STRINDEX_UPDATED: Strindex = get_module_methods(data, "create")(data, STRINDEX.settings)
|
|
189
|
+
|
|
190
|
+
updated_pointers = 0
|
|
191
|
+
search_index = 0
|
|
192
|
+
for strindex_index in range(len(STRINDEX.original)):
|
|
193
|
+
try:
|
|
194
|
+
search_index = STRINDEX_UPDATED.overwrite.index(STRINDEX.original[strindex_index], search_index)
|
|
195
|
+
except ValueError:
|
|
196
|
+
pass
|
|
197
|
+
else:
|
|
198
|
+
if len(STRINDEX.pointers_switches[strindex_index]) != len(STRINDEX_UPDATED.pointers[search_index]):
|
|
199
|
+
updated_pointers += 1
|
|
200
|
+
STRINDEX.pointers_switches[strindex_index] = STRINDEX_UPDATED.pointers[search_index]
|
|
201
|
+
|
|
202
|
+
STRINDEX.write(file_updated_filepath)
|
|
203
|
+
|
|
204
|
+
print(f"Created strindex file with {updated_pointers} updated pointer(s).")
|
|
205
|
+
|
|
206
|
+
def filter(strindex_filepath: str, strindex_filter_filepath: str):
|
|
207
|
+
"""
|
|
208
|
+
Filters a strindex file with another with respect to length, whitelist and source language.
|
|
209
|
+
"""
|
|
210
|
+
|
|
211
|
+
STRINDEX = Strindex.read(strindex_filepath)
|
|
212
|
+
STRINDEX_FILTER = Strindex()
|
|
213
|
+
STRINDEX_FILTER.full_header = STRINDEX.full_header
|
|
214
|
+
|
|
215
|
+
if STRINDEX.settings.source_language:
|
|
216
|
+
try:
|
|
217
|
+
from lingua import LanguageDetectorBuilder, IsoCode639_1
|
|
218
|
+
except ImportError:
|
|
219
|
+
raise ImportError("Please install the 'lingua' package (pip install lingua-language-detector) to filter by language.")
|
|
220
|
+
|
|
221
|
+
ALL_LANGUAGES = [code for code in IsoCode639_1.__dict__.values() if isinstance(code, IsoCode639_1)]
|
|
222
|
+
SETTINGS_LANGUAGES = [getattr(IsoCode639_1, code.upper()) for code in STRINDEX.settings.among_languages or []]
|
|
223
|
+
|
|
224
|
+
detector = LanguageDetectorBuilder.from_iso_codes_639_1(*(SETTINGS_LANGUAGES or ALL_LANGUAGES)).build()
|
|
225
|
+
|
|
226
|
+
def is_source_language(string: str) -> bool:
|
|
227
|
+
string_clean = STRINDEX.settings.clean_string(string)
|
|
228
|
+
confidence = detector.compute_language_confidence_values(string_clean)[0]
|
|
229
|
+
return confidence.language.iso_code_639_1 == getattr(IsoCode639_1, STRINDEX.settings.source_language.upper()) and confidence.value > 0.5
|
|
230
|
+
|
|
231
|
+
print_progress = PrintProgress(len(STRINDEX.type_order))
|
|
232
|
+
for strindex_index, (type_index, type) in enumerate(STRINDEX.iterate_type_count()):
|
|
233
|
+
string = STRINDEX.original[type_index] if type == "compatible" else STRINDEX.overwrite[type_index]
|
|
234
|
+
valid_language = not STRINDEX.settings.source_language or is_source_language(string)
|
|
235
|
+
valid_length = len(string) >= STRINDEX.settings.min_length
|
|
236
|
+
valid_whitelist = not STRINDEX.settings.whitelist or not any(ch not in STRINDEX.settings.whitelist for ch in string)
|
|
237
|
+
|
|
238
|
+
if all([valid_language, valid_length, valid_whitelist]):
|
|
239
|
+
STRINDEX.append_to_strindex(STRINDEX_FILTER, type, type_index)
|
|
240
|
+
|
|
241
|
+
print_progress(strindex_index)
|
|
242
|
+
|
|
243
|
+
STRINDEX_FILTER.write(strindex_filter_filepath)
|
|
244
|
+
print(f"Created strindex file with {len(STRINDEX_FILTER.type_order)} / {len(STRINDEX.type_order)} strings.")
|
|
245
|
+
|
|
246
|
+
def delta(strindex_full_filepath: str, strindex_diff_filepath: str, strindex_delta_filepath: str):
|
|
247
|
+
"""
|
|
248
|
+
Filters a full strindex file with a delta strindex file, or intersects them.
|
|
249
|
+
"""
|
|
250
|
+
|
|
251
|
+
STRINDEX_1 = Strindex.read(strindex_full_filepath)
|
|
252
|
+
STRINDEX_2 = Strindex.read(strindex_diff_filepath)
|
|
253
|
+
|
|
254
|
+
STRINDEX_1_FULL = [STRINDEX_1.original[i] if t == "compatible" else STRINDEX_1.overwrite[i] for i, t in STRINDEX_1.iterate_type_count()]
|
|
255
|
+
STRINDEX_2_FULL = [STRINDEX_2.original[i] if t == "compatible" else STRINDEX_2.overwrite[i] for i, t in STRINDEX_2.iterate_type_count()]
|
|
256
|
+
|
|
257
|
+
STRINDEX_DELTA = Strindex()
|
|
258
|
+
STRINDEX_DELTA.full_header = STRINDEX_1.full_header
|
|
259
|
+
|
|
260
|
+
index_2 = 0
|
|
261
|
+
for full_index, (type_index, type) in enumerate(STRINDEX_1.iterate_type_count()):
|
|
262
|
+
try:
|
|
263
|
+
index_2 = STRINDEX_2_FULL.index(STRINDEX_1_FULL[full_index], index_2)
|
|
264
|
+
except ValueError:
|
|
265
|
+
STRINDEX_1.append_to_strindex(STRINDEX_DELTA, type, type_index)
|
|
266
|
+
|
|
267
|
+
STRINDEX_DELTA.write(strindex_delta_filepath)
|
|
268
|
+
print(f"Created delta strindex file with {len(STRINDEX_DELTA.type_order)} / {len(STRINDEX_1.type_order)} strings.")
|
|
269
|
+
|
|
270
|
+
def spellcheck(strindex_filepath: str, strindex_spellcheck_filepath: str):
|
|
271
|
+
"""
|
|
272
|
+
Creates a spellcheck file from a strindex file, for the specified language.
|
|
273
|
+
"""
|
|
274
|
+
|
|
275
|
+
try:
|
|
276
|
+
from language_tool_python import LanguageTool
|
|
277
|
+
except ImportError:
|
|
278
|
+
raise ImportError("Please install the 'language-tool-python' package (pip install language-tool-python) to use this feature.")
|
|
279
|
+
|
|
280
|
+
STRINDEX = Strindex.read(strindex_filepath)
|
|
281
|
+
STRINDEX_FULL = [STRINDEX.replace[i] if t == "compatible" else STRINDEX.overwrite[i] for i, t in STRINDEX.iterate_type_count()]
|
|
282
|
+
|
|
283
|
+
if not STRINDEX.settings.target_language:
|
|
284
|
+
raise ValueError("Please specify the target language to spellcheck in the strindex file ('target_language').")
|
|
285
|
+
|
|
286
|
+
lang = LanguageTool(STRINDEX.settings.target_language)
|
|
287
|
+
print("Created language tool.")
|
|
288
|
+
|
|
289
|
+
with open(strindex_spellcheck_filepath, 'w', encoding='utf-8') as f:
|
|
290
|
+
print_progress = PrintProgress(len(STRINDEX_FULL))
|
|
291
|
+
for strindex_index, string in enumerate(STRINDEX_FULL):
|
|
292
|
+
string_clean = STRINDEX.settings.clean_string(string)
|
|
293
|
+
for error in lang.check(string_clean):
|
|
294
|
+
f.write('\n'.join(str(error).split('\n')[-3:]) + '\n')
|
|
295
|
+
|
|
296
|
+
print_progress(strindex_index)
|
|
297
|
+
print("Spellchecked strindex file.")
|
|
298
|
+
|
|
277
299
|
|
|
278
300
|
def main():
|
|
279
301
|
if "__compiled__" in globals():
|
|
@@ -282,42 +304,46 @@ def main():
|
|
|
282
304
|
try:
|
|
283
305
|
parser = argparse.ArgumentParser(prog="strindex", description="Command line string replacement tool for games.")
|
|
284
306
|
|
|
285
|
-
parser.add_argument("action", type=str, choices=["create", "patch", "patch_gui", "filter", "delta", "spellcheck"], help="Action to perform.")
|
|
307
|
+
parser.add_argument("action", type=str, choices=["create", "patch", "patch_gui", "update", "filter", "delta", "spellcheck"], help="Action to perform.")
|
|
286
308
|
parser.add_argument("files", type=str, nargs=argparse.ZERO_OR_MORE, help="One or more files to process.")
|
|
287
309
|
parser.add_argument("-o", "--output", type=str, help="Output file.")
|
|
288
310
|
|
|
289
311
|
# create arguments
|
|
290
312
|
parser.add_argument("-c", "--compatible", action="store_true", help="Whether to create a strindex file compatible with the previous versions of a program.")
|
|
291
|
-
parser.add_argument("-m", "--min-length", type=int,
|
|
313
|
+
parser.add_argument("-m", "--min-length", type=int, help="Minimum length of the strings to be included.")
|
|
292
314
|
parser.add_argument("-p", "--prefix-bytes", type=str, action="append", default=[], help="Prefix bytes to add to the rva in the strindex file.")
|
|
293
|
-
|
|
294
|
-
# delta arguments
|
|
295
|
-
parser.add_argument("-r", "--reverse-delta", action="store_true", help="Whether to reverse the delta operation and intersect the files.")
|
|
315
|
+
parser.add_argument("-s", "--suffix-bytes", type=str, action="append", default=[], help="Suffix bytes to add to the rva in the strindex file.")
|
|
296
316
|
|
|
297
317
|
args = parser.parse_args()
|
|
298
318
|
|
|
299
319
|
if not all([os.path.isfile(file) for file in args.files]):
|
|
300
320
|
raise FileNotFoundError("One or more files do not exist.")
|
|
301
321
|
|
|
302
|
-
args.prefix_bytes = [bytes.fromhex(prefix) for prefix in (args.prefix_bytes or [''])]
|
|
303
|
-
|
|
304
322
|
match args.action:
|
|
305
323
|
case "create":
|
|
306
|
-
|
|
324
|
+
settings = StrindexSettings(**{
|
|
325
|
+
"min_length": args.min_length,
|
|
326
|
+
"prefix_bytes": args.prefix_bytes,
|
|
327
|
+
"suffix_bytes": args.suffix_bytes
|
|
328
|
+
})
|
|
329
|
+
|
|
330
|
+
create(*args.files, (args.output or "strindex.txt"), args.compatible, settings)
|
|
307
331
|
case "patch":
|
|
308
332
|
patch(*args.files, args.output)
|
|
309
333
|
case "patch_gui":
|
|
310
334
|
patch_gui()
|
|
335
|
+
case "update":
|
|
336
|
+
update(*args.files, (args.output or "strindex_updated.txt"))
|
|
311
337
|
case "filter":
|
|
312
338
|
filter(*args.files, (args.output or "strindex_filter.txt"))
|
|
313
339
|
case "delta":
|
|
314
|
-
delta(*args.files, (args.output or "strindex_delta.txt")
|
|
340
|
+
delta(*args.files, (args.output or "strindex_delta.txt"))
|
|
315
341
|
case "spellcheck":
|
|
316
342
|
spellcheck(*args.files, (args.output or "strindex_spellcheck.txt"))
|
|
317
343
|
except KeyboardInterrupt:
|
|
318
344
|
print("Interrupted by user.")
|
|
319
345
|
except BaseException as e:
|
|
320
|
-
print(e)
|
|
346
|
+
print(f"Error: {e}")
|
|
321
347
|
|
|
322
348
|
if __name__ == "__main__":
|
|
323
349
|
main()
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import json
|
|
1
|
+
import json, re
|
|
2
2
|
from typing import Generator
|
|
3
3
|
|
|
4
4
|
|
|
@@ -38,23 +38,65 @@ class PrintProgress():
|
|
|
38
38
|
def callback():
|
|
39
39
|
return globals().get("__print_progress_callback__")
|
|
40
40
|
|
|
41
|
-
class
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
CHARACTER_CLASSES = { # Please add your language's characters here and open a pull request <3
|
|
41
|
+
class StrindexSettings():
|
|
42
|
+
# These are really limited, so I would really like if you added your language's characters here and open a pull request <3
|
|
43
|
+
CHARACTER_CLASSES = {
|
|
45
44
|
"default": """\t\n !"#$%&'()*+,-./0123456789:;<=>?@[\]^_`{|}~… """,
|
|
46
45
|
"latin": """ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz""",
|
|
47
46
|
"spanish": """¡¿ÁÉÍÓÚÜÑáéíóúüñã""",
|
|
48
47
|
"cyrillic": """ЀЁЂЃЄЅІЇЈЉЊЋЌЍЎЏАБВГДЕЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдежзийклмнопрстуфхцчшщъыьэюяѐёђѓєѕіїјљњћќѝўџѠѡѢѣѤѥѦѧѨѩѪѫѬѭѮѯѰѱѲѳѴѵѶѷѸѹѺѻѼѽѾѿҀҁ҂҃҄҅҆҇҈҉ҊҋҌҍҎҏҐґҒғҔҕҖҗҘҙҚқҜҝҞҟҠҡҢңҤҥҦҧҨҩҪҫҬҭҮүҰұҲҳҴҵҶҷҸҹҺһҼҽҾҿӀӁӂӃӄӅӆӇӈӉӊӋӌӍӎӏӐӑӒӓӔӕӖӗӘәӚӛӜӝӞӟӠӡӢӣӤӥӦӧӨөӪӫӬӭӮӯӰӱӲӳӴӵӶӷӸӹӺӻӼӽӾ""",
|
|
49
48
|
}
|
|
50
49
|
|
|
50
|
+
md5: str
|
|
51
|
+
whitelist: set[str]
|
|
52
|
+
min_length: int
|
|
53
|
+
prefix_bytes: list[bytes]
|
|
54
|
+
suffix_bytes: list[bytes]
|
|
55
|
+
patch_replace: dict[str, str]
|
|
56
|
+
clean_pattern: str
|
|
57
|
+
source_language: str
|
|
58
|
+
target_language: str
|
|
59
|
+
among_languages: list[str]
|
|
60
|
+
|
|
61
|
+
def __init__(self, **kwargs):
|
|
62
|
+
self.md5 = kwargs.get("md5")
|
|
63
|
+
self.whitelist = StrindexSettings.handle_whitelist(kwargs.get("whitelist"))
|
|
64
|
+
self.min_length = kwargs.get("min_length") or 1
|
|
65
|
+
self.prefix_bytes = StrindexSettings.handle_bytes_list(kwargs.get("prefix_bytes"))
|
|
66
|
+
self.suffix_bytes = StrindexSettings.handle_bytes_list(kwargs.get("suffix_bytes"))
|
|
67
|
+
self.patch_replace = kwargs.get("patch_replace") or {}
|
|
68
|
+
self.clean_pattern = kwargs.get("clean_pattern") or ""
|
|
69
|
+
self.source_language = kwargs.get("source_language")
|
|
70
|
+
self.target_language = kwargs.get("target_language")
|
|
71
|
+
self.among_languages = kwargs.get("among_languages") or []
|
|
72
|
+
|
|
73
|
+
@staticmethod
|
|
74
|
+
def handle_whitelist(whitelist: str):
|
|
75
|
+
return set(''.join([StrindexSettings.CHARACTER_CLASSES.get(whitelist, whitelist) for whitelist in (whitelist + ["default"])])) if whitelist else None
|
|
76
|
+
|
|
77
|
+
@staticmethod
|
|
78
|
+
def handle_bytes_list(bytes_list: list[bytes]):
|
|
79
|
+
return [bytes.fromhex(prefix) for prefix in (bytes_list or [''])]
|
|
80
|
+
|
|
81
|
+
def clean_string(self, string: str) -> str:
|
|
82
|
+
return re.sub(self.clean_pattern, "", string)
|
|
83
|
+
|
|
84
|
+
def patch_replace_string(self, string: str) -> str:
|
|
85
|
+
""" Replaces the strings in the patch with the new strings. """
|
|
86
|
+
for key, value in self.patch_replace.items():
|
|
87
|
+
string = string.replace(key, value)
|
|
88
|
+
return string
|
|
89
|
+
|
|
90
|
+
class Strindex():
|
|
91
|
+
""" A class to parse and create strindex files. """
|
|
92
|
+
|
|
51
93
|
DELIMITERS = (f"{'=' * 80}", f"{'-' * 80}", f'/', f'-')
|
|
52
94
|
HEADER = f"You can freely delete informational lines in the header like this one.\n\n{{}}\n\n"
|
|
53
|
-
INFO = f"
|
|
54
|
-
COMPATIBLE_INFO = f"
|
|
95
|
+
INFO = f"//{'=' * 78}/ offset / offset(s)-of-rva-pointer(s) /\n"
|
|
96
|
+
COMPATIBLE_INFO = f"//{'=' * 78}[reallocate pointer(s) if 1]\n// replace this string...\n//{'-' * 78}\n// ...with this string!\n"
|
|
55
97
|
|
|
56
98
|
full_header: str
|
|
57
|
-
settings:
|
|
99
|
+
settings: StrindexSettings
|
|
58
100
|
type_order: list[str]
|
|
59
101
|
rva_bytes_length: int
|
|
60
102
|
|
|
@@ -70,18 +112,7 @@ class Strindex():
|
|
|
70
112
|
""" Parses a strindex file and returns a dictionary with the data. """
|
|
71
113
|
|
|
72
114
|
self.full_header = ""
|
|
73
|
-
self.settings =
|
|
74
|
-
"md5": None,
|
|
75
|
-
"whitelist": None,
|
|
76
|
-
"min_length": 0,
|
|
77
|
-
"prefix_bytes": [''],
|
|
78
|
-
"suffix_bytes": [''],
|
|
79
|
-
"patch_replace": {},
|
|
80
|
-
"clean_pattern": "",
|
|
81
|
-
"source_language": None,
|
|
82
|
-
"target_language": None,
|
|
83
|
-
"among_languages": [],
|
|
84
|
-
}
|
|
115
|
+
self.settings = StrindexSettings()
|
|
85
116
|
self.type_order = []
|
|
86
117
|
|
|
87
118
|
self.overwrite = []
|
|
@@ -92,8 +123,9 @@ class Strindex():
|
|
|
92
123
|
self.replace = []
|
|
93
124
|
self.pointers_switches = []
|
|
94
125
|
|
|
126
|
+
|
|
95
127
|
@staticmethod
|
|
96
|
-
def
|
|
128
|
+
def read(filepath: str):
|
|
97
129
|
""" Parses a strindex file and returns a dictionary with the data. """
|
|
98
130
|
|
|
99
131
|
strindex = Strindex()
|
|
@@ -105,13 +137,15 @@ class Strindex():
|
|
|
105
137
|
strindex.full_header += line
|
|
106
138
|
while True:
|
|
107
139
|
try:
|
|
108
|
-
strindex.settings
|
|
140
|
+
strindex.settings = StrindexSettings(**json.loads(strindex_settings_lines))
|
|
109
141
|
except json.JSONDecodeError as e:
|
|
110
142
|
line = f.readline()
|
|
143
|
+
strindex.full_header += line
|
|
144
|
+
if line.lstrip().startswith("//"):
|
|
145
|
+
continue
|
|
111
146
|
if line.startswith(Strindex.DELIMITERS[0]):
|
|
112
|
-
raise ValueError("Error parsing Strindex settings
|
|
147
|
+
raise ValueError("Error parsing Strindex settings: " + str(e))
|
|
113
148
|
strindex_settings_lines += line
|
|
114
|
-
strindex.full_header += line
|
|
115
149
|
else:
|
|
116
150
|
break
|
|
117
151
|
elif line.startswith(Strindex.DELIMITERS[0]):
|
|
@@ -137,7 +171,7 @@ class Strindex():
|
|
|
137
171
|
strindex.overwrite.append('')
|
|
138
172
|
|
|
139
173
|
needles = [[int(p, 16) if p else None for p in hex.split(Strindex.DELIMITERS[3])] for hex in line.split(Strindex.DELIMITERS[2])[1:-1]]
|
|
140
|
-
strindex.pointers.append(needles[-1] if any(needles[-1]) else [])
|
|
174
|
+
strindex.pointers.append(needles[-1] if len(needles) >= 1 and any(needles[-1]) else [])
|
|
141
175
|
strindex.offsets.append(needles[-2][0] if len(needles) >= 2 else None)
|
|
142
176
|
else:
|
|
143
177
|
next_lst = "original"
|
|
@@ -156,92 +190,68 @@ class Strindex():
|
|
|
156
190
|
|
|
157
191
|
getattr(strindex, next_lst)[-1] += line
|
|
158
192
|
|
|
159
|
-
strindex.settings["prefix_bytes"] = [bytes.fromhex(prefix) for prefix in strindex.settings["prefix_bytes"]]
|
|
160
|
-
strindex.settings["suffix_bytes"] = [bytes.fromhex(suffix) for suffix in strindex.settings["suffix_bytes"]]
|
|
161
|
-
strindex.settings["whitelist"] = set(''.join([Strindex.CHARACTER_CLASSES.get(whitelist, whitelist) for whitelist in (strindex.settings["whitelist"] + ["default"])])) if strindex.settings["whitelist"] else None
|
|
162
|
-
|
|
163
193
|
strindex.assert_data()
|
|
164
194
|
|
|
165
195
|
return strindex
|
|
166
196
|
|
|
167
|
-
def
|
|
197
|
+
def write(self, filepath: str):
|
|
168
198
|
""" Saves the strindex data to a file. """
|
|
169
199
|
self.assert_data()
|
|
170
200
|
|
|
171
|
-
diff_settings = {k: v for k, v in self.settings.items() if Strindex().settings.get(k) != v}
|
|
201
|
+
diff_settings = {k: v for k, v in self.settings.__dict__.items() if Strindex().settings.__dict__.get(k) != v}
|
|
172
202
|
|
|
173
203
|
with open(filepath, 'w', encoding='utf-8') as f:
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
if
|
|
179
|
-
|
|
204
|
+
if self.full_header:
|
|
205
|
+
f.write(self.full_header)
|
|
206
|
+
else:
|
|
207
|
+
f.write(Strindex.HEADER.format(json.dumps(diff_settings, indent=4, default=lambda x: x.hex() if isinstance(x, bytes) else str(x))))
|
|
208
|
+
f.write(Strindex.COMPATIBLE_INFO if self.type_order[0] == "compatible" else Strindex.INFO)
|
|
209
|
+
|
|
210
|
+
for index, type in self.iterate_type_count():
|
|
211
|
+
if type == "compatible":
|
|
212
|
+
f.write(
|
|
213
|
+
Strindex.DELIMITERS[0] +
|
|
214
|
+
"".join([str(int(bool(p))) for p in self.pointers_switches[index]]) + "\n" +
|
|
215
|
+
self.original[index] + "\n" +
|
|
216
|
+
Strindex.DELIMITERS[1] + "\n" +
|
|
217
|
+
self.replace[index] + "\n"
|
|
218
|
+
)
|
|
180
219
|
else:
|
|
181
|
-
f.write(
|
|
220
|
+
f.write(
|
|
221
|
+
Strindex.DELIMITERS[0] + Strindex.DELIMITERS[2] +
|
|
222
|
+
hex(self.offsets[index] or 0).lstrip("0x").rjust(8, '0') + Strindex.DELIMITERS[2] +
|
|
223
|
+
Strindex.DELIMITERS[3].join([hex(p or 0).lstrip("0x").rjust(8, '0') for p in self.pointers[index]]) +
|
|
224
|
+
Strindex.DELIMITERS[2] + "\n" +
|
|
225
|
+
self.overwrite[index] + "\n"
|
|
226
|
+
)
|
|
182
227
|
|
|
183
|
-
|
|
228
|
+
f.seek(f.tell() - 1)
|
|
229
|
+
f.truncate()
|
|
184
230
|
|
|
185
231
|
|
|
186
|
-
def
|
|
187
|
-
""" Replaces the strings in the patch with the new strings. """
|
|
188
|
-
for key, value in self.settings["patch_replace"].items():
|
|
189
|
-
string = string.replace(key, value)
|
|
190
|
-
return string
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
def get_ordered_strings(self, override=None) -> list[str]:
|
|
194
|
-
ordered_strings = []
|
|
232
|
+
def iterate_type_count(self):
|
|
195
233
|
types = {}
|
|
196
234
|
for type in self.type_order:
|
|
197
|
-
if override:
|
|
198
|
-
type = override(type)
|
|
199
235
|
types[type] = (types[type] + 1) if type in types else 0
|
|
200
|
-
|
|
201
|
-
return ordered_strings
|
|
202
|
-
|
|
203
|
-
def create_raw_from_index(self, index: int) -> str:
|
|
204
|
-
type_wanted = self.type_order[index]
|
|
205
|
-
type_index = self.type_order[:index].count(type_wanted)
|
|
206
|
-
|
|
207
|
-
if type_wanted == "overwrite":
|
|
208
|
-
return Strindex.create_raw_from_args(
|
|
209
|
-
self.overwrite[type_index],
|
|
210
|
-
self.offsets[type_index],
|
|
211
|
-
self.pointers[type_index]
|
|
212
|
-
)
|
|
213
|
-
elif type_wanted == "compatible":
|
|
214
|
-
return Strindex.create_compatible_raw_from_args(
|
|
215
|
-
self.original[type_index],
|
|
216
|
-
self.replace[type_index],
|
|
217
|
-
self.pointers_switches[type_index]
|
|
218
|
-
)
|
|
236
|
+
yield types[type], type
|
|
219
237
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
238
|
+
def append_to_strindex(self, strindex, type: str, index: int):
|
|
239
|
+
if type == "compatible":
|
|
240
|
+
strindex.original.append(self.original[index])
|
|
241
|
+
strindex.replace.append(self.replace[index])
|
|
242
|
+
strindex.pointers_switches.append(self.pointers_switches[index])
|
|
243
|
+
else:
|
|
244
|
+
strindex.overwrite.append(self.overwrite[index])
|
|
245
|
+
strindex.pointers.append(self.pointers[index])
|
|
246
|
+
strindex.offsets.append(self.offsets[index])
|
|
229
247
|
|
|
230
|
-
|
|
231
|
-
def create_compatible_raw_from_args(original, replace, pointers_switches) -> str:
|
|
232
|
-
return (
|
|
233
|
-
Strindex.DELIMITERS[0] +
|
|
234
|
-
"".join([str(int(bool(p))) for p in pointers_switches]) + "\n" +
|
|
235
|
-
original + "\n" +
|
|
236
|
-
Strindex.DELIMITERS[1] + "\n" +
|
|
237
|
-
replace + "\n"
|
|
238
|
-
)
|
|
248
|
+
strindex.type_order.append(type)
|
|
239
249
|
|
|
240
250
|
|
|
241
251
|
def assert_data(self):
|
|
242
|
-
assert len(self.overwrite) == len(self.pointers), "Overwrite and pointers lists are not the same length."
|
|
243
|
-
assert len(self.original) == len(self.replace) == len(self.pointers_switches), "Original, replace and pointers_switches lists are not the same length."
|
|
244
|
-
assert len(self.type_order)
|
|
252
|
+
assert len(self.overwrite) == len(self.pointers), f"Overwrite and pointers lists are not the same length ({len(self.overwrite)} != {len(self.pointers)})."
|
|
253
|
+
assert len(self.original) == len(self.replace) == len(self.pointers_switches), f"Original, replace and pointers_switches lists are not the same length ({len(self.original)} != {len(self.replace)} != {len(self.pointers_switches)})."
|
|
254
|
+
assert len(self.type_order) == len(self.overwrite) + len(self.original), f"Type order list is not the same length ({len(self.type_order)} != {len(self.overwrite) + len(self.original)})."
|
|
245
255
|
|
|
246
256
|
class FileBytearray(bytearray):
|
|
247
257
|
""" A class to handle bytearrays with additional methods. """
|
|
@@ -296,7 +306,6 @@ class FileBytearray(bytearray):
|
|
|
296
306
|
assert all(len(search) == len(search_lst_safe[0]) for search in search_lst_safe), "Search list is not fixed length."
|
|
297
307
|
assert all(len(prefix) == len(prefixes[0]) for prefix in prefixes), "Prefix list is not fixed length."
|
|
298
308
|
|
|
299
|
-
print_progress = PrintProgress(len(self))
|
|
300
309
|
fixed_prefix_length = len(prefixes[0])
|
|
301
310
|
fixed_length = fixed_prefix_length + len(search_lst_safe[0]) + len(suffixes[0])
|
|
302
311
|
|
|
@@ -307,6 +316,7 @@ class FileBytearray(bytearray):
|
|
|
307
316
|
for suffix in suffixes:
|
|
308
317
|
indices_dict[prefix + search_string + suffix] = lst
|
|
309
318
|
|
|
319
|
+
print_progress = PrintProgress(len(self))
|
|
310
320
|
for offset in range(len(self)):
|
|
311
321
|
cur_bytes = bytes(self[offset:offset + fixed_length])
|
|
312
322
|
if cur_bytes in indices_dict:
|
|
@@ -320,9 +330,3 @@ class FileBytearray(bytearray):
|
|
|
320
330
|
indices.insert(search_index, search_string)
|
|
321
331
|
|
|
322
332
|
return indices
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
def truncate_prev_char(file):
|
|
326
|
-
""" Truncates the last character of a file. """
|
|
327
|
-
file.seek(file.tell() - 1)
|
|
328
|
-
file.truncate()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: strindex
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.4.0
|
|
4
4
|
Summary: A command line utility to extract and patch strings of various filetypes, with a focus on compatibility and translation.
|
|
5
5
|
Home-page: https://github.com/zWolfrost/strindex
|
|
6
6
|
Author: zWolfrost
|
|
@@ -36,7 +36,7 @@ The release includes a precompiled version of the program, which ONLY does the p
|
|
|
36
36
|
## Supported filetypes
|
|
37
37
|
*Support for more types won't be added unless I find some motivation to...*
|
|
38
38
|
- PE files (".exe, ".dll"...)
|
|
39
|
-
- Gamemaker data
|
|
39
|
+
- Gamemaker data files ("data.win")
|
|
40
40
|
|
|
41
41
|
## Installation
|
|
42
42
|
You can install the program with the command `pip install strindex`.
|
|
@@ -48,6 +48,7 @@ These are the available actions:
|
|
|
48
48
|
- `create`: Create a list of strings from a PE file (a strindex). Use `-c` to create a strindex that uses the original strings as references.
|
|
49
49
|
- `patch`: Patch a PE file with a strindex.
|
|
50
50
|
- `patch_gui`: Patch a PE file with a strindex using a GUI.
|
|
51
|
+
- `update` Update a strindex pointers' with the updated version of a file.
|
|
51
52
|
- `filter`: Filter a strindex by detected language, wordlist or length. You can specify those in the strindex settings.
|
|
52
53
|
- `delta`: Create a delta file between two strindexes, that only contains the lines of the first strindex missing in the second one.
|
|
53
54
|
- `spellcheck`: Spellcheck a strindex. You can specify the target language in the strindex settings as an ISO 639-1 code.
|
|
@@ -62,26 +63,6 @@ These are the available actions:
|
|
|
62
63
|
strindex patch program.exe strindex.txt
|
|
63
64
|
```
|
|
64
65
|
|
|
65
|
-
## Strindex
|
|
66
|
-
|
|
67
|
-
{
|
|
68
|
-
"md5": "29ed1f9e450d43815c2d1a0cab168da3",
|
|
69
|
-
|
|
70
|
-
"prefix_bytes": ["24c7442404", "ec04c70424"],
|
|
71
|
-
|
|
72
|
-
"patch_replace": {
|
|
73
|
-
"ì": "í",
|
|
74
|
-
"Ì": "Í",
|
|
75
|
-
"ò": "ó",
|
|
76
|
-
"Ò": "Ó"
|
|
77
|
-
},
|
|
78
|
-
|
|
79
|
-
"clean_pattern": "\\[.*?\\]|\\*",
|
|
80
|
-
"whitelist": ["latin", "spanish", "cyrillic"],
|
|
81
|
-
|
|
82
|
-
"source_language": "es",
|
|
83
|
-
"target_language": "it",
|
|
84
|
-
"among_languages": ["en", "ja", "ko", "de", "fr", "es", "pt", "ru", "zh"]
|
|
85
|
-
}
|
|
86
|
-
```
|
|
66
|
+
## Strindex Example
|
|
67
|
+
You can find an example of a strindex file and an explanation of its settings [here](strindex_example.txt).
|
|
87
68
|
|
|
@@ -11,5 +11,6 @@ strindex.egg-info/dependency_links.txt
|
|
|
11
11
|
strindex.egg-info/entry_points.txt
|
|
12
12
|
strindex.egg-info/requires.txt
|
|
13
13
|
strindex.egg-info/top_level.txt
|
|
14
|
-
strindex/filetypes/
|
|
15
|
-
strindex/filetypes/
|
|
14
|
+
strindex/filetypes/__init__.py
|
|
15
|
+
strindex/filetypes/iff.py
|
|
16
|
+
strindex/filetypes/pe.py
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
from . import strindex
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|