idk
This commit is contained in:
@@ -5,8 +5,6 @@
|
||||
|
||||
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)
|
||||
|
||||
|
||||
@@ -1,23 +0,0 @@
|
||||
# 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()
|
||||
@@ -1,65 +0,0 @@
|
||||
import argparse
|
||||
import os
|
||||
import csv
|
||||
from tools.util.type import detectType, stringToCType, typeToCType
|
||||
|
||||
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 = {}
|
||||
valType = None
|
||||
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
|
||||
if valType is None:
|
||||
valType = detectType(value)
|
||||
|
||||
if valType is None:
|
||||
valType = 'String'
|
||||
|
||||
# Now begin generating header.
|
||||
cType = typeToCType(valType)
|
||||
outHeader += f"static const {cType} {args.variable}[] = {{\n"
|
||||
for key in keyValuePairs:
|
||||
outHeader += f" [{args.index_prefix}{key}] = "
|
||||
value = keyValuePairs[key]
|
||||
outHeader += stringToCType(value)
|
||||
outHeader += ",\n"
|
||||
outHeader += "};\n\n"
|
||||
|
||||
# Write to output file
|
||||
with open(args.output, 'w') as outFile:
|
||||
outFile.write(outHeader)
|
||||
@@ -1,35 +0,0 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
function(dusk_csv_to_enum INPUT_PATH OUTPUT_NAME_RELATIVE TYPEDEF PREFIX TAKE_COLUMN)
|
||||
set(NULL_ENTRY TRUE)
|
||||
set(COUNT_ENTRY TRUE)
|
||||
|
||||
# Take NULL_ENTRY and COUNT_ENTRY from ARGN
|
||||
foreach(ARG IN LISTS ARGN)
|
||||
if(ARG STREQUAL "NO_NULL_ENTRY")
|
||||
set(NULL_ENTRY FALSE)
|
||||
elseif(ARG STREQUAL "NO_COUNT_ENTRY")
|
||||
set(COUNT_ENTRY FALSE)
|
||||
endif()
|
||||
endforeach()
|
||||
|
||||
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_enum
|
||||
--csv ${INPUT_PATH}
|
||||
--output ${DUSK_GENERATED_HEADERS_DIR}/${OUTPUT_NAME_RELATIVE}
|
||||
--typedef ${TYPEDEF}
|
||||
--prefix ${PREFIX}
|
||||
--take-column ${TAKE_COLUMN}
|
||||
--null-entry ${NULL_ENTRY}
|
||||
--count-entry ${COUNT_ENTRY}
|
||||
)
|
||||
add_dependencies(${DUSK_LIBRARY_TARGET_NAME} ${DUSK_DEFS_TARGET_NAME})
|
||||
endfunction()
|
||||
@@ -1,50 +0,0 @@
|
||||
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("--prefix", default="", help="Prefix for each define")
|
||||
parser.add_argument("--typedef", required=True, help="Typedef name for the enum")
|
||||
parser.add_argument("--take-column", required=True, help="Column name to take the values for the enum for")
|
||||
parser.add_argument("--null-entry", default=None, help="Optional name for a NULL/INVALID entry")
|
||||
parser.add_argument("--count-entry", default=None, help="Optional name for a COUNT entry")
|
||||
args = parser.parse_args()
|
||||
|
||||
outHeader = "#pragma once\n"
|
||||
outHeader += "#include \"dusk.h\"\n\n"
|
||||
with open(args.csv, newline='') as csvfile:
|
||||
reader = csv.reader(csvfile)
|
||||
|
||||
headers = next(reader)
|
||||
if args.take_column not in headers:
|
||||
raise ValueError(f"Column '{args.take_column}' not found in CSV headers")
|
||||
take_column_index = headers.index(args.take_column)
|
||||
|
||||
# Extract entries
|
||||
entries = []
|
||||
for row in reader:
|
||||
value = row[take_column_index]
|
||||
clean_define_name = value.replace(" ", "_").upper()
|
||||
if clean_define_name in entries:
|
||||
continue # Avoid duplicates
|
||||
entries.append(clean_define_name)
|
||||
|
||||
# Gen enum.
|
||||
outHeader += f"typedef enum {{\n"
|
||||
if args.null_entry == "TRUE":
|
||||
outHeader += f" {args.prefix}NULL = 0,\n\n"
|
||||
|
||||
for entry in entries:
|
||||
outHeader += f" {args.prefix}{entry},\n"
|
||||
|
||||
if args.count_entry == "TRUE":
|
||||
outHeader += f"\n {args.prefix}COUNT,\n"
|
||||
|
||||
outHeader += f"}} {args.typedef};\n\n"
|
||||
|
||||
# Write to output file (mkdirp if needed)
|
||||
os.makedirs(os.path.dirname(args.output), exist_ok=True)
|
||||
with open(args.output, 'w') as outFile:
|
||||
outFile.write(outHeader)
|
||||
@@ -3,27 +3,4 @@
|
||||
# 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()
|
||||
add_subdirectory(csv)
|
||||
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)
|
||||
@@ -1 +1,6 @@
|
||||
# Copyright (c) 2026 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
add_subdirectory(csv)
|
||||
@@ -30,16 +30,17 @@ function(dusk_item_csv CSV_FILE)
|
||||
# id
|
||||
# )
|
||||
|
||||
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
ITEM_CSV_GENERATED=1
|
||||
)
|
||||
|
||||
dusk_run_python(
|
||||
dusk_item_csv_defs
|
||||
tools.item.csv
|
||||
--csv ${CSV_FILE}
|
||||
--output ${DUSK_GENERATED_HEADERS_DIR}/item/itemdefs.h
|
||||
--output ${DUSK_GENERATED_HEADERS_DIR}/item/item.h
|
||||
)
|
||||
add_dependencies(${DUSK_LIBRARY_TARGET_NAME} dusk_item_csv_defs)
|
||||
|
||||
target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME}
|
||||
PUBLIC
|
||||
ITEM_CSV_GENERATED=1
|
||||
)
|
||||
endfunction()
|
||||
@@ -8,12 +8,75 @@ parser.add_argument("--csv", required=True, help="Path to item CSV file")
|
||||
parser.add_argument("--output", required=True, help="Path to output .h file")
|
||||
args = parser.parse_args()
|
||||
|
||||
def csvIdToEnumName(itemId):
|
||||
return "ITEM_ID_" + itemId.upper()
|
||||
|
||||
# Load up CSV file.
|
||||
outHeader = "#pragma once\n"
|
||||
outHeader += "#include \"item/item.h\"\n\n"
|
||||
outHeader += '#include "dusk.h"\n\n'
|
||||
|
||||
itemIds = []
|
||||
itemTypes = []
|
||||
itemRowById = {}
|
||||
|
||||
with open(args.csv, newline="", encoding="utf-8") as csvfile:
|
||||
reader = csv.DictReader(csvfile)
|
||||
|
||||
# CSV must have id and type columns.
|
||||
if "id" not in reader.fieldnames or "type" not in reader.fieldnames:
|
||||
raise Exception("CSV file must have 'id' and 'type' columns")
|
||||
raise Exception("CSV file must have 'id' and 'type' columns")
|
||||
|
||||
for row in reader:
|
||||
itemId = row["id"]
|
||||
itemType = row["type"]
|
||||
|
||||
if itemId not in itemIds:
|
||||
itemIds.append(itemId)
|
||||
|
||||
if itemType not in itemTypes:
|
||||
itemTypes.append(itemType)
|
||||
|
||||
itemRowById[itemId] = row
|
||||
|
||||
# Now Prep output
|
||||
outHeader = "#pragma once\n"
|
||||
outHeader += '#include "dusk.h"\n\n'
|
||||
|
||||
# Create enum for types and ids, include null and count.
|
||||
outHeader += "typedef enum {\n"
|
||||
outHeader += " ITEM_TYPE_NULL = 0,\n"
|
||||
for itemType in itemTypes:
|
||||
outHeader += f" {csvIdToEnumName(itemType)},\n"
|
||||
outHeader += f" ITEM_TYPE_COUNT\n"
|
||||
outHeader += "} itemtype_t;\n\n"
|
||||
|
||||
outHeader += "typedef enum {\n"
|
||||
outHeader += " ITEM_ID_NULL = 0,\n"
|
||||
for itemId in itemIds:
|
||||
outHeader += f" {csvIdToEnumName(itemId)},\n"
|
||||
outHeader += f" ITEM_ID_COUNT\n"
|
||||
outHeader += "} itemid_t;\n\n"
|
||||
|
||||
# Create struct for item data.
|
||||
outHeader += "typedef struct {\n"
|
||||
outHeader += " itemid_t id;\n"
|
||||
outHeader += " itemtype_t type;\n"
|
||||
outHeader += " const char_t *name;\n"
|
||||
outHeader += "} item_t;\n\n"
|
||||
|
||||
# Create array of item data.
|
||||
outHeader += f"static const item_t ITEMS[] = {{\n"
|
||||
for itemId in itemIds:
|
||||
outHeader += f" [{csvIdToEnumName(itemId)}] = {{\n"
|
||||
outHeader += f" .id = {csvIdToEnumName(itemId)},\n"
|
||||
itemType = itemRowById[itemId]["type"]
|
||||
outHeader += f" .type = {csvIdToEnumName(itemType)},\n"
|
||||
itemName = itemRowById[itemId]["id"]
|
||||
outHeader += f" .name = \"{itemName}\",\n"
|
||||
outHeader += f" }},\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