Skip to content

Commit

Permalink
<feature> support renpy 6.99, limit max log lines and improve font re…
Browse files Browse the repository at this point in the history
…place function

update version to v2.4.4
  • Loading branch information
anonymousException committed Jul 16, 2024
1 parent 300ff04 commit 1a350af
Show file tree
Hide file tree
Showing 39 changed files with 1,199 additions and 1,050 deletions.
13 changes: 9 additions & 4 deletions src/custom_engine_form.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@

from custom_engine import Ui_CustomDialog
from custom_translate import CustomTranslate
from my_log import log_path, log_print
from my_log import log_path, log_print, MAX_LOG_LINES
from renpy_translate import language_header, custom_header
from string_tool import tail

targetDic = dict()
sourceDic = dict()
Expand Down Expand Up @@ -314,9 +315,13 @@ def __del__(self):
self.wait()

def run(self):
f = io.open(log_path, 'r+', encoding='utf-8')
self.update_date.emit(f.read())
f.close()
_lines = tail(log_path, MAX_LOG_LINES)
_data = ''
for line in _lines:
_data += line + '\n'
if len(_lines) == MAX_LOG_LINES:
_data += f'log is too large, only show last {str(MAX_LOG_LINES)} lines\n'
self.update_date.emit(_data)

@staticmethod
def get_combobox_content(p, d):
Expand Down
35 changes: 15 additions & 20 deletions src/font_style_template.txt
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
init python early hide:
old_load_face = renpy.text.font.load_face
if 'tl_font_dic' not in globals():
global tl_font_dic
tl_font_dic = dict()
global old_load_face
old_load_face = renpy.text.font.load_face

def my_load_face(fn, *args):
if renpy.game.preferences.language == "{tl_name}":
fn = "{font_path}"
renpy.config.rtl = {is_rtl_enabled}
return old_load_face(fn, *args)
renpy.text.font.load_face = my_load_face


translate {tl_name} python:
import os
import sys
font_path = "{font_path}"
gui.text_font = font_path
gui.button_text_font = font_path
gui.interface_text_font = font_path
gui.choice_button_text_font = font_path
gui.name_text_font = font_path
style.default.font = font_path
gui.headline_text_font = font_path
def my_load_face(fn, *args):
renpy.text.font.free_memory()
for key, value in tl_font_dic.items():
if renpy.game.preferences.language == key:
fn = value[0]
renpy.config.rtl = value[1]
return old_load_face(fn, *args)
renpy.text.font.load_face = my_load_face
global tl_font_dic
tl_font_dic["{tl_name}"] = "{font_path}", {is_rtl_enabled}
31 changes: 29 additions & 2 deletions src/hook_add_change_language_entrance.rpy
Original file line number Diff line number Diff line change
@@ -1,12 +1,39 @@
init python early hide:
import os
global importlib
global inspect
import importlib
import inspect
global check_function_exists
def check_function_exists(module_name, function_name):
try:
module = importlib.import_module(module_name)
function = getattr(module, function_name)
if inspect.isfunction(function):
#print(f"The function '{function_name}' exists in the module '{module_name}'.")
return True
else:
#print(f"The name '{function_name}' exists in the module '{module_name}', but it is not a function.")
return False
except ImportError:
#print(f"The module '{module_name}' does not exist.")
return False
except AttributeError:
#print(f"The function '{function_name}' does not exist in the module '{module_name}'.")
return False

global my_old_show_screen
my_old_show_screen = renpy.show_screen
my_old_lookup = renpy.ast.Translate.lookup
global my_old_lookup
my_old_lookup = None
if check_function_exists('renpy.ast.Translate','lookup'):
my_old_lookup = renpy.ast.Translate.lookup
def my_show_screen(_screen_name, *_args, **kwargs):
if _screen_name == 'preferences':
_screen_name = 'my_preferences'
if _screen_name == 'director':
renpy.ast.Translate.lookup = my_old_lookup
if my_old_lookup is not None:
renpy.ast.Translate.lookup = my_old_lookup
return my_old_show_screen(_screen_name, *_args, **kwargs)
renpy.show_screen = my_show_screen

Expand Down
18 changes: 13 additions & 5 deletions src/hook_unrpa.rpy
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
init python early hide:
import renpy.loader
global threading
global io
global importlib
global inspect
import threading
import io
import importlib
import inspect
import os
import re
from renpy.loader import archives

global unpack_file_threads
unpack_file_threads = []
MAX_UNPACK_THREADS = 12
SCRIPT_ONLY = True
global SCRIPT_ONLY
SCRIPT_ONLY = False
global unpack_semaphore
unpack_semaphore = threading.Semaphore(MAX_UNPACK_THREADS)
non_ascii_file_list = []

global check_function_exists
def check_function_exists(module_name, function_name):
try:
module = importlib.import_module(module_name)
Expand All @@ -30,10 +36,10 @@ init python early hide:
except AttributeError:
#print(f"The function '{function_name}' does not exist in the module '{module_name}'.")
return False

global is_ascii
def is_ascii(string):
return all(ord(char) < 128 for char in string)

global write_out_to_file
def write_out_to_file(semaphore, name, write_path, _write_data):
with semaphore:
if is_ascii(name):
Expand All @@ -45,6 +51,7 @@ init python early hide:
file.write(_write_data)

def my_load_from_archive(name):
global io
load_packed_file_source = None
if check_function_exists('renpy.loader','load_from_archive'):
from renpy.loader import load_from_archive as load_packed_file
Expand Down Expand Up @@ -83,6 +90,7 @@ init python early hide:
os.makedirs(target_dir,exist_ok=True)
else:
os.makedirs(target_dir)
global threading
thread = threading.Thread(target=write_out_to_file, args=([unpack_semaphore, name, path, _read]))
thread.start()
unpack_file_threads.append(thread)
Expand Down
24 changes: 15 additions & 9 deletions src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
QVBoxLayout, QMainWindow, QApplication, QButtonGroup, QLabel, QMessageBox

