From fb93453482d4bd91ded276d8b1044ff74313119c Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 27 Jan 2026 08:40:13 -0600 Subject: [PATCH] Cleaned some tools up --- assets/CMakeLists.txt | 2 - assets/input/CMakeLists.txt | 6 --- assets/item/CMakeLists.txt | 6 --- src/input/CMakeLists.txt | 11 +++++- {assets => src}/input/input.csv | 0 src/input/inputaction.h | 7 +--- src/item/CMakeLists.txt | 10 ++++- assets/item/items.csv => src/item/item.csv | 0 src/script/module/moduleinput.h | 10 +---- src/script/module/moduleitem.h | 8 +--- tools/CMakeLists.txt | 2 - tools/input/CMakeLists.txt | 6 --- tools/input/csv/CMakeLists.txt | 37 ----------------- tools/input/csv/__main__.py | 18 +++++++-- tools/item/CMakeLists.txt | 6 --- tools/item/csv/CMakeLists.txt | 46 ---------------------- tools/item/csv/__main__.py | 29 +++++++++++--- 17 files changed, 61 insertions(+), 143 deletions(-) delete mode 100644 assets/input/CMakeLists.txt delete mode 100644 assets/item/CMakeLists.txt rename {assets => src}/input/input.csv (100%) rename assets/item/items.csv => src/item/item.csv (100%) delete mode 100644 tools/input/CMakeLists.txt delete mode 100644 tools/input/csv/CMakeLists.txt delete mode 100644 tools/item/CMakeLists.txt delete mode 100644 tools/item/csv/CMakeLists.txt diff --git a/assets/CMakeLists.txt b/assets/CMakeLists.txt index 0083360..ce61067 100644 --- a/assets/CMakeLists.txt +++ b/assets/CMakeLists.txt @@ -15,8 +15,6 @@ add_subdirectory(locale) add_asset(SCRIPT init.lua) # Subdirs -add_subdirectory(input) -add_subdirectory(item) add_subdirectory(entity) add_subdirectory(map) add_subdirectory(ui) diff --git a/assets/input/CMakeLists.txt b/assets/input/CMakeLists.txt deleted file mode 100644 index 0c09b54..0000000 --- a/assets/input/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) 2026 Dominic Masters -# -# This software is released under the MIT License. -# https://opensource.org/licenses/MIT - -dusk_input_csv(${CMAKE_CURRENT_LIST_DIR}/input.csv) \ No newline at end of file diff --git a/assets/item/CMakeLists.txt b/assets/item/CMakeLists.txt deleted file mode 100644 index a7a9f47..0000000 --- a/assets/item/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -# Copyright (c) 2026 Dominic Masters -# -# This software is released under the MIT License. -# https://opensource.org/licenses/MIT - -dusk_item_csv(${CMAKE_CURRENT_LIST_DIR}/items.csv) \ No newline at end of file diff --git a/src/input/CMakeLists.txt b/src/input/CMakeLists.txt index 9524354..d03e53a 100644 --- a/src/input/CMakeLists.txt +++ b/src/input/CMakeLists.txt @@ -25,4 +25,13 @@ elseif(DUSK_TARGET_SYSTEM STREQUAL "psp") INPUT_SDL2=1 INPUT_GAMEPAD=1 ) -endif() \ No newline at end of file +endif() + +# CSV +dusk_run_python( + dusk_input_csv_defs + tools.input.csv + --csv ${CMAKE_CURRENT_SOURCE_DIR}/input.csv + --output ${DUSK_GENERATED_HEADERS_DIR}/input/inputactiondefs.h +) +add_dependencies(${DUSK_LIBRARY_TARGET_NAME} dusk_input_csv_defs) \ No newline at end of file diff --git a/assets/input/input.csv b/src/input/input.csv similarity index 100% rename from assets/input/input.csv rename to src/input/input.csv diff --git a/src/input/inputaction.h b/src/input/inputaction.h index 70bd5c7..8aca01e 100644 --- a/src/input/inputaction.h +++ b/src/input/inputaction.h @@ -7,12 +7,7 @@ #pragma once #include "time/time.h" - -#ifndef INPUT_CSV_GENERATED - #error "Input CSV has not been generated. Run dusk_input_csv in CMake." -#endif -#include "input/inputactiontype.h" -// #include "input/inputactionname.h" +#include "input/inputactiondefs.h" typedef struct { inputaction_t action; diff --git a/src/item/CMakeLists.txt b/src/item/CMakeLists.txt index 4c07b80..1a30a79 100644 --- a/src/item/CMakeLists.txt +++ b/src/item/CMakeLists.txt @@ -8,4 +8,12 @@ target_sources(${DUSK_LIBRARY_TARGET_NAME} PUBLIC inventory.c backpack.c -) \ No newline at end of file +) + +dusk_run_python( + dusk_item_csv_defs + tools.item.csv + --csv ${CMAKE_CURRENT_SOURCE_DIR}/item.csv + --output ${DUSK_GENERATED_HEADERS_DIR}/item/item.h +) +add_dependencies(${DUSK_LIBRARY_TARGET_NAME} dusk_item_csv_defs) \ No newline at end of file diff --git a/assets/item/items.csv b/src/item/item.csv similarity index 100% rename from assets/item/items.csv rename to src/item/item.csv diff --git a/src/script/module/moduleinput.h b/src/script/module/moduleinput.h index f62b2e4..21537c9 100644 --- a/src/script/module/moduleinput.h +++ b/src/script/module/moduleinput.h @@ -45,16 +45,10 @@ int moduleInputBind(lua_State *L) { } void moduleInput(scriptcontext_t *context) { - char_t buffer[128]; assertNotNull(context, "Script context cannot be NULL"); - // Set input information - for(inputaction_t act = INPUT_ACTION_NULL + 1; act < INPUT_ACTION_COUNT; 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); - } + // Setup enums. + scriptContextExec(context, INPUT_ACTION_SCRIPT); // Input values. scriptContextExec(context, diff --git a/src/script/module/moduleitem.h b/src/script/module/moduleitem.h index 452c484..968b668 100644 --- a/src/script/module/moduleitem.h +++ b/src/script/module/moduleitem.h @@ -238,16 +238,10 @@ int moduleInventorySort(lua_State *L) { } void moduleItem(scriptcontext_t *context) { - char_t buffer[128]; assertNotNull(context, "Script context cannot be NULL"); // Set item information - for(itemid_t itemId = ITEM_ID_NULL + 1; itemId < ITEM_ID_COUNT; 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); - } + scriptContextExec(context, ITEM_SCRIPT); // Bind BACKPACK const pointer scriptContextRegPointer(context, "BACKPACK", (void *)&BACKPACK); diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 67832a2..72d1f95 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -5,8 +5,6 @@ add_subdirectory(run_python) add_subdirectory(env_to_h) -add_subdirectory(item) -add_subdirectory(input) # Function that adds an asset to be compiled function(add_asset ASSET_TYPE ASSET_PATH) diff --git a/tools/input/CMakeLists.txt b/tools/input/CMakeLists.txt deleted file mode 100644 index 51b63e7..0000000 --- a/tools/input/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -# 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/input/csv/CMakeLists.txt b/tools/input/csv/CMakeLists.txt deleted file mode 100644 index ef1c5e9..0000000 --- a/tools/input/csv/CMakeLists.txt +++ /dev/null @@ -1,37 +0,0 @@ -# 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 index 965530c..66c5a45 100644 --- a/tools/input/csv/__main__.py +++ b/tools/input/csv/__main__.py @@ -24,17 +24,22 @@ with open(args.csv, newline="", encoding="utf-8") as csvfile: # For each ID inputIds = [] + inputIdValues = {} for row in reader: inputId = row["id"] if inputId not in inputIds: inputIds.append(inputId) # For each ID, create enum entry. + count = 0 outHeader += "typedef enum {\n" - outHeader += " INPUT_ACTION_NULL = 0,\n\n" + outHeader += f" INPUT_ACTION_NULL = {count},\n\n" + count += 1 for inputId in inputIds: - outHeader += f" {csvIdToEnumName(inputId)},\n" - outHeader += f"\n INPUT_ACTION_COUNT\n" + inputIdValues[inputId] = count + outHeader += f" {csvIdToEnumName(inputId)} = {count},\n" + count += 1 + outHeader += f"\n INPUT_ACTION_COUNT = {count}\n" outHeader += "} inputaction_t;\n\n" # Write IDs to char array. @@ -43,6 +48,13 @@ with open(args.csv, newline="", encoding="utf-8") as csvfile: outHeader += f" [{csvIdToEnumName(inputId)}] = \"{inputId}\",\n" outHeader += f"}};\n\n" + # Lua Script + outHeader += f"static const char_t *INPUT_ACTION_SCRIPT = \n" + for inputId in inputIds: + # Reference the enum + outHeader += f" \"{csvIdToEnumName(inputId)} = {inputIdValues[inputId]}\\n\"\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: diff --git a/tools/item/CMakeLists.txt b/tools/item/CMakeLists.txt deleted file mode 100644 index 51b63e7..0000000 --- a/tools/item/CMakeLists.txt +++ /dev/null @@ -1,6 +0,0 @@ -# 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 deleted file mode 100644 index d6d6a11..0000000 --- a/tools/item/csv/CMakeLists.txt +++ /dev/null @@ -1,46 +0,0 @@ -# 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 - # ) - - - dusk_run_python( - dusk_item_csv_defs - tools.item.csv - --csv ${CSV_FILE} - --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 8a4fe88..0ab2ca2 100644 --- a/tools/item/csv/__main__.py +++ b/tools/item/csv/__main__.py @@ -42,19 +42,28 @@ with open(args.csv, newline="", encoding="utf-8") as csvfile: outHeader = "#pragma once\n" outHeader += '#include "dusk.h"\n\n' +itemTypeValues = {} +itemIdValues = {} +count = 0 + # Create enum for types and ids, include null and count. outHeader += "typedef enum {\n" -outHeader += " ITEM_TYPE_NULL = 0,\n" +outHeader += f" ITEM_TYPE_NULL = {count},\n" +count += 1 for itemType in itemTypes: - outHeader += f" {csvIdToEnumName(itemType)},\n" -outHeader += f" ITEM_TYPE_COUNT\n" + itemTypeValues[itemType] = count + outHeader += f" {csvIdToEnumName(itemType)} = {count},\n" + count += 1 +outHeader += f" ITEM_TYPE_COUNT = {count}\n" outHeader += "} itemtype_t;\n\n" outHeader += "typedef enum {\n" -outHeader += " ITEM_ID_NULL = 0,\n" +outHeader += f" ITEM_ID_NULL = {count},\n" for itemId in itemIds: - outHeader += f" {csvIdToEnumName(itemId)},\n" -outHeader += f" ITEM_ID_COUNT\n" + itemIdValues[itemId] = count + outHeader += f" {csvIdToEnumName(itemId)} = {count},\n" + count += 1 +outHeader += f" ITEM_ID_COUNT = {count}\n" outHeader += "} itemid_t;\n\n" # Create struct for item data. @@ -76,6 +85,14 @@ for itemId in itemIds: outHeader += f" }},\n" outHeader += f"}};\n\n" +# Create lua script defining items. +outHeader += f"static const char_t *ITEM_SCRIPT = \n" +for itemId in itemIds: + outHeader += f" \"{csvIdToEnumName(itemId)} = {itemIdValues[itemId]}\\n\"\n" +for itemType in itemTypes: + outHeader += f" \"{csvIdToEnumName(itemType)} = {itemTypeValues[itemType]}\\n\"\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: