From 93eb66002865db381be1418d5ea445d5b049559c Mon Sep 17 00:00:00 2001 From: Dendy Faist Date: Fri, 16 Feb 2024 13:48:18 +0100 Subject: [PATCH] Organize the project a little bit better --- config.toml.example | 3 ++- orig.py | 42 ------------------------------------ run.sh | 33 ++++++++++++++++++++++++++++ config.py => src/config.py | 0 src/main.py | 11 ++++++++++ src/orig.py | 44 ++++++++++++++++++++++++++++++++++++++ parser.py => src/parser.py | 18 +++++++--------- 7 files changed, 98 insertions(+), 53 deletions(-) delete mode 100755 orig.py create mode 100755 run.sh rename config.py => src/config.py (100%) create mode 100755 src/main.py create mode 100755 src/orig.py rename parser.py => src/parser.py (92%) diff --git a/config.toml.example b/config.toml.example index be5ace2..85baccd 100644 --- a/config.toml.example +++ b/config.toml.example @@ -1 +1,2 @@ -scripts_path = "/path/ho/higurashi/sdk/Scripts/" +scripts_path = "/path/to/higurashi/sdk/Scripts/" +original_path = "/path/to/onscripter_file.txt" diff --git a/orig.py b/orig.py deleted file mode 100755 index ec7e08f..0000000 --- a/orig.py +++ /dev/null @@ -1,42 +0,0 @@ -#!/usr/bin/env python3 - -import config -import parser -from unidecode import unidecode - -#with open('enctest.txt', 'w', encoding='shift_jisx0213') as fileout: - -japanese_ranges = [ - (0x4E00, 0x9FFF), # Kanji - (0x3040, 0x309F), # Hiragana - (0x30A0, 0x30FF), # Katakana - (0xFF00, 0xFFEF), # Full-width Roman characters and symbols - (0x3000, 0x303F), # CJK symbols and punctuation (including 「」) -] - -translation = parser.main() -outfile = open('out.txt', 'w', encoding='shift_jisx0213') - -with open(config.get('original_path'), 'r', encoding='shift_jisx0213') as file: - for line in file: - found = False - for start, end in japanese_ranges: - if start <= ord(line[0]) <= end: - found = True - - if found and len(translation) > 0: - #outfile.write(unidecode(translation.pop(0)[1]).replace("\\","¥")) - amount = line.count("@") + line.count("¥") - - outfile.write('`') - - for _ in range(amount): - outfile.write(unidecode(translation.pop(0)[1]).replace("\\","¥")) - - if amount > 0: - outfile.write('\n') - else: - outfile.write(line) - - -outfile.close() diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..2b99901 --- /dev/null +++ b/run.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +# --- Check if venv exists, create if not --- + +[ ! -d "./venv/" ] && { + echo -n " - venv not detected, creating... " + /usr/bin/env python3 -m venv venv || { + echo "Failed to initialize virtual environment. Quitting." + exit 1 + } + + echo "OK" + echo "" +} + +pycmd='./venv/bin/python3' + +# --- Make sure the current requirements are met --- + +$pycmd -c "import pkg_resources; pkg_resources.require(open('requirements.txt',mode='r'))" &>/dev/null || { + echo " - Requirements not installed, installing..." + + $pycmd -m pip install -r 'requirements.txt' || { + echo "Errored out while trying to install requirements. Check logs above." + } + + echo "" +} + + +# --- Run the code --- + +$pycmd src/main.py diff --git a/config.py b/src/config.py similarity index 100% rename from config.py rename to src/config.py diff --git a/src/main.py b/src/main.py new file mode 100755 index 0000000..4250090 --- /dev/null +++ b/src/main.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python3 + +import orig + + +def main(): + orig.write_translated() + + +if __name__ == "__main__": + main() diff --git a/src/orig.py b/src/orig.py new file mode 100755 index 0000000..0bfc0a9 --- /dev/null +++ b/src/orig.py @@ -0,0 +1,44 @@ +#!/usr/bin/env python3 + +import config +import parser +from unidecode import unidecode + +# with open('enctest.txt', 'w', encoding='shift_jisx0213') as fileout: + +japanese_ranges = [ + (0x4E00, 0x9FFF), # Kanji + (0x3040, 0x309F), # Hiragana + (0x30A0, 0x30FF), # Katakana + (0xFF00, 0xFFEF), # Full-width Roman characters and symbols + (0x3000, 0x303F), # CJK symbols and punctuation (including 「」) +] + + +def write_translated(): + translation = parser.parse_to_tokens() + outfile = open('out.txt', 'w', encoding='shift_jisx0213') + + with open(config.get('original_path'), 'r', encoding='shift_jisx0213') as file: + for line in file: + found = False + for start, end in japanese_ranges: + if start <= ord(line[0]) <= end: + found = True + + if found and len(translation) > 0: + # outfile.write(unidecode(translation.pop(0)[1]).replace("\\","¥")) + amount = line.count("@") + line.count("¥") + + outfile.write('`') + + for _ in range(amount): + outfile.write( + unidecode(translation.pop(0)[1]).replace("\\", "¥")) + + if amount > 0: + outfile.write('\n') + else: + outfile.write(line) + + outfile.close() diff --git a/parser.py b/src/parser.py similarity index 92% rename from parser.py rename to src/parser.py index cf86027..0ce8e76 100755 --- a/parser.py +++ b/src/parser.py @@ -3,6 +3,7 @@ import os import config + def get_functions_from_file(filepath: str): """ Gets the function calls from a file and returns @@ -27,7 +28,7 @@ def get_functions_from_file(filepath: str): if len(char) != 1: break - #print(currentWord) + # print(currentWord) if char in " \n\t\r": currentWord = "" @@ -40,7 +41,7 @@ def get_functions_from_file(filepath: str): # Skip comments if insideComment: if char == "\n": - #print("Skipped comment....") + # print("Skipped comment....") insideComment = False continue @@ -71,7 +72,6 @@ def get_functions_from_file(filepath: str): currentToken.append(currentTokenParameter[:-1].strip()) currentTokenParameter = "" - if insideToken and char == ")": insideToken = False @@ -84,11 +84,12 @@ def get_functions_from_file(filepath: str): return tokens -def main(): + +def parse_to_tokens(): scripts_path = config.get("scripts_path") tokens = get_functions_from_file( - os.path.join(scripts_path,"onik_000.txt") + os.path.join(scripts_path, "onik_000.txt") ) structure = [] @@ -114,14 +115,11 @@ def main(): and "\\n" in token[2] ): count = token[2].count("\\n") - #structure.append(["LineBreak", count]) + # structure.append(["LineBreak", count]) - #for coso in structure: + # for coso in structure: # if coso[0] == "OutputLine": # print(coso[1], end="") # elif coso[0] == "LineBreak": # print(":".join(["br"] * coso[1])) return structure - -if __name__ == "__main__": - main()