from html_util import open_directory_and_select_file
from string_tool import EncodeBrackets, isAllPunctuations
from string_tool import EncodeBrackets, isAllPunctuations, tail
from translated_form import MyTranslatedForm

os.environ['REQUESTS_CA_BUNDLE'] = os.path.join(os.path.dirname(sys.argv[0]), 'cacert.pem')
os.environ['NO_PROXY'] = '*'
from copyright import Ui_CopyrightDialog
from my_log import log_print, log_path
from my_log import log_print, log_path, MAX_LOG_LINES
from extraction_official_form import MyExtractionOfficialForm
from html_converter_form import MyHtmlConverterForm
from local_glossary_form import MyLocalGlossaryForm
Expand All @@ -54,8 +54,7 @@
sourceDic = dict()
translator = QTranslator()

VERSION = '2.4.3'

VERSION = '2.4.4'

class MyProxyForm(QDialog, Ui_ProxyDialog):
def __init__(self, parent=None):
Expand Down Expand Up @@ -127,6 +126,7 @@ def __init__(self, parent=None):
self.selectDirBtn.clicked.connect(self.select_directory)
self.translateBtn.clicked.connect(self.translate)
self.clearLogBtn.clicked.connect(self.clear_log)
self.locateLogBtn.clicked.connect(self.locate_log)
self.caller = None
self.translating = False
self.extracting = False
Expand Down Expand Up @@ -567,6 +567,10 @@ def init_combobox(self):
self.targetComboBox.currentTextChanged.connect(self.on_combobox_changed)
self.sourceComboBox.currentTextChanged.connect(self.on_combobox_changed)

@staticmethod
def locate_log():
open_directory_and_select_file(os.getcwd()+'/'+log_path)

@staticmethod
def clear_log():
f = io.open(log_path, 'w', encoding='utf-8')
Expand Down Expand Up @@ -662,11 +666,15 @@ def __del__(self):
self.wait()

def run(self):
f = io.open(log_path, 'r+', encoding='utf-8')
try:
self.update_date.emit(f.read())
_lines = tail(log_path, MAX_LOG_LINES)
_data = ''
for line in _lines:
_data += line + '\n'
if len(_lines) == MAX_LOG_LINES:
_data += f'log is too large, only show last {str(MAX_LOG_LINES)} lines\n'
self.update_date.emit(_data)
except Exception as e:
f.close()
shutil.copyfile(log_path, log_path + '.error.txt')
f = io.open(log_path, 'w')
f.write('Log Format UnicodeEncodeError! Log Cleared')
Expand All @@ -675,8 +683,6 @@ def run(self):
p = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
creationflags=0x08000000, text=True, encoding='utf-8')
p.wait()
f = io.open(log_path, 'r+', encoding='utf-8')
f.close()

def select_file(self):
files, filetype = QFileDialog.getOpenFileNames(self,
Expand Down
2 changes: 1 addition & 1 deletion src/my_log.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import threading
import time
from datetime import datetime

MAX_LOG_LINES = 1000
timestamp = str(int(time.time()))
log_path = 'log'+'.txt'
f=open(log_path,'a+',encoding='utf-8')
Expand Down
Binary file modified src/qm/arabic.qm
Binary file not shown.
Binary file modified src/qm/bengali.qm
Binary file not shown.
Binary file modified src/qm/chinese.qm
Binary file not shown.
Binary file modified src/qm/french.qm
Binary file not shown.
Binary file modified src/qm/german.qm
Binary file not shown.
Binary file modified src/qm/greek.qm
Binary file not shown.
Binary file modified src/qm/hindi.qm
Binary file not shown.
Binary file modified src/qm/japanese.qm
Binary file not shown.
Binary file modified src/qm/korean.qm
Binary file not shown.
Binary file modified src/qm/portuguese.qm
Binary file not shown.
Binary file modified src/qm/russian.qm
Binary file not shown.
Binary file modified src/qm/spanish.qm
Binary file not shown.
Binary file modified src/qm/turkish.qm
Binary file not shown.
Binary file modified src/qm/urdu.qm
Binary file not shown.
9 changes: 3 additions & 6 deletions src/renpy_fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,9 @@ def GenGuiFontsOriginal(p, tl_name, font_path, is_rtl_enabled):
_lines = f.readlines()
f.close()
for idx,_line in enumerate(_lines):
if 'renpy.config.rtl' in _line:
index = _line.find('renpy.config.rtl')
_original = _line[index:]
_is_rtl_enabled = str(is_rtl_enabled)
_replaced = 'renpy.config.rtl = ' + _is_rtl_enabled + '\n'
_lines[idx] = _line.replace(_original, _replaced)
if 'tl_font_dic[' in _line:
new_line = f'tl_font_dic["{tl_name}"] = "{font_path}", {str(is_rtl_enabled)}'
_lines[idx] = new_line
break
f = io.open(guiPath, 'w', encoding='utf-8')
f.writelines(_lines)
Expand Down
8 changes: 8 additions & 0 deletions src/string_tool.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
import string
import collections


def remove_upprintable_chars(s):
Expand Down Expand Up @@ -230,3 +231,10 @@ def replace_unescaped_quotes(text):
pattern = r'(?<!\\)"'
replaced_text = re.sub(pattern, r'\\"', text)
return replaced_text


def tail(filename, n):
last_lines = []
with open(filename, 'r', encoding='utf-8') as file:
last_lines = collections.deque(file, maxlen=n)
return last_lines
Loading

0 comments on commit 1a350af

Please sign in to comment.