diff --git a/src/input/inputaction.c b/src/input/inputaction.c index fc71cc2..daae645 100644 --- a/src/input/inputaction.c +++ b/src/input/inputaction.c @@ -9,14 +9,14 @@ #include "assert/assert.h" #include "util/string.h" -inputaction_t inputActionGetByName(const char_t *name) { - assertNotNull(name, "name must not be NULL"); +// inputaction_t inputActionGetByName(const char_t *name) { +// assertNotNull(name, "name must not be NULL"); - for(inputaction_t i = 0; i < INPUT_ACTION_COUNT; i++) { - if(INPUT_ACTION_NAMES[i] == NULL) continue; - if(stringCompareInsensitive(INPUT_ACTION_NAMES[i], name) != 0) continue; - return i; - } +// for(inputaction_t i = 0; i < INPUT_ACTION_COUNT; i++) { +// if(INPUT_ACTION_IDS[i] == NULL) continue; +// if(stringCompareInsensitive(INPUT_ACTION_IDS[i], name) != 0) continue; +// return i; +// } - return INPUT_ACTION_COUNT; -} \ No newline at end of file +// return INPUT_ACTION_COUNT; +// } \ No newline at end of file diff --git a/src/input/inputaction.h b/src/input/inputaction.h index 2b4de70..70bd5c7 100644 --- a/src/input/inputaction.h +++ b/src/input/inputaction.h @@ -12,7 +12,7 @@ #error "Input CSV has not been generated. Run dusk_input_csv in CMake." #endif #include "input/inputactiontype.h" -#include "input/inputactionname.h" +// #include "input/inputactionname.h" typedef struct { inputaction_t action; @@ -31,4 +31,4 @@ typedef struct { * @param name The name of the input action. * @return The input action, or INPUT_ACTION_COUNT if not found. */ -inputaction_t inputActionGetByName(const char_t *name); \ No newline at end of file +// inputaction_t inputActionGetByName(const char_t *name); \ No newline at end of file diff --git a/src/item/CMakeLists.txt b/src/item/CMakeLists.txt index 2090c3c..4c07b80 100644 --- a/src/item/CMakeLists.txt +++ b/src/item/CMakeLists.txt @@ -6,7 +6,6 @@ # Sources target_sources(${DUSK_LIBRARY_TARGET_NAME} PUBLIC - item.c inventory.c backpack.c ) \ No newline at end of file diff --git a/src/item/inventory.h b/src/item/inventory.h index c802f90..a8fdc2d 100644 --- a/src/item/inventory.h +++ b/src/item/inventory.h @@ -6,7 +6,7 @@ */ #pragma once -#include "item.h" +#include "item/item.h" #define ITEM_STACK_QUANTITY_MAX UINT8_MAX diff --git a/src/item/item.c b/src/item/item.c deleted file mode 100644 index 5c93681..0000000 --- a/src/item/item.c +++ /dev/null @@ -1,29 +0,0 @@ -/** - * Copyright (c) 2026 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#include "item.h" - -const item_t ITEMS[] = { - [ITEM_ID_NULL] = { - .type = ITEM_TYPE_NULL - }, - - // Potion - [ITEM_ID_POTION] = { - .type = ITEM_TYPE_MEDICINE - }, - - // Potato - [ITEM_ID_POTATO] = { - .type = ITEM_TYPE_FOOD - }, - - // Apple - [ITEM_ID_APPLE] = { - .type = ITEM_TYPE_FOOD - } -}; \ No newline at end of file diff --git a/src/item/item.h b/src/item/item.h deleted file mode 100644 index 08f671b..0000000 --- a/src/item/item.h +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright (c) 2026 Dominic Masters - * - * This software is released under the MIT License. - * https://opensource.org/licenses/MIT - */ - -#pragma once - -#ifndef ITEM_CSV_GENERATED -#error "Item CSV has not been generated. Ensure dusk_item_csv is run in CMake." -#endif - -#include "item/itemtype.h" -#include "item/itemid.h" -#include "item/itemname.h" - -// test. -typedef struct { - const char_t *name; - const itemtype_t type; -} item_t; - -extern const item_t ITEMS[]; \ No newline at end of file diff --git a/src/script/module/moduleinput.h b/src/script/module/moduleinput.h index 051ebd5..f62b2e4 100644 --- a/src/script/module/moduleinput.h +++ b/src/script/module/moduleinput.h @@ -50,9 +50,9 @@ void moduleInput(scriptcontext_t *context) { // Set input information for(inputaction_t act = INPUT_ACTION_NULL + 1; act < INPUT_ACTION_COUNT; act++) { - const char_t *actName = INPUT_ACTION_NAMES[act]; - assertStrLenMax(actName, 64, "Input action name too long for buffer"); - snprintf(buffer, sizeof(buffer), "INPUT_ACTION_%s = %d\n", actName, act); + const char_t *actId = INPUT_ACTION_IDS[act]; + assertStrLenMax(actId, 64, "Input action name too long for buffer"); + snprintf(buffer, sizeof(buffer), "INPUT_ACTION_%s = %d\n", actId, act); scriptContextExec(context, buffer); } diff --git a/src/script/module/moduleitem.h b/src/script/module/moduleitem.h index 53a477e..452c484 100644 --- a/src/script/module/moduleitem.h +++ b/src/script/module/moduleitem.h @@ -243,7 +243,7 @@ void moduleItem(scriptcontext_t *context) { // Set item information for(itemid_t itemId = ITEM_ID_NULL + 1; itemId < ITEM_ID_COUNT; itemId++) { - const char_t *itemName = ITEM_NAMES[itemId]; + const char_t *itemName = ITEMS[itemId].name; assertStrLenMax(itemName, 64, "Item name too long for buffer"); snprintf(buffer, sizeof(buffer), "ITEM_ID_%s = %d\n", itemName, itemId); scriptContextExec(context, buffer); diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 3822177..67832a2 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -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) diff --git a/tools/csv_to_array/CMakeLists.txt b/tools/csv_to_array/CMakeLists.txt deleted file mode 100644 index b142555..0000000 --- a/tools/csv_to_array/CMakeLists.txt +++ /dev/null @@ -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() \ No newline at end of file diff --git a/tools/csv_to_array/__main__.py b/tools/csv_to_array/__main__.py deleted file mode 100644 index f13e6ea..0000000 --- a/tools/csv_to_array/__main__.py +++ /dev/null @@ -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) \ No newline at end of file diff --git a/tools/csv_to_enum/CMakeLists.txt b/tools/csv_to_enum/CMakeLists.txt deleted file mode 100644 index 236bb48..0000000 --- a/tools/csv_to_enum/CMakeLists.txt +++ /dev/null @@ -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() \ No newline at end of file diff --git a/tools/csv_to_enum/__main__.py b/tools/csv_to_enum/__main__.py deleted file mode 100644 index d49ce43..0000000 --- a/tools/csv_to_enum/__main__.py +++ /dev/null @@ -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) \ No newline at end of file diff --git a/tools/input/CMakeLists.txt b/tools/input/CMakeLists.txt index 72fc008..51b63e7 100644 --- a/tools/input/CMakeLists.txt +++ b/tools/input/CMakeLists.txt @@ -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() \ No newline at end of file +add_subdirectory(csv) \ No newline at end of file diff --git a/tools/input/csv/CMakeLists.txt b/tools/input/csv/CMakeLists.txt new file mode 100644 index 0000000..ef1c5e9 --- /dev/null +++ b/tools/input/csv/CMakeLists.txt @@ -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() \ No newline at end of file diff --git a/tools/input/csv/__main__.py b/tools/input/csv/__main__.py new file mode 100644 index 0000000..965530c --- /dev/null +++ b/tools/input/csv/__main__.py @@ -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) \ No newline at end of file diff --git a/tools/item/CMakeLists.txt b/tools/item/CMakeLists.txt index 5f01c04..51b63e7 100644 --- a/tools/item/CMakeLists.txt +++ b/tools/item/CMakeLists.txt @@ -1 +1,6 @@ +# Copyright (c) 2026 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + add_subdirectory(csv) \ No newline at end of file diff --git a/tools/item/csv/CMakeLists.txt b/tools/item/csv/CMakeLists.txt index 9e3898d..d6d6a11 100644 --- a/tools/item/csv/CMakeLists.txt +++ b/tools/item/csv/CMakeLists.txt @@ -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() \ No newline at end of file diff --git a/tools/item/csv/__main__.py b/tools/item/csv/__main__.py index f337507..8a4fe88 100644 --- a/tools/item/csv/__main__.py +++ b/tools/item/csv/__main__.py @@ -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") \ No newline at end of file + 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) \ No newline at end of file