Skip to content

Instantly share code, notes, and snippets.

@haifengkao
Created October 14, 2023 22:03
Show Gist options
  • Save haifengkao/6116312ac2894b98297f5d33ebd75a1c to your computer and use it in GitHub Desktop.
Save haifengkao/6116312ac2894b98297f5d33ebd75a1c to your computer and use it in GitHub Desktop.
convert morrowind plugins' dialogues and scripts to yaml
import os
import subprocess
import sys
# Directory paths
plugins_dir = "./plugins/"
dialogue_dir = "yamls/dialogue/"
script_dir = "yamls/script/"
# Ensure the output directories exist
os.makedirs(dialogue_dir, exist_ok=True)
os.makedirs(script_dir, exist_ok=True)
def safe_remove(file_path):
if os.path.exists(file_path):
os.remove(file_path)
if os.path.exists(file_path):
error_and_exit(f"Failed to remove {file_path}")
def error_and_exit(message):
"""Prints an error message and terminates the script."""
print(f"Error: {message}")
sys.exit(1)
# Function to move generated YAML or directory
def move_generated_data(tmp_file, destination):
if os.path.exists(tmp_file + ".yaml"):
os.rename(tmp_file + ".yaml", destination + ".yaml")
if os.path.exists(tmp_file + ".yaml"): # Verify the move
error_and_exit(f"Failed to move {tmp_file}.yaml")
elif os.path.exists(tmp_file + ".d/"):
os.rename(tmp_file + ".d/", destination + ".d/")
if os.path.exists(tmp_file + ".d/"): # Verify the move
error_and_exit(f"Failed to move directory {tmp_file}.d/")
else:
print(f"Unmatched: {tmp_file}")
# Iterate through all files in the plugins directory
for filename in os.listdir(plugins_dir):
file_ext = None
if filename.lower().endswith(".esp"):
file_ext = ".esp"
elif filename.lower().endswith(".esm"):
file_ext = ".esm"
else:
print("skip" + filename)
if file_ext:
base_name = os.path.splitext(filename)[0].replace('.', '_')
tmp_file_1 = base_name + "_tmp1"
tmp_file_2 = base_name + "_tmp2"
input_path = os.path.join(plugins_dir, filename)
dialogue_output_base = os.path.join(dialogue_dir, base_name)
script_output_base = os.path.join(script_dir, base_name)
# Dialogue processing
if not os.path.exists(dialogue_output_base + ".yaml") and not os.path.exists(dialogue_output_base + ".d/"):
result1 = subprocess.run(["delta_plugin.exe", "filter", "--input", input_path, "--output", tmp_file_1, "match", "Dialogue"])
result2 = subprocess.run(["delta_plugin", "convert", tmp_file_1])
if result1.returncode != 0 or result2.returncode != 0:
error_and_exit(f"Command failed for file: {filename} during dialogue processing")
move_generated_data(tmp_file_1, dialogue_output_base)
safe_remove(tmp_file_1 + ".yaml")
safe_remove(tmp_file_1)
# Script processing
if not os.path.exists(script_output_base + ".yaml") and not os.path.exists(script_output_base + ".d/"):
result1 = subprocess.run(["delta_plugin.exe", "filter", "--input", input_path, "--output", tmp_file_2, "match", "Script"])
result2 = subprocess.run(["delta_plugin", "convert", tmp_file_2])
if result1.returncode != 0 or result2.returncode != 0:
error_and_exit(f"Command failed for file: {filename} during script processing")
move_generated_data(tmp_file_2, script_output_base)
safe_remove(tmp_file_2 + ".yaml")
safe_remove(tmp_file_2)
print("Processing completed.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment