Streamline extraction and JSON analysis scripts

- If directory is not specified, open a file dialog to choose it
- Extract common code into reo_shared.py
This commit is contained in:
Vsevolod Kremianskii 2021-05-31 09:31:22 +07:00
parent 58d40cf1ec
commit b7acbd448f
5 changed files with 101 additions and 29 deletions

1
scripts/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
**/__pycache__

0
scripts/__init__.py Normal file
View file

View file

@ -21,10 +21,14 @@ import re
import sys import sys
from functools import partial from functools import partial
extract_dir = r"D:\OpenKotOR\Extract\KotOR" from reo_shared import *
if not os.path.exists(extract_dir): extract_dir = r"D:\OpenKotOR\Extract\KotORx"
raise RuntimeError("Extraction directory does not exist")
if not is_valid_extract_dir(extract_dir):
extract_dir = choose_directory("Choose an extraction directory")
if not is_valid_extract_dir(extract_dir):
exit(1)
def get_unique_json_values(extract_dir, path_pattern, extract_values): def get_unique_json_values(extract_dir, path_pattern, extract_values):

View file

@ -20,9 +20,9 @@ TODO: support case sensitive paths (Unix)
import glob import glob
import os import os
import platform
import shutil import shutil
import subprocess
from reo_shared import *
game_dir = r"D:\Games\Star Wars - KotOR" game_dir = r"D:\Games\Star Wars - KotOR"
tools_dir = r"D:\Source\reone\build\bin\RelWithDebInfo" tools_dir = r"D:\Source\reone\build\bin\RelWithDebInfo"
@ -42,28 +42,6 @@ steps = [
["convert_to_ascii_pth", "Convert binary PTH to ASCII PTH (y/n)?", None], ["convert_to_ascii_pth", "Convert binary PTH to ASCII PTH (y/n)?", None],
["disassemble_scripts", "Disassemble NCS scripts (y/n)?", None] ] ["disassemble_scripts", "Disassemble NCS scripts (y/n)?", None] ]
if not os.path.exists(game_dir):
raise RuntimeError("Game directory does not exist")
if not os.path.exists(tools_dir):
raise RuntimeError("Tools directory does not exist")
if not os.path.exists(extract_dir):
raise RuntimeError("Extraction directory does not exist")
def append_dir_to_path(dir):
if os.path.exists(dir) and (not dir in os.environ["PATH"]):
separator = ":" if platform.system() == "Linux" else ";"
os.environ["PATH"] = separator.join([os.environ["PATH"], dir])
def run_subprocess(args, silent=True, check_retcode=True):
stdout = subprocess.DEVNULL if silent else None
process = subprocess.run(args, stdout=stdout)
if check_retcode:
process.check_returncode()
def extract_bifs(game_dir, extract_dir): def extract_bifs(game_dir, extract_dir):
# Create destination directory if it does not exist # Create destination directory if it does not exist
@ -223,8 +201,21 @@ def disassemble_scripts(extract_dir):
run_subprocess(["nwnnsscomp", "-d", f, "-o", pcode_path]) run_subprocess(["nwnnsscomp", "-d", f, "-o", pcode_path])
append_dir_to_path(tools_dir) if not is_valid_game_dir(game_dir):
append_dir_to_path(nwnnsscomp_dir) game_dir = choose_directory("Choose a game directory")
if not is_valid_game_dir(game_dir):
exit(1)
if not is_valid_tools_dir(tools_dir):
tools_dir = choose_directory("Choose a tools directory")
if not is_valid_tools_dir(tools_dir):
exit(1)
append_dir_to_path(tools_dir)
if not is_valid_extract_dir(extract_dir):
extract_dir = choose_directory("Choose an extraction directory")
if not is_valid_extract_dir(extract_dir):
exit(1)
for step in steps: for step in steps:
if step[2] is None: if step[2] is None:
@ -265,4 +256,10 @@ for step in steps:
convert_to_ascii_pth(extract_dir) convert_to_ascii_pth(extract_dir)
if step[0] == "disassemble_scripts": if step[0] == "disassemble_scripts":
if not is_valid_script_compiler_dir(nwnnsscomp_dir):
nwnnsscomp_dir = choose_directory("Choose a script compiler directory")
if not is_valid_script_compiler_dir(nwnnsscomp_dir):
exit(1)
append_dir_to_path(nwnnsscomp_dir)
disassemble_scripts(extract_dir) disassemble_scripts(extract_dir)

70
scripts/reo_shared.py Normal file
View file

@ -0,0 +1,70 @@
# Copyright (c) 2020-2021 The reone project contributors
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import os
import platform
import subprocess
import tkinter.filedialog
def is_valid_game_dir(dir):
if not dir or not os.path.isdir(dir):
return False
for f in os.listdir(dir):
if f == "chitin.key":
return True
print("Game directory does not contain a keyfile")
return False
def is_valid_tools_dir(dir):
if not dir or not os.path.isdir(dir):
return False
for f in os.listdir(dir):
if os.path.splitext(f)[0] == "reone-tools":
return True
print("Tools directory does not contain a tools executable")
return False
def is_valid_script_compiler_dir(dir):
if not dir or not os.path.isdir(dir):
return False
for f in os.listdir(dir):
if os.path.splitext(f)[0] == "nwnnsscomp":
return True
print("Script compiler directory does not contain a compiler executable")
return False
def is_valid_extract_dir(dir):
return dir and os.path.isdir(dir)
def choose_directory(title):
return tkinter.filedialog.askdirectory(title=title, mustexist=True)
def append_dir_to_path(dir):
if os.path.exists(dir) and (not dir in os.environ["PATH"]):
separator = ":" if platform.system() == "Linux" else ";"
os.environ["PATH"] = separator.join([os.environ["PATH"], dir])
def run_subprocess(args, silent=True, check_retcode=True):
stdout = subprocess.DEVNULL if silent else None
process = subprocess.run(args, stdout=stdout)
if check_retcode:
process.check_returncode()