Skip to content

Commit

Permalink
<feature> correct error translation format after extraction by renpy …
Browse files Browse the repository at this point in the history
…hint

update version to v2.5.7
  • Loading branch information
anonymousException committed Sep 12, 2024
1 parent a706bcc commit 626e09c
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/call_game_python.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,11 @@ def get_py_path(game_path):
def copy_files_under_directory_to_directory(src_dir, desc_dir):
shutil.copytree(src_dir, desc_dir, dirs_exist_ok=True)


def get_game_path_from_game_dir(game_dir):
for item in os.listdir(game_dir):
full_path = os.path.join(game_dir, item)
if os.path.isfile(full_path) and item.lower().endswith('.exe'):
if os.path.isfile(full_path[:-len('.exe')] + '.py'):
return full_path
return None
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
sourceDic = dict()
translator = QTranslator()

VERSION = '2.5.6'
VERSION = '2.5.7'

class MyProxyForm(QDialog, Ui_ProxyDialog):
def __init__(self, parent=None):
Expand Down
6 changes: 5 additions & 1 deletion src/renpy_extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import pathlib

from my_log import log_print
from call_game_python import is_python2_from_game_dir
from call_game_python import is_python2_from_game_dir, get_game_path_from_game_dir
from renpy_lint import fix_translation_by_lint_recursion
from string_tool import remove_upprintable_chars, EncodeBracketContent, EncodeBrackets, replace_all_blank, \
replace_unescaped_quotes

Expand Down Expand Up @@ -572,3 +573,6 @@ def ExtractAllFilesInDir(dirName, is_open_filter, filter_length, is_gen_empty, i
WriteExtracted(dirName, set(), is_open_filter, filter_length, is_gen_empty, is_skip_underline, is_py2)
log_print('start removing repeated extraction, please waiting...')
remove_repeat_extracted_from_tl(dirName, is_py2)
game_path = get_game_path_from_game_dir(dirName + '/../../../')
if game_path is not None:
fix_translation_by_lint_recursion(game_path)
95 changes: 95 additions & 0 deletions src/renpy_lint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import io
import os
import subprocess

from call_game_python import get_python_path_from_game_path, get_py_path
from my_log import log_print


def get_renpy_cmd(game_path):
python_path = get_python_path_from_game_path(game_path)
py_path = get_py_path(game_path)
game_dir = os.path.dirname(game_path)
command = '"' + python_path + '"' + ' -O "' + py_path + '" "' + game_dir + '" lint --error-code'
return command


def exec_renpy_lint(game_path):
command = get_renpy_cmd(game_path)
log_print(command)
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE,
creationflags=0x08000000, text=True, encoding='utf-8')
p.wait()
stdout, stderr = p.communicate()
return stdout, stderr


def fix_translation_by_lint(game_path):
stdout, stderr = exec_renpy_lint(game_path)
if len(stderr) > 0:
log_print(f'call renpy lint error : {stderr}')
return
lines = stdout.splitlines()
is_fixed = False
fix_list = []
for line in lines:
err_file = ''
err_line = -1
if line.endswith('is not terminated with a newline. (Check strings and parenthesis.)') or line.endswith('end of line expected.') or line.endswith('translate statement expects a non-empty block.'):
idx = line.index(', line ')
err_file = line[line.index(' ') + 1:idx].strip('"')
err_line = line[idx + len(', line '): line.index(':', idx)].strip()
err_line = int(err_line) - 1
if line in fix_list:
continue
fix_list.append(line)
if line.startswith('Exception: A translation for '):
idx = line.rindex('already exists at ')
err_content = line[len('Exception: A translation for '):idx].rstrip().strip('"')
idx = idx + len('already exists at ')
err_info = line[idx:].rstrip('.').lstrip()
err_file, err_line = err_info.split(':', 1)
err_line = int(err_line) + 1
if err_info in fix_list:
continue
fix_list.append(err_info)
if err_line == -1:
continue
err_file = os.path.dirname(game_path) + '/' + err_file
if not os.path.isfile(err_file):
log_print('error path : ' + err_file)
f = io.open(err_file, 'r', encoding='utf-8')
_lines = f.readlines()
f.close()
log_print('remove error line ' + str(err_line - 1) + ' in ' + err_file + ' , "' + _lines[err_line] + '"')
if _lines[err_line].startswith(' old ') and _lines[err_line + 1].startswith(' new '):
_lines[err_line] = ''
_lines[err_line + 1] = ''
if _lines[err_line - 1].lstrip().startswith('#'):
_lines[err_line - 1] = ''
elif _lines[err_line].startswith(' new ') and _lines[err_line - 1].startswith(' old '):
_lines[err_line] = ''
_lines[err_line - 1] = ''
if _lines[err_line - 2].lstrip().startswith('#'):
_lines[err_line - 2] = ''
else:
_lines[err_line] = ' ""\n'

f = io.open(err_file, 'w', encoding='utf-8')
f.writelines(_lines)
f.close()
is_fixed = True
return is_fixed


def fix_translation_by_lint_recursion(game_path, max_recursion_depth=512):
cnt = 0
while True:
if not fix_translation_by_lint(game_path):
break
cnt = cnt + 1
if cnt > max_recursion_depth:
break


#fix_translation_by_lint_recursion('F:/Games/RenPy/DemoGame-1.1-dists/DemoGame-1.1-win/DemoGame.exe')

0 comments on commit 626e09c

Please sign in to comment.