Events starting
This commit is contained in:
@ -4,7 +4,8 @@
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
# Tools
|
||||
add_subdirectory(mapcompile)
|
||||
add_subdirectory(tilecompile)
|
||||
add_subdirectory(eventcompile)
|
||||
add_subdirectory(fontcompile)
|
||||
add_subdirectory(languagecompile)
|
||||
add_subdirectory(languagecompile)
|
||||
add_subdirectory(mapcompile)
|
||||
add_subdirectory(tilecompile)
|
20
tools/eventcompile/CMakeLists.txt
Normal file
20
tools/eventcompile/CMakeLists.txt
Normal file
@ -0,0 +1,20 @@
|
||||
# Copyright (c) 2025 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
find_package(Python3 COMPONENTS Interpreter REQUIRED)
|
||||
|
||||
# Custom command to generate all header files
|
||||
add_custom_target(DUSK_EVENTS
|
||||
COMMAND
|
||||
${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/eventcompile.py
|
||||
--output ${DUSK_GENERATED_HEADERS_DIR}/event/
|
||||
--input ${DUSK_DATA_DIR}/events/
|
||||
DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/eventcompile.py
|
||||
COMMENT "Generating event header files"
|
||||
VERBATIM
|
||||
)
|
||||
|
||||
# Ensure headers are generated before compiling main
|
||||
add_dependencies(${DUSK_TARGET_NAME} DUSK_EVENTS)
|
90
tools/eventcompile/eventcompile.py
Normal file
90
tools/eventcompile/eventcompile.py
Normal file
@ -0,0 +1,90 @@
|
||||
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)
|
Reference in New Issue
Block a user