import sys, os import argparse from datetime import datetime import json # Check if the script is run with the correct arguments parser = argparse.ArgumentParser(description="Generate event header files") parser.add_argument('--output', required=True, help='Dir to write headers') parser.add_argument('--input', required=True, help='Input directory containing event files') args = parser.parse_args() # Ensure outdir exists outputFile = args.output outputDir = args.output os.makedirs(outputDir, exist_ok=True) inputDir = args.input # Scan for .json files in the input directory if not os.path.exists(inputDir): print(f"Error: Input directory '{inputDir}' does not exist.") sys.exit(1) jsonFiles = [f for f in os.listdir(inputDir) if f.endswith('.json')] if not jsonFiles or len(jsonFiles) == 0: print(f"Error: No JSON files found in '{inputDir}'.") sys.exit(1) # For each language file... now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") eventFiles = [] for jsonFile in jsonFiles: inputFile = os.path.join(inputDir, jsonFile) data = json.load(open(inputFile, 'r', encoding='utf-8')) if 'key' not in data: print(f"Error: JSON file '{inputFile}' does not contain 'key' field.") sys.exit(1) if 'items' not in data or not isinstance(data['items'], list) or len(data['items']) == 0: print(f"Error: JSON file '{inputFile}' does not contain 'items' field.") sys.exit(1) key = data['key'] keyUpper = key.upper() if jsonFile != f'{key}.json': print(f"Error: JSON file '{jsonFile}' does not match expected filename '{key}.json'.") sys.exit(1) outputFile = os.path.join(outputDir, f"{key}.h") with open(outputFile, 'w', encoding='utf-8') as f: f.write(f"// Generated event header for {jsonFile}\n") f.write(f"// Generated at {now}\n") f.write("#pragma once\n\n") f.write("#include \"event/event.h\"\n\n") f.write(f"static const event_t EVENT_{key.upper()} = {{\n") f.write(f" .itemCount = {len(data['items'])},\n") f.write(f" .items = {{\n") for i, item in enumerate(data['items']): if 'type' not in item: print(f"Error: Item {i} in '{jsonFile}' does not contain 'type' field.") sys.exit(1) itemType = item['type'] f.write(f" {{\n") # Text(s) Type if itemType == 'text': if 'texts' not in item or not isinstance(item['texts'], list) or len(item['texts']) == 0: print(f"Error: Item {i} in '{jsonFile}' of type 'text' does not contain 'texts' field.") sys.exit(1) f.write(f" .type = EVENT_TYPE_TEXT,\n") f.write(f" .texts = {{\n") for text in item['texts']: f.write(f" \"{text}\",\n") f.write(f" }},\n") else: print(f"Error: Unknown item type '{itemType}' in item {i} of '{jsonFile}'.") sys.exit(1) f.write(f" }},\n") f.write(f" }},\n") f.write(f"}};\n\n") eventFiles.append(key)