Added csv_to_array tool
Some checks failed
Build Dusk / run-tests (push) Failing after 1m41s
Build Dusk / build-linux (push) Failing after 1m26s
Build Dusk / build-psp (push) Failing after 1m46s

This commit is contained in:
2026-01-26 08:37:36 -06:00
parent 9c25fde548
commit 0392dd0e7f
12 changed files with 239 additions and 41 deletions

View File

@@ -6,6 +6,9 @@
add_subdirectory(run_python)
add_subdirectory(env_to_h)
add_subdirectory(csv_to_enum)
add_subdirectory(csv_to_array)
add_subdirectory(item)
add_subdirectory(input)
# Function that adds an asset to be compiled
function(add_asset ASSET_TYPE ASSET_PATH)

View File

@@ -0,0 +1,23 @@
# Copyright (c) 2026 Dominic Masters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
function(dusk_csv_to_array INPUT_PATH OUTPUT_NAME_RELATIVE VARIABLE INDEX_INCLUDE_PATH INDEX_PREFIX INDEX_COLUMN DATA_COLUMN)
set(DUSK_DEFS_TARGET_NAME "DUSK_DEFS_${OUTPUT_NAME_RELATIVE}")
string(REPLACE "." "_" DUSK_DEFS_TARGET_NAME ${DUSK_DEFS_TARGET_NAME})
string(REPLACE "/" "_" DUSK_DEFS_TARGET_NAME ${DUSK_DEFS_TARGET_NAME})
dusk_run_python(
${DUSK_DEFS_TARGET_NAME}
tools.csv_to_array
--csv ${INPUT_PATH}
--output ${DUSK_GENERATED_HEADERS_DIR}/${OUTPUT_NAME_RELATIVE}
--variable ${VARIABLE}
--index-include-path ${INDEX_INCLUDE_PATH}
--index-prefix ${INDEX_PREFIX}
--index-column ${INDEX_COLUMN}
--data-column ${DATA_COLUMN}
)
add_dependencies(${DUSK_LIBRARY_TARGET_NAME} ${DUSK_DEFS_TARGET_NAME})
endfunction()

View File

@@ -0,0 +1,117 @@
import argparse
import os
import csv
parser = argparse.ArgumentParser(description="Convert CSV to .h defines")
parser.add_argument("--csv", required=True, help="Path to CSV file")
parser.add_argument("--output", required=True, help="Path to output .h file")
parser.add_argument("--variable", required=True, help="Name of generated array variable")
parser.add_argument('--index-include-path', required=False, default='', help="Include path for index defines")
parser.add_argument('--index-prefix', required=False, default='', help="Prefix for index defines")
parser.add_argument('--index-column', required=True, help="Column name for index values")
parser.add_argument('--data-column', required=True, help="Column name for data values")
args = parser.parse_args()
# Start Header
outHeader = "#pragma once\n"
outHeader += "#include \"dusk.h\"\n"
if args.index_include_path:
outHeader += f'#include "{args.index_include_path}"\n'
outHeader += "\n"
# Read CSV file
with open(args.csv, newline='') as csvfile:
reader = csv.reader(csvfile)
headers = next(reader)
# Ensure both index and data columns exist.
if args.index_column not in headers:
raise ValueError(f"Index column '{args.index_column}' not found in CSV headers")
if args.data_column not in headers:
raise ValueError(f"Data column '{args.data_column}' not found in CSV headers")
# Create key-value pairs from CSV rows.
index_col_idx = headers.index(args.index_column)
data_col_idx = headers.index(args.data_column)
keyValuePairs = {}
for row in reader:
key = row[index_col_idx]
value = row[data_col_idx]
# Don't allow duplicate keys.
if key in keyValuePairs:
raise ValueError(f"Duplicate key '{key}' found in CSV")
keyValuePairs[key] = value
# Determine type. Can be float, int, bool, or string.
isFloat = False
isInt = False
isBool = False
isString = False
# If there's no entries, assume string type.
if len(keyValuePairs) == 0:
isString = True
else:
allInts = True
allFloats = True
# Check if ALL values can be parsed as int or float.
for value in keyValuePairs.values():
if allInts:
try:
int(value)
except:
allInts = False
if allFloats:
try:
float(value)
except:
allFloats = False
if not allInts and not allFloats:
break
if allInts:
isInt = True
elif allFloats:
isFloat = True
else:
# Not all floats/ints, probably string or bool, check first entry
firstValue = next(iter(keyValuePairs.values()))
if firstValue.lower() in ['true', 'false']:
isBool = True
else:
isString = True
typeString = ""
if isFloat:
typeString = "float"
elif isInt:
typeString = "int"
elif isBool:
typeString = "bool"
elif isString:
typeString = "char*"
# Now begin generating header.
outHeader += f"static const {typeString} {args.variable}[] = {{\n"
for key in keyValuePairs:
outHeader += f" [{args.index_prefix}{key}] = "
value = keyValuePairs[key]
if isFloat:
outHeader += str(float(value))
elif isInt:
outHeader += str(int(value))
elif isBool:
asBool = 'true' if value.lower() == 'true' else 'false'
outHeader += asBool
elif isString:
escaped = value.replace('\\', '\\\\').replace('"', '\\"')
outHeader += f'"{escaped}"'
outHeader += ",\n"
outHeader += "};\n\n"
# Write to output file
with open(args.output, 'w') as outFile:
outFile.write(outHeader)

View File

@@ -12,7 +12,7 @@ parser.add_argument("--null-entry", default=None, help="Optional name for a NULL
parser.add_argument("--count-entry", default=None, help="Optional name for a COUNT entry")
args = parser.parse_args()
outHeader = ""
outHeader = "#pragma once\n"
outHeader += "#include \"dusk.h\"\n\n"
with open(args.csv, newline='') as csvfile:
reader = csv.reader(csvfile)
@@ -34,13 +34,13 @@ with open(args.csv, newline='') as csvfile:
# Gen enum.
outHeader += f"typedef enum {{\n"
if args.null_entry == "TRUE":
outHeader += f" {args.prefix}_NULL = 0,\n\n"
outHeader += f" {args.prefix}NULL = 0,\n\n"
for entry in entries:
outHeader += f" {args.prefix}_{entry},\n"
outHeader += f" {args.prefix}{entry},\n"
if args.count_entry == "TRUE":
outHeader += f"\n {args.prefix}_COUNT,\n"
outHeader += f"\n {args.prefix}COUNT,\n"
outHeader += f"}} {args.typedef};\n\n"

View File

@@ -0,0 +1,29 @@
# 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
)
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
INPUT_CSV_GENERATED=1
)
endfunction()

37
tools/item/CMakeLists.txt Normal file
View 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_item_csv CSV_FILE)
dusk_csv_to_enum(
${CSV_FILE}
item/itemtype.h
itemtype_t
ITEM_TYPE_
type
)
dusk_csv_to_enum(
${CSV_FILE}
item/itemid.h
itemid_t
ITEM_ID_
id
)
dusk_csv_to_array(
${CSV_FILE}
item/itemname.h
ITEM_NAMES
item/itemid.h
ITEM_ID_
id
id
)
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME}
PUBLIC
ITEM_CSV_GENERATED=1
)
endfunction()