PSHomeCacheDepot / ODC /scripts /ExtractODCInfo.py
pebxcvi's picture
small update
88fb279
import os
import xml.etree.ElementTree as ET
import glob
import re
def count_quotation_marks(text):
return text.count('"')
def replace_special_quotes(text):
if text:
return text.replace('\u201d', '"').replace('\u201e', '"').replace('\u201c', '"')
return text
def parse_odcs(folder_path):
# Define the target languages
langs = ["en-GB", "fr-FR", "de-DE", "it-IT", "es-ES", "en-US", "ko-KR", "en-SG", "zh-HK", "zh-TW", "ja-JP"]
lang_to_territory = {
"en-GB": "SCEE",
"fr-FR": "SCEE",
"de-DE": "SCEE",
"it-IT": "SCEE",
"es-ES": "SCEE",
"en-US": "SCEA",
"ko-KR": "SCEAsia",
"en-SG": "SCEAsia",
"zh-HK": "SCEAsia",
"zh-TW": "SCEAsia",
"ja-JP": "SCEJ"
}
territories = ["SCEE", "SCEA", "SCEAsia", "SCEJ"]
# Define the output files
output_file = "ODCInfo.txt"
flagged_file = "FlaggedODCFiles.txt"
# Open the output files in write mode with utf-8 encoding
with open(output_file, "w", encoding="utf-8") as out_file, open(flagged_file, "w", encoding="utf-8") as flag_file:
# Write header row for output file
header = "file_name|hdk_version|version|uuid|timestamp|lang|default|territory|entitlement_ids|category_ids|product_ids|maker|maker_image|small_image|large_image|scene_entitlement|header|world_map|clans|reward|heat|name|description\n"
out_file.write(header)
# Write header for flagged file
flag_file.write("file_name|lang|problem_field|text\n")
# Iterate over all .odc files in the folder
for file in glob.glob(os.path.join(folder_path, "*.odc")):
try:
# Parse the XML file
tree = ET.parse(file)
root = tree.getroot()
# Function to clean the text by removing line breaks and leading/trailing spaces
def clean_text(text):
if text:
# Replace newline characters with nothing and strip leading/trailing whitespace
return text.replace('\n', '').strip()
return ''
# Extract common elements
hdk_version = root.attrib.get('hdk_version', '')
version = root.find('version').text if root.find('version') is not None else ''
uuid = root.find('uuid').text if root.find('uuid') is not None else ''
timestamp = root.find('timestamp').text if root.find('timestamp') is not None else ''
# Initialize dictionaries to store default values for names and descriptions
name_defaults = {lang: 'false' for lang in langs}
description_defaults = {lang: 'false' for lang in langs}
names = {lang: '' for lang in langs}
descriptions = {lang: '' for lang in langs}
# Extract default values for names and descriptions
for name in root.findall('name'):
lang = name.get('lang', '')
default = name.get('default', 'false')
if lang in name_defaults:
name_defaults[lang] = default
for description in root.findall('description'):
lang = description.get('lang', '')
default = description.get('default', 'false')
if lang in description_defaults:
description_defaults[lang] = default
entitlement_ids = {territory: '' for territory in territories}
category_ids = {territory: '' for territory in territories}
product_ids = {territory: '' for territory in territories}
for entitlement in root.findall('entitlements/entitlement_id'):
territory = entitlement.get('territory', '')
value = clean_text(entitlement.text)
entitlement_ids[territory] = value
for category in root.findall('entitlements/category_id'):
territory = category.get('territory', '')
value = clean_text(category.text)
category_ids[territory] = value
for product in root.findall('entitlements/product_id'):
territory = product.get('territory', '')
value = clean_text(product.text)
product_ids[territory] = value
maker = clean_text(root.find('maker').text) if root.find('maker') is not None else ''
maker = re.sub(r'\s+', ' ', maker).strip()
maker_image = root.find('maker_image').text.replace('[THUMBNAIL_ROOT]', '') if root.find('maker_image') is not None else ''
small_images = {lang: '' for lang in langs}
large_images = {lang: '' for lang in langs}
default_small_image = None
default_large_image = None
for small_img in root.findall('small_image'):
lang = small_img.get('lang', '')
default = small_img.get('default', 'false')
image = small_img.text.replace('[THUMBNAIL_ROOT]', '') if small_img.text else ''
if lang in small_images:
small_images[lang] = image
if default == 'true' and default_small_image is None:
default_small_image = image
for large_img in root.findall('large_image'):
lang = large_img.get('lang', '')
default = large_img.get('default', 'false')
image = large_img.text.replace('[THUMBNAIL_ROOT]', '') if large_img.text else ''
if lang in large_images:
large_images[lang] = image
if default == 'true' and default_large_image is None:
default_large_image = image
# Ensure a default is set if available
if not default_small_image:
default_small_image = next((img for img in small_images.values() if img), '')
if not default_large_image:
default_large_image = next((img for img in large_images.values() if img), '')
data_components = {}
for data in root.findall('data'):
component_name = data.get('component', '')
elements = [f"{elem.get('name', '')}={clean_text(elem.text)}" for elem in data.findall('element')]
data_components[component_name] = '/'.join(elements)
heat_element = root.find('heat')
heat_values_present = False
heat_values = {key: 'n/a' for key in ['main', 'host', 'vram', 'ppu', 'net']}
if heat_element is not None and len(heat_element) > 0:
for elem in heat_element:
if elem.text:
heat_values[elem.tag] = elem.text
heat_values_present = True
heat_data = ''
if heat_values_present:
heat_data = '/'.join([f"{key}={value}" for key, value in heat_values.items()])
for name in root.findall('name'):
lang = name.get('lang', '')
if lang in names:
name_text = clean_text(name.text)
name_text = replace_special_quotes(name_text) # Replace special quotes
# Reduce multiple spaces to a single space
name_text = re.sub(r'\s+', ' ', name_text)
names[lang] = name_text
for description in root.findall('description'):
lang = description.get('lang', '')
if lang in descriptions:
description_text = clean_text(description.text)
description_text = replace_special_quotes(description_text) # Replace special quotes
# Remove actual newlines in the XML
description_text = description_text.replace('\n', '')
# Replace occurrences of literal \n with \\n
description_text = description_text.replace('\\n', '\\\\n')
# Remove spaces between \ and n
description_text = re.sub(r'\\\s+n', '\\n', description_text)
# Remove spaces between \ and N
description_text = re.sub(r'\\\s+N', '\\n', description_text)
# Remove spaces between two \\n
description_text = re.sub(r'(\\\\n)\s+(\\\\n)', '\\1\\2', description_text)
# Reduce consecutive occurrences of \\n to a single instance
#description_text = re.sub(r'(\\\\n)+', '\\\\n', description_text)
# Reduce multiple spaces to a single space
description_text = re.sub(r'\s+', ' ', description_text)
# Replace text literal \\n with text literal \n
description_text = description_text.replace('\\\\n', '\\n')
descriptions[lang] = description_text
# Apply the conditional logic
for lang in langs:
if name_defaults[lang] == 'true' and (description_defaults[lang] == 'false' or description_defaults[lang] == ''):
description_defaults[lang] = 'true'
if description_defaults[lang] == 'true' and (name_defaults[lang] == 'false' or name_defaults[lang] == ''):
name_defaults[lang] = 'true'
# Check for odd number of quotation marks in names and descriptions
for lang in langs:
name = names.get(lang, '')
description = descriptions.get(lang, '')
if count_quotation_marks(name) % 2 != 0:
flag_file.write(f"{os.path.basename(file)}|{lang}|name|{name}\n")
if count_quotation_marks(description) % 2 != 0:
flag_file.write(f"{os.path.basename(file)}|{lang}|description|{description}\n")
# Write data to file for each language
for lang in langs:
territory = lang_to_territory.get(lang, '')
entitlement_id = entitlement_ids.get(territory, '')
category_id = category_ids.get(territory, '')
product_id = product_ids.get(territory, '')
name = names.get(lang, '')
description = descriptions.get(lang, '')
small_image_lang = small_images.get(lang) or default_small_image
large_image_lang = large_images.get(lang) or default_large_image
default_value = f"name={name_defaults[lang]}/description={description_defaults[lang]}"
data_component_values = '|'.join([data_components.get(comp, '') for comp in ['scene_entitlement', 'header', 'world_map', 'clans', 'reward']])
line = f"{os.path.basename(file)}|{hdk_version}|{version}|{uuid}|{timestamp}|{lang}|{default_value}|{territory}|{entitlement_id}|{category_id}|{product_id}|{maker}|{maker_image}|{small_image_lang}|{large_image_lang}|{data_component_values}|{heat_data}|{name}|{description}\n"
out_file.write(line)
except ET.ParseError:
# Log parse errors
print(f"Error parsing file: {file}")
return output_file
def parse_age_ratings(folder_path):
output_file = "ageratings.txt"
max_age_ratings = 0
file_data = []
for file in glob.glob(os.path.join(folder_path, "**", "*.odc"), recursive=True):
try:
print(f"Processing file: {file}")
tree = ET.parse(file)
root = tree.getroot()
legal = root.find('legal')
age_ratings = []
if legal is not None:
age_ratings = ["/".join([f"{attr}={age_rating.get(attr)}" for attr in age_rating.keys()])
for age_rating in legal.findall('age_rating')]
max_age_ratings = max(max_age_ratings, len(age_ratings))
file_data.append((os.path.basename(file), age_ratings))
except ET.ParseError:
print(f"Error parsing file: {file}")
with open(output_file, "w", encoding="utf-8") as out_file:
header = ["file_name"] + [f"age_rating_{i+1}" for i in range(max_age_ratings)]
out_file.write("|".join(header) + "|\n")
for filename, age_ratings in file_data:
age_ratings += [""] * (max_age_ratings - len(age_ratings))
out_file.write(f"{filename}|{'|'.join(age_ratings)}|\n")
return output_file
# Main execution
if __name__ == "__main__":
current_directory = os.getcwd()
target_folder = "ODCsDecrypted"
full_path = os.path.join(current_directory, target_folder)
#output_data = parse_odcs(full_path)
output_data = parse_age_ratings(full_path)
input("Done. Press Enter to exit.")