idk
This commit is contained in:
37
tools/input/csv/CMakeLists.txt
Normal file
37
tools/input/csv/CMakeLists.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
function(dusk_input_csv CSV_FILE)
|
||||
# dusk_csv_to_enum(
|
||||
# ${CSV_FILE}
|
||||
# input/inputactiontype.h
|
||||
# inputaction_t
|
||||
# INPUT_ACTION_
|
||||
# id
|
||||
# )
|
||||
|
||||
# dusk_csv_to_array(
|
||||
# ${CSV_FILE}
|
||||
# input/inputactionname.h
|
||||
# INPUT_ACTION_NAMES
|
||||
# input/inputactiontype.h
|
||||
# INPUT_ACTION_
|
||||
# id
|
||||
# id
|
||||
# )
|
||||
|
||||
dusk_run_python(
|
||||
dusk_input_csv_defs
|
||||
tools.input.csv
|
||||
--csv ${CSV_FILE}
|
||||
--output ${DUSK_GENERATED_HEADERS_DIR}/input/inputactiontype.h
|
||||
)
|
||||
add_dependencies(${DUSK_LIBRARY_TARGET_NAME} dusk_input_csv_defs)
|
||||
|
||||
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
INPUT_CSV_GENERATED=1
|
||||
)
|
||||
endfunction()
|
||||
49
tools/input/csv/__main__.py
Normal file
49
tools/input/csv/__main__.py
Normal file
@@ -0,0 +1,49 @@
|
||||
import argparse
|
||||
import os
|
||||
import csv
|
||||
from tools.util.type import detectType, stringToCType, typeToCType
|
||||
|
||||
parser = argparse.ArgumentParser(description="Input CSV to .h defines")
|
||||
parser.add_argument("--csv", required=True, help="Path to Input CSV file")
|
||||
parser.add_argument("--output", required=True, help="Path to output .h file")
|
||||
args = parser.parse_args()
|
||||
|
||||
def csvIdToEnumName(inputId):
|
||||
return "INPUT_ACTION_" + inputId.upper()
|
||||
|
||||
# Load up CSV file.
|
||||
outHeader = "#pragma once\n"
|
||||
outHeader += '#include "dusk.h"\n\n'
|
||||
|
||||
with open(args.csv, newline="", encoding="utf-8") as csvfile:
|
||||
reader = csv.DictReader(csvfile)
|
||||
|
||||
# CSV must have id column.
|
||||
if "id" not in reader.fieldnames:
|
||||
raise Exception("CSV file must have 'id' column")
|
||||
|
||||
# For each ID
|
||||
inputIds = []
|
||||
for row in reader:
|
||||
inputId = row["id"]
|
||||
if inputId not in inputIds:
|
||||
inputIds.append(inputId)
|
||||
|
||||
# For each ID, create enum entry.
|
||||
outHeader += "typedef enum {\n"
|
||||
outHeader += " INPUT_ACTION_NULL = 0,\n\n"
|
||||
for inputId in inputIds:
|
||||
outHeader += f" {csvIdToEnumName(inputId)},\n"
|
||||
outHeader += f"\n INPUT_ACTION_COUNT\n"
|
||||
outHeader += "} inputaction_t;\n\n"
|
||||
|
||||
# Write IDs to char array.
|
||||
outHeader += f"static const char_t* INPUT_ACTION_IDS[] = {{\n"
|
||||
for inputId in inputIds:
|
||||
outHeader += f" [{csvIdToEnumName(inputId)}] = \"{inputId}\",\n"
|
||||
outHeader += f"}};\n\n"
|
||||
|
||||
# Write to output file.
|
||||
os.makedirs(os.path.dirname(args.output), exist_ok=True)
|
||||
with open(args.output, "w", encoding="utf-8") as outFile:
|
||||
outFile.write(outHeader)
|
||||
Reference in New Issue
Block a user